001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package edu.samplu.common;
017    
018    import java.io.File;
019    import java.io.InputStream;
020    
021    import freemarker.cache.ClassTemplateLoader;
022    import freemarker.cache.TemplateLoader;
023    import freemarker.template.Configuration;
024    
025    /**
026     * @author Kuali Rice Team (rice.collab@kuali.org)
027     */
028    public class FreemarkerSTGenerator {
029        private static Configuration cfg = new Configuration();
030    
031        // Templates for File Generation
032        private static String DIR_TMPL = "/Gen/";
033    
034        //Configuration
035        private static TemplateLoader templateLoader = new ClassTemplateLoader(FreemarkerSTGenerator.class, DIR_TMPL);
036    
037        private static String STJUNITBASE_TMPL = "STJUnitBase.ftl";
038        private static String STJUNITBKMRKGEN_TMPL = "STJUnitBkMrkGen.ftl";
039        private static String STJUNITNAVGEN_TMPL = "STJUnitNavGen.ftl";
040        private static String STNGBASE_TMPL = "STNGBase.ftl";
041        private static String STNGBKMRKGEN_TMPL = "STNGBkMrkGen.ftl";
042        private static String STNGNAVGEN_TMPL = "STNGNavGen.ftl";
043    
044        public static void main(String[] args) throws Exception {
045            cfg.setTemplateLoader(templateLoader);
046    
047            String propsLocation = "/GenFiles/Group.properties";
048            if (args.length > 0) {
049                propsLocation = "/GenFiles/" + args[0];
050            }
051    
052            //Here we can prepare a list of template & properties file and can iterate to generate files dynamically on single run.
053            createFile(propsLocation, STJUNITBASE_TMPL);
054            createFile(propsLocation, STJUNITBKMRKGEN_TMPL);
055            createFile(propsLocation, STJUNITNAVGEN_TMPL);
056            createFile(propsLocation, STNGBASE_TMPL);
057            createFile(propsLocation, STNGBKMRKGEN_TMPL);
058            createFile(propsLocation, STNGNAVGEN_TMPL);
059        }
060    
061        private static void createFile(String propLocation, String template) throws Exception {
062            try {
063                InputStream in = FreemarkerSTGenerator.class.getResourceAsStream(propLocation);
064                File f1 = new File("src" + File.separatorChar + "it" + File.separatorChar + "resources"
065                        + File.separatorChar + "GenFiles" + File.separatorChar
066                        + propLocation.substring(propLocation.lastIndexOf("/"), propLocation.lastIndexOf("."))
067                        + template.substring(0, template.length() - 4) + ".java");
068                FreemarkerUtil.ftlWrite(f1, cfg.getTemplate(template), in);
069    
070            } catch (Exception e) {
071                e.printStackTrace();
072                throw new Exception("Unable to generate files", e);
073            }
074        }
075    }