Adding a fallback DOMImplementationSource: the DOMXSImplemenationSourceImpl class from Xerces.

git-svn-id: https://svn.apache.org/repos/asf/xml/commons/trunk@226226 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
mrglavas 2005-06-04 04:23:42 +00:00
parent 8dcb34f116
commit ad5eac5eb9

View File

@ -58,13 +58,18 @@ public final class DOMImplementationRegistry {
* DOMImplementationSource class names. * DOMImplementationSource class names.
*/ */
public static final String PROPERTY = public static final String PROPERTY =
"org.w3c.dom.DOMImplementationSourceList"; "org.w3c.dom.DOMImplementationSourceList";
/** /**
* Default columns per line. * Default columns per line.
*/ */
private static final int DEFAULT_LINE_LENGTH = 80; private static final int DEFAULT_LINE_LENGTH = 80;
/**
* Default DOMImplementationSource.
*/
private static final String DEFAULT_DOM_IMPLEMENTATION_SOURCE = "org.apache.xerces.dom.DOMXSImplementationSourceImpl";
/** /**
* The list of DOMImplementationSources. * The list of DOMImplementationSources.
*/ */
@ -75,7 +80,7 @@ public final class DOMImplementationRegistry {
* @param srcs Vector List of DOMImplementationSources * @param srcs Vector List of DOMImplementationSources
*/ */
private DOMImplementationRegistry(final Vector srcs) { private DOMImplementationRegistry(final Vector srcs) {
sources = srcs; sources = srcs;
} }
/** /**
@ -106,47 +111,47 @@ public final class DOMImplementationRegistry {
* <code>DOMImplementationSource</code> * <code>DOMImplementationSource</code>
*/ */
public static DOMImplementationRegistry newInstance() public static DOMImplementationRegistry newInstance()
throws throws
ClassNotFoundException, ClassNotFoundException,
InstantiationException, InstantiationException,
IllegalAccessException, IllegalAccessException,
ClassCastException { ClassCastException {
Vector sources = new Vector(); Vector sources = new Vector();
ClassLoader classLoader = getClassLoader(); ClassLoader classLoader = getClassLoader();
// fetch system property: // fetch system property:
String p = getSystemProperty(PROPERTY); String p = getSystemProperty(PROPERTY);
// //
// if property is not specified then use contents of // if property is not specified then use contents of
// META_INF/org.w3c.dom.DOMImplementationSourceList from classpath // META_INF/org.w3c.dom.DOMImplementationSourceList from classpath
if (p == null) {
p = getServiceValue(classLoader);
}
if (p == null) { if (p == null) {
// p = getServiceValue(classLoader);
// DOM Implementations can modify here to add *additional* fallback }
// mechanisms to access a list of default DOMImplementationSources. if (p == null) {
//
} // DOM Implementations can modify here to add *additional* fallback
if (p != null) { // mechanisms to access a list of default DOMImplementationSources.
StringTokenizer st = new StringTokenizer(p); p = DEFAULT_DOM_IMPLEMENTATION_SOURCE;
while (st.hasMoreTokens()) { }
String sourceName = st.nextToken(); if (p != null) {
// Use context class loader, falling back to Class.forName StringTokenizer st = new StringTokenizer(p);
// if and only if this fails... while (st.hasMoreTokens()) {
Class sourceClass = null; String sourceName = st.nextToken();
if (classLoader != null) { // Use context class loader, falling back to Class.forName
sourceClass = classLoader.loadClass(sourceName); // if and only if this fails...
} else { Class sourceClass = null;
sourceClass = Class.forName(sourceName); if (classLoader != null) {
} sourceClass = classLoader.loadClass(sourceName);
DOMImplementationSource source = } else {
(DOMImplementationSource) sourceClass.newInstance(); sourceClass = Class.forName(sourceName);
sources.addElement(source); }
} DOMImplementationSource source =
} (DOMImplementationSource) sourceClass.newInstance();
return new DOMImplementationRegistry(sources); sources.addElement(source);
}
}
return new DOMImplementationRegistry(sources);
} }
/** /**
@ -162,17 +167,17 @@ public final class DOMImplementationRegistry {
* or <code>null</code> if none found. * or <code>null</code> if none found.
*/ */
public DOMImplementation getDOMImplementation(final String features) { public DOMImplementation getDOMImplementation(final String features) {
int size = sources.size(); int size = sources.size();
String name = null; String name = null;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
DOMImplementationSource source = DOMImplementationSource source =
(DOMImplementationSource) sources.elementAt(i); (DOMImplementationSource) sources.elementAt(i);
DOMImplementation impl = source.getDOMImplementation(features); DOMImplementation impl = source.getDOMImplementation(features);
if (impl != null) { if (impl != null) {
return impl; return impl;
} }
} }
return null; return null;
} }
/** /**
@ -187,35 +192,35 @@ public final class DOMImplementationRegistry {
* @return A list of DOMImplementations that support the desired features. * @return A list of DOMImplementations that support the desired features.
*/ */
public DOMImplementationList getDOMImplementationList(final String features) { public DOMImplementationList getDOMImplementationList(final String features) {
final Vector implementations = new Vector(); final Vector implementations = new Vector();
int size = sources.size(); int size = sources.size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
DOMImplementationSource source = DOMImplementationSource source =
(DOMImplementationSource) sources.elementAt(i); (DOMImplementationSource) sources.elementAt(i);
DOMImplementationList impls = DOMImplementationList impls =
source.getDOMImplementationList(features); source.getDOMImplementationList(features);
for (int j = 0; j < impls.getLength(); j++) { for (int j = 0; j < impls.getLength(); j++) {
DOMImplementation impl = impls.item(j); DOMImplementation impl = impls.item(j);
implementations.addElement(impl); implementations.addElement(impl);
} }
} }
return new DOMImplementationList() { return new DOMImplementationList() {
public DOMImplementation item(final int index) { public DOMImplementation item(final int index) {
if (index >= 0 && index < implementations.size()) { if (index >= 0 && index < implementations.size()) {
try { try {
return (DOMImplementation) return (DOMImplementation)
implementations.elementAt(index); implementations.elementAt(index);
} catch (ArrayIndexOutOfBoundsException e) { } catch (ArrayIndexOutOfBoundsException e) {
return null; return null;
} }
} }
return null; return null;
} }
public int getLength() { public int getLength() {
return implementations.size(); return implementations.size();
} }
}; };
} }
/** /**
@ -224,12 +229,12 @@ public final class DOMImplementationRegistry {
* @param s The source to be registered, may not be <code>null</code> * @param s The source to be registered, may not be <code>null</code>
*/ */
public void addSource(final DOMImplementationSource s) { public void addSource(final DOMImplementationSource s) {
if (s == null) { if (s == null) {
throw new NullPointerException(); throw new NullPointerException();
} }
if (!sources.contains(s)) { if (!sources.contains(s)) {
sources.addElement(s); sources.addElement(s);
} }
} }
/** /**
@ -239,18 +244,18 @@ public final class DOMImplementationRegistry {
* @return A class loader, possibly <code>null</code> * @return A class loader, possibly <code>null</code>
*/ */
private static ClassLoader getClassLoader() { private static ClassLoader getClassLoader() {
try { try {
ClassLoader contextClassLoader = getContextClassLoader(); ClassLoader contextClassLoader = getContextClassLoader();
if (contextClassLoader != null) { if (contextClassLoader != null) {
return contextClassLoader; return contextClassLoader;
} }
} catch (Exception e) { } catch (Exception e) {
// Assume that the DOM application is in a JRE 1.1, use the // Assume that the DOM application is in a JRE 1.1, use the
// current ClassLoader // current ClassLoader
return DOMImplementationRegistry.class.getClassLoader(); return DOMImplementationRegistry.class.getClassLoader();
} }
return DOMImplementationRegistry.class.getClassLoader(); return DOMImplementationRegistry.class.getClassLoader();
} }
/** /**
@ -262,32 +267,32 @@ public final class DOMImplementationRegistry {
* @return first line of resource, or <code>null</code> * @return first line of resource, or <code>null</code>
*/ */
private static String getServiceValue(final ClassLoader classLoader) { private static String getServiceValue(final ClassLoader classLoader) {
String serviceId = "META-INF/services/" + PROPERTY; String serviceId = "META-INF/services/" + PROPERTY;
// try to find services in CLASSPATH // try to find services in CLASSPATH
try { try {
InputStream is = getResourceAsStream(classLoader, serviceId); InputStream is = getResourceAsStream(classLoader, serviceId);
if (is != null) { if (is != null) {
BufferedReader rd; BufferedReader rd;
try { try {
rd = rd =
new BufferedReader(new InputStreamReader(is, "UTF-8"), new BufferedReader(new InputStreamReader(is, "UTF-8"),
DEFAULT_LINE_LENGTH); DEFAULT_LINE_LENGTH);
} catch (java.io.UnsupportedEncodingException e) { } catch (java.io.UnsupportedEncodingException e) {
rd = rd =
new BufferedReader(new InputStreamReader(is), new BufferedReader(new InputStreamReader(is),
DEFAULT_LINE_LENGTH); DEFAULT_LINE_LENGTH);
} }
String serviceValue = rd.readLine(); String serviceValue = rd.readLine();
rd.close(); rd.close();
if (serviceValue != null && serviceValue.length() > 0) { if (serviceValue != null && serviceValue.length() > 0) {
return serviceValue; return serviceValue;
} }
} }
} catch (Exception ex) { } catch (Exception ex) {
return null; return null;
} }
return null; return null;
} }
/** /**
@ -296,16 +301,16 @@ public final class DOMImplementationRegistry {
* @return <code>true</code> if JRE 1.1 * @return <code>true</code> if JRE 1.1
*/ */
private static boolean isJRE11() { private static boolean isJRE11() {
try { try {
Class c = Class.forName("java.security.AccessController"); Class c = Class.forName("java.security.AccessController");
// java.security.AccessController existed since 1.2 so, if no // java.security.AccessController existed since 1.2 so, if no
// exception was thrown, the DOM application is running in a JRE // exception was thrown, the DOM application is running in a JRE
// 1.2 or higher // 1.2 or higher
return false; return false;
} catch (Exception ex) { } catch (Exception ex) {
// ignore // ignore
} }
return true; return true;
} }
/** /**
@ -315,20 +320,20 @@ public final class DOMImplementationRegistry {
* @return The Context Classloader * @return The Context Classloader
*/ */
private static ClassLoader getContextClassLoader() { private static ClassLoader getContextClassLoader() {
return isJRE11() return isJRE11()
? null ? null
: (ClassLoader) : (ClassLoader)
AccessController.doPrivileged(new PrivilegedAction() { AccessController.doPrivileged(new PrivilegedAction() {
public Object run() { public Object run() {
ClassLoader classLoader = null; ClassLoader classLoader = null;
try { try {
classLoader = classLoader =
Thread.currentThread().getContextClassLoader(); Thread.currentThread().getContextClassLoader();
} catch (SecurityException ex) { } catch (SecurityException ex) {
} }
return classLoader; return classLoader;
} }
}); });
} }
/** /**
@ -340,13 +345,13 @@ public final class DOMImplementationRegistry {
* @return the system property * @return the system property
*/ */
private static String getSystemProperty(final String name) { private static String getSystemProperty(final String name) {
return isJRE11() return isJRE11()
? (String) System.getProperty(name) ? (String) System.getProperty(name)
: (String) AccessController.doPrivileged(new PrivilegedAction() { : (String) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() { public Object run() {
return System.getProperty(name); return System.getProperty(name);
} }
}); });
} }
/** /**
@ -359,29 +364,29 @@ public final class DOMImplementationRegistry {
* @return an Inputstream for the resource specified * @return an Inputstream for the resource specified
*/ */
private static InputStream getResourceAsStream(final ClassLoader classLoader, private static InputStream getResourceAsStream(final ClassLoader classLoader,
final String name) { final String name) {
if (isJRE11()) { if (isJRE11()) {
InputStream ris; InputStream ris;
if (classLoader == null) { if (classLoader == null) {
ris = ClassLoader.getSystemResourceAsStream(name); ris = ClassLoader.getSystemResourceAsStream(name);
} else { } else {
ris = classLoader.getResourceAsStream(name); ris = classLoader.getResourceAsStream(name);
} }
return ris; return ris;
} else { } else {
return (InputStream) return (InputStream)
AccessController.doPrivileged(new PrivilegedAction() { AccessController.doPrivileged(new PrivilegedAction() {
public Object run() { public Object run() {
InputStream ris; InputStream ris;
if (classLoader == null) { if (classLoader == null) {
ris = ris =
ClassLoader.getSystemResourceAsStream(name); ClassLoader.getSystemResourceAsStream(name);
} else { } else {
ris = classLoader.getResourceAsStream(name); ris = classLoader.getResourceAsStream(name);
} }
return ris; return ris;
} }
}); });
} }
} }
} }