AutoDetectTemplateParser.java

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 1 1. parse : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::parse → RUN_ERROR
			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 1 1. findParser : negated conditional → RUN_ERROR
			if (impl.getDetector().canParse(templatePath, ctx)) {
56
				TemplateParser parser = impl.getParser();
57
				LOG.debug("Template engine {} is used for {}", parser, templatePath);
58 1 1. findParser : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::findParser → RUN_ERROR
				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 1 1. toString : replaced return value with "" for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::toString → NO_COVERAGE
		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 1 1. getDetector : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::getDetector → RUN_ERROR
			return detector;
85
		}
86
		public TemplateParser getParser() {
87 1 1. getParser : replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::getParser → RUN_ERROR
			return parser;
88
		}
89
		@Override
90
		public String toString() {
91 1 1. toString : replaced return value with "" for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::toString → RUN_ERROR
			return "TemplateImplementation {detector:" + detector + ", parser:" + parser + "}";
92
		}
93
	}
94
}

Mutations

47

1.1
Location : parse
Killed by :
replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::parse → RUN_ERROR

55

1.1
Location : findParser
Killed by :
negated conditional → RUN_ERROR

58

1.1
Location : findParser
Killed by :
replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::findParser → RUN_ERROR

70

1.1
Location : toString
Killed by :
replaced return value with "" for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser::toString → NO_COVERAGE

84

1.1
Location : getDetector
Killed by :
replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::getDetector → RUN_ERROR

87

1.1
Location : getParser
Killed by :
replaced return value with null for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::getParser → RUN_ERROR

91

1.1
Location : toString
Killed by :
replaced return value with "" for fr/sii/ogham/core/template/parser/AutoDetectTemplateParser$TemplateImplementation::toString → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1