001 /*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
003 *
004 * This code is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU General Public License version 2 only, as
006 * published by the Free Software Foundation. Sun designates this
007 * particular file as subject to the "Classpath" exception as provided
008 * by Sun in the LICENSE file that accompanied this code.
009 *
010 * This code is distributed in the hope that it will be useful, but WITHOUT
011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013 * version 2 for more details (a copy is included in the LICENSE file that
014 * accompanied this code).
015 *
016 * You should have received a copy of the GNU General Public License version
017 * 2 along with this work; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
019 *
020 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
021 * CA 95054 USA or visit www.sun.com if you need additional information or
022 * have any questions.
023 */
024
025 /*
026 * This file is available under and governed by the GNU General Public
027 * License version 2 only, as published by the Free Software Foundation.
028 * However, the following notice accompanied the original version of this
029 * file and, per its terms, should not be removed:
030 *
031 * Copyright (c) 2004 World Wide Web Consortium,
032 *
033 * (Massachusetts Institute of Technology, European Research Consortium for
034 * Informatics and Mathematics, Keio University). All Rights Reserved. This
035 * work is distributed under the W3C(r) Software License [1] in the hope that
036 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
037 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
038 *
039 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
040 */
041
042 package org.w3c.dom.bootstrap;
043
044 import java.util.StringTokenizer;
045 import java.util.Vector;
046 import org.w3c.dom.DOMImplementationSource;
047 import org.w3c.dom.DOMImplementationList;
048 import org.w3c.dom.DOMImplementation;
049 import java.io.InputStream;
050 import java.io.BufferedReader;
051 import java.io.InputStreamReader;
052 import java.security.AccessController;
053 import java.security.PrivilegedAction;
054
055 /**
056 * A factory that enables applications to obtain instances of
057 * <code>DOMImplementation</code>.
058 *
059 * <p>
060 * Example:
061 * </p>
062 *
063 * <pre class='example'>
064 * // get an instance of the DOMImplementation registry
065 * DOMImplementationRegistry registry =
066 * DOMImplementationRegistry.newInstance();
067 * // get a DOM implementation the Level 3 XML module
068 * DOMImplementation domImpl =
069 * registry.getDOMImplementation("XML 3.0");
070 * </pre>
071 *
072 * <p>
073 * This provides an application with an implementation-independent starting
074 * point. DOM implementations may modify this class to meet new security
075 * standards or to provide *additional* fallbacks for the list of
076 * DOMImplementationSources.
077 * </p>
078 *
079 * @see DOMImplementation
080 * @see DOMImplementationSource
081 * @since DOM Level 3
082 */
083 public final class DOMImplementationRegistry {
084 /**
085 * The system property to specify the
086 * DOMImplementationSource class names.
087 */
088 public static final String PROPERTY = "org.w3c.dom.DOMImplementationSourceList";
089
090 /**
091 * Default columns per line.
092 */
093 private static final int DEFAULT_LINE_LENGTH = 80;
094
095 /**
096 * The list of DOMImplementationSources.
097 */
098 private Vector sources;
099
100 /**
101 * Default class name.
102 */
103 private static final String FALLBACK_CLASS = "com.sun.org.apache.xerces.internal.dom.DOMXSImplementationSourceImpl";
104
105 /**
106 * Private constructor.
107 * @param srcs Vector List of DOMImplementationSources
108 */
109 private DOMImplementationRegistry(final Vector srcs) {
110 sources = srcs;
111 }
112
113 /**
114 * Obtain a new instance of a <code>DOMImplementationRegistry</code>.
115 *
116
117 * The <code>DOMImplementationRegistry</code> is initialized by the
118 * application or the implementation, depending on the context, by
119 * first checking the value of the Java system property
120 * <code>org.w3c.dom.DOMImplementationSourceList</code> and
121 * the the service provider whose contents are at
122 * "<code>META_INF/services/org.w3c.dom.DOMImplementationSourceList</code>"
123 * The value of this property is a white-space separated list of
124 * names of availables classes implementing the
125 * <code>DOMImplementationSource</code> interface. Each class listed
126 * in the class name list is instantiated and any exceptions
127 * encountered are thrown to the application.
128 *
129 * @return an initialized instance of DOMImplementationRegistry
130 * @throws ClassNotFoundException
131 * If any specified class can not be found
132 * @throws InstantiationException
133 * If any specified class is an interface or abstract class
134 * @throws IllegalAccessException
135 * If the default constructor of a specified class is not accessible
136 * @throws ClassCastException
137 * If any specified class does not implement
138 * <code>DOMImplementationSource</code>
139 */
140 public static DOMImplementationRegistry newInstance()
141 throws ClassNotFoundException, InstantiationException,
142 IllegalAccessException, ClassCastException {
143 Vector sources = new Vector();
144
145 ClassLoader classLoader = getClassLoader();
146 // fetch system property:
147 String p = getSystemProperty(PROPERTY);
148
149 //
150 // if property is not specified then use contents of
151 // META_INF/org.w3c.dom.DOMImplementationSourceList from classpath
152 if (p == null) {
153 p = getServiceValue(classLoader);
154 }
155 if (p == null) {
156 //
157 // DOM Implementations can modify here to add *additional* fallback
158 // mechanisms to access a list of default DOMImplementationSources.
159 //fall back to JAXP implementation class com.sun.org.apache.xerces.internal.dom.DOMXSImplementationSourceImpl
160 p = FALLBACK_CLASS;
161 }
162 if (p != null) {
163 StringTokenizer st = new StringTokenizer(p);
164 while (st.hasMoreTokens()) {
165 String sourceName = st.nextToken();
166 // Use context class loader, falling back to Class.forName
167 // if and only if this fails...
168 Class sourceClass = null;
169 if (classLoader != null) {
170 sourceClass = classLoader.loadClass(sourceName);
171 } else {
172 sourceClass = Class.forName(sourceName);
173 }
174 DOMImplementationSource source = (DOMImplementationSource) sourceClass
175 .newInstance();
176 sources.addElement(source);
177 }
178 }
179 return new DOMImplementationRegistry(sources);
180 }
181
182 /**
183 * Return the first implementation that has the desired
184 * features, or <code>null</code> if none is found.
185 *
186 * @param features
187 * A string that specifies which features are required. This is
188 * a space separated list in which each feature is specified by
189 * its name optionally followed by a space and a version number.
190 * This is something like: "XML 1.0 Traversal +Events 2.0"
191 * @return An implementation that has the desired features,
192 * or <code>null</code> if none found.
193 */
194 public DOMImplementation getDOMImplementation(final String features) {
195 int size = sources.size();
196 String name = null;
197 for (int i = 0; i < size; i++) {
198 DOMImplementationSource source = (DOMImplementationSource) sources
199 .elementAt(i);
200 DOMImplementation impl = source
201 .getDOMImplementation(features);
202 if (impl != null) {
203 return impl;
204 }
205 }
206 return null;
207 }
208
209 /**
210 * Return a list of implementations that support the
211 * desired features.
212 *
213 * @param features
214 * A string that specifies which features are required. This is
215 * a space separated list in which each feature is specified by
216 * its name optionally followed by a space and a version number.
217 * This is something like: "XML 1.0 Traversal +Events 2.0"
218 * @return A list of DOMImplementations that support the desired features.
219 */
220 public DOMImplementationList getDOMImplementationList(
221 final String features) {
222 final Vector implementations = new Vector();
223 int size = sources.size();
224 for (int i = 0; i < size; i++) {
225 DOMImplementationSource source = (DOMImplementationSource) sources
226 .elementAt(i);
227 DOMImplementationList impls = source
228 .getDOMImplementationList(features);
229 for (int j = 0; j < impls.getLength(); j++) {
230 DOMImplementation impl = impls.item(j);
231 implementations.addElement(impl);
232 }
233 }
234 return new DOMImplementationList() {
235 public DOMImplementation item(final int index) {
236 if (index >= 0 && index < implementations.size()) {
237 try {
238 return (DOMImplementation) implementations
239 .elementAt(index);
240 } catch (ArrayIndexOutOfBoundsException e) {
241 return null;
242 }
243 }
244 return null;
245 }
246
247 public int getLength() {
248 return implementations.size();
249 }
250 };
251 }
252
253 /**
254 * Register an implementation.
255 *
256 * @param s The source to be registered, may not be <code>null</code>
257 */
258 public void addSource(final DOMImplementationSource s) {
259 if (s == null) {
260 throw new NullPointerException();
261 }
262 if (!sources.contains(s)) {
263 sources.addElement(s);
264 }
265 }
266
267 /**
268 *
269 * Gets a class loader.
270 *
271 * @return A class loader, possibly <code>null</code>
272 */
273 private static ClassLoader getClassLoader() {
274 try {
275 ClassLoader contextClassLoader = getContextClassLoader();
276
277 if (contextClassLoader != null) {
278 return contextClassLoader;
279 }
280 } catch (Exception e) {
281 // Assume that the DOM application is in a JRE 1.1, use the
282 // current ClassLoader
283 return DOMImplementationRegistry.class.getClassLoader();
284 }
285 return DOMImplementationRegistry.class.getClassLoader();
286 }
287
288 /**
289 * This method attempts to return the first line of the resource
290 * META_INF/services/org.w3c.dom.DOMImplementationSourceList
291 * from the provided ClassLoader.
292 *
293 * @param classLoader classLoader, may not be <code>null</code>.
294 * @return first line of resource, or <code>null</code>
295 */
296 private static String getServiceValue(final ClassLoader classLoader) {
297 String serviceId = "META-INF/services/" + PROPERTY;
298 // try to find services in CLASSPATH
299 try {
300 InputStream is = getResourceAsStream(classLoader, serviceId);
301
302 if (is != null) {
303 BufferedReader rd;
304 try {
305 rd = new BufferedReader(new InputStreamReader(is,
306 "UTF-8"), DEFAULT_LINE_LENGTH);
307 } catch (java.io.UnsupportedEncodingException e) {
308 rd = new BufferedReader(new InputStreamReader(is),
309 DEFAULT_LINE_LENGTH);
310 }
311 String serviceValue = rd.readLine();
312 rd.close();
313 if (serviceValue != null && serviceValue.length() > 0) {
314 return serviceValue;
315 }
316 }
317 } catch (Exception ex) {
318 return null;
319 }
320 return null;
321 }
322
323 /**
324 * A simple JRE (Java Runtime Environment) 1.1 test
325 *
326 * @return <code>true</code> if JRE 1.1
327 */
328 private static boolean isJRE11() {
329 try {
330 Class c = Class.forName("java.security.AccessController");
331 // java.security.AccessController existed since 1.2 so, if no
332 // exception was thrown, the DOM application is running in a JRE
333 // 1.2 or higher
334 return false;
335 } catch (Exception ex) {
336 // ignore
337 }
338 return true;
339 }
340
341 /**
342 * This method returns the ContextClassLoader or <code>null</code> if
343 * running in a JRE 1.1
344 *
345 * @return The Context Classloader
346 */
347 private static ClassLoader getContextClassLoader() {
348 return isJRE11() ? null : (ClassLoader) AccessController
349 .doPrivileged(new PrivilegedAction() {
350 public Object run() {
351 ClassLoader classLoader = null;
352 try {
353 classLoader = Thread.currentThread()
354 .getContextClassLoader();
355 } catch (SecurityException ex) {
356 }
357 return classLoader;
358 }
359 });
360 }
361
362 /**
363 * This method returns the system property indicated by the specified name
364 * after checking access control privileges. For a JRE 1.1, this check is
365 * not done.
366 *
367 * @param name the name of the system property
368 * @return the system property
369 */
370 private static String getSystemProperty(final String name) {
371 return isJRE11() ? (String) System.getProperty(name)
372 : (String) AccessController
373 .doPrivileged(new PrivilegedAction() {
374 public Object run() {
375 return System.getProperty(name);
376 }
377 });
378 }
379
380 /**
381 * This method returns an Inputstream for the reading resource
382 * META_INF/services/org.w3c.dom.DOMImplementationSourceList after checking
383 * access control privileges. For a JRE 1.1, this check is not done.
384 *
385 * @param classLoader classLoader
386 * @param name the resource
387 * @return an Inputstream for the resource specified
388 */
389 private static InputStream getResourceAsStream(
390 final ClassLoader classLoader, final String name) {
391 if (isJRE11()) {
392 InputStream ris;
393 if (classLoader == null) {
394 ris = ClassLoader.getSystemResourceAsStream(name);
395 } else {
396 ris = classLoader.getResourceAsStream(name);
397 }
398 return ris;
399 } else {
400 return (InputStream) AccessController
401 .doPrivileged(new PrivilegedAction() {
402 public Object run() {
403 InputStream ris;
404 if (classLoader == null) {
405 ris = ClassLoader
406 .getSystemResourceAsStream(name);
407 } else {
408 ris = classLoader
409 .getResourceAsStream(name);
410 }
411 return ris;
412 }
413 });
414 }
415 }
416 }
|