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

« back to all changes in this revision

Viewing changes to src/org/apache/xerces/dom3/bootstrap/DOMImplementationRegistry.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-12-04 17:37:55 UTC
  • mfrom: (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20061204173755-hb6ybrrrk097zhx7
Tags: 2.8.1-1ubuntu1
* Merge with Debian unstable; remaining changes:
  - Build -gcj package.

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.apache.xerces.dom3.bootstrap;
41
 
 
42
 
import java.lang.reflect.Method;
43
 
import java.lang.reflect.InvocationTargetException;
44
 
import java.util.StringTokenizer;
45
 
import java.util.Vector;
46
 
 
47
 
import org.apache.xerces.dom3.DOMImplementationSource;
48
 
import org.apache.xerces.dom3.DOMImplementationList;
49
 
import org.w3c.dom.DOMImplementation;
50
 
 
51
 
public class DOMImplementationRegistry { 
52
 
 
53
 
    // The system property to specify the DOMImplementationSource class names.
54
 
    public final static String PROPERTY =
55
 
        "org.w3c.dom.DOMImplementationSourceList";
56
 
 
57
 
    private Vector _sources;
58
 
 
59
 
    // deny construction by other classes
60
 
    private DOMImplementationRegistry() {
61
 
    }
62
 
 
63
 
    // deny construction by other classes
64
 
    private DOMImplementationRegistry(Vector srcs) {
65
 
        _sources = srcs;
66
 
    }
67
 
 
68
 
 
69
 
    /* 
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
77
 
     */ 
78
 
    public static DOMImplementationRegistry newInstance()               
79
 
            throws ClassNotFoundException, InstantiationException, 
80
 
            IllegalAccessException
81
 
    {
82
 
        Vector _sources = new Vector();    
83
 
 
84
 
        // fetch system property:
85
 
        String p = System.getProperty(PROPERTY);
86
 
        if (p != null) {
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();
93
 
                _sources.add(source);
94
 
            }
95
 
        }
96
 
        return new DOMImplementationRegistry(_sources);
97
 
    }
98
 
 
99
 
 
100
 
    /**
101
 
     * Return the first registered implementation that has the desired
102
 
     * features, or null if none is found.
103
 
     *
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.
111
 
     */
112
 
    public DOMImplementation getDOMImplementation(String features)
113
 
            throws ClassNotFoundException,
114
 
            InstantiationException, IllegalAccessException, ClassCastException
115
 
    {
116
 
        int    size  = _sources.size();
117
 
        String name  = null;
118
 
        for (int i = 0; i < size; i++) {
119
 
            DOMImplementationSource source =
120
 
                (DOMImplementationSource) _sources.get(i);
121
 
 
122
 
            DOMImplementation impl = source.getDOMImplementation(features);
123
 
            if (impl != null) {
124
 
                return impl;
125
 
            }       
126
 
        }
127
 
        return null;
128
 
    }
129
 
 
130
 
    /**
131
 
     * Return the list of all registered implementation that support the desired
132
 
     * features.
133
 
     *
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.
140
 
     */
141
 
    public DOMImplementationList getDOMImplementationList(String features)
142
 
            throws ClassNotFoundException,
143
 
            InstantiationException, IllegalAccessException, ClassCastException
144
 
    {
145
 
        int    size  = _sources.size();
146
 
        DOMImplementationListImpl list = new DOMImplementationListImpl();
147
 
        String name = null;
148
 
        for (int i = 0; i < size; i++) {
149
 
            DOMImplementationSource source =
150
 
                (DOMImplementationSource) _sources.get(i);
151
 
 
152
 
            DOMImplementationList impls =
153
 
                 source.getDOMImplementationList(features);
154
 
            for (int j = 0; j < impls.getLength(); j++) {
155
 
                list.add(impls.item(j));
156
 
            }
157
 
        }
158
 
        return list;
159
 
    }
160
 
 
161
 
    /**
162
 
     * Register an implementation.
163
 
     */
164
 
    public void addSource(DOMImplementationSource s)
165
 
            throws ClassNotFoundException,
166
 
            InstantiationException, IllegalAccessException
167
 
    {
168
 
        _sources.add(s);
169
 
    }
170
 
 
171
 
    private static Class getClass (String className)
172
 
                throws ClassNotFoundException, IllegalAccessException,
173
 
                InstantiationException {
174
 
        Method m = null;
175
 
        ClassLoader cl = null;
176
 
 
177
 
        try {
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();
182
 
        }
183
 
 
184
 
        if (cl == null ) {
185
 
            try {
186
 
                cl = (ClassLoader) m.invoke(Thread.currentThread(), null);
187
 
            } catch (IllegalAccessException e) {
188
 
                // assert(false)
189
 
                throw new UnknownError(e.getMessage());
190
 
            } catch (InvocationTargetException e) {
191
 
                // assert(e.getTargetException() instanceof SecurityException)
192
 
                throw new UnknownError(e.getMessage());
193
 
            }
194
 
        }
195
 
        if (cl == null) { 
196
 
            // fall back to Class.forName
197
 
            return Class.forName(className);
198
 
        }
199
 
        try { 
200
 
            return cl.loadClass(className);
201
 
        } catch (ClassNotFoundException e) {
202
 
            return Class.forName(className);
203
 
        }
204
 
    }
205
 
}
206
 
  
 
 
b'\\ No newline at end of file'