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