KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > velocity > TurbineVelocity


1 package org.apache.fulcrum.velocity;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.OutputStream JavaDoc;
58 import java.io.Writer JavaDoc;
59 import org.apache.fulcrum.TurbineServices;
60 import org.apache.fulcrum.ServiceException;
61 import org.apache.velocity.context.Context;
62
63 /**
64  * This is a simple static accessor to common Velocity tasks such as
65  * getting an instance of a context as well as handling a request for
66  * processing a template.
67  * <pre>
68  * Context context = TurbineVelocity.getContext(data);
69  * context.put("message", "Hello from Turbine!");
70  * String results = TurbineVelocity.handleRequest(context, "helloWorld.vm");
71  * data.getPage().getBody().addElement(results);
72  * </pre>
73  *
74  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
75  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
76  * @author <a HREF="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
77  * @author <a HREF="mailto:stack@collab.net">Michael Stack</a>
78  * @version $Id: TurbineVelocity.java,v 1.1 2004/11/12 10:25:47 epugh Exp $
79  */

80 public abstract class TurbineVelocity
81 {
82     /**
83      * Utility method for accessing the service
84      * implementation
85      *
86      * @return a VelocityService implementation instance
87      */

88     protected static VelocityService getService()
89     {
90         return (VelocityService)TurbineServices
91             .getInstance().getService(VelocityService.SERVICE_NAME);
92     }
93
94     /**
95      * This allows you to pass in a context and a path to a template
96      * file and then grabs an instance of the velocity service and
97      * processes the template and returns the results as a String
98      * object.
99      *
100      * @param context A Context.
101      * @param template The path to the template file.
102      * @return The processed template.
103      * @exception Exception Error processing template.
104      */

105     public static String JavaDoc handleRequest(Context context, String JavaDoc template)
106         throws Exception JavaDoc
107     {
108         return getService().handleRequest(context, template);
109     }
110
111     /**
112      * @see org.apache.fulcrum.velocity.VelocityService#handleRequest(Context,
113      * String, String, String)
114      */

115     public String JavaDoc handleRequest(Context context, String JavaDoc template,
116                                 String JavaDoc charset, String JavaDoc encoding)
117         throws Exception JavaDoc
118     {
119         return getService().handleRequest(context, template, charset,
120                                           encoding);
121     }
122
123     /**
124      * Process the request and fill in the template with the values
125      * you set in the Context.
126      *
127      * @param context A Context.
128      * @param filename A String with the filename of the template.
129      * @param out A OutputStream where we will write the process template as
130      * a String.
131      *
132      * @exception Exception Error processing template.
133      *
134      * @see org.apache.fulcrum.velocity.VelocityService#handleRequest(Context,
135      * String, OutputStream)
136      */

137     public static void handleRequest(Context context, String JavaDoc template,
138                                      OutputStream JavaDoc out)
139         throws Exception JavaDoc
140     {
141         getService().handleRequest(context, template, out);
142     }
143
144     /**
145      * Process the request and fill in the template with the values
146      * you set in the Context.
147      *
148      * @param context A Context.
149      * @param template The path to the template file.
150      * @param out A OutputStream where we will write the process template as
151      * a String.
152      * @param charset The character set to use when writing the result.
153      * @param encoding The encoding to use when merging context and template.
154      *
155      * @exception Exception Error processing template.
156      *
157      * @see org.apache.fulcrum.velocity.VelocityService#handleRequest(Context,
158      * String, OutputStream)
159      */

160     public static void handleRequest(Context context, String JavaDoc template,
161                                      OutputStream JavaDoc out, String JavaDoc charset,
162                                      String JavaDoc encoding)
163         throws Exception JavaDoc
164     {
165         getService().handleRequest(context, template, out, charset, encoding);
166     }
167
168     /**
169      * @see org.apache.fulcrum.velocity.VelocityService#handleRequest(Context,
170      * String, Writer)
171      */

172     public static void handleRequest(Context context, String JavaDoc filename,
173                                      Writer JavaDoc writer)
174         throws ServiceException
175     {
176         getService().handleRequest(context, filename, writer, null);
177     }
178
179     /**
180      * @see org.apache.fulcrum.velocity.VelocityService#handleRequest(Context,
181      * String, Writer, String)
182      */

183     public static void handleRequest(Context context, String JavaDoc filename,
184                                      Writer JavaDoc writer, String JavaDoc encoding)
185         throws ServiceException
186     {
187         getService().handleRequest(context, filename, writer, encoding);
188     }
189 }
190
Popular Tags