1 | package fr.sii.ogham.template.freemarker; | |
2 | ||
3 | import static fr.sii.ogham.template.freemarker.FreemarkerConstants.DEFAULT_FREEMARKER_IMPLEMENTATION_PRIORITY; | |
4 | ||
5 | import java.io.IOException; | |
6 | import java.io.StringWriter; | |
7 | ||
8 | import org.slf4j.Logger; | |
9 | import org.slf4j.LoggerFactory; | |
10 | ||
11 | import fr.sii.ogham.core.builder.priority.Priority; | |
12 | import fr.sii.ogham.core.exception.template.ContextException; | |
13 | import fr.sii.ogham.core.exception.template.ParseException; | |
14 | import fr.sii.ogham.core.message.content.Content; | |
15 | import fr.sii.ogham.core.message.content.ParsedContent; | |
16 | import fr.sii.ogham.core.resource.path.ResourcePath; | |
17 | import fr.sii.ogham.core.resource.resolver.ResourceResolver; | |
18 | import fr.sii.ogham.core.template.context.Context; | |
19 | import fr.sii.ogham.core.template.context.LocaleContext; | |
20 | import fr.sii.ogham.core.template.parser.TemplateParser; | |
21 | import freemarker.template.Configuration; | |
22 | import freemarker.template.Template; | |
23 | import freemarker.template.TemplateException; | |
24 | ||
25 | /** | |
26 | * Implementation for FreeMarker template engine. | |
27 | * | |
28 | * @author Cyril Dejonghe | |
29 | * | |
30 | */ | |
31 | @Priority(properties = "${ogham.template.implementation-priority.freemarker}", defaultValue = DEFAULT_FREEMARKER_IMPLEMENTATION_PRIORITY) | |
32 | public class FreeMarkerParser implements TemplateParser { | |
33 | private static final Logger LOG = LoggerFactory.getLogger(FreeMarkerParser.class); | |
34 | ||
35 | private final Configuration configuration; | |
36 | private final ResourceResolver resolver; | |
37 | ||
38 | public FreeMarkerParser(Configuration configuration, ResourceResolver resolver) { | |
39 | super(); | |
40 | this.configuration = configuration; | |
41 | this.resolver = resolver; | |
42 | } | |
43 | ||
44 | @Override | |
45 | public Content parse(ResourcePath templatePath, Context ctx) throws ParseException { | |
46 | LOG.debug("Parsing FreeMarker template {} with context {}...", templatePath, ctx); | |
47 | ||
48 | try { | |
49 | Template template = configuration.getTemplate(templatePath.getOriginalPath()); | |
50 |
4
1. parse : negated conditional → KILLED 2. parse : negated conditional → KILLED 3. parse : negated conditional → KILLED 4. parse : negated conditional → KILLED |
if (ctx instanceof LocaleContext) { |
51 |
2
1. parse : removed call to freemarker/template/Template::setLocale → NO_COVERAGE 2. parse : removed call to freemarker/template/Template::setLocale → KILLED |
template.setLocale(((LocaleContext) ctx).getLocale()); |
52 | } | |
53 | StringWriter out = new StringWriter(); | |
54 |
4
1. parse : removed call to freemarker/template/Template::process → KILLED 2. parse : removed call to freemarker/template/Template::process → KILLED 3. parse : removed call to freemarker/template/Template::process → KILLED 4. parse : removed call to freemarker/template/Template::process → KILLED |
template.process(ctx.getVariables(), out); |
55 | ||
56 | LOG.debug("Template {} successfully parsed with context {}. Result:", templatePath, ctx); | |
57 | String templateString = out.toString(); | |
58 | LOG.debug("{}", templateString); | |
59 |
4
1. parse : replaced return value with null for fr/sii/ogham/template/freemarker/FreeMarkerParser::parse → TIMED_OUT 2. parse : replaced return value with null for fr/sii/ogham/template/freemarker/FreeMarkerParser::parse → KILLED 3. parse : replaced return value with null for fr/sii/ogham/template/freemarker/FreeMarkerParser::parse → KILLED 4. parse : replaced return value with null for fr/sii/ogham/template/freemarker/FreeMarkerParser::parse → KILLED |
return new ParsedContent(resolver.resolve(templatePath), ctx, templateString); |
60 | ||
61 | } catch (IOException | TemplateException e) { | |
62 | throw new ParseException("Failed to parse template with FreeMarker", templatePath, ctx, e); | |
63 | } catch (ContextException e) { | |
64 | throw new ParseException("Failed to parse template with FreeMarker due to conversion error", templatePath, ctx, e); | |
65 | ||
66 | } | |
67 | } | |
68 | ||
69 | @Override | |
70 | public String toString() { | |
71 |
3
1. toString : replaced return value with "" for fr/sii/ogham/template/freemarker/FreeMarkerParser::toString → SURVIVED 2. toString : replaced return value with "" for fr/sii/ogham/template/freemarker/FreeMarkerParser::toString → NO_COVERAGE 3. toString : replaced return value with "" for fr/sii/ogham/template/freemarker/FreeMarkerParser::toString → TIMED_OUT |
return "FremarkerParser"; |
72 | } | |
73 | ||
74 | } | |
Mutations | ||
50 |
1.1 2.2 3.3 4.4 |
|
51 |
1.1 2.2 |
|
54 |
1.1 2.2 3.3 4.4 |
|
59 |
1.1 2.2 3.3 4.4 |
|
71 |
1.1 2.2 3.3 |