Search in sources :

Example 1 with TypeConstraintValidator

use of eu.esdihumboldt.hale.common.instance.extension.validation.TypeConstraintValidator in project hale by halestudio.

the class InstanceValidator method validateInstance.

/**
 * Validates the instances value against existing
 * {@link TypeConstraintValidator}s and calls
 * {@link #validateGroupChildren(Group, InstanceValidationReporter, QName, List, boolean, InstanceReference, InstanceValidationContext, ChildDefinition, EntityDefinition)}
 * .
 *
 * @param instance the instance to validate
 * @param reporter the reporter to report to
 * @param type the top level type
 * @param path the current property path
 * @param onlyCheckExistingChildren whether to only validate existing
 *            children (in case of a choice) or not
 * @param reference the instance reference
 * @param context the instance validation context
 * @param presentIn the child definition this instance is present in, if
 *            applicable
 * @param entity the instance entity definition or <code>null</code>
 */
public void validateInstance(Instance instance, InstanceValidationReporter reporter, QName type, List<QName> path, boolean onlyCheckExistingChildren, InstanceReference reference, InstanceValidationContext context, @Nullable ChildDefinition<?> presentIn, @Nullable EntityDefinition entity) {
    TypeDefinition typeDef = instance.getDefinition();
    if (entity == null) {
        // if no entity is provided, use the instance type as entity
        entity = new TypeEntityDefinition(typeDef, SchemaSpaceID.TARGET, null);
    }
    if (skipValidation(typeDef, instance)) {
        return;
    }
    // type constraint validators
    for (Entry<Class<TypeConstraint>, TypeConstraintValidator> entry : ConstraintValidatorExtension.getInstance().getTypeConstraintValidators().entrySet()) {
        try {
            entry.getValue().validateTypeConstraint(instance, typeDef.getConstraint(entry.getKey()), context);
        } catch (ValidationException vE) {
            reporter.warn(new DefaultInstanceValidationMessage(reference, type, new ArrayList<QName>(path), entry.getKey().getSimpleName(), vE.getMessage()));
        }
    }
    // generic instance validators
    for (InstanceModelValidator validator : additionalValidators) {
        try {
            validator.validateInstance(instance, entity, context);
        } catch (ValidationException vE) {
            reporter.warn(new DefaultInstanceValidationMessage(reference, type, new ArrayList<QName>(path), validator.getCategory(), vE.getMessage()));
        }
    }
    validateGroupChildren(instance, reporter, type, path, onlyCheckExistingChildren, reference, context, presentIn, entity);
}
Also used : TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) ValidationException(eu.esdihumboldt.hale.common.instance.extension.validation.ValidationException) QName(javax.xml.namespace.QName) DefaultInstanceValidationMessage(eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage) TypeConstraintValidator(eu.esdihumboldt.hale.common.instance.extension.validation.TypeConstraintValidator) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)1 TypeConstraintValidator (eu.esdihumboldt.hale.common.instance.extension.validation.TypeConstraintValidator)1 ValidationException (eu.esdihumboldt.hale.common.instance.extension.validation.ValidationException)1 DefaultInstanceValidationMessage (eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 QName (javax.xml.namespace.QName)1