KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > mail > templates > impl > FreemarkerEmail


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 JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import javax.mail.Session JavaDoc;
14
15
16 /**
17  * Date: Apr 5, 2006 Time: 8:59:18 PM
18  * @author <a HREF="mailto:niko@macnica.com">Nicolas Modrzyk</a>
19  */

20 public class FreemarkerEmail extends HtmlEmail {
21
22     public FreemarkerEmail(Session JavaDoc _session) throws Exception JavaDoc {
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 JavaDoc e) {
38             log.error("Error while loading freemarker configuration", e);
39         }
40     }
41
42     public void setBodyFromResourceFile(String JavaDoc resourceFile, Map JavaDoc _map) throws Exception JavaDoc {
43         Template late = cfg.getTemplate(resourceFile);
44         setBodyFromTemplate(late, _map);
45     }
46
47     public void setBodyFromTemplate(Template template, Map JavaDoc _map) throws Exception JavaDoc {
48         StringWriter JavaDoc writer = new StringWriter JavaDoc();
49         template.process(_map, writer);
50         writer.flush();
51         super.setBody(writer.toString(), _map);
52     }
53 }
54
Popular Tags