Search in sources :

Example 61 with DefaultInputSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier in project hale by halestudio.

the class XMLPathUpdater method update.

/**
 * Actual implementation of the update method.
 *
 * @param xmlResource the XML resource file that gets updated
 * @param oldPath its original location
 * @param locationXPath a XPath expression to find nodes that should be
 *            processed
 * @param includeWebResources whether web resources should be copied and
 *            updates, too
 * @param reporter the reporter of the current IO process where errors
 *            should be reported to
 * @param updates a map of already copied files which is used and gets
 *            filled by this method. Needed for multiple updates on the same
 *            file.
 * @throws IOException if an IO exception occurs
 */
private static void update(File xmlResource, URI oldPath, String locationXPath, boolean includeWebResources, IOReporter reporter, Map<URI, File> updates) throws IOException {
    // every XML resource should be updated (and copied) only once
    // so we save the currently adapted resource in a map
    updates.put(oldPath, xmlResource);
    // counter for the directory because every resource should have its own
    // directory
    int count = 0;
    DocumentBuilder builder = null;
    try {
        builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new IOException("Can not create a DocumentBuilder", e);
    }
    builder.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            // FIXME some documentation would be nice why this is OK here?!
            return new InputSource(new StringReader(""));
        }
    });
    Document doc = null;
    try {
        doc = builder.parse(xmlResource);
    } catch (SAXException e1) {
        // if the file is no XML file simply stop the recursion
        return;
    }
    // find schemaLocation of imports/includes via XPath
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nodelist = null;
    try {
        nodelist = ((NodeList) xpath.evaluate(locationXPath, doc, XPathConstants.NODESET));
    } catch (XPathExpressionException e) {
        throw new IOException("The XPath expression is wrong", e);
    }
    // iterate over all imports or includes and get the schemaLocations
    for (int i = 0; i < nodelist.getLength(); i++) {
        Node locationNode = nodelist.item(i);
        String location = locationNode.getNodeValue();
        URI locationUri = null;
        try {
            locationUri = new URI(location);
        } catch (Exception e1) {
            reporter.error(new IOMessageImpl("The location is no valid URI.", e1));
            continue;
        }
        if (!locationUri.isAbsolute()) {
            locationUri = oldPath.resolve(locationUri);
        }
        String scheme = locationUri.getScheme();
        InputStream input = null;
        if (scheme != null) {
            // should the resource be included?
            if (includeWebResources || !(scheme.equals("http") || scheme.equals("https"))) {
                DefaultInputSupplier supplier = new DefaultInputSupplier(locationUri);
                input = supplier.getInput();
            } else
                continue;
        } else {
            // file is invalid - at least report that
            reporter.error(new IOMessageImpl("Skipped resource because it cannot be loaded from " + locationUri.toString(), null));
            continue;
        }
        // every file needs its own directory because of name conflicts
        String filename = location;
        if (location.contains("/"))
            filename = location.substring(location.lastIndexOf("/") + 1);
        filename = count + "/" + filename;
        File includednewFile = null;
        if (updates.containsKey(locationUri)) {
            // if the current XML schema is already updated we have to
            // find the relative path to this resource
            URI relative = IOUtils.getRelativePath(updates.get(locationUri).toURI(), xmlResource.toURI());
            locationNode.setNodeValue(relative.toString());
        } else if (input != null) {
            // we need the directory of the file
            File xmlResourceDir = xmlResource.getParentFile();
            // path where the file should be copied to
            includednewFile = new File(xmlResourceDir, filename);
            try {
                includednewFile.getParentFile().mkdirs();
            } catch (SecurityException e) {
                throw new IOException("Can not create directories " + includednewFile.getParent(), e);
            }
            // copy to new directory
            OutputStream output = new FileOutputStream(includednewFile);
            ByteStreams.copy(input, output);
            output.close();
            input.close();
            // set new location in the XML resource
            locationNode.setNodeValue(filename);
            update(includednewFile, locationUri, locationXPath, includeWebResources, reporter, updates);
            count++;
        }
        // write new XML-File
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = null;
        try {
            transformer = transformerFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            log.debug("Can not create transformer for creating XMl file", e);
            return;
        }
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(xmlResource);
        try {
            transformer.transform(source, result);
        } catch (TransformerException e) {
            log.debug("Cannot create new XML file", e);
            return;
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) Document(org.w3c.dom.Document) URI(java.net.URI) SAXException(org.xml.sax.SAXException) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException) XPath(javax.xml.xpath.XPath) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) EntityResolver(org.xml.sax.EntityResolver) XPathExpressionException(javax.xml.xpath.XPathExpressionException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 62 with DefaultInputSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier in project hale by halestudio.

the class FilterTest method simpleSchemaTestECQL.

@Test
public void simpleSchemaTestECQL() throws Exception {
    ShapeSchemaReader schemaReader = new ShapeSchemaReader();
    schemaReader.setSource(new DefaultInputSupplier(getClass().getResource("/testdata/GN_Point/GN_Point.shp").toURI()));
    schemaReader.validate();
    IOReport report = schemaReader.execute(null);
    assertTrue(report.isSuccess());
    Schema schema = schemaReader.getSchema();
    ShapeInstanceReader instanceReader = new ShapeInstanceReader();
    instanceReader.setSource(new DefaultInputSupplier(getClass().getResource("/testdata/GN_Point/GN_Point.shp").toURI()));
    instanceReader.setSourceSchema(schema);
    instanceReader.validate();
    report = instanceReader.execute(null);
    assertTrue(report.isSuccess());
    InstanceCollection instances = instanceReader.getInstances();
    assertFalse(instances.isEmpty());
    ResourceIterator<Instance> ri = instances.iterator();
    try {
        boolean foundIt = false;
        boolean stayFalse = false;
        boolean stayFalseToo = false;
        Filter cqlfilter = new FilterGeoECqlImpl("NEV = 'Piritulus'");
        Filter foulfilter = new FilterGeoECqlImpl("HERP = 'DERP'");
        Filter foulfilter1 = new FilterGeoECqlImpl("NEV = 'HURR'");
        while (ri.hasNext()) {
            Instance inst = ri.next();
            assertNotNull(inst);
            if (cqlfilter.match(inst)) {
                foundIt = true;
            }
            if (foulfilter.match(inst)) {
                stayFalse = true;
            }
            if (foulfilter1.match(inst)) {
                stayFalseToo = true;
            }
        }
        assertTrue(foundIt);
        assertFalse(stayFalse);
        assertFalse(stayFalseToo);
    } finally {
        ri.close();
    }
}
Also used : ShapeInstanceReader(eu.esdihumboldt.hale.io.shp.reader.internal.ShapeInstanceReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) ShapeSchemaReader(eu.esdihumboldt.hale.io.shp.reader.internal.ShapeSchemaReader) Test(org.junit.Test)

Example 63 with DefaultInputSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier in project hale by halestudio.

the class FilterTest method loadXMLInstances.

private InstanceCollection loadXMLInstances(URI schemaLocation, URI xmlLocation) 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 XmlInstanceReader();
    instanceReader.setSource(new DefaultInputSupplier(xmlLocation));
    instanceReader.setSourceSchema(sourceSchema);
    IOReport instanceReport = instanceReader.execute(null);
    assertTrue(instanceReport.isSuccess());
    return instanceReader.getInstances();
}
Also used : ShapeInstanceReader(eu.esdihumboldt.hale.io.shp.reader.internal.ShapeInstanceReader) InstanceReader(eu.esdihumboldt.hale.common.instance.io.InstanceReader) GmlInstanceReader(eu.esdihumboldt.hale.io.gml.reader.internal.GmlInstanceReader) XmlInstanceReader(eu.esdihumboldt.hale.io.gml.reader.internal.XmlInstanceReader) 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) XmlInstanceReader(eu.esdihumboldt.hale.io.gml.reader.internal.XmlInstanceReader) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport)

Example 64 with DefaultInputSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier in project hale by halestudio.

the class ExecTransformation method loadProject.

private void loadProject() throws IOException {
    status("Loading hale project...");
    env = new ProjectTransformationEnvironment(id, new DefaultInputSupplier(context.getProject()), reportHandler);
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) ProjectTransformationEnvironment(eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment)

Example 65 with DefaultInputSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier in project hale by halestudio.

the class ExecTransformation method setupValidators.

private void setupValidators() {
    if (context.getValidateProviderIds() != null && !context.getValidateProviderIds().isEmpty()) {
        for (int i = 0; i < context.getValidateProviderIds().size(); i++) {
            String validateProviderId = context.getValidateProviderIds().get(i);
            if (!validateProviderId.trim().isEmpty()) {
                final InstanceValidator validator = HaleIO.createIOProvider(InstanceValidator.class, null, validateProviderId);
                if (validator == null) {
                    throw fail("Instance validator with ID " + validateProviderId + " not found");
                }
                // load validator settings
                validator.loadConfiguration(context.getValidateSettings().get(i));
                // set schemas
                List<? extends Locatable> schemas = target.getValidationSchemas();
                validator.setSchemas(schemas.toArray(new Locatable[schemas.size()]));
                // set source
                validator.setSource(new DefaultInputSupplier(context.getTarget()));
                // apply target content type
                validator.setContentType(target.getContentType());
                this.validators.add(validator);
            }
        }
    }
}
Also used : InstanceValidator(eu.esdihumboldt.hale.common.instance.io.InstanceValidator) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) Locatable(eu.esdihumboldt.hale.common.core.io.supplier.Locatable)

Aggregations

DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)73 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)34 URI (java.net.URI)30 Test (org.junit.Test)21 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)15 XmlIndex (eu.esdihumboldt.hale.io.xsd.model.XmlIndex)15 IOException (java.io.IOException)13 LogProgressIndicator (eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator)11 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)11 SchemaReader (eu.esdihumboldt.hale.common.schema.io.SchemaReader)10 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)10 XmlSchemaReader (eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader)10 QName (javax.xml.namespace.QName)10 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)9 InstanceReader (eu.esdihumboldt.hale.common.instance.io.InstanceReader)7 CSVSchemaReader (eu.esdihumboldt.hale.io.csv.reader.internal.CSVSchemaReader)7 GmlInstanceReader (eu.esdihumboldt.hale.io.gml.reader.internal.GmlInstanceReader)7 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)6 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)6 File (java.io.File)6