FileDocCategorySizeDatePackage
DOMImplementationRegistry.javaAPI DocJava SE 5 API12039Fri Aug 26 14:58:38 BST 2005org.w3c.dom.bootstrap

DOMImplementationRegistry

public final class DOMImplementationRegistry extends Object
A factory that enables applications to obtain instances of DOMImplementation.

Example:

// get an instance of the DOMImplementation registry
DOMImplementationRegistry registry =
DOMImplementationRegistry.newInstance();
// get a DOM implementation the Level 3 XML module
DOMImplementation domImpl =
registry.getDOMImplementation("XML 3.0");

This provides an application with an implementation-independent starting point. DOM implementations may modify this class to meet new security standards or to provide *additional* fallbacks for the list of DOMImplementationSources.

see
DOMImplementation
see
DOMImplementationSource
since
DOM Level 3

Fields Summary
public static final String
PROPERTY
The system property to specify the DOMImplementationSource class names.
private static final int
DEFAULT_LINE_LENGTH
Default columns per line.
private Vector
sources
The list of DOMImplementationSources.
Constructors Summary
private DOMImplementationRegistry(Vector srcs)
Private constructor.

param
srcs Vector List of DOMImplementationSources

    
                 
        
	sources = srcs;
    
Methods Summary
public voidaddSource(org.w3c.dom.DOMImplementationSource s)
Register an implementation.

param
s The source to be registered, may not be null

	if (s == null) {
	    throw new NullPointerException();
	}
	if (!sources.contains(s)) {
	    sources.addElement(s);
	}
    
private static java.lang.ClassLoadergetClassLoader()
Gets a class loader.

return
A class loader, possibly null

	try {
	    ClassLoader contextClassLoader = getContextClassLoader();
	    
	    if (contextClassLoader != null) {
		return contextClassLoader;
	    }
	} catch (Exception e) {
	    // Assume that the DOM application is in a JRE 1.1, use the
	    // current ClassLoader
	    return DOMImplementationRegistry.class.getClassLoader();
	}
	return DOMImplementationRegistry.class.getClassLoader();
    
private static java.lang.ClassLoadergetContextClassLoader()
This method returns the ContextClassLoader or null if running in a JRE 1.1

return
The Context Classloader

	return isJRE11()
	    ? null
	    : (ClassLoader)
 	      AccessController.doPrivileged(new PrivilegedAction() {
		    public Object run() {
			ClassLoader classLoader = null;
			try {
			    classLoader =
				Thread.currentThread().getContextClassLoader();
			} catch (SecurityException ex) {
			}
			return classLoader;
		    }
		});
    
public org.w3c.dom.DOMImplementationgetDOMImplementation(java.lang.String features)
Return the first 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 null if none found.

	int size = sources.size();
	String name = null;
	for (int i = 0; i < size; i++) {
	    DOMImplementationSource source =
		(DOMImplementationSource) sources.elementAt(i);
	    DOMImplementation impl = source.getDOMImplementation(features);
	    if (impl != null) {
		return impl;
	    }
	}
	return null;
    
public org.w3c.dom.DOMImplementationListgetDOMImplementationList(java.lang.String features)
Return a list of implementations that support the desired features.

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
A list of DOMImplementations that support the desired features.

	final Vector implementations = new Vector();
	int size = sources.size();
	for (int i = 0; i < size; i++) {
	    DOMImplementationSource source =
		(DOMImplementationSource) sources.elementAt(i);
	    DOMImplementationList impls =
		source.getDOMImplementationList(features);
	    for (int j = 0; j < impls.getLength(); j++) {
		DOMImplementation impl = impls.item(j);
		implementations.addElement(impl);
	    }
	}
	return new DOMImplementationList() {
		public DOMImplementation item(final int index) {
		    if (index >= 0 && index < implementations.size()) {
			try {
			    return (DOMImplementation)
				implementations.elementAt(index);
			} catch (ArrayIndexOutOfBoundsException e) {
			    return null;
			}
		    }
		    return null;
		}
		
		public int getLength() {
		    return implementations.size();
		}
	    };
    
private static java.io.InputStreamgetResourceAsStream(java.lang.ClassLoader classLoader, java.lang.String name)
This method returns an Inputstream for the reading resource META_INF/services/org.w3c.dom.DOMImplementationSourceList after checking access control privileges. For a JRE 1.1, this check is not done.

param
classLoader classLoader
param
name the resource
return
an Inputstream for the resource specified

	if (isJRE11()) {
	    InputStream ris;
	    if (classLoader == null) {
		ris = ClassLoader.getSystemResourceAsStream(name);
	    } else {
		ris = classLoader.getResourceAsStream(name);
	    }    
	    return ris;
	} else {
	    return (InputStream)
		AccessController.doPrivileged(new PrivilegedAction() {
			public Object run() {
			    InputStream ris;
			    if (classLoader == null) {
				ris =
				    ClassLoader.getSystemResourceAsStream(name);
			    } else {
				ris = classLoader.getResourceAsStream(name);
			    }
			    return ris;
			}
		    });
	}
    
private static java.lang.StringgetServiceValue(java.lang.ClassLoader classLoader)
This method attempts to return the first line of the resource META_INF/services/org.w3c.dom.DOMImplementationSourceList from the provided ClassLoader.

param
classLoader classLoader, may not be null.
return
first line of resource, or null

	String serviceId = "META-INF/services/" + PROPERTY;
	// try to find services in CLASSPATH
	try {
	    InputStream is = getResourceAsStream(classLoader, serviceId);
	    
	    if (is != null) {
		BufferedReader rd;
		try {
		    rd =
			new BufferedReader(new InputStreamReader(is, "UTF-8"),
					   DEFAULT_LINE_LENGTH);
		} catch (java.io.UnsupportedEncodingException e) {
		    rd =
			new BufferedReader(new InputStreamReader(is),
					   DEFAULT_LINE_LENGTH);
		}		
		String serviceValue = rd.readLine();
		rd.close();
		if (serviceValue != null && serviceValue.length() > 0) {
		    return serviceValue;
		}
	    }
	} catch (Exception ex) {
	    return null;
	}
	return null;
    
private static java.lang.StringgetSystemProperty(java.lang.String name)
This method returns the system property indicated by the specified name after checking access control privileges. For a JRE 1.1, this check is not done.

param
name the name of the system property
return
the system property

	return isJRE11()
	    ? (String) System.getProperty(name)
	    : (String) AccessController.doPrivileged(new PrivilegedAction() {
		    public Object run() {
			return System.getProperty(name);
		    }
		});
    
private static booleanisJRE11()
A simple JRE (Java Runtime Environment) 1.1 test

return
true if JRE 1.1

	try {
	    Class c = Class.forName("java.security.AccessController");
	    // java.security.AccessController existed since 1.2 so, if no
	    // exception was thrown, the DOM application is running in a JRE
	    // 1.2 or higher
	    return false;
	} catch (Exception ex) {
	    // ignore 
	}
	return true;
    
public static org.w3c.dom.bootstrap.DOMImplementationRegistrynewInstance()
Obtain a new instance of a DOMImplementationRegistry. The DOMImplementationRegistry is initialized by the application or the implementation, depending on the context, by first checking the value of the Java system property org.w3c.dom.DOMImplementationSourceList and the the service provider whose contents are at "META_INF/services/org.w3c.dom.DOMImplementationSourceList" The value of this property is a white-space separated list of names of availables classes implementing the DOMImplementationSource interface. Each class listed in the class name list is instantiated and any exceptions encountered are thrown to the application.

return
an initialized instance of DOMImplementationRegistry
throws
ClassNotFoundException If any specified class can not be found
throws
InstantiationException If any specified class is an interface or abstract class
throws
IllegalAccessException If the default constructor of a specified class is not accessible
throws
ClassCastException If any specified class does not implement DOMImplementationSource

	Vector sources = new Vector();
	
	ClassLoader classLoader = getClassLoader();
	// fetch system property:
	String p = getSystemProperty(PROPERTY);
	
	//
	// if property is not specified then use contents of
        // META_INF/org.w3c.dom.DOMImplementationSourceList from classpath
	if (p == null) {
	    p = getServiceValue(classLoader);
	} 
        if (p == null) {
	    //
	    // DOM Implementations can modify here to add *additional* fallback
	    // mechanisms to access a list of default DOMImplementationSources.
	    
	}
	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...
		Class sourceClass = null;
		if (classLoader != null) {
		    sourceClass = classLoader.loadClass(sourceName);
		} else {
		    sourceClass = Class.forName(sourceName);
		}
		DOMImplementationSource source =
		    (DOMImplementationSource) sourceClass.newInstance();
		sources.addElement(source);
	    }
	}
	return new DOMImplementationRegistry(sources);