1 16 17 package org.apache.xerces.impl; 18 19 import java.io.IOException ; 20 21 import org.apache.xerces.impl.dtd.XMLDTDValidatorFilter; 22 import org.apache.xerces.impl.msg.XMLMessageFormatter; 23 import org.apache.xerces.util.XMLAttributesImpl; 24 import org.apache.xerces.util.XMLSymbols; 25 import org.apache.xerces.xni.NamespaceContext; 26 import org.apache.xerces.xni.QName; 27 import org.apache.xerces.xni.XMLDocumentHandler; 28 import org.apache.xerces.xni.XNIException; 29 import org.apache.xerces.xni.parser.XMLComponentManager; 30 import org.apache.xerces.xni.parser.XMLConfigurationException; 31 import org.apache.xerces.xni.parser.XMLDocumentSource; 32 33 65 public class XMLNSDocumentScannerImpl 66 extends XMLDocumentScannerImpl { 67 68 70 protected boolean fBindNamespaces; 71 72 74 protected boolean fPerformValidation; 75 76 79 80 private XMLDTDValidatorFilter fDTDValidator; 81 82 89 private boolean fSawSpace; 90 91 92 101 public void setDTDValidator(XMLDTDValidatorFilter dtd){ 102 fDTDValidator = dtd; 103 } 104 105 126 protected boolean scanStartElement() 127 throws IOException , XNIException { 128 if (DEBUG_CONTENT_SCANNING) System.out.println(">>> scanStartElementNS()"); 129 130 fEntityScanner.scanQName(fElementQName); 132 String rawname = fElementQName.rawname; 134 if (fBindNamespaces) { 135 fNamespaceContext.pushContext(); 136 if (fScannerState == SCANNER_STATE_ROOT_ELEMENT) { 137 if (fPerformValidation) { 138 fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, 139 "MSG_GRAMMAR_NOT_FOUND", 140 new Object []{ rawname}, 141 XMLErrorReporter.SEVERITY_ERROR); 142 143 if (fDoctypeName == null || !fDoctypeName.equals(rawname)) { 144 fErrorReporter.reportError( XMLMessageFormatter.XML_DOMAIN, 145 "RootElementTypeMustMatchDoctypedecl", 146 new Object []{fDoctypeName, rawname}, 147 XMLErrorReporter.SEVERITY_ERROR); 148 } 149 } 150 } 151 } 152 153 fCurrentElement = fElementStack.pushElement(fElementQName); 155 156 boolean empty = false; 158 fAttributes.removeAllAttributes(); 159 do { 160 boolean sawSpace = fEntityScanner.skipSpaces(); 162 163 int c = fEntityScanner.peekChar(); 165 if (c == '>') { 166 fEntityScanner.scanChar(); 167 break; 168 } 169 else if (c == '/') { 170 fEntityScanner.scanChar(); 171 if (!fEntityScanner.skipChar('>')) { 172 reportFatalError("ElementUnterminated", 173 new Object []{rawname}); 174 } 175 empty = true; 176 break; 177 } 178 else if (!isValidNameStartChar(c) || !sawSpace) { 179 reportFatalError("ElementUnterminated", new Object []{rawname}); 180 } 181 182 scanAttribute(fAttributes); 184 185 } while (true); 186 187 if (fBindNamespaces) { 188 if (fElementQName.prefix == XMLSymbols.PREFIX_XMLNS) { 190 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 191 "ElementXMLNSPrefix", 192 new Object []{fElementQName.rawname}, 193 XMLErrorReporter.SEVERITY_FATAL_ERROR); 194 } 195 196 String prefix = fElementQName.prefix != null 198 ? fElementQName.prefix : XMLSymbols.EMPTY_STRING; 199 fElementQName.uri = fNamespaceContext.getURI(prefix); 201 fCurrentElement.uri = fElementQName.uri; 203 204 if (fElementQName.prefix == null && fElementQName.uri != null) { 205 fElementQName.prefix = XMLSymbols.EMPTY_STRING; 206 fCurrentElement.prefix = XMLSymbols.EMPTY_STRING; 208 } 209 if (fElementQName.prefix != null && fElementQName.uri == null) { 210 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 211 "ElementPrefixUnbound", 212 new Object []{fElementQName.prefix, fElementQName.rawname}, 213 XMLErrorReporter.SEVERITY_FATAL_ERROR); 214 } 215 216 int length = fAttributes.getLength(); 218 for (int i = 0; i < length; i++) { 220 fAttributes.getName(i, fAttributeQName); 221 222 String aprefix = fAttributeQName.prefix != null 223 ? fAttributeQName.prefix : XMLSymbols.EMPTY_STRING; 224 String uri = fNamespaceContext.getURI(aprefix); 225 if (fAttributeQName.uri != null && fAttributeQName.uri == uri) { 228 continue; 230 } 231 if (aprefix != XMLSymbols.EMPTY_STRING) { 232 fAttributeQName.uri = uri; 233 if (uri == null) { 234 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 235 "AttributePrefixUnbound", 236 new Object []{fElementQName.rawname,fAttributeQName.rawname,aprefix}, 237 XMLErrorReporter.SEVERITY_FATAL_ERROR); 238 } 239 fAttributes.setURI(i, uri); 240 } 242 } 243 244 if (length > 1) { 245 QName name = fAttributes.checkDuplicatesNS(); 246 if (name != null) { 247 if (name.uri != null) { 248 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 249 "AttributeNSNotUnique", 250 new Object []{fElementQName.rawname, name.localpart, name.uri}, 251 XMLErrorReporter.SEVERITY_FATAL_ERROR); 252 } 253 else { 254 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 255 "AttributeNotUnique", 256 new Object []{fElementQName.rawname, name.rawname}, 257 XMLErrorReporter.SEVERITY_FATAL_ERROR); 258 } 259 } 260 } 261 } 262 263 264 if (fDocumentHandler != null) { 266 if (empty) { 267 268 fMarkupDepth--; 270 271 if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) { 273 reportFatalError("ElementEntityMismatch", 274 new Object []{fCurrentElement.rawname}); 275 } 276 277 fDocumentHandler.emptyElement(fElementQName, fAttributes, null); 278 279 if (fBindNamespaces) { 280 fNamespaceContext.popContext(); 281 } 282 fElementStack.popElement(fElementQName); 284 } else { 285 fDocumentHandler.startElement(fElementQName, fAttributes, null); 286 } 287 } 288 289 if (DEBUG_CONTENT_SCANNING) System.out.println("<<< scanStartElement(): "+empty); 290 return empty; 291 292 } 294 299 protected void scanStartElementName () 300 throws IOException , XNIException { 301 fEntityScanner.scanQName(fElementQName); 303 fSawSpace = fEntityScanner.skipSpaces(); 306 } 308 314 protected boolean scanStartElementAfterName() 315 throws IOException , XNIException { 316 317 String rawname = fElementQName.rawname; 319 if (fBindNamespaces) { 320 fNamespaceContext.pushContext(); 321 if (fScannerState == SCANNER_STATE_ROOT_ELEMENT) { 322 if (fPerformValidation) { 323 fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, 324 "MSG_GRAMMAR_NOT_FOUND", 325 new Object []{ rawname}, 326 XMLErrorReporter.SEVERITY_ERROR); 327 328 if (fDoctypeName == null || !fDoctypeName.equals(rawname)) { 329 fErrorReporter.reportError( XMLMessageFormatter.XML_DOMAIN, 330 "RootElementTypeMustMatchDoctypedecl", 331 new Object []{fDoctypeName, rawname}, 332 XMLErrorReporter.SEVERITY_ERROR); 333 } 334 } 335 } 336 } 337 338 fCurrentElement = fElementStack.pushElement(fElementQName); 340 341 boolean empty = false; 343 fAttributes.removeAllAttributes(); 344 do { 345 346 int c = fEntityScanner.peekChar(); 348 if (c == '>') { 349 fEntityScanner.scanChar(); 350 break; 351 } 352 else if (c == '/') { 353 fEntityScanner.scanChar(); 354 if (!fEntityScanner.skipChar('>')) { 355 reportFatalError("ElementUnterminated", 356 new Object []{rawname}); 357 } 358 empty = true; 359 break; 360 } 361 else if (!isValidNameStartChar(c) || !fSawSpace) { 362 reportFatalError("ElementUnterminated", new Object []{rawname}); 363 } 364 365 scanAttribute(fAttributes); 367 368 fSawSpace = fEntityScanner.skipSpaces(); 370 371 } while (true); 372 373 if (fBindNamespaces) { 374 if (fElementQName.prefix == XMLSymbols.PREFIX_XMLNS) { 376 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 377 "ElementXMLNSPrefix", 378 new Object []{fElementQName.rawname}, 379 XMLErrorReporter.SEVERITY_FATAL_ERROR); 380 } 381 382 String prefix = fElementQName.prefix != null 384 ? fElementQName.prefix : XMLSymbols.EMPTY_STRING; 385 fElementQName.uri = fNamespaceContext.getURI(prefix); 387 fCurrentElement.uri = fElementQName.uri; 389 390 if (fElementQName.prefix == null && fElementQName.uri != null) { 391 fElementQName.prefix = XMLSymbols.EMPTY_STRING; 392 fCurrentElement.prefix = XMLSymbols.EMPTY_STRING; 394 } 395 if (fElementQName.prefix != null && fElementQName.uri == null) { 396 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 397 "ElementPrefixUnbound", 398 new Object []{fElementQName.prefix, fElementQName.rawname}, 399 XMLErrorReporter.SEVERITY_FATAL_ERROR); 400 } 401 402 int length = fAttributes.getLength(); 404 for (int i = 0; i < length; i++) { 406 fAttributes.getName(i, fAttributeQName); 407 408 String aprefix = fAttributeQName.prefix != null 409 ? fAttributeQName.prefix : XMLSymbols.EMPTY_STRING; 410 String uri = fNamespaceContext.getURI(aprefix); 411 if (fAttributeQName.uri != null && fAttributeQName.uri == uri) { 414 continue; 416 } 417 if (aprefix != XMLSymbols.EMPTY_STRING) { 418 fAttributeQName.uri = uri; 419 if (uri == null) { 420 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 421 "AttributePrefixUnbound", 422 new Object []{fElementQName.rawname,fAttributeQName.rawname,aprefix}, 423 XMLErrorReporter.SEVERITY_FATAL_ERROR); 424 } 425 fAttributes.setURI(i, uri); 426 } 428 } 429 430 if (length > 1) { 431 QName name = fAttributes.checkDuplicatesNS(); 432 if (name != null) { 433 if (name.uri != null) { 434 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 435 "AttributeNSNotUnique", 436 new Object []{fElementQName.rawname, name.localpart, name.uri}, 437 XMLErrorReporter.SEVERITY_FATAL_ERROR); 438 } 439 else { 440 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 441 "AttributeNotUnique", 442 new Object []{fElementQName.rawname, name.rawname}, 443 XMLErrorReporter.SEVERITY_FATAL_ERROR); 444 } 445 } 446 } 447 } 448 449 450 if (fDocumentHandler != null) { 452 if (empty) { 453 454 fMarkupDepth--; 456 457 if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) { 459 reportFatalError("ElementEntityMismatch", 460 new Object []{fCurrentElement.rawname}); 461 } 462 463 fDocumentHandler.emptyElement(fElementQName, fAttributes, null); 464 465 if (fBindNamespaces) { 466 fNamespaceContext.popContext(); 467 } 468 fElementStack.popElement(fElementQName); 470 } else { 471 fDocumentHandler.startElement(fElementQName, fAttributes, null); 472 } 473 } 474 475 if (DEBUG_CONTENT_SCANNING) System.out.println("<<< scanStartElementAfterName(): "+empty); 476 return empty; 477 } 479 496 protected void scanAttribute(XMLAttributesImpl attributes) 497 throws IOException , XNIException { 498 if (DEBUG_CONTENT_SCANNING) System.out.println(">>> scanAttribute()"); 499 500 fEntityScanner.scanQName(fAttributeQName); 502 503 fEntityScanner.skipSpaces(); 505 if (!fEntityScanner.skipChar('=')) { 506 reportFatalError("EqRequiredInAttribute", 507 new Object []{fCurrentElement.rawname,fAttributeQName.rawname}); 508 } 509 fEntityScanner.skipSpaces(); 510 511 int attrIndex; 513 514 if (fBindNamespaces) { 515 attrIndex = attributes.getLength(); 516 attributes.addAttributeNS(fAttributeQName, XMLSymbols.fCDATASymbol, null); 517 } 518 else { 519 int oldLen = attributes.getLength(); 520 attrIndex = attributes.addAttribute(fAttributeQName, XMLSymbols.fCDATASymbol, null); 521 522 if (oldLen == attributes.getLength()) { 524 reportFatalError("AttributeNotUnique", 525 new Object []{fCurrentElement.rawname, 526 fAttributeQName.rawname}); 527 } 528 } 529 530 boolean isVC = fHasExternalDTD && !fStandalone; 532 533 boolean isSameNormalizedAttr = scanAttributeValue(this.fTempString, fTempString2, 535 fAttributeQName.rawname, isVC, fCurrentElement.rawname); 536 537 String value = fTempString.toString(); 538 attributes.setValue(attrIndex, value); 539 if (!isSameNormalizedAttr) { 541 attributes.setNonNormalizedValue(attrIndex, fTempString2.toString()); 542 } 543 attributes.setSpecified(attrIndex, true); 544 545 if (fBindNamespaces) { 547 548 String localpart = fAttributeQName.localpart; 549 String prefix = fAttributeQName.prefix != null 550 ? fAttributeQName.prefix : XMLSymbols.EMPTY_STRING; 551 if (prefix == XMLSymbols.PREFIX_XMLNS || 554 prefix == XMLSymbols.EMPTY_STRING && localpart == XMLSymbols.PREFIX_XMLNS) { 555 556 String uri = fSymbolTable.addSymbol(value); 558 559 if (prefix == XMLSymbols.PREFIX_XMLNS && localpart == XMLSymbols.PREFIX_XMLNS) { 561 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 562 "CantBindXMLNS", 563 new Object []{fAttributeQName}, 564 XMLErrorReporter.SEVERITY_FATAL_ERROR); 565 } 566 567 if (uri == NamespaceContext.XMLNS_URI) { 569 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 570 "CantBindXMLNS", 571 new Object []{fAttributeQName}, 572 XMLErrorReporter.SEVERITY_FATAL_ERROR); 573 } 574 575 if (localpart == XMLSymbols.PREFIX_XML) { 577 if (uri != NamespaceContext.XML_URI) { 578 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 579 "CantBindXML", 580 new Object []{fAttributeQName}, 581 XMLErrorReporter.SEVERITY_FATAL_ERROR); 582 } 583 } 584 else { 586 if (uri ==NamespaceContext.XML_URI) { 587 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 588 "CantBindXML", 589 new Object []{fAttributeQName}, 590 XMLErrorReporter.SEVERITY_FATAL_ERROR); 591 } 592 } 593 594 prefix = localpart != XMLSymbols.PREFIX_XMLNS ? localpart : XMLSymbols.EMPTY_STRING; 595 596 if (uri == XMLSymbols.EMPTY_STRING && localpart != XMLSymbols.PREFIX_XMLNS) { 600 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 601 "EmptyPrefixedAttName", 602 new Object []{fAttributeQName}, 603 XMLErrorReporter.SEVERITY_FATAL_ERROR); 604 } 605 606 fNamespaceContext.declarePrefix(prefix, uri.length() != 0 ? uri : null); 608 attributes.setURI(attrIndex, fNamespaceContext.getURI(XMLSymbols.PREFIX_XMLNS)); 610 611 } 612 else { 613 if (fAttributeQName.prefix != null) { 615 attributes.setURI(attrIndex, fNamespaceContext.getURI(fAttributeQName.prefix)); 616 } 617 } 618 } 619 620 if (DEBUG_CONTENT_SCANNING) System.out.println("<<< scanAttribute()"); 621 } 623 624 625 639 protected int scanEndElement() throws IOException , XNIException { 640 if (DEBUG_CONTENT_SCANNING) System.out.println(">>> scanEndElement()"); 641 642 fElementStack.popElement(fElementQName) ; 644 645 650 652 if (!fEntityScanner.skipString(fElementQName.rawname)) { 655 reportFatalError("ETagRequired", new Object []{fElementQName.rawname}); 656 } 657 658 fEntityScanner.skipSpaces(); 660 if (!fEntityScanner.skipChar('>')) { 661 reportFatalError("ETagUnterminated", 662 new Object []{fElementQName.rawname}); 663 } 664 fMarkupDepth--; 665 666 fMarkupDepth--; 668 669 if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) { 671 reportFatalError("ElementEntityMismatch", 672 new Object []{fCurrentElement.rawname}); 673 } 674 675 if (fDocumentHandler != null ) { 677 678 fDocumentHandler.endElement(fElementQName, null); 679 if (fBindNamespaces) { 680 fNamespaceContext.popContext(); 681 } 682 683 } 684 685 return fMarkupDepth; 686 687 } 689 690 public void reset(XMLComponentManager componentManager) 691 throws XMLConfigurationException { 692 693 super.reset(componentManager); 694 fPerformValidation = false; 695 fBindNamespaces = false; 696 } 697 698 699 protected Dispatcher createContentDispatcher() { 700 return new NSContentDispatcher(); 701 } 703 706 protected final class NSContentDispatcher 707 extends ContentDispatcher { 708 709 722 protected boolean scanRootElementHook() 723 throws IOException , XNIException { 724 725 if (fExternalSubsetResolver != null && !fSeenDoctypeDecl 726 && !fDisallowDoctype && (fValidation || fLoadExternalDTD)) { 727 scanStartElementName(); 728 resolveExternalSubsetAndRead(); 729 reconfigurePipeline(); 730 if (scanStartElementAfterName()) { 731 setScannerState(SCANNER_STATE_TRAILING_MISC); 732 setDispatcher(fTrailingMiscDispatcher); 733 return true; 734 } 735 } 736 else { 737 reconfigurePipeline(); 738 if (scanStartElement()) { 739 setScannerState(SCANNER_STATE_TRAILING_MISC); 740 setDispatcher(fTrailingMiscDispatcher); 741 return true; 742 } 743 } 744 return false; 745 746 } 748 754 private void reconfigurePipeline() { 755 if (fDTDValidator == null) { 756 fBindNamespaces = true; 757 } 758 else if (!fDTDValidator.hasGrammar()) { 759 fBindNamespaces = true; 760 fPerformValidation = fDTDValidator.validate(); 761 XMLDocumentSource source = fDTDValidator.getDocumentSource(); 763 XMLDocumentHandler handler = fDTDValidator.getDocumentHandler(); 764 source.setDocumentHandler(handler); 765 if (handler != null) 766 handler.setDocumentSource(source); 767 fDTDValidator.setDocumentSource(null); 768 fDTDValidator.setDocumentHandler(null); 769 } 770 } } 772 773 } | Popular Tags |