Search in sources :

Example 1 with DefaultInputSupplier

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

the class SchematronInstanceValidator method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    URI schematronLoc = getSchematronLocation();
    if (schematronLoc == null) {
        throw new IOProviderConfigurationException("Providing a schematron file is required");
    }
    progress.begin("Performing Schematron validation", ProgressIndicator.UNKNOWN);
    final InputStream sourceInput = this.getSource().getInput();
    if (sourceInput == null) {
        throw new RuntimeException("No input for Schematron validator");
    }
    final Source xmlSource = new StreamSource(sourceInput);
    final DefaultInputSupplier schematronInputSupplier = new DefaultInputSupplier(schematronLoc);
    final InputStream schematronInput = schematronInputSupplier.getInput();
    if (schematronInput == null) {
        throw new RuntimeException("No rules input for Schematron validator");
    }
    final Source schematronSource = new StreamSource(schematronInput);
    try {
        final SchematronValidator validator = new SchematronValidator(schematronSource);
        final Result result = validator.validate(xmlSource, /* svrlReport */
        true);
        final StringWriter reportWriter = new StringWriter();
        SchematronUtils.convertValidatorResult(result, reportWriter);
        reporter.setSuccess(!validator.ruleViolationsDetected());
        if (validator.ruleViolationsDetected()) {
            SchematronReportParser parser = new SchematronReportParser(reportWriter.toString());
            parser.reportFailedAssertions(reporter);
        }
    } catch (Exception e) {
        reporter.error(new IOMessageImpl("Error running schematron validation", e));
        reporter.setSuccess(false);
    } finally {
        schematronInput.close();
        progress.end();
    }
    return reporter;
}
Also used : SchematronReportParser(eu.esdihumboldt.hale.io.schematron.util.SchematronReportParser) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) URI(java.net.URI) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) Result(javax.xml.transform.Result) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) StringWriter(java.io.StringWriter) SchematronValidator(org.opengis.cite.validation.SchematronValidator)

Example 2 with DefaultInputSupplier

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

the class ShapefileAdvisor method copyResource.

@Override
public void copyResource(LocatableInputSupplier<? extends InputStream> resource, final Path target, IContentType resourceType, boolean includeRemote, IOReporter reporter) throws IOException {
    URI orgUri = resource.getLocation();
    if (orgUri == null) {
        throw new IOException("URI for original resource must be known");
    }
    // copy if files can be resolved as a Path
    Path orgPath = null;
    try {
        orgPath = Paths.get(orgUri);
    } catch (Exception e) {
    // ignore
    }
    if (orgPath != null) {
        // determine the filename w/o extension
        String filemain = orgPath.getFileName().toString();
        int extPos = filemain.lastIndexOf('.');
        if (extPos > 0) {
            filemain = filemain.substring(0, extPos);
        }
        // matcher for associated files
        final PathMatcher auxfiles = orgPath.getFileSystem().getPathMatcher("glob:" + filemain + ".???");
        // find all associated files
        Path orgDir = orgPath.getParent();
        try (DirectoryStream<Path> files = Files.newDirectoryStream(orgDir, new DirectoryStream.Filter<Path>() {

            @Override
            public boolean accept(Path entry) throws IOException {
                return auxfiles.matches(entry.getFileName());
            }
        })) {
            // copy the files
            for (Path orgFile : files) {
                Path targetFile = target.resolveSibling(orgFile.getFileName());
                Files.copy(orgFile, targetFile);
            }
        }
    } else {
        // copy the main file
        try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(target));
            InputStream in = new DefaultInputSupplier(orgUri).getInput()) {
            ByteStreams.copy(in, out);
        }
        // determine base URI w/o dot and extension
        String base = orgUri.toASCIIString();
        int extPos = base.lastIndexOf('.');
        if (extPos > 0) {
            base = base.substring(0, extPos);
        }
        // determine file base name w/o dot and extension
        String filemain = target.getFileName().toString();
        extPos = filemain.lastIndexOf('.');
        if (extPos > 0) {
            filemain = filemain.substring(0, extPos);
        }
        for (ShpFileType type : ShpFileType.values()) {
            if (!type.equals(ShpFileType.SHP)) {
                try {
                    URI source = URI.create(base + type.extensionWithPeriod);
                    if (HaleIO.testStream(source, true)) {
                        Path targetFile = target.resolveSibling(filemain + type.extensionWithPeriod);
                        // copy the auxiliary file
                        try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(targetFile));
                            InputStream in = new DefaultInputSupplier(source).getInput()) {
                            ByteStreams.copy(in, out);
                        }
                    }
                } catch (Exception e) {
                    log.debug("Failed to copy auxiliary file for Shapefile", e);
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) DirectoryStream(java.nio.file.DirectoryStream) IOException(java.io.IOException) URI(java.net.URI) IOException(java.io.IOException) PathMatcher(java.nio.file.PathMatcher) BufferedOutputStream(java.io.BufferedOutputStream) ShpFileType(org.geotools.data.shapefile.files.ShpFileType)

Example 3 with DefaultInputSupplier

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

the class XLSReaderTest method readXLSSchema.

private Schema readXLSSchema(String sourceLocation, int sheetIndex, String typeName, String paramPropertyType) throws Exception {
    XLSSchemaReader schemaReader = new XLSSchemaReader();
    schemaReader.setSource(new DefaultInputSupplier(getClass().getResource(sourceLocation).toURI()));
    schemaReader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(sheetIndex));
    schemaReader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of(typeName));
    schemaReader.setParameter(AbstractTableSchemaReader.PARAM_PROPERTYTYPE, Value.of(paramPropertyType));
    IOReport report = schemaReader.execute(null);
    assertTrue("Schema import was not successfull.", report.isSuccess());
    return schemaReader.getSchema();
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) XLSSchemaReader(eu.esdihumboldt.hale.io.xls.reader.XLSSchemaReader) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport)

Example 4 with DefaultInputSupplier

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

the class XLSReaderTest method testReadEmptySheet.

/**
 * Test - Check empty table
 *
 * @throws Exception , if an error occurs
 */
@Test
public void testReadEmptySheet() throws Exception {
    int sheetIndex = 0;
    XLSSchemaReader schemaReader = new XLSSchemaReader();
    schemaReader.setSource(new DefaultInputSupplier(getClass().getResource("/data/emptyAndNormalSheet.xls").toURI()));
    schemaReader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(sheetIndex));
    schemaReader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of(typeName));
    // Try read the empty sheet
    IOReport report = schemaReader.execute(null);
    assertFalse(report.isSuccess());
    // Read the correct sheet
    sheetIndex = 1;
    schemaReader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(sheetIndex));
    report = schemaReader.execute(null);
    assertTrue(report.isSuccess());
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) XLSSchemaReader(eu.esdihumboldt.hale.io.xls.reader.XLSSchemaReader) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) Test(org.junit.Test)

Example 5 with DefaultInputSupplier

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

the class XMLApiValidator method validate.

/**
 * @see Validator#validate(InputStream)
 */
@Override
public Report validate(InputStream xml) {
    javax.xml.validation.Schema validateSchema;
    try {
        URI mainUri = null;
        Source[] sources = new Source[schemaLocations.length];
        for (int i = 0; i < this.schemaLocations.length; i++) {
            URI schemaLocation = this.schemaLocations[i];
            if (mainUri == null) {
                // use first schema location for main URI
                mainUri = schemaLocation;
            }
            // load a WXS schema, represented by a Schema instance
            DefaultInputSupplier dis = new DefaultInputSupplier(schemaLocation);
            sources[i] = new StreamSource(dis.getInput());
        }
        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        factory.setResourceResolver(new SchemaResolver(mainUri));
        validateSchema = factory.newSchema(sources);
    } catch (Exception e) {
        // $NON-NLS-1$
        throw new IllegalStateException("Error parsing schema for XML validation", e);
    }
    // create a Validator instance, which can be used to validate an
    // instance document
    javax.xml.validation.Validator validator = validateSchema.newValidator();
    ReportImpl report = new ReportImpl();
    validator.setErrorHandler(new ReportErrorHandler(report));
    // validate the XML document
    try {
        validator.validate(new StreamSource(xml));
        return report;
    } catch (Exception e) {
        // $NON-NLS-1$
        throw new IllegalStateException("Error validating XML file", e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) StreamSource(javax.xml.transform.stream.StreamSource) URI(java.net.URI) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

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