/* * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package org.xml.sax.helpers; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Iterator; import java.util.Objects; import java.util.ServiceConfigurationError; import java.util.ServiceLoader; import jdk.xml.internal.SecuritySupport; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; /** * Factory for creating an XML reader. * *
This class contains static methods for creating an XML reader * from an explicit class name, or based on runtime defaults: * *
* try { * XMLReader myReader = XMLReaderFactory.createXMLReader(); * } catch (SAXException e) { * System.err.println(e.getMessage()); * } ** *
Note to Distributions bundled with parsers: * You should modify the implementation of the no-arguments * createXMLReader to handle cases where the external * configuration mechanisms aren't set up. That method should do its * best to return a parser when one is in the class path, even when * nothing bound its class name to {@code org.xml.sax.driver} so * those configuration mechanisms would see it. * * @since 1.4, SAX 2.0 * @author David Megginson, David Brownell * @version 2.0.1 (sax2r2) * * @deprecated It is recommended to use {@link javax.xml.parsers.SAXParserFactory} * instead. */ @Deprecated(since="9") final public class XMLReaderFactory { /** * Private constructor. * *
This constructor prevents the class from being instantiated. */ private XMLReaderFactory () { } private static final String property = "org.xml.sax.driver"; /** * Obtains a new instance of a {@link org.xml.sax.XMLReader}. * This method uses the following ordered lookup procedure to find and load * the {@link org.xml.sax.XMLReader} implementation class: *
* Otherwise, the system-default implementation is returned. *
Given a class name, this method attempts to load * and instantiate the class as an XML reader. * *
Note that this method will not be usable in environments where
* the caller (perhaps an applet) is not permitted to load classes
* dynamically.
*
* @return A new XML reader.
* @exception org.xml.sax.SAXException If the class cannot be
* loaded, instantiated, and cast to XMLReader.
* @see #createXMLReader()
*/
public static XMLReader createXMLReader (String className)
throws SAXException
{
return loadClass (SecuritySupport.getClassLoader(), className);
}
private static XMLReader loadClass (ClassLoader loader, String className)
throws SAXException
{
try {
return NewInstance.newInstance (XMLReader.class, loader, className);
} catch (ClassNotFoundException e1) {
throw new SAXException("SAX2 driver class " + className +
" not found", e1);
} catch (IllegalAccessException e2) {
throw new SAXException("SAX2 driver class " + className +
" found but cannot be loaded", e2);
} catch (InstantiationException e3) {
throw new SAXException("SAX2 driver class " + className +
" loaded but cannot be instantiated (no empty public constructor?)",
e3);
} catch (ClassCastException e4) {
throw new SAXException("SAX2 driver class " + className +
" does not implement XMLReader", e4);
}
}
/**
* Locates a provider by directly reading the jar service file.
* @param loader the ClassLoader to be used to read the service file
* @return the name of the provider, or null if nothing is found
*/
private static String jarLookup(final ClassLoader loader) {
final ClassLoader cl = Objects.requireNonNull(loader);
String clsFromJar = null;
String service = "META-INF/services/" + property;
InputStream in;
BufferedReader reader;
try {
in = SecuritySupport.getResourceAsStream(cl, service);
// If no provider found then try the current ClassLoader
if (in == null) {
in = SecuritySupport.getResourceAsStream(null, service);
}
if (in != null) {
reader = new BufferedReader (new InputStreamReader (in, "UTF8"));
clsFromJar = reader.readLine ();
in.close ();
}
} catch (IOException e) {
}
return clsFromJar;
}
/*
* Try to find provider using the ServiceLoader API
*
* @param type Base class / Service interface of the factory to find.
*
* @return instance of provider class if found or null
*/
private static