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 freemarker.template.Configuration;
019    import freemarker.template.Template;
020    import freemarker.template.TemplateException;
021    import org.apache.commons.io.FileUtils;
022    import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
023    
024    import java.io.File;
025    import java.io.IOException;
026    import java.io.InputStream;
027    import java.util.Properties;
028    
029    /**
030     * FreemarkerUtil loads properties from user defined InputStream.  systemPropertiesOverride should be used to override properties
031     * using a given key to identify them from JVM args. (i.e. -Dkey.name to override the name property in the key file.)
032     * TODO setup so the loading and overriding of properties is done for the user rather then them having to call it.
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    public class FreemarkerUtil {
036    
037        protected Configuration cfg;
038    
039        /**
040         * Calls ftlWrite that also accepts a key, using the output getName as the key.
041         * @param output
042         * @param template
043         * @return
044         * @throws java.io.IOException
045         * @throws freemarker.template.TemplateException
046         */
047        public static File ftlWrite(File output, Template template, InputStream inputStream) throws IOException, TemplateException {
048    
049            return ftlWrite(output.getName(), output, template, inputStream);
050        }
051    
052        /**
053         * Loads properties from user defined properties file, if not available uses resource file
054         *
055         * writes processed template  to file
056         * @param key
057         * @param output
058         * @param template
059         * @throws IOException
060         * @throws TemplateException
061         */
062        public static File ftlWrite(String key, File output, Template template, InputStream inputStream) throws IOException, TemplateException {
063            Properties props = PropertiesUtils.loadProperties(inputStream);
064            props.put("baseName", output.getName().substring(0, output.getName().indexOf("ST")));
065            props.put("className", output.getName().substring(0, output.getName().indexOf("ST"))); // backwards compatibility
066            if (output.getName().contains("TmplMthd")) { // Template method pattern
067                props.put("className", output.getName().substring(0, output.getName().indexOf("TmplMthd")));
068            }
069    
070            if (props.get("test1") == null ) { // backwards compatibility for Smoke Test Freemarker Generation
071                props.put("test1", "test" + props.get("className") + "Bookmark");
072                props.put("test2", "test" + props.get("className") + "Nav");
073            }
074    
075            PropertiesUtils.systemPropertiesOverride(props, key);
076            PropertiesUtils.transformNumberedPropertiesToList(props);
077            File outputFile = writeTemplateToFile(output, template, props);
078    
079            return outputFile;
080        }
081    
082        protected static Properties loadProperties(InputStream inputStream) throws IOException {
083            Properties props = new Properties();
084    
085            if(inputStream != null) {
086                props.load(inputStream);
087            }
088    
089            return props;
090        }
091    
092        /**
093         *
094         * @param file
095         * @param template
096         * @param props
097         * @return
098         * @throws IOException
099         * @throws freemarker.template.TemplateException
100         */
101        protected static File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
102            String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
103            FileUtils.writeStringToFile(file, output);
104    
105            return file;
106        }
107    }