1 57 58 package org.enhydra.apache.xerces.validators.dtd; 59 60 import org.enhydra.apache.xerces.dom.DocumentImpl; 61 import org.enhydra.apache.xerces.framework.XMLContentSpec; 62 import org.enhydra.apache.xerces.framework.XMLDTDScanner; 63 import org.enhydra.apache.xerces.utils.QName; 64 import org.enhydra.apache.xerces.utils.StringPool; 65 import org.enhydra.apache.xerces.validators.common.Grammar; 66 import org.enhydra.apache.xerces.validators.common.XMLAttributeDecl; 67 import org.enhydra.apache.xerces.validators.common.XMLElementDecl; 68 import org.w3c.dom.Document ; 69 import org.w3c.dom.Element ; 70 import org.w3c.dom.ProcessingInstruction ; 71 72 85 public class DTDGrammar 86 extends Grammar 87 implements XMLDTDScanner.EventHandler { 88 89 94 98 99 private static final int CHUNK_SHIFT = 8; 101 102 private static final int CHUNK_SIZE = (1 << CHUNK_SHIFT); 103 104 105 private static final int CHUNK_MASK = CHUNK_SIZE - 1; 106 107 108 private static final int INITIAL_CHUNK_COUNT = (1 << (10 - CHUNK_SHIFT)); 110 114 116 117 private StringPool fStringPool; 118 119 121 122 private XMLElementDecl fElementDecl = new XMLElementDecl(); 123 124 125 private XMLAttributeDecl fAttributeDecl = new XMLAttributeDecl(); 126 127 128 private XMLContentSpec fContentSpec = new XMLContentSpec(); 129 130 132 133 private Document fGrammarDocument; 134 135 136 private Element fRootElement; 137 138 private QName fRootElementQName = new QName(); 139 140 141 private Element fCurrentElement; 142 143 145 146 private int fElementDeclIsExternal[][] = new int[INITIAL_CHUNK_COUNT][]; 147 148 private int fElementDeclMap[][] = new int[INITIAL_CHUNK_COUNT][]; 149 150 151 private int fAttributeDeclIsExternal[][] = new int[INITIAL_CHUNK_COUNT][]; 152 153 private int fAttributeDeclMap[][] = new int[INITIAL_CHUNK_COUNT][]; 154 155 156 private int fContentSpecMap[][] = new int[INITIAL_CHUNK_COUNT][]; 157 158 160 private QName fQName = new QName(); 161 162 166 167 public DTDGrammar(StringPool stringPool) { 168 reset(stringPool); 169 } 170 171 175 176 public void reset(StringPool stringPool) { 177 fStringPool = stringPool; 178 } 179 180 184 185 public void callStartDTD() throws Exception { 186 187 setGrammarDocument(null); 189 fGrammarDocument = new DocumentImpl(); 190 fRootElement = fGrammarDocument.createElement("dtd"); 191 fCurrentElement = fRootElement; 192 193 } 195 196 public void callEndDTD() throws Exception { 197 198 setGrammarDocument(fGrammarDocument); 200 201 } 203 210 public void callTextDecl(int version, int encoding) throws Exception { 211 212 Element textDecl = fGrammarDocument.createElement("textDecl"); 214 textDecl.setAttribute("version", fStringPool.toString(version)); 215 textDecl.setAttribute("encoding", fStringPool.toString(encoding)); 216 fCurrentElement.appendChild(textDecl); 217 218 } 220 228 public void doctypeDecl(QName rootElement, int publicId, int systemId) 229 throws Exception { 230 231 Element doctypeDecl = fGrammarDocument.createElement("doctypeDecl"); 233 doctypeDecl.setAttribute("name", fStringPool.toString(rootElement.rawname)); 234 if (rootElement.uri != StringPool.EMPTY_STRING) { 235 doctypeDecl.setAttribute("xmlns:"+fStringPool.toString(rootElement.prefix), 236 fStringPool.toString(rootElement.uri)); 237 } 238 doctypeDecl.setAttribute("publicId", fStringPool.toString(publicId)); 239 doctypeDecl.setAttribute("systemId", fStringPool.toString(systemId)); 240 fCurrentElement.appendChild(doctypeDecl); 241 242 fRootElementQName.setValues(rootElement); 244 245 } 247 254 public void startReadingFromExternalSubset(int publicId, int systemId) 255 throws Exception { 256 257 Element externalSubset = fGrammarDocument.createElement("external"); 259 externalSubset.setAttribute("publicId", fStringPool.toString(publicId)); 260 externalSubset.setAttribute("systemId", fStringPool.toString(systemId)); 261 fCurrentElement.appendChild(externalSubset); 262 fCurrentElement = externalSubset; 263 264 } 266 271 public void stopReadingFromExternalSubset() throws Exception { 272 273 fCurrentElement = (Element )fCurrentElement.getParentNode(); 275 276 } 278 285 public int addElementDecl(QName elementDecl) throws Exception { 286 287 Element elementDeclElement = fGrammarDocument.createElement("elementDecl"); 289 elementDeclElement.setAttribute("name", fStringPool.toString(elementDecl.localpart)); 290 if (elementDecl.uri != StringPool.EMPTY_STRING) { 291 elementDeclElement.setAttribute("xmlns:"+fStringPool.toString(elementDecl.prefix), 292 fStringPool.toString(elementDecl.uri)); 293 } 294 fCurrentElement.appendChild(elementDeclElement); 295 296 int elementDeclIndex = createElementDecl(); 298 299 fElementDecl.clear(); 301 fElementDecl.name.setValues(elementDecl); 302 setElementDecl(elementDeclIndex, fElementDecl); 303 304 return elementDeclIndex; 306 307 } 309 318 public int addElementDecl(QName elementDecl, 319 int contentSpecType, 320 int contentSpec, 321 boolean isExternal) throws Exception { 322 323 Element elementDeclElement = fGrammarDocument.createElement("elementDecl"); 325 elementDeclElement.setAttribute("name", fStringPool.toString(elementDecl.localpart)); 326 if (elementDecl.uri != StringPool.EMPTY_STRING) { 327 elementDeclElement.setAttribute("xmlns:"+fStringPool.toString(elementDecl.prefix), 328 fStringPool.toString(elementDecl.uri)); 329 } 330 elementDeclElement.setAttribute("type", fStringPool.toString(contentSpecType)); 331 fCurrentElement.appendChild(elementDeclElement); 334 335 int elementDeclIndex = createElementDecl(); 337 338 fElementDecl.clear(); 340 fElementDecl.name.setValues(elementDecl); 341 fElementDecl.type = contentSpecType; 342 fElementDecl.contentSpecIndex = contentSpec; 343 setElementDecl(elementDeclIndex, fElementDecl); 344 345 int chunk = elementDeclIndex >> CHUNK_SHIFT; 346 int index = elementDeclIndex & CHUNK_MASK; 347 ensureElementDeclCapacity(chunk); 348 fElementDeclIsExternal[chunk][index] = isExternal? 1 : 0; 349 350 return elementDeclIndex; 352 353 } 355 protected void putElementNameMapping(QName name, int scope, 356 int elementDeclIndex) { 357 fQName.uri = StringPool.EMPTY_STRING; 358 fQName.localpart = name.rawname; 359 super.putElementNameMapping(fQName, scope, elementDeclIndex); 360 } 361 362 373 374 public int getElementDeclIndex(QName element, int scopeIndex) { 375 return super.getElementDeclIndex(element.rawname, -1); 377 } 378 379 public void setElementDeclDTD(int elementDeclIndex, XMLElementDecl elementDecl) { 380 super.setElementDecl(elementDeclIndex, elementDecl); 381 } 382 383 private XMLContentSpec fTempContentSpec = new XMLContentSpec(); 384 385 391 392 public void setElementDeclIsExternal(int elementDeclIndex, boolean isExternal) { 393 int chunk = elementDeclIndex >> CHUNK_SHIFT; 394 int index = elementDeclIndex & CHUNK_MASK; 395 ensureElementDeclCapacity(chunk); 396 fElementDeclIsExternal[chunk][index] = isExternal? 1 : 0; 397 } 398 399 public boolean getElementDeclIsExternal(int elementDeclIndex) { 401 if (elementDeclIndex < 0) { 402 return false; 403 } 404 int chunk = elementDeclIndex >> CHUNK_SHIFT; 405 int index = elementDeclIndex & CHUNK_MASK; 406 return (fElementDeclIsExternal[chunk][index] != 0); 407 } 408 409 public boolean getAttributeDeclIsExternal(int attributeDeclIndex) { 410 if (attributeDeclIndex < 0) { 411 return false; 412 } 413 int chunk = attributeDeclIndex >> CHUNK_SHIFT; 414 int index = attributeDeclIndex & CHUNK_MASK; 415 return (fAttributeDeclIsExternal[chunk][index] != 0); 416 } 417 418 public boolean getRootElementQName(QName root) { 419 if (fRootElementQName.rawname == -1) { 420 return false; 421 } 422 root.setValues(fRootElementQName); 423 return true; 424 } 425 426 438 public int addAttDef(QName elementDecl, QName attributeDecl, 439 int attType, boolean attList, int enumeration, 440 int attDefaultType, int attDefaultValue, boolean isExternal) 441 throws Exception { 442 448 449 Element attributeDeclElement = fGrammarDocument.createElement("attributeDecl"); 451 attributeDeclElement.setAttribute("element", fStringPool.toString(elementDecl.localpart)); 452 attributeDeclElement.setAttribute("name", fStringPool.toString(attributeDecl.localpart)); 453 if (attributeDecl.uri != StringPool.EMPTY_STRING) { 454 attributeDeclElement.setAttribute("xmlns:"+fStringPool.toString(attributeDecl.prefix), 455 fStringPool.toString(attributeDecl.uri)); 456 } 457 attributeDeclElement.setAttribute("type", fStringPool.toString(attType)); 458 attributeDeclElement.setAttribute("defaultType", fStringPool.toString(attDefaultType)); 461 attributeDeclElement.setAttribute("defaultValue", fStringPool.toString(attDefaultValue)); 462 fCurrentElement.appendChild(attributeDeclElement); 463 464 int attributeDeclIndex = createAttributeDecl(); 466 467 String attTypeString = ""; 469 switch (attType) { 470 case XMLAttributeDecl.TYPE_CDATA: 471 attTypeString = "string"; 472 case XMLAttributeDecl.TYPE_ENTITY: 473 attTypeString = "ENTITY";; 474 case XMLAttributeDecl.TYPE_ENUMERATION: 475 attTypeString = "ENUMERATION";; 476 case XMLAttributeDecl.TYPE_ID: 477 attTypeString = "ID";; 478 case XMLAttributeDecl.TYPE_IDREF: 479 attTypeString = "IDREF";; 480 case XMLAttributeDecl.TYPE_NMTOKEN: 481 attTypeString = "NMTOKEN";; 482 case XMLAttributeDecl.TYPE_NOTATION: 483 attTypeString = "NOTATION";; 484 default: 485 ; 486 } 487 488 fAttributeDecl.clear(); 490 fAttributeDecl.name.setValues(attributeDecl); 491 fAttributeDecl.type = attType; 492 fAttributeDecl.list = attList; 493 fAttributeDecl.enumeration = enumeration; 494 498 fAttributeDecl.defaultType = attDefaultType; 499 fAttributeDecl.defaultValue = fStringPool.toString(attDefaultValue); 500 501 int elementDeclIndex = getElementDeclIndex(elementDecl, -1); 502 setAttributeDecl(elementDeclIndex, attributeDeclIndex, fAttributeDecl); 503 504 int chunk = attributeDeclIndex >> CHUNK_SHIFT; 505 int index = attributeDeclIndex & CHUNK_MASK; 506 ensureAttributeDeclCapacity(chunk); 507 fAttributeDeclIsExternal[chunk][index] = isExternal ? 1 : 0; 508 509 return attributeDeclIndex; 511 512 } 514 521 public int addUniqueLeafNode(int nameIndex) throws Exception { 522 523 int contentSpecIndex = createContentSpec(); 525 526 fContentSpec.setValues(XMLContentSpec.CONTENTSPECNODE_LEAF, 528 nameIndex, -1); 529 setContentSpec(contentSpecIndex, fContentSpec); 530 531 return contentSpecIndex; 533 534 } 536 544 public int addContentSpecNode(int nodeType, 545 int nodeValue) throws Exception { 546 547 int contentSpecIndex = createContentSpec(); 549 550 fContentSpec.setValues(nodeType, nodeValue, -1); 552 setContentSpec(contentSpecIndex, fContentSpec); 553 554 return contentSpecIndex; 556 557 } 559 568 public int addContentSpecNode(int nodeType, 569 int leftNodeIndex, 570 int rightNodeIndex) throws Exception { 571 572 int contentSpecIndex = createContentSpec(); 574 575 fContentSpec.setValues(nodeType, 577 leftNodeIndex, rightNodeIndex); 578 setContentSpec(contentSpecIndex, fContentSpec); 579 580 return contentSpecIndex; 582 583 } 585 592 public String getContentSpecNodeAsString(int nodeIndex) throws Exception { 593 return XMLContentSpec.toString(this, fStringPool, nodeIndex); 594 } 595 596 603 public boolean startEntityDecl(boolean isPE, int entityName) 604 throws Exception { 605 606 Element entityDecl = fGrammarDocument.createElement("entityDecl"); 608 entityDecl.setAttribute("name", fStringPool.toString(entityName)); 609 entityDecl.setAttribute("parameter", isPE ? "true" : "false"); 610 fCurrentElement.appendChild(entityDecl); 611 fCurrentElement = entityDecl; 612 613 return true; 615 616 } 618 622 public void endEntityDecl() throws Exception { 623 624 fCurrentElement = (Element )fCurrentElement.getParentNode(); 626 627 } 629 637 public int addInternalPEDecl(int name, int value) throws Exception { 638 639 Element internalPEDecl = fGrammarDocument.createElement("internalPEDecl"); 641 internalPEDecl.setAttribute("name", fStringPool.toString(name)); 642 internalPEDecl.setAttribute("value", fStringPool.toString(value)); 643 fCurrentElement.appendChild(internalPEDecl); 644 645 int peDeclIndex = -1; 647 648 return peDeclIndex; 650 651 } 653 662 public int addExternalPEDecl(int name, 663 int publicId, 664 int systemId) throws Exception { 665 666 Element externalPEDecl = fGrammarDocument.createElement("externalPEDecl"); 668 externalPEDecl.setAttribute("name", fStringPool.toString(name)); 669 externalPEDecl.setAttribute("publicId", fStringPool.toString(publicId)); 670 externalPEDecl.setAttribute("systemId", fStringPool.toString(systemId)); 671 fCurrentElement.appendChild(externalPEDecl); 672 673 int peDeclIndex = -1; 675 676 return peDeclIndex; 678 679 } 681 689 public int addInternalEntityDecl(int name, int value) throws Exception { 690 691 Element internalEntityDecl = fGrammarDocument.createElement("internalEntityDecl"); 693 internalEntityDecl.setAttribute("name", fStringPool.toString(name)); 694 internalEntityDecl.setAttribute("value", fStringPool.toString(value)); 695 fCurrentElement.appendChild(internalEntityDecl); 696 697 int internalEntityDeclIndex = -1; 699 700 return internalEntityDeclIndex; 702 703 } 705 714 public int addExternalEntityDecl(int name, 715 int publicId, 716 int systemId) throws Exception { 717 718 Element externalEntityDecl = fGrammarDocument.createElement("externalEntityDecl"); 720 externalEntityDecl.setAttribute("name", fStringPool.toString(name)); 721 externalEntityDecl.setAttribute("publicId", fStringPool.toString(publicId)); 722 externalEntityDecl.setAttribute("systemId", fStringPool.toString(systemId)); 723 fCurrentElement.appendChild(externalEntityDecl); 724 725 int externalEntityDeclIndex = -1; 727 728 return externalEntityDeclIndex; 730 731 } 733 743 public int addUnparsedEntityDecl(int name, 744 int publicId, int systemId, 745 int notationName) throws Exception { 746 747 Element unparsedEntityDecl = fGrammarDocument.createElement("unparsedEntityDecl"); 749 unparsedEntityDecl.setAttribute("name", fStringPool.toString(name)); 750 unparsedEntityDecl.setAttribute("publicId", fStringPool.toString(publicId)); 751 unparsedEntityDecl.setAttribute("systemId", fStringPool.toString(systemId)); 752 unparsedEntityDecl.setAttribute("notation", fStringPool.toString(notationName)); 753 fCurrentElement.appendChild(unparsedEntityDecl); 754 755 int unparsedEntityDeclIndex = -1; 757 758 return unparsedEntityDeclIndex; 760 761 } 763 768 public int startEnumeration() throws Exception { 769 770 Element enumeration = fGrammarDocument.createElement("enumeration"); 772 fCurrentElement.appendChild(enumeration); 773 fCurrentElement = enumeration; 774 775 int enumIndex = fStringPool.startStringList(); 778 779 return enumIndex; 781 782 } 784 793 public void addNameToEnumeration(int enumIndex, 794 int elementType, int attrName, 795 int nameIndex, 796 boolean isNotationType) throws Exception { 797 798 Element literal = fGrammarDocument.createElement("literal"); 800 literal.setAttribute("element", fStringPool.toString(elementType)); 804 literal.setAttribute("attribute", fStringPool.toString(attrName)); 805 literal.setAttribute("name", fStringPool.toString(nameIndex)); 806 literal.setAttribute("notation", isNotationType ? "true" : "false"); 807 fCurrentElement.appendChild(literal); 808 809 fStringPool.addStringToList(enumIndex, nameIndex); 811 812 } 814 820 public void endEnumeration(int enumIndex) throws Exception { 821 822 fCurrentElement = (Element )fCurrentElement.getParentNode(); 824 825 fStringPool.finishStringList(enumIndex); 827 828 } 830 839 public int addNotationDecl(int notationName, 840 int publicId, int systemId) throws Exception { 841 842 Element notationDecl = fGrammarDocument.createElement("notationDecl"); 844 notationDecl.setAttribute("name", fStringPool.toString(notationName)); 845 notationDecl.setAttribute("publicId", fStringPool.toString(publicId)); 846 notationDecl.setAttribute("systemId", fStringPool.toString(systemId)); 847 fCurrentElement.appendChild(notationDecl); 848 849 int notationDeclIndex = -1; 851 852 return notationDeclIndex; 854 855 } 857 863 public void callComment(int data) throws Exception { 864 } 865 866 872 public void callProcessingInstruction(int piTarget, int piData) 873 throws Exception { 874 875 ProcessingInstruction pi = 877 fGrammarDocument.createProcessingInstruction(fStringPool.toString(piTarget), 878 fStringPool.toString(piData)); 879 fCurrentElement.appendChild(pi); 880 881 } 883 885 889 public void internalSubset(int internalSubset) throws Exception { 890 } 891 892 protected boolean isDTD() { 893 return true; 894 } 895 896 900 902 903 private void ensureElementDeclCapacity(int chunk) { 904 if (chunk >= fElementDeclMap.length) { 905 fElementDeclMap = resize(fElementDeclMap, 906 fElementDeclMap.length * 2); 907 fElementDeclIsExternal = resize(fElementDeclIsExternal, 908 fElementDeclIsExternal.length * 2); 909 } else if (fElementDeclMap[chunk] != null) { 910 return; 911 } 912 fElementDeclMap[chunk] = new int[CHUNK_SIZE]; 913 fElementDeclIsExternal[chunk] = new int[CHUNK_SIZE]; 914 } 915 916 917 private void ensureAttributeDeclCapacity(int chunk) { 918 if (chunk >= fAttributeDeclMap.length) { 919 fAttributeDeclMap = resize(fAttributeDeclMap, 920 fAttributeDeclMap.length * 2); 921 fAttributeDeclIsExternal = resize(fAttributeDeclIsExternal, 922 fAttributeDeclIsExternal.length * 2); 923 } else if (fAttributeDeclMap[chunk] != null) { 924 return; 925 } 926 fAttributeDeclMap[chunk] = new int[CHUNK_SIZE]; 927 fAttributeDeclIsExternal[chunk] = new int[CHUNK_SIZE]; 928 } 929 930 931 private void ensureContentSpecCapacity(int chunk) { 932 if (chunk >= fContentSpecMap.length) { 933 fContentSpecMap = resize(fContentSpecMap, 934 fContentSpecMap.length * 2); 935 } else if (fContentSpecMap[chunk] != null) { 936 return; 937 } 938 fContentSpecMap[chunk] = new int[CHUNK_SIZE]; 939 } 940 941 943 944 private int[][] resize(int array[][], int newsize) { 945 int newarray[][] = new int[newsize][]; 946 System.arraycopy(array, 0, newarray, 0, array.length); 947 return newarray; 948 } 949 950 } | Popular Tags |