1 /* 2 * Copyright (c) 2011, 2013, 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 com.sun.org.apache.xalan.internal.utils; 27 28 import com.sun.org.apache.xalan.internal.XalanConstants; 29 30 /** 31 * This is the base class for features and properties 32 * 33 */ 34 public abstract class FeaturePropertyBase { 35 36 /** 37 * States of the settings of a property, in the order: default value, value 38 * set by FEATURE_SECURE_PROCESSING, jaxp.properties file, jaxp system 39 * properties, and jaxp api properties 40 */ 41 public static enum State { 42 //this order reflects the overriding order 43 DEFAULT, FSP, JAXPDOTPROPERTIES, SYSTEMPROPERTY, APIPROPERTY 44 } 45 46 47 /** 48 * Values of the properties as defined in enum Properties 49 */ 50 String[] values = null; 51 /** 52 * States of the settings for each property in Properties above 53 */ 54 State[] states = {State.DEFAULT, State.DEFAULT}; 55 56 57 /** 58 * Set the value for a specific property. 59 * 60 * @param property the property 61 * @param state the state of the property 62 * @param value the value of the property 63 */ setValue(Enum property, State state, String value)64 public void setValue(Enum property, State state, String value) { 65 //only update if it shall override 66 if (state.compareTo(states[property.ordinal()]) >= 0) { 67 values[property.ordinal()] = value; 68 states[property.ordinal()] = state; 69 } 70 } 71 72 /** 73 * Set the value of a property by its index 74 * @param index the index of the property 75 * @param state the state of the property 76 * @param value the value of the property 77 */ setValue(int index, State state, String value)78 public void setValue(int index, State state, String value) { 79 //only update if it shall override 80 if (state.compareTo(states[index]) >= 0) { 81 values[index] = value; 82 states[index] = state; 83 } 84 } 85 86 /** 87 * Set value by property name and state 88 * @param propertyName property name 89 * @param state the state of the property 90 * @param value the value of the property 91 * @return true if the property is managed by the security property manager; 92 * false if otherwise. 93 */ setValue(String propertyName, State state, Object value)94 public boolean setValue(String propertyName, State state, Object value) { 95 int index = getIndex(propertyName); 96 if (index > -1) { 97 setValue(index, state, (String)value); 98 return true; 99 } 100 return false; 101 } 102 103 /** 104 * Set value by property name and state 105 * @param propertyName property name 106 * @param state the state of the property 107 * @param value the value of the property 108 * @return true if the property is managed by the security property manager; 109 * false if otherwise. 110 */ setValue(String propertyName, State state, boolean value)111 public boolean setValue(String propertyName, State state, boolean value) { 112 int index = getIndex(propertyName); 113 if (index > -1) { 114 if (value) { 115 setValue(index, state, XalanConstants.FEATURE_TRUE); 116 } else { 117 setValue(index, state, XalanConstants.FEATURE_FALSE); 118 } 119 return true; 120 } 121 return false; 122 } 123 124 /** 125 * Return the value of the specified property 126 * 127 * @param property the property 128 * @return the value of the property 129 */ getValue(Enum property)130 public String getValue(Enum property) { 131 return values[property.ordinal()]; 132 } 133 134 /** 135 * Return the value of the specified property 136 * 137 * @param property the property 138 * @return the value of the property 139 */ getValue(String property)140 public String getValue(String property) { 141 int index = getIndex(property); 142 if (index > -1) { 143 return getValueByIndex(index); 144 } 145 return null; 146 } 147 148 /** 149 * Return the value of the specified property. 150 * 151 * @param propertyName the property name 152 * @return the value of the property as a string. If a property is managed 153 * by this manager, its value shall not be null. 154 */ getValueAsString(String propertyName)155 public String getValueAsString(String propertyName) { 156 int index = getIndex(propertyName); 157 if (index > -1) { 158 return getValueByIndex(index); 159 } 160 161 return null; 162 } 163 164 /** 165 * Return the value of a property by its ordinal 166 * @param index the index of a property 167 * @return value of a property 168 */ getValueByIndex(int index)169 public String getValueByIndex(int index) { 170 return values[index]; 171 } 172 173 /** 174 * Get the index by property name 175 * @param propertyName property name 176 * @return the index of the property if found; return -1 if not 177 */ getIndex(String propertyName)178 public abstract int getIndex(String propertyName); 179 getIndex(Class<E> property, String propertyName)180 public <E extends Enum<E>> int getIndex(Class<E> property, String propertyName) { 181 for (Enum<E> enumItem : property.getEnumConstants()) { 182 if (enumItem.toString().equals(propertyName)) { 183 //internally, ordinal is used as index 184 return enumItem.ordinal(); 185 } 186 } 187 return -1; 188 }; 189 190 191 /** 192 * Read from system properties, or those in jaxp.properties 193 * 194 * @param property the property 195 * @param systemProperty the name of the system property 196 */ getSystemProperty(Enum property, String systemProperty)197 void getSystemProperty(Enum property, String systemProperty) { 198 try { 199 String value = SecuritySupport.getSystemProperty(systemProperty); 200 if (value != null) { 201 values[property.ordinal()] = value; 202 states[property.ordinal()] = State.SYSTEMPROPERTY; 203 return; 204 } 205 206 value = SecuritySupport.readJAXPProperty(systemProperty); 207 if (value != null) { 208 values[property.ordinal()] = value; 209 states[property.ordinal()] = State.JAXPDOTPROPERTIES; 210 } 211 } catch (NumberFormatException e) { 212 //invalid setting ignored 213 } 214 } 215 } 216