Search in sources :

Example 1 with DefaultInstanceValidationMessage

use of eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage in project hale by halestudio.

the class InstanceValidationReportDetailsPage method setInput.

/**
 * @see CustomReportDetailsPage#setInput(Collection, MessageType)
 */
@Override
public void setInput(Collection<? extends Message> messages, MessageType type) {
    if (more > 0) {
        Collection<Message> messageList = new ArrayList<>(messages);
        String title = MessageFormat.format("{0} more warnings", more);
        String message = MessageFormat.format("{0} more validation warnings are not listed explicitly", more);
        messageList.add(new DefaultInstanceValidationMessage(null, null, Collections.<QName>emptyList(), title, message));
        treeViewer.setInput(messageList);
    } else {
        treeViewer.setInput(messages);
    }
    // initially expand all levels
    treeViewer.expandAll();
}
Also used : InstanceValidationMessage(eu.esdihumboldt.hale.common.instance.extension.validation.report.InstanceValidationMessage) DefaultInstanceValidationMessage(eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage) Message(eu.esdihumboldt.hale.common.core.report.Message) QName(javax.xml.namespace.QName) DefaultInstanceValidationMessage(eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage) ArrayList(java.util.ArrayList)

Example 2 with DefaultInstanceValidationMessage

use of eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage in project hale by halestudio.

the class InstanceValidator method validateChildren.

/**
 * Validates the given property values (their values - as instances - and/or
 * group children).
 *
 * @param properties the array of existing properties, may be null
 * @param childDef their definition
 * @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 entity the entity definition related to the property values or
 *            <code>null</code>
 */
private void validateChildren(Object[] properties, ChildDefinition<?> childDef, InstanceValidationReporter reporter, QName type, List<QName> path, boolean onlyCheckExistingChildren, InstanceReference reference, InstanceValidationContext context, @Nullable EntityDefinition entity) {
    if (properties != null && properties.length > 0) {
        for (Object property : properties) {
            if (property instanceof Instance) {
                validateInstance((Instance) property, reporter, type, path, onlyCheckExistingChildren, reference, context, childDef, entity);
            } else if (property instanceof Group) {
                validateGroupChildren((Group) property, reporter, type, path, onlyCheckExistingChildren, reference, context, childDef, entity);
            } else {
                if (childDef.asGroup() != null)
                    reporter.warn(new DefaultInstanceValidationMessage(reference, type, new ArrayList<QName>(path), "Wrong group", "A property is no group"));
                else if (childDef.asProperty() != null) {
                    if (!skipValidation(childDef.asProperty().getPropertyType(), property)) {
                        // don't skip property
                        // wrap value in dummy instance for type validation
                        MutableInstance instance = new DefaultInstance(childDef.asProperty().getPropertyType(), null);
                        instance.setValue(property);
                        validateInstance(instance, reporter, type, path, onlyCheckExistingChildren, reference, context, childDef, entity);
                    }
                }
            }
        }
    } else {
        /*
			 * Special case: No property value, but a combination of minimum
			 * cardinality greater than zero and NillableFlag is set. Then there
			 * can be sub-properties that are required.
			 * 
			 * Applicable for XML (simple) types with mandatory attributes.
			 */
        if (childDef.asProperty() != null && childDef.asProperty().getConstraint(Cardinality.class).getMinOccurs() > 0 && childDef.asProperty().getConstraint(NillableFlag.class).isEnabled() && childDef.asProperty().getPropertyType().getConstraint(HasValueFlag.class).isEnabled() && !childDef.asProperty().getPropertyType().getChildren().isEmpty()) {
            // collect XML attribute children
            List<ChildDefinition<?>> attributes = new ArrayList<ChildDefinition<?>>();
            for (ChildDefinition<?> child : childDef.asProperty().getPropertyType().getChildren()) {
                if (child.asProperty() != null && child.asProperty().getConstraint(XmlAttributeFlag.class).isEnabled()) {
                    attributes.add(child);
                }
            }
            if (!attributes.isEmpty()) {
                // create an empty dummy instance
                Instance instance = new DefaultInstance(childDef.asProperty().getPropertyType(), null);
                validateGroupChildren(instance, attributes, reporter, type, path, onlyCheckExistingChildren, reference, context, entity);
            }
        }
    }
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) 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) DefaultInstanceValidationMessage(eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) ArrayList(java.util.ArrayList) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition)

Example 3 with DefaultInstanceValidationMessage

use of eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage 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 4 with DefaultInstanceValidationMessage

use of eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage 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 5 with DefaultInstanceValidationMessage

use of eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage 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

DefaultInstanceValidationMessage (eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage)5 QName (javax.xml.namespace.QName)4 ValidationException (eu.esdihumboldt.hale.common.instance.extension.validation.ValidationException)3 GroupPropertyConstraintValidator (eu.esdihumboldt.hale.common.instance.extension.validation.GroupPropertyConstraintValidator)2 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)2 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)2 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)2 ArrayList (java.util.ArrayList)2 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)1 Message (eu.esdihumboldt.hale.common.core.report.Message)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 InstanceValidationMessage (eu.esdihumboldt.hale.common.instance.extension.validation.report.InstanceValidationMessage)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 ChildDefinition (eu.esdihumboldt.hale.common.schema.model.ChildDefinition)1 GroupPropertyConstraint (eu.esdihumboldt.hale.common.schema.model.GroupPropertyConstraint)1