001package org.apache.turbine.services.velocity;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import java.io.OutputStream;
025import java.io.Writer;
026
027import org.apache.turbine.pipeline.PipelineData;
028import org.apache.turbine.services.TurbineServices;
029import org.apache.velocity.context.Context;
030
031/**
032 * This is a simple static accessor to common Velocity tasks such as
033 * getting an instance of a context as well as handling a request for
034 * processing a template.
035 * <pre>
036 * Context context = TurbineVelocity.getContext(data);
037 * context.put("message", "Hello from Turbine!");
038 * String results = TurbineVelocity.handleRequest(context, "helloWorld.vm");
039 * data.getPage().getBody().addElement(results);
040 * </pre>
041 *
042 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
043 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
044 * @author <a href="mailto:jvanzyl@periapt.com.com">Jason van Zyl</a>
045 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
046 * @version $Id: TurbineVelocity.java 1695634 2015-08-13 00:35:47Z tv $
047 */
048public abstract class TurbineVelocity
049{
050    /**
051     * Utility method for accessing the service
052     * implementation
053     *
054     * @return a VelocityService implementation instance
055     */
056    public static VelocityService getService()
057    {
058        return (VelocityService) TurbineServices
059                .getInstance().getService(VelocityService.SERVICE_NAME);
060    }
061
062    /**
063     * This allows you to pass in a context and a path to a template
064     * file and then grabs an instance of the velocity service and
065     * processes the template and returns the results as a String
066     * object.
067     *
068     * @param context A Context.
069     * @param template The path for the template files.
070     * @return A String.
071     * @exception Exception a generic exception.
072     */
073    public static String handleRequest(Context context, String template)
074            throws Exception
075    {
076        return getService().handleRequest(context, template);
077    }
078
079    /**
080     * Process the request and fill in the template with the values
081     * you set in the Context.
082     *
083     * @param context A Context.
084     * @param template A String with the filename of the template.
085     * @param out A OutputStream where we will write the process template as
086     * a String.
087     * @exception Exception a generic exception.
088     */
089    public static void handleRequest(Context context, String template,
090                                     OutputStream out)
091            throws Exception
092    {
093        getService().handleRequest(context, template, out);
094    }
095
096    /**
097     * Process the request and fill in the template with the values
098     * you set in the Context.
099     *
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}