Search in sources :

Example 1 with AnonymousXmlType

use of eu.esdihumboldt.hale.io.xsd.reader.internal.AnonymousXmlType in project hale by halestudio.

the class XmlSchemaReader method createAttribute.

private void createAttribute(XmlSchemaAttribute attribute, DefinitionGroup declaringGroup, String schemaLocation, String schemaNamespace) {
    // create attributes
    QName typeName = attribute.getSchemaTypeName();
    if (typeName != null) {
        QName attributeName = determineAttributeName(attribute, schemaNamespace);
        // resolve type by name
        XmlTypeDefinition type = getTypeForAttribute(typeName, declaringGroup, attributeName);
        // create property
        DefaultPropertyDefinition property = new DefaultPropertyDefinition(attributeName, declaringGroup, type);
        // set metadata and constraints
        setMetadataAndConstraints(property, attribute, schemaLocation);
    } else if (attribute.getSchemaType() != null) {
        XmlSchemaSimpleType simpleType = attribute.getSchemaType();
        // create an anonymous type
        QName anonymousName = new QName(getTypeIdentifier(declaringGroup) + "/" + attribute.getName(), // $NON-NLS-2$
        "AnonymousType");
        AnonymousXmlType anonymousType = new AnonymousXmlType(anonymousName);
        configureSimpleType(anonymousType, simpleType, schemaLocation);
        // create property
        DefaultPropertyDefinition property = new DefaultPropertyDefinition(determineAttributeName(attribute, schemaNamespace), declaringGroup, anonymousType);
        // set metadata and constraints
        setMetadataAndConstraints(property, attribute, schemaLocation);
    } else if (attribute.getRefName() != null) {
        // <attribute ref="REF_NAME" />
        // reference to a named attribute
        QName attName = attribute.getRefName();
        XmlAttributeReferenceProperty property = new XmlAttributeReferenceProperty(attName, declaringGroup, this.index, attName);
        // set metadata and constraints
        setMetadataAndConstraints(property, attribute, schemaLocation);
    } else {
        /*
			 * Attribute with no type given has anySimpleType, see
			 * "http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/#cAttribute_Declarations"
			 */
        // resolve type by name
        XmlTypeDefinition type = index.getOrCreateType(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "anySimpleType"));
        // create property
        DefaultPropertyDefinition property = new DefaultPropertyDefinition(determineAttributeName(attribute, schemaNamespace), declaringGroup, type);
        // set metadata and constraints
        setMetadataAndConstraints(property, attribute, schemaLocation);
    }
}
Also used : DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) QName(javax.xml.namespace.QName) XmlTypeDefinition(eu.esdihumboldt.hale.io.xsd.reader.internal.XmlTypeDefinition) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) AnonymousXmlType(eu.esdihumboldt.hale.io.xsd.reader.internal.AnonymousXmlType) XmlAttributeReferenceProperty(eu.esdihumboldt.hale.io.xsd.reader.internal.XmlAttributeReferenceProperty)

Example 2 with AnonymousXmlType

use of eu.esdihumboldt.hale.io.xsd.reader.internal.AnonymousXmlType in project hale by halestudio.

the class XmlSchemaReader method createPropertyFromElement.

/**
 * Create a property from an element
 *
 * @param element the schema element
 * @param declaringGroup the definition of the declaring group
 * @param schemaLocation the schema location
 * @param schemaNamespace the schema namespace
 */
private void createPropertyFromElement(XmlSchemaElement element, DefinitionGroup declaringGroup, String schemaLocation, String schemaNamespace) {
    if (element.getSchemaTypeName() != null) {
        // element referencing a type
        // <element name="ELEMENT_NAME" type="SCHEMA_TYPE_NAME" />
        QName elementName = element.getQName();
        SubstitutionGroupProperty substitutionGroup = new SubstitutionGroupProperty(new QName(elementName.getNamespaceURI() + "/" + elementName.getLocalPart(), // TODO
        "choice"), // naming?
        declaringGroup);
        DefaultPropertyDefinition property = new DefaultPropertyDefinition(elementName, substitutionGroup, index.getOrCreateType(element.getSchemaTypeName()));
        // set metadata and constraints
        setMetadataAndConstraints(property, element, schemaLocation);
        substitutionGroup.setProperty(property);
    } else if (element.getRefName() != null) {
        // references another element
        // <element ref="REF_NAME" />
        QName elementName = element.getRefName();
        SubstitutionGroupProperty substitutionGroup = new SubstitutionGroupProperty(new QName(elementName.getNamespaceURI() + "/" + elementName.getLocalPart(), // TODO
        "choice"), // naming?
        declaringGroup);
        XmlElementReferenceProperty property = new XmlElementReferenceProperty(elementName, substitutionGroup, index, elementName);
        // set metadata and constraints FIXME can the constraints be set at
        // this point? or must the property determine them from the
        // SchemaElement?
        setMetadataAndConstraints(property, element, schemaLocation);
        substitutionGroup.setProperty(property);
    } else if (element.getSchemaType() != null) {
        // definition
        if (element.getSchemaType() instanceof XmlSchemaComplexType) {
            // <element ...>
            // <complexType>
            XmlSchemaComplexType complexType = (XmlSchemaComplexType) element.getSchemaType();
            XmlSchemaContentModel model = complexType.getContentModel();
            if (model != null) {
                XmlSchemaContent content = model.getContent();
                QName superTypeName = null;
                if (content instanceof XmlSchemaComplexContentExtension || content instanceof XmlSchemaComplexContentRestriction) {
                    // <complexContent>
                    // <extension base="..."> / <restriction ...>
                    String nameExt;
                    if (content instanceof XmlSchemaComplexContentExtension) {
                        superTypeName = ((XmlSchemaComplexContentExtension) content).getBaseTypeName();
                        // $NON-NLS-1$
                        nameExt = "Extension";
                    } else {
                        superTypeName = ((XmlSchemaComplexContentRestriction) content).getBaseTypeName();
                        // $NON-NLS-1$
                        nameExt = "Restriction";
                    }
                    if (superTypeName != null) {
                        // try to get the type definition of the super type
                        XmlTypeDefinition superType = index.getOrCreateType(superTypeName);
                        // create an anonymous type that extends the super
                        // type
                        QName anonymousName = new QName(getTypeIdentifier(declaringGroup) + "/" + element.getName(), // $NON-NLS-1$
                        superTypeName.getLocalPart() + nameExt);
                        AnonymousXmlType anonymousType = new AnonymousXmlType(anonymousName);
                        anonymousType.setSuperType(superType);
                        // set metadata and constraints
                        setMetadataAndConstraints(anonymousType, complexType, schemaLocation);
                        // add properties to the anonymous type
                        createProperties(anonymousType, complexType, schemaLocation, schemaNamespace);
                        // create a property with the anonymous type
                        DefaultPropertyDefinition property = new DefaultPropertyDefinition(element.getQName(), declaringGroup, anonymousType);
                        // set metadata and constraints
                        setMetadataAndConstraints(property, element, schemaLocation);
                    } else {
                        reporter.error(new IOMessageImpl("Could not determine super type for complex content", null, content.getLineNumber(), content.getLinePosition()));
                    }
                // </extension> / </restriction>
                // </complexContent>
                } else if (content instanceof XmlSchemaSimpleContentExtension || content instanceof XmlSchemaSimpleContentRestriction) {
                    // <simpleContent>
                    // <extension base="..."> / <restriction ...>
                    String nameExt;
                    if (content instanceof XmlSchemaSimpleContentExtension) {
                        superTypeName = ((XmlSchemaSimpleContentExtension) content).getBaseTypeName();
                        // $NON-NLS-1$
                        nameExt = "Extension";
                    } else {
                        superTypeName = ((XmlSchemaSimpleContentRestriction) content).getBaseTypeName();
                        // $NON-NLS-1$
                        nameExt = "Restriction";
                    }
                    if (superTypeName != null) {
                        // try to get the type definition of the super type
                        XmlTypeDefinition superType = index.getOrCreateType(superTypeName);
                        // create an anonymous type that extends the super
                        // type
                        QName anonymousName = new QName(getTypeIdentifier(declaringGroup) + "/" + element.getName(), // $NON-NLS-1$
                        superTypeName.getLocalPart() + nameExt);
                        AnonymousXmlType anonymousType = new AnonymousXmlType(anonymousName);
                        anonymousType.setSuperType(superType);
                        // set metadata and constraints
                        setMetadata(anonymousType, complexType, schemaLocation);
                        anonymousType.setConstraint(HasValueFlag.ENABLED);
                        // set no binding, inherit it from the super type
                        // XXX is this ok?
                        // add properties to the anonymous type
                        createProperties(anonymousType, complexType, schemaLocation, schemaNamespace);
                        // create a property with the anonymous type
                        DefaultPropertyDefinition property = new DefaultPropertyDefinition(element.getQName(), declaringGroup, anonymousType);
                        // set metadata and constraints
                        setMetadataAndConstraints(property, element, schemaLocation);
                    } else {
                        reporter.error(new IOMessageImpl("Could not determine super type for simple content", null, content.getLineNumber(), content.getLinePosition()));
                    }
                // </extension>
                // </simpleContent>
                }
            } else {
                // this where we get when there is an anonymous complex type
                // as property type
                // create an anonymous type
                QName anonymousName = new QName(getTypeIdentifier(declaringGroup) + "/" + element.getName(), "AnonymousType");
                // create anonymous type with no super type
                AnonymousXmlType anonymousType = new AnonymousXmlType(anonymousName);
                // set metadata and constraints
                setMetadataAndConstraints(anonymousType, complexType, schemaLocation);
                // add properties to the anonymous type
                createProperties(anonymousType, complexType, schemaLocation, schemaNamespace);
                // create a property with the anonymous type
                DefaultPropertyDefinition property = new DefaultPropertyDefinition(element.getQName(), declaringGroup, anonymousType);
                // set metadata and constraints
                setMetadataAndConstraints(property, element, schemaLocation);
            }
        // </complexType>
        // </element>
        } else if (element.getSchemaType() instanceof XmlSchemaSimpleType) {
            // simple schema type
            XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType) element.getSchemaType();
            // create an anonymous type
            QName anonymousName = new QName(getTypeIdentifier(declaringGroup) + "/" + element.getName(), // $NON-NLS-1$
            "AnonymousType");
            AnonymousXmlType anonymousType = new AnonymousXmlType(anonymousName);
            configureSimpleType(anonymousType, simpleType, schemaLocation);
            // create a property with the anonymous type
            DefaultPropertyDefinition property = new DefaultPropertyDefinition(element.getQName(), declaringGroup, anonymousType);
            // set metadata and constraints
            setMetadataAndConstraints(property, element, schemaLocation);
        }
    } else {
        // <element name="..." />
        // no type defined
        reporter.warn(new IOMessageImpl("Element definition without an associated type: {0}", null, element.getLineNumber(), element.getLinePosition(), element.getQName()));
        // assuming xsd:anyType as default type
        QName elementName = element.getQName();
        SubstitutionGroupProperty substitutionGroup = new SubstitutionGroupProperty(new QName(elementName.getNamespaceURI() + "/" + elementName.getLocalPart(), // TODO
        "choice"), // naming?
        declaringGroup);
        DefaultPropertyDefinition property = new DefaultPropertyDefinition(elementName, substitutionGroup, index.getOrCreateType(XmlTypeUtil.NAME_ANY_TYPE));
        // set metadata and constraints
        setMetadataAndConstraints(property, element, schemaLocation);
        substitutionGroup.setProperty(property);
    }
}
Also used : DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) XmlSchemaComplexContentExtension(org.apache.ws.commons.schema.XmlSchemaComplexContentExtension) XmlSchemaSimpleContentExtension(org.apache.ws.commons.schema.XmlSchemaSimpleContentExtension) QName(javax.xml.namespace.QName) AnonymousXmlType(eu.esdihumboldt.hale.io.xsd.reader.internal.AnonymousXmlType) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) XmlElementReferenceProperty(eu.esdihumboldt.hale.io.xsd.reader.internal.XmlElementReferenceProperty) XmlSchemaContentModel(org.apache.ws.commons.schema.XmlSchemaContentModel) XmlSchemaSimpleContentRestriction(org.apache.ws.commons.schema.XmlSchemaSimpleContentRestriction) SubstitutionGroupProperty(eu.esdihumboldt.hale.io.xsd.reader.internal.SubstitutionGroupProperty) XmlTypeDefinition(eu.esdihumboldt.hale.io.xsd.reader.internal.XmlTypeDefinition) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlSchemaContent(org.apache.ws.commons.schema.XmlSchemaContent) XmlSchemaComplexContentRestriction(org.apache.ws.commons.schema.XmlSchemaComplexContentRestriction) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType)

Aggregations

DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)2 AnonymousXmlType (eu.esdihumboldt.hale.io.xsd.reader.internal.AnonymousXmlType)2 XmlTypeDefinition (eu.esdihumboldt.hale.io.xsd.reader.internal.XmlTypeDefinition)2 QName (javax.xml.namespace.QName)2 XmlSchemaSimpleType (org.apache.ws.commons.schema.XmlSchemaSimpleType)2 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 SubstitutionGroupProperty (eu.esdihumboldt.hale.io.xsd.reader.internal.SubstitutionGroupProperty)1 XmlAttributeReferenceProperty (eu.esdihumboldt.hale.io.xsd.reader.internal.XmlAttributeReferenceProperty)1 XmlElementReferenceProperty (eu.esdihumboldt.hale.io.xsd.reader.internal.XmlElementReferenceProperty)1 XmlSchemaComplexContentExtension (org.apache.ws.commons.schema.XmlSchemaComplexContentExtension)1 XmlSchemaComplexContentRestriction (org.apache.ws.commons.schema.XmlSchemaComplexContentRestriction)1 XmlSchemaComplexType (org.apache.ws.commons.schema.XmlSchemaComplexType)1 XmlSchemaContent (org.apache.ws.commons.schema.XmlSchemaContent)1 XmlSchemaContentModel (org.apache.ws.commons.schema.XmlSchemaContentModel)1 XmlSchemaSimpleContentExtension (org.apache.ws.commons.schema.XmlSchemaSimpleContentExtension)1 XmlSchemaSimpleContentRestriction (org.apache.ws.commons.schema.XmlSchemaSimpleContentRestriction)1