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 */
016package edu.samplu.common;
017
018import java.io.File;
019import java.io.InputStream;
020
021import freemarker.cache.ClassTemplateLoader;
022import freemarker.cache.TemplateLoader;
023import freemarker.template.Configuration;
024
025/**
026 * @author Kuali Rice Team (rice.collab@kuali.org)
027 */
028public class FreemarkerSTTmplMthdGenerator {
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(FreemarkerSTTmplMthdGenerator.class, DIR_TMPL);
036
037    public static void main(String[] args) throws Exception {
038        cfg.setTemplateLoader(templateLoader);
039
040        String propLocation = "/GenFiles/MainTmplMthdSTNavBase.properties";
041        String template = "TmplMthdSTNavBase.ftl";
042
043        createFile(propLocation, template);
044    }
045
046    private static void createFile(String propLocation, String template) throws Exception {
047        try {
048            InputStream in = FreemarkerSTTmplMthdGenerator.class.getResourceAsStream(propLocation);
049            File f1 = new File("src" + File.separatorChar + "it" + File.separatorChar + "resources"
050                    + File.separatorChar + "GenFiles" + File.separatorChar
051                    + propLocation.substring(propLocation.lastIndexOf("/"), propLocation.lastIndexOf("."))
052                    + template.substring(0, template.length() - 4) + ".java");
053            FreemarkerUtil.ftlWrite(f1, cfg.getTemplate(template), in);
054
055        } catch (Exception e) {
056            e.printStackTrace();
057            throw new Exception("Unable to generate files", e);
058        }
059    }
060}