Search in sources :

Example 1 with ValidationException

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

the class InstanceValidator method validate.

/**
 * Validates the given object. The created reports messages do not have an
 * {@link InstanceReference} set.
 *
 * @param object the object to validate (i. e. an instance, group or basic
 *            value)
 * @param childDef the child definition of the given object
 * @return a report of the validation
 */
public InstanceValidationReporter validate(Object object, ChildDefinition<?> childDef) {
    InstanceValidationReporter reporter = new DefaultInstanceValidationReporter(false);
    reporter.setSuccess(false);
    InstanceValidationContext context = new InstanceValidationContext();
    // first a special case for Choice-Flag
    // XXX a better way to do this than coding this special case?
    boolean onlyCheckExistingChildren = false;
    if (childDef.asGroup() != null) {
        GroupPropertyConstraintValidator validator = ConstraintValidatorExtension.getInstance().getGroupPropertyConstraintValidators().get(ChoiceFlag.class);
        if (validator != null)
            try {
                validator.validateGroupPropertyConstraint(new Object[] { object }, childDef.asGroup().getConstraint(ChoiceFlag.class), childDef.asGroup(), context);
            } catch (ValidationException vE) {
                reporter.warn(new DefaultInstanceValidationMessage(null, null, Collections.<QName>emptyList(), ChoiceFlag.class.getSimpleName(), vE.getMessage()));
            }
        onlyCheckExistingChildren = childDef.asGroup().getConstraint(ChoiceFlag.class).isEnabled();
    }
    // then validate the object as if it were a lone property value
    validateChildren(new Object[] { object }, childDef, reporter, null, new ArrayList<QName>(), onlyCheckExistingChildren, null, context, null);
    reporter.setSuccess(true);
    return reporter;
}
Also used : ValidationException(eu.esdihumboldt.hale.common.instance.extension.validation.ValidationException) InstanceValidationReporter(eu.esdihumboldt.hale.common.instance.extension.validation.report.InstanceValidationReporter) DefaultInstanceValidationReporter(eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationReporter) QName(javax.xml.namespace.QName) DefaultInstanceValidationMessage(eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage) DefaultInstanceValidationReporter(eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationReporter) InstanceValidationContext(eu.esdihumboldt.hale.common.instance.extension.validation.InstanceValidationContext) GroupPropertyConstraintValidator(eu.esdihumboldt.hale.common.instance.extension.validation.GroupPropertyConstraintValidator)

Example 2 with ValidationException

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

the class InstanceValidator method validateProperty.

/**
 * Validates the given property values against their
 * {@link PropertyDefinition}.<br>
 * Then calls
 * {@link #validateChildren(Object[], ChildDefinition, InstanceValidationReporter, QName, List, boolean, InstanceReference, InstanceValidationContext, EntityDefinition)}
 * .
 *
 * @param properties the array of existing properties, may be null
 * @param propertyDef their definition
 * @param reporter the reporter to report to
 * @param type the top level type
 * @param path the current property path
 * @param reference the instance reference
 * @param context the instance validation context
 * @param entity the property's entity definition or <code>null</code>
 */
@SuppressWarnings("unchecked")
private void validateProperty(Object[] properties, PropertyDefinition propertyDef, InstanceValidationReporter reporter, QName type, List<QName> path, InstanceReference reference, InstanceValidationContext context, @Nullable EntityDefinition entity) {
    ValidationLocation loc = new ValidationLocation(reference, type, new ArrayList<QName>(path));
    // property constraint validators
    for (Entry<Class<PropertyConstraint>, PropertyConstraintValidator> entry : ConstraintValidatorExtension.getInstance().getPropertyConstraintValidators().entrySet()) {
        try {
            entry.getValue().validatePropertyConstraint(properties, propertyDef.getConstraint((Class<? extends PropertyConstraint>) ConstraintUtil.getConstraintType(entry.getKey())), propertyDef, context, loc);
        } catch (ValidationException vE) {
            reporter.warn(new DefaultInstanceValidationMessage(loc, entry.getKey().getSimpleName(), vE.getMessage()));
        }
    }
    if (properties != null) {
        // generic validators
        for (InstanceModelValidator validator : additionalValidators) {
            for (Object value : properties) {
                // visit each value
                if (value instanceof Instance) {
                    try {
                        validator.validateInstance((Instance) value, entity, context);
                    } catch (ValidationException vE) {
                        reporter.warn(new DefaultInstanceValidationMessage(reference, type, new ArrayList<QName>(path), validator.getCategory(), vE.getMessage()));
                    }
                } else {
                    try {
                        validator.validateProperty(value, propertyDef, entity, context);
                    } catch (ValidationException vE) {
                        reporter.warn(new DefaultInstanceValidationMessage(reference, type, new ArrayList<QName>(path), validator.getCategory(), vE.getMessage()));
                    }
                }
            }
        }
    }
    validateChildren(properties, propertyDef, reporter, type, path, false, reference, context, entity);
}
Also used : GroupPropertyConstraint(eu.esdihumboldt.hale.common.schema.model.GroupPropertyConstraint) PropertyConstraint(eu.esdihumboldt.hale.common.schema.model.PropertyConstraint) ValidationException(eu.esdihumboldt.hale.common.instance.extension.validation.ValidationException) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) DefaultInstanceValidationMessage(eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage) ValidationLocation(eu.esdihumboldt.hale.common.instance.extension.validation.ValidationLocation) GroupPropertyConstraintValidator(eu.esdihumboldt.hale.common.instance.extension.validation.GroupPropertyConstraintValidator) PropertyConstraintValidator(eu.esdihumboldt.hale.common.instance.extension.validation.PropertyConstraintValidator)

Example 3 with ValidationException

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

the class CodeListValidator method validateCodeListValue.

private void validateCodeListValue(@Nullable Object value, @Nullable EntityDefinition entity) throws ValidationException {
    CodeListAssociations associations = null;
    if (projectService != null) {
        associations = projectService.getProperty(CodeListAssociations.KEY_ASSOCIATIONS).as(CodeListAssociations.class);
    }
    if (value != null && entity != null && associations != null && codeLists != null) {
        CodeListReference clRef = associations.getCodeList(entity);
        if (clRef != null) {
            CodeList cl = codeLists.findCodeList(clRef);
            if (cl != null) {
                String strValue = value.toString();
                CodeEntry entry = cl.getEntryByIdentifier(strValue);
                if (entry == null) {
                    throw new ValidationException(MessageFormat.format("Value ''{0}'' not found in associated code list", strValue));
                }
            } else {
                log.warn("Code list " + clRef + " not found for validation");
            }
        }
    }
}
Also used : CodeList(eu.esdihumboldt.hale.common.codelist.CodeList) CodeEntry(eu.esdihumboldt.hale.common.codelist.CodeList.CodeEntry) ValidationException(eu.esdihumboldt.hale.common.instance.extension.validation.ValidationException) CodeListAssociations(eu.esdihumboldt.hale.common.codelist.config.CodeListAssociations) CodeListReference(eu.esdihumboldt.hale.common.codelist.config.CodeListReference)

Example 4 with ValidationException

use of eu.esdihumboldt.hale.common.instance.extension.validation.ValidationException 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)

Example 5 with ValidationException

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

the class ChoiceFlagValidator method validateGroupPropertyConstraint.

@Override
public void validateGroupPropertyConstraint(Object[] values, GroupPropertyConstraint constraint, GroupPropertyDefinition property, InstanceValidationContext context) throws ValidationException {
    if (((ChoiceFlag) constraint).isEnabled() && values != null) {
        for (Object value : values) {
            if (value instanceof Group) {
                Iterator<QName> properties = ((Group) value).getPropertyNames().iterator();
                if (!properties.hasNext())
                    throw new ValidationException("A choice with no child.");
                properties.next();
                if (properties.hasNext())
                    throw new ValidationException("A choice with different children.");
            }
        // XXX what if value is null or not a Group?
        // XXX what if it is an Instance? May it have a value?
        }
    }
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) ValidationException(eu.esdihumboldt.hale.common.instance.extension.validation.ValidationException) QName(javax.xml.namespace.QName)

Aggregations

ValidationException (eu.esdihumboldt.hale.common.instance.extension.validation.ValidationException)6 QName (javax.xml.namespace.QName)4 DefaultInstanceValidationMessage (eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage)3 GroupPropertyConstraintValidator (eu.esdihumboldt.hale.common.instance.extension.validation.GroupPropertyConstraintValidator)2 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)1 CodeList (eu.esdihumboldt.hale.common.codelist.CodeList)1 CodeEntry (eu.esdihumboldt.hale.common.codelist.CodeList.CodeEntry)1 CodeListAssociations (eu.esdihumboldt.hale.common.codelist.config.CodeListAssociations)1 CodeListReference (eu.esdihumboldt.hale.common.codelist.config.CodeListReference)1 InstanceValidationContext (eu.esdihumboldt.hale.common.instance.extension.validation.InstanceValidationContext)1 PropertyConstraintValidator (eu.esdihumboldt.hale.common.instance.extension.validation.PropertyConstraintValidator)1 TypeConstraintValidator (eu.esdihumboldt.hale.common.instance.extension.validation.TypeConstraintValidator)1 ValidationLocation (eu.esdihumboldt.hale.common.instance.extension.validation.ValidationLocation)1 InstanceValidationReporter (eu.esdihumboldt.hale.common.instance.extension.validation.report.InstanceValidationReporter)1 DefaultInstanceValidationReporter (eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationReporter)1 Group (eu.esdihumboldt.hale.common.instance.model.Group)1 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)1 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)1 GroupPropertyConstraint (eu.esdihumboldt.hale.common.schema.model.GroupPropertyConstraint)1