1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.melati.template.freemarker;
44
45 import java.io.FileNotFoundException;
46 import java.util.HashMap;
47 import java.util.Map;
48
49 import org.apache.velocity.exception.ParseErrorException;
50 import org.apache.velocity.exception.ResourceNotFoundException;
51 import org.melati.poem.AccessPoemException;
52 import org.melati.template.AbstractTemplateEngine;
53
54 import org.melati.MelatiConfig;
55 import org.melati.template.NotFoundException;
56 import org.melati.template.Template;
57 import org.melati.template.TemplateContext;
58 import org.melati.template.TemplateEngine;
59 import org.melati.template.TemplateEngineException;
60 import org.melati.template.velocity.VelocityTemplate;
61 import org.melati.util.MelatiBugMelatiException;
62 import org.melati.util.MelatiStringWriter;
63 import org.melati.util.MelatiWriter;
64
65 import freemarker.ext.beans.BeansWrapper;
66 import freemarker.template.Configuration;
67
68
69
70
71
72
73
74
75 public class FreemarkerTemplateEngine extends AbstractTemplateEngine implements TemplateEngine {
76
77
78 public static final String NAME = "freemarker";
79
80 private Configuration config;
81
82
83
84
85 @Override
86 public void init(MelatiConfig melatiConfig) throws TemplateEngineException {
87 try {
88 config = new Configuration();
89 config.setClassForTemplateLoading(this.getClass(), "/");
90 BeansWrapper bw = new BeansWrapper();
91 bw.setExposureLevel(BeansWrapper.EXPOSE_ALL);
92 config.setObjectWrapper(bw);
93
94 } catch (Exception e) {
95 throw new TemplateEngineException(e);
96 }
97
98 }
99
100
101
102
103 @Override
104 public TemplateContext getTemplateContext() throws TemplateEngineException {
105 Map<String, Object> context = new HashMap<String, Object>();
106 return new FreemarkerServletTemplateContext(context);
107 }
108
109
110
111
112 @Override
113 public String getName() {
114 return NAME;
115 }
116
117
118
119
120 @Override
121 public String templateExtension() {
122 return ".fml";
123 }
124
125
126
127
128 @Override
129 public Template template(String templateName) throws NotFoundException {
130 freemarker.template.Template t;
131 try {
132 t = config.getTemplate(templateName);
133 return new FreemarkerTemplate(t);
134 } catch (FileNotFoundException e) {
135 if (templateName.endsWith(templateExtension())) {
136
137
138 String velocityTemplateName = templateName.substring(0, templateName
139 .lastIndexOf(templateExtension()))
140 + ".vm";
141 try {
142 return new VelocityTemplate(velocityTemplateName);
143 } catch (ParseErrorException p) {
144 throw new MelatiBugMelatiException(
145 "Problem converting a Velocity template to a Freemarker template: " + velocityTemplateName,
146 p);
147 } catch (ResourceNotFoundException e2) {
148 throw new NotFoundException("Could not find template " + templateName + " or " + velocityTemplateName);
149 }
150 } else throw new NotFoundException("Could not find template " + templateName, e);
151 } catch (Exception e) {
152 throw new TemplateEngineException(e);
153 }
154 }
155
156
157
158
159
160
161 @Override
162 public void expandTemplate(MelatiWriter out, String templateName, TemplateContext templateContext) throws NotFoundException {
163 expandTemplate(out, template(templateName), templateContext);
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177 @Override
178 public void expandTemplate(MelatiWriter out, Template template, TemplateContext templateContext) {
179 try {
180 template.write(out, templateContext);
181 } catch (TemplateEngineException problem) {
182 Exception underlying = problem.subException;
183 if (underlying instanceof AccessPoemException) {
184 throw (AccessPoemException)underlying;
185 }
186 throw problem;
187 }
188
189 }
190
191
192
193
194 @Override
195 public String expandedTemplate(Template template, TemplateContext templateContext) {
196 MelatiStringWriter s = new MelatiStringWriter();
197 expandTemplate(s, template, templateContext);
198 return s.toString();
199 }
200
201
202
203
204 @Override
205 public MelatiStringWriter getStringWriter() {
206 return new MelatiStringWriter();
207 }
208
209
210
211
212
213
214
215
216 @Override
217 public Object getEngine() {
218 return null;
219 }
220
221 }