1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
package org.webmacro.engine; |
25 | |
|
26 | |
import java.io.IOException; |
27 | |
import java.io.OutputStream; |
28 | |
import java.io.Reader; |
29 | |
import java.util.Iterator; |
30 | |
import java.util.Map; |
31 | |
|
32 | |
import org.slf4j.Logger; |
33 | |
import org.slf4j.LoggerFactory; |
34 | |
|
35 | |
import org.webmacro.Broker; |
36 | |
import org.webmacro.Context; |
37 | |
import org.webmacro.FastWriter; |
38 | |
import org.webmacro.PropertyException; |
39 | |
import org.webmacro.Template; |
40 | |
import org.webmacro.TemplateException; |
41 | |
import org.webmacro.TemplateVisitor; |
42 | |
import org.webmacro.WMConstants; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
abstract public class WMTemplate implements Template |
65 | |
{ |
66 | |
|
67 | 37 | static Logger _log = LoggerFactory.getLogger(WMTemplate.class); |
68 | |
|
69 | |
|
70 | |
|
71 | |
final protected Broker _broker; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
private boolean _parsed; |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
protected Block _content; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
private String _parserName; |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
private Map _parameters; |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
private Map _macros; |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
protected WMTemplate (Broker broker) |
104 | |
{ |
105 | 1678 | this("wm", broker); |
106 | 1678 | } |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
protected WMTemplate (String parserName, Broker broker) |
113 | 1678 | { |
114 | 1678 | _broker = broker; |
115 | 1678 | _parserName = parserName; |
116 | 1678 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
protected abstract Reader getReader () throws IOException; |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public abstract String toString (); |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public String getName () |
136 | |
{ |
137 | 0 | return toString(); |
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
public void setName (String name) |
144 | |
{ |
145 | 0 | } |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
protected Parser getParser () throws TemplateException |
153 | |
{ |
154 | |
try |
155 | |
{ |
156 | 1678 | return (Parser) _broker.get("parser", "wm"); |
157 | |
} |
158 | 0 | catch (Exception e) |
159 | |
{ |
160 | 0 | throw new TemplateException("Could not load parser type " + |
161 | |
_parserName, e); |
162 | |
} |
163 | |
} |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public void parse () throws IOException, TemplateException |
169 | |
{ |
170 | 1678 | if (!_parsed) |
171 | |
{ |
172 | 1678 | Block newContent = null; |
173 | 1678 | Map newParameters = null; |
174 | 1678 | Map newMacros = null; |
175 | 1678 | Reader in = null; |
176 | 1678 | BuildContext bc = null; |
177 | |
try |
178 | |
{ |
179 | 1678 | Parser parser = getParser(); |
180 | 1678 | in = getReader(); |
181 | 1678 | BlockBuilder bb = parser.parseBlock(getName(), in); |
182 | 1670 | in.close(); |
183 | 1670 | bc = new BuildContext(_broker); |
184 | |
|
185 | 1670 | Map globalMacros = _broker.getMacros(); |
186 | 1670 | for (Iterator i=globalMacros.entrySet().iterator(); i.hasNext(); ) { |
187 | 1 | Map.Entry entry = (Map.Entry)i.next(); |
188 | 1 | bc.putMacro((String)entry.getKey(),(MacroDefinition) entry.getValue()); |
189 | 1 | } |
190 | 1670 | newParameters = bc.getMap(); |
191 | 1670 | newMacros = bc.getMacros(); |
192 | 1670 | newContent = (Block) bb.build(bc); |
193 | |
} |
194 | 10 | catch (BuildException be) |
195 | |
{ |
196 | 10 | if (bc != null) |
197 | 10 | be.setContextLocation(bc.getCurrentLocation()); |
198 | 10 | _log.error("Template contained invalid data", be); |
199 | 10 | throw be; |
200 | |
} |
201 | 0 | catch (IOException e) |
202 | |
{ |
203 | 0 | _log.error("Template: Could not read template: " + this); |
204 | 0 | throw e; |
205 | |
} |
206 | 8 | catch (Exception e) |
207 | |
{ |
208 | 8 | _log.error("Error parsing template: " + this, e); |
209 | 8 | BuildException be = new BuildException("Error parsing template: " + this, e); |
210 | 8 | if (bc != null) |
211 | 0 | be.setContextLocation(bc.getCurrentLocation()); |
212 | 8 | throw be; |
213 | |
} |
214 | |
finally |
215 | |
{ |
216 | 18 | try |
217 | |
{ |
218 | 1678 | if (in != null) |
219 | 1678 | in.close(); |
220 | |
} |
221 | 0 | catch (IOException e) |
222 | |
{ |
223 | 0 | e = null; |
224 | 1678 | } |
225 | 1678 | _parameters = newParameters; |
226 | 1678 | _content = newContent; |
227 | 1678 | _macros = newMacros; |
228 | 1678 | _parsed = true; |
229 | 1660 | } |
230 | 1660 | } |
231 | |
else |
232 | |
{ |
233 | 0 | _log.debug("Ignoring parse request on already parsed template " + this); |
234 | |
} |
235 | 1660 | } |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
public Map getMacros () |
244 | |
{ |
245 | 20 | return _macros; |
246 | |
} |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
public final String evaluateAsString (Context context) throws PropertyException |
255 | |
{ |
256 | |
try |
257 | |
{ |
258 | 1638 | FastWriter fw = FastWriter.getInstance(_broker); |
259 | 1638 | write(fw, context); |
260 | 1595 | String ret = fw.toString(); |
261 | 1595 | fw.close(); |
262 | 1595 | return ret; |
263 | |
} |
264 | 0 | catch (IOException e) |
265 | |
{ |
266 | 0 | _log.error("Template: Could not write to ByteArrayOutputStream!", e); |
267 | 0 | return null; |
268 | |
} |
269 | |
} |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
public final byte[] evaluateAsBytes (String encoding, Context context) throws PropertyException |
278 | |
{ |
279 | |
try |
280 | |
{ |
281 | 3 | FastWriter fw = FastWriter.getInstance(_broker, encoding); |
282 | 3 | write(fw, context); |
283 | 3 | byte[] ret = fw.toByteArray(); |
284 | 3 | fw.close(); |
285 | 3 | return ret; |
286 | |
} |
287 | 0 | catch (IOException e) |
288 | |
{ |
289 | 0 | _log.error("Template: Could not write to ByteArrayOutputStream!", e); |
290 | 0 | return null; |
291 | |
} |
292 | |
} |
293 | |
|
294 | |
public void write(OutputStream out, Context context) |
295 | |
throws PropertyException, IOException { |
296 | 0 | FastWriter fw = FastWriter.getInstance(_broker, out); |
297 | 0 | write(fw, context); |
298 | 0 | fw.flush(); |
299 | 0 | fw.close(); |
300 | 0 | } |
301 | |
|
302 | |
public void write(OutputStream out, String encoding, Context context) |
303 | |
throws PropertyException, IOException { |
304 | 7 | FastWriter fw = FastWriter.getInstance(_broker, out, encoding); |
305 | 7 | write(fw, context); |
306 | 7 | fw.flush(); |
307 | 7 | fw.close(); |
308 | 7 | } |
309 | |
|
310 | |
public final void write (FastWriter out, Context context) |
311 | |
throws IOException, PropertyException |
312 | |
{ |
313 | |
try |
314 | |
{ |
315 | 3439 | if (!_parsed) |
316 | |
{ |
317 | 240 | parse(); |
318 | |
} |
319 | |
} |
320 | 1 | catch (TemplateException e) |
321 | |
{ |
322 | 1 | _log.error("Template: Unable to parse template: " + this, e); |
323 | 1 | out.write(context.getEvaluationExceptionHandler() |
324 | |
.errorString("Template failed to parse. Reason: \n" |
325 | |
+ e.toString())); |
326 | 3438 | } |
327 | |
|
328 | |
try |
329 | |
{ |
330 | 3439 | _content.write(out, context); |
331 | |
} |
332 | 43 | catch (PropertyException e) |
333 | |
{ |
334 | 43 | e.setContextLocation(context.getCurrentLocation()); |
335 | 43 | throw e; |
336 | |
} |
337 | 0 | catch (IOException ioe) |
338 | |
{ |
339 | 0 | throw ioe; |
340 | |
} |
341 | 1 | catch (Exception e) |
342 | |
{ |
343 | 1 | String warning = |
344 | |
"Template: Exception evaluating template " + this; |
345 | 1 | _log.warn(warning, e); |
346 | |
|
347 | 1 | out.write(context.getEvaluationExceptionHandler() |
348 | |
.warningString("Could not interpret template. Reason: \n" |
349 | |
+ warning + "\n" + e.toString())); |
350 | 3395 | } |
351 | 3396 | } |
352 | |
|
353 | |
public void accept (TemplateVisitor v) |
354 | |
{ |
355 | 0 | _content.accept(v); |
356 | 0 | } |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
protected final String getDefaultEncoding () |
368 | |
{ |
369 | |
try |
370 | |
{ |
371 | 58 | return (String) _broker.get("config", WMConstants.TEMPLATE_INPUT_ENCODING); |
372 | |
} |
373 | 0 | catch (Exception e) |
374 | |
{ |
375 | 0 | return System.getProperty("file.encoding"); |
376 | |
} |
377 | |
} |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
public Object getParam (String key) |
384 | |
throws IOException, TemplateException |
385 | |
{ |
386 | |
try |
387 | |
{ |
388 | 6 | return _parameters.get(key); |
389 | |
} |
390 | 0 | catch (NullPointerException e) |
391 | |
{ |
392 | 0 | parse(); |
393 | 0 | return _parameters.get(key); |
394 | |
} |
395 | |
} |
396 | |
|
397 | |
public Map getParameters () |
398 | |
{ |
399 | 19 | return _parameters; |
400 | |
} |
401 | |
|
402 | |
public void setParam (String key, Object value) |
403 | |
{ |
404 | 0 | _parameters.put(key, value); |
405 | 0 | } |
406 | |
|
407 | |
|
408 | |
} |
409 | |
|