1 57 58 package com.sun.org.apache.xerces.internal.parsers; 59 60 import java.util.Locale ; 61 import java.util.Stack ; 62 63 import com.sun.org.apache.xerces.internal.dom.AttrImpl; 64 import com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl; 65 import com.sun.org.apache.xerces.internal.dom.DOMErrorImpl; 66 import com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl; 67 import com.sun.org.apache.xerces.internal.dom.DocumentImpl; 68 import com.sun.org.apache.xerces.internal.dom.DocumentTypeImpl; 69 import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter; 70 import com.sun.org.apache.xerces.internal.dom.ElementDefinitionImpl; 71 import com.sun.org.apache.xerces.internal.dom.ElementImpl; 72 import com.sun.org.apache.xerces.internal.dom.EntityImpl; 73 import com.sun.org.apache.xerces.internal.dom.EntityReferenceImpl; 74 import com.sun.org.apache.xerces.internal.dom.NodeImpl; 75 import com.sun.org.apache.xerces.internal.dom.NotationImpl; 76 import com.sun.org.apache.xerces.internal.dom.PSVIAttrNSImpl; 77 import com.sun.org.apache.xerces.internal.dom.PSVIDocumentImpl; 78 import com.sun.org.apache.xerces.internal.dom.PSVIElementNSImpl; 79 import com.sun.org.apache.xerces.internal.dom.TextImpl; 80 import com.sun.org.apache.xerces.internal.impl.Constants; 81 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition; 82 import com.sun.org.apache.xerces.internal.util.DOMErrorHandlerWrapper; 83 import com.sun.org.apache.xerces.internal.util.TypeInfoImpl; 84 import com.sun.org.apache.xerces.internal.xni.Augmentations; 85 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 86 import com.sun.org.apache.xerces.internal.xni.QName; 87 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 88 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 89 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 90 import com.sun.org.apache.xerces.internal.xni.XMLString; 91 import com.sun.org.apache.xerces.internal.xni.XNIException; 92 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration; 93 import com.sun.org.apache.xerces.internal.xs.AttributePSVI; 94 import com.sun.org.apache.xerces.internal.xs.ElementPSVI; 95 import org.w3c.dom.Attr ; 96 import org.w3c.dom.CDATASection ; 97 import org.w3c.dom.Comment ; 98 import org.w3c.dom.DOMError ; 99 import org.w3c.dom.Document ; 100 import org.w3c.dom.DocumentType ; 101 import org.w3c.dom.Element ; 102 import org.w3c.dom.EntityReference ; 103 import org.w3c.dom.NamedNodeMap ; 104 import org.w3c.dom.Node ; 105 import org.w3c.dom.NodeList ; 106 import org.w3c.dom.ProcessingInstruction ; 107 import org.w3c.dom.Text ; 108 import org.w3c.dom.TypeInfo ; 109 import org.w3c.dom.ls.LSParserFilter ; 110 import org.w3c.dom.traversal.NodeFilter; 111 112 125 public class AbstractDOMParser extends AbstractXMLDocumentParser { 126 127 132 133 protected static final String NAMESPACES = 134 Constants.SAX_FEATURE_PREFIX+Constants.NAMESPACES_FEATURE; 135 136 137 protected static final String CREATE_ENTITY_REF_NODES = 138 Constants.XERCES_FEATURE_PREFIX + Constants.CREATE_ENTITY_REF_NODES_FEATURE; 139 140 141 protected static final String INCLUDE_COMMENTS_FEATURE = 142 Constants.XERCES_FEATURE_PREFIX + Constants.INCLUDE_COMMENTS_FEATURE; 143 144 145 protected static final String CREATE_CDATA_NODES_FEATURE = 146 Constants.XERCES_FEATURE_PREFIX + Constants.CREATE_CDATA_NODES_FEATURE; 147 148 149 protected static final String INCLUDE_IGNORABLE_WHITESPACE = 150 Constants.XERCES_FEATURE_PREFIX + Constants.INCLUDE_IGNORABLE_WHITESPACE; 151 152 153 protected static final String DEFER_NODE_EXPANSION = 154 Constants.XERCES_FEATURE_PREFIX + Constants.DEFER_NODE_EXPANSION_FEATURE; 155 156 157 158 private static final String [] RECOGNIZED_FEATURES = { 159 NAMESPACES, 160 CREATE_ENTITY_REF_NODES, 161 INCLUDE_COMMENTS_FEATURE, 162 CREATE_CDATA_NODES_FEATURE, 163 INCLUDE_IGNORABLE_WHITESPACE, 164 DEFER_NODE_EXPANSION 165 }; 166 167 169 170 protected static final String DOCUMENT_CLASS_NAME = 171 Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_CLASS_NAME_PROPERTY; 172 173 protected static final String CURRENT_ELEMENT_NODE= 174 Constants.XERCES_PROPERTY_PREFIX + Constants.CURRENT_ELEMENT_NODE_PROPERTY; 175 176 179 180 private static final String [] RECOGNIZED_PROPERTIES = { 181 DOCUMENT_CLASS_NAME, 182 CURRENT_ELEMENT_NODE, 183 }; 184 185 187 188 protected static final String DEFAULT_DOCUMENT_CLASS_NAME = 189 "com.sun.org.apache.xerces.internal.dom.DocumentImpl"; 190 191 protected static final String CORE_DOCUMENT_CLASS_NAME = 192 "com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl"; 193 194 protected static final String PSVI_DOCUMENT_CLASS_NAME = 195 "com.sun.org.apache.xerces.internal.dom.PSVIDocumentImpl"; 196 197 200 public static final RuntimeException abort = new RuntimeException (); 201 202 204 private static final boolean DEBUG_EVENTS = false; 205 private static final boolean DEBUG_BASEURI = false; 206 207 211 212 protected DOMErrorHandlerWrapper fErrorHandler = null; 213 214 215 protected boolean fInDTD; 216 217 219 220 protected boolean fCreateEntityRefNodes; 221 222 223 protected boolean fIncludeIgnorableWhitespace; 224 225 226 protected boolean fIncludeComments; 227 228 229 protected boolean fCreateCDATANodes; 230 231 233 234 protected Document fDocument; 235 236 237 protected CoreDocumentImpl fDocumentImpl; 238 239 240 protected boolean fStorePSVI; 241 242 243 protected String fDocumentClassName; 244 245 246 protected DocumentType fDocumentType; 247 248 249 protected Node fCurrentNode; 250 protected CDATASection fCurrentCDATASection; 251 protected EntityImpl fCurrentEntityDecl; 252 protected int fDeferredEntityDecl; 253 254 255 protected final StringBuffer fStringBuffer = new StringBuffer (50); 256 257 259 260 protected StringBuffer fInternalSubset; 261 262 264 protected boolean fDeferNodeExpansion; 265 protected boolean fNamespaceAware; 266 protected DeferredDocumentImpl fDeferredDocumentImpl; 267 protected int fDocumentIndex; 268 protected int fDocumentTypeIndex; 269 protected int fCurrentNodeIndex; 270 protected int fCurrentCDATASectionIndex; 271 272 274 275 protected boolean fInDTDExternalSubset; 276 277 278 protected QName fRoot = new QName(); 279 280 281 protected boolean fInCDATASection; 282 283 284 protected boolean fFirstChunk = false; 285 286 287 289 protected boolean fFilterReject = false; 290 291 293 294 protected Stack fBaseURIStack = new Stack (); 295 296 297 298 protected final QName fRejectedElement = new QName (); 299 300 301 protected Stack fSkippedElemStack = null; 302 303 304 protected boolean fInEntityRef = false; 305 306 307 private QName fAttrQName = new QName (); 308 309 311 protected LSParserFilter fDOMFilter = null; 312 313 317 318 protected AbstractDOMParser (XMLParserConfiguration config) { 319 320 super (config); 321 322 323 fConfiguration.addRecognizedFeatures (RECOGNIZED_FEATURES); 325 326 fConfiguration.setFeature (CREATE_ENTITY_REF_NODES, true); 328 fConfiguration.setFeature (INCLUDE_IGNORABLE_WHITESPACE, true); 329 fConfiguration.setFeature (DEFER_NODE_EXPANSION, true); 330 fConfiguration.setFeature (INCLUDE_COMMENTS_FEATURE, true); 331 fConfiguration.setFeature (CREATE_CDATA_NODES_FEATURE, true); 332 333 fConfiguration.addRecognizedProperties (RECOGNIZED_PROPERTIES); 335 336 fConfiguration.setProperty (DOCUMENT_CLASS_NAME, 338 DEFAULT_DOCUMENT_CLASS_NAME); 339 340 } 342 345 protected String getDocumentClassName() { 346 return fDocumentClassName; 347 } 348 349 363 protected void setDocumentClassName (String documentClassName) { 364 365 if (documentClassName == null) { 367 documentClassName = DEFAULT_DOCUMENT_CLASS_NAME; 368 } 369 370 if (!documentClassName.equals(DEFAULT_DOCUMENT_CLASS_NAME) && 371 !documentClassName.equals(PSVI_DOCUMENT_CLASS_NAME)) { 372 try { 374 Class _class = ObjectFactory.findProviderClass (documentClassName, 375 ObjectFactory.findClassLoader (), true); 376 if (!Document .class.isAssignableFrom (_class)) { 378 throw new IllegalArgumentException ( 379 DOMMessageFormatter.formatMessage( 380 DOMMessageFormatter.DOM_DOMAIN, 381 "InvalidDocumentClassName", new Object [] {documentClassName})); 382 } 383 } 384 catch (ClassNotFoundException e) { 385 throw new IllegalArgumentException ( 386 DOMMessageFormatter.formatMessage( 387 DOMMessageFormatter.DOM_DOMAIN, 388 "MissingDocumentClassName", new Object [] {documentClassName})); 389 } 390 } 391 392 fDocumentClassName = documentClassName; 394 if (!documentClassName.equals(DEFAULT_DOCUMENT_CLASS_NAME)) { 395 fDeferNodeExpansion = false; 396 } 397 398 } 400 404 405 public Document getDocument() { 406 return fDocument; 407 } 409 413 418 public void reset () throws XNIException { 419 super.reset (); 420 421 422 fCreateEntityRefNodes = 424 fConfiguration.getFeature (CREATE_ENTITY_REF_NODES); 425 426 fIncludeIgnorableWhitespace = 427 fConfiguration.getFeature (INCLUDE_IGNORABLE_WHITESPACE); 428 429 fDeferNodeExpansion = 430 fConfiguration.getFeature (DEFER_NODE_EXPANSION); 431 432 fNamespaceAware = fConfiguration.getFeature (NAMESPACES); 433 434 fIncludeComments = fConfiguration.getFeature (INCLUDE_COMMENTS_FEATURE); 435 436 fCreateCDATANodes = fConfiguration.getFeature (CREATE_CDATA_NODES_FEATURE); 437 438 setDocumentClassName ((String ) 440 fConfiguration.getProperty (DOCUMENT_CLASS_NAME)); 441 442 fDocument = null; 444 fDocumentImpl = null; 445 fStorePSVI = false; 446 fDocumentType = null; 447 fDocumentTypeIndex = -1; 448 fDeferredDocumentImpl = null; 449 fCurrentNode = null; 450 451 fStringBuffer.setLength (0); 453 454 fRoot.clear(); 456 fInDTD = false; 457 fInDTDExternalSubset = false; 458 fInCDATASection = false; 459 fFirstChunk = false; 460 fCurrentCDATASection = null; 461 fCurrentCDATASectionIndex = -1; 462 fBaseURIStack.removeAllElements (); 463 464 465 } 467 473 public void setLocale (Locale locale) { 474 fConfiguration.setLocale (locale); 475 476 } 478 482 499 public void startGeneralEntity(String name, 500 XMLResourceIdentifier identifier, 501 String encoding, Augmentations augs) 502 throws XNIException { 503 if (DEBUG_EVENTS) { 504 System.out.println("==>startGeneralEntity ("+name+")"); 505 if (DEBUG_BASEURI) { 506 System.out.println(" expandedSystemId( **baseURI): "+identifier.getExpandedSystemId()); 507 System.out.println(" baseURI:"+ identifier.getBaseSystemId()); 508 } 509 } 510 511 if (!fDeferNodeExpansion) { 514 if (fFilterReject) { 515 return; 516 } 517 setCharacterData(true); 518 EntityReference er = fDocument.createEntityReference(name); 519 if (fDocumentImpl != null) { 520 EntityReferenceImpl erImpl =(EntityReferenceImpl)er; 524 525 erImpl.setBaseURI(identifier.getExpandedSystemId()); 527 if (fDocumentType != null) { 528 NamedNodeMap entities = fDocumentType.getEntities(); 530 fCurrentEntityDecl = (EntityImpl) entities.getNamedItem(name); 531 if (fCurrentEntityDecl != null) { 532 fCurrentEntityDecl.setInputEncoding(encoding); 533 } 534 535 } 536 erImpl.needsSyncChildren(false); 539 } 540 fInEntityRef = true; 541 fCurrentNode.appendChild (er); 542 fCurrentNode = er; 543 } 544 else { 545 546 int er = 547 fDeferredDocumentImpl.createDeferredEntityReference(name, identifier.getExpandedSystemId()); 548 if (fDocumentTypeIndex != -1) { 549 int node = fDeferredDocumentImpl.getLastChild(fDocumentTypeIndex, false); 551 while (node != -1) { 552 short nodeType = fDeferredDocumentImpl.getNodeType(node, false); 553 if (nodeType == Node.ENTITY_NODE) { 554 String nodeName = 555 fDeferredDocumentImpl.getNodeName(node, false); 556 if (nodeName.equals(name)) { 557 fDeferredEntityDecl = node; 558 fDeferredDocumentImpl.setInputEncoding(node, encoding); 559 break; 560 } 561 } 562 node = fDeferredDocumentImpl.getRealPrevSibling(node, false); 563 } 564 } 565 fDeferredDocumentImpl.appendChild(fCurrentNodeIndex, er); 566 fCurrentNodeIndex = er; 567 } 568 569 } 571 588 public void textDecl(String version, String encoding, Augmentations augs) throws XNIException { 589 if (fInDTD){ 590 return; 591 } 592 if (!fDeferNodeExpansion) { 593 if (fCurrentEntityDecl != null && !fFilterReject) { 594 fCurrentEntityDecl.setXmlEncoding(encoding); 595 if(version != null) 596 fCurrentEntityDecl.setXmlVersion(version); 597 } 598 } 599 else { 600 if (fDeferredEntityDecl !=-1) { 601 fDeferredDocumentImpl.setEntityInfo(fDeferredEntityDecl, version, encoding); 602 } 603 } 604 } 606 614 public void comment(XMLString text, Augmentations augs) throws XNIException { 615 if (fInDTD) { 616 if (fInternalSubset != null && !fInDTDExternalSubset) { 617 fInternalSubset.append("<!-- "); 618 fInternalSubset.append(text.toString()); 619 fInternalSubset.append(" -->"); 620 } 621 return; 622 } 623 if (!fIncludeComments || fFilterReject) { 624 return; 625 } 626 if (!fDeferNodeExpansion) { 627 Comment comment = fDocument.createComment (text.toString ()); 628 629 setCharacterData (false); 630 fCurrentNode.appendChild (comment); 631 if (fDOMFilter !=null && !fInEntityRef && 632 (fDOMFilter.getWhatToShow () & NodeFilter.SHOW_COMMENT)!= 0) { 633 short code = fDOMFilter.acceptNode (comment); 634 switch (code) { 635 case LSParserFilter.FILTER_INTERRUPT:{ 636 throw abort; 637 } 638 case LSParserFilter.FILTER_REJECT:{ 639 642 } 644 case LSParserFilter.FILTER_SKIP: { 645 fCurrentNode.removeChild(comment); 648 fFirstChunk = true; 650 return; 651 } 652 653 default: { 654 } 656 } 657 } 658 659 } 660 else { 661 int comment = 662 fDeferredDocumentImpl.createDeferredComment(text.toString()); 663 fDeferredDocumentImpl.appendChild(fCurrentNodeIndex, comment); 664 } 665 666 } 668 685 public void processingInstruction (String target, XMLString data, Augmentations augs) 686 throws XNIException { 687 688 if (fInDTD) { 689 if (fInternalSubset != null && !fInDTDExternalSubset) { 690 fInternalSubset.append ("<?"); 691 fInternalSubset.append (target.toString ()); 692 fInternalSubset.append (' '); 693 fInternalSubset.append (data.toString ()); 694 fInternalSubset.append ("?>"); 695 696 } 697 return; 698 } 699 700 if (DEBUG_EVENTS) { 701 System.out.println ("==>processingInstruction ("+target+")"); 702 } 703 if (!fDeferNodeExpansion) { 704 if (fFilterReject) { 705 return; 706 } 707 ProcessingInstruction pi = 708 fDocument.createProcessingInstruction (target, data.toString ()); 709 710 711 setCharacterData (false); 712 fCurrentNode.appendChild (pi); 713 if (fDOMFilter !=null && !fInEntityRef && 714 (fDOMFilter.getWhatToShow () & NodeFilter.SHOW_PROCESSING_INSTRUCTION)!= 0) { 715 short code = fDOMFilter.acceptNode (pi); 716 switch (code) { 717 case LSParserFilter.FILTER_INTERRUPT:{ 718 throw abort; 719 } 720 case LSParserFilter.FILTER_REJECT:{ 721 } 723 case LSParserFilter.FILTER_SKIP: { 724 fCurrentNode.removeChild(pi); 725 fFirstChunk = true; 729 return; 730 } 731 default: { 732 } 733 } 734 } 735 } 736 else { 737 int pi = fDeferredDocumentImpl. 738 createDeferredProcessingInstruction(target, data.toString()); 739 fDeferredDocumentImpl.appendChild(fCurrentNodeIndex, pi); 740 } 741 742 } 744 766 public void startDocument (XMLLocator locator, String encoding, 767 NamespaceContext namespaceContext, Augmentations augs) 768 throws XNIException { 769 770 if (!fDeferNodeExpansion) { 771 if (fDocumentClassName.equals (DEFAULT_DOCUMENT_CLASS_NAME)) { 772 fDocument = new DocumentImpl (); 773 fDocumentImpl = (CoreDocumentImpl)fDocument; 774 fDocumentImpl.setStrictErrorChecking (false); 778 fDocumentImpl.setInputEncoding (encoding); 780 fDocumentImpl.setDocumentURI (locator.getExpandedSystemId ()); 782 } 783 else if (fDocumentClassName.equals (PSVI_DOCUMENT_CLASS_NAME)) { 784 fDocument = new PSVIDocumentImpl(); 785 fDocumentImpl = (CoreDocumentImpl)fDocument; 786 fStorePSVI = true; 787 fDocumentImpl.setStrictErrorChecking (false); 791 fDocumentImpl.setInputEncoding (encoding); 793 fDocumentImpl.setDocumentURI (locator.getExpandedSystemId ()); 795 } 796 else { 797 try { 799 ClassLoader cl = ObjectFactory.findClassLoader(); 800 Class documentClass = ObjectFactory.findProviderClass (fDocumentClassName, 801 cl, true); 802 fDocument = (Document )documentClass.newInstance (); 803 804 Class defaultDocClass = 806 ObjectFactory.findProviderClass (CORE_DOCUMENT_CLASS_NAME, 807 cl, true); 808 if (defaultDocClass.isAssignableFrom (documentClass)) { 809 fDocumentImpl = (CoreDocumentImpl)fDocument; 810 811 Class psviDocClass = ObjectFactory.findProviderClass (PSVI_DOCUMENT_CLASS_NAME, 812 cl, true); 813 if (psviDocClass.isAssignableFrom (documentClass)) { 814 fStorePSVI = true; 815 } 816 817 fDocumentImpl.setStrictErrorChecking(false); 821 fDocumentImpl.setInputEncoding(encoding); 823 if (locator != null) { 825 fDocumentImpl.setDocumentURI(locator.getExpandedSystemId()); 826 } 827 } 828 } 829 catch (ClassNotFoundException e) { 830 } 832 catch (Exception e) { 833 throw new RuntimeException ( 834 DOMMessageFormatter.formatMessage( 835 DOMMessageFormatter.DOM_DOMAIN, 836 "CannotCreateDocumentClass", 837 new Object [] {fDocumentClassName})); 838 } 839 } 840 fCurrentNode = fDocument; 841 } 842 else { 843 fDeferredDocumentImpl = new DeferredDocumentImpl(fNamespaceAware); 844 fDocument = fDeferredDocumentImpl; 845 fDocumentIndex = fDeferredDocumentImpl.createDeferredDocument(); 846 849 fDeferredDocumentImpl.setInputEncoding(encoding); 851 fDeferredDocumentImpl.setDocumentURI(locator.getExpandedSystemId()); 853 fCurrentNodeIndex = fDocumentIndex; 854 855 } 856 857 } 859 872 public void xmlDecl(String version, String encoding, String standalone, 873 Augmentations augs) 874 throws XNIException { 875 if (!fDeferNodeExpansion) { 876 if (fDocumentImpl != null) { 879 if(version != null) 880 fDocumentImpl.setXmlVersion(version); 881 fDocumentImpl.setXmlEncoding(encoding); 882 fDocumentImpl.setXmlStandalone("yes".equals(standalone)); 883 } 884 } 885 else { 886 if(version != null) 887 fDeferredDocumentImpl.setXmlVersion(version); 888 fDeferredDocumentImpl.setXmlEncoding(encoding); 889 fDeferredDocumentImpl.setXmlStandalone("yes".equals(standalone)); 890 } 891 } 893 905 public void doctypeDecl (String rootElement, 906 String publicId, String systemId, Augmentations augs) 907 throws XNIException { 908 909 if (!fDeferNodeExpansion) { 910 if (fDocumentImpl != null) { 911 fDocumentType = fDocumentImpl.createDocumentType( 912 rootElement, publicId, systemId); 913 fCurrentNode.appendChild(fDocumentType); 914 } 915 } 916 else { 917 fDocumentTypeIndex = fDeferredDocumentImpl. 918 createDeferredDocumentType(rootElement, publicId, systemId); 919 fDeferredDocumentImpl.appendChild(fCurrentNodeIndex, fDocumentTypeIndex); 920 } 921 922 } 924 935 public void startElement(QName element, XMLAttributes attributes, Augmentations augs) 936 throws XNIException { 937 if (DEBUG_EVENTS) { 938 System.out.println("==>startElement ("+element.rawname+")"); 939 } 940 if (!fDeferNodeExpansion) { 941 if (fFilterReject) { 942 return; 943 } 944 Element el = createElementNode(element); 945 int attrCount = attributes.getLength(); 946 for (int i = 0; i < attrCount; i++) { 947 attributes.getName (i, fAttrQName); 948 Attr attr = createAttrNode (fAttrQName); 949 950 String attrValue = attributes.getValue (i); 951 952 Augmentations aaugs = attributes.getAugmentations(i); 953 954 AttributePSVI attrPSVI =(AttributePSVI) aaugs.getItem(Constants.ATTRIBUTE_PSVI); 955 if (fStorePSVI && attrPSVI != null){ 956 ((PSVIAttrNSImpl) attr).setPSVI (attrPSVI); 957 } 958 attr.setValue(attrValue); 959 el.setAttributeNode(attr); 960 if (fDocumentImpl != null) { 965 AttrImpl attrImpl = (AttrImpl)attr; 966 967 attrImpl.setType(getAttributeType(attributes,aaugs,i)); 968 969 if (isIdAttribute(attributes,aaugs,i)) { 970 ((ElementImpl)el).setIdAttributeNode(attr, true); 971 } 972 973 attrImpl.setSpecified(attributes.isSpecified(i)); 974 } 976 } 977 setCharacterData (false); 978 ((ElementImpl)el).setType(getElementTypeInfoFromAugs(augs)); 979 if (fDOMFilter != null && !fInEntityRef) { 981 if (fRoot.rawname == null) { 982 fRoot.setValues(element); 984 } else { 985 short code = fDOMFilter.startElement(el); 986 switch (code) { 987 case LSParserFilter.FILTER_INTERRUPT : 988 { 989 throw abort; 990 } 991 case LSParserFilter.FILTER_REJECT : 992 { 993 fFilterReject = true; 994 fRejectedElement.setValues(element); 995 return; 996 } 997 case LSParserFilter.FILTER_SKIP : 998 { 999 fSkippedElemStack.push(new QName(element)); 1000 return; 1001 } 1002 default : {} 1003 } 1004 } 1005 } 1006 fCurrentNode.appendChild (el); 1007 fCurrentNode = el; 1008 } 1009 else { 1010 1011 int el = 1012 fDeferredDocumentImpl.createDeferredElement(fNamespaceAware ? 1013 element.uri : null, 1014 element.rawname, 1015 getElementTypeInfoFromAugs(augs)); 1016 int attrCount = attributes.getLength(); 1017 for (int i = 0; i < attrCount; i++) { 1018 Augmentations aaugs = attributes.getAugmentations(i); 1020 1021 fDeferredDocumentImpl.setDeferredAttribute( 1023 el, 1024 attributes.getQName(i), 1025 attributes.getURI(i), 1026 attributes.getValue(i), 1027 attributes.isSpecified(i), 1028 isIdAttribute(attributes,aaugs,i), 1029 getAttributeType(attributes,aaugs,i)); 1030 } 1031 1032 fDeferredDocumentImpl.appendChild (fCurrentNodeIndex, el); 1033 fCurrentNodeIndex = el; 1034 } 1035 } 1037 1040 private boolean isIdAttribute(XMLAttributes attributes,Augmentations augs, int index) { 1041 Object o = augs.getItem(Constants.ID_ATTRIBUTE); 1043 if( o instanceof Boolean ) 1044 return ((Boolean )o).booleanValue(); 1045 1046 return "ID".equals(attributes.getType(index)); 1048 } 1049 1050 1053 private TypeInfo getAttributeType(XMLAttributes attributes,Augmentations augs, int index) { 1054 TypeInfo type = (TypeInfo )augs.getItem(Constants.TYPEINFO); 1056 if(type!=null) return type; 1057 boolean isDeclared = Boolean.TRUE.equals (attributes.getAugmentations (index).getItem (Constants.ATTRIBUTE_DECLARED)); 1058 if (isDeclared ) 1060 return TypeInfoImpl.getDTDTypeInfo(attributes.getType(index)); 1061 else 1062 return new TypeInfoImpl(); 1063 } 1064 1065 1066 1071 private TypeInfo getElementTypeInfoFromAugs( Augmentations augs ) { 1072 if (augs == null) return new TypeInfoImpl(); 1074 1075 TypeInfo ti = (TypeInfo )augs.getItem(Constants.TYPEINFO); 1077 if(ti!=null) return ti; 1078 1079 ElementPSVI elementPSVI = (ElementPSVI)augs.getItem(Constants.ELEMENT_PSVI); 1081 if (elementPSVI == null) return null; 1082 1083 XSTypeDefinition type = elementPSVI.getMemberTypeDefinition(); 1084 1085 if( type!=null ) return type; 1086 1087 return elementPSVI.getTypeDefinition(); 1088 1089 } 1090 1091 1116 1117 1126 public void emptyElement (QName element, XMLAttributes attributes, Augmentations augs) 1127 throws XNIException { 1128 1129 startElement (element, attributes, augs); 1130 endElement (element, augs); 1131 1132 } 1134 1142 public void characters (XMLString text, Augmentations augs) throws XNIException { 1143 1144 if (DEBUG_EVENTS) { 1145 System.out.println("==>characters(): "+text.toString()); 1146 } 1147 1148 if (!fDeferNodeExpansion) { 1149 1150 if (fFilterReject) { 1151 return; 1152 } 1153 if (fInCDATASection && fCreateCDATANodes) { 1154 if (fCurrentCDATASection == null) { 1155 fCurrentCDATASection = 1156 fDocument.createCDATASection(text.toString()); 1157 fCurrentNode.appendChild(fCurrentCDATASection); 1158 fCurrentNode = fCurrentCDATASection; 1159 } 1160 else { 1161 fCurrentCDATASection.appendData(text.toString()); 1162 } 1163 } 1164 else if (!fInDTD) { 1165 if (text.length == 0) { 1168 return; 1169 } 1170 1171 String value = text.toString (); 1172 Node child = fCurrentNode.getLastChild (); 1173 if (child != null && child.getNodeType () == Node.TEXT_NODE) { 1174 if (fFirstChunk) { 1176 if (fDocumentImpl != null) { 1177 fStringBuffer.append(((TextImpl)child).removeData()); 1178 } else { 1179 fStringBuffer.append(((Text )child).getData()); 1180 ((Text )child).setNodeValue(null); 1181 } 1182 fFirstChunk = false; 1183 } 1184 fStringBuffer.append(value); 1185 } 1186 else { 1187 fFirstChunk = true; 1188 Text textNode = fDocument.createTextNode(value); 1189 fCurrentNode.appendChild(textNode); 1190 } 1191 } 1192 } 1193 else { 1194 if (fInCDATASection && fCreateCDATANodes) { 1197 if (fCurrentCDATASectionIndex == -1) { 1198 int cs = fDeferredDocumentImpl. 1199 createDeferredCDATASection (text.toString ()); 1200 1201 fDeferredDocumentImpl.appendChild (fCurrentNodeIndex, cs); 1202 fCurrentCDATASectionIndex = cs; 1203 fCurrentNodeIndex = cs; 1204 } 1205 else { 1206 int txt = fDeferredDocumentImpl. 1207 createDeferredTextNode(text.toString(), false); 1208 fDeferredDocumentImpl.appendChild(fCurrentNodeIndex, txt); 1209 } 1210 } else if (!fInDTD) { 1211 if (text.length == 0) { 1214 return; 1215 } 1216 1217 String value = text.toString (); 1218 int txt = fDeferredDocumentImpl. 1219 createDeferredTextNode (value, false); 1220 fDeferredDocumentImpl.appendChild (fCurrentNodeIndex, txt); 1221 1222 } 1223 } 1224 } 1226 1239 public void ignorableWhitespace (XMLString text, Augmentations augs) throws XNIException { 1240 1241 if (!fIncludeIgnorableWhitespace || fFilterReject) { 1242 return; 1243 } 1244 if (!fDeferNodeExpansion) { 1245 Node child = fCurrentNode.getLastChild(); 1246 if (child != null && child.getNodeType() == Node.TEXT_NODE) { 1247 Text textNode = (Text )child; 1248 textNode.appendData(text.toString()); 1249 } 1250 else { 1251 Text textNode = fDocument.createTextNode(text.toString()); 1252 if (fDocumentImpl != null) { 1253 TextImpl textNodeImpl = (TextImpl)textNode; 1254 textNodeImpl.setIgnorableWhitespace(true); 1255 } 1256 fCurrentNode.appendChild(textNode); 1257 } 1258 } 1259 else { 1260 int txt = fDeferredDocumentImpl. 1263 createDeferredTextNode(text.toString(), true); 1264 fDeferredDocumentImpl.appendChild(fCurrentNodeIndex, txt); 1265 } 1266 1267 } 1269 1277 public void endElement(QName element, Augmentations augs) throws XNIException { 1278 if (DEBUG_EVENTS) { 1279 System.out.println("==>endElement ("+element.rawname+")"); 1280 } 1281 if (!fDeferNodeExpansion) { 1282 1283 if (fStorePSVI && augs != null) { 1285 ElementPSVI elementPSVI = (ElementPSVI)augs.getItem(Constants.ELEMENT_PSVI); 1286 if (elementPSVI != null) { 1287 ((PSVIElementNSImpl)fCurrentNode).setPSVI(elementPSVI); 1288 } 1289 } 1290 1291 if (fDOMFilter != null) { 1292 if (fFilterReject) { 1293 if (element.equals(fRejectedElement)) { 1294 fFilterReject = false; 1295 } 1296 return; 1297 } 1298 if (!fSkippedElemStack.isEmpty()) { 1299 if (fSkippedElemStack.peek().equals(element)) { 1300 fSkippedElemStack.pop(); 1301 return; 1302 } 1303 } 1304 setCharacterData (false); 1305 if (!fRoot.equals(element) && !fInEntityRef && (fDOMFilter.getWhatToShow () & NodeFilter.SHOW_ELEMENT)!=0) { 1306 short code = fDOMFilter.acceptNode (fCurrentNode); 1307 switch (code) { 1308 case LSParserFilter.FILTER_INTERRUPT:{ 1309 throw abort; 1310 } 1311 case LSParserFilter.FILTER_REJECT:{ 1312 Node parent = fCurrentNode.getParentNode(); 1313 parent.removeChild(fCurrentNode); 1314 fCurrentNode = parent; 1315 return; 1316 } 1317 case LSParserFilter.FILTER_SKIP: { 1318 fFirstChunk = true; 1324 1325 Node parent = fCurrentNode.getParentNode (); 1327 NodeList ls = fCurrentNode.getChildNodes (); 1328 int length = ls.getLength (); 1329 1330 for (int i=0;i<length;i++) { 1331 parent.appendChild(ls.item(0)); 1332 } 1333 parent.removeChild(fCurrentNode); 1334 fCurrentNode = parent; 1335 1336 return; 1337 } 1338 1339 default: { } 1340 } 1341 } 1342 fCurrentNode = fCurrentNode.getParentNode (); 1343 1344 } else { 1346 setCharacterData(false); 1347 fCurrentNode = fCurrentNode.getParentNode(); 1348 } 1349 1350 } 1351 else { 1352 fCurrentNodeIndex = 1353 fDeferredDocumentImpl.getParentNode(fCurrentNodeIndex, false); 1354 } 1355 } 1357 1358 1364 public void startCDATA (Augmentations augs) throws XNIException { 1365 1366 fInCDATASection = true; 1367 if (!fDeferNodeExpansion) { 1368 if (fFilterReject) { 1369 return; 1370 } 1371 if (fCreateCDATANodes) { 1372 setCharacterData (false); 1373 } 1374 } 1375 } 1377 1383 public void endCDATA (Augmentations augs) throws XNIException { 1384 1385 fInCDATASection = false; 1386 if (!fDeferNodeExpansion) { 1387 1388 if (fFilterReject) { 1389 return; 1390 } 1391 1392 if (fCurrentCDATASection !=null) { 1393 1394 if (fDOMFilter !=null && !fInEntityRef && 1395 (fDOMFilter.getWhatToShow () & NodeFilter.SHOW_CDATA_SECTION)!= 0) { 1396 short code = fDOMFilter.acceptNode (fCurrentCDATASection); 1397 switch (code) { 1398 case LSParserFilter.FILTER_INTERRUPT:{ 1399 throw abort; 1400 } 1401 case LSParserFilter.FILTER_REJECT:{ 1402 } 1404 case LSParserFilter.FILTER_SKIP: { 1405 Node parent = fCurrentNode.getParentNode(); 1406 parent.removeChild(fCurrentCDATASection); 1407 fCurrentNode = parent; 1408 return; 1409 } 1410 1411 default: { 1412 } 1414 } 1415 } 1416 1417 fCurrentNode = fCurrentNode.getParentNode (); 1418 fCurrentCDATASection = null; 1419 } 1420 } 1421 else { 1422 if (fCurrentCDATASectionIndex !=-1) { 1423 fCurrentNodeIndex = 1424 fDeferredDocumentImpl.getParentNode(fCurrentNodeIndex, false); 1425 fCurrentCDATASectionIndex = -1; 1426 } 1427 } 1428 1429 } 1431 1437 public void endDocument (Augmentations augs) throws XNIException { 1438 1439 if (!fDeferNodeExpansion) { 1440 if (fDocumentImpl != null) { 1444 fDocumentImpl.setStrictErrorChecking(true); 1445 } 1446 fCurrentNode = null; 1447 } 1448 else { 1449 fCurrentNodeIndex = -1; 1450 } 1451 1452 } 1454 1466 public void endGeneralEntity(String name, Augmentations augs) throws XNIException { 1467 if (DEBUG_EVENTS) { 1468 System.out.println("==>endGeneralEntity: ("+name+")"); 1469 } 1470 if (!fDeferNodeExpansion) { 1471 1472 if (fFilterReject) { 1473 return; 1474 } 1475 setCharacterData (true); 1476 1477 if (fDocumentType != null) { 1478 NamedNodeMap entities = fDocumentType.getEntities(); 1480 fCurrentEntityDecl = (EntityImpl) entities.getNamedItem(name); 1481 if (fCurrentEntityDecl != null) { 1482 if (fCurrentEntityDecl != null && fCurrentEntityDecl.getFirstChild() == null) { 1483 fCurrentEntityDecl.setReadOnly(false, true); 1484 Node child = fCurrentNode.getFirstChild(); 1485 while (child != null) { 1486 Node copy = child.cloneNode (true); 1487 fCurrentEntityDecl.appendChild (copy); 1488 child = child.getNextSibling (); 1489 } 1490 fCurrentEntityDecl.setReadOnly (true, true); 1491 1492 } 1494 fCurrentEntityDecl = null; 1495 } 1496 1497 } 1498 fInEntityRef = false; 1499 boolean removeEntityRef = false; 1500 if (fCreateEntityRefNodes) { 1501 if (fDocumentImpl != null) { 1502 ((NodeImpl)fCurrentNode).setReadOnly(true, true); 1504 } 1505 1506 if (fDOMFilter !=null && 1507 (fDOMFilter.getWhatToShow () & NodeFilter.SHOW_ENTITY_REFERENCE)!= 0) { 1508 short code = fDOMFilter.acceptNode (fCurrentNode); 1509 switch (code) { 1510 case LSParserFilter.FILTER_INTERRUPT:{ 1511 throw abort; 1512 } 1513 case LSParserFilter.FILTER_REJECT:{ 1514 Node parent = fCurrentNode.getParentNode(); 1515 parent.removeChild(fCurrentNode); 1516 fCurrentNode = parent; 1517 return; 1518 1519 } 1520 case LSParserFilter.FILTER_SKIP: { 1521 fFirstChunk = true; 1523 removeEntityRef = true; 1524 break; 1525 } 1526 1527 default: { 1528 fCurrentNode = fCurrentNode.getParentNode (); 1529 } 1530 } 1531 } else { 1532 fCurrentNode = fCurrentNode.getParentNode(); 1533 } 1534 } 1535 1536 if (!fCreateEntityRefNodes || removeEntityRef) { 1537 NodeList children = fCurrentNode.getChildNodes(); 1540 Node parent = fCurrentNode.getParentNode(); 1541 int length = children.getLength(); 1542 if (length > 0) { 1543 Node node = fCurrentNode.getPreviousSibling(); 1545 Node child = children.item (0); 1547 if (node != null && node.getNodeType () == Node.TEXT_NODE && 1548 child.getNodeType () == Node.TEXT_NODE) { 1549 ((Text )node).appendData (child.getNodeValue ()); 1550 fCurrentNode.removeChild (child); 1551 1552 } else { 1553 node = parent.insertBefore(child, fCurrentNode); 1554 handleBaseURI(node); 1555 } 1556 1557 for (int i=1;i <length;i++) { 1558 node = parent.insertBefore(children.item(0), fCurrentNode); 1559 handleBaseURI(node); 1560 } 1561 } parent.removeChild(fCurrentNode); 1563 fCurrentNode = parent; 1564 } 1565 } 1566 else { 1567 1568 if (fDocumentTypeIndex != -1) { 1569 int node = fDeferredDocumentImpl.getLastChild(fDocumentTypeIndex, false); 1571 while (node != -1) { 1572 short nodeType = fDeferredDocumentImpl.getNodeType(node, false); 1573 if (nodeType == Node.ENTITY_NODE) { 1574 String nodeName = 1575 fDeferredDocumentImpl.getNodeName(node, false); 1576 if (nodeName.equals(name)) { 1577 fDeferredEntityDecl = node; 1578 break; 1579 } 1580 } 1581 node = fDeferredDocumentImpl.getRealPrevSibling(node, false); 1582 } 1583 } 1584 1585 if (fDeferredEntityDecl != -1 && 1586 fDeferredDocumentImpl.getLastChild (fDeferredEntityDecl, false) == -1) { 1587 int prevIndex = -1; 1589 int childIndex = fDeferredDocumentImpl.getLastChild (fCurrentNodeIndex, false); 1590 while (childIndex != -1) { 1591 int cloneIndex = fDeferredDocumentImpl.cloneNode (childIndex, true); 1592 fDeferredDocumentImpl.insertBefore (fDeferredEntityDecl, cloneIndex, prevIndex); 1593 prevIndex = cloneIndex; 1594 childIndex = fDeferredDocumentImpl.getRealPrevSibling (childIndex, false); 1595 } 1596 } 1597 if (fCreateEntityRefNodes) { 1598 fCurrentNodeIndex = 1599 fDeferredDocumentImpl.getParentNode (fCurrentNodeIndex, 1600 false); 1601 } else { 1605 int childIndex = fDeferredDocumentImpl.getLastChild (fCurrentNodeIndex, false); 1607 int parentIndex = 1608 fDeferredDocumentImpl.getParentNode (fCurrentNodeIndex, 1609 false); 1610 1611 int prevIndex = fCurrentNodeIndex; 1612 int lastChild = childIndex; 1613 int sibling = -1; 1614 while (childIndex != -1) { 1615 handleBaseURI (childIndex); 1616 sibling = fDeferredDocumentImpl.getRealPrevSibling (childIndex, false); 1617 fDeferredDocumentImpl.insertBefore (parentIndex, childIndex, prevIndex); 1618 prevIndex = childIndex; 1619 childIndex = sibling; 1620 } 1621 if(lastChild != -1) 1622 fDeferredDocumentImpl.setAsLastChild (parentIndex, lastChild); 1623 else{ 1624 sibling = fDeferredDocumentImpl.getRealPrevSibling (prevIndex, false); 1625 fDeferredDocumentImpl.setAsLastChild (parentIndex, sibling); 1626 } 1627 fCurrentNodeIndex = parentIndex; 1628 } 1629 fDeferredEntityDecl = -1; 1630 } 1631 } 1633 1640 protected final void handleBaseURI (Node node){ 1641 if (fDocumentImpl != null) { 1642 String baseURI = null; 1645 short nodeType = node.getNodeType (); 1646 1647 if (nodeType == Node.ELEMENT_NODE) { 1648 if (fNamespaceAware) { 1651 if (((Element )node).getAttributeNodeNS("http://www.w3.org/XML/1998/namespace","base")!=null) { 1652 return; 1653 } 1654 } else if (((Element )node).getAttributeNode("xml:base") != null) { 1655 return; 1656 } 1657 baseURI = ((EntityReferenceImpl)fCurrentNode).getBaseURI(); 1659 if (baseURI !=null && !baseURI.equals(fDocumentImpl.getDocumentURI())) { 1660 if (fNamespaceAware) { 1661 ((Element )node).setAttributeNS("http://www.w3.org/XML/1998/namespace","base", baseURI); 1662 } else { 1663 ((Element )node).setAttribute("xml:base", baseURI); 1664 } 1665 } 1666 } 1667 else if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) { 1668 1669 baseURI = ((EntityReferenceImpl)fCurrentNode).getBaseURI (); 1670 if (baseURI !=null && fErrorHandler != null) { 1671 DOMErrorImpl error = new DOMErrorImpl (); 1672 error.fType = "infoset-baseURI"; 1673 error.fRelatedData = baseURI; 1674 error.fSeverity = DOMError.SEVERITY_WARNING; 1675 fErrorHandler.getErrorHandler().handleError(error); 1676 } 1677 } 1678 } 1679 } 1680 1681 1689 protected final void handleBaseURI (int node){ 1690 short nodeType = fDeferredDocumentImpl.getNodeType (node, false); 1691 1692 if (nodeType == Node.ELEMENT_NODE) { 1693 String baseURI = fDeferredDocumentImpl.getNodeValueString(fCurrentNodeIndex, false); 1694 if (baseURI == null) { 1695 baseURI = fDeferredDocumentImpl.getDeferredEntityBaseURI(fDeferredEntityDecl); 1696 } 1697 if (baseURI !=null && !baseURI.equals(fDeferredDocumentImpl.getDocumentURI())) { 1698 fDeferredDocumentImpl.setDeferredAttribute(node, 1699 "xml:base", 1700 "http://www.w3.org/XML/1998/namespace", 1701 baseURI, 1702 true); 1703 } 1704 } 1705 else if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) { 1706 1707 1708 String baseURI = fDeferredDocumentImpl.getNodeValueString (fCurrentNodeIndex, false); 1710 1711 if (baseURI == null) { 1712 baseURI = fDeferredDocumentImpl.getDeferredEntityBaseURI (fDeferredEntityDecl); 1714 } 1715 1716 if (baseURI != null && fErrorHandler != null) { 1717 DOMErrorImpl error = new DOMErrorImpl (); 1718 error.fType = "infoset-baseURI"; 1719 error.fRelatedData = baseURI; 1720 error.fSeverity = DOMError.SEVERITY_WARNING; 1721 fErrorHandler.getErrorHandler().handleError(error); 1722 } 1723 } 1724 } 1725 1726 1727 1731 1745 public void startDTD(XMLLocator locator, Augmentations augs) throws XNIException { 1746 if (DEBUG_EVENTS) { 1747 System.out.println("==>startDTD"); 1748 if (DEBUG_BASEURI) { 1749 System.out.println(" expandedSystemId: "+locator.getExpandedSystemId()); 1750 System.out.println(" baseURI:"+ locator.getBaseSystemId()); 1751 } 1752 } 1753 1754 fInDTD = true; 1755 if (locator != null) { 1756 fBaseURIStack.push(locator.getBaseSystemId()); 1757 } 1758 if (fDeferNodeExpansion || fDocumentImpl != null) { 1759 fInternalSubset = new StringBuffer (1024); 1760 } 1761 } 1763 1764 1772 public void endDTD(Augmentations augs) throws XNIException { 1773 if (DEBUG_EVENTS) { 1774 System.out.println("==>endDTD()"); 1775 } 1776 fInDTD = false; 1777 if (!fBaseURIStack.isEmpty()) { 1778 fBaseURIStack.pop(); 1779 } 1780 String internalSubset = fInternalSubset != null && fInternalSubset.length() > 0 1781 ? fInternalSubset.toString() : null; 1782 if (fDeferNodeExpansion) { 1783 if (internalSubset != null) { 1784 fDeferredDocumentImpl.setInternalSubset(fDocumentTypeIndex, internalSubset); 1785 } 1786 } 1787 else if (fDocumentImpl != null) { 1788 if (internalSubset != null) { 1789 ((DocumentTypeImpl)fDocumentType).setInternalSubset(internalSubset); 1790 } 1791 } 1792 } 1794 1807 public void startConditional(short type, Augmentations augs) throws XNIException { 1808 } 1810 1818 public void endConditional(Augmentations augs) throws XNIException { 1819 } 1821 1822 1830 public void startExternalSubset(XMLResourceIdentifier identifier, 1831 Augmentations augs) throws XNIException { 1832 if (DEBUG_EVENTS) { 1833 System.out.println("==>startExternalSubset"); 1834 if (DEBUG_BASEURI) { 1835 System.out.println(" expandedSystemId: "+identifier.getExpandedSystemId()); 1836 System.out.println(" baseURI:"+ identifier.getBaseSystemId()); 1837 } 1838 } 1839 fBaseURIStack.push(identifier.getBaseSystemId()); 1840 fInDTDExternalSubset = true; 1841 } 1843 1851 public void endExternalSubset(Augmentations augs) throws XNIException { 1852 fInDTDExternalSubset = false; 1853 fBaseURIStack.pop(); 1854 } 1856 1872 public void internalEntityDecl (String name, XMLString text, 1873 XMLString nonNormalizedText, 1874 Augmentations augs) throws XNIException { 1875 1876 if (DEBUG_EVENTS) { 1877 System.out.println("==>internalEntityDecl: "+name); 1878 if (DEBUG_BASEURI) { 1879 System.out.println(" baseURI:"+ (String )fBaseURIStack.peek()); 1880 } 1881 } 1882 if (fInternalSubset != null && !fInDTDExternalSubset) { 1884 fInternalSubset.append("<!ENTITY "); 1885 if (name.startsWith("%")) { 1886 fInternalSubset.append("% "); 1887 fInternalSubset.append(name.substring(1)); 1888 } 1889 else { 1890 fInternalSubset.append(name); 1891 } 1892 fInternalSubset.append(' '); 1893 String value = nonNormalizedText.toString(); 1894 boolean singleQuote = value.indexOf('\'') == -1; 1895 fInternalSubset.append(singleQuote ? '\'' : '"'); 1896 fInternalSubset.append(value); 1897 fInternalSubset.append(singleQuote ? '\'' : '"'); 1898 fInternalSubset.append(">\n"); 1899 } 1900 1901 1905 if(name.startsWith("%")) 1908 return; 1909 if (fDocumentType != null) { 1910 NamedNodeMap entities = fDocumentType.getEntities(); 1911 EntityImpl entity = (EntityImpl)entities.getNamedItem(name); 1912 if (entity == null) { 1913 entity = (EntityImpl)fDocumentImpl.createEntity(name); 1914 entity.setBaseURI((String )fBaseURIStack.peek()); 1915 entities.setNamedItem(entity); 1916 } 1917 } 1918 1919 if (fDocumentTypeIndex != -1) { 1921 boolean found = false; 1922 int node = fDeferredDocumentImpl.getLastChild(fDocumentTypeIndex, false); 1923 while (node != -1) { 1924 short nodeType = fDeferredDocumentImpl.getNodeType(node, false); 1925 if (nodeType == Node.ENTITY_NODE) { 1926 String nodeName = fDeferredDocumentImpl.getNodeName(node, false); 1927 if (nodeName.equals(name)) { 1928 found = true; 1929 break; 1930 } 1931 } 1932 node = fDeferredDocumentImpl.getRealPrevSibling(node, false); 1933 } 1934 if (!found) { 1935 int entityIndex = 1936 fDeferredDocumentImpl.createDeferredEntity(name, null, null, null, (String )fBaseURIStack.peek()); 1937 fDeferredDocumentImpl.appendChild(fDocumentTypeIndex, entityIndex); 1938 } 1939 } 1940 1941 } 1943 1956 public void externalEntityDecl (String name, XMLResourceIdentifier identifier, 1957 Augmentations augs) throws XNIException { 1958 1959 1960 if (DEBUG_EVENTS) { 1961 System.out.println("==>externalEntityDecl: "+name); 1962 if (DEBUG_BASEURI) { 1963 System.out.println(" expandedSystemId:"+ identifier.getExpandedSystemId()); 1964 System.out.println(" baseURI:"+ identifier.getBaseSystemId()); 1965 } 1966 } 1967 String publicId = identifier.getPublicId(); 1969 String literalSystemId = identifier.getLiteralSystemId(); 1970 if (fInternalSubset != null && !fInDTDExternalSubset) { 1971 fInternalSubset.append("<!ENTITY "); 1972 if (name.startsWith("%")) { 1973 fInternalSubset.append("% "); 1974 fInternalSubset.append(name.substring(1)); 1975 } 1976 else { 1977 fInternalSubset.append(name); 1978 } 1979 fInternalSubset.append(' '); 1980 if (publicId != null) { 1981 fInternalSubset.append("PUBLIC '"); 1982 fInternalSubset.append(publicId); 1983 fInternalSubset.append("' '"); 1984 } 1985 else { 1986 fInternalSubset.append("SYSTEM '"); 1987 } 1988 fInternalSubset.append(literalSystemId); 1989 fInternalSubset.append("'>\n"); 1990 } 1991 1992 1996 if(name.startsWith("%")) 1999 return; 2000 if (fDocumentType != null) { 2001 NamedNodeMap entities = fDocumentType.getEntities(); 2002 EntityImpl entity = (EntityImpl)entities.getNamedItem(name); 2003 if (entity == null) { 2004 entity = (EntityImpl)fDocumentImpl.createEntity(name); 2005 entity.setPublicId(publicId); 2006 entity.setSystemId(literalSystemId); 2007 entity.setBaseURI(identifier.getBaseSystemId()); 2008 entities.setNamedItem(entity); 2009 } 2010 } 2011 2012 if (fDocumentTypeIndex != -1) { 2014 boolean found = false; 2015 int nodeIndex = fDeferredDocumentImpl.getLastChild(fDocumentTypeIndex, false); 2016 while (nodeIndex != -1) { 2017 short nodeType = fDeferredDocumentImpl.getNodeType(nodeIndex, false); 2018 if (nodeType == Node.ENTITY_NODE) { 2019 String nodeName = fDeferredDocumentImpl.getNodeName(nodeIndex, false); 2020 if (nodeName.equals(name)) { 2021 found = true; 2022 break; 2023 } 2024 } 2025 nodeIndex = fDeferredDocumentImpl.getRealPrevSibling(nodeIndex, false); 2026 } 2027 if (!found) { 2028 int entityIndex = fDeferredDocumentImpl.createDeferredEntity( 2029 name, publicId, literalSystemId, null, identifier.getBaseSystemId()); 2030 fDeferredDocumentImpl.appendChild(fDocumentTypeIndex, entityIndex); 2031 } 2032 } 2033 2034 2035 } 2037 2038 2053 public void startParameterEntity(String name, 2054 XMLResourceIdentifier identifier, 2055 String encoding, 2056 Augmentations augs) throws XNIException { 2057 if (DEBUG_EVENTS) { 2058 System.out.println("==>startParameterEntity: "+name); 2059 if (DEBUG_BASEURI) { 2060 System.out.println(" expandedSystemId: "+identifier.getExpandedSystemId()); 2061 System.out.println(" baseURI:"+ identifier.getBaseSystemId()); 2062 } 2063 } 2064 fBaseURIStack.push(identifier.getExpandedSystemId()); 2065 } 2066 2067 2068 2078 public void endParameterEntity (String name, Augmentations augs) throws XNIException { 2079 2080 if (DEBUG_EVENTS) { 2081 System.out.println("==>endParameterEntity: "+name); 2082 } 2083 fBaseURIStack.pop(); 2084 } 2085 2086 2098 public void unparsedEntityDecl (String name, XMLResourceIdentifier identifier, 2099 String notation, Augmentations augs) 2100 throws XNIException { 2101 2102 if (DEBUG_EVENTS) { 2103 System.out.println("==>unparsedEntityDecl: "+name); 2104 if (DEBUG_BASEURI) { 2105 System.out.println(" expandedSystemId:"+ identifier.getExpandedSystemId()); 2106 System.out.println(" baseURI:"+ identifier.getBaseSystemId()); 2107 } 2108 } 2109 String publicId = identifier.getPublicId(); 2111 String literalSystemId = identifier.getLiteralSystemId(); 2112 if (fInternalSubset != null && !fInDTDExternalSubset) { 2113 fInternalSubset.append("<!ENTITY "); 2114 fInternalSubset.append(name); 2115 fInternalSubset.append(' '); 2116 if (publicId != null) { 2117 fInternalSubset.append("PUBLIC '"); 2118 fInternalSubset.append(publicId); 2119 if (literalSystemId != null) { 2120 fInternalSubset.append("' '"); 2121 fInternalSubset.append(literalSystemId); 2122 } 2123 } 2124 else { 2125 fInternalSubset.append("SYSTEM '"); 2126 fInternalSubset.append(literalSystemId); 2127 } 2128 fInternalSubset.append("' NDATA "); 2129 fInternalSubset.append(notation); 2130 fInternalSubset.append(">\n"); 2131 } 2132 2133 2137 if (fDocumentType != null) { 2139 NamedNodeMap entities = fDocumentType.getEntities(); 2140 EntityImpl entity = (EntityImpl)entities.getNamedItem(name); 2141 if (entity == null) { 2142 entity = (EntityImpl)fDocumentImpl.createEntity(name); 2143 entity.setPublicId(publicId); 2144 entity.setSystemId(literalSystemId); 2145 entity.setNotationName(notation); 2146 entity.setBaseURI(identifier.getBaseSystemId()); 2147 entities.setNamedItem(entity); 2148 } 2149 } 2150 2151 if (fDocumentTypeIndex != -1) { 2153 boolean found = false; 2154 int nodeIndex = fDeferredDocumentImpl.getLastChild(fDocumentTypeIndex, false); 2155 while (nodeIndex != -1) { 2156 short nodeType = fDeferredDocumentImpl.getNodeType(nodeIndex, false); 2157 if (nodeType == Node.ENTITY_NODE) { 2158 String nodeName = fDeferredDocumentImpl.getNodeName(nodeIndex, false); 2159 if (nodeName.equals(name)) { 2160 found = true; 2161 break; 2162 } 2163 } 2164 nodeIndex = fDeferredDocumentImpl.getRealPrevSibling(nodeIndex, false); 2165 } 2166 if (!found) { 2167 int entityIndex = fDeferredDocumentImpl.createDeferredEntity( 2168 name, publicId, literalSystemId, notation, identifier.getBaseSystemId()); 2169 fDeferredDocumentImpl.appendChild(fDocumentTypeIndex, entityIndex); 2170 } 2171 } 2172 2173 } 2175 2186 public void notationDecl (String name, XMLResourceIdentifier identifier, 2187 Augmentations augs) throws XNIException { 2188 2189 String publicId = identifier.getPublicId (); 2191 String literalSystemId = identifier.getLiteralSystemId (); 2192 if (fInternalSubset != null && !fInDTDExternalSubset) { 2193 fInternalSubset.append ("<!NOTATION "); 2194 fInternalSubset.append (name); 2195 if (publicId != null) { 2196 fInternalSubset.append (" PUBLIC '"); 2197 fInternalSubset.append (publicId); 2198 if (literalSystemId != null) { 2199 fInternalSubset.append ("' '"); 2200 fInternalSubset.append (literalSystemId); 2201 } 2202 } 2203 else { 2204 fInternalSubset.append (" SYSTEM '"); 2205 fInternalSubset.append (literalSystemId); 2206 } 2207 fInternalSubset.append ("'>\n"); 2208 } 2209 2210 2214 if (fDocumentImpl !=null && fDocumentType != null) { 2216 NamedNodeMap notations = fDocumentType.getNotations(); 2217 if (notations.getNamedItem(name) == null) { 2218 NotationImpl notation = (NotationImpl)fDocumentImpl.createNotation(name); 2219 notation.setPublicId(publicId); 2220 notation.setSystemId(literalSystemId); 2221 notation.setBaseURI(identifier.getBaseSystemId()); 2222 notations.setNamedItem(notation); 2223 } 2224 } 2225 2226 if (fDocumentTypeIndex != -1) { 2228 boolean found = false; 2229 int nodeIndex = fDeferredDocumentImpl.getLastChild(fDocumentTypeIndex, false); 2230 while (nodeIndex != -1) { 2231 short nodeType = fDeferredDocumentImpl.getNodeType(nodeIndex, false); 2232 if (nodeType == Node.NOTATION_NODE) { 2233 String nodeName = fDeferredDocumentImpl.getNodeName(nodeIndex, false); 2234 if (nodeName.equals(name)) { 2235 found = true; 2236 break; 2237 } 2238 } 2239 nodeIndex = fDeferredDocumentImpl.getPrevSibling(nodeIndex, false); 2240 } 2241 if (!found) { 2242 int notationIndex = fDeferredDocumentImpl.createDeferredNotation( 2243 name, publicId, literalSystemId, identifier.getBaseSystemId()); 2244 fDeferredDocumentImpl.appendChild(fDocumentTypeIndex, notationIndex); 2245 } 2246 } 2247 2248 } 2250 2259 public void ignoredCharacters (XMLString text, Augmentations augs) throws XNIException { 2260 } 2262 2263 2273 public void elementDecl (String name, String contentModel, Augmentations augs) 2274 throws XNIException { 2275 2276 if (fInternalSubset != null && !fInDTDExternalSubset) { 2278 fInternalSubset.append("<!ELEMENT "); 2279 fInternalSubset.append(name); 2280 fInternalSubset.append(' '); 2281 fInternalSubset.append(contentModel); 2282 fInternalSubset.append(">\n"); 2283 } 2284 2285 } 2287 2312 public void attributeDecl (String elementName, String attributeName, 2313 String type, String [] enumeration, 2314 String defaultType, XMLString defaultValue, 2315 XMLString nonNormalizedDefaultValue, Augmentations augs) throws XNIException { 2316 2317 if (fInternalSubset != null && !fInDTDExternalSubset) { 2319 fInternalSubset.append("<!ATTLIST "); 2320 fInternalSubset.append(elementName); 2321 fInternalSubset.append(' '); 2322 fInternalSubset.append(attributeName); 2323 fInternalSubset.append(' '); 2324 if (type.equals("ENUMERATION")) { 2325 fInternalSubset.append('('); 2326 for (int i = 0; i < enumeration.length; i++) { 2327 if (i > 0) { 2328 fInternalSubset.append('|'); 2329 } 2330 fInternalSubset.append(enumeration[i]); 2331 } 2332 fInternalSubset.append(')'); 2333 } 2334 else { 2335 fInternalSubset.append(type); 2336 } 2337 if (defaultType != null) { 2338 fInternalSubset.append(' '); 2339 fInternalSubset.append(defaultType); 2340 } 2341 if (defaultValue != null) { 2342 fInternalSubset.append(" '"); 2343 for (int i = 0; i < defaultValue.length; i++) { 2344 char c = defaultValue.ch[defaultValue.offset + i]; 2345 if (c == '\'') { 2346 fInternalSubset.append("'"); 2347 } 2348 else { 2349 fInternalSubset.append(c); 2350 } 2351 } 2352 fInternalSubset.append('\''); 2353 } 2354 fInternalSubset.append(">\n"); 2355 } 2356 2359 2360 if (fDeferredDocumentImpl != null) { 2362 2363 if (defaultValue != null) { 2365 2366 int elementDefIndex = fDeferredDocumentImpl.lookupElementDefinition (elementName); 2368 2369 if (elementDefIndex == -1) { 2371 elementDefIndex = fDeferredDocumentImpl.createDeferredElementDefinition(elementName); 2372 fDeferredDocumentImpl.appendChild(fDocumentTypeIndex, elementDefIndex); 2373 } 2374 int attrIndex = fDeferredDocumentImpl.createDeferredAttribute( 2376 attributeName, defaultValue.toString(), false); 2377 if ("ID".equals(type)) { 2378 fDeferredDocumentImpl.setIdAttribute(attrIndex); 2379 } 2380 fDeferredDocumentImpl.appendChild(elementDefIndex, attrIndex); 2382 } 2383 2384 } 2386 else if (fDocumentImpl != null) { 2388 2389 if (defaultValue != null) { 2391 2392 NamedNodeMap elements = ((DocumentTypeImpl)fDocumentType).getElements(); 2394 ElementDefinitionImpl elementDef = (ElementDefinitionImpl)elements.getNamedItem(elementName); 2395 if (elementDef == null) { 2396 elementDef = fDocumentImpl.createElementDefinition(elementName); 2397 ((DocumentTypeImpl)fDocumentType).getElements().setNamedItem(elementDef); 2398 } 2399 2400 2402 boolean nsEnabled = fNamespaceAware; 2404 AttrImpl attr; 2405 if (nsEnabled) { 2406 String namespaceURI = null; 2407 if (attributeName.startsWith("xmlns:") || 2412 attributeName.equals("xmlns")) { 2413 namespaceURI = NamespaceContext.XMLNS_URI; 2414 } 2415 attr = (AttrImpl)fDocumentImpl.createAttributeNS(namespaceURI, 2416 attributeName); 2417 } 2418 else { 2419 attr = (AttrImpl)fDocumentImpl.createAttribute(attributeName); 2420 } 2421 attr.setValue (defaultValue.toString ()); 2422 attr.setSpecified (false); 2423 attr.setIdAttribute ("ID".equals (type)); 2424 2425 if (nsEnabled){ 2427 elementDef.getAttributes().setNamedItemNS(attr); 2428 } 2429 else { 2430 elementDef.getAttributes().setNamedItem(attr); 2431 } 2432 } 2433 2434 } 2436 2437 } 2439 2440 2450 public void startAttlist(String elementName, Augmentations augs) throws XNIException { 2451 } 2453 2454 2462 public void endAttlist(Augmentations augs) throws XNIException { 2463 } 2465 2466 protected Element createElementNode(QName element) { 2469 Element el = null; 2470 2471 if (fNamespaceAware) { 2472 if (fDocumentImpl != null) { 2475 el = fDocumentImpl.createElementNS(element.uri, element.rawname, 2476 element.localpart); 2477 } 2478 else { 2479 el = fDocument.createElementNS(element.uri, element.rawname); 2480 } 2481 } 2482 else { 2483 el = fDocument.createElement(element.rawname); 2484 } 2485 2486 return el; 2487 } 2488 2489 protected Attr createAttrNode(QName attrQName) { 2492 Attr attr = null; 2493 2494 if (fNamespaceAware) { 2495 if (fDocumentImpl != null) { 2496 attr = fDocumentImpl.createAttributeNS(attrQName.uri, 2499 attrQName.rawname, 2500 attrQName.localpart); 2501 } 2502 else { 2503 attr = fDocument.createAttributeNS(attrQName.uri, 2504 attrQName.rawname); 2505 } 2506 } 2507 else { 2508 attr = fDocument.createAttribute(attrQName.rawname); 2509 } 2510 2511 return attr; 2512 } 2513 2514 2527 2528 protected void setCharacterData (boolean sawChars){ 2529 2530 fFirstChunk = sawChars; 2532 2533 2534 2535 2536 2539 Node child = fCurrentNode.getLastChild (); 2540 if (child != null) { 2541 if (fStringBuffer.length () > 0) { 2542 if (child.getNodeType() == Node.TEXT_NODE) { 2544 if (fDocumentImpl != null) { 2545 ((TextImpl)child).replaceData(fStringBuffer.toString()); 2546 } 2547 else { 2548 ((Text )child).setData(fStringBuffer.toString()); 2549 } 2550 } 2551 fStringBuffer.setLength(0); 2553 } 2554 2555 if (fDOMFilter !=null && !fInEntityRef) { 2556 if ( (child.getNodeType () == Node.TEXT_NODE ) && 2557 ((fDOMFilter.getWhatToShow () & NodeFilter.SHOW_TEXT)!= 0) ) { 2558 short code = fDOMFilter.acceptNode (child); 2559 switch (code) { 2560 case LSParserFilter.FILTER_INTERRUPT:{ 2561 throw abort; 2562 } 2563 case LSParserFilter.FILTER_REJECT:{ 2564 } 2566 case LSParserFilter.FILTER_SKIP: { 2567 fCurrentNode.removeChild(child); 2568 return; 2569 } 2570 default: { 2571 } 2573 } 2574 } 2575 } 2577 } } 2579 2580 2581 2584 public void abort () { 2585 throw abort; 2586 } 2587 2588 2589} | Popular Tags |