1 57 package org.enhydra.apache.xerces.validators.common; 58 59 import java.util.Hashtable ; 60 61 import org.enhydra.apache.xerces.framework.XMLContentSpec; 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 public class AllContentModel implements XMLContentModel { 67 68 private QName fAllElements[] = new QName[10]; 69 private boolean fIsOptionalElement[] = new boolean[10]; 70 private boolean fHasOptionalContent = false; 71 private boolean fIsMixed = false; 72 73 private int fNumElements = 0; 74 private int fNumRequired = 0; 75 76 private Hashtable fElementsHash; 77 78 79 private SubstitutionGroupComparator fComparator = null; 80 81 82 private static final boolean DEBUG_VALIDATE_CONTENT = false; 83 84 87 public AllContentModel(boolean hasOptionalContent) { 88 fHasOptionalContent = hasOptionalContent; 89 90 if (DEBUG_VALIDATE_CONTENT) { 91 System.out.println("Entering AllContentModel#AllContentModel"); 92 System.out.println(" this == "+this); 93 System.out.println(" optionalContent == "+hasOptionalContent); 94 } 95 } 96 97 public AllContentModel(boolean hasOptionalContent, boolean isMixed) { 98 this(hasOptionalContent); 99 fIsMixed = isMixed; 100 101 if (DEBUG_VALIDATE_CONTENT) { 102 System.out.println(" mixed == "+fIsMixed); 103 } 104 } 105 106 void addElement(QName newElement, boolean isOptional) { 107 if (DEBUG_VALIDATE_CONTENT) { 108 System.out.println("Entering AllContentModel#addElement"); 109 } 110 111 if (fNumElements >= fAllElements.length) { 113 QName newAllElements[] = new QName[2*fAllElements.length]; 114 boolean newIsOptionalElements[] = 115 new boolean[2*fIsOptionalElement.length]; 116 117 System.arraycopy(fAllElements, 0, newAllElements, 0, 118 fAllElements.length); 119 System.arraycopy(fIsOptionalElement, 0, newIsOptionalElements, 0, 120 fIsOptionalElement.length); 121 122 fAllElements = newAllElements; 123 fIsOptionalElement = newIsOptionalElements; 124 } 125 126 fAllElements[fNumElements] = newElement; 128 fIsOptionalElement[fNumElements] = isOptional; 129 130 fNumElements++; 131 132 if (!isOptional) { 135 fNumRequired++; 136 } 137 138 if (DEBUG_VALIDATE_CONTENT) { 139 showAllElements(); 140 System.out.println("Leaving AllContentModel#addElement"); 141 } 142 } 143 144 public void checkUniqueParticleAttribution(SchemaGrammar gram) throws Exception { 146 for (int i = 0; i < fNumElements; i++) 148 fAllElements[i].uri = gram.getContentSpecOrgUri(fAllElements[i].uri); 149 150 for (int j = 0; j < fNumElements; j++) { 152 for (int k = j+1; k < fNumElements; k++) { 153 ElementWildcard.conflict(XMLContentSpec.CONTENTSPECNODE_LEAF, 154 fAllElements[j].localpart, 155 fAllElements[j].uri, 156 XMLContentSpec.CONTENTSPECNODE_LEAF, 157 fAllElements[k].localpart, 158 fAllElements[k].uri, 159 fComparator); 160 } 161 } 162 } 163 165 189 public int validateContent(QName children[], int offset, int length) 190 throws Exception { 191 192 if (DEBUG_VALIDATE_CONTENT) 193 System.out.println("Entering AllContentModel#validateContent"); 194 195 if (fHasOptionalContent && length == 0) { 198 if (DEBUG_VALIDATE_CONTENT) { 199 System.out.println("Empty content"); 200 System.out.println("Leaving AllContentModel#validateContent"); 201 } 202 return -1; 203 } 204 205 final int numElements = fNumElements; 206 207 if (fElementsHash == null) 208 createElementsHash(); 209 210 boolean elementSeen[] = new boolean[numElements]; 213 int numRequiredSeen = 0; 214 215 for (int childIndex = 0; childIndex < length; childIndex++) { 216 QName currChild = children[offset + childIndex]; 217 218 if (fIsMixed && currChild.localpart == -1) 220 continue; 221 222 Integer foundIdx = (Integer )fElementsHash.get(currChild); 223 224 if (foundIdx == null) { 228 if (DEBUG_VALIDATE_CONTENT) { 229 System.out.println("Unexpected element seen - idx == "+ 230 childIndex+" ("+currChild+")"); 231 System.out.println("Leaving AllContentModel#validateContent"); 232 } 233 234 return childIndex; 235 } 236 237 int foundIdxVal = foundIdx.intValue(); 238 239 if (elementSeen[foundIdxVal]) { 242 if (DEBUG_VALIDATE_CONTENT) { 243 System.out.println("Duplicate element seen - idx == "+ 244 childIndex+" ("+currChild+")"); 245 System.out.println("Leaving AllContentModel#validateContent"); 246 } 247 248 return childIndex; 249 } 250 251 elementSeen[foundIdxVal] = true; 252 253 if (!fIsOptionalElement[foundIdxVal]) { 254 numRequiredSeen++; 255 } 256 } 257 258 if (numRequiredSeen != fNumRequired) { 260 if (DEBUG_VALIDATE_CONTENT) { 261 System.out.println("Required element missing"); 262 System.out.println("Leaving AllContentModel#validateContent"); 263 } 264 265 return length; 266 } 267 268 if (DEBUG_VALIDATE_CONTENT) { 269 System.out.println("Successful validation"); 270 System.out.println("Leaving AllContentModel#validateContent"); 271 } 272 273 return -1; 274 } 275 276 299 public int validateContentSpecial(QName children[], int offset, int length) 300 throws Exception { 301 302 if (fComparator == null) 303 return validateContent(children, offset, length); 304 305 if (DEBUG_VALIDATE_CONTENT) 306 System.out.println("Entering AllContentModel#validateContentSpecial"); 307 308 if (fHasOptionalContent && length == 0) { 311 if (DEBUG_VALIDATE_CONTENT) { 312 System.out.println("Empty content"); 313 System.out.println("Leaving AllContentModel#validateContentSpecial"); 314 } 315 return -1; 316 } 317 318 final int numElements = fNumElements; 319 320 boolean elementSeen[] = new boolean[numElements]; 323 int numRequiredSeen = 0; 324 325 childLoop: for (int childIndex = 0; childIndex < length; childIndex++) { 326 QName currChild = children[offset + childIndex]; 327 328 if (fIsMixed && currChild.localpart == -1) 330 continue; 331 332 int compareIdx; 333 334 for (compareIdx = 0; compareIdx < numElements; compareIdx++) { 335 if (fComparator.isEquivalentTo(currChild, 336 fAllElements[compareIdx])) { 337 if (elementSeen[compareIdx]) { 340 if (DEBUG_VALIDATE_CONTENT) { 341 System.out.println("Duplicate element seen - idx == "+ 342 childIndex+" ("+currChild+")"); 343 System.out.println("Leaving AllContentModel#validateContentSpecial"); 344 } 345 346 return childIndex; 347 } 348 349 elementSeen[compareIdx] = true; 350 351 if (!fIsOptionalElement[compareIdx]) { 352 numRequiredSeen++; 353 } 354 355 continue childLoop; 357 } 358 } 359 360 if (DEBUG_VALIDATE_CONTENT) { 361 System.out.println("Unexpected element seen - idx == "+ 362 childIndex+" ("+currChild+")"); 363 System.out.println("Leaving AllContentModel#validateContentSpecial"); 364 } 365 366 return childIndex; 369 } 370 371 if (numRequiredSeen != fNumRequired) { 373 if (DEBUG_VALIDATE_CONTENT) { 374 System.out.println("Required element missing"); 375 System.out.println("Leaving AllContentModel#validateContentSpecial"); 376 } 377 378 return length; 379 } 380 381 if (DEBUG_VALIDATE_CONTENT) { 382 System.out.println("Successful validation"); 383 System.out.println("Leaving AllContentModel#validateContentSpecial"); 384 } 385 386 return -1; 387 } 388 389 396 public void setSubstitutionGroupComparator(SubstitutionGroupComparator comparator) { 397 fComparator = comparator; 398 } 399 400 428 public int whatCanGoHere(boolean fullyValid, 429 InsertableElementsInfo info) throws Exception { 430 431 if (DEBUG_VALIDATE_CONTENT) 432 System.out.println("Entering AllContentModel#whatCanGoHere"); 433 434 436 if (fElementsHash == null) 437 createElementsHash(); 438 439 final int numElements = fNumElements; 440 441 boolean elementSeen[] = new boolean[numElements]; 444 445 final int numChildren = info.curChildren.length; 446 final int insertAt = info.insertAt; 447 final QName curChildren[] = info.curChildren; 448 449 for (int childIndex = 0; childIndex < insertAt; childIndex++) { 450 QName currChild = curChildren[childIndex]; 451 452 Integer foundIdx = (Integer )fElementsHash.get(currChild); 453 454 if (foundIdx == null) 458 return childIndex; 459 460 int foundIdxVal = foundIdx.intValue(); 461 462 if (elementSeen[foundIdxVal]) 463 return childIndex; 464 465 elementSeen[foundIdxVal] = true; 466 } 467 468 info.canHoldPCData = fIsMixed; 469 470 final int resultsCount = numElements - insertAt; 471 info.resultsCount = resultsCount; 472 473 if ((info.results == null) || (info.results.length < resultsCount)) 478 info.results = new boolean[resultsCount]; 479 480 if ((info.possibleChildren == null) 481 || (info.possibleChildren.length < resultsCount)) 482 { 483 info.possibleChildren = new QName[resultsCount]; 484 485 QName possibleChildren[] = info.possibleChildren; 486 final int possibleChildrenLen = info.possibleChildren.length; 487 488 for (int i = 0; i < possibleChildrenLen; i++) { 489 possibleChildren[i] = new QName(); 490 } 491 } 492 493 int possibleChildIdx = 0; 494 495 for (int elemIdx = 0; elemIdx < numElements; elemIdx++) { 498 if (!elementSeen[elemIdx]) { 499 info.possibleChildren[possibleChildIdx]. 500 setValues(fAllElements[elemIdx]); 501 info.results[possibleChildIdx] = true; 502 possibleChildIdx++; 503 } 504 } 505 506 info.isValidEOC = (resultsCount == 0); 508 509 if (DEBUG_VALIDATE_CONTENT) 510 System.out.println("Leaving AllContentModel#whatCanGoHere"); 511 512 if (resultsCount == 0) 513 return -1; 514 515 return info.childCount; 516 } 518 519 public ContentLeafNameTypeVector getContentLeafNameTypeVector() { 520 if (DEBUG_VALIDATE_CONTENT) 521 System.out.println("Entering AllContentModel#getContentLeafNameTypeVector"); 522 523 if (DEBUG_VALIDATE_CONTENT) 524 System.out.println("Leaving AllContentModel#getContentLeafNameTypeVector"); 525 return null; 527 } 528 529 private void createElementsHash() { 530 int numElements = fNumElements; 531 fElementsHash = new Hashtable (numElements); 532 533 for (int elementIdx = 0; elementIdx < numElements; elementIdx++) { 534 fElementsHash.put(fAllElements[elementIdx], 537 new Integer (elementIdx)); 538 } 539 } 540 541 private void showAllElements() { 542 for (int elementIdx = 0; 543 elementIdx < fNumElements; 544 elementIdx++) { 545 System.out.print("fAllElements["+elementIdx+"] == " + 546 fAllElements[elementIdx].toString()); 547 548 if (fIsOptionalElement[elementIdx]) { 549 System.out.print(" (optional)"); 550 } 551 552 System.out.println(); 553 } 554 } 555 } 556 | Popular Tags |