View Javadoc

1   /**
2    * 
3    */
4   package edu.samplu.common;
5   
6   import java.io.File;
7   import java.io.InputStream;
8   import java.util.Properties;
9   import org.apache.commons.io.FileUtils;
10  import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
11  import freemarker.cache.ClassTemplateLoader;
12  import freemarker.cache.TemplateLoader;
13  import freemarker.template.Configuration;
14  import sun.applet.Main;
15  
16  /**
17   * @author hardik
18   * 
19   */
20  public class FreemarkerSmoketestGenerator {
21      private static Configuration cfg = new Configuration();
22  
23      // Templates for File Generation
24      private static String DIR_TMPL = "/Gen/";
25  
26      //Configuration
27      private static TemplateLoader templateLoader = new ClassTemplateLoader(Main.class, DIR_TMPL);
28  
29      public static void main(String[] args) throws Exception {
30          cfg.setTemplateLoader(templateLoader);
31          String DEFAULT_PROPS_LOCATION = "/GenFiles/WorkFlowDocType.properties";
32          String STJUNITBASE_TMPL = "STJUnitBase.ftl";
33          String STJUNITBKMRKGEN_TMPL = "STJUnitBkMrkGen.ftl";
34          String STJUNITNAVGEN_TMPL = "STJUnitNavGen.ftl";
35          String STNGBASE_TMPL = "STNGBase.ftl";
36          String STNGBKMRKGEN_TMPL = "STNGBkMrkGen.ftl";
37          String STNGNAVGEN_TMPL = "STNGNavGen.ftl";
38          
39          //Here we can prepare a list of template & properties file and can iterate to generate files dynamically on single run.
40          createFile(DEFAULT_PROPS_LOCATION,STJUNITBASE_TMPL);
41          createFile(DEFAULT_PROPS_LOCATION,STJUNITBKMRKGEN_TMPL);
42          createFile(DEFAULT_PROPS_LOCATION,STJUNITNAVGEN_TMPL);
43          createFile(DEFAULT_PROPS_LOCATION,STNGBASE_TMPL);
44          createFile(DEFAULT_PROPS_LOCATION,STNGBKMRKGEN_TMPL);
45          createFile(DEFAULT_PROPS_LOCATION,STNGNAVGEN_TMPL);
46      }
47  
48      private static void createFile(String DEFAULT_PROPS_LOCATION, String TMPL) throws Exception
49      {
50          try {
51              //Loading Properties file
52              Properties props = new Properties();
53              InputStream in = null;
54              in = Main.class.getResourceAsStream(DEFAULT_PROPS_LOCATION);
55  
56              if (in != null) {
57                  props.load(in);
58                  in.close();
59              }
60              else
61              {
62                  throw new Exception("Problem with input stream.");
63              }
64  
65              // build file STJUnitBase and add to array              
66              File f1 = new File("src" + File.separatorChar + "it" + File.separatorChar + "resources"
67                      + File.separatorChar + "GenFiles" + File.separatorChar + props.getProperty("className")
68                      + TMPL.substring(0, TMPL.length() - 4) + ".java");
69  
70              String output1 = FreeMarkerTemplateUtils.processTemplateIntoString(cfg.getTemplate(TMPL),
71                      props);
72              FileUtils.writeStringToFile(f1, output1);
73  
74          } catch (Exception e) {
75              e.printStackTrace();
76              throw new Exception("Unable to generate files", e);
77          }
78  
79      }
80  
81  }