Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/org/xml/sax/helpers/XMLReaderFactory.java
48576 views
1
/*
2
* Copyright (c) 2000, 2013, 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";
86
private static SecuritySupport ss = new SecuritySupport();
87
88
private static String _clsFromJar = null;
89
private static boolean _jarread = false;
90
/**
91
* Attempt to create an XMLReader from system defaults.
92
* In environments which can support it, the name of the XMLReader
93
* class is determined by trying each these options in order, and
94
* using the first one which succeeds:</p> <ul>
95
*
96
* <li>If the system property <code>org.xml.sax.driver</code>
97
* has a value, that is used as an XMLReader class name. </li>
98
*
99
* <li>The JAR "Services API" is used to look for a class name
100
* in the <em>META-INF/services/org.xml.sax.driver</em> file in
101
* jarfiles available to the runtime.</li>
102
*
103
* <li> SAX parser distributions are strongly encouraged to provide
104
* a default XMLReader class name that will take effect only when
105
* previous options (on this list) are not successful.</li>
106
*
107
* <li>Finally, if {@link ParserFactory#makeParser()} can
108
* return a system default SAX1 parser, that parser is wrapped in
109
* a {@link ParserAdapter}. (This is a migration aid for SAX1
110
* environments, where the <code>org.xml.sax.parser</code> system
111
* property will often be usable.) </li>
112
*
113
* </ul>
114
*
115
* <p> In environments such as small embedded systems, which can not
116
* support that flexibility, other mechanisms to determine the default
117
* may be used. </p>
118
*
119
* <p>Note that many Java environments allow system properties to be
120
* initialized on a command line. This means that <em>in most cases</em>
121
* setting a good value for that property ensures that calls to this
122
* method will succeed, except when security policies intervene.
123
* This will also maximize application portability to older SAX
124
* environments, with less robust implementations of this method.
125
* </p>
126
*
127
* @return A new XMLReader.
128
* @exception org.xml.sax.SAXException If no default XMLReader class
129
* can be identified and instantiated.
130
* @see #createXMLReader(java.lang.String)
131
*/
132
public static XMLReader createXMLReader ()
133
throws SAXException
134
{
135
String className = null;
136
ClassLoader cl = ss.getContextClassLoader();
137
138
// 1. try the JVM-instance-wide system property
139
try {
140
className = ss.getSystemProperty(property);
141
}
142
catch (RuntimeException e) { /* continue searching */ }
143
144
// 2. if that fails, try META-INF/services/
145
if (className == null) {
146
if (!_jarread) {
147
_jarread = true;
148
String service = "META-INF/services/" + property;
149
InputStream in;
150
BufferedReader reader;
151
152
try {
153
if (cl != null) {
154
in = ss.getResourceAsStream(cl, service);
155
156
// If no provider found then try the current ClassLoader
157
if (in == null) {
158
cl = null;
159
in = ss.getResourceAsStream(cl, service);
160
}
161
} else {
162
// No Context ClassLoader, try the current ClassLoader
163
in = ss.getResourceAsStream(cl, service);
164
}
165
166
if (in != null) {
167
reader = new BufferedReader (new InputStreamReader (in, "UTF8"));
168
_clsFromJar = reader.readLine ();
169
in.close ();
170
}
171
} catch (Exception e) {
172
}
173
}
174
className = _clsFromJar;
175
}
176
177
// 3. Distro-specific fallback
178
if (className == null) {
179
// BEGIN DISTRIBUTION-SPECIFIC
180
181
// EXAMPLE:
182
// className = "com.example.sax.XmlReader";
183
// or a $JAVA_HOME/jre/lib/*properties setting...
184
className = "com.sun.org.apache.xerces.internal.parsers.SAXParser";
185
186
// END DISTRIBUTION-SPECIFIC
187
}
188
189
// do we know the XMLReader implementation class yet?
190
if (className != null)
191
return loadClass (cl, className);
192
193
// 4. panic -- adapt any SAX1 parser
194
try {
195
return new ParserAdapter (ParserFactory.makeParser ());
196
} catch (Exception e) {
197
throw new SAXException ("Can't create default XMLReader; "
198
+ "is system property org.xml.sax.driver set?");
199
}
200
}
201
202
203
/**
204
* Attempt to create an XML reader from a class name.
205
*
206
* <p>Given a class name, this method attempts to load
207
* and instantiate the class as an XML reader.</p>
208
*
209
* <p>Note that this method will not be usable in environments where
210
* the caller (perhaps an applet) is not permitted to load classes
211
* dynamically.</p>
212
*
213
* @return A new XML reader.
214
* @exception org.xml.sax.SAXException If the class cannot be
215
* loaded, instantiated, and cast to XMLReader.
216
* @see #createXMLReader()
217
*/
218
public static XMLReader createXMLReader (String className)
219
throws SAXException
220
{
221
return loadClass (ss.getContextClassLoader(), className);
222
}
223
224
private static XMLReader loadClass (ClassLoader loader, String className)
225
throws SAXException
226
{
227
try {
228
return (XMLReader) NewInstance.newInstance (loader, className);
229
} catch (ClassNotFoundException e1) {
230
throw new SAXException("SAX2 driver class " + className +
231
" not found", e1);
232
} catch (IllegalAccessException e2) {
233
throw new SAXException("SAX2 driver class " + className +
234
" found but cannot be loaded", e2);
235
} catch (InstantiationException e3) {
236
throw new SAXException("SAX2 driver class " + className +
237
" loaded but cannot be instantiated (no empty public constructor?)",
238
e3);
239
} catch (ClassCastException e4) {
240
throw new SAXException("SAX2 driver class " + className +
241
" does not implement XMLReader", e4);
242
}
243
}
244
}
245
246