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/JdkXmlFeatures.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 javax.xml.XMLConstants;
29
import static jdk.xml.internal.JdkXmlUtils.OVERRIDE_PARSER;
30
31
/**
32
* This class manages JDK's XML Features. Previously added features and properties
33
* may be gradually moved to this class.
34
*/
35
public class JdkXmlFeatures {
36
public static final String ORACLE_JAXP_PROPERTY_PREFIX =
37
"http://www.oracle.com/xml/jaxp/properties/";
38
39
public static final String XML_FEATURE_MANAGER =
40
ORACLE_JAXP_PROPERTY_PREFIX + "XmlFeatureManager";
41
42
public static final String ORACLE_FEATURE_SERVICE_MECHANISM =
43
"http://www.oracle.com/feature/use-service-mechanism";
44
45
/**
46
* Feature enableExtensionFunctions
47
*/
48
public static final String ORACLE_ENABLE_EXTENSION_FUNCTION =
49
ORACLE_JAXP_PROPERTY_PREFIX + "enableExtensionFunctions";
50
public static final String SP_ENABLE_EXTENSION_FUNCTION =
51
"javax.xml.enableExtensionFunctions";
52
// This is the correct name by the spec
53
public static final String SP_ENABLE_EXTENSION_FUNCTION_SPEC =
54
"jdk.xml.enableExtensionFunctions";
55
56
public static enum XmlFeature {
57
/**
58
* Feature enableExtensionFunctions
59
* FSP: extension function is enforced by FSP. When FSP is on, extension
60
* function is disabled.
61
*/
62
ENABLE_EXTENSION_FUNCTION(ORACLE_ENABLE_EXTENSION_FUNCTION, SP_ENABLE_EXTENSION_FUNCTION_SPEC,
63
ORACLE_ENABLE_EXTENSION_FUNCTION, SP_ENABLE_EXTENSION_FUNCTION,
64
true, false, true, true),
65
66
/**
67
* Feature overrideDefaultParser
68
* FSP: not enforced by FSP.
69
*/
70
JDK_OVERRIDE_PARSER(OVERRIDE_PARSER, OVERRIDE_PARSER,
71
ORACLE_FEATURE_SERVICE_MECHANISM, ORACLE_FEATURE_SERVICE_MECHANISM,
72
false, false, true, false);
73
74
private final String name;
75
private final String nameSP;
76
private final String nameOld;
77
private final String nameOldSP;
78
private final boolean valueDefault;
79
private final boolean valueEnforced;
80
private final boolean hasSystem;
81
private final boolean enforced;
82
83
/**
84
* Constructs an XmlFeature instance.
85
* @param name the name of the feature
86
* @param nameSP the name of the System Property
87
* @param nameOld the name of the corresponding legacy property
88
* @param nameOldSP the system property of the legacy property
89
* @param value the value of the feature
90
* @param hasSystem a flag to indicate whether the feature is supported
91
* @param enforced a flag indicating whether the feature is
92
* FSP (Feature_Secure_Processing) enforced
93
* with a System property
94
*/
95
XmlFeature(String name, String nameSP, String nameOld, String nameOldSP,
96
boolean value, boolean valueEnforced, boolean hasSystem, boolean enforced) {
97
this.name = name;
98
this.nameSP = nameSP;
99
this.nameOld = nameOld;
100
this.nameOldSP = nameOldSP;
101
this.valueDefault = value;
102
this.valueEnforced = valueEnforced;
103
this.hasSystem = hasSystem;
104
this.enforced = enforced;
105
}
106
107
/**
108
* Checks whether the specified property is equal to the current property.
109
* @param propertyName the name of a property
110
* @return true if the specified property is the current property, false
111
* otherwise
112
*/
113
boolean equalsPropertyName(String propertyName) {
114
return name.equals(propertyName) ||
115
(nameOld != null && nameOld.equals(propertyName));
116
}
117
118
/**
119
* Returns the name of the property.
120
*
121
* @return the name of the property
122
*/
123
public String apiProperty() {
124
return name;
125
}
126
127
/**
128
* Returns the name of the corresponding System Property.
129
*
130
* @return the name of the System Property
131
*/
132
String systemProperty() {
133
return nameSP;
134
}
135
136
/**
137
* Returns the name of the legacy System Property.
138
*
139
* @return the name of the legacy System Property
140
*/
141
String systemPropertyOld() {
142
return nameOldSP;
143
}
144
145
/**
146
* Returns the default value of the property.
147
* @return the default value of the property
148
*/
149
public boolean defaultValue() {
150
return valueDefault;
151
}
152
153
/**
154
* Returns the FSP-enforced value.
155
* @return the FSP-enforced value
156
*/
157
public boolean enforcedValue() {
158
return valueEnforced;
159
}
160
161
/**
162
* Checks whether System property is supported for the feature.
163
* @return true it is supported, false otherwise
164
*/
165
boolean hasSystemProperty() {
166
return hasSystem;
167
}
168
169
/**
170
* Checks whether the property is enforced by FSP
171
* @return true it is, false otherwise
172
*/
173
boolean enforced() {
174
return enforced;
175
}
176
177
}
178
179
/**
180
* States of the settings of a property, in the order: default value, value
181
* set by FEATURE_SECURE_PROCESSING, jaxp.properties file, jaxp system
182
* properties, and jaxp api properties
183
*/
184
public static enum State {
185
//this order reflects the overriding order
186
187
DEFAULT("default"), FSP("FEATURE_SECURE_PROCESSING"),
188
JAXPDOTPROPERTIES("jaxp.properties"), SYSTEMPROPERTY("system property"),
189
APIPROPERTY("property");
190
191
final String literal;
192
State(String literal) {
193
this.literal = literal;
194
}
195
196
String literal() {
197
return literal;
198
}
199
}
200
201
/**
202
* Values of the features
203
*/
204
private final boolean[] featureValues;
205
206
/**
207
* States of the settings for each property
208
*/
209
private final State[] states;
210
211
/**
212
* Flag indicating if secure processing is set
213
*/
214
boolean secureProcessing;
215
216
/**
217
* Instantiate JdkXmlFeatures and initialize the fields
218
* @param secureProcessing
219
*/
220
public JdkXmlFeatures(boolean secureProcessing) {
221
featureValues = new boolean[XmlFeature.values().length];
222
states = new State[XmlFeature.values().length];
223
this.secureProcessing = secureProcessing;
224
for (XmlFeature f : XmlFeature.values()) {
225
if (secureProcessing && f.enforced()) {
226
featureValues[f.ordinal()] = f.enforcedValue();
227
states[f.ordinal()] = State.FSP;
228
} else {
229
featureValues[f.ordinal()] = f.defaultValue();
230
states[f.ordinal()] = State.DEFAULT;
231
}
232
}
233
//read system properties or jaxp.properties
234
readSystemProperties();
235
}
236
237
/**
238
* Updates the JdkXmlFeatures instance by reading the system properties again.
239
* This will become necessary in case the system properties are set after
240
* the instance has been created.
241
*/
242
public void update() {
243
readSystemProperties();
244
}
245
246
/**
247
* Set feature by property name and state
248
* @param propertyName property name
249
* @param state the state of the property
250
* @param value the value of the property
251
* @return true if the property is managed by the JdkXmlFeatures instance;
252
* false otherwise.
253
*/
254
public boolean setFeature(String propertyName, State state, Object value) {
255
int index = getIndex(propertyName);
256
if (index > -1) {
257
setFeature(index, state, value);
258
return true;
259
}
260
return false;
261
}
262
263
/**
264
* Set the value for a specific feature.
265
*
266
* @param feature the feature
267
* @param state the state of the property
268
* @param value the value of the property
269
*/
270
public void setFeature(XmlFeature feature, State state, boolean value) {
271
setFeature(feature.ordinal(), state, value);
272
}
273
274
/**
275
* Return the value of the specified property
276
*
277
* @param feature the property
278
* @return the value of the property
279
*/
280
public boolean getFeature(XmlFeature feature) {
281
return featureValues[feature.ordinal()];
282
}
283
284
/**
285
* Return the value of a feature by its index (the Feature's ordinal)
286
* @param index the index of a feature
287
* @return value of a feature
288
*/
289
public boolean getFeature(int index) {
290
return featureValues[index];
291
}
292
293
/**
294
* Set the value of a property by its index
295
*
296
* @param index the index of the property
297
* @param state the state of the property
298
* @param value the value of the property
299
*/
300
public void setFeature(int index, State state, Object value) {
301
boolean temp;
302
if (Boolean.class.isAssignableFrom(value.getClass())) {
303
temp = (Boolean)value;
304
} else {
305
temp = Boolean.parseBoolean((String) value);
306
}
307
setFeature(index, state, temp);
308
}
309
310
/**
311
* Set the value of a property by its index
312
*
313
* @param index the index of the property
314
* @param state the state of the property
315
* @param value the value of the property
316
*/
317
public void setFeature(int index, State state, boolean value) {
318
//only update if it shall override
319
if (state.compareTo(states[index]) >= 0) {
320
featureValues[index] = value;
321
states[index] = state;
322
}
323
}
324
325
/**
326
* Get the index by property name
327
*
328
* @param propertyName property name
329
* @return the index of the property if found; return -1 if not
330
*/
331
public int getIndex(String propertyName) {
332
for (XmlFeature feature : XmlFeature.values()) {
333
if (feature.equalsPropertyName(propertyName)) {
334
//internally, ordinal is used as index
335
return feature.ordinal();
336
}
337
}
338
return -1;
339
}
340
341
/**
342
* Read from system properties, or those in jaxp.properties
343
*/
344
private void readSystemProperties() {
345
for (XmlFeature feature : XmlFeature.values()) {
346
if (!getSystemProperty(feature, feature.systemProperty())) {
347
//if system property is not found, try the older form if any
348
String oldName = feature.systemPropertyOld();
349
if (oldName != null) {
350
getSystemProperty(feature, oldName);
351
}
352
}
353
}
354
}
355
356
/**
357
* Read from system properties, or those in jaxp.properties
358
*
359
* @param feature the feature get
360
* @param sysPropertyName the name of system property
361
* @return true if the system property is found, false otherwise
362
*/
363
private boolean getSystemProperty(XmlFeature feature, String sysPropertyName) {
364
try {
365
String value = SecuritySupport.getSystemProperty(sysPropertyName);
366
if (value != null && !value.equals("")) {
367
setFeature(feature, State.SYSTEMPROPERTY, Boolean.parseBoolean(value));
368
return true;
369
}
370
371
value = SecuritySupport.readJAXPProperty(sysPropertyName);
372
if (value != null && !value.equals("")) {
373
setFeature(feature, State.JAXPDOTPROPERTIES, Boolean.parseBoolean(value));
374
return true;
375
}
376
} catch (NumberFormatException e) {
377
//invalid setting
378
throw new NumberFormatException("Invalid setting for system property: " + feature.systemProperty());
379
}
380
return false;
381
}
382
383
}
384
385