1 57 58 package org.enhydra.apache.xerces.validators.common; 59 60 import org.enhydra.apache.xerces.framework.XMLContentSpec; 61 import org.enhydra.apache.xerces.utils.ImplementationMessages; 62 import org.enhydra.apache.xerces.utils.QName; 63 import org.enhydra.apache.xerces.validators.schema.SchemaGrammar; 64 import org.enhydra.apache.xerces.validators.schema.SubstitutionGroupComparator; 65 66 88 public class SimpleContentModel 89 implements XMLContentModel { 90 91 95 100 private QName fFirstChild = new QName(); 101 102 107 private QName fSecondChild = new QName(); 108 109 115 private int fOp; 116 117 118 private boolean fDTD; 119 120 121 private SubstitutionGroupComparator comparator = null; 122 123 127 136 public SimpleContentModel(QName firstChild, QName secondChild, int cmOp) { 137 this(firstChild, secondChild, cmOp, false); 138 } 139 140 149 public SimpleContentModel(QName firstChild, QName secondChild, 150 int cmOp, boolean dtd) { 151 fFirstChild.setValues(firstChild); 158 if (secondChild != null) { 159 fSecondChild.setValues(secondChild); 160 } 161 else { 162 fSecondChild.clear(); 163 } 164 fOp = cmOp; 165 fDTD = dtd; 166 } 167 168 public void checkUniqueParticleAttribution(SchemaGrammar gram) throws Exception { 170 fFirstChild.uri = gram.getContentSpecOrgUri(fFirstChild.uri); 172 fSecondChild.uri = gram.getContentSpecOrgUri(fSecondChild.uri); 173 174 if (fOp == XMLContentSpec.CONTENTSPECNODE_CHOICE && 176 ElementWildcard.conflict(XMLContentSpec.CONTENTSPECNODE_LEAF, 177 fFirstChild.localpart, fFirstChild.uri, 178 XMLContentSpec.CONTENTSPECNODE_LEAF, 179 fSecondChild.localpart, fSecondChild.uri, 180 comparator)) { 181 } 182 } 183 185 189 213 public int validateContent(QName children[], int offset, int length) throws Exception { 214 215 switch(fOp) 220 { 221 case XMLContentSpec.CONTENTSPECNODE_LEAF : 222 if (length == 0) 224 return 0; 225 226 if (fDTD) { 228 if (children[offset].rawname != fFirstChild.rawname) { 229 return 0; 230 } 231 } 232 else { 233 if (children[offset].uri != fFirstChild.uri || 234 children[offset].localpart != fFirstChild.localpart) 235 return 0; 236 } 237 238 if (length > 1) 240 return 1; 241 break; 242 243 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE : 244 if (length == 1) { 249 if (fDTD) { 250 if (children[offset].rawname != fFirstChild.rawname) { 251 return 0; 252 } 253 } 254 else { 255 if (children[offset].uri != fFirstChild.uri || 256 children[offset].localpart != fFirstChild.localpart) 257 return 0; 258 } 259 } 260 261 if (length > 1) 266 return 1; 267 break; 268 269 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE : 270 if (length > 0) 277 { 278 if (fDTD) { 279 for (int index = 0; index < length; index++) { 280 if (children[offset + index].rawname != fFirstChild.rawname) { 281 return index; 282 } 283 } 284 } 285 else { 286 for (int index = 0; index < length; index++) 287 { 288 if (children[offset + index].uri != fFirstChild.uri || 289 children[offset + index].localpart != fFirstChild.localpart) 290 return index; 291 } 292 } 293 } 294 break; 295 296 case XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE : 297 if (length == 0) 302 return 0; 303 304 if (fDTD) { 310 for (int index = 0; index < length; index++) { 311 if (children[offset + index].rawname != fFirstChild.rawname) { 312 return index; 313 } 314 } 315 } 316 else { 317 for (int index = 0; index < length; index++) 318 { 319 if (children[offset + index].uri != fFirstChild.uri || 320 children[offset + index].localpart != fFirstChild.localpart) 321 return index; 322 } 323 } 324 break; 325 326 case XMLContentSpec.CONTENTSPECNODE_CHOICE : 327 if (length == 0) 332 return 0; 333 334 if (fDTD) { 336 if ((children[offset].rawname != fFirstChild.rawname) && 337 (children[offset].rawname != fSecondChild.rawname)) { 338 return 0; 339 } 340 } 341 else { 342 if ((children[offset].uri != fFirstChild.uri || children[offset].localpart != fFirstChild.localpart) && 343 (children[offset].uri != fSecondChild.uri || children[offset].localpart != fSecondChild.localpart)) 344 return 0; 345 } 346 347 if (length > 1) 349 return 1; 350 break; 351 352 case XMLContentSpec.CONTENTSPECNODE_SEQ : 353 if (length == 2) { 358 if (fDTD) { 359 if (children[offset].rawname != fFirstChild.rawname) { 360 return 0; 361 } 362 if (children[offset + 1].rawname != fSecondChild.rawname) { 363 return 1; 364 } 365 } 366 else { 367 if (children[offset].uri != fFirstChild.uri || children[offset].localpart != fFirstChild.localpart) 368 return 0; 369 370 if (children[offset + 1].uri != fSecondChild.uri || children[offset + 1].localpart != fSecondChild.localpart) 371 return 1; 372 } 373 } 374 else { 375 if (length > 2) { 376 return 2; 377 } 378 379 return length; 380 } 381 382 break; 383 384 default : 385 throw new CMException(ImplementationMessages.VAL_CST); 386 } 387 388 return -1; 390 } 391 392 public int validateContentSpecial(QName children[], int offset, int length) throws Exception { 393 394 if (comparator==null) { 395 return validateContent(children,offset, length); 396 } 397 switch(fOp) 402 { 403 case XMLContentSpec.CONTENTSPECNODE_LEAF : 404 if (length == 0) 406 return 0; 407 408 if (children[offset].uri != fFirstChild.uri || 410 children[offset].localpart != fFirstChild.localpart) 411 if (!comparator.isEquivalentTo(children[offset], fFirstChild)) 412 return 0; 413 414 if (length > 1) 416 return 1; 417 break; 418 419 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE : 420 if (length == 1 && 425 (children[offset].uri != fFirstChild.uri || 426 children[offset].localpart != fFirstChild.localpart)) 427 if (!comparator.isEquivalentTo(children[offset], fFirstChild)) 428 return 0; 429 430 if (length > 1) 435 return 1; 436 break; 437 438 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE : 439 if (length > 0) 446 { 447 for (int index = 0; index < length; index++) 448 { 449 if (children[offset + index].uri != fFirstChild.uri || 450 children[offset + index].localpart != fFirstChild.localpart) 451 if (!comparator.isEquivalentTo(children[offset+index], fFirstChild)) 452 return index; 453 } 454 } 455 break; 456 457 case XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE : 458 if (length == 0) 463 return 0; 464 465 for (int index = 0; index < length; index++) 471 { 472 if (children[offset + index].uri != fFirstChild.uri || 473 children[offset + index].localpart != fFirstChild.localpart) 474 if (!comparator.isEquivalentTo(children[offset+index], fFirstChild)) 475 return index; 476 } 477 break; 478 479 case XMLContentSpec.CONTENTSPECNODE_CHOICE : 480 if (length == 0) 485 return 0; 486 487 if ((children[offset].uri != fFirstChild.uri || children[offset].localpart != fFirstChild.localpart) && 489 (children[offset].uri != fSecondChild.uri || children[offset].localpart != fSecondChild.localpart)) 490 if ( !comparator.isEquivalentTo(children[offset], fFirstChild) 491 && !comparator.isEquivalentTo(children[offset], fSecondChild) ) 492 return 0; 493 494 if (length > 1) 496 return 1; 497 break; 498 499 case XMLContentSpec.CONTENTSPECNODE_SEQ : 500 if (length == 2) { 505 if (children[offset].uri != fFirstChild.uri || children[offset].localpart != fFirstChild.localpart) 506 if (!comparator.isEquivalentTo(children[offset], fFirstChild)) 507 return 0; 508 509 if (children[offset + 1].uri != fSecondChild.uri || children[offset + 1].localpart != fSecondChild.localpart) 510 if (!comparator.isEquivalentTo(children[offset+1], fSecondChild)) 511 return 1; 512 } 513 else { 514 if (length > 2) { 515 return 2; 516 } 517 518 return length; 519 } 520 521 break; 522 523 default : 524 throw new CMException(ImplementationMessages.VAL_CST); 525 } 526 527 return -1; 529 } 530 531 public void setSubstitutionGroupComparator(SubstitutionGroupComparator comparator) { 532 this.comparator = comparator; 533 } 534 535 563 public int whatCanGoHere(boolean fullyValid, InsertableElementsInfo info) 564 throws Exception { 565 566 for (int index = info.insertAt; index < info.childCount-1; index++) { 572 info.curChildren[index].setValues(info.curChildren[index+1]); 573 } 574 info.childCount--; 575 576 final int failedIndex = validateContent(info.curChildren, 0, info.childCount); 581 if ((failedIndex != -1) && (failedIndex < info.insertAt)) 582 return failedIndex; 583 584 info.canHoldPCData = false; 586 587 if ((fOp == XMLContentSpec.CONTENTSPECNODE_LEAF) 589 || (fOp == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE) 590 || (fOp == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE) 591 || (fOp == XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE)) 592 { 593 info.resultsCount = 1; 594 } 595 else if ((fOp == XMLContentSpec.CONTENTSPECNODE_CHOICE) 596 || (fOp == XMLContentSpec.CONTENTSPECNODE_SEQ)) 597 { 598 info.resultsCount = 2; 599 } 600 else 601 { 602 throw new CMException(ImplementationMessages.VAL_CST); 603 } 604 605 if ((info.results == null) || (info.results.length < info.resultsCount)) 610 info.results = new boolean[info.resultsCount]; 611 612 if ((info.possibleChildren == null) 613 || (info.possibleChildren.length < info.resultsCount)) 614 { 615 info.possibleChildren = new QName[info.resultsCount]; 616 for (int i = 0; i < info.possibleChildren.length; i++) { 617 info.possibleChildren[i] = new QName(); 618 } 619 } 620 621 info.possibleChildren[0].setValues(fFirstChild); 626 info.results[0] = false; 627 if (info.resultsCount == 2) 628 { 629 info.possibleChildren[1].setValues(fSecondChild); 630 info.results[1] = false; 631 } 632 633 info.isValidEOC = false; 638 639 switch(fOp) 648 { 649 case XMLContentSpec.CONTENTSPECNODE_LEAF : 650 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE : 651 if (info.childCount == 0) 656 { 657 info.results[0] = true; 658 } 659 else if (info.childCount > 0) 660 { 661 if (!fullyValid && (info.insertAt == 0)) 667 info.results[0] = true; 668 } 669 670 if (fOp == XMLContentSpec.CONTENTSPECNODE_LEAF) 671 { 672 if (info.insertAt == 0) 674 info.isValidEOC = true; 675 } 676 else 677 { 678 info.isValidEOC = true; 680 } 681 break; 682 683 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE : 684 case XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE : 685 info.results[0] = true; 692 693 if ((fOp == XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE) 698 || (info.insertAt > 0)) 699 { 700 info.isValidEOC = true; 701 } 702 break; 703 704 case XMLContentSpec.CONTENTSPECNODE_CHOICE : 705 if (info.insertAt == 0) 711 { 712 if (!fullyValid && (info.childCount == 0)) 713 { 714 info.results[0] = true; 715 info.results[1] = true; 716 } 717 } 718 719 if (info.insertAt == 1) 721 info.isValidEOC = true; 722 break; 723 724 case XMLContentSpec.CONTENTSPECNODE_SEQ : 725 if (info.insertAt == 0) 730 { 731 if (fullyValid) 739 { 740 if (info.childCount == 1) 741 info.results[0] = info.curChildren[0].uri == fSecondChild.uri && 742 info.curChildren[0].localpart == fSecondChild.localpart; 743 } 744 else 745 { 746 info.results[0] = true; 747 } 748 } 749 else if (info.insertAt == 1) 750 { 751 if (!fullyValid || (info.childCount == 1)) 753 info.results[1] = true; 754 } 755 756 if (info.insertAt == 2) 758 info.isValidEOC = true; 759 break; 760 761 default : 762 throw new CMException(ImplementationMessages.VAL_CST); 763 } 764 765 return -1; 767 } 768 769 public ContentLeafNameTypeVector getContentLeafNameTypeVector() { 770 return null; 771 } 772 } | Popular Tags |