ThymeleafParser.java

1
package fr.sii.ogham.template.thymeleaf.common;
2
3
import static fr.sii.ogham.template.thymeleaf.common.ThymeleafConstants.DEFAULT_THYMELEAF_IMPLEMENTATION_PRIORITY;
4
5
import org.slf4j.Logger;
6
import org.slf4j.LoggerFactory;
7
import org.thymeleaf.TemplateEngine;
8
import org.thymeleaf.exceptions.TemplateEngineException;
9
10
import fr.sii.ogham.core.builder.priority.Priority;
11
import fr.sii.ogham.core.exception.template.ContextException;
12
import fr.sii.ogham.core.exception.template.ParseException;
13
import fr.sii.ogham.core.message.content.Content;
14
import fr.sii.ogham.core.message.content.ParsedContent;
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.parser.TemplateParser;
19
import fr.sii.ogham.template.exception.TemplateRuntimeException;
20
21
/**
22
 * Implementation for Thymeleaf template engine.
23
 * 
24
 * @author Aurélien Baudet
25
 *
26
 */
27
@Priority(properties="${ogham.template.implementation-priority.thymeleaf}", defaultValue = DEFAULT_THYMELEAF_IMPLEMENTATION_PRIORITY)
28
public class ThymeleafParser implements TemplateParser {
29
	private static final Logger LOG = LoggerFactory.getLogger(ThymeleafParser.class);
30
31
	/**
32
	 * Thymeleaf engine
33
	 */
34
	private TemplateEngine engine;
35
	
36
	/**
37
	 * Converts general context into Thymeleaf specific context
38
	 */
39
	private ThymeleafContextConverter contextConverter;
40
41
	/**
42
	 * The template resolver used to find the template
43
	 */
44
	private final ResourceResolver resolver;
45
	
46
	public ThymeleafParser(TemplateEngine engine, ResourceResolver resolver, ThymeleafContextConverter contextConverter) {
47
		super();
48
		this.engine = engine;
49
		this.resolver = resolver;
50
		this.contextConverter = contextConverter;
51
	}
52
53
	public ThymeleafParser(TemplateEngine engine, ResourceResolver resolver) {
54
		this(engine, resolver, new SimpleThymeleafContextConverter());
55
	}
56
	
57
	@Override
58
	public Content parse(ResourcePath templatePath, Context ctx) throws ParseException {
59
		try {
60
			LOG.debug("Parsing Thymeleaf template {} with context {}...", templatePath, ctx);
61
			String result = engine.process(templatePath.getOriginalPath(), contextConverter.convert(ctx));
62
			LOG.debug("Template {} successfully parsed with context {}. Result:", templatePath, ctx);
63
			LOG.trace(result);
64 5 1. parse : replaced return value with null for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::parse → NO_COVERAGE
2. parse : replaced return value with null for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::parse → TIMED_OUT
3. parse : replaced return value with null for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::parse → KILLED
4. parse : replaced return value with null for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::parse → KILLED
5. parse : replaced return value with null for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::parse → KILLED
			return new ParsedContent(resolver.resolve(templatePath), ctx, result);
65
		} catch (TemplateEngineException | TemplateRuntimeException e) {
66
			throw new ParseException("Failed to parse template with thymeleaf", templatePath, ctx, e);
67
		} catch (ContextException e) {
68
			throw new ParseException("Failed to parse template with thymeleaf due to conversion error", templatePath, ctx, e);
69
		}
70
	}
71
72
	@Override
73
	public String toString() {
74 3 1. toString : replaced return value with "" for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::toString → NO_COVERAGE
2. toString : replaced return value with "" for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::toString → SURVIVED
3. toString : replaced return value with "" for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::toString → TIMED_OUT
		return "ThymeleafParser";
75
	}
76
	
77
}

Mutations

64

1.1
Location : parse
Killed by : oghamall.it.email.EmailMultiTemplateTest.withThymeleafOneVariantWithInvalidResourcePath(oghamall.it.email.EmailMultiTemplateTest)
replaced return value with null for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::parse → KILLED

2.2
Location : parse
Killed by : none
replaced return value with null for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::parse → NO_COVERAGE

3.3
Location : parse
Killed by : oghamthymeleafv3.it.resolver.StringResourceResolverTest.text(oghamthymeleafv3.it.resolver.StringResourceResolverTest)
replaced return value with null for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::parse → KILLED

4.4
Location : parse
Killed by : none
replaced return value with null for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::parse → TIMED_OUT

5.5
Location : parse
Killed by : oghamthymeleafv2.it.resolver.StringResourceResolverTest.text(oghamthymeleafv2.it.resolver.StringResourceResolverTest)
replaced return value with null for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::parse → KILLED

74

1.1
Location : toString
Killed by : none
replaced return value with "" for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::toString → TIMED_OUT

2.2
Location : toString
Killed by : none
replaced return value with "" for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::toString → NO_COVERAGE

3.3
Location : toString
Killed by : none
replaced return value with "" for fr/sii/ogham/template/thymeleaf/common/ThymeleafParser::toString → SURVIVED

Active mutators

Tests examined


Report generated by PIT OGHAM