View Javadoc
1   /**
2    * Copyright 2005-2016 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.sampleu.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.kuali.rice.testtools.common.PropertiesUtils;
23  import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.util.Properties;
29  
30  /**
31   * FreemarkerUtil loads properties from user defined InputStream.  systemPropertiesOverride should be used to override properties
32   * using a given key to identify them from JVM args. (i.e. -Dkey.name to override the name property in the key file.)
33   * TODO setup so the loading and overriding of properties is done for the user rather then them having to call it.
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class FreemarkerUtil {
37  
38      protected Configuration cfg;
39  
40      /**
41       * Calls ftlWrite that also accepts a key, using the output getName as the key.
42       * @param output
43       * @param template
44       * @return
45       * @throws java.io.IOException
46       * @throws freemarker.template.TemplateException
47       */
48      public static File ftlWrite(File output, Template template, InputStream inputStream) throws IOException, TemplateException {
49  
50          return ftlWrite(output.getName(), output, template, inputStream);
51      }
52  
53      /**
54       * Loads properties from user defined properties file, if not available uses resource file
55       *
56       * writes processed template  to file
57       * @param key
58       * @param output
59       * @param template
60       * @throws IOException
61       * @throws TemplateException
62       */
63      public static File ftlWrite(String key, File output, Template template, InputStream inputStream) throws IOException, TemplateException {
64          PropertiesUtils propUtils = new PropertiesUtils();
65          Properties props = propUtils.loadProperties(inputStream);
66          props.put("baseName", output.getName().substring(0, output.getName().indexOf("ST")));
67          props.put("className", output.getName().substring(0, output.getName().indexOf("ST"))); // backwards compatibility
68          if (output.getName().contains("TmplMthd")) { // Template method pattern
69              props.put("className", output.getName().substring(0, output.getName().indexOf("TmplMthd")));
70          }
71  
72          if (props.get("test1") == null ) { // backwards compatibility for Smoke Test Freemarker Generation
73              props.put("test1", "test" + props.get("className") + "Bookmark");
74              props.put("test2", "test" + props.get("className") + "Nav");
75          }
76  
77          props = propUtils.systemPropertiesOverride(props, key);
78          props = propUtils.transformNumberedPropertiesToList(props);
79          File outputFile = writeTemplateToFile(output, template, props);
80  
81          return outputFile;
82      }
83  
84      protected static Properties loadProperties(InputStream inputStream) throws IOException {
85          Properties props = new Properties();
86  
87          if(inputStream != null) {
88              props.load(inputStream);
89          }
90  
91          return props;
92      }
93  
94      /**
95       *
96       * @param file
97       * @param template
98       * @param props
99       * @return
100      * @throws IOException
101      * @throws freemarker.template.TemplateException
102      */
103     protected static File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
104         String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
105         FileUtils.writeStringToFile(file, output);
106 
107         return file;
108     }
109 }