Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.xml/share/classes/jdk/xml/internal/JdkXmlUtils.java
40948 views
1
/*
2
* Copyright (c) 2016, 2021, 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
package jdk.xml.internal;
26
27
import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
28
import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
29
import com.sun.org.apache.xerces.internal.impl.Constants;
30
import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;
31
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
32
import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings;
33
import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
34
import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
35
import javax.xml.XMLConstants;
36
import javax.xml.catalog.CatalogFeatures;
37
import javax.xml.catalog.CatalogFeatures.Feature;
38
import javax.xml.parsers.DocumentBuilderFactory;
39
import javax.xml.parsers.ParserConfigurationException;
40
import javax.xml.parsers.SAXParserFactory;
41
import javax.xml.transform.TransformerConfigurationException;
42
import javax.xml.transform.sax.SAXTransformerFactory;
43
import static jdk.xml.internal.JdkConstants.OVERRIDE_PARSER;
44
import org.w3c.dom.Document;
45
import org.xml.sax.SAXException;
46
import org.xml.sax.SAXNotRecognizedException;
47
import org.xml.sax.SAXNotSupportedException;
48
import org.xml.sax.XMLReader;
49
50
/**
51
* Constants for use across JAXP processors.
52
*/
53
public class JdkXmlUtils {
54
private static final String DOM_FACTORY_ID = "javax.xml.parsers.DocumentBuilderFactory";
55
private static final String SAX_FACTORY_ID = "javax.xml.parsers.SAXParserFactory";
56
private static final String SAX_DRIVER = "org.xml.sax.driver";
57
58
/**
59
* Xerces features
60
*/
61
public static final String NAMESPACES_FEATURE =
62
Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE;
63
public static final String NAMESPACE_PREFIXES_FEATURE =
64
Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE;
65
66
67
/**
68
* Catalog features
69
*/
70
public final static String USE_CATALOG = XMLConstants.USE_CATALOG;
71
public final static String SP_USE_CATALOG = "javax.xml.useCatalog";
72
public final static String CATALOG_FILES = CatalogFeatures.Feature.FILES.getPropertyName();
73
public final static String CATALOG_DEFER = CatalogFeatures.Feature.DEFER.getPropertyName();
74
public final static String CATALOG_PREFER = CatalogFeatures.Feature.PREFER.getPropertyName();
75
public final static String CATALOG_RESOLVE = CatalogFeatures.Feature.RESOLVE.getPropertyName();
76
77
78
79
/**
80
* Default value of USE_CATALOG. This will read the System property
81
*/
82
public static final boolean USE_CATALOG_DEFAULT
83
= SecuritySupport.getJAXPSystemProperty(Boolean.class, SP_USE_CATALOG, "true");
84
85
86
/**
87
* The system-default factory
88
*/
89
private static final SAXParserFactory defaultSAXFactory = getSAXFactory(false);
90
91
/**
92
* Returns the value.
93
*
94
* @param value the specified value
95
* @param defValue the default value
96
* @return the value, or the default value if the value is null
97
*/
98
public static int getValue(Object value, int defValue) {
99
if (value == null) {
100
return defValue;
101
}
102
103
if (value instanceof Number) {
104
return ((Number) value).intValue();
105
} else if (value instanceof String) {
106
return Integer.parseInt(String.valueOf(value));
107
} else {
108
throw new IllegalArgumentException("Unexpected class: "
109
+ value.getClass());
110
}
111
}
112
113
/**
114
* Sets the XMLReader instance with the specified property if the the
115
* property is supported, ignores error if not, issues a warning if so
116
* requested.
117
*
118
* @param reader an XMLReader instance
119
* @param property the name of the property
120
* @param value the value of the property
121
* @param warn a flag indicating whether a warning should be issued
122
*/
123
public static void setXMLReaderPropertyIfSupport(XMLReader reader, String property,
124
Object value, boolean warn) {
125
try {
126
reader.setProperty(property, value);
127
} catch (SAXNotRecognizedException | SAXNotSupportedException e) {
128
if (warn) {
129
XMLSecurityManager.printWarning(reader.getClass().getName(),
130
property, e);
131
}
132
}
133
}
134
135
/**
136
* Returns the value of a Catalog feature by the property name.
137
*
138
* @param features a CatalogFeatures instance
139
* @param name the name of a Catalog feature
140
* @return the value of a Catalog feature, null if the name does not match
141
* any feature supported by the Catalog.
142
*/
143
public static String getCatalogFeature(CatalogFeatures features, String name) {
144
for (Feature feature : Feature.values()) {
145
if (feature.getPropertyName().equals(name)) {
146
return features.get(feature);
147
}
148
}
149
return null;
150
}
151
152
/**
153
* Creates an instance of a CatalogFeatures.
154
*
155
* @param defer the defer property defined in CatalogFeatures
156
* @param file the file path to a catalog
157
* @param prefer the prefer property defined in CatalogFeatures
158
* @param resolve the resolve property defined in CatalogFeatures
159
* @return a {@link javax.xml.transform.Source} object
160
*/
161
public static CatalogFeatures getCatalogFeatures(String defer, String file,
162
String prefer, String resolve) {
163
164
CatalogFeatures.Builder builder = CatalogFeatures.builder();
165
if (file != null) {
166
builder = builder.with(CatalogFeatures.Feature.FILES, file);
167
}
168
if (prefer != null) {
169
builder = builder.with(CatalogFeatures.Feature.PREFER, prefer);
170
}
171
if (defer != null) {
172
builder = builder.with(CatalogFeatures.Feature.DEFER, defer);
173
}
174
if (resolve != null) {
175
builder = builder.with(CatalogFeatures.Feature.RESOLVE, resolve);
176
}
177
178
return builder.build();
179
}
180
181
/**
182
* Passing on the CatalogFeatures settings from one Xerces configuration
183
* object to another.
184
*
185
* @param config1 a Xerces configuration object
186
* @param config2 a Xerces configuration object
187
*/
188
public static void catalogFeaturesConfig2Config(XMLComponentManager config1,
189
ParserConfigurationSettings config2) {
190
boolean supportCatalog = true;
191
boolean useCatalog = config1.getFeature(XMLConstants.USE_CATALOG);
192
try {
193
config2.setFeature(JdkXmlUtils.USE_CATALOG, useCatalog);
194
} catch (XMLConfigurationException e) {
195
supportCatalog = false;
196
}
197
198
if (supportCatalog && useCatalog) {
199
try {
200
for (CatalogFeatures.Feature f : CatalogFeatures.Feature.values()) {
201
config2.setProperty(f.getPropertyName(), config1.getProperty(f.getPropertyName()));
202
}
203
} catch (XMLConfigurationException e) {
204
//shall not happen for internal settings
205
}
206
}
207
}
208
209
/**
210
* Passing on the CatalogFeatures settings from a Xerces configuration
211
* object to an XMLReader.
212
*
213
* @param config a Xerces configuration object
214
* @param reader an XMLReader
215
*/
216
public static void catalogFeaturesConfig2Reader(XMLComponentManager config, XMLReader reader) {
217
boolean supportCatalog = true;
218
boolean useCatalog = config.getFeature(XMLConstants.USE_CATALOG);
219
try {
220
reader.setFeature(JdkXmlUtils.USE_CATALOG, useCatalog);
221
} catch (SAXNotRecognizedException | SAXNotSupportedException e) {
222
supportCatalog = false;
223
}
224
225
if (supportCatalog && useCatalog) {
226
try {
227
for (CatalogFeatures.Feature f : CatalogFeatures.Feature.values()) {
228
reader.setProperty(f.getPropertyName(), config.getProperty(f.getPropertyName()));
229
}
230
} catch (SAXNotRecognizedException | SAXNotSupportedException e) {
231
//shall not happen for internal settings
232
}
233
}
234
}
235
236
/**
237
* Returns an XMLReader instance. If overrideDefaultParser is requested, use
238
* SAXParserFactory or XMLReaderFactory, otherwise use the system-default
239
* SAXParserFactory to locate an XMLReader.
240
*
241
* @param overrideDefaultParser a flag indicating whether a 3rd party's
242
* parser implementation may be used to override the system-default one
243
* @param secureProcessing a flag indicating whether secure processing is
244
* requested
245
* @param useXMLReaderFactory a flag indicating when the XMLReader should be
246
* created using XMLReaderFactory. True is a compatibility mode that honors
247
* the property org.xml.sax.driver (see JDK-6490921).
248
* @return an XMLReader instance
249
*/
250
public static XMLReader getXMLReader(boolean overrideDefaultParser,
251
boolean secureProcessing) {
252
SAXParserFactory saxFactory;
253
XMLReader reader = null;
254
String spSAXDriver = SecuritySupport.getSystemProperty(SAX_DRIVER);
255
if (spSAXDriver != null) {
256
reader = getXMLReaderWXMLReaderFactory();
257
} else if (overrideDefaultParser) {
258
reader = getXMLReaderWSAXFactory(overrideDefaultParser);
259
}
260
261
if (reader != null) {
262
if (secureProcessing) {
263
try {
264
reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, secureProcessing);
265
} catch (SAXException e) {
266
XMLSecurityManager.printWarning(reader.getClass().getName(),
267
XMLConstants.FEATURE_SECURE_PROCESSING, e);
268
}
269
}
270
try {
271
reader.setFeature(NAMESPACES_FEATURE, true);
272
reader.setFeature(NAMESPACE_PREFIXES_FEATURE, false);
273
} catch (SAXException se) {
274
// older version of a parser
275
}
276
return reader;
277
}
278
279
// use the system-default
280
saxFactory = defaultSAXFactory;
281
282
try {
283
reader = saxFactory.newSAXParser().getXMLReader();
284
} catch (ParserConfigurationException | SAXException ex) {
285
// shall not happen with the system-default reader
286
}
287
return reader;
288
}
289
290
/**
291
* Creates a system-default DOM Document.
292
*
293
* @return a DOM Document instance
294
*/
295
public static Document getDOMDocument() {
296
try {
297
DocumentBuilderFactory dbf = JdkXmlUtils.getDOMFactory(false);
298
return dbf.newDocumentBuilder().newDocument();
299
} catch (ParserConfigurationException pce) {
300
// can never happen with the system-default configuration
301
}
302
return null;
303
}
304
305
/**
306
* Returns a DocumentBuilderFactory instance.
307
*
308
* @param overrideDefaultParser a flag indicating whether the system-default
309
* implementation may be overridden. If the system property of the
310
* DOM factory ID is set, override is always allowed.
311
*
312
* @return a DocumentBuilderFactory instance.
313
*/
314
@SuppressWarnings("removal")
315
public static DocumentBuilderFactory getDOMFactory(boolean overrideDefaultParser) {
316
boolean override = overrideDefaultParser;
317
String spDOMFactory = SecuritySupport.getJAXPSystemProperty(DOM_FACTORY_ID);
318
319
if (spDOMFactory != null && System.getSecurityManager() == null) {
320
override = true;
321
}
322
DocumentBuilderFactory dbf
323
= !override
324
? new DocumentBuilderFactoryImpl()
325
: DocumentBuilderFactory.newInstance();
326
dbf.setNamespaceAware(true);
327
// false is the default setting. This step here is for compatibility
328
dbf.setValidating(false);
329
return dbf;
330
}
331
332
/**
333
* Returns a SAXParserFactory instance.
334
*
335
* @param overrideDefaultParser a flag indicating whether the system-default
336
* implementation may be overridden. If the system property of the
337
* DOM factory ID is set, override is always allowed.
338
*
339
* @return a SAXParserFactory instance.
340
*/
341
@SuppressWarnings("removal")
342
public static SAXParserFactory getSAXFactory(boolean overrideDefaultParser) {
343
boolean override = overrideDefaultParser;
344
String spSAXFactory = SecuritySupport.getJAXPSystemProperty(SAX_FACTORY_ID);
345
if (spSAXFactory != null && System.getSecurityManager() == null) {
346
override = true;
347
}
348
349
SAXParserFactory factory
350
= !override
351
? new SAXParserFactoryImpl()
352
: SAXParserFactory.newInstance();
353
factory.setNamespaceAware(true);
354
return factory;
355
}
356
357
public static SAXTransformerFactory getSAXTransformFactory(boolean overrideDefaultParser) {
358
SAXTransformerFactory tf = overrideDefaultParser
359
? (SAXTransformerFactory) SAXTransformerFactory.newInstance()
360
: (SAXTransformerFactory) new TransformerFactoryImpl();
361
try {
362
tf.setFeature(OVERRIDE_PARSER, overrideDefaultParser);
363
} catch (TransformerConfigurationException ex) {
364
// ignore since it'd never happen with the JDK impl.
365
}
366
return tf;
367
}
368
369
private static XMLReader getXMLReaderWSAXFactory(boolean overrideDefaultParser) {
370
SAXParserFactory saxFactory = getSAXFactory(overrideDefaultParser);
371
try {
372
return saxFactory.newSAXParser().getXMLReader();
373
} catch (ParserConfigurationException | SAXException ex) {
374
return getXMLReaderWXMLReaderFactory();
375
}
376
}
377
378
@SuppressWarnings("deprecation")
379
private static XMLReader getXMLReaderWXMLReaderFactory() {
380
try {
381
return org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
382
} catch (SAXException ex1) {
383
}
384
return null;
385
}
386
}
387
388