286N/A/*
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/A/*
286N/A * This file is available under and governed by the GNU General Public
286N/A * License version 2 only, as published by the Free Software Foundation.
286N/A * However, the following notice accompanied the original version of this
286N/A * file and, per its terms, should not be removed:
286N/A *
286N/A * Copyright (c) 2004 World Wide Web Consortium,
286N/A *
286N/A * (Massachusetts Institute of Technology, European Research Consortium for
286N/A * Informatics and Mathematics, Keio University). All Rights Reserved. This
286N/A * work is distributed under the W3C(r) Software License [1] in the hope that
286N/A * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
286N/A * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
286N/A *
286N/A * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
286N/A */
286N/A
286N/A
286N/Apackage org.w3c.dom.bootstrap;
286N/A
286N/Aimport java.util.StringTokenizer;
286N/Aimport java.util.Vector;
286N/Aimport org.w3c.dom.DOMImplementationSource;
286N/Aimport org.w3c.dom.DOMImplementationList;
286N/Aimport org.w3c.dom.DOMImplementation;
286N/Aimport java.io.InputStream;
286N/Aimport java.io.BufferedReader;
286N/Aimport java.io.InputStreamReader;
286N/Aimport java.security.AccessController;
286N/Aimport java.security.PrivilegedAction;
286N/A
286N/A/**
286N/A * A factory that enables applications to obtain instances of
286N/A * <code>DOMImplementation</code>.
286N/A *
286N/A * <p>
286N/A * Example:
286N/A * </p>
286N/A *
286N/A * <pre class='example'>
286N/A * // get an instance of the DOMImplementation registry
286N/A * DOMImplementationRegistry registry =
286N/A * DOMImplementationRegistry.newInstance();
286N/A * // get a DOM implementation the Level 3 XML module
286N/A * DOMImplementation domImpl =
286N/A * registry.getDOMImplementation("XML 3.0");
286N/A * </pre>
286N/A *
286N/A * <p>
286N/A * This provides an application with an implementation-independent starting
286N/A * point. DOM implementations may modify this class to meet new security
286N/A * standards or to provide *additional* fallbacks for the list of
286N/A * DOMImplementationSources.
286N/A * </p>
286N/A *
286N/A * @see DOMImplementation
286N/A * @see DOMImplementationSource
286N/A * @since DOM Level 3
286N/A */
286N/Apublic final class DOMImplementationRegistry {
286N/A /**
286N/A * The system property to specify the
286N/A * DOMImplementationSource class names.
286N/A */
286N/A public static final String PROPERTY =
286N/A "org.w3c.dom.DOMImplementationSourceList";
286N/A
286N/A /**
286N/A * Default columns per line.
286N/A */
286N/A private static final int DEFAULT_LINE_LENGTH = 80;
286N/A
286N/A /**
286N/A * The list of DOMImplementationSources.
286N/A */
286N/A private Vector sources;
286N/A
286N/A /**
286N/A * Default class name.
286N/A */
286N/A private static final String FALLBACK_CLASS =
286N/A "com.sun.org.apache.xerces.internal.dom.DOMXSImplementationSourceImpl";
524N/A private static final String DEFAULT_PACKAGE =
524N/A "com.sun.org.apache.xerces.internal.dom";
286N/A /**
286N/A * Private constructor.
286N/A * @param srcs Vector List of DOMImplementationSources
286N/A */
286N/A private DOMImplementationRegistry(final Vector srcs) {
286N/A sources = srcs;
286N/A }
286N/A
286N/A /**
286N/A * Obtain a new instance of a <code>DOMImplementationRegistry</code>.
286N/A *
286N/A
286N/A * The <code>DOMImplementationRegistry</code> is initialized by the
286N/A * application or the implementation, depending on the context, by
286N/A * first checking the value of the Java system property
286N/A * <code>org.w3c.dom.DOMImplementationSourceList</code> and
286N/A * the service provider whose contents are at
286N/A * "<code>META_INF/services/org.w3c.dom.DOMImplementationSourceList</code>".
286N/A * The value of this property is a white-space separated list of
286N/A * names of availables classes implementing the
286N/A * <code>DOMImplementationSource</code> interface. Each class listed
286N/A * in the class name list is instantiated and any exceptions
286N/A * encountered are thrown to the application.
286N/A *
286N/A * @return an initialized instance of DOMImplementationRegistry
286N/A * @throws ClassNotFoundException
286N/A * If any specified class can not be found
286N/A * @throws InstantiationException
286N/A * If any specified class is an interface or abstract class
286N/A * @throws IllegalAccessException
286N/A * If the default constructor of a specified class is not accessible
286N/A * @throws ClassCastException
286N/A * If any specified class does not implement
286N/A * <code>DOMImplementationSource</code>
286N/A */
286N/A public static DOMImplementationRegistry newInstance()
286N/A throws
286N/A ClassNotFoundException,
286N/A InstantiationException,
286N/A IllegalAccessException,
286N/A ClassCastException {
286N/A Vector sources = new Vector();
286N/A
286N/A ClassLoader classLoader = getClassLoader();
286N/A // fetch system property:
286N/A String p = getSystemProperty(PROPERTY);
286N/A
286N/A //
286N/A // if property is not specified then use contents of
286N/A // META_INF/org.w3c.dom.DOMImplementationSourceList from classpath
286N/A if (p == null) {
286N/A p = getServiceValue(classLoader);
286N/A }
286N/A if (p == null) {
286N/A //
286N/A // DOM Implementations can modify here to add *additional* fallback
286N/A // mechanisms to access a list of default DOMImplementationSources.
286N/A //fall back to JAXP implementation class com.sun.org.apache.xerces.internal.dom.DOMXSImplementationSourceImpl
286N/A p = FALLBACK_CLASS;
286N/A }
286N/A if (p != null) {
286N/A StringTokenizer st = new StringTokenizer(p);
286N/A while (st.hasMoreTokens()) {
286N/A String sourceName = st.nextToken();
524N/A // make sure we have access to restricted packages
524N/A boolean internal = false;
524N/A if (System.getSecurityManager() != null) {
524N/A if (sourceName != null && sourceName.startsWith(DEFAULT_PACKAGE)) {
524N/A internal = true;
524N/A }
524N/A }
286N/A Class sourceClass = null;
524N/A if (classLoader != null && !internal) {
286N/A sourceClass = classLoader.loadClass(sourceName);
286N/A } else {
286N/A sourceClass = Class.forName(sourceName);
286N/A }
286N/A DOMImplementationSource source =
286N/A (DOMImplementationSource) sourceClass.newInstance();
286N/A sources.addElement(source);
286N/A }
286N/A }
286N/A return new DOMImplementationRegistry(sources);
286N/A }
286N/A
286N/A /**
286N/A * Return the first implementation that has the desired
286N/A * features, or <code>null</code> if none is found.
286N/A *
286N/A * @param features
286N/A * A string that specifies which features are required. This is
286N/A * a space separated list in which each feature is specified by
286N/A * its name optionally followed by a space and a version number.
286N/A * This is something like: "XML 1.0 Traversal +Events 2.0"
286N/A * @return An implementation that has the desired features,
286N/A * or <code>null</code> if none found.
286N/A */
286N/A public DOMImplementation getDOMImplementation(final String features) {
286N/A int size = sources.size();
286N/A String name = null;
286N/A for (int i = 0; i < size; i++) {
286N/A DOMImplementationSource source =
286N/A (DOMImplementationSource) sources.elementAt(i);
286N/A DOMImplementation impl = source.getDOMImplementation(features);
286N/A if (impl != null) {
286N/A return impl;
286N/A }
286N/A }
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * Return a list of implementations that support the
286N/A * desired features.
286N/A *
286N/A * @param features
286N/A * A string that specifies which features are required. This is
286N/A * a space separated list in which each feature is specified by
286N/A * its name optionally followed by a space and a version number.
286N/A * This is something like: "XML 1.0 Traversal +Events 2.0"
286N/A * @return A list of DOMImplementations that support the desired features.
286N/A */
286N/A public DOMImplementationList getDOMImplementationList(final String features) {
286N/A final Vector implementations = new Vector();
286N/A int size = sources.size();
286N/A for (int i = 0; i < size; i++) {
286N/A DOMImplementationSource source =
286N/A (DOMImplementationSource) sources.elementAt(i);
286N/A DOMImplementationList impls =
286N/A source.getDOMImplementationList(features);
286N/A for (int j = 0; j < impls.getLength(); j++) {
286N/A DOMImplementation impl = impls.item(j);
286N/A implementations.addElement(impl);
286N/A }
286N/A }
286N/A return new DOMImplementationList() {
286N/A public DOMImplementation item(final int index) {
286N/A if (index >= 0 && index < implementations.size()) {
286N/A try {
286N/A return (DOMImplementation)
286N/A implementations.elementAt(index);
286N/A } catch (ArrayIndexOutOfBoundsException e) {
286N/A return null;
286N/A }
286N/A }
286N/A return null;
286N/A }
286N/A
286N/A public int getLength() {
286N/A return implementations.size();
286N/A }
286N/A };
286N/A }
286N/A
286N/A /**
286N/A * Register an implementation.
286N/A *
286N/A * @param s The source to be registered, may not be <code>null</code>
286N/A */
286N/A public void addSource(final DOMImplementationSource s) {
286N/A if (s == null) {
286N/A throw new NullPointerException();
286N/A }
286N/A if (!sources.contains(s)) {
286N/A sources.addElement(s);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A *
286N/A * Gets a class loader.
286N/A *
286N/A * @return A class loader, possibly <code>null</code>
286N/A */
286N/A private static ClassLoader getClassLoader() {
286N/A try {
286N/A ClassLoader contextClassLoader = getContextClassLoader();
286N/A
286N/A if (contextClassLoader != null) {
286N/A return contextClassLoader;
286N/A }
286N/A } catch (Exception e) {
286N/A // Assume that the DOM application is in a JRE 1.1, use the
286N/A // current ClassLoader
286N/A return DOMImplementationRegistry.class.getClassLoader();
286N/A }
286N/A return DOMImplementationRegistry.class.getClassLoader();
286N/A }
286N/A
286N/A /**
286N/A * This method attempts to return the first line of the resource
286N/A * META_INF/services/org.w3c.dom.DOMImplementationSourceList
286N/A * from the provided ClassLoader.
286N/A *
286N/A * @param classLoader classLoader, may not be <code>null</code>.
286N/A * @return first line of resource, or <code>null</code>
286N/A */
286N/A private static String getServiceValue(final ClassLoader classLoader) {
286N/A String serviceId = "META-INF/services/" + PROPERTY;
286N/A // try to find services in CLASSPATH
286N/A try {
286N/A InputStream is = getResourceAsStream(classLoader, serviceId);
286N/A
286N/A if (is != null) {
286N/A BufferedReader rd;
286N/A try {
286N/A rd =
286N/A new BufferedReader(new InputStreamReader(is, "UTF-8"),
286N/A DEFAULT_LINE_LENGTH);
286N/A } catch (java.io.UnsupportedEncodingException e) {
286N/A rd =
286N/A new BufferedReader(new InputStreamReader(is),
286N/A DEFAULT_LINE_LENGTH);
286N/A }
286N/A String serviceValue = rd.readLine();
286N/A rd.close();
286N/A if (serviceValue != null && serviceValue.length() > 0) {
286N/A return serviceValue;
286N/A }
286N/A }
286N/A } catch (Exception ex) {
286N/A return null;
286N/A }
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * A simple JRE (Java Runtime Environment) 1.1 test
286N/A *
286N/A * @return <code>true</code> if JRE 1.1
286N/A */
286N/A private static boolean isJRE11() {
286N/A try {
286N/A Class c = Class.forName("java.security.AccessController");
286N/A // java.security.AccessController existed since 1.2 so, if no
286N/A // exception was thrown, the DOM application is running in a JRE
286N/A // 1.2 or higher
286N/A return false;
286N/A } catch (Exception ex) {
286N/A // ignore
286N/A }
286N/A return true;
286N/A }
286N/A
286N/A /**
286N/A * This method returns the ContextClassLoader or <code>null</code> if
286N/A * running in a JRE 1.1
286N/A *
286N/A * @return The Context Classloader
286N/A */
286N/A private static ClassLoader getContextClassLoader() {
286N/A return isJRE11()
286N/A ? null
286N/A : (ClassLoader)
286N/A AccessController.doPrivileged(new PrivilegedAction() {
286N/A public Object run() {
286N/A ClassLoader classLoader = null;
286N/A try {
286N/A classLoader =
286N/A Thread.currentThread().getContextClassLoader();
286N/A } catch (SecurityException ex) {
286N/A }
286N/A return classLoader;
286N/A }
286N/A });
286N/A }
286N/A
286N/A /**
286N/A * This method returns the system property indicated by the specified name
286N/A * after checking access control privileges. For a JRE 1.1, this check is
286N/A * not done.
286N/A *
286N/A * @param name the name of the system property
286N/A * @return the system property
286N/A */
286N/A private static String getSystemProperty(final String name) {
286N/A return isJRE11()
286N/A ? (String) System.getProperty(name)
286N/A : (String) AccessController.doPrivileged(new PrivilegedAction() {
286N/A public Object run() {
286N/A return System.getProperty(name);
286N/A }
286N/A });
286N/A }
286N/A
286N/A /**
286N/A * This method returns an Inputstream for the reading resource
286N/A * META_INF/services/org.w3c.dom.DOMImplementationSourceList after checking
286N/A * access control privileges. For a JRE 1.1, this check is not done.
286N/A *
286N/A * @param classLoader classLoader
286N/A * @param name the resource
286N/A * @return an Inputstream for the resource specified
286N/A */
286N/A private static InputStream getResourceAsStream(final ClassLoader classLoader,
286N/A final String name) {
286N/A if (isJRE11()) {
286N/A InputStream ris;
286N/A if (classLoader == null) {
286N/A ris = ClassLoader.getSystemResourceAsStream(name);
286N/A } else {
286N/A ris = classLoader.getResourceAsStream(name);
286N/A }
286N/A return ris;
286N/A } else {
286N/A return (InputStream)
286N/A AccessController.doPrivileged(new PrivilegedAction() {
286N/A public Object run() {
286N/A InputStream ris;
286N/A if (classLoader == null) {
286N/A ris =
286N/A ClassLoader.getSystemResourceAsStream(name);
286N/A } else {
286N/A ris = classLoader.getResourceAsStream(name);
286N/A }
286N/A return ris;
286N/A }
286N/A });
286N/A }
286N/A }
286N/A}