View Javadoc
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  
22  import org.apache.maven.plugin.MojoFailureException;
23  
24  import io.wcm.devops.conga.model.reader.ModelReader;
25  import io.wcm.devops.conga.resource.Resource;
26  
27  /**
28   * Validates YAML model file against it's reader.
29   * @param <T> Model class
30   */
31  public final class ModelValidator<T> implements DefinitionValidator<T> {
32  
33    private final String modelName;
34    private final ModelReader<T> modelReader;
35  
36    /**
37     * @param modelName Model name (for log message)
38     * @param modelReader Model reader implementation
39     */
40    public ModelValidator(String modelName, ModelReader<T> modelReader) {
41      this.modelName = modelName;
42      this.modelReader = modelReader;
43    }
44  
45    @Override
46    @SuppressWarnings("PMD.PreserveStackTrace")
47    public T validate(Resource resource, String pathForLog) throws MojoFailureException {
48      try {
49        return modelReader.read(resource);
50      }
51      /*CHECKSTYLE:OFF*/ catch (Exception ex) { /*CHECKSTYLE:ON*/
52        throw new MojoFailureException(modelName + " definition " + pathForLog + " is invalid:\n" + ex.getMessage());
53      }
54    }
55  
56  }