You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-cvs@xml.apache.org by mk...@apache.org on 2005/06/21 17:08:52 UTC
cvs commit: xml-commons/java/external/src/javax/xml/xpath XPathFactoryFinder.java
mkwan 2005/06/21 08:08:51
Modified: java/external/src/javax/xml/xpath XPathFactoryFinder.java
Log:
Fix a bug in the factory finding algorithm for service files.
Revision Changes Path
1.2 +72 -26 xml-commons/java/external/src/javax/xml/xpath/XPathFactoryFinder.java
Index: XPathFactoryFinder.java
===================================================================
RCS file: /home/cvs/xml-commons/java/external/src/javax/xml/xpath/XPathFactoryFinder.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- XPathFactoryFinder.java 8 Apr 2005 10:39:14 -0000 1.1
+++ XPathFactoryFinder.java 21 Jun 2005 15:08:51 -0000 1.2
@@ -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 @@
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 @@
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 @@
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 );
-
- 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;
+ /** 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 );
+
+ 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.