001 /** 002 * Copyright 2005-2014 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.sampleu.common; 017 018 import freemarker.cache.ClassTemplateLoader; 019 import freemarker.cache.TemplateLoader; 020 import freemarker.template.Configuration; 021 022 import java.io.File; 023 import java.io.InputStream; 024 025 /** 026 * @author Kuali Rice Team (rice.collab@kuali.org) 027 */ 028 public class FreemarkerAftTmplMthdBlanketAppGenerator { 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(FreemarkerAftTmplMthdBlanketAppGenerator.class, DIR_TMPL); 036 037 public static void main(String[] args) throws Exception { 038 cfg.setTemplateLoader(templateLoader); 039 040 String propLocation = "/GenFiles/AdminTmplMthdBlanketAppAftNavBase.properties"; 041 if (args.length > 0) { 042 propLocation = "/GenFiles/" + args[0]; 043 } 044 045 String template = "TmplMthdBlanketAppSTNavBase.ftl"; 046 047 //Here we can prepare a list of template & properties file and can iterate to generate files dynamically on single run. 048 createFile(propLocation, template); 049 } 050 051 private static void createFile(String propLocation, String template) throws Exception { 052 try { 053 InputStream in = FreemarkerAftTmplMthdBlanketAppGenerator.class.getResourceAsStream(propLocation); 054 File f1 = new File("src" + File.separatorChar + "it" + File.separatorChar + "resources" 055 + File.separatorChar + "GenFiles" + File.separatorChar 056 + propLocation.substring(propLocation.lastIndexOf("/"), propLocation.lastIndexOf(".")) 057 + template.substring(0, template.length() - 4) + ".java"); 058 FreemarkerUtil.ftlWrite(f1, cfg.getTemplate(template), in); 059 060 } catch (Exception e) { 061 e.printStackTrace(); 062 throw new Exception("Unable to generate files", e); 063 } 064 } 065 }