From 2c61e0d811367f8c5b56fab86cf972e45e55c358 Mon Sep 17 00:00:00 2001 From: mrglavas Date: Mon, 20 Jun 2005 22:05:52 +0000 Subject: [PATCH] When processing META-INF/services SchemaFactoryFinder was calling loadFromProperty() which reads a META-INF/services file as if it were a properties file. In SchemaFactory [1] for the META-INF/services file it says to look at the "JAR file specification for file format and parsing rules". Looking at the JAR file specification [2] it says: "The file should contain a newline-separated list of unique concrete provider-class names". We should now be reading the META-INF/services files as specified in the JAR file specification. [1] http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/SchemaFactory.html#newInstance(java.lang.String) [2] http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Provider%20Configuration%20File git-svn-id: https://svn.apache.org/repos/asf/xml/commons/trunk@226247 13f79535-47bb-0310-9956-ffa450edef68 --- .../xml/validation/SchemaFactoryFinder.java | 89 ++++++++++++++++++- 1 file changed, 86 insertions(+), 3 deletions(-) diff --git a/java/external/src/javax/xml/validation/SchemaFactoryFinder.java b/java/external/src/javax/xml/validation/SchemaFactoryFinder.java index 0af2ac1..29859c1 100644 --- a/java/external/src/javax/xml/validation/SchemaFactoryFinder.java +++ b/java/external/src/javax/xml/validation/SchemaFactoryFinder.java @@ -17,9 +17,11 @@ package javax.xml.validation; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; @@ -52,6 +54,11 @@ class SchemaFactoryFinder { */ private static boolean firstTime = true; + /** + * Default columns per line. + */ + private static final int DEFAULT_LINE_LENGTH = 80; + static { // Use try/catch block to support applets try { @@ -146,7 +153,6 @@ class SchemaFactoryFinder { */ private SchemaFactory _newFactory(String schemaLanguage) { SchemaFactory sf; - String propertyName = SERVICE_CLASS.getName() + ":" + schemaLanguage; // system property look up @@ -229,8 +235,7 @@ class SchemaFactoryFinder { URL resource = (URL)sitr.next(); debugPrintln("looking into " + resource); try { - //sf = loadFromProperty(schemaLanguage,resource.toExternalForm(),resource.openStream()); - sf = loadFromProperty(schemaLanguage,resource.toExternalForm(),ss.getURLInputStream(resource)); + sf = loadFromServicesFile(schemaLanguage,resource.toExternalForm(),ss.getURLInputStream(resource)); if(sf!=null) return sf; } catch(IOException e) { if( debug ) { @@ -363,6 +368,84 @@ class SchemaFactoryFinder { } } + /** Searches for a SchemaFactory for a given schema language in a META-INF/services file. */ + private SchemaFactory loadFromServicesFile(String schemaLanguage, String resourceName, InputStream in) { + + debugPrintln("Reading "+resourceName ); + + // Read the service provider name in UTF-8 as specified in + // the jar spec. Unfortunately this fails in Microsoft + // VJ++, which does not implement the UTF-8 + // encoding. Theoretically, we should simply let it fail in + // that case, since the JVM is obviously broken if it + // doesn't support such a basic standard. But since there + // are still some users attempting to use VJ++ for + // development, we have dropped in a fallback which makes a + // second attempt using the platform's default encoding. In + // VJ++ this is apparently ASCII, which is a subset of + // UTF-8... and since the strings we'll be reading here are + // also primarily limited to the 7-bit ASCII range (at + // least, in English versions), this should work well + // enough to keep us on the air until we're ready to + // officially decommit from VJ++. [Edited comment from + // jkesselm] + 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; + SchemaFactory 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 SchemaFactory if its isSchemaLanguageSupported(schemaLanguage) method returns true. + SchemaFactory foundFactory = (SchemaFactory) createInstance(factoryClassName); + if (foundFactory.isSchemaLanguageSupported(schemaLanguage)) { + resultFactory = foundFactory; + break; + } + } + catch (Exception e) {} + } + else { + break; + } + } + + try { + // try to close the reader. + rd.close(); + } + // Ignore the exception. + catch (IOException exc) {} + + return resultFactory; + } + private static final Class SERVICE_CLASS = SchemaFactory.class; private static final String SERVICE_ID = "META-INF/services/" + SERVICE_CLASS.getName();