MzTabBeanValidator.java

  1. /*
  2.  * Copyright 2018 Leibniz-Institut für Analytische Wissenschaften – ISAS – e.V..
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package de.isas.mztab2.validation;

  17. import de.isas.lipidomics.mztab2.validation.Validator;
  18. import de.isas.mztab2.model.MzTab;
  19. import de.isas.mztab2.model.ValidationMessage;
  20. import java.lang.annotation.Annotation;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import java.util.Set;
  24. import javax.validation.ConstraintValidator;
  25. import javax.validation.ConstraintViolation;
  26. import javax.validation.Validation;
  27. import org.hibernate.validator.HibernateValidator;
  28. import org.hibernate.validator.HibernateValidatorConfiguration;
  29. import org.hibernate.validator.cfg.ConstraintMapping;

  30. /**
  31.  * <p>
  32.  * This validator used the HibernateValidator bean validation reference
  33.  * implementation. It understands the standard annotations that are placed on
  34.  * the domain objects generated from Swagger Codegen.</p>
  35.  *
  36.  * @author nilshoffmann
  37.  *
  38.  */
  39. public class MzTabBeanValidator implements Validator<MzTab> {

  40.     private final HibernateValidatorConfiguration configuration;
  41.     private final ConstraintMapping constraintMapping;

  42.     /**
  43.      * Default constructor. Fail fast validation is disabled.
  44.      */
  45.     public MzTabBeanValidator() {
  46.         this(false);
  47.     }

  48.     /**
  49.      * Constructor setting up the validator configuration and default constraint
  50.      * mapping.
  51.      *
  52.      * @param failFast if true, first validation error will terminate any
  53.      * further validation. If false, validation will continue and report all
  54.      * validation errors.
  55.      */
  56.     public MzTabBeanValidator(boolean failFast) {
  57.         this.configuration = Validation
  58.             .byProvider(HibernateValidator.class).
  59.             configure();

  60.         this.configuration.failFast(failFast);
  61.         this.constraintMapping = configuration.
  62.             createConstraintMapping();

  63.     }

  64.     /**
  65.      * Allows registration of custom constraint / validator pairs on the
  66.      * validation configuration. This requires that the objects to be validated
  67.      * are annotated with the corresponding constraint definition.
  68.      *
  69.      * @param <A> The annotation marking elements that should be validated.
  70.      * @param <T> The Object type on which the validation should be performed.
  71.      * @param <V> The validator to use for the validation.
  72.      * @param constraintDefinition The annotation marking elements that should
  73.      * be validated.
  74.      * @param validator The validator to use for the validation.
  75.      * @param includeExistingValidators If true, existing validators for the
  76.      * same type will be applied, too. If false, only the registered validator
  77.      * will be retained for that type.
  78.      */
  79.     public <A extends Annotation, T extends Object, V extends ConstraintValidator<A, T>> void addConstraintAndValidator(
  80.         Class<A> constraintDefinition, Class<V> validator,
  81.         boolean includeExistingValidators) {
  82.         constraintMapping.constraintDefinition(constraintDefinition).
  83.             includeExistingValidators(includeExistingValidators).
  84.             validatedBy(validator);
  85.     }

  86.     /**
  87.      * Allows registration of custom constraint / validator pairs on the
  88.      * validation configuration for a specific (unannotated) type.
  89.      *
  90.      * @param <A> The annotation marking elements that should be validated.
  91.      * @param <T> The Object type on which the validation should be performed.
  92.      * @param <V> The validator to use for the validation.
  93.      * @param typeToValidate The class/type that should be validated.
  94.      * @param constraintDefinition The annotation marking elements that should
  95.      * be validated.
  96.      * @param validator The validator to use for the validation.
  97.      * @param includeExistingValidators If true, existing validators for the
  98.      * same type will be applied, too. If false, only the registered validator
  99.      * will be retained for that type.
  100.      */
  101.     public <A extends Annotation, T extends Object, V extends ConstraintValidator<A, T>> void addConstraintAndValidator(
  102.         Class<T> typeToValidate,
  103.         Class<A> constraintDefinition, Class<V> validator,
  104.         boolean includeExistingValidators) {
  105.         constraintMapping.type(typeToValidate).
  106.             constraintDefinition(constraintDefinition).
  107.             includeExistingValidators(includeExistingValidators).
  108.             validatedBy(validator);
  109.     }

  110.     /**
  111.      * {@inheritDoc}
  112.      */
  113.     @Override
  114.     public List<ValidationMessage> validate(MzTab mzTab) {
  115.         List<ValidationMessage> list = new LinkedList<>();

  116.         javax.validation.Validator validator = configuration.addMapping(
  117.             constraintMapping).
  118.             buildValidatorFactory().
  119.             getValidator();

  120.         Set<ConstraintViolation<MzTab>> violations = validator.validate(mzTab);
  121.         for (ConstraintViolation<MzTab> violation : violations) {
  122.             list.add(new ValidationMessage().message(getPathLocatorString(
  123.                 violation) + ": " + violation.getMessage()).
  124.                 messageType(ValidationMessage.MessageTypeEnum.ERROR));
  125.         }
  126.         return list;
  127.     }

  128.     /**
  129.      * <p>
  130.      * getPathLocatorString.</p>
  131.      *
  132.      * @param cv a {@link javax.validation.ConstraintViolation} object.
  133.      * @return a {@link java.lang.String} object.
  134.      */
  135.     protected String getPathLocatorString(ConstraintViolation<?> cv) {
  136.         return cv.getPropertyPath().
  137.             toString();
  138.     }
  139. }