1 57 58 package com.sun.org.apache.xerces.internal.impl; 59 60 import java.io.IOException ; 61 62 import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidatorFilter; 63 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 64 import com.sun.org.apache.xerces.internal.util.XMLAttributesImpl; 65 import com.sun.org.apache.xerces.internal.util.XMLSymbols; 66 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 67 import com.sun.org.apache.xerces.internal.xni.QName; 68 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler; 69 import com.sun.org.apache.xerces.internal.xni.XNIException; 70 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 71 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 72 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource; 73 74 104 public class XMLNSDocumentScannerImpl 105 extends XMLDocumentScannerImpl { 106 107 109 protected boolean fBindNamespaces; 110 111 113 protected boolean fPerformValidation; 114 115 118 119 private XMLDTDValidatorFilter fDTDValidator; 120 121 128 private boolean fSawSpace; 129 130 131 140 public void setDTDValidator(XMLDTDValidatorFilter dtd){ 141 fDTDValidator = dtd; 142 } 143 144 165 protected boolean scanStartElement() 166 throws IOException , XNIException { 167 if (DEBUG_CONTENT_SCANNING) System.out.println(">>> scanStartElementNS()"); 168 169 fEntityScanner.scanQName(fElementQName); 171 String rawname = fElementQName.rawname; 173 if (fBindNamespaces) { 174 fNamespaceContext.pushContext(); 175 if (fScannerState == SCANNER_STATE_ROOT_ELEMENT) { 176 if (fPerformValidation) { 177 fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, 178 "MSG_GRAMMAR_NOT_FOUND", 179 new Object []{ rawname}, 180 XMLErrorReporter.SEVERITY_ERROR); 181 182 if (fDoctypeName == null || !fDoctypeName.equals(rawname)) { 183 fErrorReporter.reportError( XMLMessageFormatter.XML_DOMAIN, 184 "RootElementTypeMustMatchDoctypedecl", 185 new Object []{fDoctypeName, rawname}, 186 XMLErrorReporter.SEVERITY_ERROR); 187 } 188 } 189 } 190 } 191 192 fCurrentElement = fElementStack.pushElement(fElementQName); 194 195 boolean empty = false; 197 fAttributes.removeAllAttributes(); 198 do { 199 boolean sawSpace = fEntityScanner.skipSpaces(); 201 202 int c = fEntityScanner.peekChar(); 204 if (c == '>') { 205 fEntityScanner.scanChar(); 206 break; 207 } 208 else if (c == '/') { 209 fEntityScanner.scanChar(); 210 if (!fEntityScanner.skipChar('>')) { 211 reportFatalError("ElementUnterminated", 212 new Object []{rawname}); 213 } 214 empty = true; 215 break; 216 } 217 else if (!isValidNameStartChar(c) || !sawSpace) { 218 reportFatalError("ElementUnterminated", new Object []{rawname}); 219 } 220 221 scanAttribute(fAttributes); 223 if (fSecurityManager != null && fAttributes.getLength() > fElementAttributeLimit){ 224 fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, 225 "ElementAttributeLimit", 226 new Object []{rawname, new Integer (fAttributes.getLength()) }, 227 XMLErrorReporter.SEVERITY_FATAL_ERROR ); 228 } 229 230 } while (true); 231 232 if (fBindNamespaces) { 233 if (fElementQName.prefix == XMLSymbols.PREFIX_XMLNS) { 235 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 236 "ElementXMLNSPrefix", 237 new Object []{fElementQName.rawname}, 238 XMLErrorReporter.SEVERITY_FATAL_ERROR); 239 } 240 241 String prefix = fElementQName.prefix != null 243 ? fElementQName.prefix : XMLSymbols.EMPTY_STRING; 244 fElementQName.uri = fNamespaceContext.getURI(prefix); 246 fCurrentElement.uri = fElementQName.uri; 248 249 if (fElementQName.prefix == null && fElementQName.uri != null) { 250 fElementQName.prefix = XMLSymbols.EMPTY_STRING; 251 fCurrentElement.prefix = XMLSymbols.EMPTY_STRING; 253 } 254 if (fElementQName.prefix != null && fElementQName.uri == null) { 255 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 256 "ElementPrefixUnbound", 257 new Object []{fElementQName.prefix, fElementQName.rawname}, 258 XMLErrorReporter.SEVERITY_FATAL_ERROR); 259 } 260 261 int length = fAttributes.getLength(); 263 for (int i = 0; i < length; i++) { 265 fAttributes.getName(i, fAttributeQName); 266 267 String aprefix = fAttributeQName.prefix != null 268 ? fAttributeQName.prefix : XMLSymbols.EMPTY_STRING; 269 String uri = fNamespaceContext.getURI(aprefix); 270 if (fAttributeQName.uri != null && fAttributeQName.uri == uri) { 273 continue; 275 } 276 if (aprefix != XMLSymbols.EMPTY_STRING) { 277 fAttributeQName.uri = uri; 278 if (uri == null) { 279 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 280 "AttributePrefixUnbound", 281 new Object []{fElementQName.rawname,fAttributeQName.rawname,aprefix}, 282 XMLErrorReporter.SEVERITY_FATAL_ERROR); 283 } 284 fAttributes.setURI(i, uri); 285 } 287 } 288 289 if (length > 1) { 290 QName name = fAttributes.checkDuplicatesNS(); 291 if (name != null) { 292 if (name.uri != null) { 293 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 294 "AttributeNSNotUnique", 295 new Object []{fElementQName.rawname, name.localpart, name.uri}, 296 XMLErrorReporter.SEVERITY_FATAL_ERROR); 297 } 298 else { 299 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 300 "AttributeNotUnique", 301 new Object []{fElementQName.rawname, name.rawname}, 302 XMLErrorReporter.SEVERITY_FATAL_ERROR); 303 } 304 } 305 } 306 } 307 308 309 if (fDocumentHandler != null) { 311 if (empty) { 312 313 fMarkupDepth--; 315 316 if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) { 318 reportFatalError("ElementEntityMismatch", 319 new Object []{fCurrentElement.rawname}); 320 } 321 322 fDocumentHandler.emptyElement(fElementQName, fAttributes, null); 323 324 if (fBindNamespaces) { 325 fNamespaceContext.popContext(); 326 } 327 fElementStack.popElement(fElementQName); 329 } else { 330 fDocumentHandler.startElement(fElementQName, fAttributes, null); 331 } 332 } 333 334 if (DEBUG_CONTENT_SCANNING) System.out.println("<<< scanStartElement(): "+empty); 335 return empty; 336 337 } 339 344 protected void scanStartElementName () 345 throws IOException , XNIException { 346 fEntityScanner.scanQName(fElementQName); 348 fSawSpace = fEntityScanner.skipSpaces(); 351 } 353 359 protected boolean scanStartElementAfterName() 360 throws IOException , XNIException { 361 362 String rawname = fElementQName.rawname; 364 if (fBindNamespaces) { 365 fNamespaceContext.pushContext(); 366 if (fScannerState == SCANNER_STATE_ROOT_ELEMENT) { 367 if (fPerformValidation) { 368 fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, 369 "MSG_GRAMMAR_NOT_FOUND", 370 new Object []{ rawname}, 371 XMLErrorReporter.SEVERITY_ERROR); 372 373 if (fDoctypeName == null || !fDoctypeName.equals(rawname)) { 374 fErrorReporter.reportError( XMLMessageFormatter.XML_DOMAIN, 375 "RootElementTypeMustMatchDoctypedecl", 376 new Object []{fDoctypeName, rawname}, 377 XMLErrorReporter.SEVERITY_ERROR); 378 } 379 } 380 } 381 } 382 383 fCurrentElement = fElementStack.pushElement(fElementQName); 385 386 boolean empty = false; 388 fAttributes.removeAllAttributes(); 389 do { 390 391 int c = fEntityScanner.peekChar(); 393 if (c == '>') { 394 fEntityScanner.scanChar(); 395 break; 396 } 397 else if (c == '/') { 398 fEntityScanner.scanChar(); 399 if (!fEntityScanner.skipChar('>')) { 400 reportFatalError("ElementUnterminated", 401 new Object []{rawname}); 402 } 403 empty = true; 404 break; 405 } 406 else if (!isValidNameStartChar(c) || !fSawSpace) { 407 reportFatalError("ElementUnterminated", new Object []{rawname}); 408 } 409 410 scanAttribute(fAttributes); 412 413 fSawSpace = fEntityScanner.skipSpaces(); 415 416 } while (true); 417 418 if (fBindNamespaces) { 419 if (fElementQName.prefix == XMLSymbols.PREFIX_XMLNS) { 421 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 422 "ElementXMLNSPrefix", 423 new Object []{fElementQName.rawname}, 424 XMLErrorReporter.SEVERITY_FATAL_ERROR); 425 } 426 427 String prefix = fElementQName.prefix != null 429 ? fElementQName.prefix : XMLSymbols.EMPTY_STRING; 430 fElementQName.uri = fNamespaceContext.getURI(prefix); 432 fCurrentElement.uri = fElementQName.uri; 434 435 if (fElementQName.prefix == null && fElementQName.uri != null) { 436 fElementQName.prefix = XMLSymbols.EMPTY_STRING; 437 fCurrentElement.prefix = XMLSymbols.EMPTY_STRING; 439 } 440 if (fElementQName.prefix != null && fElementQName.uri == null) { 441 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 442 "ElementPrefixUnbound", 443 new Object []{fElementQName.prefix, fElementQName.rawname}, 444 XMLErrorReporter.SEVERITY_FATAL_ERROR); 445 } 446 447 int length = fAttributes.getLength(); 449 for (int i = 0; i < length; i++) { 451 fAttributes.getName(i, fAttributeQName); 452 453 String aprefix = fAttributeQName.prefix != null 454 ? fAttributeQName.prefix : XMLSymbols.EMPTY_STRING; 455 String uri = fNamespaceContext.getURI(aprefix); 456 if (fAttributeQName.uri != null && fAttributeQName.uri == uri) { 459 continue; 461 } 462 if (aprefix != XMLSymbols.EMPTY_STRING) { 463 fAttributeQName.uri = uri; 464 if (uri == null) { 465 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 466 "AttributePrefixUnbound", 467 new Object []{fElementQName.rawname,fAttributeQName.rawname,aprefix}, 468 XMLErrorReporter.SEVERITY_FATAL_ERROR); 469 } 470 fAttributes.setURI(i, uri); 471 } 473 } 474 475 if (length > 1) { 476 QName name = fAttributes.checkDuplicatesNS(); 477 if (name != null) { 478 if (name.uri != null) { 479 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 480 "AttributeNSNotUnique", 481 new Object []{fElementQName.rawname, name.localpart, name.uri}, 482 XMLErrorReporter.SEVERITY_FATAL_ERROR); 483 } 484 else { 485 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 486 "AttributeNotUnique", 487 new Object []{fElementQName.rawname, name.rawname}, 488 XMLErrorReporter.SEVERITY_FATAL_ERROR); 489 } 490 } 491 } 492 } 493 494 495 if (fDocumentHandler != null) { 497 if (empty) { 498 499 fMarkupDepth--; 501 502 if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) { 504 reportFatalError("ElementEntityMismatch", 505 new Object []{fCurrentElement.rawname}); 506 } 507 508 fDocumentHandler.emptyElement(fElementQName, fAttributes, null); 509 510 if (fBindNamespaces) { 511 fNamespaceContext.popContext(); 512 } 513 fElementStack.popElement(fElementQName); 515 } else { 516 fDocumentHandler.startElement(fElementQName, fAttributes, null); 517 } 518 } 519 520 if (DEBUG_CONTENT_SCANNING) System.out.println("<<< scanStartElementAfterName(): "+empty); 521 return empty; 522 } 524 541 protected void scanAttribute(XMLAttributesImpl attributes) 542 throws IOException , XNIException { 543 if (DEBUG_CONTENT_SCANNING) System.out.println(">>> scanAttribute()"); 544 545 fEntityScanner.scanQName(fAttributeQName); 547 548 fEntityScanner.skipSpaces(); 550 if (!fEntityScanner.skipChar('=')) { 551 reportFatalError("EqRequiredInAttribute", 552 new Object []{fCurrentElement.rawname,fAttributeQName.rawname}); 553 } 554 fEntityScanner.skipSpaces(); 555 556 int attrIndex; 558 559 if (fBindNamespaces) { 560 attrIndex = attributes.getLength(); 561 attributes.addAttributeNS(fAttributeQName, XMLSymbols.fCDATASymbol, null); 562 } 563 else { 564 int oldLen = attributes.getLength(); 565 attrIndex = attributes.addAttribute(fAttributeQName, XMLSymbols.fCDATASymbol, null); 566 567 if (oldLen == attributes.getLength()) { 569 reportFatalError("AttributeNotUnique", 570 new Object []{fCurrentElement.rawname, 571 fAttributeQName.rawname}); 572 } 573 } 574 575 boolean isVC = fHasExternalDTD && !fStandalone; 577 578 scanAttributeValue(this.fTempString, fTempString2, 579 fAttributeQName.rawname, isVC, 580 fCurrentElement.rawname); 581 String value = fTempString.toString(); 582 attributes.setValue(attrIndex, value); 583 attributes.setNonNormalizedValue(attrIndex, fTempString2.toString()); 584 attributes.setSpecified(attrIndex, true); 585 586 if (fBindNamespaces) { 588 589 String localpart = fAttributeQName.localpart; 590 String prefix = fAttributeQName.prefix != null 591 ? fAttributeQName.prefix : XMLSymbols.EMPTY_STRING; 592 if (prefix == XMLSymbols.PREFIX_XMLNS || 595 prefix == XMLSymbols.EMPTY_STRING && localpart == XMLSymbols.PREFIX_XMLNS) { 596 597 String uri = fSymbolTable.addSymbol(value); 599 600 if (prefix == XMLSymbols.PREFIX_XMLNS && localpart == XMLSymbols.PREFIX_XMLNS) { 602 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 603 "CantBindXMLNS", 604 new Object []{fAttributeQName}, 605 XMLErrorReporter.SEVERITY_FATAL_ERROR); 606 } 607 608 if (uri == NamespaceContext.XMLNS_URI) { 610 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 611 "CantBindXMLNS", 612 new Object []{fAttributeQName}, 613 XMLErrorReporter.SEVERITY_FATAL_ERROR); 614 } 615 616 if (localpart == XMLSymbols.PREFIX_XML) { 618 if (uri != NamespaceContext.XML_URI) { 619 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 620 "CantBindXML", 621 new Object []{fAttributeQName}, 622 XMLErrorReporter.SEVERITY_FATAL_ERROR); 623 } 624 } 625 else { 627 if (uri ==NamespaceContext.XML_URI) { 628 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 629 "CantBindXML", 630 new Object []{fAttributeQName}, 631 XMLErrorReporter.SEVERITY_FATAL_ERROR); 632 } 633 } 634 635 prefix = localpart != XMLSymbols.PREFIX_XMLNS ? localpart : XMLSymbols.EMPTY_STRING; 636 637 if (uri == XMLSymbols.EMPTY_STRING && localpart != XMLSymbols.PREFIX_XMLNS) { 641 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 642 "EmptyPrefixedAttName", 643 new Object []{fAttributeQName}, 644 XMLErrorReporter.SEVERITY_FATAL_ERROR); 645 } 646 647 fNamespaceContext.declarePrefix(prefix, uri.length() != 0 ? uri : null); 649 attributes.setURI(attrIndex, fNamespaceContext.getURI(XMLSymbols.PREFIX_XMLNS)); 651 652 } 653 else { 654 if (fAttributeQName.prefix != null) { 656 attributes.setURI(attrIndex, fNamespaceContext.getURI(fAttributeQName.prefix)); 657 } 658 } 659 } 660 661 if (DEBUG_CONTENT_SCANNING) System.out.println("<<< scanAttribute()"); 662 } 664 665 666 680 protected int scanEndElement() throws IOException , XNIException { 681 if (DEBUG_CONTENT_SCANNING) System.out.println(">>> scanEndElement()"); 682 683 fElementStack.popElement(fElementQName) ; 685 686 691 693 if (!fEntityScanner.skipString(fElementQName.rawname)) { 696 reportFatalError("ETagRequired", new Object []{fElementQName.rawname}); 697 } 698 699 fEntityScanner.skipSpaces(); 701 if (!fEntityScanner.skipChar('>')) { 702 reportFatalError("ETagUnterminated", 703 new Object []{fElementQName.rawname}); 704 } 705 fMarkupDepth--; 706 707 fMarkupDepth--; 709 710 if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) { 712 reportFatalError("ElementEntityMismatch", 713 new Object []{fCurrentElement.rawname}); 714 } 715 716 if (fDocumentHandler != null ) { 718 719 fDocumentHandler.endElement(fElementQName, null); 720 if (fBindNamespaces) { 721 fNamespaceContext.popContext(); 722 } 723 724 } 725 726 return fMarkupDepth; 727 728 } 730 731 public void reset(XMLComponentManager componentManager) 732 throws XMLConfigurationException { 733 734 super.reset(componentManager); 735 fPerformValidation = false; 736 fBindNamespaces = false; 737 } 738 739 740 protected Dispatcher createContentDispatcher() { 741 return new NSContentDispatcher(); 742 } 744 747 protected final class NSContentDispatcher 748 extends ContentDispatcher { 749 750 763 protected boolean scanRootElementHook() 764 throws IOException , XNIException { 765 766 if (fExternalSubsetResolver != null && !fSeenDoctypeDecl 767 && !fDisallowDoctype && (fValidation || fLoadExternalDTD)) { 768 scanStartElementName(); 769 resolveExternalSubsetAndRead(); 770 reconfigurePipeline(); 771 if (scanStartElementAfterName()) { 772 setScannerState(SCANNER_STATE_TRAILING_MISC); 773 setDispatcher(fTrailingMiscDispatcher); 774 return true; 775 } 776 } 777 else { 778 reconfigurePipeline(); 779 if (scanStartElement()) { 780 setScannerState(SCANNER_STATE_TRAILING_MISC); 781 setDispatcher(fTrailingMiscDispatcher); 782 return true; 783 } 784 } 785 return false; 786 787 } 789 795 private void reconfigurePipeline() { 796 if (fDTDValidator == null) { 797 fBindNamespaces = true; 798 } 799 else if (!fDTDValidator.hasGrammar()) { 800 fBindNamespaces = true; 801 fPerformValidation = fDTDValidator.validate(); 802 XMLDocumentSource source = fDTDValidator.getDocumentSource(); 804 XMLDocumentHandler handler = fDTDValidator.getDocumentHandler(); 805 source.setDocumentHandler(handler); 806 if (handler != null) 807 handler.setDocumentSource(source); 808 fDTDValidator.setDocumentSource(null); 809 fDTDValidator.setDocumentHandler(null); 810 } 811 } } 813 814 } | Popular Tags |