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/14 13:18:32
6    */
7   package org.asyrinx.joey.gen.core.impl;
8   
9   import java.io.File;
10  import java.io.IOException;
11  import java.util.HashMap;
12  import java.util.Map;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  import org.apache.velocity.context.Context;
17  import org.asyrinx.brownie.core.io.FileNameUtils;
18  import org.asyrinx.joey.gen.core.BizLogicDistiller;
19  import org.asyrinx.joey.gen.core.GenerationQuery;
20  import org.asyrinx.joey.gen.core.JoeyGenRuntimeException;
21  
22  /***
23   * @author takeshi
24   */
25  public class BizLogicDistillerComposite implements BizLogicDistiller, GenerationQuery {
26  
27      /***
28       *  
29       */
30      public BizLogicDistillerComposite() {
31          super();
32      }
33  
34      private final Map distillers = new HashMap();
35  
36      public boolean canGenerate(String inputTemplate, File outputFile, Context context) {
37          context.put("bizLogic", "");
38          if (outputFile == null)
39              return true;
40          if (!outputFile.exists())
41              return true;
42          final String ext = FileNameUtils.getExtension(outputFile.getName()).toLowerCase();
43          if (!this.containsDistiller(ext))
44              return false;
45          final String distilled;
46          try {
47              distilled = this.distill(outputFile);
48          } catch (IOException e) {
49              throw new JoeyGenRuntimeException(e);
50          }
51          if ((distilled != null) && (!"".equals(distilled.trim()))) {
52              context.put("bizLogic", distilled);
53          }
54          log.debug("canGenerate context.hashCode=" + context.hashCode());
55          log.debug("canGenerate context['bizLogic']=" + context.get("bizLogic"));
56          return true;
57      }
58  
59      final Log log = LogFactory.getLog(this.getClass());
60  
61      public String distill(File source) throws IOException {
62          final String ext = FileNameUtils.getExtension(source.getName());
63          final BizLogicDistiller distiller = (BizLogicDistiller) distillers.get(ext);
64          if (distiller != null)
65              return distiller.distill(source);
66          else
67              return "";
68      }
69  
70      public boolean containsDistiller(String ext) {
71          return this.distillers.containsKey(ext.toLowerCase());
72      }
73  
74      public void put(String ext, BizLogicDistiller distiller) {
75          this.distillers.put(ext.toLowerCase(), distiller);
76      }
77  
78  }