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