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 105 public class XML11NSDocumentScannerImpl extends XML11DocumentScannerImpl { 106 107 111 protected boolean fBindNamespaces; 112 113 117 protected boolean fPerformValidation; 118 119 122 123 private XMLDTDValidatorFilter fDTDValidator; 124 125 132 private boolean fSawSpace; 133 134 140 public void setDTDValidator(XMLDTDValidatorFilter validator) { 141 fDTDValidator = validator; 142 } 143 144 165 protected boolean scanStartElement() throws IOException , XNIException { 166 if (DEBUG_CONTENT_SCANNING) 167 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( 178 XMLMessageFormatter.XML_DOMAIN, 179 "MSG_GRAMMAR_NOT_FOUND", 180 new Object [] { rawname }, 181 XMLErrorReporter.SEVERITY_ERROR); 182 183 if (fDoctypeName == null 184 || !fDoctypeName.equals(rawname)) { 185 fErrorReporter.reportError( 186 XMLMessageFormatter.XML_DOMAIN, 187 "RootElementTypeMustMatchDoctypedecl", 188 new Object [] { fDoctypeName, rawname }, 189 XMLErrorReporter.SEVERITY_ERROR); 190 } 191 } 192 } 193 } 194 195 fCurrentElement = fElementStack.pushElement(fElementQName); 197 198 boolean empty = false; 200 fAttributes.removeAllAttributes(); 201 do { 202 boolean sawSpace = fEntityScanner.skipSpaces(); 204 205 int c = fEntityScanner.peekChar(); 207 if (c == '>') { 208 fEntityScanner.scanChar(); 209 break; 210 } else if (c == '/') { 211 fEntityScanner.scanChar(); 212 if (!fEntityScanner.skipChar('>')) { 213 reportFatalError( 214 "ElementUnterminated", 215 new Object [] { rawname }); 216 } 217 empty = true; 218 break; 219 } else if (!isValidNameStartChar(c) || !sawSpace) { 220 if (!isValidNameStartHighSurrogate(c) || !sawSpace) { 223 reportFatalError( 224 "ElementUnterminated", 225 new Object [] { rawname }); 226 } 227 } 228 229 scanAttribute(fAttributes); 231 if (fSecurityManager != null && fAttributes.getLength() > fElementAttributeLimit){ 232 fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, 233 "ElementAttributeLimit", 234 new Object []{rawname, new Integer (fAttributes.getLength()) }, 235 XMLErrorReporter.SEVERITY_FATAL_ERROR ); 236 } 237 238 } while (true); 239 240 if (fBindNamespaces) { 241 if (fElementQName.prefix == XMLSymbols.PREFIX_XMLNS) { 243 fErrorReporter.reportError( 244 XMLMessageFormatter.XMLNS_DOMAIN, 245 "ElementXMLNSPrefix", 246 new Object [] { fElementQName.rawname }, 247 XMLErrorReporter.SEVERITY_FATAL_ERROR); 248 } 249 250 String prefix = 252 fElementQName.prefix != null 253 ? fElementQName.prefix 254 : XMLSymbols.EMPTY_STRING; 255 fElementQName.uri = fNamespaceContext.getURI(prefix); 257 fCurrentElement.uri = fElementQName.uri; 259 260 if (fElementQName.prefix == null && fElementQName.uri != null) { 261 fElementQName.prefix = XMLSymbols.EMPTY_STRING; 262 fCurrentElement.prefix = XMLSymbols.EMPTY_STRING; 264 } 265 if (fElementQName.prefix != null && fElementQName.uri == null) { 266 fErrorReporter.reportError( 267 XMLMessageFormatter.XMLNS_DOMAIN, 268 "ElementPrefixUnbound", 269 new Object [] { 270 fElementQName.prefix, 271 fElementQName.rawname }, 272 XMLErrorReporter.SEVERITY_FATAL_ERROR); 273 } 274 275 int length = fAttributes.getLength(); 277 for (int i = 0; i < length; i++) { 278 fAttributes.getName(i, fAttributeQName); 279 280 String aprefix = 281 fAttributeQName.prefix != null 282 ? fAttributeQName.prefix 283 : XMLSymbols.EMPTY_STRING; 284 String uri = fNamespaceContext.getURI(aprefix); 285 if (fAttributeQName.uri != null 288 && fAttributeQName.uri == uri) { 289 continue; 290 } 291 if (aprefix != XMLSymbols.EMPTY_STRING) { 292 fAttributeQName.uri = uri; 293 if (uri == null) { 294 fErrorReporter.reportError( 295 XMLMessageFormatter.XMLNS_DOMAIN, 296 "AttributePrefixUnbound", 297 new Object [] { 298 fElementQName.rawname, 299 fAttributeQName.rawname, 300 aprefix }, 301 XMLErrorReporter.SEVERITY_FATAL_ERROR); 302 } 303 fAttributes.setURI(i, uri); 304 } 305 } 306 307 if (length > 1) { 308 QName name = fAttributes.checkDuplicatesNS(); 309 if (name != null) { 310 if (name.uri != null) { 311 fErrorReporter.reportError( 312 XMLMessageFormatter.XMLNS_DOMAIN, 313 "AttributeNSNotUnique", 314 new Object [] { 315 fElementQName.rawname, 316 name.localpart, 317 name.uri }, 318 XMLErrorReporter.SEVERITY_FATAL_ERROR); 319 } else { 320 fErrorReporter.reportError( 321 XMLMessageFormatter.XMLNS_DOMAIN, 322 "AttributeNotUnique", 323 new Object [] { 324 fElementQName.rawname, 325 name.rawname }, 326 XMLErrorReporter.SEVERITY_FATAL_ERROR); 327 } 328 } 329 } 330 } 331 332 if (fDocumentHandler != null) { 334 if (empty) { 335 336 fMarkupDepth--; 338 339 if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) { 341 reportFatalError( 342 "ElementEntityMismatch", 343 new Object [] { fCurrentElement.rawname }); 344 } 345 346 fDocumentHandler.emptyElement(fElementQName, fAttributes, null); 347 348 if (fBindNamespaces) { 349 fNamespaceContext.popContext(); 350 } 351 fElementStack.popElement(fElementQName); 353 } else { 354 fDocumentHandler.startElement(fElementQName, fAttributes, null); 355 } 356 } 357 358 if (DEBUG_CONTENT_SCANNING) 359 System.out.println("<<< scanStartElement(): " + empty); 360 return empty; 361 362 } 364 369 protected void scanStartElementName () 370 throws IOException , XNIException { 371 fEntityScanner.scanQName(fElementQName); 373 fSawSpace = fEntityScanner.skipSpaces(); 376 } 378 384 protected boolean scanStartElementAfterName() 385 throws IOException , XNIException { 386 387 String rawname = fElementQName.rawname; 389 if (fBindNamespaces) { 390 fNamespaceContext.pushContext(); 391 if (fScannerState == SCANNER_STATE_ROOT_ELEMENT) { 392 if (fPerformValidation) { 393 fErrorReporter.reportError( 394 XMLMessageFormatter.XML_DOMAIN, 395 "MSG_GRAMMAR_NOT_FOUND", 396 new Object [] { rawname }, 397 XMLErrorReporter.SEVERITY_ERROR); 398 399 if (fDoctypeName == null 400 || !fDoctypeName.equals(rawname)) { 401 fErrorReporter.reportError( 402 XMLMessageFormatter.XML_DOMAIN, 403 "RootElementTypeMustMatchDoctypedecl", 404 new Object [] { fDoctypeName, rawname }, 405 XMLErrorReporter.SEVERITY_ERROR); 406 } 407 } 408 } 409 } 410 411 fCurrentElement = fElementStack.pushElement(fElementQName); 413 414 boolean empty = false; 416 fAttributes.removeAllAttributes(); 417 do { 418 419 int c = fEntityScanner.peekChar(); 421 if (c == '>') { 422 fEntityScanner.scanChar(); 423 break; 424 } else if (c == '/') { 425 fEntityScanner.scanChar(); 426 if (!fEntityScanner.skipChar('>')) { 427 reportFatalError( 428 "ElementUnterminated", 429 new Object [] { rawname }); 430 } 431 empty = true; 432 break; 433 } else if (!isValidNameStartChar(c) || !fSawSpace) { 434 if (!isValidNameStartHighSurrogate(c) || !fSawSpace) { 437 reportFatalError( 438 "ElementUnterminated", 439 new Object [] { rawname }); 440 } 441 } 442 443 scanAttribute(fAttributes); 445 446 fSawSpace = fEntityScanner.skipSpaces(); 448 449 } while (true); 450 451 if (fBindNamespaces) { 452 if (fElementQName.prefix == XMLSymbols.PREFIX_XMLNS) { 454 fErrorReporter.reportError( 455 XMLMessageFormatter.XMLNS_DOMAIN, 456 "ElementXMLNSPrefix", 457 new Object [] { fElementQName.rawname }, 458 XMLErrorReporter.SEVERITY_FATAL_ERROR); 459 } 460 461 String prefix = 463 fElementQName.prefix != null 464 ? fElementQName.prefix 465 : XMLSymbols.EMPTY_STRING; 466 fElementQName.uri = fNamespaceContext.getURI(prefix); 468 fCurrentElement.uri = fElementQName.uri; 470 471 if (fElementQName.prefix == null && fElementQName.uri != null) { 472 fElementQName.prefix = XMLSymbols.EMPTY_STRING; 473 fCurrentElement.prefix = XMLSymbols.EMPTY_STRING; 475 } 476 if (fElementQName.prefix != null && fElementQName.uri == null) { 477 fErrorReporter.reportError( 478 XMLMessageFormatter.XMLNS_DOMAIN, 479 "ElementPrefixUnbound", 480 new Object [] { 481 fElementQName.prefix, 482 fElementQName.rawname }, 483 XMLErrorReporter.SEVERITY_FATAL_ERROR); 484 } 485 486 int length = fAttributes.getLength(); 488 for (int i = 0; i < length; i++) { 489 fAttributes.getName(i, fAttributeQName); 490 491 String aprefix = 492 fAttributeQName.prefix != null 493 ? fAttributeQName.prefix 494 : XMLSymbols.EMPTY_STRING; 495 String uri = fNamespaceContext.getURI(aprefix); 496 if (fAttributeQName.uri != null 499 && fAttributeQName.uri == uri) { 500 continue; 501 } 502 if (aprefix != XMLSymbols.EMPTY_STRING) { 503 fAttributeQName.uri = uri; 504 if (uri == null) { 505 fErrorReporter.reportError( 506 XMLMessageFormatter.XMLNS_DOMAIN, 507 "AttributePrefixUnbound", 508 new Object [] { 509 fElementQName.rawname, 510 fAttributeQName.rawname, 511 aprefix }, 512 XMLErrorReporter.SEVERITY_FATAL_ERROR); 513 } 514 fAttributes.setURI(i, uri); 515 } 516 } 517 518 if (length > 1) { 519 QName name = fAttributes.checkDuplicatesNS(); 520 if (name != null) { 521 if (name.uri != null) { 522 fErrorReporter.reportError( 523 XMLMessageFormatter.XMLNS_DOMAIN, 524 "AttributeNSNotUnique", 525 new Object [] { 526 fElementQName.rawname, 527 name.localpart, 528 name.uri }, 529 XMLErrorReporter.SEVERITY_FATAL_ERROR); 530 } else { 531 fErrorReporter.reportError( 532 XMLMessageFormatter.XMLNS_DOMAIN, 533 "AttributeNotUnique", 534 new Object [] { 535 fElementQName.rawname, 536 name.rawname }, 537 XMLErrorReporter.SEVERITY_FATAL_ERROR); 538 } 539 } 540 } 541 } 542 543 if (fDocumentHandler != null) { 545 if (empty) { 546 547 fMarkupDepth--; 549 550 if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) { 552 reportFatalError( 553 "ElementEntityMismatch", 554 new Object [] { fCurrentElement.rawname }); 555 } 556 557 fDocumentHandler.emptyElement(fElementQName, fAttributes, null); 558 559 if (fBindNamespaces) { 560 fNamespaceContext.popContext(); 561 } 562 fElementStack.popElement(fElementQName); 564 } else { 565 fDocumentHandler.startElement(fElementQName, fAttributes, null); 566 } 567 } 568 569 if (DEBUG_CONTENT_SCANNING) 570 System.out.println("<<< scanStartElementAfterName(): " + empty); 571 return empty; 572 573 } 575 592 protected void scanAttribute(XMLAttributesImpl attributes) 593 throws IOException , XNIException { 594 if (DEBUG_CONTENT_SCANNING) 595 System.out.println(">>> scanAttribute()"); 596 597 fEntityScanner.scanQName(fAttributeQName); 599 600 fEntityScanner.skipSpaces(); 602 if (!fEntityScanner.skipChar('=')) { 603 reportFatalError( 604 "EqRequiredInAttribute", 605 new Object [] { 606 fCurrentElement.rawname, 607 fAttributeQName.rawname }); 608 } 609 fEntityScanner.skipSpaces(); 610 611 int attrIndex; 613 614 if (fBindNamespaces) { 615 attrIndex = attributes.getLength(); 616 attributes.addAttributeNS( 617 fAttributeQName, 618 XMLSymbols.fCDATASymbol, 619 null); 620 } else { 621 int oldLen = attributes.getLength(); 622 attrIndex = 623 attributes.addAttribute( 624 fAttributeQName, 625 XMLSymbols.fCDATASymbol, 626 null); 627 628 if (oldLen == attributes.getLength()) { 630 reportFatalError( 631 "AttributeNotUnique", 632 new Object [] { 633 fCurrentElement.rawname, 634 fAttributeQName.rawname }); 635 } 636 } 637 638 boolean isVC = fHasExternalDTD && !fStandalone; 640 641 scanAttributeValue( 643 this.fTempString, 644 fTempString2, 645 fAttributeQName.rawname, 646 isVC, 647 fCurrentElement.rawname); 648 String value = fTempString.toString(); 649 attributes.setValue(attrIndex, value); 650 attributes.setNonNormalizedValue(attrIndex, fTempString2.toString()); 651 attributes.setSpecified(attrIndex, true); 652 653 if (fBindNamespaces) { 655 656 String localpart = fAttributeQName.localpart; 657 String prefix = 658 fAttributeQName.prefix != null 659 ? fAttributeQName.prefix 660 : XMLSymbols.EMPTY_STRING; 661 if (prefix == XMLSymbols.PREFIX_XMLNS 664 || prefix == XMLSymbols.EMPTY_STRING 665 && localpart == XMLSymbols.PREFIX_XMLNS) { 666 667 String uri = fSymbolTable.addSymbol(value); 669 670 if (prefix == XMLSymbols.PREFIX_XMLNS 672 && localpart == XMLSymbols.PREFIX_XMLNS) { 673 fErrorReporter.reportError( 674 XMLMessageFormatter.XMLNS_DOMAIN, 675 "CantBindXMLNS", 676 new Object [] { fAttributeQName }, 677 XMLErrorReporter.SEVERITY_FATAL_ERROR); 678 } 679 680 if (uri == NamespaceContext.XMLNS_URI) { 682 fErrorReporter.reportError( 683 XMLMessageFormatter.XMLNS_DOMAIN, 684 "CantBindXMLNS", 685 new Object [] { fAttributeQName }, 686 XMLErrorReporter.SEVERITY_FATAL_ERROR); 687 } 688 689 if (localpart == XMLSymbols.PREFIX_XML) { 691 if (uri != NamespaceContext.XML_URI) { 692 fErrorReporter.reportError( 693 XMLMessageFormatter.XMLNS_DOMAIN, 694 "CantBindXML", 695 new Object [] { fAttributeQName }, 696 XMLErrorReporter.SEVERITY_FATAL_ERROR); 697 } 698 } 699 else { 701 if (uri == NamespaceContext.XML_URI) { 702 fErrorReporter.reportError( 703 XMLMessageFormatter.XMLNS_DOMAIN, 704 "CantBindXML", 705 new Object [] { fAttributeQName }, 706 XMLErrorReporter.SEVERITY_FATAL_ERROR); 707 } 708 } 709 710 prefix = 711 localpart != XMLSymbols.PREFIX_XMLNS 712 ? localpart 713 : XMLSymbols.EMPTY_STRING; 714 715 fNamespaceContext.declarePrefix( 719 prefix, 720 uri.length() != 0 ? uri : null); 721 attributes.setURI( 723 attrIndex, 724 fNamespaceContext.getURI(XMLSymbols.PREFIX_XMLNS)); 725 726 } else { 727 if (fAttributeQName.prefix != null) { 729 attributes.setURI( 730 attrIndex, 731 fNamespaceContext.getURI(fAttributeQName.prefix)); 732 } 733 } 734 } 735 736 if (DEBUG_CONTENT_SCANNING) 737 System.out.println("<<< scanAttribute()"); 738 } 740 754 protected int scanEndElement() throws IOException , XNIException { 755 if (DEBUG_CONTENT_SCANNING) 756 System.out.println(">>> scanEndElement()"); 757 758 fElementStack.popElement(fElementQName); 760 761 766 768 if (!fEntityScanner.skipString(fElementQName.rawname)) { 771 reportFatalError( 772 "ETagRequired", 773 new Object [] { fElementQName.rawname }); 774 } 775 776 fEntityScanner.skipSpaces(); 778 if (!fEntityScanner.skipChar('>')) { 779 reportFatalError( 780 "ETagUnterminated", 781 new Object [] { fElementQName.rawname }); 782 } 783 fMarkupDepth--; 784 785 fMarkupDepth--; 787 788 if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) { 790 reportFatalError( 791 "ElementEntityMismatch", 792 new Object [] { fCurrentElement.rawname }); 793 } 794 795 if (fDocumentHandler != null) { 797 798 fDocumentHandler.endElement(fElementQName, null); 799 if (fBindNamespaces) { 800 fNamespaceContext.popContext(); 801 } 802 803 } 804 805 return fMarkupDepth; 806 807 } 809 public void reset(XMLComponentManager componentManager) 810 throws XMLConfigurationException { 811 812 super.reset(componentManager); 813 fPerformValidation = false; 814 fBindNamespaces = false; 815 } 816 817 818 protected Dispatcher createContentDispatcher() { 819 return new NS11ContentDispatcher(); 820 } 822 825 protected final class NS11ContentDispatcher extends ContentDispatcher { 826 839 protected boolean scanRootElementHook() 840 throws IOException , XNIException { 841 842 if (fExternalSubsetResolver != null && !fSeenDoctypeDecl 843 && !fDisallowDoctype && (fValidation || fLoadExternalDTD)) { 844 scanStartElementName(); 845 resolveExternalSubsetAndRead(); 846 reconfigurePipeline(); 847 if (scanStartElementAfterName()) { 848 setScannerState(SCANNER_STATE_TRAILING_MISC); 849 setDispatcher(fTrailingMiscDispatcher); 850 return true; 851 } 852 } 853 else { 854 reconfigurePipeline(); 855 if (scanStartElement()) { 856 setScannerState(SCANNER_STATE_TRAILING_MISC); 857 setDispatcher(fTrailingMiscDispatcher); 858 return true; 859 } 860 } 861 return false; 862 863 } 865 871 private void reconfigurePipeline() { 872 if (fDTDValidator == null) { 873 fBindNamespaces = true; 874 } 875 else if (!fDTDValidator.hasGrammar()) { 876 fBindNamespaces = true; 877 fPerformValidation = fDTDValidator.validate(); 878 XMLDocumentSource source = fDTDValidator.getDocumentSource(); 880 XMLDocumentHandler handler = fDTDValidator.getDocumentHandler(); 881 source.setDocumentHandler(handler); 882 if (handler != null) 883 handler.setDocumentSource(source); 884 fDTDValidator.setDocumentSource(null); 885 fDTDValidator.setDocumentHandler(null); 886 } 887 } } 889 } 890 | Popular Tags |