| 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 | 971 | public class FreemarkerProcessor implements TemplateProcessor { |
| 10 | |
|
| 11 | |
public void process(String resource, Map<String, Object> dataModel, Writer writer) { |
| 12 | 54 | Configuration configuration = configuration(); |
| 13 | |
try { |
| 14 | 54 | configuration.getTemplate(resource).process(dataModel, writer); |
| 15 | 1 | } catch (Exception e) { |
| 16 | 1 | throw new FreemarkerProcessingFailed(configuration, resource, dataModel, e); |
| 17 | 53 | } |
| 18 | 53 | } |
| 19 | |
|
| 20 | |
public Configuration configuration() { |
| 21 | 54 | Configuration configuration = new Configuration(); |
| 22 | 54 | configuration.setClassForTemplateLoading(FreemarkerProcessor.class, "/"); |
| 23 | 54 | configuration.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER); |
| 24 | 54 | return configuration; |
| 25 | |
} |
| 26 | |
|
| 27 | 971 | @SuppressWarnings("serial") |
| 28 | |
public static class FreemarkerProcessingFailed extends RuntimeException { |
| 29 | |
|
| 30 | |
public FreemarkerProcessingFailed(Configuration configuration, String resource, Map<String, Object> dataModel, Exception cause) { |
| 31 | 1 | super("Freemarker failed to process template " + resource + " using configuration "+configuration + " and data model "+dataModel, cause); |
| 32 | 1 | } |
| 33 | |
|
| 34 | |
} |
| 35 | |
|
| 36 | |
} |