Search in sources :

Example 16 with XmlIndex

use of eu.esdihumboldt.hale.io.xsd.model.XmlIndex in project hale by halestudio.

the class XmlSchemaReaderTest method testRead_definitive_substitution.

/**
 * Test reading a simple XML schema containing substitution groups.
 *
 * @throws Exception if reading the schema fails
 */
@Test
public void testRead_definitive_substitution() throws Exception {
    URI location = getClass().getResource("/testdata/definitive/substgroups.xsd").toURI();
    LocatableInputSupplier<? extends InputStream> input = new DefaultInputSupplier(location);
    XmlIndex schema = (XmlIndex) readSchema(input);
    // shirt element
    XmlElement shirt = schema.getElements().get(new QName("shirt"));
    assertNotNull(shirt);
    assertEquals(new QName("product"), shirt.getSubstitutionGroup());
// TODO extend
}
Also used : 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) Test(org.junit.Test)

Example 17 with XmlIndex

use of eu.esdihumboldt.hale.io.xsd.model.XmlIndex in project hale by halestudio.

the class XmlSchemaReaderTest method testRead_shiporder_types.

/**
 * Test reading a simple XML schema that uses several custom named types
 *
 * @throws Exception if reading the schema fails
 */
@Test
public void testRead_shiporder_types() throws Exception {
    URI location = getClass().getResource("/testdata/shiporder/shiporder-types.xsd").toURI();
    LocatableInputSupplier<? extends InputStream> input = new DefaultInputSupplier(location);
    XmlIndex schema = (XmlIndex) readSchema(input);
    String ns = "http://www.example.com";
    assertEquals(ns, schema.getNamespace());
    // shiporder element
    Collection<XmlElement> elements = getElementsWithNS(ns, schema.getElements().values());
    assertEquals(1, elements.size());
    XmlElement shiporder = elements.iterator().next();
    testShiporderStructure(shiporder, ns);
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) URI(java.net.URI) Test(org.junit.Test)

Example 18 with XmlIndex

use of eu.esdihumboldt.hale.io.xsd.model.XmlIndex in project hale by halestudio.

the class XmlSchemaReaderTest method testRead_definitive_groups.

/**
 * Test reading a simple XML schema containing groups and group references.
 *
 * @throws Exception if reading the schema fails
 */
@Test
public void testRead_definitive_groups() throws Exception {
    URI location = getClass().getResource("/testdata/definitive/groups.xsd").toURI();
    LocatableInputSupplier<? extends InputStream> input = new DefaultInputSupplier(location);
    XmlIndex schema = (XmlIndex) readSchema(input);
    // ShirtType
    TypeDefinition shirtType = schema.getType(new QName("ShirtType"));
    assertNotNull(shirtType);
    assertEquals(5, shirtType.getChildren().size());
    Iterator<? extends ChildDefinition<?>> it = shirtType.getChildren().iterator();
    // ProductPropertyGroup
    GroupPropertyDefinition prodGroup = it.next().asGroup();
    // cardinality
    Cardinality cc = prodGroup.getConstraint(Cardinality.class);
    assertEquals(0, cc.getMinOccurs());
    assertEquals(1, cc.getMaxOccurs());
    // name
    assertEquals("ProductPropertyGroup", prodGroup.getName().getLocalPart());
    assertEquals(4, prodGroup.getDeclaredChildren().size());
    Iterator<? extends ChildDefinition<?>> itProd = prodGroup.getDeclaredChildren().iterator();
    // not there any more because it is flattened away
    // // DescriptionGroup
    // GroupPropertyDefinition descGroup = itProd.next().asGroup();
    // assertNotNull(descGroup);
    // // cardinality
    // cc = descGroup.getConstraint(Cardinality.class);
    // assertEquals(1, cc.getMinOccurs());
    // assertEquals(1, cc.getMaxOccurs());
    // 
    // assertEquals(2, descGroup.getDeclaredChildren().size());
    // Iterator<? extends ChildDefinition<?>> itDesc = descGroup.getDeclaredChildren().iterator();
    // description
    PropertyDefinition description = itProd.next().asProperty();
    assertNotNull(description);
    assertEquals("description", description.getName().getLocalPart());
    // comment
    PropertyDefinition comment = itProd.next().asProperty();
    assertNotNull(comment);
    assertEquals("comment", comment.getName().getLocalPart());
    // number
    PropertyDefinition number = itProd.next().asProperty();
    assertNotNull(number);
    assertEquals("number", number.getName().getLocalPart());
    // name
    PropertyDefinition name = itProd.next().asProperty();
    assertNotNull(name);
    assertEquals("name", name.getName().getLocalPart());
    // size
    PropertyDefinition size = it.next().asProperty();
    assertNotNull(size);
    assertEquals("size", size.getName().getLocalPart());
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) QName(javax.xml.namespace.QName) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) 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 19 with XmlIndex

use of eu.esdihumboldt.hale.io.xsd.model.XmlIndex in project hale by halestudio.

the class XmlSchemaReaderTest method testRead_definitive_sequencegroup.

/**
 * Test reading a simple XML schema with sequences that have to be grouped.
 *
 * @throws Exception if reading the schema fails
 */
@Test
public void testRead_definitive_sequencegroup() throws Exception {
    URI location = getClass().getResource("/testdata/definitive/sequencegroup.xsd").toURI();
    LocatableInputSupplier<? extends InputStream> input = new DefaultInputSupplier(location);
    XmlIndex schema = (XmlIndex) readSchema(input);
    // ItemsType
    TypeDefinition itemsType = schema.getType(new QName("ItemsType"));
    assertNotNull(itemsType);
    assertEquals(1, itemsType.getChildren().size());
    // sequence group
    GroupPropertyDefinition sequence = itemsType.getChildren().iterator().next().asGroup();
    assertNotNull(sequence);
    // cardinality
    Cardinality cc = sequence.getConstraint(Cardinality.class);
    assertEquals(1, cc.getMinOccurs());
    assertEquals(Cardinality.UNBOUNDED, cc.getMaxOccurs());
    // choice flag (not a choice)
    assertFalse(sequence.getConstraint(ChoiceFlag.class).isEnabled());
    Iterator<? extends ChildDefinition<?>> it = sequence.getDeclaredChildren().iterator();
    // name
    PropertyDefinition name = it.next().asProperty();
    assertNotNull(name);
    assertEquals("name", name.getName().getLocalPart());
    // id
    PropertyDefinition id = it.next().asProperty();
    assertNotNull(id);
    assertEquals("id", id.getName().getLocalPart());
    // choice
    GroupPropertyDefinition choice = it.next().asGroup();
    assertNotNull(choice);
    // cardinality
    cc = choice.getConstraint(Cardinality.class);
    assertEquals(1, cc.getMinOccurs());
    assertEquals(1, cc.getMaxOccurs());
    // choice flag
    assertTrue(choice.getConstraint(ChoiceFlag.class).isEnabled());
    it = choice.getDeclaredChildren().iterator();
    // choice sequence
    GroupPropertyDefinition seqGroup = it.next().asGroup();
    assertNotNull(seqGroup);
    // choice flag (not a choice)
    assertFalse(seqGroup.getConstraint(ChoiceFlag.class).isEnabled());
    // sequence elements
    // one
    PropertyDefinition one = seqGroup.getChild(new QName("one")).asProperty();
    assertNotNull(one);
    // two
    PropertyDefinition two = seqGroup.getChild(new QName("two")).asProperty();
    assertNotNull(two);
    // choice element
    PropertyDefinition single = it.next().asProperty();
    assertNotNull(single);
    assertEquals("single", single.getName().getLocalPart());
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) QName(javax.xml.namespace.QName) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) 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 20 with XmlIndex

use of eu.esdihumboldt.hale.io.xsd.model.XmlIndex in project hale by halestudio.

the class StreamGmlWriterTest method fillFeatureTest.

/**
 * Create a feature, fill it with values, write it as GML, validate the GML
 * and load the GML file again to compare the loaded values with the ones
 * that were written
 *
 * @param elementName the element name of the feature type to use, if
 *            <code>null</code> a random element will be used
 * @param targetSchema the schema to use, the first element will be used for
 *            the type of the feature
 * @param values the values to set on the feature
 * @param testName the name of the test
 * @param srsName the SRS name
 * @param skipValueTest if the check for equality shall be skipped
 * @param expectWriteFail if the GML writing is expected to fail
 * @param windingOrderParam winding order parameter or <code>null</code>
 * @return the validation report or the GML writing report if writing
 *         expected to fail
 * @throws Exception if any error occurs
 */
private IOReport fillFeatureTest(String elementName, URI targetSchema, Map<List<QName>, Object> values, String testName, String srsName, boolean skipValueTest, boolean expectWriteFail, EnumWindingOrderTypes windingOrderParam) throws Exception {
    // load the sample schema
    XmlSchemaReader reader = new XmlSchemaReader();
    reader.setSharedTypes(null);
    reader.setSource(new DefaultInputSupplier(targetSchema));
    IOReport schemaReport = reader.execute(null);
    assertTrue(schemaReport.isSuccess());
    XmlIndex schema = reader.getSchema();
    XmlElement element = null;
    if (elementName == null) {
        element = schema.getElements().values().iterator().next();
        if (element == null) {
            // $NON-NLS-1$
            fail("No element found in the schema");
        }
    } else {
        for (XmlElement candidate : schema.getElements().values()) {
            if (candidate.getName().getLocalPart().equals(elementName)) {
                element = candidate;
                break;
            }
        }
        if (element == null) {
            // $NON-NLS-1$ //$NON-NLS-2$
            fail("Element " + elementName + " not found in the schema");
        }
    }
    if (element == null) {
        throw new IllegalStateException();
    }
    // create feature
    MutableInstance feature = new DefaultInstance(element.getType(), null);
    // set some values
    for (Entry<List<QName>, Object> entry : values.entrySet()) {
        MutableGroup parent = feature;
        List<QName> properties = entry.getKey();
        for (int i = 0; i < properties.size() - 1; i++) {
            QName propertyName = properties.get(i);
            DefinitionGroup def = parent.getDefinition();
            Object[] vals = parent.getProperty(propertyName);
            if (vals != null && vals.length > 0) {
                Object value = vals[0];
                if (value instanceof MutableGroup) {
                    parent = (MutableGroup) value;
                } else {
                    MutableGroup child;
                    ChildDefinition<?> childDef = def.getChild(propertyName);
                    if (childDef.asProperty() != null || value != null) {
                        // create instance
                        child = new DefaultInstance(childDef.asProperty().getPropertyType(), null);
                    } else {
                        // create group
                        child = new DefaultGroup(childDef.asGroup());
                    }
                    if (value != null) {
                        // wrap value
                        ((MutableInstance) child).setValue(value);
                    }
                    parent = child;
                }
            }
        }
        parent.addProperty(properties.get(properties.size() - 1), entry.getValue());
    }
    InstanceCollection instances = new DefaultInstanceCollection(Collections.singleton(feature));
    // write to file
    InstanceWriter writer = new GmlInstanceWriter();
    if (windingOrderParam != null) {
        writer.setParameter(GeoInstanceWriter.PARAM_UNIFY_WINDING_ORDER, Value.of(windingOrderParam));
    }
    writer.setInstances(instances);
    DefaultSchemaSpace schemaSpace = new DefaultSchemaSpace();
    schemaSpace.addSchema(schema);
    writer.setTargetSchema(schemaSpace);
    // $NON-NLS-1$
    File outFile = File.createTempFile(testName, ".gml");
    writer.setTarget(new FileIOSupplier(outFile));
    if (windingOrderParam != null && windingOrderParam == EnumWindingOrderTypes.counterClockwise) {
        assertTrue(writer.getParameter(GeoInstanceWriter.PARAM_UNIFY_WINDING_ORDER).as(EnumWindingOrderTypes.class) == EnumWindingOrderTypes.counterClockwise);
    }
    // new LogProgressIndicator());
    IOReport report = writer.execute(null);
    if (expectWriteFail) {
        assertFalse("Writing the GML output should not be successful", report.isSuccess());
        return report;
    } else {
        assertTrue("Writing the GML output not successful", report.isSuccess());
    }
    List<? extends Locatable> validationSchemas = writer.getValidationSchemas();
    System.out.println(outFile.getAbsolutePath());
    System.out.println(targetSchema.toString());
    // if (!DEL_TEMP_FILES && Desktop.isDesktopSupported()) {
    // Desktop.getDesktop().open(outFile);
    // }
    IOReport valReport = validate(outFile.toURI(), validationSchemas);
    // load file
    InstanceCollection loaded = loadGML(outFile.toURI(), schema);
    ResourceIterator<Instance> it = loaded.iterator();
    try {
        assertTrue(it.hasNext());
        if (!skipValueTest) {
            Instance l = it.next();
            // test values
            for (Entry<List<QName>, Object> entry : values.entrySet()) {
                // XXX conversion?
                Object expected = entry.getValue();
                // String propertyPath = Joiner.on('.').join(Collections2.transform(entry.getKey(), new Function<QName, String>() {
                // 
                // @Override
                // public String apply(QName input) {
                // return input.toString();
                // }
                // }));
                // Collection<Object> propValues = PropertyResolver.getValues(
                // l, propertyPath, true);
                // assertEquals(1, propValues.size());
                // Object value = propValues.iterator().next();
                Collection<GeometryProperty<?>> geoms = GeometryUtil.getAllGeometries(l);
                assertEquals(1, geoms.size());
                Object value = geoms.iterator().next().getGeometry();
                if (expected instanceof Geometry && value instanceof Geometry) {
                    if (windingOrderParam == null || windingOrderParam == EnumWindingOrderTypes.noChanges) {
                        matchGeometries((Geometry) expected, (Geometry) value);
                    }
                    // Winding Order Test.
                    if (windingOrderParam != null) {
                        if (windingOrderParam == EnumWindingOrderTypes.counterClockwise) {
                            assertTrue(((Geometry) expected).getNumGeometries() == ((Geometry) value).getNumGeometries());
                            assertTrue(WindingOrder.isCounterClockwise((Geometry) value));
                        } else if (windingOrderParam == EnumWindingOrderTypes.clockwise) {
                            assertFalse(WindingOrder.isCounterClockwise((Geometry) value));
                        } else {
                            assertTrue(WindingOrder.isCounterClockwise((Geometry) value) == WindingOrder.isCounterClockwise((Geometry) expected));
                        }
                    } else {
                        // TODO check winding order is CCW
                        if (value instanceof Polygon || value instanceof MultiPolygon)
                            assertTrue(WindingOrder.isCounterClockwise((Geometry) value));
                    }
                } else {
                    assertEquals(expected.toString(), value.toString());
                }
            }
            assertFalse(it.hasNext());
        }
    } finally {
        it.close();
    }
    if (DEL_TEMP_FILES) {
        outFile.deleteOnExit();
    }
    return valReport;
}
Also used : GmlInstanceWriter(eu.esdihumboldt.hale.io.gml.writer.GmlInstanceWriter) GeoInstanceWriter(eu.esdihumboldt.hale.common.instance.io.GeoInstanceWriter) InstanceWriter(eu.esdihumboldt.hale.common.instance.io.InstanceWriter) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) XmlSchemaReader(eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) List(java.util.List) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) QName(javax.xml.namespace.QName) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) DefaultGroup(eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup) DefaultSchemaSpace(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchemaSpace) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) MutableGroup(eu.esdihumboldt.hale.common.instance.model.MutableGroup) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) Geometry(com.vividsolutions.jts.geom.Geometry) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) GmlInstanceWriter(eu.esdihumboldt.hale.io.gml.writer.GmlInstanceWriter) File(java.io.File)

Aggregations

XmlIndex (eu.esdihumboldt.hale.io.xsd.model.XmlIndex)24 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)16 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)15 URI (java.net.URI)15 Test (org.junit.Test)13 QName (javax.xml.namespace.QName)11 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)8 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)7 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)6 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)4 SchemaSpace (eu.esdihumboldt.hale.common.schema.model.SchemaSpace)4 Cardinality (eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality)4 IOException (java.io.IOException)4 XMLStreamException (javax.xml.stream.XMLStreamException)3 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)2 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)2 XmlSchemaReader (eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader)2 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 Geometry (com.vividsolutions.jts.geom.Geometry)1