1 // XMLReaderFactory.java - factory for creating a new reader.
2 // http://www.saxproject.org
3 // Written by David Megginson
4 // and by David Brownell
5 // NO WARRANTY!  This class is in the Public Domain.
6 // $Id: XMLReaderFactory.java,v 1.10 2002/04/22 01:00:13 dbrownell Exp $
7 
8 package org.xml.sax.helpers;
9 
10 import org.xml.sax.SAXException;
11 import org.xml.sax.XMLReader;
12 
13 import android.compat.annotation.UnsupportedAppUsage;
14 
15 import java.io.BufferedReader;
16 import java.io.InputStream;
17 import java.io.InputStreamReader;
18 import java.nio.charset.StandardCharsets;
19 
20 
21 /**
22  * Factory for creating an XML reader.
23  *
24  * <blockquote>
25  * <em>This module, both source code and documentation, is in the
26  * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
27  * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
28  * for further information.
29  * </blockquote>
30  *
31  * <p>This class contains static methods for creating an XML reader
32  * from an explicit class name, or based on runtime defaults:</p>
33  *
34  * <pre>
35  * try {
36  *   XMLReader myReader = XMLReaderFactory.createXMLReader();
37  * } catch (SAXException e) {
38  *   System.err.println(e.getMessage());
39  * }
40  * </pre>
41  *
42  * <p><strong>Note to Distributions bundled with parsers:</strong>
43  * You should modify the implementation of the no-arguments
44  * <em>createXMLReader</em> to handle cases where the external
45  * configuration mechanisms aren't set up.  That method should do its
46  * best to return a parser when one is in the class path, even when
47  * nothing bound its class name to <code>org.xml.sax.driver</code> so
48  * those configuration mechanisms would see it.</p>
49  *
50  * @since SAX 2.0
51  * @author David Megginson, David Brownell
52  * @version 2.0.1 (sax2r2)
53  */
54 final public class XMLReaderFactory
55 {
56     /**
57      * Private constructor.
58      *
59      * <p>This constructor prevents the class from being instantiated.</p>
60      */
XMLReaderFactory()61     private XMLReaderFactory ()
62     {
63     }
64 
65     private static final String property = "org.xml.sax.driver";
66 
67     /**
68      * Attempt to create an XMLReader from system defaults.
69      * In environments which can support it, the name of the XMLReader
70      * class is determined by trying each these options in order, and
71      * using the first one which succeeds:</p> <ul>
72      *
73      * <li>If the system property <code>org.xml.sax.driver</code>
74      * has a value, that is used as an XMLReader class name. </li>
75      *
76      * <li>The JAR "Services API" is used to look for a class name
77      * in the <em>META-INF/services/org.xml.sax.driver</em> file in
78      * jarfiles available to the runtime.</li>
79      *
80      * <li> SAX parser distributions are strongly encouraged to provide
81      * a default XMLReader class name that will take effect only when
82      * previous options (on this list) are not successful.</li>
83      *
84      * <li>Finally, if {@link ParserFactory#makeParser()} can
85      * return a system default SAX1 parser, that parser is wrapped in
86      * a {@link ParserAdapter}.  (This is a migration aid for SAX1
87      * environments, where the <code>org.xml.sax.parser</code> system
88      * property will often be usable.) </li>
89      *
90      * </ul>
91      *
92      * <p> In environments such as small embedded systems, which can not
93      * support that flexibility, other mechanisms to determine the default
94      * may be used. </p>
95      *
96      * <p>Note that many Java environments allow system properties to be
97      * initialized on a command line.  This means that <em>in most cases</em>
98      * setting a good value for that property ensures that calls to this
99      * method will succeed, except when security policies intervene.
100      * This will also maximize application portability to older SAX
101      * environments, with less robust implementations of this method.
102      * </p>
103      *
104      * @return A new XMLReader.
105      * @exception org.xml.sax.SAXException If no default XMLReader class
106      *            can be identified and instantiated.
107      * @see #createXMLReader(java.lang.String)
108      */
createXMLReader()109     public static XMLReader createXMLReader ()
110     throws SAXException
111     {
112     String        className = null;
113     ClassLoader    loader = NewInstance.getClassLoader ();
114 
115     // 1. try the JVM-instance-wide system property
116     try { className = System.getProperty (property); }
117     catch (RuntimeException e) { /* normally fails for applets */ }
118 
119     // 2. if that fails, try META-INF/services/
120     if (className == null) {
121         try {
122         String        service = "META-INF/services/" + property;
123         InputStream    in;
124         BufferedReader    reader;
125 
126         if (loader == null)
127             in = ClassLoader.getSystemResourceAsStream (service);
128         else
129             in = loader.getResourceAsStream (service);
130 
131         if (in != null) {
132             try {
133                 reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
134                 className = reader.readLine();
135             } finally {
136                 in.close(); // may throw IOException
137             }
138         }
139         } catch (Exception e) {
140         }
141     }
142 
143     // 3. Distro-specific fallback
144     if (className == null) {
145 // BEGIN DISTRIBUTION-SPECIFIC
146 
147         // EXAMPLE:
148         // className = "com.example.sax.XmlReader";
149         // or a $JAVA_HOME/jre/lib/*properties setting...
150 
151 // END DISTRIBUTION-SPECIFIC
152     }
153 
154     // do we know the XMLReader implementation class yet?
155     if (className != null)
156         return loadClass (loader, className);
157 
158     // 4. panic -- adapt any SAX1 parser
159     try {
160         return new ParserAdapter (ParserFactory.makeParser ());
161     } catch (Exception e) {
162         throw new SAXException ("Can't create default XMLReader; "
163             + "is system property org.xml.sax.driver set?");
164     }
165     }
166 
167 
168     /**
169      * Attempt to create an XML reader from a class name.
170      *
171      * <p>Given a class name, this method attempts to load
172      * and instantiate the class as an XML reader.</p>
173      *
174      * @param className the name of the class that should be instantiated.
175      *
176      * <p>Note that this method will not be usable in environments where
177      * the caller (perhaps an applet) is not permitted to load classes
178      * dynamically.</p>
179      *
180      * @return A new XML reader.
181      * @exception org.xml.sax.SAXException If the class cannot be
182      *            loaded, instantiated, and cast to XMLReader.
183      * @see #createXMLReader()
184      */
createXMLReader(String className)185     public static XMLReader createXMLReader (String className)
186     throws SAXException
187     {
188     return loadClass (NewInstance.getClassLoader (), className);
189     }
190 
191     @UnsupportedAppUsage
loadClass(ClassLoader loader, String className)192     private static XMLReader loadClass (ClassLoader loader, String className)
193     throws SAXException
194     {
195     try {
196         return (XMLReader) NewInstance.newInstance (loader, className);
197     } catch (ClassNotFoundException e1) {
198         throw new SAXException("SAX2 driver class " + className +
199                    " not found", e1);
200     } catch (IllegalAccessException e2) {
201         throw new SAXException("SAX2 driver class " + className +
202                    " found but cannot be loaded", e2);
203     } catch (InstantiationException e3) {
204         throw new SAXException("SAX2 driver class " + className +
205        " loaded but cannot be instantiated (no empty public constructor?)",
206                    e3);
207     } catch (ClassCastException e4) {
208         throw new SAXException("SAX2 driver class " + className +
209                    " does not implement XMLReader", e4);
210     }
211     }
212 }
213