Path: blob/master/src/java.xml/share/classes/jdk/xml/internal/JdkXmlFeatures.java
40948 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.xml.internal;2627import javax.xml.XMLConstants;28import jdk.xml.internal.JdkProperty.ImplPropMap;29import jdk.xml.internal.JdkProperty.State;30import static jdk.xml.internal.JdkXmlUtils.SP_USE_CATALOG;3132/**33* This class manages JDK's XML Features. Previously added features and properties34* may be gradually moved to this class.35*/36public class JdkXmlFeatures {37public static final String ORACLE_JAXP_PROPERTY_PREFIX =38"http://www.oracle.com/xml/jaxp/properties/";3940public static final String XML_FEATURE_MANAGER =41ORACLE_JAXP_PROPERTY_PREFIX + "XmlFeatureManager";4243public static final String CATALOG_FEATURES = "javax.xml.catalog.catalogFeatures";4445public final static String PROPERTY_USE_CATALOG = XMLConstants.USE_CATALOG;4647public static enum XmlFeature {48/**49* Feature enableExtensionFunctions50* FSP: extension function is enforced by FSP. When FSP is on, extension51* function is disabled.52*/53ENABLE_EXTENSION_FUNCTION(ImplPropMap.ENABLEEXTFUNC, null, null, true,54null, null, true, false, true, true),55/**56* The {@link javax.xml.XMLConstants.USE_CATALOG} feature.57* FSP: USE_CATALOG is not enforced by FSP.58*/59USE_CATALOG(null, PROPERTY_USE_CATALOG, SP_USE_CATALOG, false,60null, null, true, false, true, false),6162/**63* Feature resetSymbolTable64* FSP: RESET_SYMBOL_TABLE_FEATURE is not enforced by FSP.65*/66RESET_SYMBOL_TABLE_FEATURE(ImplPropMap.RESETSYMBOLTABLE, null, null, false,67null, null, false, false, true, false),6869/**70* Feature overrideDefaultParser71* FSP: not enforced by FSP.72*/73JDK_OVERRIDE_PARSER(ImplPropMap.OVERRIDEPARSER, null, null, false,74null, null, false, false, true, false);7576private final ImplPropMap implMap;77private final String name;78private final String nameSP;79private final boolean differ;80private final String nameOld;81private final String nameOldSP;82private final boolean valueDefault;83private final boolean valueEnforced;84private final boolean hasSystem;85private final boolean enforced;8687/**88* Constructs an XmlFeature instance.89* @param implMap the Implementation specific properties map. When the90* map is specified, there is no need to repeat or enter other name91* parameters.92* @param name the name of the feature93* @param nameSP the name of the System Property94* @param nameOld the name of the corresponding legacy property95* @param nameOldSP the system property of the legacy property96* @param value the value of the feature97* @param hasSystem a flag to indicate whether the feature is supported98* @param enforced a flag indicating whether the feature is99* FSP (Feature_Secure_Processing) enforced100* with a System property101*/102XmlFeature(ImplPropMap implMap, String name, String nameSP, boolean differ,103String nameOld, String nameOldSP, boolean value, boolean valueEnforced,104boolean hasSystem, boolean enforced) {105this.implMap = implMap;106if (implMap != null) {107this.name = implMap.qName();108this.nameSP = implMap.systemProperty();109this.nameOld = implMap.qNameOld();110this.nameOldSP = implMap.systemPropertyOld();111} else {112this.name = name;113this.nameSP = nameSP;114this.nameOld = nameOld;115this.nameOldSP = nameOldSP;116}117this.differ = differ;118this.valueDefault = value;119this.valueEnforced = valueEnforced;120this.hasSystem = hasSystem;121this.enforced = enforced;122}123124/**125* Checks whether the specified property is equal to the current property.126* @param propertyName the name of a property127* @return true if the specified property is the current property, false128* otherwise129*/130boolean equalsPropertyName(String propertyName) {131if (implMap != null) {132return implMap.is(propertyName);133}134return name.equals(propertyName) ||135(nameOld != null && nameOld.equals(propertyName));136}137138/**139* Returns the name of the property.140*141* @return the name of the property142*/143public String apiProperty() {144return name;145}146147/**148* Returns the name of the corresponding System Property.149*150* @return the name of the System Property151*/152String systemProperty() {153return nameSP;154}155156/**157* Returns the name of the legacy System Property.158*159* @return the name of the legacy System Property160*/161String systemPropertyOld() {162return nameOldSP;163}164165/**166* Returns the default value of the property.167* @return the default value of the property168*/169public boolean defaultValue() {170return valueDefault;171}172173/**174* Returns the FSP-enforced value.175* @return the FSP-enforced value176*/177public boolean enforcedValue() {178return valueEnforced;179}180181/**182* Checks whether System property is supported for the feature.183* @return true it is supported, false otherwise184*/185boolean hasSystemProperty() {186return hasSystem;187}188189/**190* Checks whether the property is enforced by FSP191* @return true it is, false otherwise192*/193boolean enforced() {194return enforced;195}196197/**198* Returns the state of a property name. By the specification as of JDK 17,199* the "jdk.xml." prefixed System property name is also the current API200* name. Both the URI-based qName and old name if any are legacy.201*202* @param name the property name203* @return the state of the property name, null if no match204*/205public State getState(String name) {206if (implMap != null) {207return implMap.getState(name);208} else if (this.name.equals(name)) {209return State.APIPROPERTY;210}211return null;212}213214}215216/**217* Values of the features218*/219private final boolean[] featureValues;220221/**222* States of the settings for each property223*/224private final State[] states;225226/**227* Flag indicating if secure processing is set228*/229boolean secureProcessing;230231/**232* Instantiate JdkXmlFeatures and initialize the fields233* @param secureProcessing234*/235public JdkXmlFeatures(boolean secureProcessing) {236featureValues = new boolean[XmlFeature.values().length];237states = new State[XmlFeature.values().length];238this.secureProcessing = secureProcessing;239for (XmlFeature f : XmlFeature.values()) {240if (secureProcessing && f.enforced()) {241featureValues[f.ordinal()] = f.enforcedValue();242states[f.ordinal()] = State.FSP;243} else {244featureValues[f.ordinal()] = f.defaultValue();245states[f.ordinal()] = State.DEFAULT;246}247}248//read system properties or jaxp.properties249readSystemProperties();250}251252/**253* Updates the JdkXmlFeatures instance by reading the system properties again.254* This will become necessary in case the system properties are set after255* the instance has been created.256*/257public void update() {258readSystemProperties();259}260261/**262* Set feature by property name and state263* @param propertyName property name264* @param state the state of the property265* @param value the value of the property266* @return true if the property is managed by the JdkXmlFeatures instance;267* false otherwise.268*/269public boolean setFeature(String propertyName, State state, Object value) {270State pState = state;271XmlFeature f = findByName(propertyName);272// if the feature is managed by JdkXmlFeatures273if (f != null) {274// if it's set from an API, get the correct state275if (state == State.APIPROPERTY) {276pState = f.getState(propertyName);277}278if (pState != null) {279setFeature(f.ordinal(), pState, value);280return true;281}282}283return false;284}285286/**287* Set the value for a specific feature.288*289* @param feature the feature290* @param state the state of the property291* @param value the value of the property292*/293public void setFeature(XmlFeature feature, State state, boolean value) {294setFeature(feature.ordinal(), state, value);295}296297/**298* Return the value of the specified property299*300* @param feature the property301* @return the value of the property302*/303public boolean getFeature(XmlFeature feature) {304return featureValues[feature.ordinal()];305}306307/**308* Return the value of a feature by its index (the Feature's ordinal)309* @param index the index of a feature310* @return value of a feature311*/312public boolean getFeature(int index) {313return featureValues[index];314}315316/**317* Set the value of a property by its index318*319* @param index the index of the property320* @param state the state of the property321* @param value the value of the property322*/323public void setFeature(int index, State state, Object value) {324boolean temp;325if (Boolean.class.isAssignableFrom(value.getClass())) {326temp = (Boolean)value;327} else {328temp = Boolean.parseBoolean((String) value);329}330setFeature(index, state, temp);331}332333/**334* Set the value of a property by its index335*336* @param index the index of the property337* @param state the state of the property338* @param value the value of the property339*/340public void setFeature(int index, State state, boolean value) {341//only update if it shall override342if (state.compareTo(states[index]) >= 0) {343featureValues[index] = value;344states[index] = state;345}346}347348/**349* Finds the feature by string name.350*351* @param propertyName property name352* @return the feature if found; null otherwise353*/354public XmlFeature findByName(String propertyName) {355for (XmlFeature feature : XmlFeature.values()) {356if (feature.equalsPropertyName(propertyName)) {357return feature;358}359}360return null;361}362363/**364* Get the index by property name365*366* @param propertyName property name367* @return the index of the property if found; return -1 if not368*/369public int getIndex(String propertyName) {370for (XmlFeature feature : XmlFeature.values()) {371if (feature.equalsPropertyName(propertyName)) {372//internally, ordinal is used as index373return feature.ordinal();374}375}376return -1;377}378379/**380* Read from system properties, or those in jaxp.properties381*/382private void readSystemProperties() {383for (XmlFeature feature : XmlFeature.values()) {384if (!getSystemProperty(feature, feature.systemProperty())) {385//if system property is not found, try the older form if any386String oldName = feature.systemPropertyOld();387if (oldName != null) {388getSystemProperty(feature, oldName);389}390}391}392}393394/**395* Read from system properties, or those in jaxp.properties396*397* @param property the type of the property398* @param sysPropertyName the name of system property399* @return true if the system property is found, false otherwise400*/401private boolean getSystemProperty(XmlFeature feature, String sysPropertyName) {402try {403String value = SecuritySupport.getSystemProperty(sysPropertyName);404if (value != null && !value.isEmpty()) {405setFeature(feature, State.SYSTEMPROPERTY, Boolean.parseBoolean(value));406return true;407}408409value = SecuritySupport.readJAXPProperty(sysPropertyName);410if (value != null && !value.isEmpty()) {411setFeature(feature, State.JAXPDOTPROPERTIES, Boolean.parseBoolean(value));412return true;413}414} catch (NumberFormatException e) {415//invalid setting416throw new NumberFormatException("Invalid setting for system property: " + feature.systemProperty());417}418return false;419}420421}422423424