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.Enumeration; 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 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 = loadProperties(inputStream); 065 systemPropertiesOverride(props, key); 066 File outputFile = writeTemplateToFile(output, template, props); 067 068 return outputFile; 069 } 070 071 protected static Properties loadProperties(InputStream inputStream) throws IOException { 072 Properties props = new Properties(); 073 074 if(inputStream != null) { 075 props.load(inputStream); 076 } 077 078 return props; 079 } 080 081 /** 082 * 083 * @param file 084 * @param template 085 * @param props 086 * @return 087 * @throws IOException 088 * @throws freemarker.template.TemplateException 089 */ 090 protected static File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException { 091 String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props); 092 FileUtils.writeStringToFile(file, output); 093 094 return file; 095 } 096 097 /** 098 * -Dkey.propertyname= to override the property value for propertyname. 099 * @param props 100 */ 101 public static void systemPropertiesOverride(Properties props, String key) { 102 Enumeration<?> names = props.propertyNames(); 103 Object nameObject; 104 String name; 105 while (names.hasMoreElements()) { 106 nameObject = names.nextElement(); 107 if (nameObject instanceof String) { 108 name = (String)nameObject; 109 props.setProperty(name, System.getProperty(key + "." + name, props.getProperty(name))); 110 } 111 } 112 } 113 114 }