From 410062edf469298625f1b042966684f715fcc9eb Mon Sep 17 00:00:00 2001 From: mkwan Date: Tue, 21 Jun 2005 15:08:51 +0000 Subject: [PATCH] Fix a bug in the factory finding algorithm for service files. git-svn-id: https://svn.apache.org/repos/asf/xml/commons/trunk@226248 13f79535-47bb-0310-9956-ffa450edef68 --- .../javax/xml/xpath/XPathFactoryFinder.java | 92 ++++++++++++++----- 1 file changed, 69 insertions(+), 23 deletions(-) diff --git a/java/external/src/javax/xml/xpath/XPathFactoryFinder.java b/java/external/src/javax/xml/xpath/XPathFactoryFinder.java index 0e5bd10..d117d06 100644 --- a/java/external/src/javax/xml/xpath/XPathFactoryFinder.java +++ b/java/external/src/javax/xml/xpath/XPathFactoryFinder.java @@ -17,10 +17,12 @@ package javax.xml.xpath; +import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; @@ -38,8 +40,15 @@ import java.util.Properties; class XPathFactoryFinder { private static SecuritySupport ss = new SecuritySupport() ; + /** debug support code. */ private static boolean debug = false; + + /** + * Default columns per line. + */ + private static final int DEFAULT_LINE_LENGTH = 80; + static { // Use try/catch block to support applets try { @@ -204,8 +213,7 @@ class XPathFactoryFinder { URL resource = (URL)sitr.next(); debugPrintln("looking into " + resource); try { - //sf = loadFromProperty(uri,resource.toExternalForm(),resource.openStream()); - sf = loadFromProperty(uri,resource.toExternalForm(),ss.getURLInputStream(resource)); + sf = loadFromServicesFile(uri, resource.toExternalForm(), ss.getURLInputStream(resource)); if(sf!=null) return sf; } catch(IOException e) { if( debug ) { @@ -271,30 +279,68 @@ class XPathFactoryFinder { protected abstract Object value(); } - /** - * Looks up a value in a property file - * while producing all sorts of debug messages. - * - * @return null - * if there was an error. - */ - private XPathFactory loadFromProperty( String keyName, String resourceName, InputStream in ) - throws IOException { - debugPrintln("Reading "+resourceName ); + /** Searches for a XPathFactory for a given uri in a META-INF/services file. */ + private XPathFactory loadFromServicesFile(String uri, String resourceName, InputStream in) { + + debugPrintln("Reading " + resourceName ); - Properties props = new Properties(); - props.load(in); - in.close(); - String factoryClassName = props.getProperty(keyName); - if(factoryClassName != null){ - debugPrintln("found "+keyName+" = " + factoryClassName); - return createInstance(factoryClassName); - } else { - debugPrintln(keyName+" is not in the property file"); - return null; + BufferedReader rd; + try { + rd = new BufferedReader(new InputStreamReader(in, "UTF-8"), DEFAULT_LINE_LENGTH); + } catch (java.io.UnsupportedEncodingException e) { + rd = new BufferedReader(new InputStreamReader(in), DEFAULT_LINE_LENGTH); } + + String factoryClassName = null; + XPathFactory resultFactory = null; + // See spec for provider-configuration files: http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Provider%20Configuration%20File + while (true) { + try { + factoryClassName = rd.readLine(); + } catch (IOException x) { + // No provider found + break; + } + if (factoryClassName != null) { + // Ignore comments in the provider-configuration file + int hashIndex = factoryClassName.indexOf('#'); + if (hashIndex != -1) { + factoryClassName = factoryClassName.substring(0, hashIndex); + } + + // Ignore leading and trailing whitespace + factoryClassName = factoryClassName.trim(); + + // If there's no text left or if this was a blank line, go to the next one. + if (factoryClassName.length() == 0) { + continue; + } + + try { + // Found the right XPathFactory if its isObjectModelSupported(String uri) method returns true. + XPathFactory foundFactory = (XPathFactory) createInstance(factoryClassName); + if (foundFactory.isObjectModelSupported(uri)) { + resultFactory = foundFactory; + break; + } + } + catch (Exception e) {} + } + else { + break; + } + } + + try { + // try to close the reader. + rd.close(); + } + // Ignore the exception. + catch (IOException exc) {} + + return resultFactory; } - + /** * Returns an {@link Iterator} that enumerates all * the META-INF/services files that we care.