| 1 | package fr.sii.ogham.core.template.parser; | |
| 2 | ||
| 3 | import java.util.List; | |
| 4 | ||
| 5 | import org.slf4j.Logger; | |
| 6 | import org.slf4j.LoggerFactory; | |
| 7 | ||
| 8 | import fr.sii.ogham.core.exception.template.EngineDetectionException; | |
| 9 | import fr.sii.ogham.core.exception.template.NoEngineDetectionException; | |
| 10 | import fr.sii.ogham.core.exception.template.ParseException; | |
| 11 | import fr.sii.ogham.core.message.content.Content; | |
| 12 | import fr.sii.ogham.core.resource.path.ResourcePath; | |
| 13 | import fr.sii.ogham.core.template.context.Context; | |
| 14 | import fr.sii.ogham.core.template.detector.TemplateEngineDetector; | |
| 15 | ||
| 16 | /** | |
| 17 | * Decorator that automatically detects the template engine parser to use. The | |
| 18 | * auto-detection is based on pairs of engine detector and associated template | |
| 19 | * engine parser. | |
| 20 | * | |
| 21 | * The detection mechanism loop through the engine detectors until one indicates | |
| 22 | * that the associated engine can parse the template. | |
| 23 | * | |
| 24 | * @author Aurélien Baudet | |
| 25 | * | |
| 26 | */ | |
| 27 | public class AutoDetectTemplateParser implements TemplateParser { | |
| 28 | private static final Logger LOG = LoggerFactory.getLogger(AutoDetectTemplateParser.class); | |
| 29 | | |
| 30 | ||
| 31 | /** | |
| 32 | * The pairs of engine detector and template engine parser | |
| 33 | */ | |
| 34 | private List<TemplateImplementation> implementations; | |
| 35 | ||
| 36 | public AutoDetectTemplateParser(List<TemplateImplementation> implementations) { | |
| 37 | super(); | |
| 38 | this.implementations = implementations; | |
| 39 | } | |
| 40 | ||
| 41 | @Override | |
| 42 | public Content parse(ResourcePath templatePath, Context ctx) throws ParseException { | |
| 43 | try { | |
| 44 | LOG.debug("Start template engine automatic detection for {}", templatePath); | |
| 45 | TemplateParser parser = findParser(templatePath, ctx); | |
| 46 | LOG.info("Parse the template {} using template engine {}", templatePath, parser); | |
| 47 |
4
1. parse : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::parse → NO_COVERAGE 2. parse : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::parse → SURVIVED 3. parse : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::parse → TIMED_OUT 4. parse : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::parse → KILLED |
return parser.parse(templatePath, ctx); |
| 48 | } catch (EngineDetectionException e) { | |
| 49 | throw new ParseException("Failed to automatically detect parser due to detection error", templatePath, ctx, e); | |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | private TemplateParser findParser(ResourcePath templatePath, Context ctx) throws EngineDetectionException { | |
| 54 | for (TemplateImplementation impl : implementations) { | |
| 55 |
4
1. findParser : negated conditional → NO_COVERAGE 2. findParser : negated conditional → TIMED_OUT 3. findParser : negated conditional → KILLED 4. findParser : negated conditional → KILLED |
if (impl.getDetector().canParse(templatePath, ctx)) { |
| 56 | TemplateParser parser = impl.getParser(); | |
| 57 | LOG.debug("Template engine {} is used for {}", parser, templatePath); | |
| 58 |
4
1. findParser : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::findParser → NO_COVERAGE 2. findParser : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::findParser → TIMED_OUT 3. findParser : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::findParser → KILLED 4. findParser : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::findParser → KILLED |
return parser; |
| 59 | } else { | |
| 60 | LOG.debug("Template engine {} can't be used for {}", impl.getParser(), templatePath); | |
| 61 | } | |
| 62 | } | |
| 63 | throw new NoEngineDetectionException("Auto detection couldn't find any parser able to handle the template " + templatePath.getOriginalPath() + ".\n" | |
| 64 | + "Either the template uses a template engine that is not registered in Ogham or the path points to a non existing template."); | |
| 65 | } | |
| 66 | | |
| 67 | ||
| 68 | @Override | |
| 69 | public String toString() { | |
| 70 |
3
1. toString : replaced return value with "" for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::toString → SURVIVED 2. toString : replaced return value with "" for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::toString → NO_COVERAGE 3. toString : replaced return value with "" for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::toString → TIMED_OUT |
return "AutoDetectTemplateParser (" + implementations + ")"; |
| 71 | } | |
| 72 | ||
| 73 | ||
| 74 | ||
| 75 | public static class TemplateImplementation { | |
| 76 | private final TemplateEngineDetector detector; | |
| 77 | private final TemplateParser parser; | |
| 78 | public TemplateImplementation(TemplateEngineDetector detector, TemplateParser parser) { | |
| 79 | super(); | |
| 80 | this.detector = detector; | |
| 81 | this.parser = parser; | |
| 82 | } | |
| 83 | public TemplateEngineDetector getDetector() { | |
| 84 |
4
1. getDetector : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::getDetector → NO_COVERAGE 2. getDetector : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::getDetector → TIMED_OUT 3. getDetector : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::getDetector → KILLED 4. getDetector : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::getDetector → KILLED |
return detector; |
| 85 | } | |
| 86 | public TemplateParser getParser() { | |
| 87 |
4
1. getParser : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::getParser → NO_COVERAGE 2. getParser : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::getParser → TIMED_OUT 3. getParser : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::getParser → KILLED 4. getParser : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::getParser → KILLED |
return parser; |
| 88 | } | |
| 89 | @Override | |
| 90 | public String toString() { | |
| 91 |
3
1. toString : replaced return value with "" for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::toString → SURVIVED 2. toString : replaced return value with "" for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::toString → NO_COVERAGE 3. toString : replaced return value with "" for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::toString → TIMED_OUT |
return "TemplateImplementation {detector:" + detector + ", parser:" + parser + "}"; |
| 92 | } | |
| 93 | } | |
| 94 | } | |
Mutations | ||
| 47 |
1.1 2.2 3.3 4.4 |
|
| 55 |
1.1 2.2 3.3 4.4 |
|
| 58 |
1.1 2.2 3.3 4.4 |
|
| 70 |
1.1 2.2 3.3 |
|
| 84 |
1.1 2.2 3.3 4.4 |
|
| 87 |
1.1 2.2 3.3 4.4 |
|
| 91 |
1.1 2.2 3.3 |