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