1
2
3
4
5
6
7 package org.asyrinx.joey.gen.task.impl;
8
9 import java.io.File;
10 import java.io.InputStream;
11 import java.util.Properties;
12
13 import org.apache.velocity.app.VelocityEngine;
14 import org.apache.velocity.context.Context;
15 import org.apache.velocity.texen.Generator;
16 import org.asyrinx.brownie.core.lang.StringUtils;
17 import org.asyrinx.joey.gen.task.GenerationQuery;
18 import org.asyrinx.joey.gen.task.JoeyVelocityGenerator;
19
20 /***
21 * @author takeshi
22 */
23 public class JoeyVelocityGeneratorImpl extends Generator implements JoeyVelocityGenerator {
24
25 /***
26 * @param propFile
27 */
28 public JoeyVelocityGeneratorImpl() {
29 super(loadDefaultProperties());
30 }
31
32 protected void fillContextDefaults(Context context) {
33 context.put("generator", this);
34 context.put("outputDirectory", getOutputPath());
35 }
36
37 private static final String DEFAULT_TEXEN_PROPERTIES = "org/asyrinx/joey/gen/task/texen.properties";
38
39 protected static final Properties loadDefaultProperties() {
40 final Properties result = new Properties();
41 final ClassLoader classLoader = VelocityEngine.class.getClassLoader();
42 try {
43 InputStream inputStream = null;
44 try {
45 inputStream = classLoader.getResourceAsStream(DEFAULT_TEXEN_PROPERTIES);
46 result.load(inputStream);
47 } finally {
48 if (inputStream != null)
49 inputStream.close();
50 }
51 } catch (Exception e) {
52 System.err.println("Cannot get default properties!");
53 }
54 return result;
55 }
56
57
58
59 private GenerationQuery query = GenerationQuery.THROUGH;
60
61 private Context controlContext = null;
62
63
64
65
66
67
68
69 public String parse(String controlTemplate, Context controlContext) throws Exception {
70 this.controlContext = controlContext;
71 return super.parse(controlTemplate, controlContext);
72 }
73
74
75
76
77
78
79
80
81 public String parse(String inputTemplate, String intputEncoding, String outputFile, String outputEncoding,
82 String objectID, Object object) throws Exception {
83 if (!StringUtils.isEmpty(outputFile)) {
84 final File file = new File(getOutputPath(), outputFile);
85 if (!query.canGenerate(inputTemplate, file, this.controlContext))
86 return "";
87 }
88 return super.parse(inputTemplate, intputEncoding, outputFile, outputEncoding, objectID, object);
89 }
90
91 public GenerationQuery getQuery() {
92 return query;
93 }
94
95 public void setQuery(GenerationQuery query) {
96 this.query = query;
97 }
98 }