1 18 19 package org.apache.struts.action; 20 21 import java.lang.reflect.Array ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import javax.servlet.ServletRequest ; 28 import javax.servlet.http.HttpServletRequest ; 29 30 import org.apache.commons.beanutils.ConversionException; 31 import org.apache.commons.beanutils.DynaBean; 32 import org.apache.commons.beanutils.DynaClass; 33 import org.apache.commons.beanutils.DynaProperty; 34 import org.apache.struts.config.FormBeanConfig; 35 import org.apache.struts.config.FormPropertyConfig; 36 37 38 53 public class DynaActionForm extends ActionForm implements DynaBean { 54 55 56 58 59 63 protected DynaActionFormClass dynaClass = null; 64 65 66 70 protected HashMap dynaValues = new HashMap (); 71 72 73 75 76 83 public void initialize(ActionMapping mapping) { 84 85 String name = mapping.getName(); 86 if (name == null) { 87 return; 88 } 89 FormBeanConfig config = 90 mapping.getModuleConfig().findFormBeanConfig(name); 91 if (config == null) { 92 return; 93 } 94 95 initialize(config); 96 } 97 98 public void initialize(FormBeanConfig config) { 99 100 FormPropertyConfig props[] = config.findFormPropertyConfigs(); 101 for (int i = 0; i < props.length; i++) { 102 set(props[i].getName(), props[i].initial()); 103 } 104 105 } 106 107 108 111 122 public void reset(ActionMapping mapping, ServletRequest request) { 123 super.reset(mapping,request); 124 } 125 126 127 141 public void reset(ActionMapping mapping, HttpServletRequest request) { 142 super.reset(mapping,request); 143 } 144 145 146 148 149 159 public boolean contains(String name, String key) { 160 161 Object value = dynaValues.get(name); 162 if (value == null) { 163 throw new NullPointerException 164 ("No mapped value for '" + name + "(" + key + ")'"); 165 } else if (value instanceof Map ) { 166 return (((Map ) value).containsKey(key)); 167 } else { 168 throw new IllegalArgumentException 169 ("Non-mapped property for '" + name + "(" + key + ")'"); 170 } 171 172 } 173 174 175 185 public Object get(String name) { 186 187 Object value = dynaValues.get(name); 189 if (value != null) { 190 return (value); 191 } 192 193 Class type = getDynaProperty(name).getType(); 195 if (type == null) { 196 throw new NullPointerException 197 ("The type for property " + name + " is invalid"); 198 } 199 if (!type.isPrimitive()) { 200 return (value); 201 } 202 203 if (type == Boolean.TYPE) { 205 return (Boolean.FALSE); 206 } else if (type == Byte.TYPE) { 207 return (new Byte ((byte) 0)); 208 } else if (type == Character.TYPE) { 209 return (new Character ((char) 0)); 210 } else if (type == Double.TYPE) { 211 return (new Double (0.0)); 212 } else if (type == Float.TYPE) { 213 return (new Float ((float) 0.0)); 214 } else if (type == Integer.TYPE) { 215 return (new Integer (0)); 216 } else if (type == Long.TYPE) { 217 return (new Long (0)); 218 } else if (type == Short.TYPE) { 219 return (new Short ((short) 0)); 220 } else { 221 return (null); 222 } 223 224 } 225 226 227 243 public Object get(String name, int index) { 244 245 Object value = dynaValues.get(name); 246 if (value == null) { 247 throw new NullPointerException 248 ("No indexed value for '" + name + "[" + index + "]'"); 249 } else if (value.getClass().isArray()) { 250 return (Array.get(value, index)); 251 } else if (value instanceof List ) { 252 return ((List ) value).get(index); 253 } else { 254 throw new IllegalArgumentException 255 ("Non-indexed property for '" + name + "[" + index + "]'"); 256 } 257 258 } 259 260 261 274 public Object get(String name, String key) { 275 276 Object value = dynaValues.get(name); 277 if (value == null) { 278 throw new NullPointerException 279 ("No mapped value for '" + name + "(" + key + ")'"); 280 } else if (value instanceof Map ) { 281 return (((Map ) value).get(key)); 282 } else { 283 throw new IllegalArgumentException 284 ("Non-mapped property for '" + name + "(" + key + ")'"); 285 } 286 287 } 288 289 290 304 public String getString(String name) { 305 306 return (String ) this.get(name); 307 308 } 309 310 311 325 public String [] getStrings(String name) { 326 327 return (String []) this.get(name); 328 329 } 330 331 332 336 public DynaClass getDynaClass() { 337 338 return (this.dynaClass); 339 340 } 341 342 343 360 public Map getMap() { 361 362 return (dynaValues); 363 364 } 365 366 367 378 public void remove(String name, String key) { 379 380 Object value = dynaValues.get(name); 381 if (value == null) { 382 throw new NullPointerException 383 ("No mapped value for '" + name + "(" + key + ")'"); 384 } else if (value instanceof Map ) { 385 ((Map ) value).remove(key); 386 } else { 387 throw new IllegalArgumentException 388 ("Non-mapped property for '" + name + "(" + key + ")'"); 389 } 390 391 } 392 393 394 409 public void set(String name, Object value) { 410 411 DynaProperty descriptor = getDynaProperty(name); 412 if (descriptor.getType() == null) { 413 throw new NullPointerException 414 ("The type for property " + name + " is invalid"); 415 } 416 if (value == null) { 417 if (descriptor.getType().isPrimitive()) { 418 throw new NullPointerException 419 ("Primitive value for '" + name + "'"); 420 } 421 } else if (!isDynaAssignable(descriptor.getType(), value.getClass())) { 422 throw new ConversionException 423 ("Cannot assign value of type '" + 424 value.getClass().getName() + 425 "' to property '" + name + "' of type '" + 426 descriptor.getType().getName() + "'"); 427 } 428 dynaValues.put(name, value); 429 430 } 431 432 433 449 public void set(String name, int index, Object value) { 450 451 Object prop = dynaValues.get(name); 452 if (prop == null) { 453 throw new NullPointerException 454 ("No indexed value for '" + name + "[" + index + "]'"); 455 } else if (prop.getClass().isArray()) { 456 Array.set(prop, index, value); 457 } else if (prop instanceof List ) { 458 try { 459 ((List ) prop).set(index, value); 460 } catch (ClassCastException e) { 461 throw new ConversionException(e.getMessage()); 462 } 463 } else { 464 throw new IllegalArgumentException 465 ("Non-indexed property for '" + name + "[" + index + "]'"); 466 } 467 468 } 469 470 471 485 public void set(String name, String key, Object value) { 486 487 Object prop = dynaValues.get(name); 488 if (prop == null) { 489 throw new NullPointerException 490 ("No mapped value for '" + name + "(" + key + ")'"); 491 } else if (prop instanceof Map ) { 492 ((Map ) prop).put(key, value); 493 } else { 494 throw new IllegalArgumentException 495 ("Non-mapped property for '" + name + "(" + key + ")'"); 496 } 497 498 } 499 500 501 503 504 507 public String toString() { 508 509 StringBuffer sb = new StringBuffer ("DynaActionForm[dynaClass="); 510 DynaClass dynaClass = getDynaClass(); 511 if (dynaClass == null) { 512 return sb.append("null]").toString(); 513 } 514 515 sb.append(dynaClass.getName()); 516 DynaProperty props[] = dynaClass.getDynaProperties(); 517 if (props == null) { 518 props = new DynaProperty[0]; 519 } 520 for (int i = 0; i < props.length; i++) { 521 sb.append(','); 522 sb.append(props[i].getName()); 523 sb.append('='); 524 Object value = get(props[i].getName()); 525 if (value == null) { 526 sb.append("<NULL>"); 527 } else if (value.getClass().isArray()) { 528 int n = Array.getLength(value); 529 sb.append("{"); 530 for (int j = 0; j < n; j++) { 531 if (j > 0) { 532 sb.append(','); 533 } 534 sb.append(Array.get(value, j)); 535 } 536 sb.append("}"); 537 } else if (value instanceof List ) { 538 int n = ((List ) value).size(); 539 sb.append("{"); 540 for (int j = 0; j < n; j++) { 541 if (j > 0) { 542 sb.append(','); 543 } 544 sb.append(((List ) value).get(j)); 545 } 546 sb.append("}"); 547 } else if (value instanceof Map ) { 548 int n = 0; 549 Iterator keys = ((Map ) value).keySet().iterator(); 550 sb.append("{"); 551 while (keys.hasNext()) { 552 if (n > 0) { 553 sb.append(','); 554 } 555 n++; 556 Object key = keys.next(); 557 sb.append(key); 558 sb.append('='); 559 sb.append(((Map ) value).get(key)); 560 } 561 sb.append("}"); 562 } else { 563 sb.append(value); 564 } 565 } 566 sb.append("]"); 567 return (sb.toString()); 568 569 } 570 571 572 574 575 581 void setDynaActionFormClass(DynaActionFormClass dynaClass) { 582 583 this.dynaClass = dynaClass; 584 585 } 586 587 588 590 591 599 protected DynaProperty getDynaProperty(String name) { 600 601 DynaProperty descriptor = getDynaClass().getDynaProperty(name); 602 if (descriptor == null) { 603 throw new IllegalArgumentException 604 ("Invalid property name '" + name + "'"); 605 } 606 return (descriptor); 607 608 } 609 610 611 618 protected boolean isDynaAssignable(Class dest, Class source) { 619 620 if (dest.isAssignableFrom(source) || 621 ((dest == Boolean.TYPE) && (source == Boolean .class)) || 622 ((dest == Byte.TYPE) && (source == Byte .class)) || 623 ((dest == Character.TYPE) && (source == Character .class)) || 624 ((dest == Double.TYPE) && (source == Double .class)) || 625 ((dest == Float.TYPE) && (source == Float .class)) || 626 ((dest == Integer.TYPE) && (source == Integer .class)) || 627 ((dest == Long.TYPE) && (source == Long .class)) || 628 ((dest == Short.TYPE) && (source == Short .class))) { 629 return (true); 630 } else { 631 return (false); 632 } 633 634 } 635 636 637 } 638 | Popular Tags |