use of eu.esdihumboldt.util.io.InputSupplier in project hale by halestudio.
the class BundleResolver method resolve.
/**
* @see ResourceResolver#resolve(URI)
*/
@Override
public InputSupplier<? extends InputStream> resolve(URI uri) throws ResourceNotFoundException {
if (bundle != null) {
// OSGi
final URL entry = bundle.getEntry(uri.getPath());
if (entry == null) {
throw new ResourceNotFoundException("Resource with path " + uri.getPath() + " not contained in bundle " + bundle.getSymbolicName());
}
preventDirectoryMatch(uri, entry);
return new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return entry.openStream();
}
};
} else {
// no OSGi
// ClassLoader.getSystemClassLoader();
final ClassLoader loader = getClass().getClassLoader();
String pathCandidate = uri.getPath();
final String path = (pathCandidate != null && pathCandidate.startsWith("/")) ? (pathCandidate.substring(1)) : (pathCandidate);
Enumeration<URL> resources;
try {
resources = loader.getResources(path);
} catch (IOException e) {
log.error("Error accessing classpath resource", e);
throw new ResourceNotFoundException(e);
}
if (resources.hasMoreElements()) {
URL entry = resources.nextElement();
// XXX not sure if this is needed here
preventDirectoryMatch(uri, entry);
return new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return loader.getResourceAsStream(path);
}
};
} else {
throw new ResourceNotFoundException();
}
}
}
use of eu.esdihumboldt.util.io.InputSupplier in project hale by halestudio.
the class INSPIRECodeListReader method execute.
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
progress.begin("Loading code list.", ProgressIndicator.UNKNOWN);
try {
Document doc;
URI loc = getSource().getLocation();
if (loc != null && (loc.getScheme().equals("http") || loc.getScheme().equals("https"))) {
// and provide headers to retrieve correct format and language
try {
doc = loadXmlDocument(loc);
} catch (Exception e) {
// try local resources as fall-back
InputSupplier<? extends InputStream> localInput = Resources.tryResolve(loc, Resources.RESOURCE_TYPE_XML_CODELIST);
if (localInput != null) {
try (InputStream is = localInput.getInput()) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(is);
}
} else
throw e;
}
} else {
// just access stream
try (InputStream is = getSource().getInput()) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(is);
}
}
reporter.setSuccess(parse(doc, loc, reporter));
} catch (Exception e) {
throw new RuntimeException(e);
}
progress.setCurrentTask("Code list loaded.");
return reporter;
}
Aggregations