Search in sources :

Example 1 with XmlSchemaFractionDigitsFacet

use of org.apache.ws.commons.schema.XmlSchemaFractionDigitsFacet 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 2 with XmlSchemaFractionDigitsFacet

use of org.apache.ws.commons.schema.XmlSchemaFractionDigitsFacet in project cxf by apache.

the class WSDLTypes method processDecimalType.

public static CorbaType processDecimalType(XmlSchemaSimpleTypeRestriction restrictionType, QName name, CorbaType corbaTypeImpl, boolean anonymous) throws Exception {
    String tdigits = null;
    String fdigits = null;
    boolean boundedDecimal = false;
    boolean boundedScale = false;
    for (XmlSchemaFacet val : restrictionType.getFacets()) {
        if (val instanceof XmlSchemaTotalDigitsFacet) {
            tdigits = val.getValue().toString();
            boundedDecimal = true;
        }
        if (val instanceof XmlSchemaFractionDigitsFacet) {
            fdigits = val.getValue().toString();
            boundedScale = true;
        }
    }
    int digits = 0;
    int scale = 0;
    if (boundedDecimal) {
        try {
            digits = Integer.parseInt(tdigits);
            if ((digits > 31) || (digits < 1)) {
                String msg = "totalDigits facet for the type " + name + " cannot be more than 31 for corba fixed types";
                LOG.log(Level.WARNING, msg);
                boundedDecimal = false;
            } else if (digits == 31) {
                boundedDecimal = false;
            }
        } catch (NumberFormatException ex) {
            String msg = "totalDigits facet on the simple type restriction for type" + name.getLocalPart() + "is incorrect.";
            throw new Exception(msg);
        }
    }
    if (boundedScale) {
        try {
            scale = Integer.parseInt(fdigits);
            if ((scale > 6) || (scale < 0)) {
                String msg = "fixedDigits facet for the type " + name + " cannot be more than 6 for corba fixed types";
                LOG.log(Level.WARNING, msg);
                boundedScale = false;
            } else if (scale == 6) {
                boundedScale = false;
            }
        } catch (NumberFormatException ex) {
            String msg = "fractionDigits facet on the simple type restriction for type" + name.getLocalPart() + " is incorrect.";
            throw new Exception(msg);
        }
    }
    if (!boundedDecimal) {
        if (anonymous && corbaTypeImpl instanceof Anonfixed) {
            Anonfixed fixed = (Anonfixed) corbaTypeImpl;
            digits = Integer.parseInt(String.valueOf(fixed.getDigits()));
        } else if (corbaTypeImpl instanceof Fixed) {
            Fixed fixed = (Fixed) corbaTypeImpl;
            digits = Integer.parseInt(String.valueOf(fixed.getDigits()));
        }
    }
    if (!boundedScale) {
        if (anonymous) {
            Anonfixed fixed = (Anonfixed) corbaTypeImpl;
            scale = Integer.parseInt(String.valueOf(fixed.getScale()));
        } else {
            Fixed fixed = (Fixed) corbaTypeImpl;
            scale = Integer.parseInt(String.valueOf(fixed.getScale()));
        }
    }
    if (boundedDecimal || boundedScale) {
        if (anonymous) {
            corbaTypeImpl = getAnonFixedCorbaType(name, W3CConstants.NT_SCHEMA_DECIMAL, digits, scale);
        } else {
            corbaTypeImpl = getFixedCorbaType(name, W3CConstants.NT_SCHEMA_DECIMAL, digits, scale);
        }
    }
    return corbaTypeImpl;
}
Also used : XmlSchemaFacet(org.apache.ws.commons.schema.XmlSchemaFacet) Anonfixed(org.apache.cxf.binding.corba.wsdl.Anonfixed) XmlSchemaTotalDigitsFacet(org.apache.ws.commons.schema.XmlSchemaTotalDigitsFacet) XmlSchemaFractionDigitsFacet(org.apache.ws.commons.schema.XmlSchemaFractionDigitsFacet) Fixed(org.apache.cxf.binding.corba.wsdl.Fixed)

Example 3 with XmlSchemaFractionDigitsFacet

use of org.apache.ws.commons.schema.XmlSchemaFractionDigitsFacet in project cxf by apache.

the class FixedVisitor method visit.

public void visit(AST fixedNode) {
    // "typedef" <type_declarator>
    // <type_declarator> ::= <type_spec> <declarators>
    // <type_spec> ::= <simple_type_spec>
    // | <constr_type_spec>
    // <simple_type_spec> ::= <base_type_spec>
    // | <template_type_spec>
    // | <scoped_name>
    // <base_type_spec> ::= ... omitted (integer, char, octect, etc)
    // <template_type_spec> ::= <sequence_type>
    // | <string_type>
    // | <wstring_type>
    // | <fixed_pt_type>
    // <constr_type_spec> ::= <struct_type>
    // | <union_type>
    // | <enum_type>
    // <declarators> ::= <declarator> {"," <declarator>}*
    // <declarator> ::= <simple_declarator>
    // | <complex_declarator>
    // <simple_declarator> ::= <identifier>
    // <complex_declarator> ::= <array_declarator>
    // <array_declarator> ::= <identifier> <fixed_array_size>+
    // <fixed_array_size> ::= "[" <positive_int_const> "]"
    AST digitsNode = fixedNode.getFirstChild();
    AST scaleNode = digitsNode.getNextSibling();
    final Scope scopedName;
    if (identifierNode == null) {
        scopedName = TypesUtils.generateAnonymousScopedName(getScope(), schema);
    } else {
        scopedName = new Scope(getScope(), identifierNode);
    }
    // validate digits and scale
    Long digits = Long.valueOf(digitsNode.toString());
    Long scale = Long.valueOf(scaleNode.toString());
    if (digits < 1 || digits > 31) {
        // throw IllegalIDLException();
        System.out.println("Digits cannot be greater than 31");
        return;
    }
    if (scale.compareTo(digits) > 0) {
        // throw IllegalIDLException();
        System.out.println("Scale cannot be greater than digits");
        return;
    }
    // xmlschema:fixed
    XmlSchemaSimpleType fixedSimpleType = new XmlSchemaSimpleType(schema, true);
    XmlSchemaSimpleTypeRestriction fixedRestriction = new XmlSchemaSimpleTypeRestriction();
    fixedRestriction.setBaseTypeName(Constants.XSD_DECIMAL);
    XmlSchemaTotalDigitsFacet fixedTotalDigits = new XmlSchemaTotalDigitsFacet();
    fixedTotalDigits.setValue(digitsNode.toString());
    XmlSchemaFractionDigitsFacet fixedFractionDigits = new XmlSchemaFractionDigitsFacet();
    fixedFractionDigits.setValue(scaleNode.toString());
    fixedFractionDigits.setFixed(true);
    fixedRestriction.getFacets().add(fixedTotalDigits);
    fixedRestriction.getFacets().add(fixedFractionDigits);
    fixedSimpleType.setName(mapper.mapToQName(scopedName));
    fixedSimpleType.setContent(fixedRestriction);
    // add xmlschema:fixed
    setSchemaType(fixedSimpleType);
    final CorbaType type;
    if (identifierNode != null) {
        // corba:fixed
        Fixed corbaFixed = new Fixed();
        corbaFixed.setQName(new QName(typeMap.getTargetNamespace(), scopedName.toString()));
        corbaFixed.setDigits(digits);
        corbaFixed.setScale(scale);
        corbaFixed.setRepositoryID(scopedName.toIDLRepositoryID());
        // corbaFixed.setType(Constants.XSD_DECIMAL);
        corbaFixed.setType(fixedSimpleType.getQName());
        type = corbaFixed;
    } else {
        // corba:anonfixed
        Anonfixed corbaFixed = new Anonfixed();
        corbaFixed.setQName(new QName(typeMap.getTargetNamespace(), scopedName.toString()));
        corbaFixed.setDigits(digits);
        corbaFixed.setScale(scale);
        // corbaFixed.setType(Constants.XSD_DECIMAL);
        corbaFixed.setType(fixedSimpleType.getQName());
        typeMap.getStructOrExceptionOrUnion().add(corbaFixed);
        type = corbaFixed;
    }
    // add corba:fixed
    setCorbaType(type);
}
Also used : Anonfixed(org.apache.cxf.binding.corba.wsdl.Anonfixed) AST(antlr.collections.AST) XmlSchemaTotalDigitsFacet(org.apache.ws.commons.schema.XmlSchemaTotalDigitsFacet) XmlSchemaFractionDigitsFacet(org.apache.ws.commons.schema.XmlSchemaFractionDigitsFacet) CorbaType(org.apache.cxf.binding.corba.wsdl.CorbaType) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) QName(javax.xml.namespace.QName) XmlSchemaSimpleTypeRestriction(org.apache.ws.commons.schema.XmlSchemaSimpleTypeRestriction) Fixed(org.apache.cxf.binding.corba.wsdl.Fixed)

Aggregations

XmlSchemaFractionDigitsFacet (org.apache.ws.commons.schema.XmlSchemaFractionDigitsFacet)3 XmlSchemaTotalDigitsFacet (org.apache.ws.commons.schema.XmlSchemaTotalDigitsFacet)3 QName (javax.xml.namespace.QName)2 Anonfixed (org.apache.cxf.binding.corba.wsdl.Anonfixed)2 Fixed (org.apache.cxf.binding.corba.wsdl.Fixed)2 XmlSchemaFacet (org.apache.ws.commons.schema.XmlSchemaFacet)2 XmlSchemaSimpleType (org.apache.ws.commons.schema.XmlSchemaSimpleType)2 AST (antlr.collections.AST)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 TypeConstraint (eu.esdihumboldt.hale.common.schema.model.TypeConstraint)1 ValidationConstraint (eu.esdihumboldt.hale.common.schema.model.constraint.type.ValidationConstraint)1 UnionValidationConstraint (eu.esdihumboldt.hale.io.xsd.reader.internal.constraint.UnionValidationConstraint)1 AndValidator (eu.esdihumboldt.util.validator.AndValidator)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 OrValidator (eu.esdihumboldt.util.validator.OrValidator)1 PatternValidator (eu.esdihumboldt.util.validator.PatternValidator)1 Validator (eu.esdihumboldt.util.validator.Validator)1