1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.util;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.Reader;
21 import java.io.StringReader;
22 import java.io.StringWriter;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.velocity.VelocityContext;
27 import org.apache.velocity.app.VelocityEngine;
28 import org.apache.velocity.exception.VelocityException;
29 import org.apache.velocity.tools.generic.ComparisonDateTool;
30 import org.apache.velocity.tools.generic.DateTool;
31 import org.apache.velocity.tools.generic.MathTool;
32 import org.apache.velocity.tools.generic.NumberTool;
33 import org.apache.velocity.tools.generic.SortTool;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class VelocityTemplateEngine {
60
61 private final VelocityEngine velocityEngine = new VelocityEngine();
62 private VelocityContext defaultContext;
63 private Map<String,Object> configMap = new HashMap<String, Object>();
64
65
66
67
68 public VelocityTemplateEngine() {
69 init();
70 }
71
72
73
74
75
76
77 public VelocityTemplateEngine(final Map<String,Object> config) {
78 this.configMap = config;
79 init();
80 }
81
82
83
84
85 private void init() {
86 velocityEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "class");
87 velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
88 setLogFile();
89
90 DateTool dateTool = new DateTool();
91 dateTool.configure(this.configMap);
92 MathTool mathTool = new MathTool();
93 NumberTool numberTool = new NumberTool();
94 numberTool.configure(this.configMap);
95 SortTool sortTool = new SortTool();
96
97 defaultContext = new VelocityContext();
98 defaultContext.put("dateTool", dateTool);
99 defaultContext.put("dateComparisonTool", new ComparisonDateTool());
100 defaultContext.put("mathTool", mathTool);
101 defaultContext.put("numberTool", numberTool);
102 defaultContext.put("sortTool", sortTool);
103
104
105
106
107 try {
108 velocityEngine.init();
109 } catch (Exception e) {
110 throw new VelocityException(e);
111 }
112 }
113
114
115
116
117
118
119 public void setLogging(boolean enableLogging) {
120 if (!enableLogging) {
121
122 velocityEngine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem");
123 } else {
124 velocityEngine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS, null);
125 setLogFile();
126 }
127 }
128
129
130
131
132
133 public void setLogFile() {
134 if (System.getProperty("catalina.base") != null) {
135 setLogFile(System.getProperty("catalina.base") + "/logs/velocity.log");
136 } else {
137 setLogFile("target/velocity.log");
138 }
139 }
140
141
142
143
144
145
146 public void setLogFile(final String logfile) {
147 velocityEngine.setProperty(VelocityEngine.RUNTIME_LOG, logfile);
148 }
149
150
151
152
153
154
155
156
157
158 public String evaluate(final Map<String, Object> contextMap, final String template) throws VelocityException {
159 Reader readerOut = null;
160 try {
161 readerOut = new BufferedReader(new StringReader(template));
162 return evaluate(contextMap, readerOut);
163 } finally {
164 if(readerOut != null) {
165 try {
166 readerOut.close();
167 } catch (IOException e) {
168 throw new VelocityException(e);
169 }
170
171 }
172 }
173 }
174
175
176
177
178
179
180
181
182 public String evaluate(final Map<String, Object> mapContext, final Reader template) throws VelocityException {
183 VelocityContext context = new VelocityContext(mapContext, defaultContext);
184
185 StringWriter writerOut = new StringWriter();
186 try {
187 velocityEngine.evaluate(context, writerOut, "VelocityEngine", template);
188 return writerOut.toString();
189 } catch(Exception e) {
190 throw new VelocityException(e);
191 }
192 }
193
194 }