2
* Copyright (c) 2003 World Wide Web Consortium,
4
* (Massachusetts Institute of Technology, European Research Consortium for
5
* Informatics and Mathematics, Keio University). All Rights Reserved. This
6
* work is distributed under the W3C(r) Software License [1] in the hope that
7
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
8
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
* [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
15
* This class holds the list of registered DOMImplementations. The contents
16
* of the registry are drawn from the System Property
17
* <code>org.w3c.dom.DOMImplementationSourceList</code>, which must contain a
18
* white-space delimited sequence of the names of classes implementing
19
* <code>DOMImplementationSource</code>.
20
* Applications may also register DOMImplementationSource
21
* implementations by using a method on this class. They may then
22
* query instances of the registry for implementations supporting
26
* <pre class='example'>
27
* // get an instance of the DOMImplementation registry
28
* DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
29
* // get a DOM implementation the Level 3 XML module
30
* DOMImplementation domImpl = registry.getDOMImplementation("XML 3.0");
32
* <p>This provides an application with an implementation-independent
35
* @see DOMImplementation
36
* @see DOMImplementationSource
40
package org.apache.xerces.dom3.bootstrap;
42
import java.lang.reflect.Method;
43
import java.lang.reflect.InvocationTargetException;
44
import java.util.StringTokenizer;
45
import java.util.Vector;
47
import org.apache.xerces.dom3.DOMImplementationSource;
48
import org.apache.xerces.dom3.DOMImplementationList;
49
import org.w3c.dom.DOMImplementation;
51
public class DOMImplementationRegistry {
53
// The system property to specify the DOMImplementationSource class names.
54
public final static String PROPERTY =
55
"org.w3c.dom.DOMImplementationSourceList";
57
private Vector _sources;
59
// deny construction by other classes
60
private DOMImplementationRegistry() {
63
// deny construction by other classes
64
private DOMImplementationRegistry(Vector srcs) {
70
* This method queries the System property
71
* <code>org.w3c.dom.DOMImplementationSourceList</code>. If it is
72
* able to read and parse the property, it attempts to instantiate
73
* classes according to each space-delimited substring. Any
74
* exceptions it encounters are thrown to the application. An application
75
* must call this method before using the class.
76
* @return an initialized instance of DOMImplementationRegistry
78
public static DOMImplementationRegistry newInstance()
79
throws ClassNotFoundException, InstantiationException,
80
IllegalAccessException
82
Vector _sources = new Vector();
84
// fetch system property:
85
String p = System.getProperty(PROPERTY);
87
StringTokenizer st = new StringTokenizer(p);
88
while (st.hasMoreTokens()) {
89
String sourceName = st.nextToken();
90
// Use context class loader, falling back to Class.forName
91
// if and only if this fails...
92
Object source = getClass(sourceName).newInstance();
96
return new DOMImplementationRegistry(_sources);
101
* Return the first registered implementation that has the desired
102
* features, or null if none is found.
104
* @param features A string that specifies which features are required.
105
* This is a space separated list in which each feature is
106
* specified by its name optionally followed by a space
107
* and a version number.
108
* This is something like: "XML 1.0 Traversal +Events 2.0"
109
* @return An implementation that has the desired features, or
110
* <code>null</code> if this source has none.
112
public DOMImplementation getDOMImplementation(String features)
113
throws ClassNotFoundException,
114
InstantiationException, IllegalAccessException, ClassCastException
116
int size = _sources.size();
118
for (int i = 0; i < size; i++) {
119
DOMImplementationSource source =
120
(DOMImplementationSource) _sources.get(i);
122
DOMImplementation impl = source.getDOMImplementation(features);
131
* Return the list of all registered implementation that support the desired
134
* @param features A string that specifies which features are required.
135
* This is a space separated list in which each feature is
136
* specified by its name optionally followed by a space
137
* and a version number.
138
* This is something like: "XML 1.0 Traversal +Events 2.0"
139
* @return A list of DOMImplementations that support the desired features.
141
public DOMImplementationList getDOMImplementationList(String features)
142
throws ClassNotFoundException,
143
InstantiationException, IllegalAccessException, ClassCastException
145
int size = _sources.size();
146
DOMImplementationListImpl list = new DOMImplementationListImpl();
148
for (int i = 0; i < size; i++) {
149
DOMImplementationSource source =
150
(DOMImplementationSource) _sources.get(i);
152
DOMImplementationList impls =
153
source.getDOMImplementationList(features);
154
for (int j = 0; j < impls.getLength(); j++) {
155
list.add(impls.item(j));
162
* Register an implementation.
164
public void addSource(DOMImplementationSource s)
165
throws ClassNotFoundException,
166
InstantiationException, IllegalAccessException
171
private static Class getClass (String className)
172
throws ClassNotFoundException, IllegalAccessException,
173
InstantiationException {
175
ClassLoader cl = null;
178
m = Thread.class.getMethod("getContextClassLoader", null);
179
} catch (NoSuchMethodException e) {
180
// Assume that we are running JDK 1.1, use the current ClassLoader
181
cl = DOMImplementationRegistry.class.getClassLoader();
186
cl = (ClassLoader) m.invoke(Thread.currentThread(), null);
187
} catch (IllegalAccessException e) {
189
throw new UnknownError(e.getMessage());
190
} catch (InvocationTargetException e) {
191
// assert(e.getTargetException() instanceof SecurityException)
192
throw new UnknownError(e.getMessage());
196
// fall back to Class.forName
197
return Class.forName(className);
200
return cl.loadClass(className);
201
} catch (ClassNotFoundException e) {
202
return Class.forName(className);
b'\\ No newline at end of file'