View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package edu.samplu.common;
17  
18  import freemarker.template.Configuration;
19  import freemarker.template.Template;
20  import freemarker.template.TemplateException;
21  import org.apache.commons.io.FileUtils;
22  import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.util.Enumeration;
28  import java.util.HashMap;
29  import java.util.Iterator;
30  import java.util.Map;
31  import java.util.Properties;
32  
33  /**
34   * FreemarkerUtil loads properties from user defined InputStream.  systemPropertiesOverride should be used to override properties
35   * using a given key to identify them from JVM args. (i.e. -Dkey.name to override the name property in the key file.)
36   * TODO setup so the loading and overriding of properties is done for the user rather then them having to call it.
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  public class FreemarkerUtil {
40  
41      protected Configuration cfg;
42  
43      /**
44       * Calls ftlWrite that also accepts a key, using the output getName as the key.
45       * @param output
46       * @param template
47       * @return
48       * @throws java.io.IOException
49       * @throws freemarker.template.TemplateException
50       */
51      public static File ftlWrite(File output, Template template, InputStream inputStream) throws IOException, TemplateException {
52  
53          return ftlWrite(output.getName(), output, template, inputStream);
54      }
55  
56      /**
57       * Loads properties from user defined properties file, if not available uses resource file
58       *
59       * writes processed template  to file
60       * @param key
61       * @param output
62       * @param template
63       * @throws IOException
64       * @throws TemplateException
65       */
66      public static File ftlWrite(String key, File output, Template template, InputStream inputStream) throws IOException, TemplateException {
67          Properties props = loadProperties(inputStream);
68          props.put("baseName", output.getName().substring(0, output.getName().indexOf("ST")));
69          props.put("className", output.getName().substring(0, output.getName().indexOf("ST"))); // backwards compatibility
70          if (output.getName().contains("TmplMthd")) { // Template method pattern
71              props.put("className", output.getName().substring(0, output.getName().indexOf("TmplMthd")));
72          }
73  
74          systemPropertiesOverride(props, key);
75          transformNumberedTestPropertiesToList(props);
76          File outputFile = writeTemplateToFile(output, template, props);
77  
78          return outputFile;
79      }
80  
81      protected static Properties loadProperties(InputStream inputStream) throws IOException {
82          Properties props = new Properties();
83  
84          if(inputStream != null) {
85              props.load(inputStream);
86          }
87  
88          return props;
89      }
90  
91      protected static void transformNumberedTestPropertiesToList(Properties props) {
92          Iterator keys = props.keySet().iterator();
93          Map<String, String> keyLists = new HashMap<String, String>();
94          while (keys.hasNext()) {
95              String key = (String)keys.next();
96              if (Character.isDigit(key.charAt(key.length()-1))) {
97                  keyLists.put(key, props.getProperty(key));
98              }
99          }
100 
101         Iterator listKeys = keyLists.keySet().iterator();
102         while (listKeys.hasNext()) {
103             String key = (String)listKeys.next();
104             props.remove(key);
105         }
106 
107         if (keyLists.values().size() == 0) { // backwards compatibility
108             keyLists.put("test1", (String)props.get("className"));
109         }
110 
111         props.put("tests", keyLists.values());
112     }
113 
114     /**
115      *
116      * @param file
117      * @param template
118      * @param props
119      * @return
120      * @throws IOException
121      * @throws freemarker.template.TemplateException
122      */
123     protected static File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
124         String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
125         FileUtils.writeStringToFile(file, output);
126 
127         return file;
128     }
129 
130     /**
131      * -Dkey.propertyname= to override the property value for propertyname.
132      * @param props
133      */
134     public static void systemPropertiesOverride(Properties props, String key) {
135         Enumeration<?> names = props.propertyNames();
136         Object nameObject;
137         String name;
138         while (names.hasMoreElements()) {
139             nameObject = names.nextElement();
140             if (nameObject instanceof String) {
141                 name = (String)nameObject;
142                 props.setProperty(name, System.getProperty(key + "." + name, props.getProperty(name)));
143             }
144         }
145     }
146 
147 }