lehors 2002/07/31 18:45:49 Modified: java/src/dom3/org/w3c/dom DOMImplementationRegistry.java Log: new version without any static table to avoid static mutability problems Revision Changes Path 1.3 +158 -112 xml-xerces/java/src/dom3/org/w3c/dom/DOMImplementationRegistry.java Index: DOMImplementationRegistry.java =================================================================== RCS file: /home/cvs/xml-xerces/java/src/dom3/org/w3c/dom/DOMImplementationRegistry.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- DOMImplementationRegistry.java 19 Jun 2002 22:27:10 -0000 1.2 +++ DOMImplementationRegistry.java 1 Aug 2002 01:45:49 -0000 1.3 @@ -1,112 +1,158 @@ -/* - * Copyright (c) 2002 World Wide Web Consortium, - * (Massachusetts Institute of Technology, Institut National de - * Recherche en Informatique et en Automatique, Keio University). All - * Rights Reserved. This program is distributed under the W3C's Software - * Intellectual Property License. This program is distributed in the - * hope that it will be useful, but WITHOUT ANY WARRANTY; without even - * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - * PURPOSE. - * See W3C License http://www.w3.org/Consortium/Legal/ for more details. - */ - - -package org.w3c.dom; - -import java.util.StringTokenizer; -import java.util.Vector; - -/** - * DOM Level 3 WD Experimental: - * The DOM Level 3 specification is at the stage - * of Working Draft, which represents work in - * progress and thus may be updated, replaced, - * or obsoleted by other documents at any time. - * <p> - * This class holds the list of registered DOMImplementations. It is first - * initialized based on the content of the space separated list of classnames - * contained in the System Property "org.w3c.dom.DOMImplementationSourceList". - * - * <p>Subsequently, additional sources can be registered and implementations - * can be queried based on a list of requested features. - * - * <p>This provides an application with an implementation independent starting - * point. - * - * @see DOMImplementation - * @see DOMImplementationSource - */ -public class DOMImplementationRegistry -{ - - // The system property to specify the DOMImplementationSource class names. - public static String PROPERTY = "org.w3c.dom.DOMImplementationSourceList"; - - private static Vector sources = new Vector(); - private static boolean initialized = false; - - private static void initialize() throws ClassNotFoundException, - InstantiationException, IllegalAccessException - { - initialized = true; - String p = System.getProperty(PROPERTY); - if (p == null) { - return; - } - StringTokenizer st = new StringTokenizer(p); - while (st.hasMoreTokens()) { - Object source = Class.forName(st.nextToken()).newInstance(); - sources.addElement(source); - } - } - - /** - * Return the first registered implementation that has the desired features, - * or null if none is found. - * - * @param features A string that specifies which features are required. - * This is a space separated list in which each feature is - * specified by its name optionally followed by a space - * and a version number. - * This is something like: "XML 1.0 Traversal Events 2.0" - * @return An implementation that has the desired features, or - * <code>null</code> if this source has none. - */ - public static DOMImplementation getDOMImplementation(String features) - throws ClassNotFoundException, - InstantiationException, IllegalAccessException - { - if (!initialized) { - initialize(); - } - int len = sources.size(); - for (int i = 0; i < len; i++) { - DOMImplementationSource source = - (DOMImplementationSource) sources.elementAt(i); - - DOMImplementation impl = source.getDOMImplementation(features); - if (impl != null) { - return impl; - } - } - return null; - } - - /** - * Register an implementation. - */ - public static void addSource(DOMImplementationSource s) - throws ClassNotFoundException, - InstantiationException, IllegalAccessException - { - if (!initialized) { - initialize(); - } - sources.addElement(s); - // update system property accordingly - StringBuffer b = new StringBuffer(System.getProperty(PROPERTY)); - b.append(" " + s.getClass().getName()); - System.setProperty(PROPERTY, b.toString()); - } -} - \ No newline at end of file +/** + * This class holds the list of registered DOMImplementations. The contents + * of the registry are drawn from the System Property + * <code>org.w3c.dom.DOMImplementationSourceList</code>, which must contain a + * white-space delimited sequence of the names of classes implementing + * <code>DOMImplementationSource</code>. + * Applications may also register DOMImplementationSource + * implementations by using a method on this class. They may then + * query instances of the registry for implementations supporting + * specific features.</p> + * + * <p>This provides an application with an implementation-independent + * starting point. + * + * @see DOMImplementation + * @see DOMImplementationSource + */ + +package org.w3c.dom; + +import java.lang.reflect.Method; +import java.lang.reflect.InvocationTargetException; +import java.lang.ClassLoader; +import java.lang.String; +import java.util.StringTokenizer; +import java.util.Enumeration; +import java.util.Hashtable; + +import org.w3c.dom.DOMImplementationSource; +import org.w3c.dom.DOMImplementation; + +public class DOMImplementationRegistry { + + // The system property to specify the DOMImplementationSource class names. + public final static String PROPERTY = + "org.w3c.dom.DOMImplementationSourceList"; + + private Hashtable sources = new Hashtable(); + + // deny construction by other classes + private DOMImplementationRegistry() { + } + + // deny construction by other classes + private DOMImplementationRegistry(Hashtable srcs) { + sources = srcs; + } + + + /* + * This method queries the System property + * <code>org.w3c.dom.DOMImplementationSourceList</code>. If it is + * able to read and parse the property, it attempts to instantiate + * classes according to each space-delimited substring. Any + * exceptions it encounters are thrown to the application. An application + * must call this method before using the class. + * @return an initialized instance of DOMImplementationRegistry + */ + public static DOMImplementationRegistry newInstance() + throws ClassNotFoundException, InstantiationException, + IllegalAccessException + { + Hashtable sources = new Hashtable(); + + // fetch system property: + String p = System.getProperty(PROPERTY); + if (p != null) { + StringTokenizer st = new StringTokenizer(p); + while (st.hasMoreTokens()) { + String sourceName = st.nextToken(); + // Use context class loader, falling back to Class.forName + // if and only if this fails... + Object source = getClass(sourceName).newInstance(); + sources.put(sourceName, source); + } + } + return new DOMImplementationRegistry(sources); + } + + + /** + * Return the first registered implementation that has the desired + * features, or null if none is found. + * + * @param features A string that specifies which features are required. + * This is a space separated list in which each feature is + * specified by its name optionally followed by a space + * and a version number. + * This is something like: "XML 1.0 Traversal Events 2.0" + * @return An implementation that has the desired features, or + * <code>null</code> if this source has none. + */ + public DOMImplementation getDOMImplementation(String features) + throws ClassNotFoundException, + InstantiationException, IllegalAccessException, ClassCastException + { + Enumeration names = sources.keys(); + String name = null; + while(names.hasMoreElements()) { + name = (String)names.nextElement(); + DOMImplementationSource source = + (DOMImplementationSource) sources.get(name); + + DOMImplementation impl = source.getDOMImplementation(features); + if (impl != null) { + return impl; + } + } + return null; + } + + /** + * Register an implementation. + */ + public void addSource(DOMImplementationSource s) + throws ClassNotFoundException, + InstantiationException, IllegalAccessException + { + String sourceName = s.getClass().getName(); + sources.put(sourceName, s); + } + + private static Class getClass (String className) + throws ClassNotFoundException, IllegalAccessException, + InstantiationException { + Method m = null; + ClassLoader cl = null; + + try { + m = Thread.class.getMethod("getContextClassLoader", null); + } catch (NoSuchMethodException e) { + // Assume that we are running JDK 1.1, use the current ClassLoader + cl = DOMImplementationRegistry.class.getClassLoader(); + } + + if (cl == null ) { + try { + cl = (ClassLoader) m.invoke(Thread.currentThread(), null); + } catch (IllegalAccessException e) { + // assert(false) + throw new UnknownError(e.getMessage()); + } catch (InvocationTargetException e) { + // assert(e.getTargetException() instanceof SecurityException) + throw new UnknownError(e.getMessage()); + } + } + if (cl == null) { + // fall back to Class.forName + return Class.forName(className); + } + try { + return cl.loadClass(className); + } catch (ClassNotFoundException e) { + return Class.forName(className); + } + } +} +
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]