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