Coverage Report - net.admin4j.util.FreemarkerUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FreemarkerUtils
57%
11/19
25%
1/4
3
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.util;
 15  
 
 16  
 import java.io.File;
 17  
 import java.io.IOException;
 18  
 import java.util.Map;
 19  
 
 20  
 import net.admin4j.config.Admin4JConfiguration;
 21  
 import net.admin4j.deps.commons.lang3.SystemUtils;
 22  
 import net.admin4j.deps.commons.lang3.Validate;
 23  
 import freemarker.ext.beans.BeansWrapper;
 24  
 import freemarker.template.Configuration;
 25  
 import freemarker.template.DefaultObjectWrapper;
 26  
 import freemarker.template.Template;
 27  
 import freemarker.template.TemplateModelException;
 28  
 import freemarker.template.TemplateScalarModel;
 29  
 
 30  
 /**
 31  
  * Generic Freemarker utilities
 32  
  * @author D. Ashmore
 33  
  *
 34  
  */
 35  0
 public class FreemarkerUtils {
 36  
 
 37  
     @SuppressWarnings("rawtypes")
 38  
     public static Template createConfiguredTemplate(Class templateLoadingClass, String templateName)
 39  
             throws IOException {
 40  
         Validate.notNull(templateLoadingClass, "Null templateLoadingClass not allowed.");
 41  
         Validate.notEmpty(templateName, "Null or blank templateName not allowed.");
 42  258
         Configuration cfg = new Configuration();
 43  258
         if (Admin4JConfiguration.getBaseFreemarkerTemplateDirectory() == null) {
 44  258
             cfg.setClassForTemplateLoading(templateLoadingClass, "");
 45  
         }
 46  
         else {
 47  0
             StringBuffer dirName = new StringBuffer(Admin4JConfiguration.getBaseFreemarkerTemplateDirectory().getAbsolutePath());
 48  0
             if ( !dirName.toString().endsWith(SystemUtils.FILE_SEPARATOR) ) {
 49  0
                 dirName.append(SystemUtils.FILE_SEPARATOR);
 50  
             }
 51  0
             dirName.append(templateLoadingClass.getPackage().getName().replace('.', SystemUtils.FILE_SEPARATOR.charAt(0)));
 52  0
             cfg.setDirectoryForTemplateLoading( new File(dirName.toString()));
 53  
         }
 54  
         
 55  258
         DefaultObjectWrapper  myObjWrapper = new  DefaultObjectWrapper();
 56  258
         myObjWrapper.setNullModel(TemplateScalarModel.EMPTY_STRING);
 57  258
         cfg.setObjectWrapper(myObjWrapper);
 58  
                 
 59  258
         Template temp = cfg.getTemplate(templateName);
 60  258
         return temp;
 61  
     }
 62  
 
 63  
     public static void addConfiguration(Map<String, Object> variableMap) {
 64  
         try {
 65  9
             variableMap.put("configuration", BeansWrapper.getDefaultInstance().getStaticModels().get("net.admin4j.config.Admin4JConfiguration"));
 66  0
         } catch (TemplateModelException e) {
 67  0
             throw new Admin4jRuntimeException(e);
 68  9
         }
 69  9
     }
 70  
 
 71  
 }