Search in sources :

Example 1 with XmlElements

use of eu.esdihumboldt.hale.io.xsd.constraint.XmlElements 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 XmlElements

use of eu.esdihumboldt.hale.io.xsd.constraint.XmlElements in project hale by halestudio.

the class XmlSchemaReaderTest method testRead_definitive_chapter03.

/**
 * Test reading a simple XML schema that is split into several files. Tests
 * also the {@link XmlElements} and {@link MappingRelevantFlag} constraints
 *
 * @throws Exception if reading the schema fails
 */
@Test
public void testRead_definitive_chapter03() throws Exception {
    URI location = getClass().getResource("/testdata/definitive/chapter03env.xsd").toURI();
    LocatableInputSupplier<? extends InputStream> input = new DefaultInputSupplier(location);
    XmlIndex schema = (XmlIndex) readSchema(input);
    // envelope element
    XmlElement envelope = schema.getElements().get(new QName("http://example.org/ord", "envelope"));
    assertNotNull(envelope);
    TypeDefinition envType = envelope.getType();
    // mappable
    assertTrue(envType.getConstraint(MappingRelevantFlag.class).isEnabled());
    // XmlElements
    Collection<? extends XmlElement> elements = envType.getConstraint(XmlElements.class).getElements();
    assertEquals(1, elements.size());
    assertEquals(envelope, elements.iterator().next());
    // order
    PropertyDefinition order = envType.getChild(new QName("http://example.org/ord", "order")).asProperty();
    assertNotNull(order);
    TypeDefinition orderType = order.getPropertyType();
    // mappable
    assertTrue(orderType.getConstraint(MappingRelevantFlag.class).isEnabled());
    // number
    PropertyDefinition number = orderType.getChild(new QName("http://example.org/ord", "number")).asProperty();
    assertNotNull(number);
    // binding must be string
    assertEquals(String.class, number.getPropertyType().getConstraint(Binding.class).getBinding());
    // items
    PropertyDefinition items = orderType.getChild(new QName("http://example.org/ord", "items")).asProperty();
    assertNotNull(items);
    // not mappable
    assertFalse(items.getPropertyType().getConstraint(MappingRelevantFlag.class).isEnabled());
    // no elements
    assertTrue(items.getPropertyType().getConstraint(XmlElements.class).getElements().isEmpty());
    // SpecialOrderType
    // extension to OrderType, should be mappable using xsi:type
    TypeDefinition specialOrderType = schema.getType(new QName("http://example.org/ord", "SpecialOrderType"));
    assertNotNull(specialOrderType);
    // number of declared children
    assertEquals(1, specialOrderType.getDeclaredChildren().size());
    // number of children
    assertEquals(3, specialOrderType.getChildren().size());
    // mappable
    assertTrue(specialOrderType.getConstraint(MappableFlag.class).isEnabled());
    // no elements
    assertTrue(specialOrderType.getConstraint(XmlElements.class).getElements().isEmpty());
    // overall mappable types
    Collection<? extends TypeDefinition> mt = schema.getMappingRelevantTypes();
    // envelope, order, special order
    assertEquals(2, mt.size());
}
Also used : XmlElements(eu.esdihumboldt.hale.io.xsd.constraint.XmlElements) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) QName(javax.xml.namespace.QName) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) URI(java.net.URI) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Test(org.junit.Test)

Example 3 with XmlElements

use of eu.esdihumboldt.hale.io.xsd.constraint.XmlElements in project hale by halestudio.

the class WFSGetFeatureWizard method addPages.

@Override
public void addPages() {
    super.addPages();
    /**
     * Page for specifying the WFS capabilities URL.
     */
    AbstractWFSCapabilitiesPage<WFSGetFeatureConfig> capPage = new AbstractWFSCapabilitiesPage<WFSGetFeatureConfig>(this) {

        @Override
        protected boolean updateConfiguration(WFSGetFeatureConfig configuration, URL capabilitiesUrl, WFSCapabilities capabilities) {
            if (capabilities != null && capabilities.getGetFeatureOp() != null) {
                WFSOperation op = capabilities.getGetFeatureOp();
                configuration.setGetFeatureUri(URI.create(op.getHttpGetUrl()));
                configuration.setVersion(capabilities.getVersion());
                return true;
            }
            setErrorMessage("Invalid capabilities or WFS does not support GetFeature KVP");
            return false;
        }
    };
    addPage(capPage);
    addPage(new AbstractFeatureTypesPage<WFSGetFeatureConfig>(this, capPage, "Please specify the feature types to request") {

        private boolean selectAll = false;

        @Override
        protected void updateState(Set<QName> selected) {
            // at least one type must be specified
            setPageComplete(!selected.isEmpty());
        }

        @Override
        protected Collection<? extends QName> initialSelection(Set<QName> types) {
            // select all by default
            if (selectAll) {
                return types;
            }
            return super.initialSelection(types);
        }

        @Override
        protected Set<QName> filterTypes(Set<QName> types) {
            // relevant types
            if (schemaSpaceID != null) {
                SchemaService ss = PlatformUI.getWorkbench().getService(SchemaService.class);
                if (ss != null) {
                    Set<QName> relevantElements = new HashSet<>();
                    SchemaSpace schemas = ss.getSchemas(schemaSpaceID);
                    for (TypeDefinition type : schemas.getMappingRelevantTypes()) {
                        XmlElements elms = type.getConstraint(XmlElements.class);
                        for (XmlElement elm : elms.getElements()) {
                            relevantElements.add(elm.getName());
                        }
                    }
                    Set<QName> selection = new HashSet<>(types);
                    selection.retainAll(relevantElements);
                    // don't filter if we have no match at all
                    if (!selection.isEmpty()) {
                        selectAll = true;
                        return selection;
                    }
                }
            }
            selectAll = false;
            return super.filterTypes(types);
        }

        @Override
        protected boolean updateConfiguration(WFSGetFeatureConfig configuration, Set<QName> selected) {
            configuration.getTypeNames().clear();
            configuration.getTypeNames().addAll(selected);
            return true;
        }
    });
    // bounding box
    addPage(new BBOXPage(this, capPage));
    // additional params
    addPage(new GetFeatureParamsPage(this));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) QName(javax.xml.namespace.QName) SchemaSpace(eu.esdihumboldt.hale.common.schema.model.SchemaSpace) WFSOperation(eu.esdihumboldt.hale.io.wfs.capabilities.WFSOperation) AbstractWFSCapabilitiesPage(eu.esdihumboldt.hale.io.wfs.ui.capabilities.AbstractWFSCapabilitiesPage) WFSCapabilities(eu.esdihumboldt.hale.io.wfs.capabilities.WFSCapabilities) URL(java.net.URL) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) XmlElements(eu.esdihumboldt.hale.io.xsd.constraint.XmlElements) SchemaService(eu.esdihumboldt.hale.ui.service.schema.SchemaService) Collection(java.util.Collection) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement)

Example 4 with XmlElements

use of eu.esdihumboldt.hale.io.xsd.constraint.XmlElements in project hale by halestudio.

the class GmlInstanceCollectionTest method testWVAInstances.

private void testWVAInstances(InstanceCollection instances) {
    String ns = "http://www.esdi-humboldt.org/waterVA";
    String gmlNs = "http://www.opengis.net/gml";
    ResourceIterator<Instance> it = instances.iterator();
    try {
        assertTrue(it.hasNext());
        Instance instance = it.next();
        assertNotNull(instance);
        // check type and element
        TypeDefinition type = instance.getDefinition();
        assertEquals(new QName(ns, "Watercourses_VA_Type"), type.getName());
        XmlElements elements = type.getConstraint(XmlElements.class);
        Collection<? extends XmlElement> elementCollection = elements.getElements();
        assertEquals(1, elementCollection.size());
        XmlElement element = elementCollection.iterator().next();
        assertEquals(new QName(ns, "Watercourses_VA"), element.getName());
        // check instance
        // check a simple property first (FGW_ID)
        Object[] fgwID = instance.getProperty(new QName(ns, "FGW_ID"));
        assertNotNull(fgwID);
        assertEquals(1, fgwID.length);
        assertEquals("81011403", fgwID[0]);
        // the_geom
        Object[] the_geom = instance.getProperty(new QName(ns, "the_geom"));
        assertNotNull(the_geom);
        assertEquals(1, the_geom.length);
        assertTrue(the_geom[0] instanceof Instance);
        // MultiLineString
        Object[] multiLineString = ((Instance) the_geom[0]).getProperty(new QName(gmlNs, "MultiLineString"));
        assertNotNull(multiLineString);
        assertEquals(1, multiLineString.length);
        assertTrue(multiLineString[0] instanceof Instance);
        // TODO the MultiLineString should have a GeometryProperty value
        // with a MultiLineString as geometry and a CRS definition
        // ...getValue()
        // srsName
        Object[] srsName = ((Instance) multiLineString[0]).getProperty(new QName("srsName"));
        assertNotNull(srsName);
        assertEquals(1, srsName.length);
        assertEquals("EPSG:31251", srsName[0].toString());
        // lineStringMember
        Object[] lineStringMember = ((Instance) multiLineString[0]).getProperty(new QName(gmlNs, "lineStringMember"));
        assertNotNull(lineStringMember);
        assertEquals(1, lineStringMember.length);
        assertTrue(lineStringMember[0] instanceof Instance);
        // LineString
        Object[] lineString = ((Instance) lineStringMember[0]).getProperty(new QName(gmlNs, "LineString"));
        assertNotNull(lineString);
        assertEquals(1, lineString.length);
        assertTrue(lineString[0] instanceof Instance);
        // TODO the LineString should have a GeometryProperty value with a
        // LineString as geometry and a CRS definition
        // ...getValue()
        // choice
        Object[] choice_1 = ((Instance) lineString[0]).getProperty(new QName(gmlNs + "/LineStringType", "choice_1"));
        assertNotNull(choice_1);
        assertEquals(1, choice_1.length);
        assertTrue(choice_1[0] instanceof Group);
        // coordinates
        Object[] coordinates = ((Group) choice_1[0]).getProperty(new QName(gmlNs, "coordinates"));
        assertNotNull(coordinates);
        assertEquals(1, coordinates.length);
        assertTrue(coordinates[0] instanceof Instance);
        assertTrue(((Instance) coordinates[0]).getValue().toString().contains("-39799.68820381"));
        // only one instance should be present
        assertFalse(it.hasNext());
    } finally {
        it.close();
    }
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) XmlElements(eu.esdihumboldt.hale.io.xsd.constraint.XmlElements) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 5 with XmlElements

use of eu.esdihumboldt.hale.io.xsd.constraint.XmlElements in project hale by halestudio.

the class OmlReader method findElementType.

private QName findElementType(TypeIndex schema, QName elementName) {
    if (schema instanceof SchemaSpace) {
        SchemaSpace ss = (SchemaSpace) schema;
        for (Schema schem : ss.getSchemas()) {
            if (schem instanceof XmlIndex) {
                XmlElement xmlelem = ((XmlIndex) schem).getElements().get(elementName);
                if (xmlelem != null) {
                    return xmlelem.getType().getName();
                }
                // if there is no element try to find one with an extra "/"
                // sign in the namespace because in earlier version this
                // case can occur
                xmlelem = ((XmlIndex) schem).getElements().get(new QName(elementName.getNamespaceURI() + "/", elementName.getLocalPart()));
                if (xmlelem != null) {
                    return xmlelem.getType().getName();
                }
            }
        }
    } else {
        for (TypeDefinition typedef : schema.getTypes()) {
            XmlElements xmlelem = typedef.getConstraint(XmlElements.class);
            for (XmlElement elem : xmlelem.getElements()) {
                if (elem.getName().equals(elementName) || elem.getName().equals(new QName(elementName.getNamespaceURI() + "/", elementName.getLocalPart()))) {
                    return typedef.getName();
                }
            }
        }
    }
    return elementName;
}
Also used : XmlElements(eu.esdihumboldt.hale.io.xsd.constraint.XmlElements) SchemaSpace(eu.esdihumboldt.hale.common.schema.model.SchemaSpace) QName(javax.xml.namespace.QName) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

XmlElements (eu.esdihumboldt.hale.io.xsd.constraint.XmlElements)9 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)9 QName (javax.xml.namespace.QName)7 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)5 HashSet (java.util.HashSet)3 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)2 SchemaSpace (eu.esdihumboldt.hale.common.schema.model.SchemaSpace)2 XmlAttribute (eu.esdihumboldt.hale.io.xsd.model.XmlAttribute)2 XmlAttributeGroup (eu.esdihumboldt.hale.io.xsd.model.XmlAttributeGroup)2 XmlGroup (eu.esdihumboldt.hale.io.xsd.model.XmlGroup)2 XmlIndex (eu.esdihumboldt.hale.io.xsd.model.XmlIndex)2 XmlTypeDefinition (eu.esdihumboldt.hale.io.xsd.reader.internal.XmlTypeDefinition)2 ElementName (eu.esdihumboldt.hale.io.xsd.reader.internal.constraint.ElementName)2 XmlSchema (org.apache.ws.commons.schema.XmlSchema)2 XmlSchemaAttribute (org.apache.ws.commons.schema.XmlSchemaAttribute)2 XmlSchemaAttributeGroup (org.apache.ws.commons.schema.XmlSchemaAttributeGroup)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