01: package net.sourceforge.jaxor.parser;
02:
03: import net.sourceforge.jaxor.util.SystemException;
04: import org.apache.velocity.VelocityContext;
05: import org.apache.velocity.app.Velocity;
06: import org.apache.velocity.app.VelocityEngine;
07: import org.apache.velocity.runtime.log.LogSystem;
08:
09: import java.io.PrintStream;
10: import java.io.PrintWriter;
11: import java.io.File;
12: import java.io.FileOutputStream;
13: import java.io.FileNotFoundException;
14: import java.util.Enumeration;
15: import java.util.Properties;
16:
17: public class CodeTemplate {
18:
19: private final VelocityEngine _engine = new VelocityEngine();
20: private final VelocityContext _context;
21:
22: public CodeTemplate(LogSystem log, VelocityContext context) {
23: _context = context;
24: try {
25: Properties props = new Properties();
26: props.load(CodeTemplate.class.getResourceAsStream("/jaxor-velocity.properties"));
27: _engine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, log);
28: Enumeration enum = props.keys();
29: while (enum.hasMoreElements()) {
30: String key = (String) enum.nextElement();
31: _engine.setProperty(key, props.getProperty(key));
32: }
33: _engine.init();
34: } catch (Exception e) {
35: throw new SystemException(e);
36: }
37: }
38:
39: public void write(String template, File destFile) {
40: try {
41: write(template, new PrintStream(new FileOutputStream(
42: destFile)));
43: } catch (FileNotFoundException e) {
44: throw new SystemException(e);
45: }
46: }
47:
48: public void write(String template, PrintStream out) {
49: try {
50: PrintWriter printWriter = new PrintWriter(out);
51: _engine.mergeTemplate(template, _context, printWriter);
52: printWriter.flush();
53: printWriter.close();
54: } catch (Exception e) {
55: throw new SystemException(e);
56: }
57: }
58: }
|