1 18 19 package org.apache.struts.validator; 20 21 import java.io.Serializable ; 22 import java.util.Date ; 23 import java.util.Locale ; 24 import java.util.StringTokenizer ; 25 26 import javax.servlet.ServletContext ; 27 import javax.servlet.http.HttpServletRequest ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.commons.validator.Field; 32 import org.apache.commons.validator.GenericTypeValidator; 33 import org.apache.commons.validator.GenericValidator; 34 import org.apache.commons.validator.UrlValidator; 35 import org.apache.commons.validator.Validator; 36 import org.apache.commons.validator.ValidatorAction; 37 import org.apache.commons.validator.util.ValidatorUtils; 38 import org.apache.struts.action.ActionMessages; 39 import org.apache.struts.util.RequestUtils; 40 41 54 public class FieldChecks implements Serializable { 55 56 59 private static final Log log = LogFactory.getLog(FieldChecks.class); 60 61 public static final String FIELD_TEST_NULL = "NULL"; 62 public static final String FIELD_TEST_NOTNULL = "NOTNULL"; 63 public static final String FIELD_TEST_EQUAL = "EQUAL"; 64 65 80 public static boolean validateRequired(Object bean, 81 ValidatorAction va, Field field, 82 ActionMessages errors, 83 Validator validator, 84 HttpServletRequest request) { 85 86 String value = null; 87 if (isString(bean)) { 88 value = (String ) bean; 89 } else { 90 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 91 } 92 93 if (GenericValidator.isBlankOrNull(value)) { 94 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 95 return false; 96 } else { 97 return true; 98 } 99 100 } 101 102 117 public static boolean validateRequiredIf(Object bean, 118 ValidatorAction va, Field field, 119 ActionMessages errors, 120 Validator validator, 121 HttpServletRequest request) { 122 123 Object form = validator.getParameterValue(org.apache.commons.validator.Validator.BEAN_PARAM); 124 String value = null; 125 boolean required = false; 126 127 if (isString(bean)) { 128 value = (String ) bean; 129 } else { 130 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 131 } 132 133 int i = 0; 134 String fieldJoin = "AND"; 135 if (!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))) { 136 fieldJoin = field.getVarValue("fieldJoin"); 137 } 138 139 if (fieldJoin.equalsIgnoreCase("AND")) { 140 required = true; 141 } 142 143 while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i + "]"))) { 144 String dependProp = field.getVarValue("field[" + i + "]"); 145 String dependTest = field.getVarValue("fieldTest[" + i + "]"); 146 String dependTestValue = field.getVarValue("fieldValue[" + i + "]"); 147 String dependIndexed = field.getVarValue("fieldIndexed[" + i + "]"); 148 149 if (dependIndexed == null) { 150 dependIndexed = "false"; 151 } 152 153 String dependVal = null; 154 boolean thisRequired = false; 155 if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) { 156 String key = field.getKey(); 157 if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) { 158 String ind = key.substring(0, key.indexOf(".") + 1); 159 dependProp = ind + dependProp; 160 } 161 } 162 163 dependVal = ValidatorUtils.getValueAsString(form, dependProp); 164 if (dependTest.equals(FIELD_TEST_NULL)) { 165 if ((dependVal != null) && (dependVal.length() > 0)) { 166 thisRequired = false; 167 } else { 168 thisRequired = true; 169 } 170 } 171 172 if (dependTest.equals(FIELD_TEST_NOTNULL)) { 173 if ((dependVal != null) && (dependVal.length() > 0)) { 174 thisRequired = true; 175 } else { 176 thisRequired = false; 177 } 178 } 179 180 if (dependTest.equals(FIELD_TEST_EQUAL)) { 181 thisRequired = dependTestValue.equalsIgnoreCase(dependVal); 182 } 183 184 if (fieldJoin.equalsIgnoreCase("AND")) { 185 required = required && thisRequired; 186 } else { 187 required = required || thisRequired; 188 } 189 190 i++; 191 } 192 193 if (required) { 194 if (GenericValidator.isBlankOrNull(value)) { 195 errors.add( 196 field.getKey(), 197 Resources.getActionMessage(validator, request, va, field)); 198 199 return false; 200 201 } else { 202 return true; 203 } 204 } 205 return true; 206 } 207 208 223 public static boolean validateMask(Object bean, 224 ValidatorAction va, Field field, 225 ActionMessages errors, 226 Validator validator, 227 HttpServletRequest request) { 228 229 String mask = field.getVarValue("mask"); 230 String value = null; 231 if (isString(bean)) { 232 value = (String ) bean; 233 } else { 234 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 235 } 236 237 try { 238 if (!GenericValidator.isBlankOrNull(value) 239 && !GenericValidator.matchRegexp(value, mask)) { 240 241 errors.add( 242 field.getKey(), 243 Resources.getActionMessage(validator, request, va, field)); 244 245 return false; 246 } else { 247 return true; 248 } 249 } catch (Exception e) { 250 log.error(e.getMessage(), e); 251 } 252 return true; 253 } 254 255 256 270 public static Object validateByte(Object bean, 271 ValidatorAction va, Field field, 272 ActionMessages errors, 273 Validator validator, 274 HttpServletRequest request) { 275 276 Object result = null; 277 String value = null; 278 if (isString(bean)) { 279 value = (String ) bean; 280 } else { 281 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 282 } 283 284 if (GenericValidator.isBlankOrNull(value)) { 285 return Boolean.TRUE; 286 } 287 288 result = GenericTypeValidator.formatByte(value); 289 290 if (result == null) { 291 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 292 } 293 294 return result == null ? Boolean.FALSE : result; 295 } 296 297 298 312 public static Object validateShort(Object bean, 313 ValidatorAction va, Field field, 314 ActionMessages errors, 315 Validator validator, 316 HttpServletRequest request) { 317 Object result = null; 318 String value = null; 319 if (isString(bean)) { 320 value = (String ) bean; 321 } else { 322 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 323 } 324 325 if (GenericValidator.isBlankOrNull(value)) { 326 return Boolean.TRUE; 327 } 328 329 result = GenericTypeValidator.formatShort(value); 330 331 if (result == null) { 332 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 333 } 334 335 return result == null ? Boolean.FALSE : result; 336 } 337 338 339 353 public static Object validateInteger(Object bean, 354 ValidatorAction va, Field field, 355 ActionMessages errors, 356 Validator validator, 357 HttpServletRequest request) { 358 Object result = null; 359 String value = null; 360 if (isString(bean)) { 361 value = (String ) bean; 362 } else { 363 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 364 } 365 366 if (GenericValidator.isBlankOrNull(value)) { 367 return Boolean.TRUE; 368 } 369 370 result = GenericTypeValidator.formatInt(value); 371 372 if (result == null) { 373 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 374 } 375 376 return result == null ? Boolean.FALSE : result; 377 } 378 379 380 394 public static Object validateLong(Object bean, 395 ValidatorAction va, Field field, 396 ActionMessages errors, 397 Validator validator, 398 HttpServletRequest request) { 399 Object result = null; 400 String value = null; 401 if (isString(bean)) { 402 value = (String ) bean; 403 } else { 404 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 405 } 406 407 if (GenericValidator.isBlankOrNull(value)) { 408 return Boolean.TRUE; 409 } 410 411 result = GenericTypeValidator.formatLong(value); 412 413 if (result == null) { 414 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 415 } 416 417 return result == null ? Boolean.FALSE : result; 418 } 419 420 421 435 public static Object validateFloat(Object bean, 436 ValidatorAction va, Field field, 437 ActionMessages errors, 438 Validator validator, 439 HttpServletRequest request) { 440 Object result = null; 441 String value = null; 442 if (isString(bean)) { 443 value = (String ) bean; 444 } else { 445 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 446 } 447 448 if (GenericValidator.isBlankOrNull(value)) { 449 return Boolean.TRUE; 450 } 451 452 result = GenericTypeValidator.formatFloat(value); 453 454 if (result == null) { 455 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 456 } 457 458 return result == null ? Boolean.FALSE : result; 459 } 460 461 462 476 public static Object validateDouble(Object bean, 477 ValidatorAction va, Field field, 478 ActionMessages errors, 479 Validator validator, 480 HttpServletRequest request) { 481 Object result = null; 482 String value = null; 483 if (isString(bean)) { 484 value = (String ) bean; 485 } else { 486 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 487 } 488 489 if (GenericValidator.isBlankOrNull(value)) { 490 return Boolean.TRUE; 491 } 492 493 result = GenericTypeValidator.formatDouble(value); 494 495 if (result == null) { 496 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 497 } 498 499 return result == null ? Boolean.FALSE : result; 500 } 501 502 503 523 public static Object validateDate(Object bean, 524 ValidatorAction va, Field field, 525 ActionMessages errors, 526 Validator validator, 527 HttpServletRequest request) { 528 529 Object result = null; 530 String value = null; 531 if (isString(bean)) { 532 value = (String ) bean; 533 } else { 534 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 535 } 536 String datePattern = field.getVarValue("datePattern"); 537 String datePatternStrict = field.getVarValue("datePatternStrict"); 538 Locale locale = RequestUtils.getUserLocale(request, null); 539 540 if (GenericValidator.isBlankOrNull(value)) { 541 return Boolean.TRUE; 542 } 543 544 try { 545 if (datePattern != null && datePattern.length() > 0) { 546 result = GenericTypeValidator.formatDate(value, datePattern, false); 547 } else if (datePatternStrict != null && datePatternStrict.length() > 0) { 548 result = GenericTypeValidator.formatDate(value, datePatternStrict, true); 549 } else { 550 result = GenericTypeValidator.formatDate(value, locale); 551 } 552 } catch (Exception e) { 553 log.error(e.getMessage(), e); 554 } 555 556 if (result == null) { 557 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 558 } 559 560 return result == null ? Boolean.FALSE : result; 561 } 562 563 578 public static boolean validateIntRange(Object bean, 579 ValidatorAction va, Field field, 580 ActionMessages errors, 581 Validator validator, 582 HttpServletRequest request) { 583 584 String value = null; 585 if (isString(bean)) { 586 value = (String ) bean; 587 } else { 588 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 589 } 590 591 if (!GenericValidator.isBlankOrNull(value)) { 592 try { 593 int intValue = Integer.parseInt(value); 594 int min = Integer.parseInt(field.getVarValue("min")); 595 int max = Integer.parseInt(field.getVarValue("max")); 596 597 if (!GenericValidator.isInRange(intValue, min, max)) { 598 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 599 600 return false; 601 } 602 } catch (Exception e) { 603 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 604 return false; 605 } 606 } 607 608 return true; 609 } 610 611 626 public static boolean validateDoubleRange(Object bean, 627 ValidatorAction va, Field field, 628 ActionMessages errors, 629 Validator validator, 630 HttpServletRequest request) { 631 632 String value = null; 633 if (isString(bean)) { 634 value = (String ) bean; 635 } else { 636 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 637 } 638 639 if (!GenericValidator.isBlankOrNull(value)) { 640 try { 641 double doubleValue = Double.parseDouble(value); 642 double min = Double.parseDouble(field.getVarValue("min")); 643 double max = Double.parseDouble(field.getVarValue("max")); 644 645 if (!GenericValidator.isInRange(doubleValue, min, max)) { 646 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 647 648 return false; 649 } 650 } catch (Exception e) { 651 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 652 return false; 653 } 654 } 655 656 return true; 657 } 658 659 674 public static boolean validateFloatRange(Object bean, 675 ValidatorAction va, Field field, 676 ActionMessages errors, 677 Validator validator, 678 HttpServletRequest request) { 679 680 String value = null; 681 if (isString(bean)) { 682 value = (String ) bean; 683 } else { 684 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 685 } 686 687 if (!GenericValidator.isBlankOrNull(value)) { 688 try { 689 float floatValue = Float.parseFloat(value); 690 float min = Float.parseFloat(field.getVarValue("min")); 691 float max = Float.parseFloat(field.getVarValue("max")); 692 693 if (!GenericValidator.isInRange(floatValue, min, max)) { 694 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 695 696 return false; 697 } 698 } catch (Exception e) { 699 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 700 return false; 701 } 702 } 703 704 return true; 705 } 706 707 708 722 public static Object validateCreditCard(Object bean, 723 ValidatorAction va, Field field, 724 ActionMessages errors, 725 Validator validator, 726 HttpServletRequest request) { 727 728 Object result = null; 729 String value = null; 730 if (isString(bean)) { 731 value = (String ) bean; 732 } else { 733 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 734 } 735 736 if (GenericValidator.isBlankOrNull(value)) { 737 return Boolean.TRUE; 738 } 739 740 result = GenericTypeValidator.formatCreditCard(value); 741 742 if (result == null) { 743 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 744 } 745 746 return result == null ? Boolean.FALSE : result; 747 748 } 749 750 751 765 public static boolean validateEmail(Object bean, 766 ValidatorAction va, Field field, 767 ActionMessages errors, 768 Validator validator, 769 HttpServletRequest request) { 770 771 String value = null; 772 if (isString(bean)) { 773 value = (String ) bean; 774 } else { 775 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 776 } 777 778 if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.isEmail(value)) { 779 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 780 return false; 781 } else { 782 return true; 783 } 784 } 785 786 787 802 public static boolean validateMaxLength(Object bean, 803 ValidatorAction va, Field field, 804 ActionMessages errors, 805 Validator validator, 806 HttpServletRequest request) { 807 808 String value = null; 809 if (isString(bean)) { 810 value = (String ) bean; 811 } else { 812 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 813 } 814 815 if (value != null) { 816 try { 817 int max = Integer.parseInt(field.getVarValue("maxlength")); 818 819 if (!GenericValidator.maxLength(value, max)) { 820 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 821 822 return false; 823 } 824 } catch (Exception e) { 825 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 826 return false; 827 } 828 } 829 830 return true; 831 } 832 833 834 849 public static boolean validateMinLength(Object bean, 850 ValidatorAction va, Field field, 851 ActionMessages errors, 852 Validator validator, 853 HttpServletRequest request) { 854 855 String value = null; 856 if (isString(bean)) { 857 value = (String ) bean; 858 } else { 859 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 860 } 861 862 if (!GenericValidator.isBlankOrNull(value)) { 863 try { 864 int min = Integer.parseInt(field.getVarValue("minlength")); 865 866 if (!GenericValidator.minLength(value, min)) { 867 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 868 869 return false; 870 } 871 } catch (Exception e) { 872 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 873 return false; 874 } 875 } 876 877 return true; 878 } 879 880 910 public static boolean validateUrl(Object bean, 911 ValidatorAction va, Field field, 912 ActionMessages errors, 913 Validator validator, 914 HttpServletRequest request) { 915 916 String value = null; 917 if (isString(bean)) { 918 value = (String ) bean; 919 } else { 920 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 921 } 922 923 if (GenericValidator.isBlankOrNull(value)) { 924 return true; 925 } 926 927 boolean allowallschemes = "true".equalsIgnoreCase(field.getVarValue("allowallschemes")); 929 int options = allowallschemes ? UrlValidator.ALLOW_ALL_SCHEMES : 0; 930 931 if ("true".equalsIgnoreCase(field.getVarValue("allow2slashes"))) { 932 options += UrlValidator.ALLOW_2_SLASHES; 933 } 934 935 if ("true".equalsIgnoreCase(field.getVarValue("nofragments"))) { 936 options += UrlValidator.NO_FRAGMENTS; 937 } 938 939 String schemesVar = allowallschemes ? null : field.getVarValue("schemes"); 940 941 if (options == 0 && schemesVar == null) { 943 if (GenericValidator.isUrl(value)) { 944 return true; 945 } else { 946 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 947 return false; 948 } 949 } 950 951 String [] schemes = null; 953 if (schemesVar != null) { 954 955 StringTokenizer st = new StringTokenizer (schemesVar, ","); 956 schemes = new String [st.countTokens()]; 957 958 int i = 0; 959 while (st.hasMoreTokens()) { 960 schemes[i++] = st.nextToken().trim(); 961 } 962 963 } 964 965 UrlValidator urlValidator = new UrlValidator(schemes, options); 967 if (urlValidator.isValid(value)) { 968 return true; 969 } else { 970 errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); 971 return false; 972 } 973 } 974 975 982 protected static boolean isString(Object o) { 983 return (o == null) ? true : String .class.isInstance(o); 984 } 985 986 } 987 988 | Popular Tags |