View Javadoc

1   package org.apache.turbine.services.velocity;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.OutputStream;
23  import java.io.Writer;
24  
25  import org.apache.turbine.services.TurbineServices;
26  import org.apache.turbine.util.RunData;
27  
28  import org.apache.velocity.context.Context;
29  
30  /***
31   * This is a simple static accessor to common Velocity tasks such as
32   * getting an instance of a context as well as handling a request for
33   * processing a template.
34   * <pre>
35   * Context context = TurbineVelocity.getContext(data);
36   * context.put("message", "Hello from Turbine!");
37   * String results = TurbineVelocity.handleRequest(context, "helloWorld.vm");
38   * data.getPage().getBody().addElement(results);
39   * </pre>
40   *
41   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
42   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
43   * @author <a href="mailto:jvanzyl@periapt.com.com">Jason van Zyl</a>
44   * @version $Id: TurbineVelocity.java 534527 2007-05-02 16:10:59Z tv $
45   */
46  public abstract class TurbineVelocity
47  {
48      /***
49       * Utility method for accessing the service
50       * implementation
51       *
52       * @return a VelocityService implementation instance
53       */
54      public static VelocityService getService()
55      {
56          return (VelocityService) TurbineServices
57                  .getInstance().getService(VelocityService.SERVICE_NAME);
58      }
59  
60      /***
61       * This allows you to pass in a context and a path to a template
62       * file and then grabs an instance of the velocity service and
63       * processes the template and returns the results as a String
64       * object.
65       *
66       * @param context A Context.
67       * @param template The path for the template files.
68       * @return A String.
69       * @exception Exception a generic exception.
70       */
71      public static String handleRequest(Context context, String template)
72              throws Exception
73      {
74          return getService().handleRequest(context, template);
75      }
76  
77      /***
78       * Process the request and fill in the template with the values
79       * you set in the Context.
80       *
81       * @param context A Context.
82       * @param template A String with the filename of the template.
83       * @param out A OutputStream where we will write the process template as
84       * a String.
85       * @exception Exception a generic exception.
86       */
87      public static void handleRequest(Context context, String template,
88                                       OutputStream out)
89              throws Exception
90      {
91          getService().handleRequest(context, template, out);
92      }
93  
94      /***
95       * Process the request and fill in the template with the values
96       * you set in the Context.
97       *
98       * @param context A Context.
99       * @param template A String with the filename of the template.
100      * @param writer A Writer where we will write the process template as
101      * a String.
102      * @exception Exception a generic exception.
103      */
104     public static void handleRequest(Context context,
105                                      String template,
106                                      Writer writer)
107             throws Exception
108     {
109         getService().handleRequest(context, template, writer);
110     }
111 
112     /***
113      * This returns a Context that you can pass into handleRequest
114      * once you have populated it with information that the template
115      * will know about.
116      *
117      * @param data A Turbine RunData.
118      * @return A Context.
119      */
120     public static Context getContext(RunData data)
121     {
122         return getService().getContext(data);
123     }
124 
125     /***
126      * This method returns a blank Context object, which
127      * also contains the global context object. Do not use
128      * this method if you need an empty context object! Use
129      * getNewContext for this.
130      *
131      * @return A WebContext.
132      */
133     public static Context getContext()
134     {
135         return getService().getContext();
136     }
137 
138     /***
139      * This method returns a new, empty Context object.
140      *
141      * @return A WebContext.
142      */
143     public static Context getNewContext()
144     {
145         return getService().getNewContext();
146     }
147 
148     /***
149      * Performs post-request actions (releases context
150      * tools back to the object pool).
151      *
152      * @param context a Velocity Context
153      */
154     public static void requestFinished(Context context)
155     {
156         getService().requestFinished(context);
157     }
158 }