Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
VelocityServiceFacade |
|
| 1.0;1 |
1 | package org.apache.fulcrum.template.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.fulcrum.template.TemplateException; | |
28 | import org.apache.velocity.context.Context; | |
29 | ||
30 | /** | |
31 | * This is a simple static accessor to common Velocity tasks such as | |
32 | * getting an instance of a context as well as handling a request for | |
33 | * processing a template. | |
34 | * <pre> | |
35 | * Context context = VelocityServiceFacade.getContext(data); | |
36 | * context.put("message", "Hello from Turbine!"); | |
37 | * String results = VelocityServiceFacade.handleRequest(context, "helloWorld.vm"); | |
38 | * data.getPage().getBody().addElement(results); | |
39 | * </pre> | |
40 | * | |
41 | * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a> | |
42 | * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> | |
43 | * @author <a href="mailto:jvanzyl@zenplex.com">Jason van Zyl</a> | |
44 | * @author <a href="mailto:stack@collab.net">Michael Stack</a> | |
45 | * @version $Id: VelocityServiceFacade.java 535465 2007-05-05 06:58:06Z tv $ | |
46 | */ | |
47 | 0 | public class VelocityServiceFacade |
48 | { | |
49 | private static VelocityService velocityService; | |
50 | /** | |
51 | * Utility method for accessing the service | |
52 | * implementation | |
53 | * | |
54 | * @return a VelocityService implementation instance | |
55 | */ | |
56 | protected static VelocityService getService() | |
57 | { | |
58 | 0 | return velocityService; |
59 | } | |
60 | ||
61 | protected static void setService(VelocityService velocityService){ | |
62 | 0 | VelocityServiceFacade.velocityService = velocityService; |
63 | 0 | } |
64 | ||
65 | /** | |
66 | * This allows you to pass in a context and a path to a template | |
67 | * file and then grabs an instance of the velocity service and | |
68 | * processes the template and returns the results as a String | |
69 | * object. | |
70 | * | |
71 | * @param context A Context. | |
72 | * @param template The path to the template file. | |
73 | * @return The processed template. | |
74 | * @exception Exception Error processing template. | |
75 | */ | |
76 | public static String handleRequest(Context context, String template) | |
77 | throws TemplateException | |
78 | { | |
79 | 0 | return getService().handleRequest(context, template); |
80 | } | |
81 | ||
82 | /** | |
83 | * @see org.apache.fulcrum.velocity.VelocityService#handleRequest(Context, | |
84 | * String, String, String) | |
85 | */ | |
86 | public String handleRequest(Context context, String template, | |
87 | String charset, String encoding) | |
88 | throws TemplateException | |
89 | { | |
90 | 0 | return getService().handleRequest(context, template, charset, |
91 | encoding); | |
92 | } | |
93 | ||
94 | /** | |
95 | * Process the request and fill in the template with the values | |
96 | * you set in the Context. | |
97 | * | |
98 | * @param context A Context. | |
99 | * @param filename A String with the filename of the template. | |
100 | * @param out A OutputStream where we will write the process template as | |
101 | * a String. | |
102 | * | |
103 | * @exception Exception Error processing template. | |
104 | * | |
105 | * @see org.apache.fulcrum.velocity.VelocityService#handleRequest(Context, | |
106 | * String, OutputStream) | |
107 | */ | |
108 | public static void handleRequest(Context context, String template, | |
109 | OutputStream out) | |
110 | throws TemplateException | |
111 | { | |
112 | 0 | getService().handleRequest(context, template, out); |
113 | 0 | } |
114 | ||
115 | /** | |
116 | * Process the request and fill in the template with the values | |
117 | * you set in the Context. | |
118 | * | |
119 | * @param context A Context. | |
120 | * @param template The path to the template file. | |
121 | * @param out A OutputStream where we will write the process template as | |
122 | * a String. | |
123 | * @param charset The character set to use when writing the result. | |
124 | * @param encoding The encoding to use when merging context and template. | |
125 | * | |
126 | * @exception Exception Error processing template. | |
127 | * | |
128 | * @see org.apache.fulcrum.velocity.VelocityService#handleRequest(Context, | |
129 | * String, OutputStream) | |
130 | */ | |
131 | public static void handleRequest(Context context, String template, | |
132 | OutputStream out, String charset, | |
133 | String encoding) | |
134 | throws TemplateException | |
135 | { | |
136 | 0 | getService().handleRequest(context, template, out, charset, encoding); |
137 | 0 | } |
138 | ||
139 | /** | |
140 | * @see org.apache.fulcrum.velocity.VelocityService#handleRequest(Context, | |
141 | * String, Writer) | |
142 | */ | |
143 | public static void handleRequest(Context context, String filename, | |
144 | Writer writer) | |
145 | throws TemplateException | |
146 | { | |
147 | 0 | getService().handleRequest(context, filename, writer, null); |
148 | 0 | } |
149 | ||
150 | /** | |
151 | * @see org.apache.fulcrum.velocity.VelocityService#handleRequest(Context, | |
152 | * String, Writer, String) | |
153 | */ | |
154 | public static void handleRequest(Context context, String filename, | |
155 | Writer writer, String encoding) | |
156 | throws TemplateException | |
157 | { | |
158 | 0 | getService().handleRequest(context, filename, writer, encoding); |
159 | 0 | } |
160 | } |