1 | package fr.sii.ogham.core.translator.content; | |
2 | ||
3 | import org.slf4j.Logger; | |
4 | import org.slf4j.LoggerFactory; | |
5 | ||
6 | import fr.sii.ogham.core.exception.handler.ContentTranslatorException; | |
7 | import fr.sii.ogham.core.exception.handler.TemplateNotFoundException; | |
8 | import fr.sii.ogham.core.exception.handler.TemplateParsingFailedException; | |
9 | import fr.sii.ogham.core.exception.template.ParseException; | |
10 | import fr.sii.ogham.core.message.content.Content; | |
11 | import fr.sii.ogham.core.message.content.TemplateContent; | |
12 | import fr.sii.ogham.core.message.content.TemplateVariantContent; | |
13 | import fr.sii.ogham.core.resource.path.ResourcePath; | |
14 | import fr.sii.ogham.core.template.context.Context; | |
15 | import fr.sii.ogham.core.template.parser.TemplateParser; | |
16 | import fr.sii.ogham.template.common.adapter.VariantResolver; | |
17 | import fr.sii.ogham.template.exception.TemplateVariantNotFoundException; | |
18 | import fr.sii.ogham.template.exception.VariantResolutionException; | |
19 | ||
20 | /** | |
21 | * <p> | |
22 | * Translator that handles {@link TemplateContent}. It parses the template and | |
23 | * provide an evaluated content. It also handles {@link TemplateVariantContent} | |
24 | * through {@link VariantResolver}. | |
25 | * </p> | |
26 | * <p> | |
27 | * The template parsing is delegated to a {@link TemplateParser}. | |
28 | * </p> | |
29 | * <p> | |
30 | * If the content is not a {@link TemplateContent}, then the content is returned | |
31 | * as-is | |
32 | * </p> | |
33 | * | |
34 | * @author Aurélien Baudet | |
35 | * | |
36 | */ | |
37 | public class TemplateContentTranslator implements ContentTranslator { | |
38 | private static final Logger LOG = LoggerFactory.getLogger(TemplateContentTranslator.class); | |
39 | ||
40 | /** | |
41 | * The parser to use for finding, loading and evaluating the template | |
42 | */ | |
43 | private final TemplateParser parser; | |
44 | ||
45 | /** | |
46 | * The resolver that converts partial path with variant into real path | |
47 | */ | |
48 | private final VariantResolver variantResolver; | |
49 | ||
50 | public TemplateContentTranslator(TemplateParser parser) { | |
51 | this(parser, null); | |
52 | } | |
53 | ||
54 | public TemplateContentTranslator(TemplateParser parser, VariantResolver variantResolver) { | |
55 | super(); | |
56 | this.parser = parser; | |
57 | this.variantResolver = variantResolver; | |
58 | } | |
59 | ||
60 | @Override | |
61 | public Content translate(Content content) throws ContentTranslatorException { | |
62 |
3
1. translate : negated conditional → NO_COVERAGE 2. translate : negated conditional → TIMED_OUT 3. translate : negated conditional → KILLED |
if (!(content instanceof TemplateContent)) { |
63 | LOG.trace("Not a TemplateContent => skip it"); | |
64 |
4
1. translate : replaced return value with null for fr/sii/ogham/core/translator/content/TemplateContentTranslator::translate → NO_COVERAGE 2. translate : replaced return value with null for fr/sii/ogham/core/translator/content/TemplateContentTranslator::translate → TIMED_OUT 3. translate : replaced return value with null for fr/sii/ogham/core/translator/content/TemplateContentTranslator::translate → KILLED 4. translate : replaced return value with null for fr/sii/ogham/core/translator/content/TemplateContentTranslator::translate → KILLED |
return content; |
65 | } | |
66 | try { | |
67 | TemplateContent template = (TemplateContent) content; | |
68 | ResourcePath realPath = getRealPath(template); | |
69 |
3
1. translate : negated conditional → NO_COVERAGE 2. translate : negated conditional → TIMED_OUT 3. translate : negated conditional → KILLED |
if (realPath == null) { |
70 | LOG.debug("No template found for {}", template.getPath()); | |
71 | throw new TemplateNotFoundException("Template not found for " + template.getPath().getOriginalPath()); | |
72 | } | |
73 | Context ctx = template.getContext(); | |
74 | LOG.info("Parse template {} using context {}", realPath, ctx); | |
75 | LOG.debug("Parse template content {} using {}", template, parser); | |
76 |
3
1. translate : replaced return value with null for fr/sii/ogham/core/translator/content/TemplateContentTranslator::translate → NO_COVERAGE 2. translate : replaced return value with null for fr/sii/ogham/core/translator/content/TemplateContentTranslator::translate → TIMED_OUT 3. translate : replaced return value with null for fr/sii/ogham/core/translator/content/TemplateContentTranslator::translate → KILLED |
return parser.parse(realPath, ctx); |
77 | } catch (TemplateVariantNotFoundException e) { | |
78 | LOG.debug("No template found for {} after trying to load from {}", e.getTemplatePath(), e.getResolvedPaths()); | |
79 | throw new TemplateNotFoundException("Template not found for " + e.getTemplatePath().getOriginalPath() + " after trying to load from " + e.getResolvedPaths(), e); | |
80 | } catch (ParseException e) { | |
81 | throw new TemplateParsingFailedException("failed to translate templated content", e); | |
82 | } | |
83 | } | |
84 | ||
85 | private ResourcePath getRealPath(TemplateContent template) throws VariantResolutionException { | |
86 |
3
1. getRealPath : negated conditional → NO_COVERAGE 2. getRealPath : negated conditional → TIMED_OUT 3. getRealPath : negated conditional → KILLED |
if (variantResolver == null) { |
87 |
3
1. getRealPath : replaced return value with null for fr/sii/ogham/core/translator/content/TemplateContentTranslator::getRealPath → NO_COVERAGE 2. getRealPath : replaced return value with null for fr/sii/ogham/core/translator/content/TemplateContentTranslator::getRealPath → TIMED_OUT 3. getRealPath : replaced return value with null for fr/sii/ogham/core/translator/content/TemplateContentTranslator::getRealPath → KILLED |
return template.getPath(); |
88 | } | |
89 |
3
1. getRealPath : replaced return value with null for fr/sii/ogham/core/translator/content/TemplateContentTranslator::getRealPath → NO_COVERAGE 2. getRealPath : replaced return value with null for fr/sii/ogham/core/translator/content/TemplateContentTranslator::getRealPath → TIMED_OUT 3. getRealPath : replaced return value with null for fr/sii/ogham/core/translator/content/TemplateContentTranslator::getRealPath → KILLED |
return variantResolver.getRealPath(template); |
90 | } | |
91 | ||
92 | @Override | |
93 | public String toString() { | |
94 |
3
1. toString : replaced return value with "" for fr/sii/ogham/core/translator/content/TemplateContentTranslator::toString → SURVIVED 2. toString : replaced return value with "" for fr/sii/ogham/core/translator/content/TemplateContentTranslator::toString → NO_COVERAGE 3. toString : replaced return value with "" for fr/sii/ogham/core/translator/content/TemplateContentTranslator::toString → TIMED_OUT |
return "TemplateContentTranslator"; |
95 | } | |
96 | ||
97 | } | |
Mutations | ||
62 |
1.1 2.2 3.3 |
|
64 |
1.1 2.2 3.3 4.4 |
|
69 |
1.1 2.2 3.3 |
|
76 |
1.1 2.2 3.3 |
|
86 |
1.1 2.2 3.3 |
|
87 |
1.1 2.2 3.3 |
|
89 |
1.1 2.2 3.3 |
|
94 |
1.1 2.2 3.3 |