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();
}
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);
}
}
}
}
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;
}
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);
}
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);
}
Aggregations