1 package info.magnolia.cms.mail.templates.impl; 2 3 import freemarker.cache.ClassTemplateLoader; 4 import freemarker.cache.MultiTemplateLoader; 5 import freemarker.cache.TemplateLoader; 6 import freemarker.template.Configuration; 7 import freemarker.template.DefaultObjectWrapper; 8 import freemarker.template.Template; 9 10 import java.io.StringWriter ; 11 import java.util.Map ; 12 13 import javax.mail.Session ; 14 15 16 20 public class FreemarkerEmail extends HtmlEmail { 21 22 public FreemarkerEmail(Session _session) throws Exception { 23 super(_session); 24 } 25 26 static Configuration cfg = new Configuration(); 27 28 static { 29 cfg.setObjectWrapper(new DefaultObjectWrapper()); 30 try { 31 ClassTemplateLoader ctl = new ClassTemplateLoader(FreemarkerEmail.class, "/"); 32 TemplateLoader[] loaders = new TemplateLoader[]{ctl}; 33 MultiTemplateLoader mtl = new MultiTemplateLoader(loaders); 34 cfg.setTemplateLoader(mtl); 35 cfg.setDefaultEncoding("UTF8"); 36 } 37 catch (Exception e) { 38 log.error("Error while loading freemarker configuration", e); 39 } 40 } 41 42 public void setBodyFromResourceFile(String resourceFile, Map _map) throws Exception { 43 Template late = cfg.getTemplate(resourceFile); 44 setBodyFromTemplate(late, _map); 45 } 46 47 public void setBodyFromTemplate(Template template, Map _map) throws Exception { 48 StringWriter writer = new StringWriter (); 49 template.process(_map, writer); 50 writer.flush(); 51 super.setBody(writer.toString(), _map); 52 } 53 } 54 | Popular Tags |