1 package org.apache.turbine.services.intake.model; 2 3 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 22 import java.util.Locale ; 23 24 import org.apache.commons.lang.StringUtils; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.apache.turbine.om.Retrievable; 30 import org.apache.turbine.services.TurbineServices; 31 import org.apache.turbine.services.intake.IntakeException; 32 import org.apache.turbine.services.intake.TurbineIntake; 33 import org.apache.turbine.services.intake.validator.DefaultValidator; 34 import org.apache.turbine.services.intake.validator.InitableByConstraintMap; 35 import org.apache.turbine.services.intake.validator.ValidationException; 36 import org.apache.turbine.services.intake.validator.Validator; 37 import org.apache.turbine.services.intake.xmlmodel.Rule; 38 import org.apache.turbine.services.intake.xmlmodel.XmlField; 39 import org.apache.turbine.services.localization.Localization; 40 import org.apache.turbine.services.localization.LocalizationService; 41 import org.apache.turbine.util.SystemError; 42 import org.apache.turbine.util.parser.ParameterParser; 43 import org.apache.turbine.util.parser.ValueParser; 44 45 54 public abstract class Field 55 { 56 57 private static final String EMPTY = ""; 58 59 60 private static final String VALUE_IF_ABSENT_KEY = "_vifa_"; 61 62 63 public static final String defaultFieldPackage = "org.apache.turbine.services.intake.validator."; 64 65 67 68 protected final String name; 69 70 71 protected final String key; 72 73 74 protected String displayName; 75 76 77 protected final String mapToObject; 78 79 80 protected Validator validator; 81 82 83 protected final Method getter; 84 85 86 protected final Method setter; 87 88 89 protected String ifRequiredMessage; 90 91 92 protected final boolean isMultiValued; 93 94 95 protected final Group group; 96 97 98 protected boolean alwaysRequired; 99 100 104 protected Object onError; 105 106 107 protected Object defaultValue; 108 109 110 protected Object emptyValue; 111 112 113 private String displaySize; 114 115 116 private String maxSize; 117 118 120 121 protected boolean setFlag; 122 123 124 protected boolean validFlag; 125 126 127 protected boolean required; 128 129 130 protected boolean initialized; 131 132 133 protected String message; 134 135 136 protected Retrievable retrievable; 137 138 private Locale locale; 139 140 private String stringValue; 141 142 private String [] stringValues; 143 144 private Object validValue; 145 146 private Object testValue; 147 148 private Object [] valArray; 149 150 protected ValueParser parser; 151 152 153 protected Log log = LogFactory.getLog(this.getClass()); 154 protected boolean isDebugEnabled = false; 155 156 167 public Field(XmlField field, Group group) throws IntakeException 168 { 169 isDebugEnabled = log.isDebugEnabled(); 170 171 this.group = group; 172 key = field.getKey(); 173 name = field.getName(); 174 displayName = field.getDisplayName(); 175 displaySize = field.getDisplaySize(); 176 isMultiValued = field.isMultiValued(); 177 178 try 179 { 180 setDefaultValue(field.getDefaultValue()); 181 } 182 catch (RuntimeException e) 183 { 184 log.error("Could not set default value of " + 185 this.getDisplayName() + " to " 186 + field.getDefaultValue(), e); 187 } 188 189 try 190 { 191 setEmptyValue(field.getEmptyValue()); 192 } 193 catch (RuntimeException e) 194 { 195 log.error("Could not set empty value of " + 196 this.getDisplayName() + " to " 197 + field.getEmptyValue(), e); 198 } 199 200 String validatorClassName = field.getValidator(); 201 if (validatorClassName == null && field.getRules().size() > 0) 202 { 203 validatorClassName = getDefaultValidator(); 204 } 205 else if (validatorClassName != null 206 && validatorClassName.indexOf('.') == -1) 207 { 208 validatorClassName = defaultFieldPackage + validatorClassName; 209 } 210 211 if (validatorClassName != null) 212 { 213 try 214 { 215 validator = (Validator) 216 Class.forName(validatorClassName).newInstance(); 217 } 218 catch (InstantiationException e) 219 { 220 throw new IntakeException( 221 "Could not create new instance of Validator(" 222 + validatorClassName + ")", e); 223 } 224 catch (IllegalAccessException e) 225 { 226 throw new IntakeException( 227 "Could not create new instance of Validator(" 228 + validatorClassName + ")", e); 229 } 230 catch (ClassNotFoundException e) 231 { 232 throw new IntakeException( 233 "Could not load Validator class(" 234 + validatorClassName + ")", e); 235 } 236 if (validator instanceof InitableByConstraintMap) 239 { 240 ((InitableByConstraintMap) validator).init(field.getRuleMap()); 241 } 242 else 243 { 244 throw new SystemError( 245 "All Validation objects must be subclasses of " 246 + "InitableByConstraintMap"); 247 } 248 } 249 250 Rule reqRule = (Rule) field.getRuleMap().get("required"); 252 if (reqRule != null) 253 { 254 alwaysRequired = new Boolean (reqRule.getValue()).booleanValue(); 255 ifRequiredMessage = reqRule.getMessage(); 256 } 257 258 Rule maxLengthRule = (Rule) field.getRuleMap().get("maxLength"); 259 if (maxLengthRule != null) 260 { 261 maxSize = maxLengthRule.getValue(); 262 } 263 264 mapToObject = field.getMapToObject(); 266 String propName = field.getMapToProperty(); 267 Method tmpGetter = null; 268 Method tmpSetter = null; 269 if (StringUtils.isNotEmpty(mapToObject) 270 && StringUtils.isNotEmpty(propName)) 271 { 272 try 273 { 274 tmpGetter = TurbineIntake.getFieldGetter(mapToObject, propName); 275 } 276 catch (Exception e) 277 { 278 log.error("IntakeService could not map the getter for field " 279 + this.getDisplayName() + " in group " 280 + this.group.getIntakeGroupName() 281 + " to the property " + propName + " in object " 282 + mapToObject, e); 283 } 284 try 285 { 286 tmpSetter = TurbineIntake.getFieldSetter(mapToObject, propName); 287 } 288 catch (Exception e) 289 { 290 log.error("IntakeService could not map the setter for field " 291 + this.getDisplayName() + " in group " 292 + this.group.getIntakeGroupName() 293 + " to the property " + propName + " in object " 294 + mapToObject, e); 295 } 296 } 297 getter = tmpGetter; 298 setter = tmpSetter; 299 300 valArray = new Object [1]; 301 } 302 303 314 public Field init(ValueParser pp) 315 throws IntakeException 316 { 317 this.parser = pp; 318 validFlag = true; 319 320 if (TurbineServices.getInstance() 323 .isRegistered(LocalizationService.SERVICE_NAME)) 324 { 325 if (pp instanceof ParameterParser) 326 { 327 this.locale = Localization.getLocale 328 (((ParameterParser) pp).getRequest()); 329 } 330 else 331 { 332 this.locale = Localization.getLocale((String ) null); 333 } 334 } 335 336 if (pp.containsKey(getKey())) 337 { 338 if (isDebugEnabled) 339 { 340 log.debug(name + ": Found our Key in the request, setting Value"); 341 } 342 if (StringUtils.isNotEmpty(pp.getString(getKey()))) 343 { 344 setFlag = true; 345 } 346 validate(); 347 } 348 else if (pp.containsKey(getValueIfAbsent()) && 349 pp.getString(getValueIfAbsent()) != null) 350 { 351 pp.add(getKey(), pp.getString(getValueIfAbsent())); 352 setFlag = true; 353 validate(); 354 } 355 356 initialized = true; 357 return this; 358 } 359 360 369 public Field init(Retrievable obj) 370 { 371 if (!initialized) 372 { 373 validFlag = true; 374 } 375 retrievable = obj; 376 return this; 377 } 378 379 385 protected Locale getLocale() 386 { 387 return locale; 388 } 389 390 395 protected String getDefaultValidator() 396 { 397 return DefaultValidator.class.getName(); 398 } 399 400 404 public Validator getValidator() 405 { 406 return validator; 407 } 408 409 414 public boolean isRequired() 415 { 416 return alwaysRequired || required; 417 } 418 419 426 public void setRequired(boolean v) 427 { 428 setRequired(v, ifRequiredMessage); 429 } 430 431 437 public void setRequired(boolean v, String message) 438 { 439 this.required = v; 440 if (v && (!setFlag || null == getTestValue())) 441 { 442 validFlag = false; 443 this.message = message; 444 } 445 } 446 447 451 public void removeFromRequest() 452 { 453 parser.remove(getKey()); 454 parser.remove(getKey()+ VALUE_IF_ABSENT_KEY); 455 } 456 457 462 public void dispose() 463 { 464 parser = null; 465 initialized = false; 466 setFlag = false; 467 validFlag = false; 468 required = false; 469 message = null; 470 retrievable = null; 471 472 locale = null; 473 stringValue = null; 474 stringValues = null; 475 validValue = null; 476 testValue = null; 477 valArray[0] = null; 478 } 479 480 485 public String getKey() 486 { 487 return (group == null) ? key : group.getObjectKey() + key; 488 } 489 490 495 public String getValueIfAbsent() 496 { 497 return getKey() + VALUE_IF_ABSENT_KEY; 498 } 499 500 507 public boolean isValid() 508 { 509 return validFlag; 510 } 511 512 518 public boolean isSet() 519 { 520 return setFlag; 521 } 522 523 530 public String getDisplayName() 531 { 532 return (displayName == null) ? name : displayName; 533 } 534 535 541 public void setDisplayName(String newDisplayName) 542 { 543 displayName = newDisplayName; 544 } 545 546 551 public String getMessage() 552 { 553 return (message == null) ? EMPTY : message; 554 } 555 556 559 public void setMessage(String message) 560 { 561 this.message = message; 562 validFlag = false; 563 } 564 565 568 protected boolean validate(ValueParser pp) 569 { 570 return validate(); 571 } 572 573 576 protected boolean validate() 577 { 578 log.debug(name + ": validate()"); 579 580 if (isMultiValued) 581 { 582 stringValues = parser.getStrings(getKey()); 583 584 if (isDebugEnabled) 585 { 586 log.debug(name + ": Multi-Valued"); 587 for (int i = 0; i < stringValues.length; i++) 588 { 589 log.debug(name + ": " + i + ". Wert: " + stringValues[i]); 590 } 591 } 592 593 594 if (validator != null) 595 { 596 setTestValue(parser.getStrings(getKey())); 599 for (int i = 0; i < stringValues.length; i++) 600 { 601 try 602 { 603 validator.assertValidity(stringValues[i]); 604 } 605 catch (ValidationException ve) 606 { 607 setMessage(ve.getMessage()); 608 } 609 } 610 } 611 612 if (validFlag) 613 { 614 doSetValue(); 615 } 616 } 617 else 618 { 619 stringValue = parser.getString(getKey()); 620 621 if (isDebugEnabled) 622 { 623 log.debug(name + ": Single Valued, Value is " + stringValue); 624 } 625 626 if (validator != null) 627 { 628 setTestValue(parser.getString(getKey())); 631 632 try 633 { 634 validator.assertValidity(stringValue); 635 log.debug(name + ": Value is ok"); 636 doSetValue(); 637 } 638 catch (ValidationException ve) 639 { 640 log.debug(name + ": Value failed validation!"); 641 setMessage(ve.getMessage()); 642 } 643 } 644 else 645 { 646 doSetValue(); 647 } 648 } 649 650 return validFlag; 651 } 652 653 659 public abstract void setDefaultValue(String prop); 660 661 669 public abstract void setEmptyValue(String prop); 670 671 674 protected void doSetValue(ValueParser pp) 675 { 676 doSetValue(); 677 } 678 679 682 protected abstract void doSetValue(); 683 684 690 void setInitialValue(Object obj) 691 { 692 validValue = obj; 693 } 694 695 705 public Object getInitialValue() throws IntakeException 706 { 707 if (validValue == null) 708 { 709 if (retrievable != null) 710 { 711 getProperty(retrievable); 712 } 713 else 714 { 715 getDefault(); 716 } 717 } 718 return validValue; 719 } 720 721 726 void setTestValue(Object obj) 727 { 728 testValue = obj; 729 } 730 731 736 public Object getTestValue() 737 { 738 return testValue; 739 } 740 741 751 public Object getValue() 752 { 753 Object val = null; 754 try 755 { 756 val = getInitialValue(); 757 } 758 catch (IntakeException e) 759 { 760 log.error("Could not get intial value of " + this.getDisplayName() + 761 " in group " + this.group.getIntakeGroupName(), e); 762 } 763 764 if (getTestValue() != null) 765 { 766 val = getTestValue(); 767 } 768 769 if (val == null) 770 { 771 val = onError; 772 } 773 return val; 774 } 775 776 782 public String toString() 783 { 784 String res = EMPTY; 785 786 if (stringValue != null) 787 { 788 res = stringValue; 789 } 790 else if (getValue() != null) 791 { 792 res = getValue().toString(); 793 } 794 return res; 795 } 796 797 805 public String getHTMLString() 806 { 807 String res = toString(); 808 return StringUtils.replace(res, "\"", """); 809 } 810 811 817 public void getProperty(Object obj) 818 throws IntakeException 819 { 820 try 821 { 822 validValue = getter.invoke(obj, null); 823 } 824 catch (IllegalAccessException e) 825 { 826 throwSetGetException("getter", obj, this.getDisplayName(), 827 this.group.getIntakeGroupName(), e); 828 } 829 catch (IllegalArgumentException e) 830 { 831 throwSetGetException("getter", obj, this.getDisplayName(), 832 this.group.getIntakeGroupName(), e); 833 } 834 catch (InvocationTargetException e) 835 { 836 throwSetGetException("getter", obj, this.getDisplayName(), 837 this.group.getIntakeGroupName(), e); 838 } 839 } 840 841 844 845 public void getDefault() 846 { 847 validValue = getDefaultValue(); 848 } 849 850 856 public void setProperty(Object obj) throws IntakeException 857 { 858 if (isDebugEnabled) 859 { 860 log.debug(name + ".setProperty(" + obj.getClass().getName() + ")"); 861 } 862 863 if (!isValid()) 864 { 865 throw new IntakeException( 866 "Attempted to assign an invalid input."); 867 } 868 if (isSet()) 869 { 870 valArray[0] = getTestValue(); 871 if (isDebugEnabled) 872 { 873 log.debug(name + ": Property is set, value is " + valArray[0]); 874 } 875 } 876 else 877 { 878 valArray[0] = getSafeEmptyValue(); 879 if (isDebugEnabled) 880 { 881 log.debug(name + ": Property is not set, using emptyValue " + valArray[0]); 882 } 883 } 884 885 try 886 { 887 setter.invoke(obj, valArray); 888 } 889 catch (IllegalAccessException e) 890 { 891 throwSetGetException("setter", obj, this.getDisplayName(), 892 this.group.getIntakeGroupName(), e); 893 } 894 catch (IllegalArgumentException e) 895 { 896 throwSetGetException("setter", obj, this.getDisplayName(), 897 this.group.getIntakeGroupName(), e); 898 } 899 catch (InvocationTargetException e) 900 { 901 throwSetGetException("setter", obj, this.getDisplayName(), 902 this.group.getIntakeGroupName(), e); 903 } 904 } 905 906 916 private void throwSetGetException(String type, Object obj, 917 String fieldName, String groupName, 918 Exception e) 919 throws IntakeException 920 { 921 throw new IntakeException("Could not execute " + type 922 + " method for " + fieldName + " in group " + groupName 923 + " on " + obj.getClass().getName(), e); 924 925 } 926 927 932 public Object getDefaultValue() 933 { 934 return defaultValue; 935 } 936 937 942 public Object getEmptyValue() 943 { 944 return emptyValue; 945 } 946 947 957 protected Object getSafeEmptyValue() 958 { 959 return getEmptyValue(); 960 } 961 962 967 public String getName() 968 { 969 return name; 970 } 971 972 977 public String getDisplaySize() 978 { 979 return (StringUtils.isEmpty(displaySize) ? "" : displaySize); 980 } 981 982 987 public String getMaxSize() 988 { 989 return (StringUtils.isEmpty(maxSize) ? "" : maxSize); 990 } 991 992 1002 public String getStringValue() 1003 { 1004 return this.toString(); 1005 } 1006 1007} 1008 | Popular Tags |