1 19 20 package org.netbeans.modules.j2ee.sun.validation; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.lang.reflect.Constructor ; 28 import java.lang.reflect.Method ; 29 import java.net.URL ; 30 import java.text.MessageFormat ; 31 import java.util.ArrayList ; 32 import java.util.Collection ; 33 import java.util.HashMap ; 34 35 import org.netbeans.modules.schema2beans.BaseBean; 36 37 import org.netbeans.modules.j2ee.sun.validation.Constants; 38 import org.netbeans.modules.j2ee.sun.validation.constraints.CardinalConstraint; 39 import org.netbeans.modules.j2ee.sun.validation.constraints.Constraint; 40 import org.netbeans.modules.j2ee.sun.validation.constraints.data.Argument; 41 import org.netbeans.modules.j2ee.sun.validation.constraints.data.Arguments; 42 import org.netbeans.modules.j2ee.sun.validation.constraints.data.CheckInfo; 43 import org.netbeans.modules.j2ee.sun.validation.constraints.data.Constraints; 44 import org.netbeans.modules.j2ee.sun.validation.data.Check; 45 import org.netbeans.modules.j2ee.sun.validation.data.Element; 46 import org.netbeans.modules.j2ee.sun.validation.data.Validation; 47 import org.netbeans.modules.j2ee.sun.validation.data.Parameter; 48 import org.netbeans.modules.j2ee.sun.validation.data.Parameters; 49 import org.netbeans.modules.j2ee.sun.validation.util.BundleReader; 50 import org.netbeans.modules.j2ee.sun.validation.util.ObjectFactory; 51 import org.netbeans.modules.j2ee.sun.validation.util.Utils; 52 53 54 92 public class ValidationManager { 93 104 105 108 private static String GET_PREFIX = "get"; 110 111 114 private static String SET_PREFIX = "set"; 116 117 120 private static String SIZE_PREFIX = "size"; 122 123 128 private static final String defaultConstraintsFile = 129 "org/netbeans/modules/j2ee/sun/validation/constraints/" + "constraints.xml"; 132 133 136 private static final String CARDINAL_CONSTRAINT_CLASS = 137 "org.netbeans.modules.j2ee.sun.validation.constraints." + "CardinalConstraint"; 140 141 149 private String validationFile = null; 150 151 152 158 private String defaultValidationFile = 159 "org/netbeans/modules/j2ee/sun/validation/validation.xml"; 161 162 166 private HashMap xpathToValidator = null; 167 168 169 172 private Validation validation = null; 173 174 175 178 private Constraints constraints = null; 179 180 181 184 private Utils utils = null; 185 186 187 188 public ValidationManager() { 189 utils = new Utils(); 190 } 191 192 193 194 public ValidationManager(String validationFile) { 195 if(null != validationFile){ 196 this.validationFile = validationFile; 197 } 198 199 utils = new Utils(); 200 } 201 202 203 221 public Collection validate(Object object){ 222 226 Collection collection = new ArrayList (); 227 228 if(object != null){ 229 boolean validateeImplFound = false; 230 String validateeImpl = null; 231 232 String implFile = 233 System.getProperty("impl.file"); 236 if(implFile != null){ 239 validateeImpl = getValidateeImplementation(object, implFile); 240 } 241 242 if(validateeImpl == null){ 244 validateeImpl = 245 getValidateeImplementation(object, Constants.IMPL_FILE); 246 } 247 248 BundleReader.setBundle(Constants.BUNDLE_FILE); 250 251 if(validateeImpl != null){ 252 Validatee validatee = 253 (Validatee)ObjectFactory.newInstance(validateeImpl, object); 254 if(validatee != null){ 255 collection = validate(validatee); 256 } 257 } else { 258 Class classObject = utils.getClass(object); 259 String className = classObject.getName(); 260 261 String format = BundleReader.getValue( 262 "MSG_given_object_is_not_validatee"); Object [] arguments = new Object []{className}; 264 System.out.println(MessageFormat.format(format, arguments)); 265 } 266 267 } 268 return collection; 269 } 270 271 272 281 public Collection validate(Validatee validatee){ 282 ArrayList failures = new ArrayList (); 286 if(validatee != null){ 287 String xpath = validatee.getXPath(); 288 289 ArrayList elementNames = validatee.getElementNames(); 290 ArrayList elementDtdNames = validatee.getElementDtdNames(); 291 int noOfElements = elementNames.size(); 292 String elementName = null; 293 String elementDtdName = null; 294 int count = 0; 295 296 for(int i=0; i<noOfElements; i++){ 297 elementName = (String )elementNames.get(i); 298 elementDtdName = (String )elementDtdNames.get(i); 299 300 failures.addAll(validateCardinalConstraint(validatee, elementName, 302 elementDtdName)); 303 304 boolean isBean = validatee.isBeanElement(elementName); 306 if(isBean){ 307 failures.addAll(recurse(elementName, validatee)); 309 } else { 310 Validator validator = getValidator(xpath); 311 if(null != validator){ 312 failures.addAll(validator.validate(elementName, 313 elementDtdName, validatee)); 314 } else { 315 } 322 } 323 } 324 } 325 return failures; 326 } 327 328 329 341 public Collection validateIndividualProperty(String property, 342 String absoluteDtdName, String fieldName){ 343 ArrayList failures = new ArrayList (); 344 345 String xpath = utils.getParentName(absoluteDtdName, 346 Constants.XPATH_DELIMITER_CHAR); 347 Validator validator = getValidator(xpath); 348 if(null != validator){ 349 failures.addAll(validator.validateIndividualProperty( 350 property, absoluteDtdName, fieldName)); 351 } else { 352 } 359 360 return failures; 361 } 362 363 364 377 private Collection recurse(String elementName, Validatee validatee){ 378 ArrayList failures = new ArrayList (); 379 boolean isIndexed = validatee.isIndexed(elementName); 380 381 if(isIndexed){ 382 failures.addAll(validateBeans(elementName, validatee)); 383 } else { 384 failures.addAll(validateBean(elementName, validatee)); 385 } 386 return failures; 387 } 388 389 390 406 private Collection validateCardinalConstraint(Validatee validatee, 407 String elementName, String elementDtdName){ 408 ArrayList failures = new ArrayList (); 409 int cardinal = validatee.getElementCardinal(elementName); 410 CardinalConstraint constraint = getCardinalConstraint(cardinal); 411 412 switch(cardinal){ 413 case Constants.MANDATORY_ARRAY : 414 case Constants.OPTIONAL_ARRAY: { 415 Object [] elements = 416 (Object [])validatee.getElements(elementName); 417 String name = validatee.getIndexedXPath() + 418 Constants.XPATH_DELIMITER + elementDtdName; 419 failures.addAll(constraint.match(elements, name)); 422 break; 423 } 424 case Constants.OPTIONAL_ELEMENT : { 425 Object element = validatee.getElement(elementName); 426 break; 427 } 428 case Constants.MANDATORY_ELEMENT : 429 default : { 430 Object element = validatee.getElement(elementName); 431 String name = validatee.getIndexedXPath() + 432 Constants.XPATH_DELIMITER + elementDtdName; 433 failures.addAll(constraint.match(element, name)); 436 } 437 break; 438 } 439 return failures; 440 } 441 442 443 454 private Validator getValidator(String xpath){ 455 Validator validator = null; 456 if(null == xpathToValidator){ 457 constructXpathToValidator(); 458 } 459 if(null != xpathToValidator) { 460 validator = (Validator)xpathToValidator.get(xpath); 461 } 462 return validator; 463 } 464 465 466 470 private void constructXpathToValidator(){ 471 473 if(null == validation){ 474 constructValidation(); 475 } 476 477 if(null == validation){ 478 String format = 479 BundleReader.getValue("Warning_Not_available"); Object [] arguments = new Object []{"Validation Data"}; System.out.println(MessageFormat.format(format, arguments)); 482 return; 483 } 484 485 486 xpathToValidator = new HashMap (); 487 int noOfElements = validation.sizeElement(); 488 Element element = null; 489 Validator validator = null; 490 String elementName = null; 491 String beanName = null; 492 Check check = null; 493 String checkName = null; 494 495 for(int i=0; i<noOfElements; i++) { 496 element = validation.getElement(i); 497 elementName = utils.getName(element.getName(), 498 Constants.XPATH_DELIMITER_CHAR); 499 beanName = utils.getParentName(element.getName(), 500 Constants.XPATH_DELIMITER_CHAR); 501 validator = (Validator) xpathToValidator.get(beanName); 502 if(null == validator) { 506 validator = new Validator(); 507 xpathToValidator.put(beanName, validator); 508 } 516 517 int noOfChecks = element.sizeCheck(); 518 Constraint constraint = null; 519 for(int j=0; j<noOfChecks; j++){ 520 check = element.getCheck(j); 521 constraint = getConstraint(check); 522 if(null != constraint){ 523 validator.addElementConstraint(elementName, constraint); 524 } 533 } 534 } 535 } 536 537 538 546 private void constructValidation() { 547 549 URL url = null; 550 InputStream inputStream = null; 551 552 if(validationFile != null){ 553 inputStream = getInputStream(validationFile); 554 if(inputStream == null){ 555 String format = 556 BundleReader.getValue("MSG_using_the_default_file"); Object [] arguments = new Object []{defaultValidationFile}; 558 System.out.println(MessageFormat.format(format, arguments)); 559 560 inputStream = getDafaultStream(); 561 } 562 } else { 563 inputStream = getDafaultStream(); 564 } 565 566 if(inputStream != null){ 567 try { 569 validation = Validation.createGraph(inputStream); 570 } catch(Exception e) { 571 System.out.println(e.getMessage()); 572 validation = null; 573 } 574 } 575 } 576 577 578 593 private Constraint getConstraint(Check check){ 594 Constraint constraint = null; 595 if(null == constraints) { 596 constructConstraints(); 597 } 598 CheckInfo checkInfo = null; 599 if(null != constraints){ 600 String checkName = check.getName(); 601 checkInfo = getCheckInfo(checkName, constraints); 602 if(null != checkInfo){ 603 constraint = buildConstraint(check, checkInfo); 604 } else { 605 String format = 606 BundleReader.getValue("MSG_No_definition_for"); Object [] arguments = 608 new Object []{"CheckInfo", checkName}; System.out.println(MessageFormat.format(format, arguments)); 610 } 611 } else { 612 String format = 613 BundleReader.getValue("MSG_Not_defined"); Object [] arguments = new Object []{"Constraints"}; System.out.println(MessageFormat.format(format, arguments)); 616 } 617 return constraint; 618 } 619 620 621 627 private void constructConstraints() { 628 Constraints customConstraints = null; 629 Constraints defaultConstraints = null; 630 631 String constraintsFile = 632 System.getProperty("constraints.file"); if(constraintsFile != null){ 635 customConstraints = getConstraints(constraintsFile); 636 } 637 638 if(defaultConstraintsFile != null){ 639 defaultConstraints = getConstraints(defaultConstraintsFile); 640 } 641 642 if(customConstraints != null){ 643 if(defaultConstraints != null){ 644 int count = defaultConstraints.sizeCheckInfo(); 645 CheckInfo checkInfo = null; 646 CheckInfo checkInfoClone = null; 647 for(int i=0; i<count; i++){ 648 checkInfo = defaultConstraints.getCheckInfo(i); 649 checkInfoClone = (CheckInfo) checkInfo.clone(); 651 customConstraints.addCheckInfo(checkInfoClone); 653 } 654 constraints = customConstraints; 655 } else { 656 constraints = customConstraints; 657 } 658 } else { 659 constraints = defaultConstraints; 660 } 661 } 662 663 664 676 private CheckInfo getCheckInfo(String checkName, 677 Constraints constraints){ 678 CheckInfo checkInfo = null; 679 int size = constraints.sizeCheckInfo(); 680 for(int i=0; i<size; i++) { 681 checkInfo = constraints.getCheckInfo(i); 682 if(checkName.equals(checkInfo.getName())) { 683 return checkInfo; 684 } 685 } 686 return null; 687 } 688 689 690 713 private Constraint buildConstraint(Check check, CheckInfo checkInfo){ 714 Constraint constraint = null; 730 String classname = checkInfo.getClassname(); 731 Arguments arguments = checkInfo.getArguments(); 732 Class [] argumentTypeClass = new Class [1]; 733 Object [] argumentValue = new Object [1]; 734 735 String argumentName; 736 String argumentType; 737 738 constraint = (Constraint)utils.createObject(classname); 739 if(null != arguments){ 740 int size = arguments.sizeArgument(); 741 Parameters parameters = check.getParameters(); 742 743 if((null != parameters) && (size == parameters.sizeParameter())){ 744 Argument argument = null; 745 Parameter parameter = null; 746 for(int i=0; i<size; i++) { 747 argument = arguments.getArgument(i); 748 argumentName = argument.getName(); 749 argumentType = argument.getType(); 750 parameter = 751 getParameter(parameters, argumentName); 752 753 if(parameter == null){ 754 String format = BundleReader.getValue( 755 "Warning_no_value_specified_for"); Object [] substitutes = 757 new Object []{argumentName, check.getName()}; 758 System.out.println( 759 MessageFormat.format(format, substitutes)); 760 continue; 761 } 762 763 if(null == argumentType){ 764 argumentType = "java.lang.String"; } 766 767 int noOfValues = 1; 768 if(argumentType.equals("java.lang.String[]")){ Integer sz = (Integer )utils.getElement("value", parameter, SIZE_PREFIX); 771 noOfValues = sz.intValue(); 772 argumentType = "java.lang.String"; } 774 for(int j=0; j<noOfValues; j++) { 775 argumentValue[0] = utils.getElement("value", j, parameter); 777 argumentTypeClass[0] = 778 utils.getClass(argumentType); 779 String methodName = 780 utils.methodNameFromDtdName(argumentName, 781 SET_PREFIX); 782 Method method = 783 utils.getMethod(utils.getClass(constraint), 784 methodName, argumentTypeClass); 785 utils.invoke(constraint, method, argumentValue); 786 } 787 } 788 } else { 789 String format = BundleReader.getValue( 790 "MSG_Conflicting_Constraint_information"); Object [] substitues = new Object []{check.getName()}; String message = MessageFormat.format(format, substitues); 793 assert false : message; 794 } 795 } 796 return constraint; 797 } 798 799 800 814 private CardinalConstraint getCardinalConstraint(int cardinal){ 815 Class [] argumentTypes = new Class [] {int.class}; 816 Constructor constructor = 817 utils.getConstructor(CARDINAL_CONSTRAINT_CLASS, argumentTypes); 818 819 Integer parameter = new Integer (cardinal); 820 Object [] argumentValues = new Object [] {parameter}; 821 822 return (CardinalConstraint) utils.createObject(constructor, 823 argumentValues); 824 } 825 826 827 840 private Collection validateBeans(String elementName, 841 Validatee validatee){ 842 int noOfElements = 0; 845 String sizeMethodName = utils.methodNameFromBeanName(elementName, 846 SIZE_PREFIX); 847 Method sizeMethod = validatee.getMethod(sizeMethodName); 848 noOfElements = ((Integer )validatee.invoke(sizeMethod)).intValue(); 849 850 ArrayList failures = new ArrayList (); 851 Object child = null; 852 for(int i=0; i<noOfElements; i++) { 853 child = validatee.getElement(elementName, i); 854 if(child != null) { 855 failures.addAll(validate(child)); 856 } 857 } 858 return failures; 859 } 860 861 862 874 private Collection validateBean(String elementName, 875 Validatee validatee){ 876 ArrayList failures = new ArrayList (); 878 Object child = null; 879 child = validatee.getElement(elementName); 880 if(child != null) { 881 failures.addAll(validate(child)); 882 } 883 return failures; 884 } 885 886 887 896 private InputStream getDafaultStream(){ 897 InputStream inputStream = null; 898 URL url = null; 899 if(defaultValidationFile != null){ 900 url = utils.getUrlObject(defaultValidationFile); 901 if(url != null) { 902 try { 903 inputStream = url.openStream(); 904 } catch (IOException exception){ 905 System.out.println(exception.getMessage()); 906 } 907 } else { 908 assert false : (BundleReader.getValue( 909 "Error_control_should_never_reach_here")); 911 String format = 912 BundleReader.getValue("Error_can_not_access_file"); Object [] arguments = new Object []{defaultValidationFile}; 914 String message = MessageFormat.format(format, arguments); 915 assert false : message; 916 } 917 } else { 918 assert false : (BundleReader.getValue( 919 "Error_control_should_never_reach_here")); } 921 return inputStream; 922 } 923 924 925 931 private Parameter getParameter(Parameters parameters, String name){ 932 int size = parameters.sizeParameter(); 933 Parameter returnValue = null; 934 Parameter parameter = null; 935 String parameterName = null; 936 for(int i=0; i<size; i++){ 937 parameter = (Parameter)utils.getElement("parameter", i, parameters); 939 parameterName = parameter.getName(); 940 if(parameterName.equals(name)){ 941 returnValue = parameter; 942 break; 943 } 944 } 945 return returnValue; 946 } 947 948 949 955 private InputStream getInputStream(String inputFile){ 956 958 InputStream inputStream = null; 959 960 if(inputFile.lastIndexOf(':') == -1){ 961 URL url = null; 962 963 url = utils.getUrlObject(inputFile); 964 if(url != null) { 965 try { 966 inputStream = url.openStream(); 967 } catch (IOException exception){ 968 System.out.println(exception.getMessage()); 969 } 970 } else { 971 String format = BundleReader.getValue( 972 "Error_specified_file_can_not_be_used"); Object [] arguments = new Object []{inputFile}; 974 System.out.println(MessageFormat.format(format, arguments)); 975 } 976 } else { 977 File file = new File (inputFile); 978 if(file.exists()){ 979 try { 980 inputStream = new FileInputStream (file); 981 } catch(FileNotFoundException exception){ 982 System.out.println(exception.getMessage()); 983 inputStream = null; 984 } 985 } else { 986 String format = BundleReader.getValue( 987 "Error_specified_file_can_not_be_used"); Object [] arguments = new Object []{inputFile}; 989 System.out.println(MessageFormat.format(format, arguments)); 990 } 991 } 992 993 return inputStream; 994 } 995 996 997 1005 private String getValidateeImplementation(Object object, 1006 String propertiesFile){ 1007 String returnVal = null; 1008 Class classObject = utils.getClass(object); 1009 String className = classObject.getName(); 1010 1011 BundleReader.setBundle(propertiesFile); 1014 1015 String validateeImplName = BundleReader.getValue(className); 1016 while(!(className.equals("java.lang.Object"))){ if(!(validateeImplName.equals(className))){ 1018 returnVal = validateeImplName; 1019 break; 1020 } else { 1021 classObject = classObject.getSuperclass(); 1022 className = classObject.getName(); 1023 validateeImplName = BundleReader.getValue(className); 1024 } 1025 } 1026 return returnVal; 1027 } 1028 1029 1030 1037 private Constraints getConstraints(String constraintsFile){ 1038 URL url = null; 1039 InputStream inputStream = null; 1040 Constraints constraints = null; 1041 1042 if(constraintsFile != null){ 1043 inputStream = getInputStream(constraintsFile); 1044 } 1045 1046 if(inputStream != null){ 1048 try { 1049 constraints = Constraints.createGraph(inputStream); 1050 } catch(Exception e) { 1051 System.out.println(e.getMessage()); 1052 constraints = null; 1053 } 1054 } 1055 return constraints; 1056 } 1057} 1058 | Popular Tags |