1 23 24 27 28 package com.sun.enterprise.tools.common.validation; 29 30 import java.io.File ; 31 import java.io.FileInputStream ; 32 import java.io.FileNotFoundException ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.lang.reflect.Constructor ; 36 import java.lang.reflect.Method ; 37 import java.net.URL ; 38 import java.text.MessageFormat ; 39 import java.util.ArrayList ; 40 import java.util.Collection ; 41 import java.util.HashMap ; 42 43 import org.netbeans.modules.schema2beans.BaseBean; 44 45 import com.sun.enterprise.tools.common.validation.Constants; 46 import com.sun.enterprise.tools.common.validation.constraints.CardinalConstraint; 47 import com.sun.enterprise.tools.common.validation.constraints.Constraint; 48 import com.sun.enterprise.tools.common.validation.constraints.data.Argument; 49 import com.sun.enterprise.tools.common.validation.constraints.data.Arguments; 50 import com.sun.enterprise.tools.common.validation.constraints.data.CheckInfo; 51 import com.sun.enterprise.tools.common.validation.constraints.data.Constraints; 52 import com.sun.enterprise.tools.common.validation.data.Check; 53 import com.sun.enterprise.tools.common.validation.data.Element; 54 import com.sun.enterprise.tools.common.validation.data.Validation; 55 import com.sun.enterprise.tools.common.validation.data.Parameter; 56 import com.sun.enterprise.tools.common.validation.data.Parameters; 57 import com.sun.enterprise.tools.common.validation.util.BundleReader; 58 import com.sun.enterprise.tools.common.validation.util.ObjectFactory; 59 import com.sun.enterprise.tools.common.validation.util.Utils; 60 61 62 100 public class ValidationManager { 101 112 113 116 private static String GET_PREFIX = "get"; 118 119 122 private static String SET_PREFIX = "set"; 124 125 128 private static String SIZE_PREFIX = "size"; 130 131 136 private static final String defaultConstraintsFile = 137 "com/sun/enterprise/tools/common/validation/constraints/" + "constraints.xml"; 140 141 144 private static final String CARDINAL_CONSTRAINT_CLASS = 145 "com.sun.enterprise.tools.common.validation.constraints." + "CardinalConstraint"; 148 149 157 private String validationFile = null; 158 159 160 166 private String defaultValidationFile = 167 "com/sun/enterprise/tools/common/validation/validation.xml"; 169 170 174 private HashMap xpathToValidator = null; 175 176 177 180 private Validation validation = null; 181 182 183 186 private Constraints constraints = null; 187 188 189 192 private Utils utils = null; 193 194 195 196 public ValidationManager() { 197 utils = new Utils(); 198 } 199 200 201 202 public ValidationManager(String validationFile) { 203 if(null != validationFile){ 204 this.validationFile = validationFile; 205 } 206 207 utils = new Utils(); 208 } 209 210 211 229 public Collection validate(Object object){ 230 234 Collection collection = new ArrayList (); 235 236 if(object != null){ 237 boolean validateeImplFound = false; 238 String validateeImpl = null; 239 240 String implFile = 241 System.getProperty("impl.file"); 244 if(implFile != null){ 247 validateeImpl = getValidateeImplementation(object, implFile); 248 } 249 250 if(validateeImpl == null){ 252 validateeImpl = 253 getValidateeImplementation(object, Constants.IMPL_FILE); 254 } 255 256 BundleReader.setBundle(Constants.BUNDLE_FILE); 258 259 if(validateeImpl != null){ 260 Validatee validatee = 261 (Validatee)ObjectFactory.newInstance(validateeImpl, object); 262 if(validatee != null){ 263 collection = validate(validatee); 264 } 265 } else { 266 Class classObject = utils.getClass(object); 267 String className = classObject.getName(); 268 269 String format = BundleReader.getValue( 270 "MSG_given_object_is_not_validatee"); Object [] arguments = new Object []{className}; 272 System.out.println(MessageFormat.format(format, arguments)); 273 } 274 275 } 276 return collection; 277 } 278 279 280 289 public Collection validate(Validatee validatee){ 290 ArrayList failures = new ArrayList (); 294 if(validatee != null){ 295 String xpath = validatee.getXPath(); 296 297 ArrayList elementNames = validatee.getElementNames(); 298 ArrayList elementDtdNames = validatee.getElementDtdNames(); 299 int noOfElements = elementNames.size(); 300 String elementName = null; 301 String elementDtdName = null; 302 int count = 0; 303 304 for(int i=0; i<noOfElements; i++){ 305 elementName = (String )elementNames.get(i); 306 elementDtdName = (String )elementDtdNames.get(i); 307 308 failures.addAll(validateCardinalConstraint(validatee, elementName, 310 elementDtdName)); 311 312 boolean isBean = validatee.isBeanElement(elementName); 314 if(isBean){ 315 failures.addAll(recurse(elementName, validatee)); 317 } else { 318 Validator validator = getValidator(xpath); 319 if(null != validator){ 320 failures.addAll(validator.validate(elementName, 321 elementDtdName, validatee)); 322 } else { 323 } 330 } 331 } 332 } 333 return failures; 334 } 335 336 337 349 public Collection validateIndividualProperty(String property, 350 String absoluteDtdName, String fieldName){ 351 ArrayList failures = new ArrayList (); 352 353 String xpath = utils.getParentName(absoluteDtdName, 354 Constants.XPATH_DELIMITER_CHAR); 355 Validator validator = getValidator(xpath); 356 if(null != validator){ 357 failures.addAll(validator.validateIndividualProperty( 358 property, absoluteDtdName, fieldName)); 359 } else { 360 } 367 368 return failures; 369 } 370 371 372 385 private Collection recurse(String elementName, Validatee validatee){ 386 ArrayList failures = new ArrayList (); 387 boolean isIndexed = validatee.isIndexed(elementName); 388 389 if(isIndexed){ 390 failures.addAll(validateBeans(elementName, validatee)); 391 } else { 392 failures.addAll(validateBean(elementName, validatee)); 393 } 394 return failures; 395 } 396 397 398 414 private Collection validateCardinalConstraint(Validatee validatee, 415 String elementName, String elementDtdName){ 416 ArrayList failures = new ArrayList (); 417 int cardinal = validatee.getElementCardinal(elementName); 418 CardinalConstraint constraint = getCardinalConstraint(cardinal); 419 420 switch(cardinal){ 421 case Constants.MANDATORY_ARRAY : 422 case Constants.OPTIONAL_ARRAY: { 423 Object [] elements = 424 (Object [])validatee.getElements(elementName); 425 String name = validatee.getIndexedXPath() + 426 Constants.XPATH_DELIMITER + elementDtdName; 427 failures.addAll(constraint.match(elements, name)); 430 break; 431 } 432 case Constants.OPTIONAL_ELEMENT : { 433 Object element = validatee.getElement(elementName); 434 break; 435 } 436 case Constants.MANDATORY_ELEMENT : 437 default : { 438 Object element = validatee.getElement(elementName); 439 String name = validatee.getIndexedXPath() + 440 Constants.XPATH_DELIMITER + elementDtdName; 441 failures.addAll(constraint.match(element, name)); 444 } 445 break; 446 } 447 return failures; 448 } 449 450 451 462 private Validator getValidator(String xpath){ 463 Validator validator = null; 464 if(null == xpathToValidator){ 465 constructXpathToValidator(); 466 } 467 if(null != xpathToValidator) { 468 validator = (Validator)xpathToValidator.get(xpath); 469 } 470 return validator; 471 } 472 473 474 478 private void constructXpathToValidator(){ 479 481 if(null == validation){ 482 constructValidation(); 483 } 484 485 if(null == validation){ 486 String format = 487 BundleReader.getValue("Warning_Not_available"); Object [] arguments = new Object []{"Validation Data"}; System.out.println(MessageFormat.format(format, arguments)); 490 return; 491 } 492 493 494 xpathToValidator = new HashMap (); 495 int noOfElements = validation.sizeElement(); 496 Element element = null; 497 Validator validator = null; 498 String elementName = null; 499 String beanName = null; 500 Check check = null; 501 String checkName = null; 502 503 for(int i=0; i<noOfElements; i++) { 504 element = validation.getElement(i); 505 elementName = utils.getName(element.getName(), 506 Constants.XPATH_DELIMITER_CHAR); 507 beanName = utils.getParentName(element.getName(), 508 Constants.XPATH_DELIMITER_CHAR); 509 validator = (Validator) xpathToValidator.get(beanName); 510 if(null == validator) { 514 validator = new Validator(); 515 xpathToValidator.put(beanName, validator); 516 } 524 525 int noOfChecks = element.sizeCheck(); 526 Constraint constraint = null; 527 for(int j=0; j<noOfChecks; j++){ 528 check = element.getCheck(j); 529 constraint = getConstraint(check); 530 if(null != constraint){ 531 validator.addElementConstraint(elementName, constraint); 532 } 541 } 542 } 543 } 544 545 546 554 private void constructValidation() { 555 557 URL url = null; 558 InputStream inputStream = null; 559 560 if(validationFile != null){ 561 inputStream = getInputStream(validationFile); 562 if(inputStream == null){ 563 String format = 564 BundleReader.getValue("MSG_using_the_default_file"); Object [] arguments = new Object []{defaultValidationFile}; 566 System.out.println(MessageFormat.format(format, arguments)); 567 568 inputStream = getDafaultStream(); 569 } 570 } else { 571 inputStream = getDafaultStream(); 572 } 573 574 if(inputStream != null){ 575 try { 577 validation = Validation.createGraph(inputStream); 578 } catch(Exception e) { 579 System.out.println(e.getMessage()); 580 validation = null; 581 } 582 } 583 } 584 585 586 601 private Constraint getConstraint(Check check){ 602 Constraint constraint = null; 603 if(null == constraints) { 604 constructConstraints(); 605 } 606 CheckInfo checkInfo = null; 607 if(null != constraints){ 608 String checkName = check.getName(); 609 checkInfo = getCheckInfo(checkName, constraints); 610 if(null != checkInfo){ 611 constraint = buildConstraint(check, checkInfo); 612 } else { 613 String format = 614 BundleReader.getValue("MSG_No_definition_for"); Object [] arguments = 616 new Object []{"CheckInfo", checkName}; System.out.println(MessageFormat.format(format, arguments)); 618 } 619 } else { 620 String format = 621 BundleReader.getValue("MSG_Not_defined"); Object [] arguments = new Object []{"Constraints"}; System.out.println(MessageFormat.format(format, arguments)); 624 } 625 return constraint; 626 } 627 628 629 635 private void constructConstraints() { 636 Constraints customConstraints = null; 637 Constraints defaultConstraints = null; 638 639 String constraintsFile = 640 System.getProperty("constraints.file"); if(constraintsFile != null){ 643 customConstraints = getConstraints(constraintsFile); 644 } 645 646 if(defaultConstraintsFile != null){ 647 defaultConstraints = getConstraints(defaultConstraintsFile); 648 } 649 650 if(customConstraints != null){ 651 if(defaultConstraints != null){ 652 int count = defaultConstraints.sizeCheckInfo(); 653 CheckInfo checkInfo = null; 654 CheckInfo checkInfoClone = null; 655 for(int i=0; i<count; i++){ 656 checkInfo = defaultConstraints.getCheckInfo(i); 657 checkInfoClone = (CheckInfo) checkInfo.clone(); 659 customConstraints.addCheckInfo(checkInfoClone); 661 } 662 constraints = customConstraints; 663 } else { 664 constraints = customConstraints; 665 } 666 } else { 667 constraints = defaultConstraints; 668 } 669 } 670 671 672 684 private CheckInfo getCheckInfo(String checkName, 685 Constraints constraints){ 686 CheckInfo checkInfo = null; 687 int size = constraints.sizeCheckInfo(); 688 for(int i=0; i<size; i++) { 689 checkInfo = constraints.getCheckInfo(i); 690 if(checkName.equals(checkInfo.getName())) { 691 return checkInfo; 692 } 693 } 694 return null; 695 } 696 697 698 721 private Constraint buildConstraint(Check check, CheckInfo checkInfo){ 722 Constraint constraint = null; 738 String classname = checkInfo.getClassname(); 739 Arguments arguments = checkInfo.getArguments(); 740 Class [] argumentTypeClass = new Class [1]; 741 Object [] argumentValue = new Object [1]; 742 743 String argumentName; 744 String argumentType; 745 746 constraint = (Constraint)utils.createObject(classname); 747 if(null != arguments){ 748 int size = arguments.sizeArgument(); 749 Parameters parameters = check.getParameters(); 750 751 if((null != parameters) && (size == parameters.sizeParameter())){ 752 Argument argument = null; 753 Parameter parameter = null; 754 for(int i=0; i<size; i++) { 755 argument = arguments.getArgument(i); 756 argumentName = argument.getName(); 757 argumentType = argument.getType(); 758 parameter = 759 getParameter(parameters, argumentName); 760 761 if(parameter == null){ 762 String format = BundleReader.getValue( 763 "Warning_no_value_specified_for"); Object [] substitutes = 765 new Object []{argumentName, check.getName()}; 766 System.out.println( 767 MessageFormat.format(format, substitutes)); 768 continue; 769 } 770 771 if(null == argumentType){ 772 argumentType = "java.lang.String"; } 774 775 int noOfValues = 1; 776 if(argumentType.equals("java.lang.String[]")){ Integer sz = (Integer )utils.getElement("value", parameter, SIZE_PREFIX); 779 noOfValues = sz.intValue(); 780 argumentType = "java.lang.String"; } 782 for(int j=0; j<noOfValues; j++) { 783 argumentValue[0] = utils.getElement("value", j, parameter); 785 argumentTypeClass[0] = 786 utils.getClass(argumentType); 787 String methodName = 788 utils.methodNameFromDtdName(argumentName, 789 SET_PREFIX); 790 Method method = 791 utils.getMethod(utils.getClass(constraint), 792 methodName, argumentTypeClass); 793 utils.invoke(constraint, method, argumentValue); 794 } 795 } 796 } else { 797 String format = BundleReader.getValue( 798 "MSG_Conflicting_Constraint_information"); Object [] substitues = new Object []{check.getName()}; String message = MessageFormat.format(format, substitues); 801 assert false : message; 802 } 803 } 804 return constraint; 805 } 806 807 808 822 private CardinalConstraint getCardinalConstraint(int cardinal){ 823 Class [] argumentTypes = new Class [] {int.class}; 824 Constructor constructor = 825 utils.getConstructor(CARDINAL_CONSTRAINT_CLASS, argumentTypes); 826 827 Integer parameter = new Integer (cardinal); 828 Object [] argumentValues = new Object [] {parameter}; 829 830 return (CardinalConstraint) utils.createObject(constructor, 831 argumentValues); 832 } 833 834 835 848 private Collection validateBeans(String elementName, 849 Validatee validatee){ 850 int noOfElements = 0; 853 String sizeMethodName = utils.methodNameFromBeanName(elementName, 854 SIZE_PREFIX); 855 Method sizeMethod = validatee.getMethod(sizeMethodName); 856 noOfElements = ((Integer )validatee.invoke(sizeMethod)).intValue(); 857 858 ArrayList failures = new ArrayList (); 859 Object child = null; 860 for(int i=0; i<noOfElements; i++) { 861 child = validatee.getElement(elementName, i); 862 if(child != null) { 863 failures.addAll(validate(child)); 864 } 865 } 866 return failures; 867 } 868 869 870 882 private Collection validateBean(String elementName, 883 Validatee validatee){ 884 ArrayList failures = new ArrayList (); 886 Object child = null; 887 child = validatee.getElement(elementName); 888 if(child != null) { 889 failures.addAll(validate(child)); 890 } 891 return failures; 892 } 893 894 895 904 private InputStream getDafaultStream(){ 905 InputStream inputStream = null; 906 URL url = null; 907 if(defaultValidationFile != null){ 908 url = utils.getUrlObject(defaultValidationFile); 909 if(url != null) { 910 try { 911 inputStream = url.openStream(); 912 } catch (IOException exception){ 913 System.out.println(exception.getMessage()); 914 } 915 } else { 916 assert false : (BundleReader.getValue( 917 "Error_control_should_never_reach_here")); 919 String format = 920 BundleReader.getValue("Error_can_not_access_file"); Object [] arguments = new Object []{defaultValidationFile}; 922 String message = MessageFormat.format(format, arguments); 923 assert false : message; 924 } 925 } else { 926 assert false : (BundleReader.getValue( 927 "Error_control_should_never_reach_here")); } 929 return inputStream; 930 } 931 932 933 939 private Parameter getParameter(Parameters parameters, String name){ 940 int size = parameters.sizeParameter(); 941 Parameter returnValue = null; 942 Parameter parameter = null; 943 String parameterName = null; 944 for(int i=0; i<size; i++){ 945 parameter = (Parameter)utils.getElement("parameter", i, parameters); 947 parameterName = parameter.getName(); 948 if(parameterName.equals(name)){ 949 returnValue = parameter; 950 break; 951 } 952 } 953 return returnValue; 954 } 955 956 957 963 private InputStream getInputStream(String inputFile){ 964 966 InputStream inputStream = null; 967 968 if(inputFile.lastIndexOf(':') == -1){ 969 URL url = null; 970 971 url = utils.getUrlObject(inputFile); 972 if(url != null) { 973 try { 974 inputStream = url.openStream(); 975 } catch (IOException exception){ 976 System.out.println(exception.getMessage()); 977 } 978 } else { 979 String format = BundleReader.getValue( 980 "Error_specified_file_can_not_be_used"); Object [] arguments = new Object []{inputFile}; 982 System.out.println(MessageFormat.format(format, arguments)); 983 } 984 } else { 985 File file = new File (inputFile); 986 if(file.exists()){ 987 try { 988 inputStream = new FileInputStream (file); 989 } catch(FileNotFoundException exception){ 990 System.out.println(exception.getMessage()); 991 inputStream = null; 992 } 993 } else { 994 String format = BundleReader.getValue( 995 "Error_specified_file_can_not_be_used"); Object [] arguments = new Object []{inputFile}; 997 System.out.println(MessageFormat.format(format, arguments)); 998 } 999 } 1000 1001 return inputStream; 1002 } 1003 1004 1005 1013 private String getValidateeImplementation(Object object, 1014 String propertiesFile){ 1015 String returnVal = null; 1016 Class classObject = utils.getClass(object); 1017 String className = classObject.getName(); 1018 1019 BundleReader.setBundle(propertiesFile); 1022 1023 String validateeImplName = BundleReader.getValue(className); 1024 while(!(className.equals("java.lang.Object"))){ if(!(validateeImplName.equals(className))){ 1026 returnVal = validateeImplName; 1027 break; 1028 } else { 1029 classObject = classObject.getSuperclass(); 1030 className = classObject.getName(); 1031 validateeImplName = BundleReader.getValue(className); 1032 } 1033 } 1034 return returnVal; 1035 } 1036 1037 1038 1045 private Constraints getConstraints(String constraintsFile){ 1046 URL url = null; 1047 InputStream inputStream = null; 1048 Constraints constraints = null; 1049 1050 if(constraintsFile != null){ 1051 inputStream = getInputStream(constraintsFile); 1052 } 1053 1054 if(inputStream != null){ 1056 try { 1057 constraints = Constraints.createGraph(inputStream); 1058 } catch(Exception e) { 1059 System.out.println(e.getMessage()); 1060 constraints = null; 1061 } 1062 } 1063 return constraints; 1064 } 1065} 1066 | Popular Tags |