Search in sources :

Example 1 with Validator

use of eu.esdihumboldt.util.validator.Validator in project hale by halestudio.

the class AbstractCombinedValidatorFactory method restore.

@Override
public Validator restore(Value value) throws Exception {
    ValueList list = value.as(ValueList.class);
    List<Validator> children = new ArrayList<>();
    for (Value childVal : list) {
        children.add(childVal.as(ValidatorValue.class).toValidator());
    }
    return createValidator(children);
}
Also used : ValueList(eu.esdihumboldt.hale.common.core.io.ValueList) ArrayList(java.util.ArrayList) ValidatorValue(eu.esdihumboldt.hale.common.schema.model.validate.factory.ValidatorValue) Value(eu.esdihumboldt.hale.common.core.io.Value) CombinedValidator(eu.esdihumboldt.util.validator.CombinedValidator) Validator(eu.esdihumboldt.util.validator.Validator)

Example 2 with Validator

use of eu.esdihumboldt.util.validator.Validator in project hale by halestudio.

the class ValidationConstraint method getValidator.

/**
 * Returns the validator for this constraint.
 *
 * @return the validator for this constraint
 */
public Validator getValidator() {
    if (type != null && type.getSuperType() != null && !init) {
        init = true;
        Validator parentValidator = type.getSuperType().getConstraint(ValidationConstraint.class).getValidator();
        if (!parentValidator.isAlwaysTrue()) {
            ArrayList<Validator> validators = new ArrayList<Validator>(2);
            validators.add(parentValidator);
            validators.add(validator);
            validator = new AndValidator(validators);
        }
    }
    return validator;
}
Also used : AndValidator(eu.esdihumboldt.util.validator.AndValidator) ArrayList(java.util.ArrayList) AndValidator(eu.esdihumboldt.util.validator.AndValidator) Validator(eu.esdihumboldt.util.validator.Validator)

Example 3 with Validator

use of eu.esdihumboldt.util.validator.Validator in project hale by halestudio.

the class XmlTypeUtil method configureSimpleTypeRestriction.

/**
 * Configure a type definition for a simple type based on a simple type
 * restriction.
 *
 * @param type the type definition
 * @param restriction the simple type restriction
 * @param index the XML index for resolving type definitions
 * @param reporter the report
 */
private static void configureSimpleTypeRestriction(XmlTypeDefinition type, XmlSchemaSimpleTypeRestriction restriction, XmlIndex index, IOReporter reporter) {
    QName baseTypeName = restriction.getBaseTypeName();
    XmlTypeDefinition baseTypeDef;
    if (baseTypeName != null) {
        // resolve super type
        baseTypeDef = index.getOrCreateType(baseTypeName);
    } else if (restriction.getBaseType() != null) {
        // simple schema type
        XmlSchemaSimpleType simpleType = restriction.getBaseType();
        // create an anonymous type
        QName anonymousName = new QName(type.getName().getNamespaceURI() + "/" + type.getName().getLocalPart(), // $NON-NLS-1$
        "AnonymousSuperType");
        baseTypeDef = new AnonymousXmlType(anonymousName);
        XmlTypeUtil.configureSimpleType(type, simpleType, index, reporter);
        // no schema location available at this point
        final String schemaLoc = null;
        // set metadata
        XmlSchemaReader.setMetadata(type, simpleType, schemaLoc, index);
    } else {
        reporter.error(new IOMessageImpl("Simple type restriction without base type, skipping type configuration.", null, restriction.getLineNumber(), restriction.getLinePosition()));
        return;
    }
    // set super type
    type.setSuperType(baseTypeDef);
    // mark as restriction
    type.setConstraint(RestrictionFlag.ENABLED);
    // assign no binding, inherit from super type
    // The following code expects schema to be valid.
    List<Validator> validators = new LinkedList<Validator>();
    // patterns and enumerations in one step are ORed together!
    List<String> values = new LinkedList<String>();
    List<Validator> patternValidators = new LinkedList<Validator>();
    // TODO different handling for date/time/g.../duration in
    // (min|max)(In|Ex)clusive
    // XXX only for date, time, duration, dateTime, gMonthDay, gYearMonth?
    // no also for some cases of gYear, gMonth, gDay (they can have a
    // timezone!)
    // but only need to handle cases where isDecimal() is false...
    XmlSchemaObjectCollection facets = restriction.getFacets();
    for (int i = 0; i < facets.getCount(); i++) {
        XmlSchemaFacet facet = (XmlSchemaFacet) facets.getItem(i);
        if (facet instanceof XmlSchemaEnumerationFacet) {
            values.add(facet.getValue().toString());
        } else if (facet instanceof XmlSchemaFractionDigitsFacet) {
            validators.add(new DigitCountValidator(DigitCountValidator.Type.FRACTIONDIGITS, Integer.parseInt(facet.getValue().toString())));
        } else if (facet instanceof XmlSchemaLengthFacet) {
            validators.add(new LengthValidator(LengthValidator.Type.EXACT, Integer.parseInt(facet.getValue().toString())));
        } else if (facet instanceof XmlSchemaMaxExclusiveFacet) {
            if (// number or date?
            isDecimal(facet.getValue().toString()))
                validators.add(new NumberValidator(NumberValidator.Type.MAXEXCLUSIVE, new BigDecimal(facet.getValue().toString())));
            else
                reporter.warn(new IOMessageImpl("(min|max)(In|Ex)clusive not supported for non-number types", null, facet.getLineNumber(), facet.getLinePosition()));
        } else if (facet instanceof XmlSchemaMaxInclusiveFacet) {
            if (// number or date?
            isDecimal(facet.getValue().toString()))
                validators.add(new NumberValidator(NumberValidator.Type.MAXINCLUSIVE, new BigDecimal(facet.getValue().toString())));
            else
                reporter.warn(new IOMessageImpl("(min|max)(In|Ex)clusive not supported for non-number types", null, facet.getLineNumber(), facet.getLinePosition()));
        } else if (facet instanceof XmlSchemaMaxLengthFacet) {
            validators.add(new LengthValidator(LengthValidator.Type.MAXIMUM, Integer.parseInt(facet.getValue().toString())));
        } else if (facet instanceof XmlSchemaMinLengthFacet) {
            validators.add(new LengthValidator(LengthValidator.Type.MINIMUM, Integer.parseInt(facet.getValue().toString())));
        } else if (facet instanceof XmlSchemaMinExclusiveFacet) {
            if (// number or date?
            isDecimal(facet.getValue().toString()))
                validators.add(new NumberValidator(NumberValidator.Type.MINEXCLUSIVE, new BigDecimal(facet.getValue().toString())));
            else
                reporter.warn(new IOMessageImpl("(min|max)(In|Ex)clusive not supported for non-number types", null, facet.getLineNumber(), facet.getLinePosition()));
        } else if (facet instanceof XmlSchemaMinInclusiveFacet) {
            if (// number or date?
            isDecimal(facet.getValue().toString()))
                validators.add(new NumberValidator(NumberValidator.Type.MININCLUSIVE, new BigDecimal(facet.getValue().toString())));
            else
                reporter.warn(new IOMessageImpl("(min|max)(In|Ex)clusive not supported for non-number types", null, facet.getLineNumber(), facet.getLinePosition()));
        } else if (facet instanceof XmlSchemaPatternFacet) {
            patternValidators.add(new PatternValidator(facet.getValue().toString()));
        } else if (facet instanceof XmlSchemaTotalDigitsFacet) {
            validators.add(new DigitCountValidator(DigitCountValidator.Type.TOTALDIGITS, Integer.parseInt(facet.getValue().toString())));
        } else if (facet instanceof XmlSchemaWhiteSpaceFacet) {
            reporter.info(new IOMessageImpl("White space facet not supported", null, facet.getLineNumber(), facet.getLinePosition()));
        // Nothing to validate according to w3.
        // Values should be processed according to rule?
        } else {
            reporter.error(new IOMessageImpl("Unrecognized facet: " + facet.getClass().getSimpleName(), null, facet.getLineNumber(), facet.getLinePosition()));
        }
    }
    if (!patternValidators.isEmpty())
        validators.add(new OrValidator(patternValidators));
    if (!values.isEmpty()) {
        // set enumeration constraint
        // no check of which values are okay, they must be validated
        // somewhere else.
        // XXX conversion to be done?
        type.setConstraint(new Enumeration<String>(values, false));
        validators.add(new EnumerationValidator(values));
    }
    if (!validators.isEmpty())
        type.setConstraint(new ValidationConstraint(new AndValidator(validators), type));
}
Also used : LengthValidator(eu.esdihumboldt.util.validator.LengthValidator) AndValidator(eu.esdihumboldt.util.validator.AndValidator) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) XmlSchemaPatternFacet(org.apache.ws.commons.schema.XmlSchemaPatternFacet) XmlSchemaFractionDigitsFacet(org.apache.ws.commons.schema.XmlSchemaFractionDigitsFacet) NumberValidator(eu.esdihumboldt.util.validator.NumberValidator) UnionValidationConstraint(eu.esdihumboldt.hale.io.xsd.reader.internal.constraint.UnionValidationConstraint) ValidationConstraint(eu.esdihumboldt.hale.common.schema.model.constraint.type.ValidationConstraint) XmlSchemaMinExclusiveFacet(org.apache.ws.commons.schema.XmlSchemaMinExclusiveFacet) OrValidator(eu.esdihumboldt.util.validator.OrValidator) XmlSchemaEnumerationFacet(org.apache.ws.commons.schema.XmlSchemaEnumerationFacet) PatternValidator(eu.esdihumboldt.util.validator.PatternValidator) XmlSchemaObjectCollection(org.apache.ws.commons.schema.XmlSchemaObjectCollection) XmlSchemaFacet(org.apache.ws.commons.schema.XmlSchemaFacet) DigitCountValidator(eu.esdihumboldt.util.validator.DigitCountValidator) XmlSchemaMinInclusiveFacet(org.apache.ws.commons.schema.XmlSchemaMinInclusiveFacet) QName(javax.xml.namespace.QName) LinkedList(java.util.LinkedList) UnionValidationConstraint(eu.esdihumboldt.hale.io.xsd.reader.internal.constraint.UnionValidationConstraint) TypeConstraint(eu.esdihumboldt.hale.common.schema.model.TypeConstraint) ValidationConstraint(eu.esdihumboldt.hale.common.schema.model.constraint.type.ValidationConstraint) XmlSchemaMaxExclusiveFacet(org.apache.ws.commons.schema.XmlSchemaMaxExclusiveFacet) BigDecimal(java.math.BigDecimal) XmlSchemaTotalDigitsFacet(org.apache.ws.commons.schema.XmlSchemaTotalDigitsFacet) XmlSchemaMaxInclusiveFacet(org.apache.ws.commons.schema.XmlSchemaMaxInclusiveFacet) EnumerationValidator(eu.esdihumboldt.util.validator.EnumerationValidator) XmlSchemaMinLengthFacet(org.apache.ws.commons.schema.XmlSchemaMinLengthFacet) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlSchemaLengthFacet(org.apache.ws.commons.schema.XmlSchemaLengthFacet) EnumerationValidator(eu.esdihumboldt.util.validator.EnumerationValidator) NumberValidator(eu.esdihumboldt.util.validator.NumberValidator) PatternValidator(eu.esdihumboldt.util.validator.PatternValidator) LengthValidator(eu.esdihumboldt.util.validator.LengthValidator) OrValidator(eu.esdihumboldt.util.validator.OrValidator) AndValidator(eu.esdihumboldt.util.validator.AndValidator) Validator(eu.esdihumboldt.util.validator.Validator) DigitCountValidator(eu.esdihumboldt.util.validator.DigitCountValidator) XmlSchemaMaxLengthFacet(org.apache.ws.commons.schema.XmlSchemaMaxLengthFacet) XmlSchemaWhiteSpaceFacet(org.apache.ws.commons.schema.XmlSchemaWhiteSpaceFacet)

Example 4 with Validator

use of eu.esdihumboldt.util.validator.Validator in project hale by halestudio.

the class UnionValidationConstraint method getValidator.

/**
 * @see eu.esdihumboldt.hale.common.schema.model.constraint.type.ValidationConstraint#getValidator()
 */
@Override
public Validator getValidator() {
    if (!initialized) {
        ArrayList<Validator> validators = new ArrayList<Validator>(unionTypes.size());
        for (TypeDefinition type : unionTypes) validators.add(type.getConstraint(ValidationConstraint.class).getValidator());
        validator = new OrValidator(validators);
    }
    return validator;
}
Also used : ValidationConstraint(eu.esdihumboldt.hale.common.schema.model.constraint.type.ValidationConstraint) ArrayList(java.util.ArrayList) OrValidator(eu.esdihumboldt.util.validator.OrValidator) OrValidator(eu.esdihumboldt.util.validator.OrValidator) Validator(eu.esdihumboldt.util.validator.Validator) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 5 with Validator

use of eu.esdihumboldt.util.validator.Validator in project hale by halestudio.

the class SequentialIDParameterPage method validateValue.

/**
 * Validates if the given value is valid for the target property.
 *
 * @param value the value to validate
 * @return if the value is valid
 */
protected boolean validateValue(String value) {
    Cell cell = getWizard().getUnfinishedCell();
    List<? extends Entity> targets = cell.getTarget().get(null);
    if (!targets.isEmpty()) {
        Entity entity = targets.get(0);
        Definition<?> def = entity.getDefinition().getDefinition();
        if (def instanceof PropertyDefinition) {
            TypeDefinition propertyType = ((PropertyDefinition) def).getPropertyType();
            Validator validator = propertyType.getConstraint(ValidationConstraint.class).getValidator();
            // TODO conversion to binding needed?
            String error = validator.validate(value);
            // update the example decoration
            if (error == null) {
                exampleDecoration.hide();
            } else {
                exampleDecoration.setDescriptionText(error);
                exampleDecoration.show();
            }
            return error == null;
        }
    }
    // no validation possible
    return true;
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) ValidationConstraint(eu.esdihumboldt.hale.common.schema.model.constraint.type.ValidationConstraint) Cell(eu.esdihumboldt.hale.common.align.model.Cell) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Validator(eu.esdihumboldt.util.validator.Validator) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

Validator (eu.esdihumboldt.util.validator.Validator)6 ValidationConstraint (eu.esdihumboldt.hale.common.schema.model.constraint.type.ValidationConstraint)4 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)3 ArrayList (java.util.ArrayList)3 ValidatorValue (eu.esdihumboldt.hale.common.schema.model.validate.factory.ValidatorValue)2 AndValidator (eu.esdihumboldt.util.validator.AndValidator)2 OrValidator (eu.esdihumboldt.util.validator.OrValidator)2 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1 Entity (eu.esdihumboldt.hale.common.align.model.Entity)1 Value (eu.esdihumboldt.hale.common.core.io.Value)1 ValueList (eu.esdihumboldt.hale.common.core.io.ValueList)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)1 TypeConstraint (eu.esdihumboldt.hale.common.schema.model.TypeConstraint)1 UnionValidationConstraint (eu.esdihumboldt.hale.io.xsd.reader.internal.constraint.UnionValidationConstraint)1 CombinedValidator (eu.esdihumboldt.util.validator.CombinedValidator)1 DigitCountValidator (eu.esdihumboldt.util.validator.DigitCountValidator)1 EnumerationValidator (eu.esdihumboldt.util.validator.EnumerationValidator)1 LengthValidator (eu.esdihumboldt.util.validator.LengthValidator)1 NumberValidator (eu.esdihumboldt.util.validator.NumberValidator)1