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;
    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
    18  /***
    19   * @author takeshi
    20   */
    21  public class JoeyVelocityGeneratorImpl extends Generator implements JoeyVelocityGenerator {
    22
    23      /***
    24       * @param propFile
    25       */
    26      public JoeyVelocityGeneratorImpl() {
    27          super(loadDefaultProperties());
    28      }
    29
    30      protected void fillContextDefaults(Context context) {
    31          context.put("generator", this);
    32          context.put("outputDirectory", getOutputPath());
    33      }
    34
    35      private static final String DEFAULT_TEXEN_PROPERTIES = "org/asyrinx/joey/gen/task/texen.properties";
    36
    37      protected static final Properties loadDefaultProperties() {
    38          final Properties result = new Properties();
    39          final ClassLoader classLoader = VelocityEngine.class.getClassLoader();
    40          try {
    41              InputStream inputStream = null;
    42              try {
    43                  inputStream = classLoader.getResourceAsStream(DEFAULT_TEXEN_PROPERTIES);
    44                  result.load(inputStream);
    45              } finally {
    46                  if (inputStream != null)
    47                      inputStream.close();
    48              }
    49          } catch (Exception e) {
    50              System.err.println("Cannot get default properties!");
    51          }
    52          return result;
    53      }
    54
    55      //final Log log = LogFactory.getLog(this.getClass());
    56
    57      private GenerationQuery query = GenerationQuery.THROUGH;
    58
    59      /*
    60       * (non-Javadoc)
    61       *
    62       * @see org.apache.velocity.texen.Generator#parse(java.lang.String,
    63       *      org.apache.velocity.context.Context)
    64       */
    65      public String parse(String controlTemplate, Context controlContext) throws Exception {
    66          return super.parse(controlTemplate, controlContext);
    67      }
    68
    69      /*
    70       * (non-Javadoc)
    71       *
    72       * @see org.apache.velocity.texen.Generator#parse(java.lang.String,
    73       *      java.lang.String, java.lang.String, java.lang.String,
    74       *      java.lang.String, java.lang.Object)
    75       */
    76      public String parse(String inputTemplate, String intputEncoding, String outputFile, String outputEncoding,
    77              String objectID, Object object) throws Exception {
    78          if (!StringUtils.isEmpty(outputFile)) {
    79              final File file = new File(getOutputPath(), outputFile);
    80              if (!query.canGenerate(inputTemplate, file))
    81                  return "";
    82          }
    83          return super.parse(inputTemplate, intputEncoding, outputFile, outputEncoding, objectID, object);
    84      }
    85
    86      public GenerationQuery getQuery() {
    87          return query;
    88      }
    89
    90      public void setQuery(GenerationQuery query) {
    91          this.query = query;
    92      }
    93  }