1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.devops.conga.tooling.maven.plugin.validation;
21
22 import java.nio.charset.StandardCharsets;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.apache.maven.plugin.MojoFailureException;
26
27 import com.github.jknack.handlebars.Handlebars;
28
29 import io.wcm.devops.conga.generator.handlebars.HandlebarsManager;
30 import io.wcm.devops.conga.generator.plugins.handlebars.escaping.NoneEscapingStrategy;
31 import io.wcm.devops.conga.resource.Resource;
32 import io.wcm.devops.conga.resource.ResourceCollection;
33 import io.wcm.devops.conga.tooling.maven.plugin.util.PathUtil;
34
35
36
37
38 public final class TemplateValidator implements DefinitionValidator<Void> {
39
40 private static final String FILE_EXTENSION = "hbs";
41
42 private final ResourceCollection templateDir;
43 private final HandlebarsManager handlebarsManager;
44
45
46
47
48
49 public TemplateValidator(ResourceCollection templateDir, HandlebarsManager handlebarsManager) {
50 this.templateDir = templateDir;
51 this.handlebarsManager = handlebarsManager;
52 }
53
54 @Override
55 @SuppressWarnings({
56 "PMD.PreserveStackTrace",
57 "java:S1075"
58 })
59 public Void validate(Resource resource, String pathForLog) throws MojoFailureException {
60 if (StringUtils.equalsIgnoreCase(resource.getFileExtension(), FILE_EXTENSION)) {
61 String templatePath = StringUtils.substringAfter(PathUtil.unifySlashes(resource.getCanonicalPath()),
62 PathUtil.unifySlashes(templateDir.getCanonicalPath()) + "/");
63 Handlebars handlebars = handlebarsManager.get(NoneEscapingStrategy.NAME, StandardCharsets.UTF_8.name());
64 try {
65 handlebars.compile(templatePath);
66 }
67 catch (Exception ex) {
68 throw new MojoFailureException("Template " + pathForLog + " is invalid:\n" + ex.getMessage());
69 }
70 }
71 return null;
72 }
73
74 }