| 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 | 873 | this(FreemarkerProcessor.class); |
| 14 | 873 | } |
| 15 | |
|
| 16 | 885 | public FreemarkerProcessor(Class<?> templateLoadingFrom) { |
| 17 | 885 | this.templateLoadingFrom = templateLoadingFrom; |
| 18 | 885 | } |
| 19 | |
|
| 20 | |
public void process(String resource, Map<String, Object> dataModel, Writer writer) { |
| 21 | 62 | Configuration configuration = configuration(); |
| 22 | |
try { |
| 23 | 62 | configuration.getTemplate(resource).process(dataModel, writer); |
| 24 | 1 | } catch (Exception e) { |
| 25 | 1 | throw new FreemarkerProcessingFailed(configuration, resource, dataModel, e); |
| 26 | 61 | } |
| 27 | 61 | } |
| 28 | |
|
| 29 | |
public Configuration configuration() { |
| 30 | 62 | Configuration configuration = new Configuration(); |
| 31 | 62 | configuration.setClassForTemplateLoading(templateLoadingFrom, "/"); |
| 32 | 62 | configuration.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER); |
| 33 | 62 | 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 | |
} |