1 18 19 20 package org.apache.tomcat.util.digester; 21 22 23 import org.apache.tomcat.util.IntrospectionUtils; 24 import org.xml.sax.Attributes ; 25 26 27 37 38 public class SetPropertiesRule extends Rule { 39 40 41 43 44 52 public SetPropertiesRule(Digester digester) { 53 54 this(); 55 56 } 57 58 59 62 public SetPropertiesRule() { 63 64 66 } 67 68 77 public SetPropertiesRule(String attributeName, String propertyName) { 78 79 attributeNames = new String [1]; 80 attributeNames[0] = attributeName; 81 propertyNames = new String [1]; 82 propertyNames[0] = propertyName; 83 } 84 85 122 public SetPropertiesRule(String [] attributeNames, String [] propertyNames) { 123 this.attributeNames = new String [attributeNames.length]; 125 for (int i=0, size=attributeNames.length; i<size; i++) { 126 this.attributeNames[i] = attributeNames[i]; 127 } 128 129 this.propertyNames = new String [propertyNames.length]; 130 for (int i=0, size=propertyNames.length; i<size; i++) { 131 this.propertyNames[i] = propertyNames[i]; 132 } 133 } 134 135 137 140 private String [] attributeNames; 141 144 private String [] propertyNames; 145 146 147 149 150 155 public void begin(Attributes attributes) throws Exception { 156 157 Object top = digester.peek(); 159 if (digester.log.isDebugEnabled()) { 160 if (top != null) { 161 digester.log.debug("[SetPropertiesRule]{" + digester.match + 162 "} Set " + top.getClass().getName() + 163 " properties"); 164 } else { 165 digester.log.debug("[SetPropertiesRule]{" + digester.match + 166 "} Set NULL properties"); 167 } 168 } 169 170 int attNamesLength = 0; 172 if (attributeNames != null) { 173 attNamesLength = attributeNames.length; 174 } 175 int propNamesLength = 0; 176 if (propertyNames != null) { 177 propNamesLength = propertyNames.length; 178 } 179 180 for (int i = 0; i < attributes.getLength(); i++) { 181 String name = attributes.getLocalName(i); 182 if ("".equals(name)) { 183 name = attributes.getQName(i); 184 } 185 String value = attributes.getValue(i); 186 187 for (int n = 0; n<attNamesLength; n++) { 189 if (name.equals(attributeNames[n])) { 190 if (n < propNamesLength) { 191 name = propertyNames[n]; 193 194 } else { 195 name = null; 198 } 199 break; 200 } 201 } 202 203 if (digester.log.isDebugEnabled()) { 204 digester.log.debug("[SetPropertiesRule]{" + digester.match + 205 "} Setting property '" + name + "' to '" + 206 value + "'"); 207 } 208 IntrospectionUtils.setProperty(top, name, value); 209 } 210 211 } 212 213 214 218 public void addAlias(String attributeName, String propertyName) { 219 220 if (attributeNames == null) { 224 225 attributeNames = new String [1]; 226 attributeNames[0] = attributeName; 227 propertyNames = new String [1]; 228 propertyNames[0] = propertyName; 229 230 } else { 231 int length = attributeNames.length; 232 String [] tempAttributes = new String [length + 1]; 233 for (int i=0; i<length; i++) { 234 tempAttributes[i] = attributeNames[i]; 235 } 236 tempAttributes[length] = attributeName; 237 238 String [] tempProperties = new String [length + 1]; 239 for (int i=0; i<length && i< propertyNames.length; i++) { 240 tempProperties[i] = propertyNames[i]; 241 } 242 tempProperties[length] = propertyName; 243 244 propertyNames = tempProperties; 245 attributeNames = tempAttributes; 246 } 247 } 248 249 250 253 public String toString() { 254 255 StringBuffer sb = new StringBuffer ("SetPropertiesRule["); 256 sb.append("]"); 257 return (sb.toString()); 258 259 } 260 261 262 } 263 | Popular Tags |