ThymeleafV2TemplateDetector.java

1
package fr.sii.ogham.template.thymeleaf.v2;
2
3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.io.InputStreamReader;
7
import java.util.regex.Pattern;
8
9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11
12
import fr.sii.ogham.core.exception.resource.ResourceResolutionException;
13
import fr.sii.ogham.core.exception.template.EngineDetectionException;
14
import fr.sii.ogham.core.resource.Resource;
15
import fr.sii.ogham.core.resource.path.ResourcePath;
16
import fr.sii.ogham.core.resource.resolver.ResourceResolver;
17
import fr.sii.ogham.core.template.context.Context;
18
import fr.sii.ogham.core.template.detector.TemplateEngineDetector;
19
20
/**
21
 * Detector that reads the content of the template. If the template contains the
22
 * Thymeleaf namespace (http://www.thymeleaf.org) then the detector returns
23
 * true. Otherwise it returns false.
24
 * 
25
 * @author Aurélien Baudet
26
 *
27
 */
28
public class ThymeleafV2TemplateDetector implements TemplateEngineDetector {
29
	private static final Logger LOG = LoggerFactory.getLogger(ThymeleafV2TemplateDetector.class);
30
31
	/**
32
	 * The pattern to search into the template
33
	 */
34
	private static final Pattern NAMESPACE_PATTERN = Pattern.compile("xmlns[^=]+=\\s*\"http://www.thymeleaf.org\"");
35
36
	/**
37
	 * The template resolver used to find the template
38
	 */
39
	private final ResourceResolver resolver;
40
41
	public ThymeleafV2TemplateDetector(ResourceResolver resolver) {
42
		super();
43
		this.resolver = resolver;
44
	}
45
46
	@Override
47
	public boolean canParse(ResourcePath template, Context ctx) throws EngineDetectionException {
48
		LOG.debug("Checking if Thymeleaf can handle the template {}", template);
49
		Resource resolvedTemplate = getTemplate(template);
50 1 1. canParse : negated conditional → RUN_ERROR
		if (resolvedTemplate == null) {
51 1 1. canParse : replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → RUN_ERROR
			return false;
52
		}
53
		try (BufferedReader br = new BufferedReader(new InputStreamReader(resolvedTemplate.getInputStream()))) {
54
			boolean containsThymeleafNamespace = containsThymeleafNamespace(br);
55 1 1. canParse : negated conditional → RUN_ERROR
			if (containsThymeleafNamespace) {
56
				LOG.debug("The template {} contains the namespace http://www.thymeleaf.org. Thymeleaf can be used", template);
57
			} else {
58
				LOG.debug("The template {} doesn't contain the namespace http://www.thymeleaf.org. Thymeleaf can't be used", template);
59
			}
60 2 1. canParse : negated conditional → RUN_ERROR
2. canParse : negated conditional → RUN_ERROR
			return containsThymeleafNamespace || isEmptyTemplate(resolvedTemplate);
61
		} catch (IOException e) {
62
			throw new EngineDetectionException("Failed to detect because template can't be read by thymeleaf", e);
63
		}
64
	}
65
66
	private static boolean containsThymeleafNamespace(BufferedReader br) throws IOException {
67
		String line;
68
		do {
69
			line = br.readLine();
70 2 1. containsThymeleafNamespace : negated conditional → RUN_ERROR
2. containsThymeleafNamespace : negated conditional → RUN_ERROR
			if (line != null && NAMESPACE_PATTERN.matcher(line).find()) {
71 1 1. containsThymeleafNamespace : replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → RUN_ERROR
				return true;
72
			}
73 1 1. containsThymeleafNamespace : negated conditional → RUN_ERROR
		} while (line != null);
74 1 1. containsThymeleafNamespace : replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → RUN_ERROR
		return false;
75
	}
76
77
78
	private static boolean isEmptyTemplate(Resource template) throws IOException {
79
		try (InputStream stream = template.getInputStream()) {
80 1 1. isEmptyTemplate : negated conditional → RUN_ERROR
			return stream.read() == -1;
81
		}
82
	}
83
84
	private Resource getTemplate(ResourcePath templateName) {
85
		try {
86 1 1. getTemplate : replaced return value with null for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::getTemplate → RUN_ERROR
			return resolver.getResource(templateName);
87
		} catch (ResourceResolutionException e) {
88
			LOG.trace("Thymeleaf detector can't be applied because {} couldn't be resolved", templateName, e);
89
			return null;
90
		}
91
	}
92
93
}

Mutations

50

1.1
Location : canParse
Killed by :
negated conditional → RUN_ERROR

51

1.1
Location : canParse
Killed by :
replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::canParse → RUN_ERROR

55

1.1
Location : canParse
Killed by :
negated conditional → RUN_ERROR

60

1.1
Location : canParse
Killed by :
negated conditional → RUN_ERROR

2.2
Location : canParse
Killed by :
negated conditional → RUN_ERROR

70

1.1
Location : containsThymeleafNamespace
Killed by :
negated conditional → RUN_ERROR

2.2
Location : containsThymeleafNamespace
Killed by :
negated conditional → RUN_ERROR

71

1.1
Location : containsThymeleafNamespace
Killed by :
replaced boolean return with false for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → RUN_ERROR

73

1.1
Location : containsThymeleafNamespace
Killed by :
negated conditional → RUN_ERROR

74

1.1
Location : containsThymeleafNamespace
Killed by :
replaced boolean return with true for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::containsThymeleafNamespace → RUN_ERROR

80

1.1
Location : isEmptyTemplate
Killed by :
negated conditional → RUN_ERROR

86

1.1
Location : getTemplate
Killed by :
replaced return value with null for fr/sii/ogham/template/thymeleaf/v2/ThymeleafV2TemplateDetector::getTemplate → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1