| 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 |