1 16 package org.apache.struts.validator; 17 18 import java.util.List ; 19 import java.util.Map ; 20 import java.lang.reflect.Array ; 21 import java.io.Serializable ; 22 import javax.servlet.http.HttpServletRequest ; 23 import org.apache.struts.action.ActionMapping; 24 import org.apache.commons.beanutils.DynaClass; 25 import org.apache.commons.beanutils.DynaBean; 26 import org.apache.commons.beanutils.WrapDynaBean; 27 import org.apache.commons.beanutils.ConvertUtils; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 44 public class BeanValidatorForm extends ValidatorForm implements DynaBean, Serializable { 45 46 49 protected static Log logger = LogFactory.getLog(BeanValidatorForm.class); 50 51 54 protected DynaBean dynaBean; 55 56 60 protected boolean pathValidation = false; 61 62 64 67 public BeanValidatorForm(Object bean) { 68 69 if (bean instanceof DynaBean) { 70 71 dynaBean = (DynaBean)bean; 72 73 } else { 74 75 dynaBean = new WrapDynaBean(bean); 76 77 } 78 } 79 80 82 85 protected void setPathValidation(boolean pathValidation) { 86 this.pathValidation = pathValidation; 87 } 88 89 92 protected boolean isPathValidation() { 93 return pathValidation; 94 } 95 96 97 99 102 public DynaBean getDynaBean() { 103 return dynaBean; 104 } 105 106 113 public Object getInstance() { 114 115 if (dynaBean instanceof WrapDynaBean) { 116 return ((WrapDynaBean)dynaBean).getInstance(); 117 } 118 119 return dynaBean; 120 121 } 122 123 126 public int size(String name) { 127 128 Object value = dynaBean.get(name); 129 if (value == null) { 130 return 0; 131 } 132 133 if (value instanceof Map ) { 134 return ((Map )value).size(); 135 } 136 137 if (value instanceof List ) { 138 return ((List )value).size(); 139 } 140 141 if ((value.getClass().isArray())) { 142 return Array.getLength(value); 143 } 144 145 return 0; 146 147 } 148 149 151 158 public String getValidationKey(ActionMapping mapping, 159 HttpServletRequest request) { 160 161 String validationKey = null; 162 163 if (isPathValidation()) { 164 165 validationKey = mapping.getPath(); 167 168 if (validationKey.charAt(0) == '/') { 170 validationKey = validationKey.substring(1); 171 } 172 173 if (validationKey.indexOf("/") > 0) { 175 validationKey = validationKey.replace('/', '_'); 176 } 177 178 } else { 179 180 validationKey = mapping.getAttribute(); 181 182 } 183 184 if (logger.isDebugEnabled()) { 185 logger.debug("Validating ActionForm '" + mapping.getName() + 186 "' using key '" + validationKey + 187 "' for mapping '" + mapping.getPath() + "'"); 188 } 189 190 191 return validationKey; 192 193 } 194 195 197 201 public DynaClass getDynaClass() { 202 return dynaBean.getDynaClass(); 203 } 204 205 210 public Object get(String name) { 211 return dynaBean.get(name); 212 } 213 214 220 public Object get(String name, int index) { 221 return dynaBean.get(name, index); 222 } 223 224 231 public Object get(String name, String key) { 232 return dynaBean.get(name, key); 233 } 234 235 241 public void set(String name, Object value) { 242 243 if ("page".equals(name)) { 245 246 if (value == null) { 247 page = 0; 248 } else if (value instanceof Integer ) { 249 page = ((Integer )value).intValue(); 250 } else { 251 try { 252 page = ((Integer )ConvertUtils.convert(value.toString(), Integer .class)).intValue(); 253 } 254 catch (Exception ignore) { 255 page = 0; 256 } 257 } 258 } 259 260 dynaBean.set(name, value); 261 262 } 263 264 271 public void set(String name, int index, Object value) { 272 dynaBean.set(name, index, value); 273 } 274 275 282 public void set(String name, String key, Object value) { 283 dynaBean.set(name, key, value); 284 } 285 286 293 public boolean contains(String name, String key) { 294 return dynaBean.contains(name, key); 295 } 296 297 305 public void remove(String name, String key) { 306 dynaBean.remove(name, key); 307 } 308 } | Popular Tags |