1 package br.com.ibnetwork.guara.view.impl.freemarker; 2 3 import java.io.File; 4 import java.net.URL; 5 import java.util.HashMap; 6 import java.util.Iterator; 7 import java.util.Map; 8 import java.util.Properties; 9 10 import org.apache.avalon.framework.service.ServiceException; 11 import org.apache.avalon.framework.service.ServiceManager; 12 import org.apache.avalon.framework.service.Serviceable; 13 import org.apache.commons.lang.StringUtils; 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 17 import br.com.ibnetwork.xingu.factory.Factory; 18 import br.com.ibnetwork.xingu.utils.ObjectUtils; 19 20 import freemarker.cache.CacheStorage; 21 import freemarker.template.Configuration; 22 import freemarker.template.TemplateExceptionHandler; 23 24 public class FreemarkerConfiguration 25 implements Serviceable 26 { 27 private Log log = LogFactory.getLog(this.getClass()); 28 29 private Factory factory; 30 31 private Map methods = new HashMap(); 32 33 public void service(ServiceManager manager) 34 throws ServiceException 35 { 36 factory = (Factory) manager.lookup(Factory.ROLE); 37 } 38 39 public void configureTemplateEngine(URL url,Configuration cfg) 40 throws Exception 41 { 42 log.debug("Loading freemarker configuration from: "+url); 43 Properties props = new Properties(); 44 props.load(url.openStream()); 45 for (Iterator iter = props.keySet().iterator(); iter.hasNext();) 46 { 47 String key = (String) iter.next(); 48 String value = StringUtils.trimToNull(props.getProperty(key)); 49 if(value != null) 50 { 51 updateEntry(cfg, key,value); 52 } 53 } 54 } 55 56 private void updateEntry(Configuration cfg, String key, String value) 57 throws Exception 58 { 59 log.debug("Configuration key["+key+"] value["+value+"]"); 60 if("cacheStorage".equalsIgnoreCase(key)) 61 { 62 CacheStorage obj = (CacheStorage) createObject(value); 63 cfg.setCacheStorage(obj); 64 } 65 else if("defaultEncoding".equalsIgnoreCase(key)) 66 { 67 cfg.setDefaultEncoding(value); 68 } 69 else if("directoryForTemplateLoading".equalsIgnoreCase(key)) 70 { 71 cfg.setDirectoryForTemplateLoading(new File(value)); 72 } 73 else if("strictSyntaxMode".equalsIgnoreCase(key)) 74 { 75 cfg.setStrictSyntaxMode(new Boolean(value).booleanValue()); 76 } 77 else if("whitespaceStripping".equalsIgnoreCase(key)) 78 { 79 cfg.setWhitespaceStripping(new Boolean(value).booleanValue()); 80 } 81 else if("exceptionHandler".equalsIgnoreCase(key)) 82 { 83 TemplateExceptionHandler handler = (TemplateExceptionHandler) createObject(value); 84 cfg.setTemplateExceptionHandler(handler); 85 } 86 else if(key.startsWith("autoImport")) 87 { 88 String nameSpace = key.substring(key.indexOf(".") + 1); 89 cfg.addAutoImport(nameSpace,value); 90 } 91 else if(key.startsWith("method")) 92 { 93 String methodName = key.substring(key.indexOf(".") + 1); 94 Object method = ObjectUtils.getInstance(value); 95 methods.put(methodName,method); 96 } 97 else 98 { 99 cfg.setSetting(key,value); 100 } 101 } 102 103 private Object createObject(String className) 104 { 105 return factory.create(className); 106 } 107 108 public Map getMethods() 109 { 110 return methods; 111 } 112 }