1 package org.apache.torque.generator.template.velocity;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.StringWriter;
23 import java.io.Writer;
24 import java.util.Properties;
25 import java.util.Set;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.torque.generator.GeneratorException;
30 import org.apache.torque.generator.configuration.ConfigurationException;
31 import org.apache.torque.generator.configuration.ConfigurationProvider;
32 import org.apache.torque.generator.control.ControllerState;
33 import org.apache.torque.generator.option.Option;
34 import org.apache.torque.generator.option.Options;
35 import org.apache.torque.generator.outlet.OutletResult;
36 import org.apache.torque.generator.qname.Namespace;
37 import org.apache.torque.generator.qname.QualifiedName;
38 import org.apache.torque.generator.qname.QualifiedNameMap;
39 import org.apache.torque.generator.source.SourceElement;
40 import org.apache.torque.generator.template.TemplateOutletImpl;
41 import org.apache.torque.generator.variable.Variable;
42 import org.apache.torque.generator.variable.VariableStore;
43 import org.apache.velocity.VelocityContext;
44 import org.apache.velocity.app.Velocity;
45 import org.apache.velocity.context.Context;
46 import org.apache.velocity.runtime.RuntimeConstants;
47 import org.apache.velocity.runtime.log.Log4JLogChute;
48 import org.apache.velocity.util.StringUtils;
49
50
51
52
53 public class VelocityOutlet extends TemplateOutletImpl
54 {
55
56
57
58
59 public static final String TORQUE_GEN_CONTEXT_NAME = "torqueGen";
60
61
62
63
64
65 public static final String STRING_UTILS_CONTEXT_NAME = "stringUtils";
66
67
68
69
70
71 public static final String NULL_KEY_CONTEXT_NAME = "value";
72
73
74 private static Log log = LogFactory.getLog(VelocityOutlet.class);
75
76
77
78
79 private boolean optionsInContext = true;
80
81
82
83
84 private boolean variablesInContext = true;
85
86
87
88
89
90 private boolean sourceAttributesInContext = true;
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 public VelocityOutlet(
108 QualifiedName name,
109 ConfigurationProvider configurationProvider,
110 String path,
111 String encoding)
112 throws ConfigurationException
113 {
114 super(name,
115 configurationProvider,
116 path,
117 encoding,
118 new VelocityTemplateFilter());
119 }
120
121
122
123
124
125
126
127
128
129 public void setOptionsInContext(boolean optionsInContext)
130 {
131 this.optionsInContext = optionsInContext;
132 }
133
134
135
136
137
138
139
140 public boolean isOptionsInContext()
141 {
142 return optionsInContext;
143 }
144
145
146
147
148
149
150
151
152
153 public void setVariablesInContext(boolean variablesInContext)
154 {
155 this.variablesInContext = variablesInContext;
156 }
157
158
159
160
161
162
163
164 public boolean isVariablesInContext()
165 {
166 return variablesInContext;
167 }
168
169
170
171
172
173
174
175
176 public void setSourceAttributesInContext(
177 boolean sourceAttributesInContext)
178 {
179 this.sourceAttributesInContext = sourceAttributesInContext;
180 }
181
182
183
184
185
186
187
188
189 public boolean isSourceAttributesInContext()
190 {
191 return sourceAttributesInContext;
192 }
193
194
195
196
197
198
199
200
201
202
203 @Override
204 public OutletResult execute(ControllerState controllerState)
205 throws GeneratorException
206
207 {
208 if (log.isDebugEnabled())
209 {
210 log.debug("Start executing VelocityOutlet " + getName());
211 }
212
213 try
214 {
215 try
216 {
217 Properties properties = new Properties();
218 properties.put(
219 RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
220 Log4JLogChute.class.getName());
221 properties.put(
222 RuntimeConstants.RUNTIME_LOG,
223 "");
224 Velocity.init(properties);
225 }
226 catch (Exception e)
227 {
228 throw new GeneratorException(
229 "Could not initialize velocity",
230 e);
231 }
232
233 SourceElement sourceElement = controllerState.getSourceElement();
234
235 String inputElementName = getInputElementName();
236 if (inputElementName != null
237 && !inputElementName.equals(sourceElement.getName()))
238 {
239 throw new GeneratorException("Input element name, "
240 + sourceElement.getName()
241 + ", is not the expected name, "
242 + getInputElementName()
243 + ", for outlet "
244 + getName());
245 }
246
247 Context context = createVelocityContext(controllerState);
248
249 Writer writer = new StringWriter();
250 try
251 {
252 Velocity.evaluate(context, writer,
253 "VelocityTemplateOutlet:" + getName(),
254 getContent(controllerState));
255 writer.flush();
256 writer.close();
257 }
258 catch (Exception e)
259 {
260 log.error("error during execution of outlet "
261 + getName()
262 + " : " + e.getMessage(),
263 e);
264 throw new GeneratorException(
265 "Error during execution of outlet "
266 + getName()
267 + " : "
268 + e.getMessage(),
269 e);
270 }
271
272 return new OutletResult(writer.toString());
273 }
274 finally
275 {
276 if (log.isDebugEnabled())
277 {
278 log.debug("End executing VelocityOutlet " + getName());
279 }
280 }
281 }
282
283
284
285
286
287
288
289 private Context createVelocityContext(
290 ControllerState controllerState)
291 {
292 Context context = new VelocityContext();
293 context.put(
294 TORQUE_GEN_CONTEXT_NAME,
295 new TorqueGenVelocity(this, controllerState));
296 context.put(
297 STRING_UTILS_CONTEXT_NAME,
298 new StringUtils());
299
300 if (optionsInContext)
301 {
302
303 Options visibleOptions
304 = controllerState.getVisibleOptions();
305 for (Option option : visibleOptions.values())
306 {
307 QualifiedName qualifiedName = option.getQualifiedName();
308 context.put(qualifiedName.getName(), option.getValue());
309 }
310 log.debug("Put options in context " + visibleOptions.keySet());
311 }
312 else
313 {
314 log.debug("options in context are disabled");
315 }
316
317 SourceElement sourceElement = controllerState.getSourceElement();
318 if (sourceAttributesInContext)
319 {
320 Set<String> attributes = sourceElement.getAttributeNames();
321 for (String key : attributes)
322 {
323 Object value = sourceElement.getAttribute(key);
324 if (key == null)
325 {
326
327
328
329 if (sourceElement.getAttributeNames().contains(
330 NULL_KEY_CONTEXT_NAME))
331 {
332 continue;
333 }
334 key = NULL_KEY_CONTEXT_NAME;
335 }
336 context.put(key, value);
337 }
338 log.debug("Put attributes in context " + attributes);
339 }
340 else
341 {
342 log.debug("source attributes in context are disabled");
343 }
344
345 if (variablesInContext)
346 {
347
348
349
350
351 Namespace namespace = getName().getNamespace();
352 VariableStore variableStore
353 = controllerState.getVariableStore();
354 QualifiedNameMap<Variable> visibleVariables
355 = variableStore
356 .getContent()
357 .getInHierarchy(namespace);
358 for (Variable variable : visibleVariables.values())
359 {
360 QualifiedName qualifiedName = variable.getName();
361 context.put(
362 qualifiedName.getName(),
363 variable.getValue());
364 }
365 log.debug("Put variables in context "
366 + visibleVariables.keySet());
367 }
368 else
369 {
370 log.debug("variables in context are disabled");
371 }
372
373 return context;
374 }
375 }