1 24 package org.riotfamily.common.web.view.freemarker; 25 26 import java.io.IOException ; 27 import java.util.Enumeration ; 28 import java.util.List ; 29 import java.util.Properties ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; 34 35 import freemarker.template.Configuration; 36 import freemarker.template.ObjectWrapper; 37 import freemarker.template.TemplateException; 38 import freemarker.template.TemplateExceptionHandler; 39 40 44 public class RiotFreeMarkerConfigurer extends FreeMarkerConfigurer { 45 46 private static final Log log = LogFactory 47 .getLog(RiotFreeMarkerConfigurer.class); 48 49 private TemplateExceptionHandler exceptionHandler = 50 new ErrorPrintingExceptionHandler(); 51 52 private Properties macroLibraries; 53 54 private ObjectWrapper objectWrapper; 55 56 private boolean whitespaceStripping = false; 57 58 61 public void setMacroLibraries(Properties macroLibraries) { 62 this.macroLibraries = macroLibraries; 63 } 64 65 69 public void setExceptionHandler(TemplateExceptionHandler exceptionHandler) { 70 this.exceptionHandler = exceptionHandler; 71 } 72 73 77 public void setObjectWrapper(ObjectWrapper objectWrapper) { 78 this.objectWrapper = objectWrapper; 79 } 80 81 85 public void setWhitespaceStripping(boolean whitespaceStripping) { 86 this.whitespaceStripping = whitespaceStripping; 87 } 88 89 protected void postProcessTemplateLoaders(List templateLoaders) { 90 super.postProcessTemplateLoaders(templateLoaders); 91 templateLoaders.add(new ResourceTemplateLoader(getResourceLoader())); 92 } 93 94 protected void postProcessConfiguration(Configuration config) 95 throws IOException , TemplateException { 96 97 config.setWhitespaceStripping(whitespaceStripping); 98 importMacroLibraries(config); 99 config.setTemplateExceptionHandler(exceptionHandler); 100 if (objectWrapper != null) { 101 config.setObjectWrapper(objectWrapper); 102 } 103 } 104 105 protected void importMacroLibraries(Configuration config) { 106 if (macroLibraries != null) { 107 Enumeration names = macroLibraries.propertyNames(); 108 while (names.hasMoreElements()) { 109 String namespace = (String ) names.nextElement(); 110 String lib = macroLibraries.getProperty(namespace); 111 log.info(lib + " imported under namespace " + namespace); 112 config.addAutoImport(namespace, lib); 113 } 114 } 115 } 116 117 } 118 | Popular Tags |