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
This commit is contained in:
mkwan 2005-06-21 15:08:51 +00:00
parent 2c61e0d811
commit 410062edf4

View File

@ -17,10 +17,12 @@
package javax.xml.xpath; package javax.xml.xpath;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
@ -38,8 +40,15 @@ import java.util.Properties;
class XPathFactoryFinder { class XPathFactoryFinder {
private static SecuritySupport ss = new SecuritySupport() ; private static SecuritySupport ss = new SecuritySupport() ;
/** debug support code. */ /** debug support code. */
private static boolean debug = false; private static boolean debug = false;
/**
* Default columns per line.
*/
private static final int DEFAULT_LINE_LENGTH = 80;
static { static {
// Use try/catch block to support applets // Use try/catch block to support applets
try { try {
@ -204,8 +213,7 @@ class XPathFactoryFinder {
URL resource = (URL)sitr.next(); URL resource = (URL)sitr.next();
debugPrintln("looking into " + resource); debugPrintln("looking into " + resource);
try { try {
//sf = loadFromProperty(uri,resource.toExternalForm(),resource.openStream()); sf = loadFromServicesFile(uri, resource.toExternalForm(), ss.getURLInputStream(resource));
sf = loadFromProperty(uri,resource.toExternalForm(),ss.getURLInputStream(resource));
if(sf!=null) return sf; if(sf!=null) return sf;
} catch(IOException e) { } catch(IOException e) {
if( debug ) { if( debug ) {
@ -271,28 +279,66 @@ class XPathFactoryFinder {
protected abstract Object value(); protected abstract Object value();
} }
/** /** Searches for a XPathFactory for a given uri in a META-INF/services file. */
* Looks up a value in a property file private XPathFactory loadFromServicesFile(String uri, String resourceName, InputStream in) {
* 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 );
Properties props = new Properties(); debugPrintln("Reading " + resourceName );
props.load(in);
in.close(); BufferedReader rd;
String factoryClassName = props.getProperty(keyName); try {
if(factoryClassName != null){ rd = new BufferedReader(new InputStreamReader(in, "UTF-8"), DEFAULT_LINE_LENGTH);
debugPrintln("found "+keyName+" = " + factoryClassName); } catch (java.io.UnsupportedEncodingException e) {
return createInstance(factoryClassName); rd = new BufferedReader(new InputStreamReader(in), DEFAULT_LINE_LENGTH);
} else {
debugPrintln(keyName+" is not in the property file");
return null;
} }
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;
} }
/** /**