001: package org.apache.turbine.services.velocity;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.OutputStream;
020: import java.io.Writer;
021:
022: import org.apache.turbine.services.TurbineServices;
023: import org.apache.turbine.util.RunData;
024:
025: import org.apache.velocity.context.Context;
026:
027: /**
028: * This is a simple static accessor to common Velocity tasks such as
029: * getting an instance of a context as well as handling a request for
030: * processing a template.
031: * <pre>
032: * Context context = TurbineVelocity.getContext(data);
033: * context.put("message", "Hello from Turbine!");
034: * String results = TurbineVelocity.handleRequest(context, "helloWorld.vm");
035: * data.getPage().getBody().addElement(results);
036: * </pre>
037: *
038: * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
039: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
040: * @author <a href="mailto:jvanzyl@periapt.com.com">Jason van Zyl</a>
041: * @version $Id: TurbineVelocity.java 264148 2005-08-29 14:21:04Z henning $
042: */
043: public abstract class TurbineVelocity {
044: /**
045: * Utility method for accessing the service
046: * implementation
047: *
048: * @return a VelocityService implementation instance
049: */
050: public static VelocityService getService() {
051: return (VelocityService) TurbineServices.getInstance()
052: .getService(VelocityService.SERVICE_NAME);
053: }
054:
055: /**
056: * This allows you to pass in a context and a path to a template
057: * file and then grabs an instance of the velocity service and
058: * processes the template and returns the results as a String
059: * object.
060: *
061: * @param context A Context.
062: * @param template The path for the template files.
063: * @return A String.
064: * @exception Exception a generic exception.
065: */
066: public static String handleRequest(Context context, String template)
067: throws Exception {
068: return getService().handleRequest(context, template);
069: }
070:
071: /**
072: * Process the request and fill in the template with the values
073: * you set in the Context.
074: *
075: * @param context A Context.
076: * @param template A String with the filename of the template.
077: * @param out A OutputStream where we will write the process template as
078: * a String.
079: * @exception Exception a generic exception.
080: */
081: public static void handleRequest(Context context, String template,
082: OutputStream out) throws Exception {
083: getService().handleRequest(context, template, out);
084: }
085:
086: /**
087: * Process the request and fill in the template with the values
088: * you set in the Context.
089: *
090: * @param context A Context.
091: * @param template A String with the filename of the template.
092: * @param writer A Writer where we will write the process template as
093: * a String.
094: * @exception Exception a generic exception.
095: */
096: public static void handleRequest(Context context, String template,
097: Writer writer) throws Exception {
098: getService().handleRequest(context, template, writer);
099: }
100:
101: /**
102: * This returns a Context that you can pass into handleRequest
103: * once you have populated it with information that the template
104: * will know about.
105: *
106: * @param data A Turbine RunData.
107: * @return A Context.
108: */
109: public static Context getContext(RunData data) {
110: return getService().getContext(data);
111: }
112:
113: /**
114: * This method returns a blank Context object, which
115: * also contains the global context object. Do not use
116: * this method if you need an empty context object! Use
117: * getNewContext for this.
118: *
119: * @return A WebContext.
120: */
121: public static Context getContext() {
122: return getService().getContext();
123: }
124:
125: /**
126: * This method returns a new, empty Context object.
127: *
128: * @return A WebContext.
129: */
130: public static Context getNewContext() {
131: return getService().getNewContext();
132: }
133:
134: /**
135: * Performs post-request actions (releases context
136: * tools back to the object pool).
137: *
138: * @param context a Velocity Context
139: */
140: public static void requestFinished(Context context) {
141: getService().requestFinished(context);
142: }
143: }
|