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;
}
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);
}
}
}
}
}
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();
}
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());
}
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);
}
}
Aggregations