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/w3c/dom/bootstrap/DOMImplementationRegistry.java
86410 views
1
/*
2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3
*
4
* This code is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 2 only, as
6
* published by the Free Software Foundation. Oracle designates this
7
* particular file as subject to the "Classpath" exception as provided
8
* by Oracle in the LICENSE file that accompanied this code.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*/
24
25
/*
26
* This file is available under and governed by the GNU General Public
27
* License version 2 only, as published by the Free Software Foundation.
28
* However, the following notice accompanied the original version of this
29
* file and, per its terms, should not be removed:
30
*
31
* Copyright (c) 2004 World Wide Web Consortium,
32
*
33
* (Massachusetts Institute of Technology, European Research Consortium for
34
* Informatics and Mathematics, Keio University). All Rights Reserved. This
35
* work is distributed under the W3C(r) Software License [1] in the hope that
36
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
37
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
38
*
39
* [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
40
*/
41
42
43
package org.w3c.dom.bootstrap;
44
45
import java.util.StringTokenizer;
46
import java.util.Vector;
47
import org.w3c.dom.DOMImplementationSource;
48
import org.w3c.dom.DOMImplementationList;
49
import org.w3c.dom.DOMImplementation;
50
import java.io.InputStream;
51
import java.io.BufferedReader;
52
import java.io.InputStreamReader;
53
import java.security.AccessController;
54
import java.security.PrivilegedAction;
55
56
/**
57
* A factory that enables applications to obtain instances of
58
* <code>DOMImplementation</code>.
59
*
60
* <p>
61
* Example:
62
* </p>
63
*
64
* <pre class='example'>
65
* // get an instance of the DOMImplementation registry
66
* DOMImplementationRegistry registry =
67
* DOMImplementationRegistry.newInstance();
68
* // get a DOM implementation the Level 3 XML module
69
* DOMImplementation domImpl =
70
* registry.getDOMImplementation("XML 3.0");
71
* </pre>
72
*
73
* <p>
74
* This provides an application with an implementation-independent starting
75
* point. DOM implementations may modify this class to meet new security
76
* standards or to provide *additional* fallbacks for the list of
77
* DOMImplementationSources.
78
* </p>
79
*
80
* @see DOMImplementation
81
* @see DOMImplementationSource
82
* @since DOM Level 3
83
*/
84
public final class DOMImplementationRegistry {
85
/**
86
* The system property to specify the
87
* DOMImplementationSource class names.
88
*/
89
public static final String PROPERTY =
90
"org.w3c.dom.DOMImplementationSourceList";
91
92
/**
93
* Default columns per line.
94
*/
95
private static final int DEFAULT_LINE_LENGTH = 80;
96
97
/**
98
* The list of DOMImplementationSources.
99
*/
100
private Vector sources;
101
102
/**
103
* Default class name.
104
*/
105
private static final String FALLBACK_CLASS =
106
"com.sun.org.apache.xerces.internal.dom.DOMXSImplementationSourceImpl";
107
private static final String DEFAULT_PACKAGE =
108
"com.sun.org.apache.xerces.internal.dom";
109
/**
110
* Private constructor.
111
* @param srcs Vector List of DOMImplementationSources
112
*/
113
private DOMImplementationRegistry(final Vector srcs) {
114
sources = srcs;
115
}
116
117
/**
118
* Obtain a new instance of a <code>DOMImplementationRegistry</code>.
119
*
120
121
* The <code>DOMImplementationRegistry</code> is initialized by the
122
* application or the implementation, depending on the context, by
123
* first checking the value of the Java system property
124
* <code>org.w3c.dom.DOMImplementationSourceList</code> and
125
* the service provider whose contents are at
126
* "<code>META_INF/services/org.w3c.dom.DOMImplementationSourceList</code>".
127
* The value of this property is a white-space separated list of
128
* names of availables classes implementing the
129
* <code>DOMImplementationSource</code> interface. Each class listed
130
* in the class name list is instantiated and any exceptions
131
* encountered are thrown to the application.
132
*
133
* @return an initialized instance of DOMImplementationRegistry
134
* @throws ClassNotFoundException
135
* If any specified class can not be found
136
* @throws InstantiationException
137
* If any specified class is an interface or abstract class
138
* @throws IllegalAccessException
139
* If the default constructor of a specified class is not accessible
140
* @throws ClassCastException
141
* If any specified class does not implement
142
* <code>DOMImplementationSource</code>
143
*/
144
public static DOMImplementationRegistry newInstance()
145
throws
146
ClassNotFoundException,
147
InstantiationException,
148
IllegalAccessException,
149
ClassCastException {
150
Vector sources = new Vector();
151
152
ClassLoader classLoader = getClassLoader();
153
// fetch system property:
154
String p = getSystemProperty(PROPERTY);
155
156
//
157
// if property is not specified then use contents of
158
// META_INF/org.w3c.dom.DOMImplementationSourceList from classpath
159
if (p == null) {
160
p = getServiceValue(classLoader);
161
}
162
if (p == null) {
163
//
164
// DOM Implementations can modify here to add *additional* fallback
165
// mechanisms to access a list of default DOMImplementationSources.
166
//fall back to JAXP implementation class com.sun.org.apache.xerces.internal.dom.DOMXSImplementationSourceImpl
167
p = FALLBACK_CLASS;
168
}
169
if (p != null) {
170
StringTokenizer st = new StringTokenizer(p);
171
while (st.hasMoreTokens()) {
172
String sourceName = st.nextToken();
173
// make sure we have access to restricted packages
174
boolean internal = false;
175
if (System.getSecurityManager() != null) {
176
if (sourceName != null && sourceName.startsWith(DEFAULT_PACKAGE)) {
177
internal = true;
178
}
179
}
180
Class sourceClass = null;
181
if (classLoader != null && !internal) {
182
sourceClass = classLoader.loadClass(sourceName);
183
} else {
184
sourceClass = Class.forName(sourceName);
185
}
186
DOMImplementationSource source =
187
(DOMImplementationSource) sourceClass.newInstance();
188
sources.addElement(source);
189
}
190
}
191
return new DOMImplementationRegistry(sources);
192
}
193
194
/**
195
* Return the first implementation that has the desired
196
* features, or <code>null</code> if none is found.
197
*
198
* @param features
199
* A string that specifies which features are required. This is
200
* a space separated list in which each feature is specified by
201
* its name optionally followed by a space and a version number.
202
* This is something like: "XML 1.0 Traversal +Events 2.0"
203
* @return An implementation that has the desired features,
204
* or <code>null</code> if none found.
205
*/
206
public DOMImplementation getDOMImplementation(final String features) {
207
int size = sources.size();
208
String name = null;
209
for (int i = 0; i < size; i++) {
210
DOMImplementationSource source =
211
(DOMImplementationSource) sources.elementAt(i);
212
DOMImplementation impl = source.getDOMImplementation(features);
213
if (impl != null) {
214
return impl;
215
}
216
}
217
return null;
218
}
219
220
/**
221
* Return a list of implementations that support the
222
* desired features.
223
*
224
* @param features
225
* A string that specifies which features are required. This is
226
* a space separated list in which each feature is specified by
227
* its name optionally followed by a space and a version number.
228
* This is something like: "XML 1.0 Traversal +Events 2.0"
229
* @return A list of DOMImplementations that support the desired features.
230
*/
231
public DOMImplementationList getDOMImplementationList(final String features) {
232
final Vector implementations = new Vector();
233
int size = sources.size();
234
for (int i = 0; i < size; i++) {
235
DOMImplementationSource source =
236
(DOMImplementationSource) sources.elementAt(i);
237
DOMImplementationList impls =
238
source.getDOMImplementationList(features);
239
for (int j = 0; j < impls.getLength(); j++) {
240
DOMImplementation impl = impls.item(j);
241
implementations.addElement(impl);
242
}
243
}
244
return new DOMImplementationList() {
245
public DOMImplementation item(final int index) {
246
if (index >= 0 && index < implementations.size()) {
247
try {
248
return (DOMImplementation)
249
implementations.elementAt(index);
250
} catch (ArrayIndexOutOfBoundsException e) {
251
return null;
252
}
253
}
254
return null;
255
}
256
257
public int getLength() {
258
return implementations.size();
259
}
260
};
261
}
262
263
/**
264
* Register an implementation.
265
*
266
* @param s The source to be registered, may not be <code>null</code>
267
*/
268
public void addSource(final DOMImplementationSource s) {
269
if (s == null) {
270
throw new NullPointerException();
271
}
272
if (!sources.contains(s)) {
273
sources.addElement(s);
274
}
275
}
276
277
/**
278
*
279
* Gets a class loader.
280
*
281
* @return A class loader, possibly <code>null</code>
282
*/
283
private static ClassLoader getClassLoader() {
284
try {
285
ClassLoader contextClassLoader = getContextClassLoader();
286
287
if (contextClassLoader != null) {
288
return contextClassLoader;
289
}
290
} catch (Exception e) {
291
// Assume that the DOM application is in a JRE 1.1, use the
292
// current ClassLoader
293
return DOMImplementationRegistry.class.getClassLoader();
294
}
295
return DOMImplementationRegistry.class.getClassLoader();
296
}
297
298
/**
299
* This method attempts to return the first line of the resource
300
* META_INF/services/org.w3c.dom.DOMImplementationSourceList
301
* from the provided ClassLoader.
302
*
303
* @param classLoader classLoader, may not be <code>null</code>.
304
* @return first line of resource, or <code>null</code>
305
*/
306
private static String getServiceValue(final ClassLoader classLoader) {
307
String serviceId = "META-INF/services/" + PROPERTY;
308
// try to find services in CLASSPATH
309
try {
310
InputStream is = getResourceAsStream(classLoader, serviceId);
311
312
if (is != null) {
313
BufferedReader rd;
314
try {
315
rd =
316
new BufferedReader(new InputStreamReader(is, "UTF-8"),
317
DEFAULT_LINE_LENGTH);
318
} catch (java.io.UnsupportedEncodingException e) {
319
rd =
320
new BufferedReader(new InputStreamReader(is),
321
DEFAULT_LINE_LENGTH);
322
}
323
String serviceValue = rd.readLine();
324
rd.close();
325
if (serviceValue != null && serviceValue.length() > 0) {
326
return serviceValue;
327
}
328
}
329
} catch (Exception ex) {
330
return null;
331
}
332
return null;
333
}
334
335
/**
336
* A simple JRE (Java Runtime Environment) 1.1 test
337
*
338
* @return <code>true</code> if JRE 1.1
339
*/
340
private static boolean isJRE11() {
341
try {
342
Class c = Class.forName("java.security.AccessController");
343
// java.security.AccessController existed since 1.2 so, if no
344
// exception was thrown, the DOM application is running in a JRE
345
// 1.2 or higher
346
return false;
347
} catch (Exception ex) {
348
// ignore
349
}
350
return true;
351
}
352
353
/**
354
* This method returns the ContextClassLoader or <code>null</code> if
355
* running in a JRE 1.1
356
*
357
* @return The Context Classloader
358
*/
359
private static ClassLoader getContextClassLoader() {
360
return isJRE11()
361
? null
362
: (ClassLoader)
363
AccessController.doPrivileged(new PrivilegedAction() {
364
public Object run() {
365
ClassLoader classLoader = null;
366
try {
367
classLoader =
368
Thread.currentThread().getContextClassLoader();
369
} catch (SecurityException ex) {
370
}
371
return classLoader;
372
}
373
});
374
}
375
376
/**
377
* This method returns the system property indicated by the specified name
378
* after checking access control privileges. For a JRE 1.1, this check is
379
* not done.
380
*
381
* @param name the name of the system property
382
* @return the system property
383
*/
384
private static String getSystemProperty(final String name) {
385
return isJRE11()
386
? (String) System.getProperty(name)
387
: (String) AccessController.doPrivileged(new PrivilegedAction() {
388
public Object run() {
389
return System.getProperty(name);
390
}
391
});
392
}
393
394
/**
395
* This method returns an Inputstream for the reading resource
396
* META_INF/services/org.w3c.dom.DOMImplementationSourceList after checking
397
* access control privileges. For a JRE 1.1, this check is not done.
398
*
399
* @param classLoader classLoader
400
* @param name the resource
401
* @return an Inputstream for the resource specified
402
*/
403
private static InputStream getResourceAsStream(final ClassLoader classLoader,
404
final String name) {
405
if (isJRE11()) {
406
InputStream ris;
407
if (classLoader == null) {
408
ris = ClassLoader.getSystemResourceAsStream(name);
409
} else {
410
ris = classLoader.getResourceAsStream(name);
411
}
412
return ris;
413
} else {
414
return (InputStream)
415
AccessController.doPrivileged(new PrivilegedAction() {
416
public Object run() {
417
InputStream ris;
418
if (classLoader == null) {
419
ris =
420
ClassLoader.getSystemResourceAsStream(name);
421
} else {
422
ris = classLoader.getResourceAsStream(name);
423
}
424
return ris;
425
}
426
});
427
}
428
}
429
}
430
431