ModelValidator.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 org.apache.maven.plugin.MojoFailureException;

  22. import io.wcm.devops.conga.model.reader.ModelReader;
  23. import io.wcm.devops.conga.resource.Resource;

  24. /**
  25.  * Validates YAML model file against it's reader.
  26.  * @param <T> Model class
  27.  */
  28. public final class ModelValidator<T> implements DefinitionValidator<T> {

  29.   private final String modelName;
  30.   private final ModelReader<T> modelReader;

  31.   /**
  32.    * @param modelName Model name (for log message)
  33.    * @param modelReader Model reader implementation
  34.    */
  35.   public ModelValidator(String modelName, ModelReader<T> modelReader) {
  36.     this.modelName = modelName;
  37.     this.modelReader = modelReader;
  38.   }

  39.   @Override
  40.   @SuppressWarnings("PMD.PreserveStackTrace")
  41.   public T validate(Resource resource, String pathForLog) throws MojoFailureException {
  42.     try {
  43.       return modelReader.read(resource);
  44.     }
  45.     /*CHECKSTYLE:OFF*/ catch (Exception ex) { /*CHECKSTYLE:ON*/
  46.       throw new MojoFailureException(modelName + " definition " + pathForLog + " is invalid:\n" + ex.getMessage());
  47.     }
  48.   }

  49. }