Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/jdk/xml/internal/JdkXmlUtils.java
38813 views
1
/*
2
* Copyright (c) 2017, 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
package jdk.xml.internal;
27
28
import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
29
import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
30
import com.sun.org.apache.xerces.internal.impl.Constants;
31
import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;
32
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
33
import javax.xml.XMLConstants;
34
import javax.xml.parsers.DocumentBuilderFactory;
35
import javax.xml.parsers.ParserConfigurationException;
36
import javax.xml.parsers.SAXParserFactory;
37
import javax.xml.transform.TransformerConfigurationException;
38
import javax.xml.transform.sax.SAXTransformerFactory;
39
import org.w3c.dom.Document;
40
import org.xml.sax.SAXException;
41
import org.xml.sax.SAXNotRecognizedException;
42
import org.xml.sax.SAXNotSupportedException;
43
import org.xml.sax.XMLReader;
44
45
/**
46
* Constants for use across JAXP processors.
47
*/
48
public class JdkXmlUtils {
49
private static final String DOM_FACTORY_ID = "javax.xml.parsers.DocumentBuilderFactory";
50
private static final String SAX_FACTORY_ID = "javax.xml.parsers.SAXParserFactory";
51
private static final String SAX_DRIVER = "org.xml.sax.driver";
52
53
/**
54
* Xerces features
55
*/
56
public static final String NAMESPACES_FEATURE =
57
Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE;
58
public static final String NAMESPACE_PREFIXES_FEATURE =
59
Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE;
60
61
/**
62
* jdk.xml.overrideDefaultParser: enables the use of a 3rd party's parser
63
* implementation to override the system-default parser.
64
*/
65
public static final String OVERRIDE_PARSER = "jdk.xml.overrideDefaultParser";
66
public static final boolean OVERRIDE_PARSER_DEFAULT = SecuritySupport.getJAXPSystemProperty(
67
Boolean.class, OVERRIDE_PARSER, "false");
68
69
/**
70
* Values for a feature
71
*/
72
public static final String FEATURE_TRUE = "true";
73
public static final String FEATURE_FALSE = "false";
74
75
/**
76
* The system-default factory
77
*/
78
private static final SAXParserFactory defaultSAXFactory = getSAXFactory(false);
79
80
/**
81
* Returns the value.
82
*
83
* @param value the specified value
84
* @param defValue the default value
85
* @return the value, or the default value if the value is null
86
*/
87
public static int getValue(Object value, int defValue) {
88
if (value == null) {
89
return defValue;
90
}
91
92
if (value instanceof Number) {
93
return ((Number) value).intValue();
94
} else if (value instanceof String) {
95
return Integer.parseInt(String.valueOf(value));
96
} else {
97
throw new IllegalArgumentException("Unexpected class: "
98
+ value.getClass());
99
}
100
}
101
102
/**
103
* Sets the XMLReader instance with the specified property if the the
104
* property is supported, ignores error if not, issues a warning if so
105
* requested.
106
*
107
* @param reader an XMLReader instance
108
* @param property the name of the property
109
* @param value the value of the property
110
* @param warn a flag indicating whether a warning should be issued
111
*/
112
public static void setXMLReaderPropertyIfSupport(XMLReader reader, String property,
113
Object value, boolean warn) {
114
try {
115
reader.setProperty(property, value);
116
} catch (SAXNotRecognizedException | SAXNotSupportedException e) {
117
if (warn) {
118
XMLSecurityManager.printWarning(reader.getClass().getName(),
119
property, e);
120
}
121
}
122
}
123
124
/**
125
* Returns an XMLReader instance. If overrideDefaultParser is requested, use
126
* SAXParserFactory or XMLReaderFactory, otherwise use the system-default
127
* SAXParserFactory to locate an XMLReader.
128
*
129
* @param overrideDefaultParser a flag indicating whether a 3rd party's
130
* parser implementation may be used to override the system-default one
131
* @param secureProcessing a flag indicating whether secure processing is
132
* requested
133
* @return an XMLReader instance
134
*/
135
public static XMLReader getXMLReader(boolean overrideDefaultParser,
136
boolean secureProcessing) {
137
SAXParserFactory saxFactory;
138
XMLReader reader = null;
139
String spSAXDriver = SecuritySupport.getSystemProperty(SAX_DRIVER);
140
if (spSAXDriver != null) {
141
reader = getXMLReaderWXMLReaderFactory();
142
} else if (overrideDefaultParser) {
143
reader = getXMLReaderWSAXFactory(overrideDefaultParser);
144
}
145
146
if (reader != null) {
147
if (secureProcessing) {
148
try {
149
reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, secureProcessing);
150
} catch (SAXException e) {
151
XMLSecurityManager.printWarning(reader.getClass().getName(),
152
XMLConstants.FEATURE_SECURE_PROCESSING, e);
153
}
154
}
155
try {
156
reader.setFeature(NAMESPACES_FEATURE, true);
157
reader.setFeature(NAMESPACE_PREFIXES_FEATURE, false);
158
} catch (SAXException se) {
159
// older version of a parser
160
}
161
return reader;
162
}
163
164
// use the system-default
165
saxFactory = defaultSAXFactory;
166
167
try {
168
reader = saxFactory.newSAXParser().getXMLReader();
169
} catch (ParserConfigurationException | SAXException ex) {
170
// shall not happen with the system-default reader
171
}
172
return reader;
173
}
174
175
/**
176
* Creates a system-default DOM Document.
177
*
178
* @return a DOM Document instance
179
*/
180
public static Document getDOMDocument() {
181
try {
182
DocumentBuilderFactory dbf = JdkXmlUtils.getDOMFactory(false);
183
return dbf.newDocumentBuilder().newDocument();
184
} catch (ParserConfigurationException pce) {
185
// can never happen with the system-default configuration
186
}
187
return null;
188
}
189
190
/**
191
* Returns a DocumentBuilderFactory instance.
192
*
193
* @param overrideDefaultParser a flag indicating whether the system-default
194
* implementation may be overridden. If the system property of the
195
* DOM factory ID is set, override is always allowed.
196
*
197
* @return a DocumentBuilderFactory instance.
198
*/
199
public static DocumentBuilderFactory getDOMFactory(boolean overrideDefaultParser) {
200
boolean override = overrideDefaultParser;
201
String spDOMFactory = SecuritySupport.getJAXPSystemProperty(DOM_FACTORY_ID);
202
203
if (spDOMFactory != null && System.getSecurityManager() == null) {
204
override = true;
205
}
206
DocumentBuilderFactory dbf
207
= !override
208
? new DocumentBuilderFactoryImpl()
209
: DocumentBuilderFactory.newInstance();
210
dbf.setNamespaceAware(true);
211
// false is the default setting. This step here is for compatibility
212
dbf.setValidating(false);
213
return dbf;
214
}
215
216
/**
217
* Returns a SAXParserFactory instance.
218
*
219
* @param overrideDefaultParser a flag indicating whether the system-default
220
* implementation may be overridden. If the system property of the
221
* DOM factory ID is set, override is always allowed.
222
*
223
* @return a SAXParserFactory instance.
224
*/
225
public static SAXParserFactory getSAXFactory(boolean overrideDefaultParser) {
226
boolean override = overrideDefaultParser;
227
String spSAXFactory = SecuritySupport.getJAXPSystemProperty(SAX_FACTORY_ID);
228
if (spSAXFactory != null && System.getSecurityManager() == null) {
229
override = true;
230
}
231
232
SAXParserFactory factory
233
= !override
234
? new SAXParserFactoryImpl()
235
: SAXParserFactory.newInstance();
236
factory.setNamespaceAware(true);
237
return factory;
238
}
239
240
public static SAXTransformerFactory getSAXTransformFactory(boolean overrideDefaultParser) {
241
SAXTransformerFactory tf = overrideDefaultParser
242
? (SAXTransformerFactory) SAXTransformerFactory.newInstance()
243
: (SAXTransformerFactory) new TransformerFactoryImpl();
244
try {
245
tf.setFeature(OVERRIDE_PARSER, overrideDefaultParser);
246
} catch (TransformerConfigurationException ex) {
247
// ignore since it'd never happen with the JDK impl.
248
}
249
return tf;
250
}
251
252
private static XMLReader getXMLReaderWSAXFactory(boolean overrideDefaultParser) {
253
SAXParserFactory saxFactory = getSAXFactory(overrideDefaultParser);
254
try {
255
return saxFactory.newSAXParser().getXMLReader();
256
} catch (ParserConfigurationException | SAXException ex) {
257
return getXMLReaderWXMLReaderFactory();
258
}
259
}
260
261
@SuppressWarnings("deprecation")
262
private static XMLReader getXMLReaderWXMLReaderFactory() {
263
try {
264
return org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
265
} catch (SAXException ex1) {
266
}
267
return null;
268
}
269
}
270
271