1 | |
package org.jbehave.core.reporters; |
2 | |
|
3 | |
import java.io.Writer; |
4 | |
import java.util.Map; |
5 | |
|
6 | |
import freemarker.template.Configuration; |
7 | |
import freemarker.template.ObjectWrapper; |
8 | |
|
9 | |
public class FreemarkerProcessor implements TemplateProcessor { |
10 | |
private Class<?> templateLoadingFrom; |
11 | |
|
12 | |
public FreemarkerProcessor() { |
13 | 837 | this(FreemarkerProcessor.class); |
14 | 837 | } |
15 | |
|
16 | 849 | public FreemarkerProcessor(Class<?> templateLoadingFrom) { |
17 | 849 | this.templateLoadingFrom = templateLoadingFrom; |
18 | 849 | } |
19 | |
|
20 | |
public void process(String resource, Map<String, Object> dataModel, Writer writer) { |
21 | 70 | Configuration configuration = configuration(); |
22 | |
try { |
23 | 70 | configuration.getTemplate(resource).process(dataModel, writer); |
24 | 1 | } catch (Exception e) { |
25 | 1 | throw new FreemarkerProcessingFailed(configuration, resource, dataModel, e); |
26 | 69 | } |
27 | 69 | } |
28 | |
|
29 | |
public Configuration configuration() { |
30 | 70 | Configuration configuration = new Configuration(); |
31 | 70 | configuration.setClassForTemplateLoading(templateLoadingFrom, "/"); |
32 | 70 | configuration.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER); |
33 | 70 | return configuration; |
34 | |
} |
35 | |
|
36 | |
@SuppressWarnings("serial") |
37 | |
public static class FreemarkerProcessingFailed extends RuntimeException { |
38 | |
|
39 | |
public FreemarkerProcessingFailed(Configuration configuration, String resource, Map<String, Object> dataModel, Exception cause) { |
40 | 1 | super("Freemarker failed to process template " + resource + " using configuration "+configuration + " and data model "+dataModel, cause); |
41 | 1 | } |
42 | |
|
43 | |
} |
44 | |
|
45 | |
} |