~ubuntu-branches/ubuntu/karmic/libxerces2-java/karmic

« back to all changes in this revision

Viewing changes to src/dom3/org/w3c/dom/bootstrap/DOMImplementationRegistry.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2004-06-06 18:00:26 UTC
  • Revision ID: james.westby@ubuntu.com-20040606180026-a3vh56uc95hjbyfh
Tags: upstream-2.6.2
ImportĀ upstreamĀ versionĀ 2.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2003 World Wide Web Consortium,
 
3
 *
 
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.
 
9
 *
 
10
 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
 
11
 */
 
12
 
 
13
 
 
14
/**
 
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
 
23
 * specific features.
 
24
 *
 
25
 * <p>Example:</p>
 
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");
 
31
 * </pre>
 
32
 * <p>This provides an application with an implementation-independent 
 
33
 * starting point.</p>
 
34
 *
 
35
 * @see DOMImplementation
 
36
 * @see DOMImplementationSource
 
37
 * @since DOM Level 3
 
38
 */
 
39
 
 
40
package org.w3c.dom.bootstrap;
 
41
 
 
42
import java.lang.reflect.Method;
 
43
import java.lang.reflect.InvocationTargetException;
 
44
import java.lang.ClassLoader;
 
45
import java.lang.String;
 
46
import java.util.StringTokenizer;
 
47
import java.util.Enumeration;
 
48
import java.util.Vector;
 
49
 
 
50
import org.w3c.dom.DOMImplementationSource;
 
51
import org.w3c.dom.DOMImplementationList;
 
52
import org.w3c.dom.DOMImplementation;
 
53
 
 
54
public class DOMImplementationRegistry { 
 
55
 
 
56
    // The system property to specify the DOMImplementationSource class names.
 
57
    public final static String PROPERTY =
 
58
        "org.w3c.dom.DOMImplementationSourceList";
 
59
 
 
60
    private Vector _sources;
 
61
 
 
62
    // deny construction by other classes
 
63
    private DOMImplementationRegistry() {
 
64
    }
 
65
 
 
66
    // deny construction by other classes
 
67
    private DOMImplementationRegistry(Vector srcs) {
 
68
        _sources = srcs;
 
69
    }
 
70
 
 
71
 
 
72
    /* 
 
73
     * This method queries the System property
 
74
     * <code>org.w3c.dom.DOMImplementationSourceList</code>. If it is
 
75
     * able to read and parse the property, it attempts to instantiate
 
76
     * classes according to each space-delimited substring. Any
 
77
     * exceptions it encounters are thrown to the application. An application
 
78
     * must call this method before using the class.
 
79
     * @return  an initialized instance of DOMImplementationRegistry
 
80
     */ 
 
81
    public static DOMImplementationRegistry newInstance()               
 
82
            throws ClassNotFoundException, InstantiationException, 
 
83
            IllegalAccessException
 
84
    {
 
85
        Vector _sources = new Vector();    
 
86
 
 
87
        // fetch system property:
 
88
        String p = System.getProperty(PROPERTY);
 
89
        if (p != null) {
 
90
            StringTokenizer st = new StringTokenizer(p);
 
91
            while (st.hasMoreTokens()) {
 
92
                String sourceName = st.nextToken();
 
93
                // Use context class loader, falling back to Class.forName
 
94
                // if and only if this fails...
 
95
                Object source = getClass(sourceName).newInstance();
 
96
                _sources.add(source);
 
97
            }
 
98
        }
 
99
        return new DOMImplementationRegistry(_sources);
 
100
    }
 
101
 
 
102
 
 
103
    /**
 
104
     * Return the first registered implementation that has the desired
 
105
     * features, or null if none is found.
 
106
     *
 
107
     * @param features A string that specifies which features are required.
 
108
     *                 This is a space separated list in which each feature is
 
109
     *                 specified by its name optionally followed by a space
 
110
     *                 and a version number.
 
111
     *                 This is something like: "XML 1.0 Traversal +Events 2.0"
 
112
     * @return An implementation that has the desired features, or
 
113
     *   <code>null</code> if this source has none.
 
114
     */
 
115
    public DOMImplementation getDOMImplementation(String features)
 
116
            throws ClassNotFoundException,
 
117
            InstantiationException, IllegalAccessException, ClassCastException
 
118
    {
 
119
        int    size  = _sources.size();
 
120
        String name  = null;
 
121
        for (int i = 0; i < size; i++) {
 
122
            DOMImplementationSource source =
 
123
                (DOMImplementationSource) _sources.get(i);
 
124
 
 
125
            DOMImplementation impl = source.getDOMImplementation(features);
 
126
            if (impl != null) {
 
127
                return impl;
 
128
            }       
 
129
        }
 
130
        return null;
 
131
    }
 
132
 
 
133
    /**
 
134
     * Return the list of all registered implementation that support the desired
 
135
     * features.
 
136
     *
 
137
     * @param features A string that specifies which features are required.
 
138
     *                 This is a space separated list in which each feature is
 
139
     *                 specified by its name optionally followed by a space
 
140
     *                 and a version number.
 
141
     *                 This is something like: "XML 1.0 Traversal +Events 2.0"
 
142
     * @return A list of DOMImplementations that support the desired features.
 
143
     */
 
144
    public DOMImplementationList getDOMImplementationList(String features)
 
145
            throws ClassNotFoundException,
 
146
            InstantiationException, IllegalAccessException, ClassCastException
 
147
    {
 
148
        int    size  = _sources.size();
 
149
        DOMImplementationListImpl list = new DOMImplementationListImpl();
 
150
        String name = null;
 
151
        for (int i = 0; i < size; i++) {
 
152
            DOMImplementationSource source =
 
153
                (DOMImplementationSource) _sources.get(i);
 
154
 
 
155
            DOMImplementationList impls =
 
156
                 source.getDOMImplementationList(features);
 
157
            for (int j = 0; j < impls.getLength(); j++) {
 
158
                list.add(impls.item(j));
 
159
            }
 
160
        }
 
161
        return list;
 
162
    }
 
163
 
 
164
    /**
 
165
     * Register an implementation.
 
166
     */
 
167
    public void addSource(DOMImplementationSource s)
 
168
            throws ClassNotFoundException,
 
169
            InstantiationException, IllegalAccessException
 
170
    {
 
171
        _sources.add(s);
 
172
    }
 
173
 
 
174
    private static Class getClass (String className)
 
175
                throws ClassNotFoundException, IllegalAccessException,
 
176
                InstantiationException {
 
177
        Method m = null;
 
178
        ClassLoader cl = null;
 
179
 
 
180
        try {
 
181
            m = Thread.class.getMethod("getContextClassLoader", null);
 
182
        } catch (NoSuchMethodException e) {
 
183
            // Assume that we are running JDK 1.1, use the current ClassLoader
 
184
            cl = DOMImplementationRegistry.class.getClassLoader();
 
185
        }
 
186
 
 
187
        if (cl == null ) {
 
188
            try {
 
189
                cl = (ClassLoader) m.invoke(Thread.currentThread(), null);
 
190
            } catch (IllegalAccessException e) {
 
191
                // assert(false)
 
192
                throw new UnknownError(e.getMessage());
 
193
            } catch (InvocationTargetException e) {
 
194
                // assert(e.getTargetException() instanceof SecurityException)
 
195
                throw new UnknownError(e.getMessage());
 
196
            }
 
197
        }
 
198
        if (cl == null) { 
 
199
            // fall back to Class.forName
 
200
            return Class.forName(className);
 
201
        }
 
202
        try { 
 
203
            return cl.loadClass(className);
 
204
        } catch (ClassNotFoundException e) {
 
205
            return Class.forName(className);
 
206
        }
 
207
    }
 
208
}
 
209
  
 
 
b'\\ No newline at end of file'