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
29 import org.apache.velocity.context.Context;
30 import org.apache.velocity.context.InternalContextAdapterImpl;
31 import org.apache.velocity.exception.MethodInvocationException;
32 import org.apache.velocity.exception.ParseErrorException;
33 import org.apache.velocity.exception.ResourceNotFoundException;
34 import org.apache.velocity.exception.TemplateInitException;
35 import org.apache.velocity.exception.VelocityException;
36 import org.apache.velocity.runtime.parser.ParseException;
37 import org.apache.velocity.runtime.parser.node.SimpleNode;
38 import org.apache.velocity.runtime.resource.Resource;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class Template extends Resource
63 {
64 private VelocityException errorCondition = null;
65
66
67 public Template()
68 {
69 }
70
71
72
73
74
75
76
77
78
79
80
81 public boolean process()
82 throws ResourceNotFoundException, ParseErrorException, IOException
83 {
84 data = null;
85 InputStream is = null;
86 errorCondition = null;
87
88
89
90
91 try
92 {
93 is = resourceLoader.getResourceStream(name);
94 }
95 catch( ResourceNotFoundException rnfe )
96 {
97
98
99
100
101 errorCondition = rnfe;
102 throw rnfe;
103 }
104
105
106
107
108
109
110 if (is != null)
111 {
112
113
114
115
116 try
117 {
118 BufferedReader br = new BufferedReader( new InputStreamReader( is, encoding ) );
119
120 data = rsvc.parse( br, name);
121 initDocument();
122 return true;
123 }
124 catch( UnsupportedEncodingException uce )
125 {
126 String msg = "Template.process : Unsupported input encoding : " + encoding
127 + " for template " + name;
128
129 errorCondition = new ParseErrorException( msg );
130 throw errorCondition;
131 }
132 catch ( ParseException pex )
133 {
134
135
136
137 errorCondition = new ParseErrorException( pex );
138 throw errorCondition;
139 }
140 catch ( TemplateInitException pex )
141 {
142 errorCondition = new ParseErrorException( pex );
143 throw errorCondition;
144 }
145
146
147
148 catch( RuntimeException e )
149 {
150 throw e;
151 }
152 finally
153 {
154
155
156
157 is.close();
158 }
159 }
160 else
161 {
162
163
164
165
166 errorCondition = new ResourceNotFoundException("Unknown resource error for resource " + name );
167 throw errorCondition;
168 }
169 }
170
171
172
173
174
175
176
177
178 public void initDocument()
179 throws TemplateInitException
180 {
181
182
183
184
185 InternalContextAdapterImpl ica = new InternalContextAdapterImpl( new VelocityContext() );
186
187 try
188 {
189
190
191
192
193 ica.pushCurrentTemplateName( name );
194
195
196
197
198
199 ((SimpleNode)data).init( ica, rsvc);
200 }
201 finally
202 {
203
204
205
206
207
208 ica.popCurrentTemplateName();
209 }
210
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226 public void merge( Context context, Writer writer)
227 throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, IOException
228 {
229
230
231
232
233
234
235 if (errorCondition != null)
236 {
237 throw errorCondition;
238 }
239
240 if( data != null)
241 {
242
243
244
245
246
247 InternalContextAdapterImpl ica = new InternalContextAdapterImpl( context );
248
249 try
250 {
251 ica.pushCurrentTemplateName( name );
252 ica.setCurrentResource( this );
253
254 ( (SimpleNode) data ).render( ica, writer);
255 }
256 finally
257 {
258
259
260
261 ica.popCurrentTemplateName();
262 ica.setCurrentResource( null );
263 }
264 }
265 else
266 {
267
268
269
270
271 String msg = "Template.merge() failure. The document is null, " +
272 "most likely due to parsing error.";
273
274 throw new RuntimeException(msg);
275
276 }
277 }
278 }
279
280