KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > apache > velocity > VelocitySubTemplateEngine


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.modules.apache.velocity;
6
7 import java.io.StringWriter JavaDoc;
8 import java.util.Properties JavaDoc;
9
10 import org.apache.velocity.VelocityContext;
11 import org.apache.velocity.app.Velocity;
12
13 import xdoclet.XDocletException;
14 import xdoclet.modules.apache.SubTemplateEngine;
15
16 /**
17  * Velocity subtemplate engine
18  *
19  * @author zluspai
20  * @created July 16, 2003
21  */

22 class VelocitySubTemplateEngine implements SubTemplateEngine
23 {
24
25     // context where the Velocity variables stored
26
private VelocityContext context = new VelocityContext();
27
28     /*
29      * (non-Javadoc)
30      * @see xdoclet.templateutil.SubTemplateEngine#getVariable(java.lang.String)
31      */

32     public Object JavaDoc getVariable(String JavaDoc name)
33     {
34         return context.get(name);
35     }
36
37
38     /*
39      * (non-Javadoc)
40      * @see xdoclet.templateutil.SubTemplateEngine#setVariable(java.lang.String, java.lang.Object)
41      */

42     public void setVariable(String JavaDoc name, Object JavaDoc value)
43     {
44         context.put(name, value);
45     }
46
47     /*
48      * (non-Javadoc)
49      * @see xdoclet.templateutil.SubTemplateEngine#clearVariables()
50      */

51     public void clearVariables()
52     {
53         context = new VelocityContext();
54     }
55
56     /*
57      * (non-Javadoc)
58      * @see xdoclet.templateutil.SubTemplateEngine#generate(java.lang.String, java.util.Properties)
59      */

60     public String JavaDoc generate(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException
61     {
62         // write out the current template as a temp file
63
// String templateFile = "TEMP_" + System.currentTimeMillis() + ".vm";
64
// String templateFile = "c:\\temp\\"+System.currentTimeMillis()+".vm";
65
// File f = new File(templateFile);
66

67 // f.deleteOnExit();
68
try {
69 // BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(f));
70
//
71
// os.write(template.getBytes());
72
// os.flush();
73
// os.close();
74

75             /*
76              * setup
77              */

78             Velocity.init();
79
80             //init("velocity.properties");
81

82             /*
83              * Make a context object and populate with the data. This
84              * is where the Velocity engine gets the data to resolve the
85              * references (ex. $list) in the template
86              */

87             // VelocityContext context = new VelocityContext();
88

89             /*
90              * get the Template object. This is the parsed version of your
91              * template input file. Note that getTemplate() can throw
92              * ResourceNotFoundException : if it doesn't find the template
93              * ParseErrorException : if there is something wrong with the VTL
94              * Exception : if something else goes wrong (this is generally
95              * indicative of as serious problem...)
96              */

97             /*
98              * Template velocityTemplate = null;
99              * try {
100              * velocityTemplate = Velocity.
101              * /.getTemplate(templateFile);
102              * }
103              * catch (ResourceNotFoundException rnfe) {
104              * throw new XDocletException(rnfe, "Cannot load temporary template file:" + templateFile);
105              * }
106              * catch (ParseErrorException pee) {
107              * throw new XDocletException(pee, "Syntax error in template!");
108              * }
109              */

110             /*
111              * Now have the template engine process your template using the
112              * data placed into the context. Think of it as a 'merge'
113              * of the template and the data to produce the output stream.
114              */

115             StringWriter JavaDoc writer = new StringWriter JavaDoc();
116
117 // if (velocityTemplate != null) {
118
Velocity.evaluate(context, writer, "generate", template);
119 // velocityTemplate.merge(context, writer);
120
// }
121

122             /*
123              * flush and cleanup
124              */

125             writer.flush();
126             writer.close();
127
128             return writer.getBuffer().toString();
129         }
130         catch (Exception JavaDoc e) {
131             throw new XDocletException(e, "Exception when executing Velocity template!");
132         }
133 // finally {
134
// f.delete();
135
// }
136
}
137 }
138
139
Popular Tags