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.core.impl;
8   
9   import java.io.File;
10  import java.io.InputStream;
11  import java.util.Hashtable;
12  import java.util.Properties;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  import org.apache.velocity.app.VelocityEngine;
17  import org.apache.velocity.context.Context;
18  import org.apache.velocity.texen.Generator;
19  import org.asyrinx.brownie.core.lang.StringUtils;
20  import org.asyrinx.joey.gen.core.GenerationQuery;
21  import org.asyrinx.joey.gen.core.JoeyVelocityGenerator;
22  
23  /***
24   * @author takeshi
25   */
26  public class JoeyVelocityGeneratorImpl extends Generator implements JoeyVelocityGenerator {
27  
28      /***
29       * @param propFile
30       */
31      public JoeyVelocityGeneratorImpl() {
32          super(loadDefaultProperties());
33      }
34  
35      protected void fillContextDefaults(Context context) {
36          context.put("generator", this);
37          context.put("outputDirectory", getOutputPath());
38      }
39  
40      private static final String DEFAULT_TEXEN_PROPERTIES = "org/asyrinx/joey/gen/core/impl/texen.properties";
41  
42      protected static final Properties loadDefaultProperties() {
43          final Properties result = new Properties();
44          final ClassLoader classLoader = VelocityEngine.class.getClassLoader();
45          try {
46              InputStream inputStream = null;
47              try {
48                  inputStream = classLoader.getResourceAsStream(DEFAULT_TEXEN_PROPERTIES);
49                  result.load(inputStream);
50              } finally {
51                  if (inputStream != null)
52                      inputStream.close();
53              }
54          } catch (Exception e) {
55              System.err.println("Cannot get default properties!");
56          }
57          return result;
58      }
59  
60      //final Log log = LogFactory.getLog(this.getClass());
61  
62      private GenerationQuery query = GenerationQuery.THROUGH;
63  
64      /*
65       * (non-Javadoc)
66       * 
67       * @see org.apache.velocity.texen.Generator#parse(java.lang.String,
68       *      org.apache.velocity.context.Context)
69       */
70      public String parse(String controlTemplate, Context controlContext) throws Exception {
71          return super.parse(controlTemplate, controlContext);
72      }
73  
74      final Log log = LogFactory.getLog(this.getClass());
75  
76      /*
77       * (non-Javadoc)
78       * 
79       * @see org.apache.velocity.texen.Generator#parse(java.lang.String,
80       *      java.lang.String, java.lang.String, java.lang.String,
81       *      java.lang.String, java.lang.Object)
82       */
83      public String parse(String inputTemplate, String intputEncoding, String outputFile, String outputEncoding,
84              String objectID, Object object) throws Exception {
85          //出力先ファイルを指定しない場合
86          if (StringUtils.isEmpty(outputFile))
87              return super.parse(inputTemplate, intputEncoding, outputFile, outputEncoding, objectID, object);
88          //出力先ファイルを指定した場合
89          final File file = StringUtils.isEmpty(outputFile) ? null : new File(getOutputPath(), outputFile);
90          if (!query.canGenerate(inputTemplate, file, this.getContext())) {
91              log.debug("cannot generate file: " + file);
92              return "";
93          }
94          return super.parse(inputTemplate, intputEncoding, outputFile, outputEncoding, objectID, object);
95      }
96  
97      public GenerationQuery getQuery() {
98          return query;
99      }
100 
101     public void setQuery(GenerationQuery query) {
102         this.query = query;
103     }
104 
105     public Context getContext() {
106         final Hashtable dummy = new Hashtable();
107         return super.getContext(dummy);
108     }
109 
110 }