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