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