1 17 18 19 package org.apache.commons.digester; 20 21 22 import java.beans.PropertyDescriptor ; 23 24 import org.apache.commons.beanutils.BeanUtils; 25 import org.apache.commons.beanutils.DynaBean; 26 import org.apache.commons.beanutils.DynaProperty; 27 import org.apache.commons.beanutils.PropertyUtils; 28 import org.xml.sax.Attributes ; 29 30 31 35 36 public class SetPropertyRule extends Rule { 37 38 39 41 42 55 public SetPropertyRule(Digester digester, String name, String value) { 56 57 this(name, value); 58 59 } 60 61 70 public SetPropertyRule(String name, String value) { 71 72 this.name = name; 73 this.value = value; 74 75 } 76 77 79 80 83 protected String name = null; 84 85 86 89 protected String value = null; 90 91 92 94 95 103 public void begin(Attributes attributes) throws Exception { 104 105 String actualName = null; 107 String actualValue = null; 108 for (int i = 0; i < attributes.getLength(); i++) { 109 String name = attributes.getLocalName(i); 110 if ("".equals(name)) { 111 name = attributes.getQName(i); 112 } 113 String value = attributes.getValue(i); 114 if (name.equals(this.name)) { 115 actualName = value; 116 } else if (name.equals(this.value)) { 117 actualValue = value; 118 } 119 } 120 121 Object top = digester.peek(); 123 124 if (digester.log.isDebugEnabled()) { 126 digester.log.debug("[SetPropertyRule]{" + digester.match + 127 "} Set " + top.getClass().getName() + " property " + 128 actualName + " to " + actualValue); 129 } 130 131 if (top instanceof DynaBean) { 137 DynaProperty desc = 138 ((DynaBean) top).getDynaClass().getDynaProperty(actualName); 139 if (desc == null) { 140 throw new NoSuchMethodException 141 ("Bean has no property named " + actualName); 142 } 143 } else { 144 PropertyDescriptor desc = 145 PropertyUtils.getPropertyDescriptor(top, actualName); 146 if (desc == null) { 147 throw new NoSuchMethodException 148 ("Bean has no property named " + actualName); 149 } 150 } 151 152 BeanUtils.setProperty(top, actualName, actualValue); 154 155 } 156 157 158 161 public String toString() { 162 163 StringBuffer sb = new StringBuffer ("SetPropertyRule["); 164 sb.append("name="); 165 sb.append(name); 166 sb.append(", value="); 167 sb.append(value); 168 sb.append("]"); 169 return (sb.toString()); 170 171 } 172 173 174 } 175 | Popular Tags |