Search in sources :

Example 1 with XmlSchemaAttributeGroup

use of org.apache.ws.commons.schema.XmlSchemaAttributeGroup in project hale by halestudio.

the class XmlSchemaReader method loadSchema.

/**
 * Load the feature types defined by the given schema
 *
 * @param schemaLocation the schema location
 * @param xmlSchema the schema
 * @param imports the imports/includes that were already loaded or where
 *            loading has been started
 * @param progress the progress indicator
 * @param mainSchema states if this is a main schema and therefore elements
 *            declared here should be flagged mappable
 */
protected void loadSchema(String schemaLocation, XmlSchema xmlSchema, Set<String> imports, ProgressIndicator progress, boolean mainSchema) {
    String namespace = xmlSchema.getTargetNamespace();
    if (namespace == null) {
        namespace = XMLConstants.NULL_NS_URI;
    }
    // add namespace prefixes
    NamespacePrefixList namespaces = xmlSchema.getNamespaceContext();
    addPrefixes(namespaces, namespace, mainSchema);
    // the schema items
    XmlSchemaObjectCollection items = xmlSchema.getItems();
    // go through all schema items
    for (int i = 0; i < items.getCount(); i++) {
        XmlSchemaObject item = items.getItem(i);
        if (item instanceof XmlSchemaElement) {
            // global element declaration
            XmlSchemaElement element = (XmlSchemaElement) item;
            // determine type
            XmlTypeDefinition elementType = null;
            if (element.getSchemaTypeName() != null) {
                // reference to type
                elementType = index.getOrCreateType(element.getSchemaTypeName());
            } else if (element.getSchemaType() != null) {
                // element has internal type definition, generate anonymous
                // type name
                QName typeName = new QName(element.getQName().getNamespaceURI(), // $NON-NLS-1$
                element.getQName().getLocalPart() + "_AnonymousType");
                // create type
                elementType = createType(element.getSchemaType(), typeName, schemaLocation, namespace, mainSchema);
            } else if (element.getQName() != null) {
                // element with no type
                elementType = index.getOrCreateType(XmlTypeUtil.NAME_ANY_TYPE);
            }
            if (elementType != null) {
                // the element name
                // XXX use element QName instead?
                QName elementName = new QName(namespace, element.getName());
                // the substitution group
                QName subGroup = element.getSubstitutionGroup();
                // TODO do we also need an index for substitutions?
                // create schema element
                XmlElement schemaElement = new XmlElement(elementName, elementType, subGroup);
                // set metadata
                setMetadata(schemaElement, element, schemaLocation);
                // extend XmlElements constraint
                XmlElements xmlElements = elementType.getConstraint(XmlElements.class);
                xmlElements.addElement(schemaElement);
                // set custom display name
                elementType.setConstraint(new ElementName(xmlElements));
                // set Mappable constraint (e.g. Mappable)
                // for types with an associated element it can be determined
                // on the spot if it is mappable
                configureMappingRelevant(elementType, mainSchema);
                // XXX needed? may result in conflicts when defining
                // mappable types manually XXX the element is also marked
                // with the Mappable constraint, to help with cases where
                // multiple elements are defined for one
                // schemaElement.setConstraint(MappableFlag.get(mainSchema));
                // store element in index
                index.getElements().put(elementName, schemaElement);
            } else {
                reporter.error(new IOMessageImpl(MessageFormat.format("No type for element {0} found.", element.getName()), null, element.getLineNumber(), element.getLinePosition()));
            }
        } else if (item instanceof XmlSchemaType) {
            // complex or simple type
            createType((XmlSchemaType) item, null, schemaLocation, namespace, mainSchema);
        } else if (item instanceof XmlSchemaAttribute) {
            // schema attribute that might be referenced somewhere
            XmlSchemaAttribute att = (XmlSchemaAttribute) item;
            if (att.getQName() != null) {
                XmlTypeDefinition type = getAttributeType(att, null, schemaLocation);
                if (type == null) {
                    // XXX if this occurs we might need a attribute
                    // referencing attribute
                    reporter.error(new IOMessageImpl("Could not determine attribute type", null, att.getLineNumber(), att.getLinePosition()));
                } else {
                    XmlAttribute attribute = new XmlAttribute(att.getQName(), type);
                    index.getAttributes().put(attribute.getName(), attribute);
                }
            } else {
                reporter.warn(new IOMessageImpl(MessageFormat.format("Attribute could not be processed: {0}", att.getName()), null, att.getLineNumber(), att.getLinePosition()));
            }
        } else if (item instanceof XmlSchemaAttributeGroup) {
            // schema attribute group that might be referenced somewhere
            XmlSchemaAttributeGroup attributeGroup = (XmlSchemaAttributeGroup) item;
            if (attributeGroup.getName() != null) {
                String groupIdent = attributeGroup.getName().getNamespaceURI() + "/" + attributeGroup.getName().getLocalPart();
                XmlAttributeGroup attGroup = new XmlAttributeGroup(groupIdent, true);
                createAttributes(attributeGroup, attGroup, "", schemaLocation, namespace);
                index.getAttributeGroups().put(attributeGroup.getName(), attGroup);
            } else {
                reporter.warn(new IOMessageImpl("Attribute group could not be processed", null, attributeGroup.getLineNumber(), attributeGroup.getLinePosition()));
            }
        } else if (item instanceof XmlSchemaGroup) {
            // group that might be referenced somewhere
            XmlSchemaGroup schemaGroup = (XmlSchemaGroup) item;
            if (schemaGroup.getName() != null) {
                String groupIdent = schemaGroup.getName().getNamespaceURI() + "/" + schemaGroup.getName().getLocalPart();
                XmlGroup group = new XmlGroup(groupIdent, true);
                createPropertiesFromParticle(group, schemaGroup.getParticle(), schemaLocation, namespace, false);
                index.getGroups().put(schemaGroup.getName(), group);
            } else {
                reporter.warn(new IOMessageImpl("Group could not be processed", null, schemaGroup.getLineNumber(), schemaGroup.getLinePosition()));
            }
        } else if (item instanceof XmlSchemaImport || item instanceof XmlSchemaInclude) {
        // ignore, is treated separately
        } else if (item instanceof XmlSchemaNotation) {
        // notations are ignored
        } else {
            reporter.error(new IOMessageImpl("Unrecognized global definition: " + item.getClass().getSimpleName(), null, item.getLineNumber(), item.getLinePosition()));
        }
    }
    // Set of include locations
    Set<String> includes = new HashSet<String>();
    // handle imports
    XmlSchemaObjectCollection externalItems = xmlSchema.getIncludes();
    if (externalItems.getCount() > 0) {
        // $NON-NLS-1$
        _log.info("Loading includes and imports for schema at " + schemaLocation);
    }
    for (int i = 0; i < externalItems.getCount(); i++) {
        try {
            XmlSchemaExternal imp = (XmlSchemaExternal) externalItems.getItem(i);
            XmlSchema importedSchema = imp.getSchema();
            String location = importedSchema.getSourceURI();
            if (!(imports.contains(location))) {
                // only add schemas that
                // were not already
                // added
                // place a marker in the map to
                imports.add(location);
                // prevent loading the location in
                // the call to loadSchema
                loadSchema(location, importedSchema, imports, progress, mainSchema && imp instanceof XmlSchemaInclude);
            // is part of main schema if it's a main schema include
            }
            if (imp instanceof XmlSchemaInclude) {
                includes.add(location);
            }
        } catch (Throwable e) {
            reporter.error(new IOMessageImpl("Error adding imported schema from " + schemaLocation, // $NON-NLS-1$
            e));
        }
    }
    // $NON-NLS-1$
    _log.info("Creating types for schema at " + schemaLocation);
    progress.setCurrentTask(// $NON-NLS-1$
    MessageFormat.format(Messages.getString("ApacheSchemaProvider.33"), namespace));
}
Also used : XmlAttribute(eu.esdihumboldt.hale.io.xsd.model.XmlAttribute) XmlAttributeGroup(eu.esdihumboldt.hale.io.xsd.model.XmlAttributeGroup) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) NamespacePrefixList(org.apache.ws.commons.schema.utils.NamespacePrefixList) XmlGroup(eu.esdihumboldt.hale.io.xsd.model.XmlGroup) XmlElements(eu.esdihumboldt.hale.io.xsd.constraint.XmlElements) XmlSchemaObject(org.apache.ws.commons.schema.XmlSchemaObject) XmlTypeDefinition(eu.esdihumboldt.hale.io.xsd.reader.internal.XmlTypeDefinition) ElementName(eu.esdihumboldt.hale.io.xsd.reader.internal.constraint.ElementName) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport) XmlSchemaObjectCollection(org.apache.ws.commons.schema.XmlSchemaObjectCollection) HashSet(java.util.HashSet) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) XmlSchemaAttributeGroup(org.apache.ws.commons.schema.XmlSchemaAttributeGroup) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlSchemaAttribute(org.apache.ws.commons.schema.XmlSchemaAttribute) XmlSchemaExternal(org.apache.ws.commons.schema.XmlSchemaExternal) XmlSchemaGroup(org.apache.ws.commons.schema.XmlSchemaGroup) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaNotation(org.apache.ws.commons.schema.XmlSchemaNotation) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) XmlSchemaInclude(org.apache.ws.commons.schema.XmlSchemaInclude)

Example 2 with XmlSchemaAttributeGroup

use of org.apache.ws.commons.schema.XmlSchemaAttributeGroup in project hale by halestudio.

the class XmlSchemaReader method createAttributesFromCollection.

private void createAttributesFromCollection(XmlSchemaObjectCollection attributeCollection, DefinitionGroup declaringType, String indexPrefix, String schemaLocation, String schemaNamespace) {
    if (indexPrefix == null) {
        // $NON-NLS-1$
        indexPrefix = "";
    }
    for (int index = 0; index < attributeCollection.getCount(); index++) {
        XmlSchemaObject object = attributeCollection.getItem(index);
        if (object instanceof XmlSchemaAttribute) {
            // <attribute ... />
            XmlSchemaAttribute attribute = (XmlSchemaAttribute) object;
            createAttribute(attribute, declaringType, schemaLocation, schemaNamespace);
        } else if (object instanceof XmlSchemaAttributeGroup) {
            XmlSchemaAttributeGroup group = (XmlSchemaAttributeGroup) object;
            createAttributes(group, declaringType, indexPrefix + index, schemaLocation, schemaNamespace);
        } else if (object instanceof XmlSchemaAttributeGroupRef) {
            XmlSchemaAttributeGroupRef groupRef = (XmlSchemaAttributeGroupRef) object;
            if (groupRef.getRefName() != null) {
                QName groupName = groupRef.getRefName();
                // XXX extend group name with namespace?
                XmlAttributeGroupReferenceProperty property = new XmlAttributeGroupReferenceProperty(groupName, declaringType, this.index, groupName, true);
                // TODO add constraints?
                // set metadata
                setMetadata(property, groupRef, schemaLocation);
            } else {
                reporter.error(new IOMessageImpl("Unrecognized attribute group reference", null, object.getLineNumber(), object.getLinePosition()));
            }
        }
    }
}
Also used : XmlAttributeGroupReferenceProperty(eu.esdihumboldt.hale.io.xsd.reader.internal.XmlAttributeGroupReferenceProperty) XmlSchemaObject(org.apache.ws.commons.schema.XmlSchemaObject) QName(javax.xml.namespace.QName) XmlSchemaAttributeGroup(org.apache.ws.commons.schema.XmlSchemaAttributeGroup) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) XmlSchemaAttribute(org.apache.ws.commons.schema.XmlSchemaAttribute) XmlSchemaAttributeGroupRef(org.apache.ws.commons.schema.XmlSchemaAttributeGroupRef)

Example 3 with XmlSchemaAttributeGroup

use of org.apache.ws.commons.schema.XmlSchemaAttributeGroup in project hale by halestudio.

the class XmlSchemaReader method loadSchema.

/**
 * Load the feature types defined by the given schema
 *
 * @param schemaLocation the schema location
 * @param xmlSchema the schema
 * @param imports the imports/includes that were already loaded or where
 *            loading has been started
 * @param progress the progress indicator
 * @param mainSchema states if this is a main schema and therefore elements
 *            declared here should be flagged mappable
 */
protected void loadSchema(String schemaLocation, XmlSchema xmlSchema, Map<String, String> imports, ProgressIndicator progress, boolean mainSchema) {
    String namespace = xmlSchema.getTargetNamespace();
    if (namespace == null) {
        namespace = XMLConstants.NULL_NS_URI;
    }
    // add namespace prefixes
    NamespacePrefixList namespaces = xmlSchema.getNamespaceContext();
    addPrefixes(namespaces, namespace, mainSchema);
    // the schema items
    XmlSchemaObjectCollection items = xmlSchema.getItems();
    // go through all schema items
    for (int i = 0; i < items.getCount(); i++) {
        XmlSchemaObject item = items.getItem(i);
        if (item instanceof XmlSchemaElement) {
            // global element declaration
            XmlSchemaElement element = (XmlSchemaElement) item;
            // determine type
            XmlTypeDefinition elementType = null;
            if (element.getSchemaTypeName() != null) {
                // reference to type
                elementType = index.getOrCreateType(element.getSchemaTypeName());
            } else if (element.getSchemaType() != null) {
                // element has internal type definition, generate anonymous
                // type name
                QName typeName = new QName(element.getQName().getNamespaceURI(), // $NON-NLS-1$
                element.getQName().getLocalPart() + "_AnonymousType");
                // create type
                elementType = createType(element.getSchemaType(), typeName, schemaLocation, namespace, mainSchema);
            } else if (element.getQName() != null) {
                // element with no type
                elementType = index.getOrCreateType(XmlTypeUtil.NAME_ANY_TYPE);
            }
            if (elementType != null) {
                // the element name
                // XXX use element QName instead?
                QName elementName = new QName(namespace, element.getName());
                // the substitution group
                QName subGroup = element.getSubstitutionGroup();
                // TODO do we also need an index for substitutions?
                // create schema element
                XmlElement schemaElement = new XmlElement(elementName, elementType, subGroup);
                // set metadata
                setMetadata(schemaElement, element, schemaLocation);
                // extend XmlElements constraint
                XmlElements xmlElements = elementType.getConstraint(XmlElements.class);
                xmlElements.addElement(schemaElement);
                // set custom display name
                elementType.setConstraint(new ElementName(xmlElements));
                // set Mappable constraint (e.g. Mappable)
                // for types with an associated element it can be determined
                // on the spot if it is mappable
                configureMappingRelevant(elementType, mainSchema);
                // XXX needed? may result in conflicts when defining
                // mappable types manually XXX the element is also marked
                // with the Mappable constraint, to help with cases where
                // multiple elements are defined for one
                // schemaElement.setConstraint(MappableFlag.get(mainSchema));
                // store element in index
                index.getElements().put(elementName, schemaElement);
            } else {
                reporter.error(new IOMessageImpl(MessageFormat.format("No type for element {0} found.", element.getName()), null, element.getLineNumber(), element.getLinePosition()));
            }
        } else if (item instanceof XmlSchemaType) {
            // complex or simple type
            createType((XmlSchemaType) item, null, schemaLocation, namespace, mainSchema);
        } else if (item instanceof XmlSchemaAttribute) {
            // schema attribute that might be referenced somewhere
            XmlSchemaAttribute att = (XmlSchemaAttribute) item;
            if (att.getQName() != null) {
                XmlTypeDefinition type = getAttributeType(att, null, schemaLocation);
                if (type == null) {
                    // XXX if this occurs we might need a attribute
                    // referencing attribute
                    reporter.error(new IOMessageImpl("Could not determine attribute type", null, att.getLineNumber(), att.getLinePosition()));
                } else {
                    XmlAttribute attribute = new XmlAttribute(att.getQName(), type);
                    index.getAttributes().put(attribute.getName(), attribute);
                }
            } else {
                reporter.warn(new IOMessageImpl(MessageFormat.format("Attribute could not be processed: {0}", att.getName()), null, att.getLineNumber(), att.getLinePosition()));
            }
        } else if (item instanceof XmlSchemaAttributeGroup) {
            // schema attribute group that might be referenced somewhere
            XmlSchemaAttributeGroup attributeGroup = (XmlSchemaAttributeGroup) item;
            if (attributeGroup.getName() != null) {
                String groupIdent = attributeGroup.getName().getNamespaceURI() + "/" + attributeGroup.getName().getLocalPart();
                XmlAttributeGroup attGroup = new XmlAttributeGroup(groupIdent, true);
                createAttributes(attributeGroup, attGroup, "", schemaLocation, namespace);
                index.getAttributeGroups().put(attributeGroup.getName(), attGroup);
            } else {
                reporter.warn(new IOMessageImpl("Attribute group could not be processed", null, attributeGroup.getLineNumber(), attributeGroup.getLinePosition()));
            }
        } else if (item instanceof XmlSchemaGroup) {
            // group that might be referenced somewhere
            XmlSchemaGroup schemaGroup = (XmlSchemaGroup) item;
            if (schemaGroup.getName() != null) {
                String groupIdent = schemaGroup.getName().getNamespaceURI() + "/" + schemaGroup.getName().getLocalPart();
                XmlGroup group = new XmlGroup(groupIdent, true);
                createPropertiesFromParticle(group, schemaGroup.getParticle(), schemaLocation, namespace, false);
                index.getGroups().put(schemaGroup.getName(), group);
            } else {
                reporter.warn(new IOMessageImpl("Group could not be processed", null, schemaGroup.getLineNumber(), schemaGroup.getLinePosition()));
            }
        } else if (item instanceof XmlSchemaImport || item instanceof XmlSchemaInclude) {
        // ignore, is treated separately
        } else if (item instanceof XmlSchemaNotation) {
        // notations are ignored
        } else {
            reporter.error(new IOMessageImpl("Unrecognized global definition: " + item.getClass().getSimpleName(), null, item.getLineNumber(), item.getLinePosition()));
        }
    }
    // Set of include locations
    Set<String> includes = new HashSet<String>();
    // handle imports
    XmlSchemaObjectCollection externalItems = xmlSchema.getIncludes();
    if (externalItems.getCount() > 0) {
        // $NON-NLS-1$
        _log.info("Loading includes and imports for schema at " + schemaLocation);
    }
    for (int i = 0; i < externalItems.getCount(); i++) {
        try {
            XmlSchemaExternal imp = (XmlSchemaExternal) externalItems.getItem(i);
            XmlSchema importedSchema = imp.getSchema();
            String location = importedSchema.getSourceURI();
            String targetNamespace = importedSchema.getTargetNamespace();
            if (!imports.containsKey(location)) {
                // only add schemas that
                // were not already
                // added
                boolean addedBefore = imports.entrySet().stream().anyMatch(e -> {
                    // and "https") and they have the same target namespace.
                    return targetNamespace.equals(e.getValue()) && schemeIndependentEquals(location, e.getKey());
                });
                if (!addedBefore) {
                    // place a marker in the map to prevent loading the
                    // location in the call to loadSchema
                    imports.put(location, targetNamespace);
                    loadSchema(location, importedSchema, imports, progress, mainSchema && imp instanceof XmlSchemaInclude);
                // is part of main schema if it's a main schema include
                }
            }
            if (imp instanceof XmlSchemaInclude) {
                includes.add(location);
            }
        } catch (Throwable e) {
            reporter.error(new IOMessageImpl("Error adding imported schema from " + schemaLocation, // $NON-NLS-1$
            e));
        }
    }
    // $NON-NLS-1$
    _log.info("Creating types for schema at " + schemaLocation);
    progress.setCurrentTask(// $NON-NLS-1$
    MessageFormat.format(Messages.getString("ApacheSchemaProvider.33"), namespace));
}
Also used : XmlAttribute(eu.esdihumboldt.hale.io.xsd.model.XmlAttribute) XmlAttributeGroup(eu.esdihumboldt.hale.io.xsd.model.XmlAttributeGroup) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) NamespacePrefixList(org.apache.ws.commons.schema.utils.NamespacePrefixList) XmlGroup(eu.esdihumboldt.hale.io.xsd.model.XmlGroup) XmlElements(eu.esdihumboldt.hale.io.xsd.constraint.XmlElements) XmlSchemaObject(org.apache.ws.commons.schema.XmlSchemaObject) XmlTypeDefinition(eu.esdihumboldt.hale.io.xsd.reader.internal.XmlTypeDefinition) ElementName(eu.esdihumboldt.hale.io.xsd.reader.internal.constraint.ElementName) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport) XmlSchemaObjectCollection(org.apache.ws.commons.schema.XmlSchemaObjectCollection) HashSet(java.util.HashSet) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) XmlSchemaAttributeGroup(org.apache.ws.commons.schema.XmlSchemaAttributeGroup) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlSchemaAttribute(org.apache.ws.commons.schema.XmlSchemaAttribute) XmlSchemaExternal(org.apache.ws.commons.schema.XmlSchemaExternal) XmlSchemaGroup(org.apache.ws.commons.schema.XmlSchemaGroup) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaNotation(org.apache.ws.commons.schema.XmlSchemaNotation) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) XmlSchemaInclude(org.apache.ws.commons.schema.XmlSchemaInclude)

Aggregations

IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)3 QName (javax.xml.namespace.QName)3 XmlSchemaAttribute (org.apache.ws.commons.schema.XmlSchemaAttribute)3 XmlSchemaAttributeGroup (org.apache.ws.commons.schema.XmlSchemaAttributeGroup)3 XmlSchemaObject (org.apache.ws.commons.schema.XmlSchemaObject)3 XmlElements (eu.esdihumboldt.hale.io.xsd.constraint.XmlElements)2 XmlAttribute (eu.esdihumboldt.hale.io.xsd.model.XmlAttribute)2 XmlAttributeGroup (eu.esdihumboldt.hale.io.xsd.model.XmlAttributeGroup)2 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)2 XmlGroup (eu.esdihumboldt.hale.io.xsd.model.XmlGroup)2 XmlTypeDefinition (eu.esdihumboldt.hale.io.xsd.reader.internal.XmlTypeDefinition)2 ElementName (eu.esdihumboldt.hale.io.xsd.reader.internal.constraint.ElementName)2 HashSet (java.util.HashSet)2 XmlSchema (org.apache.ws.commons.schema.XmlSchema)2 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)2 XmlSchemaExternal (org.apache.ws.commons.schema.XmlSchemaExternal)2 XmlSchemaGroup (org.apache.ws.commons.schema.XmlSchemaGroup)2 XmlSchemaImport (org.apache.ws.commons.schema.XmlSchemaImport)2 XmlSchemaInclude (org.apache.ws.commons.schema.XmlSchemaInclude)2 XmlSchemaNotation (org.apache.ws.commons.schema.XmlSchemaNotation)2