View Javadoc

1   /*
2    * joey-gen and its relative products are published under the terms
3    * of the Apache Software License.
4    * 
5    * Created on 2005/01/11 15:11:37
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      //final Log log = LogFactory.getLog(this.getClass());
58  
59      private GenerationQuery query = GenerationQuery.THROUGH;
60  
61      private Context controlContext = null;
62  
63      /*
64       * (non-Javadoc)
65       * 
66       * @see org.apache.velocity.texen.Generator#parse(java.lang.String,
67       *      org.apache.velocity.context.Context)
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       * (non-Javadoc)
76       * 
77       * @see org.apache.velocity.texen.Generator#parse(java.lang.String,
78       *      java.lang.String, java.lang.String, java.lang.String,
79       *      java.lang.String, java.lang.Object)
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  }