Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/jdk/xml/internal/JdkXmlFeatures.java
38813 views
/*1* Copyright (c) 2017, 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 static jdk.xml.internal.JdkXmlUtils.OVERRIDE_PARSER;2930/**31* This class manages JDK's XML Features. Previously added features and properties32* may be gradually moved to this class.33*/34public class JdkXmlFeatures {35public static final String ORACLE_JAXP_PROPERTY_PREFIX =36"http://www.oracle.com/xml/jaxp/properties/";3738public static final String XML_FEATURE_MANAGER =39ORACLE_JAXP_PROPERTY_PREFIX + "XmlFeatureManager";4041public static final String ORACLE_FEATURE_SERVICE_MECHANISM =42"http://www.oracle.com/feature/use-service-mechanism";4344/**45* Feature enableExtensionFunctions46*/47public static final String ORACLE_ENABLE_EXTENSION_FUNCTION =48ORACLE_JAXP_PROPERTY_PREFIX + "enableExtensionFunctions";49public static final String SP_ENABLE_EXTENSION_FUNCTION =50"javax.xml.enableExtensionFunctions";51// This is the correct name by the spec52public static final String SP_ENABLE_EXTENSION_FUNCTION_SPEC =53"jdk.xml.enableExtensionFunctions";5455public static enum XmlFeature {56/**57* Feature enableExtensionFunctions58* FSP: extension function is enforced by FSP. When FSP is on, extension59* function is disabled.60*/61ENABLE_EXTENSION_FUNCTION(ORACLE_ENABLE_EXTENSION_FUNCTION, SP_ENABLE_EXTENSION_FUNCTION_SPEC,62ORACLE_ENABLE_EXTENSION_FUNCTION, SP_ENABLE_EXTENSION_FUNCTION,63true, false, true, true),6465/**66* Feature overrideDefaultParser67* FSP: not enforced by FSP.68*/69JDK_OVERRIDE_PARSER(OVERRIDE_PARSER, OVERRIDE_PARSER,70ORACLE_FEATURE_SERVICE_MECHANISM, ORACLE_FEATURE_SERVICE_MECHANISM,71false, false, true, false);7273private final String name;74private final String nameSP;75private final String nameOld;76private final String nameOldSP;77private final boolean valueDefault;78private final boolean valueEnforced;79private final boolean hasSystem;80private final boolean enforced;8182/**83* Constructs an XmlFeature instance.84* @param name the name of the feature85* @param nameSP the name of the System Property86* @param nameOld the name of the corresponding legacy property87* @param nameOldSP the system property of the legacy property88* @param value the value of the feature89* @param hasSystem a flag to indicate whether the feature is supported90* @param enforced a flag indicating whether the feature is91* FSP (Feature_Secure_Processing) enforced92* with a System property93*/94XmlFeature(String name, String nameSP, String nameOld, String nameOldSP,95boolean value, boolean valueEnforced, boolean hasSystem, boolean enforced) {96this.name = name;97this.nameSP = nameSP;98this.nameOld = nameOld;99this.nameOldSP = nameOldSP;100this.valueDefault = value;101this.valueEnforced = valueEnforced;102this.hasSystem = hasSystem;103this.enforced = enforced;104}105106/**107* Checks whether the specified property is equal to the current property.108* @param propertyName the name of a property109* @return true if the specified property is the current property, false110* otherwise111*/112boolean equalsPropertyName(String propertyName) {113return name.equals(propertyName) ||114(nameOld != null && nameOld.equals(propertyName));115}116117/**118* Returns the name of the property.119*120* @return the name of the property121*/122public String apiProperty() {123return name;124}125126/**127* Returns the name of the corresponding System Property.128*129* @return the name of the System Property130*/131String systemProperty() {132return nameSP;133}134135/**136* Returns the name of the legacy System Property.137*138* @return the name of the legacy System Property139*/140String systemPropertyOld() {141return nameOldSP;142}143144/**145* Returns the default value of the property.146* @return the default value of the property147*/148public boolean defaultValue() {149return valueDefault;150}151152/**153* Returns the FSP-enforced value.154* @return the FSP-enforced value155*/156public boolean enforcedValue() {157return valueEnforced;158}159160/**161* Checks whether System property is supported for the feature.162* @return true it is supported, false otherwise163*/164boolean hasSystemProperty() {165return hasSystem;166}167168/**169* Checks whether the property is enforced by FSP170* @return true it is, false otherwise171*/172boolean enforced() {173return enforced;174}175176}177178/**179* States of the settings of a property, in the order: default value, value180* set by FEATURE_SECURE_PROCESSING, jaxp.properties file, jaxp system181* properties, and jaxp api properties182*/183public static enum State {184//this order reflects the overriding order185186DEFAULT("default"), FSP("FEATURE_SECURE_PROCESSING"),187JAXPDOTPROPERTIES("jaxp.properties"), SYSTEMPROPERTY("system property"),188APIPROPERTY("property");189190final String literal;191State(String literal) {192this.literal = literal;193}194195String literal() {196return literal;197}198}199200/**201* Values of the features202*/203private final boolean[] featureValues;204205/**206* States of the settings for each property207*/208private final State[] states;209210/**211* Flag indicating if secure processing is set212*/213boolean secureProcessing;214215/**216* Instantiate JdkXmlFeatures and initialize the fields217* @param secureProcessing218*/219public JdkXmlFeatures(boolean secureProcessing) {220featureValues = new boolean[XmlFeature.values().length];221states = new State[XmlFeature.values().length];222this.secureProcessing = secureProcessing;223for (XmlFeature f : XmlFeature.values()) {224if (secureProcessing && f.enforced()) {225featureValues[f.ordinal()] = f.enforcedValue();226states[f.ordinal()] = State.FSP;227} else {228featureValues[f.ordinal()] = f.defaultValue();229states[f.ordinal()] = State.DEFAULT;230}231}232//read system properties or jaxp.properties233readSystemProperties();234}235236/**237* Updates the JdkXmlFeatures instance by reading the system properties again.238* This will become necessary in case the system properties are set after239* the instance has been created.240*/241public void update() {242readSystemProperties();243}244245/**246* Set feature by property name and state247* @param propertyName property name248* @param state the state of the property249* @param value the value of the property250* @return true if the property is managed by the JdkXmlFeatures instance;251* false otherwise.252*/253public boolean setFeature(String propertyName, State state, Object value) {254int index = getIndex(propertyName);255if (index > -1) {256setFeature(index, state, value);257return true;258}259return false;260}261262/**263* Set the value for a specific feature.264*265* @param feature the feature266* @param state the state of the property267* @param value the value of the property268*/269public void setFeature(XmlFeature feature, State state, boolean value) {270setFeature(feature.ordinal(), state, value);271}272273/**274* Return the value of the specified property275*276* @param feature the property277* @return the value of the property278*/279public boolean getFeature(XmlFeature feature) {280return featureValues[feature.ordinal()];281}282283/**284* Return the value of a feature by its index (the Feature's ordinal)285* @param index the index of a feature286* @return value of a feature287*/288public boolean getFeature(int index) {289return featureValues[index];290}291292/**293* Set the value of a property by its index294*295* @param index the index of the property296* @param state the state of the property297* @param value the value of the property298*/299public void setFeature(int index, State state, Object value) {300boolean temp;301if (Boolean.class.isAssignableFrom(value.getClass())) {302temp = (Boolean)value;303} else {304temp = Boolean.parseBoolean((String) value);305}306setFeature(index, state, temp);307}308309/**310* Set the value of a property by its index311*312* @param index the index of the property313* @param state the state of the property314* @param value the value of the property315*/316public void setFeature(int index, State state, boolean value) {317//only update if it shall override318if (state.compareTo(states[index]) >= 0) {319featureValues[index] = value;320states[index] = state;321}322}323324/**325* Get the index by property name326*327* @param propertyName property name328* @return the index of the property if found; return -1 if not329*/330public int getIndex(String propertyName) {331for (XmlFeature feature : XmlFeature.values()) {332if (feature.equalsPropertyName(propertyName)) {333//internally, ordinal is used as index334return feature.ordinal();335}336}337return -1;338}339340/**341* Read from system properties, or those in jaxp.properties342*/343private void readSystemProperties() {344for (XmlFeature feature : XmlFeature.values()) {345if (!getSystemProperty(feature, feature.systemProperty())) {346//if system property is not found, try the older form if any347String oldName = feature.systemPropertyOld();348if (oldName != null) {349getSystemProperty(feature, oldName);350}351}352}353}354355/**356* Read from system properties, or those in jaxp.properties357*358* @param feature the feature get359* @param sysPropertyName the name of system property360* @return true if the system property is found, false otherwise361*/362private boolean getSystemProperty(XmlFeature feature, String sysPropertyName) {363try {364String value = SecuritySupport.getSystemProperty(sysPropertyName);365if (value != null && !value.equals("")) {366setFeature(feature, State.SYSTEMPROPERTY, Boolean.parseBoolean(value));367return true;368}369370value = SecuritySupport.readJAXPProperty(sysPropertyName);371if (value != null && !value.equals("")) {372setFeature(feature, State.JAXPDOTPROPERTIES, Boolean.parseBoolean(value));373return true;374}375} catch (NumberFormatException e) {376//invalid setting377throw new NumberFormatException("Invalid setting for system property: " + feature.systemProperty());378}379return false;380}381382}383384385