1 package org.apache.velocity;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.UnsupportedEncodingException;
27 import java.io.Writer;
28 import java.util.List;
29
30 import org.apache.velocity.context.Context;
31 import org.apache.velocity.context.InternalContextAdapterImpl;
32 import org.apache.velocity.exception.MethodInvocationException;
33 import org.apache.velocity.exception.ParseErrorException;
34 import org.apache.velocity.exception.ResourceNotFoundException;
35 import org.apache.velocity.exception.TemplateInitException;
36 import org.apache.velocity.exception.VelocityException;
37 import org.apache.velocity.runtime.parser.ParseException;
38 import org.apache.velocity.runtime.parser.node.SimpleNode;
39 import org.apache.velocity.runtime.resource.Resource;
40 import org.apache.velocity.runtime.resource.ResourceManager;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class Template extends Resource
65 {
66 private VelocityException errorCondition = null;
67
68
69 public Template()
70 {
71 super();
72
73 setType(ResourceManager.RESOURCE_TEMPLATE);
74 }
75
76
77
78
79
80
81
82
83
84
85
86 public boolean process()
87 throws ResourceNotFoundException, ParseErrorException, IOException
88 {
89 data = null;
90 InputStream is = null;
91 errorCondition = null;
92
93
94
95
96 try
97 {
98 is = resourceLoader.getResourceStream(name);
99 }
100 catch( ResourceNotFoundException rnfe )
101 {
102
103
104
105
106 errorCondition = rnfe;
107 throw rnfe;
108 }
109
110
111
112
113
114
115 if (is != null)
116 {
117
118
119
120
121 try
122 {
123 BufferedReader br = new BufferedReader( new InputStreamReader( is, encoding ) );
124 data = rsvc.parse( br, name);
125 initDocument();
126 return true;
127 }
128 catch( UnsupportedEncodingException uce )
129 {
130 String msg = "Template.process : Unsupported input encoding : " + encoding
131 + " for template " + name;
132
133 errorCondition = new ParseErrorException( msg );
134 throw errorCondition;
135 }
136 catch ( ParseException pex )
137 {
138
139
140
141 errorCondition = new ParseErrorException( pex );
142 throw errorCondition;
143 }
144 catch ( TemplateInitException pex )
145 {
146 errorCondition = new ParseErrorException( pex );
147 throw errorCondition;
148 }
149
150
151
152 catch( RuntimeException e )
153 {
154 throw new RuntimeException("Exception thrown processing Template "+getName(), e);
155 }
156 finally
157 {
158
159
160
161 is.close();
162 }
163 }
164 else
165 {
166
167
168
169
170 errorCondition = new ResourceNotFoundException("Unknown resource error for resource " + name );
171 throw errorCondition;
172 }
173 }
174
175
176
177
178
179
180
181
182 public void initDocument()
183 throws TemplateInitException
184 {
185
186
187
188
189 InternalContextAdapterImpl ica = new InternalContextAdapterImpl( new VelocityContext() );
190
191 try
192 {
193
194
195
196
197 ica.pushCurrentTemplateName( name );
198 ica.setCurrentResource( this );
199
200
201
202
203
204 ((SimpleNode)data).init( ica, rsvc);
205 }
206 finally
207 {
208
209
210
211
212
213 ica.popCurrentTemplateName();
214 ica.setCurrentResource( null );
215 }
216
217 }
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232 public void merge( Context context, Writer writer)
233 throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, IOException
234 {
235 merge(context, writer, null);
236 }
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 public void merge( Context context, Writer writer, List macroLibraries)
255 throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, IOException
256 {
257
258
259
260
261
262
263 if (errorCondition != null)
264 {
265 throw errorCondition;
266 }
267
268 if( data != null)
269 {
270
271
272
273
274
275 InternalContextAdapterImpl ica = new InternalContextAdapterImpl( context );
276
277
278
279
280 ica.setMacroLibraries(macroLibraries);
281
282 if (macroLibraries != null)
283 {
284 for (int i = 0; i < macroLibraries.size(); i++)
285 {
286
287
288
289 try
290 {
291 rsvc.getTemplate((String) macroLibraries.get(i));
292 }
293 catch (ResourceNotFoundException re)
294 {
295
296
297
298 rsvc.getLog().error("template.merge(): " +
299 "cannot find template " +
300 (String) macroLibraries.get(i));
301 throw re;
302 }
303 catch (ParseErrorException pe)
304 {
305
306
307
308
309 rsvc.getLog().error("template.merge(): " +
310 "syntax error in template " +
311 (String) macroLibraries.get(i) + ".");
312 throw pe;
313 }
314
315 catch (Exception e)
316 {
317 throw new RuntimeException("Template.merge(): parse failed in template " +
318 (String) macroLibraries.get(i) + ".", e);
319 }
320 }
321 }
322
323 try
324 {
325 ica.pushCurrentTemplateName( name );
326 ica.setCurrentResource( this );
327
328 ( (SimpleNode) data ).render( ica, writer);
329 }
330 finally
331 {
332
333
334
335 ica.popCurrentTemplateName();
336 ica.setCurrentResource( null );
337 }
338 }
339 else
340 {
341
342
343
344
345 String msg = "Template.merge() failure. The document is null, " +
346 "most likely due to parsing error.";
347
348 throw new RuntimeException(msg);
349
350 }
351 }
352 }