Search in sources :

Example 1 with XmlSchemaReader

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

the class WFSDescribeFeatureSource method updateConfiguration.

@Override
public boolean updateConfiguration(ImportProvider provider) {
    boolean success = super.updateConfiguration(provider);
    if (success && provider instanceof XmlSchemaReader) {
        // analyze URI and determine relevant elements
        URI loc = provider.getSource().getLocation();
        if (loc != null) {
            URIBuilder b = new URIBuilder(loc);
            String namespace = null;
            String typename = null;
            for (NameValuePair param : b.getQueryParams()) {
                switch(param.getName().toLowerCase()) {
                    case "namespace":
                    case "namespaces":
                        namespace = param.getValue();
                        break;
                    case "typename":
                    case "typenames":
                        typename = param.getValue();
                        break;
                }
            }
            if (typename != null && !typename.isEmpty()) {
                // parse namespaces
                Map<String, String> prefixToNamespace = new HashMap<>();
                if (namespace != null && !namespace.isEmpty()) {
                    Pattern ex = Pattern.compile("xmlns\\((([\\w\\d_\\.\\-]+)(=|,))?([^)]+)\\)");
                    Matcher matcher = ex.matcher(namespace);
                    while (matcher.find()) {
                        String prefix = matcher.group(2);
                        if (prefix == null) {
                            prefix = XMLConstants.DEFAULT_NS_PREFIX;
                        }
                        String ns = matcher.group(4);
                        prefixToNamespace.put(prefix, ns);
                    }
                // previously used implementation below does not support
                // comma separator inside xmlns(...)
                // for (String xmlns : Splitter.on(',').omitEmptyStrings().trimResults()
                // .split(namespace)) {
                // if (xmlns.startsWith("xmlns(") && xmlns.endsWith(")")) {
                // String mapping = xmlns.substring("xmlns(".length(),
                // xmlns.length() - 1);
                // List<String> mp = Splitter.on('=').limit(2).trimResults()
                // .splitToList(mapping);
                // if (mp.size() == 2) {
                // prefixToNamespace.put(mp.get(0), mp.get(1));
                // }
                // else {
                // // mapping for default namespace
                // prefixToNamespace.put(XMLConstants.DEFAULT_NS_PREFIX, mp.get(0));
                // }
                // }
                // }
                }
                Set<QName> elements = new HashSet<>();
                // parse type names
                for (String type : Splitter.on(',').omitEmptyStrings().trimResults().split(typename)) {
                    List<String> nameParts = Splitter.on(':').limit(2).trimResults().splitToList(type);
                    QName name;
                    if (nameParts.size() == 2) {
                        // prefix and name
                        String prefix = nameParts.get(0);
                        String ns = prefixToNamespace.get(prefix);
                        if (ns != null) {
                            name = new QName(ns, nameParts.get(1), prefix);
                        } else {
                            // namespace unknown - fall back to local name
                            // only
                            name = new QName(nameParts.get(1));
                        }
                    } else {
                        // only local name
                        name = new QName(nameParts.get(0));
                    }
                    elements.add(name);
                }
                ((XmlSchemaReader) provider).setRelevantElements(elements);
            }
        }
    }
    return success;
}
Also used : NameValuePair(org.apache.http.NameValuePair) Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) QName(javax.xml.namespace.QName) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder) XmlSchemaReader(eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader) HashSet(java.util.HashSet)

Example 2 with XmlSchemaReader

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

the class GmlInstanceCollectionTest method loadInstances.

private GmlInstanceCollection loadInstances(URI schemaLocation, URI xmlLocation, boolean restrictToFeatures, boolean ignoreNamespace) throws IOException, IOProviderConfigurationException {
    SchemaReader reader = new XmlSchemaReader();
    reader.setSharedTypes(null);
    reader.setSource(new DefaultInputSupplier(schemaLocation));
    IOReport schemaReport = reader.execute(null);
    assertTrue(schemaReport.isSuccess());
    Schema sourceSchema = reader.getSchema();
    return new GmlInstanceCollection(new DefaultInputSupplier(xmlLocation), sourceSchema, restrictToFeatures, false, true, ignoreNamespace, null, reader);
}
Also used : SchemaReader(eu.esdihumboldt.hale.common.schema.io.SchemaReader) XmlSchemaReader(eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader) XmlSchemaReader(eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport)

Example 3 with XmlSchemaReader

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

the class OMLReaderTest method readXMLSchema.

/**
 * Reads a XML schema
 *
 * @param input the input supplier
 * @return the schema
 * @throws IOProviderConfigurationException if the configuration of the
 *             reader is invalid
 * @throws IOException if reading the schema fails
 */
private static Schema readXMLSchema(LocatableInputSupplier<? extends InputStream> input) throws IOProviderConfigurationException, IOException {
    XmlSchemaReader reader = new XmlSchemaReader();
    reader.setSharedTypes(new DefaultTypeIndex());
    reader.setSource(input);
    reader.validate();
    IOReport report = reader.execute(null);
    assertTrue(report.isSuccess());
    assertTrue("Errors are contained in the report", report.getErrors().isEmpty());
    return reader.getSchema();
}
Also used : DefaultTypeIndex(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeIndex) XmlSchemaReader(eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport)

Example 4 with XmlSchemaReader

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

the class AbstractHandlerTest method loadXMLInstances.

/**
 * Load an instance collection from a GML file.
 *
 * @param schemaLocation the GML application schema location
 * @param xmlLocation the GML file location
 * @param interpolConfig the interpolation configuration
 * @return the instance collection
 * @throws IOException if reading schema or instances failed
 * @throws IOProviderConfigurationException if the I/O providers were not
 *             configured correctly
 */
public static InstanceCollection loadXMLInstances(URI schemaLocation, URI xmlLocation, @Nullable ReaderConfiguration interpolConfig) throws IOException, IOProviderConfigurationException {
    SchemaReader reader = new XmlSchemaReader();
    reader.setSharedTypes(null);
    reader.setSource(new DefaultInputSupplier(schemaLocation));
    IOReport schemaReport = reader.execute(null);
    assertTrue(schemaReport.isSuccess());
    Schema sourceSchema = reader.getSchema();
    InstanceReader instanceReader = new GmlInstanceReader();
    instanceReader.setSource(new DefaultInputSupplier(xmlLocation));
    instanceReader.setSourceSchema(sourceSchema);
    if (interpolConfig != null) {
        interpolConfig.apply(instanceReader);
    }
    IOReport instanceReport = instanceReader.execute(null);
    assertTrue(instanceReport.isSuccess());
    return instanceReader.getInstances();
}
Also used : InstanceReader(eu.esdihumboldt.hale.common.instance.io.InstanceReader) GmlInstanceReader(eu.esdihumboldt.hale.io.gml.reader.internal.GmlInstanceReader) XmlSchemaReader(eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader) SchemaReader(eu.esdihumboldt.hale.common.schema.io.SchemaReader) XmlSchemaReader(eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) GmlInstanceReader(eu.esdihumboldt.hale.io.gml.reader.internal.GmlInstanceReader)

Example 5 with XmlSchemaReader

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

the class FilterTest method loadXML.

@Before
public void loadXML() throws Exception {
    if (init == false) {
        SchemaReader reader = new XmlSchemaReader();
        reader.setSharedTypes(null);
        reader.setSource(new DefaultInputSupplier((getClass().getResource("/testdata/inspire3/HydroPhysicalWaters.xsd").toURI())));
        IOReport report = reader.execute(null);
        assertTrue(report.isSuccess());
        Schema schema = reader.getSchema();
        StreamGmlReader instanceReader = new GmlInstanceReader();
        instanceReader.setSource(new DefaultInputSupplier(getClass().getResource("/testdata/out/transformWrite_ERM_HPW.gml").toURI()));
        instanceReader.setSourceSchema(schema);
        instanceReader.validate();
        report = instanceReader.execute(null);
        assertTrue(report.isSuccess());
        FilterTest.complexinstances = instanceReader.getInstances();
        assertFalse(FilterTest.complexinstances.isEmpty());
        init = true;
    }
}
Also used : XmlSchemaReader(eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader) ShapeSchemaReader(eu.esdihumboldt.hale.io.shp.reader.internal.ShapeSchemaReader) SchemaReader(eu.esdihumboldt.hale.common.schema.io.SchemaReader) XmlSchemaReader(eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) StreamGmlReader(eu.esdihumboldt.hale.io.gml.reader.internal.StreamGmlReader) GmlInstanceReader(eu.esdihumboldt.hale.io.gml.reader.internal.GmlInstanceReader) Before(org.junit.Before)

Aggregations

XmlSchemaReader (eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader)13 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)11 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)10 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)8 SchemaReader (eu.esdihumboldt.hale.common.schema.io.SchemaReader)7 GmlInstanceReader (eu.esdihumboldt.hale.io.gml.reader.internal.GmlInstanceReader)6 InstanceReader (eu.esdihumboldt.hale.common.instance.io.InstanceReader)4 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)2 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)2 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)2 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)2 DefaultSchemaSpace (eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchemaSpace)2 DefaultTypeIndex (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeIndex)2 StreamGmlReader (eu.esdihumboldt.hale.io.gml.reader.internal.StreamGmlReader)2 XmlInstanceReader (eu.esdihumboldt.hale.io.gml.reader.internal.XmlInstanceReader)2 ShapeSchemaReader (eu.esdihumboldt.hale.io.shp.reader.internal.ShapeSchemaReader)2 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)2 XmlIndex (eu.esdihumboldt.hale.io.xsd.model.XmlIndex)2 Geometry (com.vividsolutions.jts.geom.Geometry)1 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)1