12005
|
1 |
/*
|
|
2 |
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
// XMLReaderFactory.java - factory for creating a new reader.
|
|
27 |
// http://www.saxproject.org
|
|
28 |
// Written by David Megginson
|
|
29 |
// and by David Brownell
|
|
30 |
// NO WARRANTY! This class is in the Public Domain.
|
|
31 |
// $Id: XMLReaderFactory.java,v 1.2.2.1 2005/07/31 22:48:08 jeffsuttor Exp $
|
|
32 |
|
|
33 |
package org.xml.sax.helpers;
|
|
34 |
import java.io.BufferedReader;
|
|
35 |
import java.io.InputStream;
|
|
36 |
import java.io.InputStreamReader;
|
|
37 |
import org.xml.sax.XMLReader;
|
|
38 |
import org.xml.sax.SAXException;
|
|
39 |
|
|
40 |
|
|
41 |
/**
|
|
42 |
* Factory for creating an XML reader.
|
|
43 |
*
|
|
44 |
* <blockquote>
|
|
45 |
* <em>This module, both source code and documentation, is in the
|
|
46 |
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
|
|
47 |
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
|
|
48 |
* for further information.
|
|
49 |
* </blockquote>
|
|
50 |
*
|
|
51 |
* <p>This class contains static methods for creating an XML reader
|
|
52 |
* from an explicit class name, or based on runtime defaults:</p>
|
|
53 |
*
|
|
54 |
* <pre>
|
|
55 |
* try {
|
|
56 |
* XMLReader myReader = XMLReaderFactory.createXMLReader();
|
|
57 |
* } catch (SAXException e) {
|
|
58 |
* System.err.println(e.getMessage());
|
|
59 |
* }
|
|
60 |
* </pre>
|
|
61 |
*
|
|
62 |
* <p><strong>Note to Distributions bundled with parsers:</strong>
|
|
63 |
* You should modify the implementation of the no-arguments
|
|
64 |
* <em>createXMLReader</em> to handle cases where the external
|
|
65 |
* configuration mechanisms aren't set up. That method should do its
|
|
66 |
* best to return a parser when one is in the class path, even when
|
|
67 |
* nothing bound its class name to <code>org.xml.sax.driver</code> so
|
|
68 |
* those configuration mechanisms would see it.</p>
|
|
69 |
*
|
|
70 |
* @since SAX 2.0
|
|
71 |
* @author David Megginson, David Brownell
|
|
72 |
* @version 2.0.1 (sax2r2)
|
|
73 |
*/
|
|
74 |
final public class XMLReaderFactory
|
|
75 |
{
|
|
76 |
/**
|
|
77 |
* Private constructor.
|
|
78 |
*
|
|
79 |
* <p>This constructor prevents the class from being instantiated.</p>
|
|
80 |
*/
|
|
81 |
private XMLReaderFactory ()
|
|
82 |
{
|
|
83 |
}
|
|
84 |
|
|
85 |
private static final String property = "org.xml.sax.driver";
|
16953
|
86 |
private static SecuritySupport ss = new SecuritySupport();
|
12005
|
87 |
|
|
88 |
private static boolean _jarread = false;
|
|
89 |
/**
|
|
90 |
* Attempt to create an XMLReader from system defaults.
|
|
91 |
* In environments which can support it, the name of the XMLReader
|
|
92 |
* class is determined by trying each these options in order, and
|
|
93 |
* using the first one which succeeds:</p> <ul>
|
|
94 |
*
|
|
95 |
* <li>If the system property <code>org.xml.sax.driver</code>
|
|
96 |
* has a value, that is used as an XMLReader class name. </li>
|
|
97 |
*
|
|
98 |
* <li>The JAR "Services API" is used to look for a class name
|
|
99 |
* in the <em>META-INF/services/org.xml.sax.driver</em> file in
|
|
100 |
* jarfiles available to the runtime.</li>
|
|
101 |
*
|
|
102 |
* <li> SAX parser distributions are strongly encouraged to provide
|
|
103 |
* a default XMLReader class name that will take effect only when
|
|
104 |
* previous options (on this list) are not successful.</li>
|
|
105 |
*
|
|
106 |
* <li>Finally, if {@link ParserFactory#makeParser()} can
|
|
107 |
* return a system default SAX1 parser, that parser is wrapped in
|
|
108 |
* a {@link ParserAdapter}. (This is a migration aid for SAX1
|
|
109 |
* environments, where the <code>org.xml.sax.parser</code> system
|
|
110 |
* property will often be usable.) </li>
|
|
111 |
*
|
|
112 |
* </ul>
|
|
113 |
*
|
|
114 |
* <p> In environments such as small embedded systems, which can not
|
|
115 |
* support that flexibility, other mechanisms to determine the default
|
|
116 |
* may be used. </p>
|
|
117 |
*
|
|
118 |
* <p>Note that many Java environments allow system properties to be
|
|
119 |
* initialized on a command line. This means that <em>in most cases</em>
|
|
120 |
* setting a good value for that property ensures that calls to this
|
|
121 |
* method will succeed, except when security policies intervene.
|
|
122 |
* This will also maximize application portability to older SAX
|
|
123 |
* environments, with less robust implementations of this method.
|
|
124 |
* </p>
|
|
125 |
*
|
|
126 |
* @return A new XMLReader.
|
|
127 |
* @exception org.xml.sax.SAXException If no default XMLReader class
|
|
128 |
* can be identified and instantiated.
|
|
129 |
* @see #createXMLReader(java.lang.String)
|
|
130 |
*/
|
|
131 |
public static XMLReader createXMLReader ()
|
|
132 |
throws SAXException
|
|
133 |
{
|
|
134 |
String className = null;
|
16953
|
135 |
ClassLoader cl = ss.getContextClassLoader();
|
12005
|
136 |
|
|
137 |
// 1. try the JVM-instance-wide system property
|
16953
|
138 |
try {
|
|
139 |
className = ss.getSystemProperty(property);
|
|
140 |
}
|
|
141 |
catch (RuntimeException e) { /* continue searching */ }
|
12005
|
142 |
|
|
143 |
// 2. if that fails, try META-INF/services/
|
|
144 |
if (className == null) {
|
|
145 |
if (!_jarread) {
|
|
146 |
_jarread = true;
|
16953
|
147 |
String service = "META-INF/services/" + property;
|
|
148 |
InputStream in;
|
|
149 |
BufferedReader reader;
|
|
150 |
|
|
151 |
try {
|
|
152 |
if (cl != null) {
|
|
153 |
in = ss.getResourceAsStream(cl, service);
|
12005
|
154 |
|
16953
|
155 |
// If no provider found then try the current ClassLoader
|
|
156 |
if (in == null) {
|
|
157 |
cl = null;
|
|
158 |
in = ss.getResourceAsStream(cl, service);
|
12005
|
159 |
}
|
16953
|
160 |
} else {
|
|
161 |
// No Context ClassLoader, try the current ClassLoader
|
|
162 |
in = ss.getResourceAsStream(cl, service);
|
12005
|
163 |
}
|
16953
|
164 |
|
|
165 |
if (in != null) {
|
|
166 |
reader = new BufferedReader (
|
|
167 |
new InputStreamReader (in, "UTF8"));
|
|
168 |
className = reader.readLine ();
|
|
169 |
in.close ();
|
|
170 |
}
|
|
171 |
} catch (Exception e) {
|
|
172 |
}
|
12005
|
173 |
}
|
|
174 |
}
|
|
175 |
|
|
176 |
// 3. Distro-specific fallback
|
|
177 |
if (className == null) {
|
|
178 |
// BEGIN DISTRIBUTION-SPECIFIC
|
|
179 |
|
|
180 |
// EXAMPLE:
|
|
181 |
// className = "com.example.sax.XmlReader";
|
|
182 |
// or a $JAVA_HOME/jre/lib/*properties setting...
|
|
183 |
className = "com.sun.org.apache.xerces.internal.parsers.SAXParser";
|
|
184 |
|
|
185 |
// END DISTRIBUTION-SPECIFIC
|
|
186 |
}
|
|
187 |
|
|
188 |
// do we know the XMLReader implementation class yet?
|
|
189 |
if (className != null)
|
16953
|
190 |
return loadClass (cl, className);
|
12005
|
191 |
|
|
192 |
// 4. panic -- adapt any SAX1 parser
|
|
193 |
try {
|
|
194 |
return new ParserAdapter (ParserFactory.makeParser ());
|
|
195 |
} catch (Exception e) {
|
|
196 |
throw new SAXException ("Can't create default XMLReader; "
|
|
197 |
+ "is system property org.xml.sax.driver set?");
|
|
198 |
}
|
|
199 |
}
|
|
200 |
|
|
201 |
|
|
202 |
/**
|
|
203 |
* Attempt to create an XML reader from a class name.
|
|
204 |
*
|
|
205 |
* <p>Given a class name, this method attempts to load
|
|
206 |
* and instantiate the class as an XML reader.</p>
|
|
207 |
*
|
|
208 |
* <p>Note that this method will not be usable in environments where
|
|
209 |
* the caller (perhaps an applet) is not permitted to load classes
|
|
210 |
* dynamically.</p>
|
|
211 |
*
|
|
212 |
* @return A new XML reader.
|
|
213 |
* @exception org.xml.sax.SAXException If the class cannot be
|
|
214 |
* loaded, instantiated, and cast to XMLReader.
|
|
215 |
* @see #createXMLReader()
|
|
216 |
*/
|
|
217 |
public static XMLReader createXMLReader (String className)
|
|
218 |
throws SAXException
|
|
219 |
{
|
16953
|
220 |
return loadClass (ss.getContextClassLoader(), className);
|
12005
|
221 |
}
|
|
222 |
|
|
223 |
private static XMLReader loadClass (ClassLoader loader, String className)
|
|
224 |
throws SAXException
|
|
225 |
{
|
|
226 |
try {
|
|
227 |
return (XMLReader) NewInstance.newInstance (loader, className);
|
|
228 |
} catch (ClassNotFoundException e1) {
|
|
229 |
throw new SAXException("SAX2 driver class " + className +
|
|
230 |
" not found", e1);
|
|
231 |
} catch (IllegalAccessException e2) {
|
|
232 |
throw new SAXException("SAX2 driver class " + className +
|
|
233 |
" found but cannot be loaded", e2);
|
|
234 |
} catch (InstantiationException e3) {
|
|
235 |
throw new SAXException("SAX2 driver class " + className +
|
|
236 |
" loaded but cannot be instantiated (no empty public constructor?)",
|
|
237 |
e3);
|
|
238 |
} catch (ClassCastException e4) {
|
|
239 |
throw new SAXException("SAX2 driver class " + className +
|
|
240 |
" does not implement XMLReader", e4);
|
|
241 |
}
|
|
242 |
}
|
|
243 |
}
|