From 3ae0fd52ef7de6eaf8d0f1fc4de9d3371e8771f8 Mon Sep 17 00:00:00 2001 From: Stefan Schroeder <4sschroeder@gmail.com> Date: Thu, 6 Jun 2013 08:39:28 +0200 Subject: [PATCH] change algorithmReader to read inputStreams --- .../basics/io/AlgorithmConfigXmlReader.java | 5 ++-- jsprit-core/src/main/java/util/Resource.java | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/jsprit-core/src/main/java/basics/io/AlgorithmConfigXmlReader.java b/jsprit-core/src/main/java/basics/io/AlgorithmConfigXmlReader.java index fb05ca4b..f749f1dc 100644 --- a/jsprit-core/src/main/java/basics/io/AlgorithmConfigXmlReader.java +++ b/jsprit-core/src/main/java/basics/io/AlgorithmConfigXmlReader.java @@ -21,6 +21,7 @@ package basics.io; import java.io.IOException; +import java.io.InputStream; import java.net.URL; import org.apache.commons.configuration.ConfigurationException; @@ -57,14 +58,14 @@ public class AlgorithmConfigXmlReader { algorithmConfig.getXMLConfiguration().setDelimiterParsingDisabled(true); if(schemaValidation){ - final URL resource = Resource.getAsURL("algorithm_schema.xsd"); + final InputStream resource = Resource.getAsInputStream("algorithm_schema.xsd"); if(resource != null) { EntityResolver resolver = new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { { - InputSource is = new InputSource(resource.getFile()); + InputSource is = new InputSource(resource); return is; } } diff --git a/jsprit-core/src/main/java/util/Resource.java b/jsprit-core/src/main/java/util/Resource.java index 2d3ab1df..09dcd473 100644 --- a/jsprit-core/src/main/java/util/Resource.java +++ b/jsprit-core/src/main/java/util/Resource.java @@ -1,6 +1,9 @@ package util; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; @@ -44,4 +47,24 @@ public class Resource { return url; } + /** + * @param filename relative path from within the resource directory to a file to be loaded + * @return a Stream to the requested resource file, or null if no such file exists. + */ + public final static InputStream getAsInputStream(final String filename) { + // look for the file locally + try { + return new FileInputStream("/" + filename); + } catch (FileNotFoundException e) { + log.info("Resource '" + filename + "' not found locally. May not be fatal."); + // just continue, maybe we have more luck in the classpath + } + // maybe we find the file in the classpath, possibly inside a jar-file + InputStream stream = Resource.class.getResourceAsStream("/" + filename); + if (stream == null) { + log.warn("Resource '" + filename + "' not found!"); + } + return stream; + } + }