| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 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 | |
|
| 32 | |
|
| 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 | |
} |