| 1 | package fr.sii.ogham.template.freemarker; | |
| 2 | ||
| 3 | import static java.util.Arrays.asList; | |
| 4 | ||
| 5 | import java.util.List; | |
| 6 | ||
| 7 | import org.slf4j.Logger; | |
| 8 | import org.slf4j.LoggerFactory; | |
| 9 | ||
| 10 | import fr.sii.ogham.core.exception.resource.ResourceResolutionException; | |
| 11 | import fr.sii.ogham.core.exception.template.EngineDetectionException; | |
| 12 | import fr.sii.ogham.core.resource.path.ResolvedPath; | |
| 13 | import fr.sii.ogham.core.resource.path.ResourcePath; | |
| 14 | import fr.sii.ogham.core.resource.resolver.ResourceResolver; | |
| 15 | import fr.sii.ogham.core.template.context.Context; | |
| 16 | import fr.sii.ogham.core.template.detector.TemplateEngineDetector; | |
| 17 | ||
| 18 | /** | |
| 19 | * Detector checking template name extension. If it ends with '.ftl' then the | |
| 20 | * detector returns true. Otherwise it returns false. | |
| 21 | * | |
| 22 | * @author Cyril Dejonghe | |
| 23 | * | |
| 24 | */ | |
| 25 | public class FreeMarkerTemplateDetector implements TemplateEngineDetector { | |
| 26 | private static final Logger LOG = LoggerFactory.getLogger(FreeMarkerTemplateDetector.class); | |
| 27 | ||
| 28 | /** | |
| 29 | * The template resolver used to find the template | |
| 30 | */ | |
| 31 | private final ResourceResolver resolver; | |
| 32 | ||
| 33 | /** Recognized FTL extensions. */ | |
| 34 | private List<String> extensions; | |
| 35 | ||
| 36 | public FreeMarkerTemplateDetector(ResourceResolver resolver) { | |
| 37 | this(resolver, ".ftl", ".ftlh"); | |
| 38 | } | |
| 39 | ||
| 40 | public FreeMarkerTemplateDetector(ResourceResolver resolver, String... extensions) { | |
| 41 | super(); | |
| 42 | this.resolver = resolver; | |
| 43 | this.extensions = asList(extensions); | |
| 44 | } | |
| 45 | ||
| 46 | @Override | |
| 47 | public boolean canParse(ResourcePath templatePath, Context ctx) throws EngineDetectionException { | |
| 48 | LOG.debug("Checking if FreeMarker can handle the template {}", templatePath); | |
| 49 | ||
| 50 | ResolvedPath resolvedTemplatePath = resolver.resolve(templatePath); | |
| 51 |
3
1. canParse : negated conditional → TIMED_OUT 2. canParse : negated conditional → KILLED 3. canParse : negated conditional → KILLED |
if (resolvedTemplatePath == null) { |
| 52 |
1
1. canParse : replaced boolean return with true for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::canParse → NO_COVERAGE |
return false; |
| 53 | } | |
| 54 | ||
| 55 | for (String extension : extensions) { | |
| 56 |
5
1. canParse : negated conditional → SURVIVED 2. canParse : negated conditional → TIMED_OUT 3. canParse : negated conditional → KILLED 4. canParse : negated conditional → KILLED 5. canParse : negated conditional → KILLED |
if (resolvedTemplatePath.getResolvedPath().endsWith(extension) && exists(resolvedTemplatePath)) { |
| 57 | LOG.debug("The template {} ends with {}. FreeMarker can be used", templatePath, extension); | |
| 58 |
4
1. canParse : replaced boolean return with false for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::canParse → KILLED 2. canParse : replaced boolean return with false for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::canParse → KILLED 3. canParse : replaced boolean return with false for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::canParse → KILLED 4. canParse : replaced boolean return with false for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::canParse → KILLED |
return true; |
| 59 | } | |
| 60 | } | |
| 61 | ||
| 62 | LOG.debug("The template {} doesn't end with any of {}. FreeMarker can't be used", templatePath, extensions); | |
| 63 |
3
1. canParse : replaced boolean return with true for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::canParse → NO_COVERAGE 2. canParse : replaced boolean return with true for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::canParse → KILLED 3. canParse : replaced boolean return with true for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::canParse → KILLED |
return false; |
| 64 | ||
| 65 | } | |
| 66 | ||
| 67 | @SuppressWarnings("squid:S1166") | |
| 68 | private boolean exists(ResolvedPath resolvedTemplatePath) { | |
| 69 | try { | |
| 70 | resolver.getResource(resolvedTemplatePath); | |
| 71 |
4
1. exists : replaced boolean return with false for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::exists → KILLED 2. exists : replaced boolean return with false for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::exists → KILLED 3. exists : replaced boolean return with false for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::exists → KILLED 4. exists : replaced boolean return with false for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::exists → KILLED |
return true; |
| 72 | } catch(ResourceResolutionException e) { | |
| 73 |
3
1. exists : replaced boolean return with true for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::exists → NO_COVERAGE 2. exists : replaced boolean return with true for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::exists → KILLED 3. exists : replaced boolean return with true for fr/sii/ogham/template/freemarker/FreeMarkerTemplateDetector::exists → KILLED |
return false; |
| 74 | } | |
| 75 | } | |
| 76 | ||
| 77 | } | |
Mutations | ||
| 51 |
1.1 2.2 3.3 |
|
| 52 |
1.1 |
|
| 56 |
1.1 2.2 3.3 4.4 5.5 |
|
| 58 |
1.1 2.2 3.3 4.4 |
|
| 63 |
1.1 2.2 3.3 |
|
| 71 |
1.1 2.2 3.3 4.4 |
|
| 73 |
1.1 2.2 3.3 |