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