1 16 17 package org.apache.xerces.impl.xs; 18 19 import org.apache.xerces.impl.dv.XSSimpleType; 20 import org.apache.xerces.impl.dv.InvalidDatatypeValueException; 21 import org.apache.xerces.impl.dv.ValidatedInfo; 22 import org.apache.xerces.impl.XMLErrorReporter; 23 import org.apache.xerces.impl.xs.models.CMBuilder; 24 import org.apache.xerces.impl.xs.models.XSCMValidator; 25 import org.apache.xerces.impl.xs.util.SimpleLocator; 26 import org.apache.xerces.xs.XSConstants; 27 import org.apache.xerces.xs.XSObjectList; 28 import org.apache.xerces.xs.XSTypeDefinition; 29 import org.apache.xerces.impl.dv.ValidationContext; 30 import org.apache.xerces.util.SymbolHash; 31 import java.util.Vector ; 32 33 42 public class XSConstraints { 43 44 static final int OCCURRENCE_UNKNOWN = SchemaSymbols.OCCURRENCE_UNBOUNDED-1; 45 static final XSSimpleType STRING_TYPE = (XSSimpleType)SchemaGrammar.SG_SchemaNS.getGlobalTypeDecl(SchemaSymbols.ATTVAL_STRING); 46 47 51 public static boolean checkTypeDerivationOk(XSTypeDefinition derived, XSTypeDefinition base, short block) { 52 if (derived == SchemaGrammar.fAnyType) 54 return derived == base; 55 if (derived == SchemaGrammar.fAnySimpleType) { 58 return (base == SchemaGrammar.fAnyType || 59 base == SchemaGrammar.fAnySimpleType); 60 } 61 62 if (derived.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) { 64 if (base.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) { 66 if (base == SchemaGrammar.fAnyType) 69 base = SchemaGrammar.fAnySimpleType; 70 else 71 return false; 72 } 73 return checkSimpleDerivation((XSSimpleType)derived, 74 (XSSimpleType)base, block); 75 } else { 76 return checkComplexDerivation((XSComplexTypeDecl)derived, base, block); 77 } 78 } 79 80 84 public static boolean checkSimpleDerivationOk(XSSimpleType derived, XSTypeDefinition base, short block) { 85 if (derived == SchemaGrammar.fAnySimpleType) { 88 return (base == SchemaGrammar.fAnyType || 89 base == SchemaGrammar.fAnySimpleType); 90 } 91 92 if (base.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) { 94 if (base == SchemaGrammar.fAnyType) 97 base = SchemaGrammar.fAnySimpleType; 98 else 99 return false; 100 } 101 return checkSimpleDerivation((XSSimpleType)derived, 102 (XSSimpleType)base, block); 103 } 104 105 109 public static boolean checkComplexDerivationOk(XSComplexTypeDecl derived, XSTypeDefinition base, short block) { 110 if (derived == SchemaGrammar.fAnyType) 112 return derived == base; 113 return checkComplexDerivation((XSComplexTypeDecl)derived, base, block); 114 } 115 116 121 private static boolean checkSimpleDerivation(XSSimpleType derived, XSSimpleType base, short block) { 122 if (derived == base) 124 return true; 125 126 if ((block & XSConstants.DERIVATION_RESTRICTION) != 0 || 129 (derived.getBaseType().getFinal() & XSConstants.DERIVATION_RESTRICTION) != 0) { 130 return false; 131 } 132 133 XSSimpleType directBase = (XSSimpleType)derived.getBaseType(); 136 if (directBase == base) 137 return true; 138 139 if (directBase != SchemaGrammar.fAnySimpleType && 141 checkSimpleDerivation(directBase, base, block)) { 142 return true; 143 } 144 145 if ((derived.getVariety() == XSSimpleType.VARIETY_LIST || 147 derived.getVariety() == XSSimpleType.VARIETY_UNION) && 148 base == SchemaGrammar.fAnySimpleType) { 149 return true; 150 } 151 152 if (base.getVariety() == XSSimpleType.VARIETY_UNION) { 154 XSObjectList subUnionMemberDV = base.getMemberTypes(); 155 int subUnionSize = subUnionMemberDV.getLength(); 156 for (int i=0; i<subUnionSize; i++) { 157 base = (XSSimpleType)subUnionMemberDV.item(i); 158 if (checkSimpleDerivation(derived, base, block)) 159 return true; 160 } 161 } 162 163 return false; 164 } 165 166 171 private static boolean checkComplexDerivation(XSComplexTypeDecl derived, XSTypeDefinition base, short block) { 172 if (derived == base) 174 return true; 175 176 if ((derived.fDerivedBy & block) != 0) 178 return false; 179 180 XSTypeDefinition directBase = derived.fBaseType; 182 if (directBase == base) 184 return true; 185 186 if (directBase == SchemaGrammar.fAnyType || 189 directBase == SchemaGrammar.fAnySimpleType) { 190 return false; 191 } 192 193 if (directBase.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) 196 return checkComplexDerivation((XSComplexTypeDecl)directBase, base, block); 197 198 if (directBase.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) { 200 if (base.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) { 202 if (base == SchemaGrammar.fAnyType) 205 base = SchemaGrammar.fAnySimpleType; 206 else 207 return false; 208 } 209 return checkSimpleDerivation((XSSimpleType)directBase, 210 (XSSimpleType)base, block); 211 } 212 213 return false; 214 } 215 216 221 public static Object ElementDefaultValidImmediate(XSTypeDefinition type, String value, ValidationContext context, ValidatedInfo vinfo) { 222 223 XSSimpleType dv = null; 224 225 if (type.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) { 229 dv = (XSSimpleType)type; 230 } 231 232 else { 234 XSComplexTypeDecl ctype = (XSComplexTypeDecl)type; 236 if (ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_SIMPLE) { 239 dv = ctype.fXSSimpleType; 240 } 241 else if (ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_MIXED) { 243 if (!((XSParticleDecl)ctype.getParticle()).emptiable()) 244 return null; 245 } 246 else { 247 return null; 248 } 249 } 250 251 Object actualValue = null; 253 if (dv == null) { 254 dv = STRING_TYPE; 258 } 259 try { 260 actualValue = dv.validate(value, context, vinfo); 262 if (vinfo != null) 264 actualValue = dv.validate(vinfo.stringValue(), context, vinfo); 265 } catch (InvalidDatatypeValueException ide) { 266 return null; 267 } 268 269 return actualValue; 270 } 271 272 static void reportSchemaError(XMLErrorReporter errorReporter, 273 SimpleLocator loc, 274 String key, Object [] args) { 275 if (loc != null) { 276 errorReporter.reportError(loc, XSMessageFormatter.SCHEMA_DOMAIN, 277 key, args, XMLErrorReporter.SEVERITY_ERROR); 278 } 279 else { 280 errorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 281 key, args, XMLErrorReporter.SEVERITY_ERROR); 282 } 283 } 284 285 291 public static void fullSchemaChecking(XSGrammarBucket grammarBucket, 292 SubstitutionGroupHandler SGHandler, 293 CMBuilder cmBuilder, 294 XMLErrorReporter errorReporter) { 295 SchemaGrammar[] grammars = grammarBucket.getGrammars(); 298 for (int i = grammars.length-1; i >= 0; i--) { 299 SGHandler.addSubstitutionGroup(grammars[i].getSubstitutionGroups()); 300 } 301 302 XSParticleDecl fakeDerived = new XSParticleDecl(); 303 XSParticleDecl fakeBase = new XSParticleDecl(); 304 fakeDerived.fType = XSParticleDecl.PARTICLE_MODELGROUP; 305 fakeBase.fType = XSParticleDecl.PARTICLE_MODELGROUP; 306 for (int g = grammars.length-1; g >= 0; g--) { 309 XSGroupDecl [] redefinedGroups = grammars[g].getRedefinedGroupDecls(); 310 SimpleLocator [] rgLocators = grammars[g].getRGLocators(); 311 for(int i=0; i<redefinedGroups.length; ) { 312 XSGroupDecl derivedGrp = redefinedGroups[i++]; 313 XSModelGroupImpl derivedMG = derivedGrp.fModelGroup; 314 XSGroupDecl baseGrp = redefinedGroups[i++]; 315 XSModelGroupImpl baseMG = baseGrp.fModelGroup; 316 if(baseMG == null) { 317 if(derivedMG != null) { reportSchemaError(errorReporter, rgLocators[i/2-1], 319 "src-redefine.6.2.2", 320 new Object []{derivedGrp.fName, "rcase-Recurse.2"}); 321 } 322 } else { 323 fakeDerived.fValue = derivedMG; 324 fakeBase.fValue = baseMG; 325 try { 326 particleValidRestriction(fakeDerived, SGHandler, fakeBase, SGHandler); 327 } catch (XMLSchemaException e) { 328 String key = e.getKey(); 329 reportSchemaError(errorReporter, rgLocators[i/2-1], 330 key, 331 e.getArgs()); 332 reportSchemaError(errorReporter, rgLocators[i/2-1], 333 "src-redefine.6.2.2", 334 new Object []{derivedGrp.fName, key}); 335 } 336 } 337 } 338 } 339 340 XSComplexTypeDecl[] types; 343 SimpleLocator [] ctLocators; 344 boolean further, fullChecked; 350 int keepType; 352 SymbolHash elemTable = new SymbolHash(); 355 for (int i = grammars.length-1, j, k; i >= 0; i--) { 356 keepType = 0; 358 fullChecked = grammars[i].fFullChecked; 359 types = grammars[i].getUncheckedComplexTypeDecls(); 360 ctLocators = grammars[i].getUncheckedCTLocators(); 361 for (j = 0; j < types.length; j++) { 363 if (!fullChecked) { 366 if (types[j].fParticle!=null) { 368 elemTable.clear(); 369 try { 370 checkElementDeclsConsistent(types[j], types[j].fParticle, 371 elemTable, SGHandler); 372 } 373 catch (XMLSchemaException e) { 374 reportSchemaError(errorReporter, ctLocators[j], 375 e.getKey(), 376 e.getArgs()); 377 } 378 } 379 } 380 381 383 if (types[j].fBaseType != null && 384 types[j].fBaseType != SchemaGrammar.fAnyType && 385 types[j].fDerivedBy == XSConstants.DERIVATION_RESTRICTION && 386 (types[j].fBaseType instanceof XSComplexTypeDecl)) { 387 388 XSParticleDecl derivedParticle=types[j].fParticle; 389 XSParticleDecl baseParticle= 390 ((XSComplexTypeDecl)(types[j].fBaseType)).fParticle; 391 if (derivedParticle==null) { 392 if (baseParticle!=null && !baseParticle.emptiable()) { 393 reportSchemaError(errorReporter,ctLocators[j], 394 "derivation-ok-restriction.5.3.2", 395 new Object []{types[j].fName, types[j].fBaseType.getName()}); 396 } 397 } 398 else if (baseParticle!=null) { 399 try { 400 particleValidRestriction(types[j].fParticle, 401 SGHandler, 402 ((XSComplexTypeDecl)(types[j].fBaseType)).fParticle, 403 SGHandler); 404 } catch (XMLSchemaException e) { 405 reportSchemaError(errorReporter, ctLocators[j], 406 e.getKey(), 407 e.getArgs()); 408 reportSchemaError(errorReporter, ctLocators[j], 409 "derivation-ok-restriction.5.4.2", 410 new Object []{types[j].fName}); 411 } 412 } 413 else { 414 reportSchemaError(errorReporter, ctLocators[j], 415 "derivation-ok-restriction.5.4.2", 416 new Object []{types[j].fName}); 417 } 418 } 419 XSCMValidator cm = types[j].getContentModel(cmBuilder); 422 further = false; 423 if (cm != null) { 424 try { 425 further = cm.checkUniqueParticleAttribution(SGHandler); 426 } catch (XMLSchemaException e) { 427 reportSchemaError(errorReporter, ctLocators[j], 428 e.getKey(), 429 e.getArgs()); 430 } 431 } 432 439 440 if (!fullChecked && further) 443 types[keepType++] = types[j]; 444 445 } 449 if (!fullChecked) { 454 grammars[i].setUncheckedTypeNum(keepType); 455 grammars[i].fFullChecked = true; 456 } 457 } 458 } 459 460 463 464 public static void checkElementDeclsConsistent(XSComplexTypeDecl type, 465 XSParticleDecl particle, 466 SymbolHash elemDeclHash, 467 SubstitutionGroupHandler sgHandler) 468 throws XMLSchemaException { 469 470 472 int pType = particle.fType; 473 474 if (pType == XSParticleDecl.PARTICLE_WILDCARD) 475 return; 476 477 if (pType == XSParticleDecl.PARTICLE_ELEMENT) { 478 XSElementDecl elem = (XSElementDecl)(particle.fValue); 479 findElemInTable(type, elem, elemDeclHash); 480 481 if (elem.fScope == XSConstants.SCOPE_GLOBAL) { 482 XSElementDecl[] subGroup = sgHandler.getSubstitutionGroup(elem); 484 for (int i = 0; i < subGroup.length; i++) { 485 findElemInTable(type, subGroup[i], elemDeclHash); 486 } 487 } 488 return; 489 } 490 491 XSModelGroupImpl group = (XSModelGroupImpl)particle.fValue; 492 for (int i = 0; i < group.fParticleCount; i++) 493 checkElementDeclsConsistent(type, group.fParticles[i], elemDeclHash, sgHandler); 494 } 495 496 public static void findElemInTable(XSComplexTypeDecl type, XSElementDecl elem, 497 SymbolHash elemDeclHash) 498 throws XMLSchemaException { 499 500 String name = elem.fName + "," + elem.fTargetNamespace; 502 503 XSElementDecl existingElem = null; 504 if ((existingElem = (XSElementDecl)(elemDeclHash.get(name))) == null) { 505 elemDeclHash.put(name, elem); 507 } 508 else { 509 if (elem == existingElem) 511 return; 512 513 if (elem.fType != existingElem.fType) { 514 throw new XMLSchemaException("cos-element-consistent", 516 new Object [] {type.fName, elem.fName}); 517 518 } 519 } 520 } 521 522 525 private static void particleValidRestriction(XSParticleDecl dParticle, 526 SubstitutionGroupHandler dSGHandler, 527 XSParticleDecl bParticle, 528 SubstitutionGroupHandler bSGHandler) 529 throws XMLSchemaException { 530 particleValidRestriction(dParticle, dSGHandler, bParticle, bSGHandler, true); 531 } 532 533 private static void particleValidRestriction(XSParticleDecl dParticle, 534 SubstitutionGroupHandler dSGHandler, 535 XSParticleDecl bParticle, 536 SubstitutionGroupHandler bSGHandler, 537 boolean checkWCOccurrence) 538 throws XMLSchemaException { 539 540 Vector dChildren = null; 541 Vector bChildren = null; 542 int dMinEffectiveTotalRange=OCCURRENCE_UNKNOWN; 543 int dMaxEffectiveTotalRange=OCCURRENCE_UNKNOWN; 544 545 546 if (dParticle.isEmpty() && !bParticle.emptiable()) { 549 throw new XMLSchemaException("cos-particle-restrict.a", null); 550 } 551 else if (!dParticle.isEmpty() && bParticle.isEmpty()) { 552 throw new XMLSchemaException("cos-particle-restrict.b", null); 553 } 554 555 563 short dType = dParticle.fType; 564 if (dType == XSParticleDecl.PARTICLE_MODELGROUP) { 568 dType = ((XSModelGroupImpl)dParticle.fValue).fCompositor; 569 570 XSParticleDecl dtmp = getNonUnaryGroup(dParticle); 574 if (dtmp != dParticle) { 575 dParticle = dtmp; 577 dType = dParticle.fType; 578 if (dType == XSParticleDecl.PARTICLE_MODELGROUP) 579 dType = ((XSModelGroupImpl)dParticle.fValue).fCompositor; 580 } 581 582 dChildren = removePointlessChildren(dParticle); 585 } 586 587 int dMinOccurs = dParticle.fMinOccurs; 588 int dMaxOccurs = dParticle.fMaxOccurs; 589 590 if (dSGHandler != null && dType == XSParticleDecl.PARTICLE_ELEMENT) { 594 XSElementDecl dElement = (XSElementDecl)dParticle.fValue; 595 596 if (dElement.fScope == XSConstants.SCOPE_GLOBAL) { 597 XSElementDecl[] subGroup = dSGHandler.getSubstitutionGroup(dElement); 601 if (subGroup.length >0 ) { 602 dType = XSModelGroupImpl.MODELGROUP_CHOICE; 605 dMinEffectiveTotalRange = dMinOccurs; 606 dMaxEffectiveTotalRange = dMaxOccurs; 607 608 dChildren = new Vector (subGroup.length+1); 610 for (int i = 0; i < subGroup.length; i++) { 611 addElementToParticleVector(dChildren, subGroup[i]); 612 } 613 addElementToParticleVector(dChildren, dElement); 614 615 dSGHandler = null; 618 } 619 } 620 } 621 622 short bType = bParticle.fType; 623 if (bType == XSParticleDecl.PARTICLE_MODELGROUP) { 627 bType = ((XSModelGroupImpl)bParticle.fValue).fCompositor; 628 629 XSParticleDecl btmp = getNonUnaryGroup(bParticle); 633 if (btmp != bParticle) { 634 bParticle = btmp; 636 bType = bParticle.fType; 637 if (bType == XSParticleDecl.PARTICLE_MODELGROUP) 638 bType = ((XSModelGroupImpl)bParticle.fValue).fCompositor; 639 } 640 641 bChildren = removePointlessChildren(bParticle); 644 } 645 646 int bMinOccurs = bParticle.fMinOccurs; 647 int bMaxOccurs = bParticle.fMaxOccurs; 648 649 if (bSGHandler != null && bType == XSParticleDecl.PARTICLE_ELEMENT) { 650 XSElementDecl bElement = (XSElementDecl)bParticle.fValue; 651 652 if (bElement.fScope == XSConstants.SCOPE_GLOBAL) { 653 XSElementDecl[] bsubGroup = bSGHandler.getSubstitutionGroup(bElement); 657 if (bsubGroup.length >0 ) { 658 bType = XSModelGroupImpl.MODELGROUP_CHOICE; 660 661 bChildren = new Vector (bsubGroup.length+1); 662 for (int i = 0; i < bsubGroup.length; i++) { 663 addElementToParticleVector(bChildren, bsubGroup[i]); 664 } 665 addElementToParticleVector(bChildren, bElement); 666 bSGHandler = null; 669 } 670 } 671 } 672 673 switch (dType) { 677 case XSParticleDecl.PARTICLE_ELEMENT: 678 { 679 switch (bType) { 680 681 case XSParticleDecl.PARTICLE_ELEMENT: 683 { 684 checkNameAndTypeOK((XSElementDecl)dParticle.fValue,dMinOccurs,dMaxOccurs, 685 (XSElementDecl)bParticle.fValue,bMinOccurs,bMaxOccurs); 686 return; 687 } 688 689 case XSParticleDecl.PARTICLE_WILDCARD: 691 { 692 checkNSCompat((XSElementDecl)dParticle.fValue,dMinOccurs,dMaxOccurs, 693 (XSWildcardDecl)bParticle.fValue,bMinOccurs,bMaxOccurs, 694 checkWCOccurrence); 695 return; 696 } 697 698 case XSModelGroupImpl.MODELGROUP_CHOICE: 700 { 701 dChildren = new Vector (); 704 dChildren.addElement(dParticle); 705 706 checkRecurseLax(dChildren, 1, 1, dSGHandler, 707 bChildren, bMinOccurs, bMaxOccurs, bSGHandler); 708 return; 709 } 710 case XSModelGroupImpl.MODELGROUP_SEQUENCE: 711 case XSModelGroupImpl.MODELGROUP_ALL: 712 { 713 dChildren = new Vector (); 716 dChildren.addElement(dParticle); 717 718 checkRecurse(dChildren, 1, 1, dSGHandler, 719 bChildren, bMinOccurs, bMaxOccurs, bSGHandler); 720 return; 721 } 722 723 default: 724 { 725 throw new XMLSchemaException("Internal-Error", 726 new Object []{"in particleValidRestriction"}); 727 } 728 } 729 } 730 731 case XSParticleDecl.PARTICLE_WILDCARD: 732 { 733 switch (bType) { 734 735 case XSParticleDecl.PARTICLE_WILDCARD: 737 { 738 checkNSSubset((XSWildcardDecl)dParticle.fValue, dMinOccurs, dMaxOccurs, 739 (XSWildcardDecl)bParticle.fValue, bMinOccurs, bMaxOccurs); 740 return; 741 } 742 743 case XSModelGroupImpl.MODELGROUP_CHOICE: 744 case XSModelGroupImpl.MODELGROUP_SEQUENCE: 745 case XSModelGroupImpl.MODELGROUP_ALL: 746 case XSParticleDecl.PARTICLE_ELEMENT: 747 { 748 throw new XMLSchemaException("cos-particle-restrict.2", 749 new Object []{"any:choice,sequence,all,elt"}); 750 } 751 752 default: 753 { 754 throw new XMLSchemaException("Internal-Error", 755 new Object []{"in particleValidRestriction"}); 756 } 757 } 758 } 759 760 case XSModelGroupImpl.MODELGROUP_ALL: 761 { 762 switch (bType) { 763 764 case XSParticleDecl.PARTICLE_WILDCARD: 766 { 767 if (dMinEffectiveTotalRange == OCCURRENCE_UNKNOWN) 768 dMinEffectiveTotalRange = dParticle.minEffectiveTotalRange(); 769 if (dMaxEffectiveTotalRange == OCCURRENCE_UNKNOWN) 770 dMaxEffectiveTotalRange = dParticle.maxEffectiveTotalRange(); 771 772 checkNSRecurseCheckCardinality(dChildren, dMinEffectiveTotalRange, 773 dMaxEffectiveTotalRange, 774 dSGHandler, 775 bParticle,bMinOccurs,bMaxOccurs, 776 checkWCOccurrence); 777 778 return; 779 } 780 781 case XSModelGroupImpl.MODELGROUP_ALL: 782 { 783 checkRecurse(dChildren, dMinOccurs, dMaxOccurs, dSGHandler, 784 bChildren, bMinOccurs, bMaxOccurs, bSGHandler); 785 return; 786 } 787 788 case XSModelGroupImpl.MODELGROUP_CHOICE: 789 case XSModelGroupImpl.MODELGROUP_SEQUENCE: 790 case XSParticleDecl.PARTICLE_ELEMENT: 791 { 792 throw new XMLSchemaException("cos-particle-restrict.2", 793 new Object []{"all:choice,sequence,elt"}); 794 } 795 796 default: 797 { 798 throw new XMLSchemaException("Internal-Error", 799 new Object []{"in particleValidRestriction"}); 800 } 801 } 802 } 803 804 case XSModelGroupImpl.MODELGROUP_CHOICE: 805 { 806 switch (bType) { 807 808 case XSParticleDecl.PARTICLE_WILDCARD: 810 { 811 if (dMinEffectiveTotalRange == OCCURRENCE_UNKNOWN) 812 dMinEffectiveTotalRange = dParticle.minEffectiveTotalRange(); 813 if (dMaxEffectiveTotalRange == OCCURRENCE_UNKNOWN) 814 dMaxEffectiveTotalRange = dParticle.maxEffectiveTotalRange(); 815 816 checkNSRecurseCheckCardinality(dChildren, dMinEffectiveTotalRange, 817 dMaxEffectiveTotalRange, 818 dSGHandler, 819 bParticle,bMinOccurs,bMaxOccurs, 820 checkWCOccurrence); 821 return; 822 } 823 824 case XSModelGroupImpl.MODELGROUP_CHOICE: 825 { 826 checkRecurseLax(dChildren, dMinOccurs, dMaxOccurs, dSGHandler, 827 bChildren, bMinOccurs, bMaxOccurs, bSGHandler); 828 return; 829 } 830 831 case XSModelGroupImpl.MODELGROUP_ALL: 832 case XSModelGroupImpl.MODELGROUP_SEQUENCE: 833 case XSParticleDecl.PARTICLE_ELEMENT: 834 { 835 throw new XMLSchemaException("cos-particle-restrict.2", 836 new Object []{"choice:all,sequence,elt"}); 837 } 838 839 default: 840 { 841 throw new XMLSchemaException("Internal-Error", 842 new Object []{"in particleValidRestriction"}); 843 } 844 } 845 } 846 847 848 case XSModelGroupImpl.MODELGROUP_SEQUENCE: 849 { 850 switch (bType) { 851 852 case XSParticleDecl.PARTICLE_WILDCARD: 854 { 855 if (dMinEffectiveTotalRange == OCCURRENCE_UNKNOWN) 856 dMinEffectiveTotalRange = dParticle.minEffectiveTotalRange(); 857 if (dMaxEffectiveTotalRange == OCCURRENCE_UNKNOWN) 858 dMaxEffectiveTotalRange = dParticle.maxEffectiveTotalRange(); 859 860 checkNSRecurseCheckCardinality(dChildren, dMinEffectiveTotalRange, 861 dMaxEffectiveTotalRange, 862 dSGHandler, 863 bParticle,bMinOccurs,bMaxOccurs, 864 checkWCOccurrence); 865 return; 866 } 867 868 case XSModelGroupImpl.MODELGROUP_ALL: 869 { 870 checkRecurseUnordered(dChildren, dMinOccurs, dMaxOccurs, dSGHandler, 871 bChildren, bMinOccurs, bMaxOccurs, bSGHandler); 872 return; 873 } 874 875 case XSModelGroupImpl.MODELGROUP_SEQUENCE: 876 { 877 checkRecurse(dChildren, dMinOccurs, dMaxOccurs, dSGHandler, 878 bChildren, bMinOccurs, bMaxOccurs, bSGHandler); 879 return; 880 } 881 882 case XSModelGroupImpl.MODELGROUP_CHOICE: 883 { 884 int min1 = dMinOccurs * dChildren.size(); 885 int max1 = (dMaxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED)? 886 dMaxOccurs : dMaxOccurs * dChildren.size(); 887 checkMapAndSum(dChildren, min1, max1, dSGHandler, 888 bChildren, bMinOccurs, bMaxOccurs, bSGHandler); 889 return; 890 } 891 892 case XSParticleDecl.PARTICLE_ELEMENT: 893 { 894 throw new XMLSchemaException("cos-particle-restrict.2", 895 new Object []{"seq:elt"}); 896 } 897 898 default: 899 { 900 throw new XMLSchemaException("Internal-Error", 901 new Object []{"in particleValidRestriction"}); 902 } 903 } 904 } 905 906 } 907 } 908 909 private static void addElementToParticleVector (Vector v, XSElementDecl d) { 910 911 XSParticleDecl p = new XSParticleDecl(); 912 p.fValue = d; 913 p.fType = XSParticleDecl.PARTICLE_ELEMENT; 914 v.addElement(p); 915 916 } 917 918 private static XSParticleDecl getNonUnaryGroup(XSParticleDecl p) { 919 920 if (p.fType == XSParticleDecl.PARTICLE_ELEMENT || 921 p.fType == XSParticleDecl.PARTICLE_WILDCARD) 922 return p; 923 924 if (p.fMinOccurs==1 && p.fMaxOccurs==1 && 925 p.fValue!=null && ((XSModelGroupImpl)p.fValue).fParticleCount == 1) 926 return getNonUnaryGroup(((XSModelGroupImpl)p.fValue).fParticles[0]); 927 else 928 return p; 929 } 930 931 private static Vector removePointlessChildren(XSParticleDecl p) { 932 933 934 if (p.fType == XSParticleDecl.PARTICLE_ELEMENT || 935 p.fType == XSParticleDecl.PARTICLE_WILDCARD) 936 return null; 937 938 Vector children = new Vector (); 939 940 XSModelGroupImpl group = (XSModelGroupImpl)p.fValue; 941 for (int i = 0; i < group.fParticleCount; i++) 942 gatherChildren(group.fCompositor, group.fParticles[i], children); 943 944 return children; 945 } 946 947 948 private static void gatherChildren(int parentType, XSParticleDecl p, Vector children) { 949 950 int min = p.fMinOccurs; 951 int max = p.fMaxOccurs; 952 int type = p.fType; 953 if (type == XSParticleDecl.PARTICLE_MODELGROUP) 954 type = ((XSModelGroupImpl)p.fValue).fCompositor; 955 956 if (type == XSParticleDecl.PARTICLE_ELEMENT || 957 type== XSParticleDecl.PARTICLE_WILDCARD) { 958 children.addElement(p); 959 return; 960 } 961 962 if (! (min==1 && max==1)) { 963 children.addElement(p); 964 } 965 else if (parentType == type) { 966 XSModelGroupImpl group = (XSModelGroupImpl)p.fValue; 967 for (int i = 0; i < group.fParticleCount; i++) 968 gatherChildren(type, group.fParticles[i], children); 969 } 970 else if (!p.isEmpty()) { 971 children.addElement(p); 972 } 973 974 } 975 976 private static void checkNameAndTypeOK(XSElementDecl dElement, int dMin, int dMax, 977 XSElementDecl bElement, int bMin, int bMax) 978 throws XMLSchemaException { 979 980 981 if (dElement.fName != bElement.fName || 985 dElement.fTargetNamespace != bElement.fTargetNamespace) { 986 throw new XMLSchemaException( 987 "rcase-NameAndTypeOK.1",new Object []{dElement.fName, 988 dElement.fTargetNamespace, bElement.fName, bElement.fTargetNamespace}); 989 } 990 991 if (!bElement.getNillable() && dElement.getNillable()) { 995 throw new XMLSchemaException("rcase-NameAndTypeOK.2", 996 new Object []{dElement.fName}); 997 } 998 999 if (!checkOccurrenceRange(dMin, dMax, bMin, bMax)) { 1003 throw new XMLSchemaException("rcase-NameAndTypeOK.3", 1004 new Object []{ 1005 dElement.fName, 1006 Integer.toString(dMin), 1007 dMax==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(dMax), 1008 Integer.toString(bMin), 1009 bMax==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(bMax)}); 1010 } 1011 1012 if (bElement.getConstraintType() == XSConstants.VC_FIXED) { 1016 if (dElement.getConstraintType() != XSConstants.VC_FIXED) { 1018 throw new XMLSchemaException("rcase-NameAndTypeOK.4.a", 1019 new Object []{dElement.fName, bElement.fDefault.stringValue()}); 1020 } 1021 1022 boolean isSimple = false; 1024 if (dElement.fType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE || 1025 ((XSComplexTypeDecl)dElement.fType).fContentType == XSComplexTypeDecl.CONTENTTYPE_SIMPLE) { 1026 isSimple = true; 1027 } 1028 1029 if (!isSimple && !bElement.fDefault.normalizedValue.equals(dElement.fDefault.normalizedValue) || 1031 isSimple && !bElement.fDefault.actualValue.equals(dElement.fDefault.actualValue)) { 1032 throw new XMLSchemaException("rcase-NameAndTypeOK.4.b", 1033 new Object []{dElement.fName, 1034 dElement.fDefault.stringValue(), 1035 bElement.fDefault.stringValue()}); 1036 } 1037 } 1038 1039 checkIDConstraintRestriction(dElement, bElement); 1043 1044 int blockSet1 = dElement.fBlock; 1048 int blockSet2 = bElement.fBlock; 1049 if (((blockSet1 & blockSet2)!=blockSet2) || 1050 (blockSet1==XSConstants.DERIVATION_NONE && blockSet2!=XSConstants.DERIVATION_NONE)) 1051 throw new XMLSchemaException("rcase-NameAndTypeOK.6", 1052 new Object []{dElement.fName}); 1053 1054 1055 if (!checkTypeDerivationOk(dElement.fType, bElement.fType, 1059 (short)(XSConstants.DERIVATION_EXTENSION|XSConstants.DERIVATION_LIST|XSConstants.DERIVATION_UNION))) { 1060 throw new XMLSchemaException("rcase-NameAndTypeOK.7", 1061 new Object []{dElement.fName, dElement.fType.getName(), bElement.fType.getName()}); 1062 } 1063 1064 } 1065 1066 1067 private static void checkIDConstraintRestriction(XSElementDecl derivedElemDecl, 1068 XSElementDecl baseElemDecl) 1069 throws XMLSchemaException { 1070 } 1073 1074 private static boolean checkOccurrenceRange(int min1, int max1, int min2, int max2) { 1075 1076 if ((min1 >= min2) && 1077 ((max2==SchemaSymbols.OCCURRENCE_UNBOUNDED) || 1078 (max1!=SchemaSymbols.OCCURRENCE_UNBOUNDED && max1<=max2))) 1079 return true; 1080 else 1081 return false; 1082 } 1083 1084 private static void checkNSCompat(XSElementDecl elem, int min1, int max1, 1085 XSWildcardDecl wildcard, int min2, int max2, 1086 boolean checkWCOccurrence) 1087 throws XMLSchemaException { 1088 1089 if (checkWCOccurrence && !checkOccurrenceRange(min1,max1,min2,max2)) { 1091 throw new XMLSchemaException("rcase-NSCompat.2", 1092 new Object []{ 1093 elem.fName, 1094 Integer.toString(min1), 1095 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1), 1096 Integer.toString(min2), 1097 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)}); 1098 } 1099 1100 if (!wildcard.allowNamespace(elem.fTargetNamespace)) { 1102 throw new XMLSchemaException("rcase-NSCompat.1", 1103 new Object []{elem.fName,elem.fTargetNamespace}); 1104 } 1105 1106 } 1107 1108 private static void checkNSSubset(XSWildcardDecl dWildcard, int min1, int max1, 1109 XSWildcardDecl bWildcard, int min2, int max2) 1110 throws XMLSchemaException { 1111 1112 if (!checkOccurrenceRange(min1,max1,min2,max2)) { 1114 throw new XMLSchemaException("rcase-NSSubset.2", new Object []{ 1115 Integer.toString(min1), 1116 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1), 1117 Integer.toString(min2), 1118 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)}); 1119 } 1120 1121 if (!dWildcard.isSubsetOf(bWildcard)) { 1123 throw new XMLSchemaException("rcase-NSSubset.1", null); 1124 } 1125 1126 if (dWildcard.weakerProcessContents(bWildcard)) { 1127 throw new XMLSchemaException("rcase-NSSubset.3", 1128 new Object []{dWildcard.getProcessContentsAsString(), 1129 bWildcard.getProcessContentsAsString()}); 1130 } 1131 1132 } 1133 1134 1135 private static void checkNSRecurseCheckCardinality(Vector children, int min1, int max1, 1136 SubstitutionGroupHandler dSGHandler, 1137 XSParticleDecl wildcard, int min2, int max2, 1138 boolean checkWCOccurrence) 1139 throws XMLSchemaException { 1140 1141 1142 if (checkWCOccurrence && !checkOccurrenceRange(min1,max1,min2,max2)) { 1144 throw new XMLSchemaException("rcase-NSRecurseCheckCardinality.2", new Object []{ 1145 Integer.toString(min1), 1146 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1), 1147 Integer.toString(min2), 1148 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)}); 1149 } 1150 1151 int count = children.size(); 1153 try { 1154 for (int i = 0; i < count; i++) { 1155 XSParticleDecl particle1 = (XSParticleDecl)children.elementAt(i); 1156 particleValidRestriction(particle1, dSGHandler, wildcard, null, false); 1157 1158 } 1159 } 1160 catch (XMLSchemaException e) { 1163 throw new XMLSchemaException("rcase-NSRecurseCheckCardinality.1", null); 1164 } 1165 1166 } 1167 1168 private static void checkRecurse(Vector dChildren, int min1, int max1, 1169 SubstitutionGroupHandler dSGHandler, 1170 Vector bChildren, int min2, int max2, 1171 SubstitutionGroupHandler bSGHandler) 1172 throws XMLSchemaException { 1173 1174 if (!checkOccurrenceRange(min1,max1,min2,max2)) { 1176 throw new XMLSchemaException("rcase-Recurse.1", new Object []{ 1177 Integer.toString(min1), 1178 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1), 1179 Integer.toString(min2), 1180 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)}); 1181 } 1182 1183 int count1= dChildren.size(); 1184 int count2= bChildren.size(); 1185 1186 int current = 0; 1187 label: for (int i = 0; i<count1; i++) { 1188 1189 XSParticleDecl particle1 = (XSParticleDecl)dChildren.elementAt(i); 1190 for (int j = current; j<count2; j++) { 1191 XSParticleDecl particle2 = (XSParticleDecl)bChildren.elementAt(j); 1192 current +=1; 1193 try { 1194 particleValidRestriction(particle1, dSGHandler, particle2, bSGHandler); 1195 continue label; 1196 } 1197 catch (XMLSchemaException e) { 1198 if (!particle2.emptiable()) 1199 throw new XMLSchemaException("rcase-Recurse.2", null); 1200 } 1201 } 1202 throw new XMLSchemaException("rcase-Recurse.2", null); 1203 } 1204 1205 for (int j=current; j < count2; j++) { 1207 XSParticleDecl particle2 = (XSParticleDecl)bChildren.elementAt(j); 1208 if (!particle2.emptiable()) { 1209 throw new XMLSchemaException("rcase-Recurse.2", null); 1210 } 1211 } 1212 1213 } 1214 1215 private static void checkRecurseUnordered(Vector dChildren, int min1, int max1, 1216 SubstitutionGroupHandler dSGHandler, 1217 Vector bChildren, int min2, int max2, 1218 SubstitutionGroupHandler bSGHandler) 1219 throws XMLSchemaException { 1220 1221 1222 if (!checkOccurrenceRange(min1,max1,min2,max2)) { 1224 throw new XMLSchemaException("rcase-RecurseUnordered.1", new Object []{ 1225 Integer.toString(min1), 1226 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1), 1227 Integer.toString(min2), 1228 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)}); 1229 } 1230 1231 int count1= dChildren.size(); 1232 int count2 = bChildren.size(); 1233 1234 boolean foundIt[] = new boolean[count2]; 1235 1236 label: for (int i = 0; i<count1; i++) { 1237 XSParticleDecl particle1 = (XSParticleDecl)dChildren.elementAt(i); 1238 1239 for (int j = 0; j<count2; j++) { 1240 XSParticleDecl particle2 = (XSParticleDecl)bChildren.elementAt(j); 1241 try { 1242 particleValidRestriction(particle1, dSGHandler, particle2, bSGHandler); 1243 if (foundIt[j]) 1244 throw new XMLSchemaException("rcase-RecurseUnordered.2", null); 1245 else 1246 foundIt[j]=true; 1247 1248 continue label; 1249 } 1250 catch (XMLSchemaException e) { 1251 } 1252 } 1253 throw new XMLSchemaException("rcase-RecurseUnordered.2", null); 1255 } 1256 1257 for (int j=0; j < count2; j++) { 1259 XSParticleDecl particle2 = (XSParticleDecl)bChildren.elementAt(j); 1260 if (!foundIt[j] && !particle2.emptiable()) { 1261 throw new XMLSchemaException("rcase-RecurseUnordered.2", null); 1262 } 1263 } 1264 1265 } 1266 1267 private static void checkRecurseLax(Vector dChildren, int min1, int max1, 1268 SubstitutionGroupHandler dSGHandler, 1269 Vector bChildren, int min2, int max2, 1270 SubstitutionGroupHandler bSGHandler) 1271 throws XMLSchemaException { 1272 1273 if (!checkOccurrenceRange(min1,max1,min2,max2)) { 1275 throw new XMLSchemaException("rcase-RecurseLax.1", new Object []{ 1276 Integer.toString(min1), 1277 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1), 1278 Integer.toString(min2), 1279 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)}); 1280 } 1281 1282 int count1= dChildren.size(); 1283 int count2 = bChildren.size(); 1284 1285 int current = 0; 1286 label: for (int i = 0; i<count1; i++) { 1287 1288 XSParticleDecl particle1 = (XSParticleDecl)dChildren.elementAt(i); 1289 for (int j = current; j<count2; j++) { 1290 XSParticleDecl particle2 = (XSParticleDecl)bChildren.elementAt(j); 1291 current +=1; 1292 try { 1293 particleValidRestriction(particle1, dSGHandler, particle2, bSGHandler); 1294 continue label; 1295 } 1296 catch (XMLSchemaException e) { 1297 } 1298 } 1299 throw new XMLSchemaException("rcase-RecurseLax.2", null); 1301 1302 } 1303 1304 } 1305 1306 private static void checkMapAndSum(Vector dChildren, int min1, int max1, 1307 SubstitutionGroupHandler dSGHandler, 1308 Vector bChildren, int min2, int max2, 1309 SubstitutionGroupHandler bSGHandler) 1310 throws XMLSchemaException { 1311 1312 1314 1326 if (!checkOccurrenceRange(min1,max1,min2,max2)) { 1328 throw new XMLSchemaException("rcase-MapAndSum.2", 1329 new Object []{Integer.toString(min1), 1330 max1==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max1), 1331 Integer.toString(min2), 1332 max2==SchemaSymbols.OCCURRENCE_UNBOUNDED?"unbounded":Integer.toString(max2)}); 1333 } 1334 1335 int count1 = dChildren.size(); 1336 int count2 = bChildren.size(); 1337 1338 label: for (int i = 0; i<count1; i++) { 1339 1340 XSParticleDecl particle1 = (XSParticleDecl)dChildren.elementAt(i); 1341 for (int j = 0; j<count2; j++) { 1342 XSParticleDecl particle2 = (XSParticleDecl)bChildren.elementAt(j); 1343 try { 1344 particleValidRestriction(particle1, dSGHandler, particle2, bSGHandler); 1345 continue label; 1346 } 1347 catch (XMLSchemaException e) { 1348 } 1349 } 1350 throw new XMLSchemaException("rcase-MapAndSum.1", null); 1352 } 1353 } 1354 public static boolean overlapUPA(XSElementDecl element1, 1356 XSElementDecl element2, 1357 SubstitutionGroupHandler sgHandler) { 1358 if (element1.fName == element2.fName && 1360 element1.fTargetNamespace == element2.fTargetNamespace) { 1361 return true; 1362 } 1363 1364 XSElementDecl[] subGroup = sgHandler.getSubstitutionGroup(element1); 1367 for (int i = subGroup.length-1; i >= 0; i--) { 1368 if (subGroup[i].fName == element2.fName && 1369 subGroup[i].fTargetNamespace == element2.fTargetNamespace) { 1370 return true; 1371 } 1372 } 1373 1374 subGroup = sgHandler.getSubstitutionGroup(element2); 1377 for (int i = subGroup.length-1; i >= 0; i--) { 1378 if (subGroup[i].fName == element1.fName && 1379 subGroup[i].fTargetNamespace == element1.fTargetNamespace) { 1380 return true; 1381 } 1382 } 1383 1384 return false; 1385 } 1386 1387 public static boolean overlapUPA(XSElementDecl element, 1390 XSWildcardDecl wildcard, 1391 SubstitutionGroupHandler sgHandler) { 1392 if (wildcard.allowNamespace(element.fTargetNamespace)) 1394 return true; 1395 1396 XSElementDecl[] subGroup = sgHandler.getSubstitutionGroup(element); 1398 for (int i = subGroup.length-1; i >= 0; i--) { 1399 if (wildcard.allowNamespace(subGroup[i].fTargetNamespace)) 1400 return true; 1401 } 1402 1403 return false; 1404 } 1405 1406 public static boolean overlapUPA(XSWildcardDecl wildcard1, 1407 XSWildcardDecl wildcard2) { 1408 XSWildcardDecl intersect = wildcard1.performIntersectionWith(wildcard2, wildcard1.fProcessContents); 1410 if (intersect == null || 1411 intersect.fType != XSWildcardDecl.NSCONSTRAINT_LIST || 1412 intersect.fNamespaceList.length != 0) { 1413 return true; 1414 } 1415 1416 return false; 1417 } 1418 1419 public static boolean overlapUPA(Object decl1, Object decl2, 1421 SubstitutionGroupHandler sgHandler) { 1422 if (decl1 instanceof XSElementDecl) { 1423 if (decl2 instanceof XSElementDecl) { 1424 return overlapUPA((XSElementDecl)decl1, 1425 (XSElementDecl)decl2, 1426 sgHandler); 1427 } else { 1428 return overlapUPA((XSElementDecl)decl1, 1429 (XSWildcardDecl)decl2, 1430 sgHandler); 1431 } 1432 } else { 1433 if (decl2 instanceof XSElementDecl) { 1434 return overlapUPA((XSElementDecl)decl2, 1435 (XSWildcardDecl)decl1, 1436 sgHandler); 1437 } else { 1438 return overlapUPA((XSWildcardDecl)decl1, 1439 (XSWildcardDecl)decl2); 1440 } 1441 } 1442 } 1443 1444} | Popular Tags |