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