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