TemplateValidator.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2015 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.devops.conga.tooling.maven.plugin.validation;

  21. import java.nio.charset.StandardCharsets;

  22. import org.apache.commons.lang3.StringUtils;
  23. import org.apache.maven.plugin.MojoFailureException;

  24. import com.github.jknack.handlebars.Handlebars;

  25. import io.wcm.devops.conga.generator.handlebars.HandlebarsManager;
  26. import io.wcm.devops.conga.generator.plugins.handlebars.escaping.NoneEscapingStrategy;
  27. import io.wcm.devops.conga.resource.Resource;
  28. import io.wcm.devops.conga.resource.ResourceCollection;
  29. import io.wcm.devops.conga.tooling.maven.plugin.util.PathUtil;

  30. /**
  31.  * Validates Handlebars templates by compiling it.
  32.  */
  33. public final class TemplateValidator implements DefinitionValidator<Void> {

  34.   private static final String FILE_EXTENSION = "hbs";

  35.   private final ResourceCollection templateDir;
  36.   private final HandlebarsManager handlebarsManager;

  37.   /**
  38.    * @param templateDir Template directory
  39.    * @param handlebarsManager Handlebars Manager
  40.    */
  41.   public TemplateValidator(ResourceCollection templateDir, HandlebarsManager handlebarsManager) {
  42.     this.templateDir = templateDir;
  43.     this.handlebarsManager = handlebarsManager;
  44.   }

  45.   @Override
  46.   @SuppressWarnings({
  47.       "PMD.PreserveStackTrace",
  48.       "java:S1075" // uses / by intention
  49.   })
  50.   public Void validate(Resource resource, String pathForLog) throws MojoFailureException {
  51.     if (StringUtils.equalsIgnoreCase(resource.getFileExtension(), FILE_EXTENSION)) {
  52.       String templatePath = StringUtils.substringAfter(PathUtil.unifySlashes(resource.getCanonicalPath()),
  53.           PathUtil.unifySlashes(templateDir.getCanonicalPath()) + "/");
  54.       Handlebars handlebars = handlebarsManager.get(NoneEscapingStrategy.NAME, StandardCharsets.UTF_8.name());
  55.       try {
  56.         handlebars.compile(templatePath);
  57.       }
  58.       /*CHECKSTYLE:OFF*/ catch (Exception ex) { /*CHECKSTYLE:ON*/
  59.         throw new MojoFailureException("Template " + pathForLog + " is invalid:\n" + ex.getMessage());
  60.       }
  61.     }
  62.     return null;
  63.   }

  64. }