1 package org.apache.velocity.texen;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.InputStream;
24 import java.io.FileInputStream;
25 import java.io.BufferedInputStream;
26 import java.io.Writer;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.io.StringWriter;
30 import java.io.OutputStreamWriter;
31 import java.io.BufferedWriter;
32 import java.io.FileOutputStream;
33
34 import java.util.Enumeration;
35 import java.util.Hashtable;
36 import java.util.Iterator;
37 import java.util.Properties;
38
39 import org.apache.velocity.Template;
40 import org.apache.velocity.context.Context;
41 import org.apache.velocity.VelocityContext;
42 import org.apache.velocity.app.VelocityEngine;
43 import org.apache.velocity.util.ClassUtils;
44
45
46
47
48
49
50
51
52 public class Generator
53 {
54
55
56
57 public static final String OUTPUT_PATH = "output.path";
58
59
60
61
62 public static final String TEMPLATE_PATH = "template.path";
63
64
65
66
67
68 private static final String DEFAULT_TEXEN_PROPERTIES =
69 "org/apache/velocity/texen/defaults/texen.properties";
70
71
72
73
74 private Properties props = new Properties();
75
76
77
78
79 private Context controlContext;
80
81
82
83
84
85
86
87
88 private Hashtable writers = new Hashtable();
89
90
91
92
93
94
95 private static Generator instance = new Generator();
96
97
98
99
100 protected String outputEncoding;
101
102
103
104
105
106 protected String inputEncoding;
107
108
109
110
111 protected VelocityEngine ve;
112
113
114
115
116 private Generator()
117 {
118 setDefaultProps();
119 }
120
121
122
123
124
125
126 public static Generator getInstance()
127 {
128 return instance;
129 }
130
131
132
133
134
135 public void setVelocityEngine(VelocityEngine ve)
136 {
137 this.ve = ve;
138 }
139
140
141
142
143
144
145
146
147
148 public Generator (String propFile)
149 {
150 try
151 {
152 BufferedInputStream bi = null;
153 try
154 {
155 bi = new BufferedInputStream (new FileInputStream (propFile));
156 props.load (bi);
157 }
158 finally
159 {
160 if (bi != null)
161 {
162 bi.close();
163 }
164 }
165 }
166 catch (IOException e)
167 {
168 System.err.println("Could not load " + propFile
169 + ", falling back to defaults. ("
170 + e.getMessage() + ")");
171
172
173
174 setDefaultProps();
175 }
176 }
177
178
179
180
181
182
183
184 public Generator (Properties props)
185 {
186 this.props = (Properties)props.clone();
187 }
188
189
190
191
192 protected void setDefaultProps()
193 {
194 ClassLoader classLoader = VelocityEngine.class.getClassLoader();
195 try
196 {
197 InputStream inputStream = null;
198 try
199 {
200 inputStream = classLoader.getResourceAsStream(
201 DEFAULT_TEXEN_PROPERTIES);
202
203 props.load( inputStream );
204 }
205 finally
206 {
207 if (inputStream != null)
208 {
209 inputStream.close();
210 }
211 }
212 }
213 catch (IOException ioe)
214 {
215 System.err.println("Cannot get default properties: " + ioe.getMessage());
216 }
217 }
218
219
220
221
222
223
224
225 public void setTemplatePath(String templatePath)
226 {
227 props.put(TEMPLATE_PATH, templatePath);
228 }
229
230
231
232
233
234
235 public String getTemplatePath()
236 {
237 return props.getProperty(TEMPLATE_PATH);
238 }
239
240
241
242
243
244
245 public void setOutputPath(String outputPath)
246 {
247 props.put(OUTPUT_PATH, outputPath);
248 }
249
250
251
252
253
254
255
256 public String getOutputPath()
257 {
258 return props.getProperty(OUTPUT_PATH);
259 }
260
261
262
263
264
265 public void setOutputEncoding(String outputEncoding)
266 {
267 this.outputEncoding = outputEncoding;
268 }
269
270
271
272
273
274 public void setInputEncoding(String inputEncoding)
275 {
276 this.inputEncoding = inputEncoding;
277 }
278
279
280
281
282
283
284
285
286
287 public Writer getWriter(String path, String encoding) throws Exception {
288 Writer writer;
289 if (encoding == null || encoding.length() == 0 || encoding.equals("8859-1") || encoding.equals("8859_1")) {
290 writer = new FileWriter(path);
291 }
292 else
293 {
294 writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), encoding));
295 }
296 return writer;
297 }
298
299
300
301
302
303
304
305
306
307 public Template getTemplate(String templateName, String encoding) throws Exception {
308 Template template;
309 if (encoding == null || encoding.length() == 0 || encoding.equals("8859-1") || encoding.equals("8859_1")) {
310 template = ve.getTemplate(templateName);
311 }
312 else {
313 template = ve.getTemplate(templateName, encoding);
314 }
315 return template;
316 }
317
318
319
320
321
322
323
324
325
326
327
328 public String parse (String inputTemplate, String outputFile)
329 throws Exception
330 {
331 return parse(inputTemplate, outputFile, null, null);
332 }
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347 public String parse (String inputTemplate,
348 String outputFile,
349 String objectID,
350 Object object)
351 throws Exception
352 {
353 return parse(inputTemplate, null, outputFile, null, objectID, object);
354 }
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370 public String parse (String inputTemplate,
371 String inputEncoding,
372 String outputFile,
373 String outputEncoding,
374 String objectID,
375 Object object)
376 throws Exception
377 {
378 if (objectID != null && object != null)
379 {
380 controlContext.put(objectID, object);
381 }
382
383 Template template = getTemplate(inputTemplate, inputEncoding != null ? inputEncoding : this.inputEncoding);
384
385 if (outputFile == null || outputFile.equals(""))
386 {
387 StringWriter sw = new StringWriter();
388 template.merge (controlContext,sw);
389 return sw.toString();
390 }
391 else
392 {
393 Writer writer = null;
394
395 if (writers.get(outputFile) == null)
396 {
397
398
399
400
401 writer = getWriter(
402 getOutputPath() + File.separator + outputFile,
403 outputEncoding != null ? outputEncoding : this.outputEncoding
404 );
405
406
407
408
409
410 writers.put(outputFile, writer);
411 }
412 else
413 {
414 writer = (Writer) writers.get(outputFile);
415 }
416
417 VelocityContext vc = new VelocityContext( controlContext );
418 template.merge (vc,writer);
419
420
421
422
423 return "";
424 }
425 }
426
427
428
429
430
431
432
433
434
435
436 public String parse (String controlTemplate, Context controlContext)
437 throws Exception
438 {
439 this.controlContext = controlContext;
440 fillContextDefaults(this.controlContext);
441 fillContextProperties(this.controlContext);
442
443 Template template = getTemplate(controlTemplate, inputEncoding);
444 StringWriter sw = new StringWriter();
445 template.merge (controlContext,sw);
446
447 return sw.toString();
448 }
449
450
451
452
453
454
455
456
457
458
459 protected Context getContext (Hashtable objs)
460 {
461 fillContextHash (controlContext,objs);
462 return controlContext;
463 }
464
465
466
467
468
469
470
471 protected void fillContextHash (Context context, Hashtable objs)
472 {
473 Enumeration enumeration = objs.keys();
474 while (enumeration.hasMoreElements())
475 {
476 String key = enumeration.nextElement().toString();
477 context.put (key, objs.get(key));
478 }
479 }
480
481
482
483
484
485
486 protected void fillContextDefaults (Context context)
487 {
488 context.put ("generator", instance);
489 context.put ("outputDirectory", getOutputPath());
490 }
491
492
493
494
495
496
497
498
499 protected void fillContextProperties (Context context)
500 {
501 Enumeration enumeration = props.propertyNames();
502
503 while (enumeration.hasMoreElements())
504 {
505 String nm = (String) enumeration.nextElement();
506 if (nm.startsWith ("context.objects."))
507 {
508
509 String contextObj = props.getProperty (nm);
510 int colon = nm.lastIndexOf ('.');
511 String contextName = nm.substring (colon+1);
512
513 try
514 {
515 Object o = ClassUtils.getNewInstance(contextObj);
516 context.put (contextName,o);
517 }
518 catch (Exception e)
519 {
520 e.printStackTrace();
521
522 }
523 }
524 }
525 }
526
527
528
529
530
531
532 public void shutdown()
533 {
534 Iterator iterator = writers.values().iterator();
535
536 while(iterator.hasNext())
537 {
538 Writer writer = (Writer) iterator.next();
539
540 try
541 {
542 writer.flush();
543 }
544 catch (IOException e)
545 {
546
547 }
548
549 try
550 {
551 writer.close();
552 }
553 catch (IOException e)
554 {
555
556 }
557 }
558
559 writers.clear();
560 }
561 }