1 17 18 19 package org.apache.commons.digester; 20 21 22 import java.util.HashMap ; 23 24 import org.apache.commons.beanutils.BeanUtils; 25 import org.apache.commons.beanutils.PropertyUtils; 26 import org.xml.sax.Attributes ; 27 28 29 39 40 public class SetPropertiesRule extends Rule { 41 42 43 45 46 54 public SetPropertiesRule(Digester digester) { 55 56 this(); 57 58 } 59 60 61 64 public SetPropertiesRule() { 65 66 68 } 69 70 79 public SetPropertiesRule(String attributeName, String propertyName) { 80 81 attributeNames = new String [1]; 82 attributeNames[0] = attributeName; 83 propertyNames = new String [1]; 84 propertyNames[0] = propertyName; 85 } 86 87 124 public SetPropertiesRule(String [] attributeNames, String [] propertyNames) { 125 this.attributeNames = new String [attributeNames.length]; 127 for (int i=0, size=attributeNames.length; i<size; i++) { 128 this.attributeNames[i] = attributeNames[i]; 129 } 130 131 this.propertyNames = new String [propertyNames.length]; 132 for (int i=0, size=propertyNames.length; i<size; i++) { 133 this.propertyNames[i] = propertyNames[i]; 134 } 135 } 136 137 139 142 private String [] attributeNames; 143 146 private String [] propertyNames; 147 148 152 private boolean ignoreMissingProperty = true; 153 154 155 157 158 163 public void begin(Attributes attributes) throws Exception { 164 165 HashMap values = new HashMap (); 167 168 int attNamesLength = 0; 170 if (attributeNames != null) { 171 attNamesLength = attributeNames.length; 172 } 173 int propNamesLength = 0; 174 if (propertyNames != null) { 175 propNamesLength = propertyNames.length; 176 } 177 178 179 for (int i = 0; i < attributes.getLength(); i++) { 180 String name = attributes.getLocalName(i); 181 if ("".equals(name)) { 182 name = attributes.getQName(i); 183 } 184 String value = attributes.getValue(i); 185 186 for (int n = 0; n<attNamesLength; n++) { 188 if (name.equals(attributeNames[n])) { 189 if (n < propNamesLength) { 190 name = propertyNames[n]; 192 193 } else { 194 name = null; 197 } 198 break; 199 } 200 } 201 202 if (digester.log.isDebugEnabled()) { 203 digester.log.debug("[SetPropertiesRule]{" + digester.match + 204 "} Setting property '" + name + "' to '" + 205 value + "'"); 206 } 207 208 if ((!ignoreMissingProperty) && (name != null)) { 209 228 Object top = digester.peek(); 229 boolean test = PropertyUtils.isWriteable(top, name); 230 if (!test) 231 throw new NoSuchMethodException ("Property " + name + " can't be set"); 232 } 233 234 if (name != null) { 235 values.put(name, value); 236 } 237 } 238 239 Object top = digester.peek(); 241 if (digester.log.isDebugEnabled()) { 242 if (top != null) { 243 digester.log.debug("[SetPropertiesRule]{" + digester.match + 244 "} Set " + top.getClass().getName() + 245 " properties"); 246 } else { 247 digester.log.debug("[SetPropertiesRule]{" + digester.match + 248 "} Set NULL properties"); 249 } 250 } 251 BeanUtils.populate(top, values); 252 253 254 } 255 256 257 261 public void addAlias(String attributeName, String propertyName) { 262 263 if (attributeNames == null) { 267 268 attributeNames = new String [1]; 269 attributeNames[0] = attributeName; 270 propertyNames = new String [1]; 271 propertyNames[0] = propertyName; 272 273 } else { 274 int length = attributeNames.length; 275 String [] tempAttributes = new String [length + 1]; 276 for (int i=0; i<length; i++) { 277 tempAttributes[i] = attributeNames[i]; 278 } 279 tempAttributes[length] = attributeName; 280 281 String [] tempProperties = new String [length + 1]; 282 for (int i=0; i<length && i< propertyNames.length; i++) { 283 tempProperties[i] = propertyNames[i]; 284 } 285 tempProperties[length] = propertyName; 286 287 propertyNames = tempProperties; 288 attributeNames = tempAttributes; 289 } 290 } 291 292 293 296 public String toString() { 297 298 StringBuffer sb = new StringBuffer ("SetPropertiesRule["); 299 sb.append("]"); 300 return (sb.toString()); 301 302 } 303 304 312 public boolean isIgnoreMissingProperty() { 313 314 return this.ignoreMissingProperty; 315 } 316 317 325 public void setIgnoreMissingProperty(boolean ignoreMissingProperty) { 326 327 this.ignoreMissingProperty = ignoreMissingProperty; 328 } 329 330 331 } 332 | Popular Tags |