1 57 58 package com.sun.org.apache.xerces.internal.impl; 59 60 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 61 import com.sun.org.apache.xerces.internal.util.SymbolTable; 62 import com.sun.org.apache.xerces.internal.util.XMLSymbols; 63 import com.sun.org.apache.xerces.internal.xni.Augmentations; 64 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 65 import com.sun.org.apache.xerces.internal.xni.QName; 66 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 67 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler; 68 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 69 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 70 import com.sun.org.apache.xerces.internal.xni.XMLString; 71 import com.sun.org.apache.xerces.internal.xni.XNIException; 72 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent; 73 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 74 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 75 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentFilter; 76 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource; 77 78 96 public class XMLNamespaceBinder 97 implements XMLComponent, XMLDocumentFilter { 98 99 103 105 106 protected static final String NAMESPACES = 107 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE; 108 109 111 112 protected static final String SYMBOL_TABLE = 113 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 114 115 116 protected static final String ERROR_REPORTER = 117 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 118 119 121 122 private static final String [] RECOGNIZED_FEATURES = { 123 NAMESPACES, 124 }; 125 126 127 private static final Boolean [] FEATURE_DEFAULTS = { 128 null, 129 }; 130 131 132 private static final String [] RECOGNIZED_PROPERTIES = { 133 SYMBOL_TABLE, 134 ERROR_REPORTER, 135 }; 136 137 138 private static final Object [] PROPERTY_DEFAULTS = { 139 null, 140 null, 141 }; 142 143 147 149 150 protected boolean fNamespaces; 151 152 154 155 protected SymbolTable fSymbolTable; 156 157 158 protected XMLErrorReporter fErrorReporter; 159 160 162 163 protected XMLDocumentHandler fDocumentHandler; 164 165 protected XMLDocumentSource fDocumentSource; 166 167 169 170 protected boolean fOnlyPassPrefixMappingEvents; 171 172 174 175 private NamespaceContext fNamespaceContext; 176 177 179 180 private QName fAttributeQName = new QName(); 181 182 186 187 public XMLNamespaceBinder() { 188 } 190 194 196 205 public void setOnlyPassPrefixMappingEvents(boolean onlyPassPrefixMappingEvents) { 206 fOnlyPassPrefixMappingEvents = onlyPassPrefixMappingEvents; 207 } 209 214 public boolean getOnlyPassPrefixMappingEvents() { 215 return fOnlyPassPrefixMappingEvents; 216 } 218 222 236 public void reset(XMLComponentManager componentManager) 237 throws XNIException { 238 239 try { 241 fNamespaces = componentManager.getFeature(NAMESPACES); 242 } 243 catch (XMLConfigurationException e) { 244 fNamespaces = true; 245 } 246 247 fSymbolTable = (SymbolTable)componentManager.getProperty(SYMBOL_TABLE); 249 fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER); 250 251 } 253 258 public String [] getRecognizedFeatures() { 259 return (String [])(RECOGNIZED_FEATURES.clone()); 260 } 262 277 public void setFeature(String featureId, boolean state) 278 throws XMLConfigurationException { 279 } 281 286 public String [] getRecognizedProperties() { 287 return (String [])(RECOGNIZED_PROPERTIES.clone()); 288 } 290 296 public void setProperty(String propertyId, Object value) 297 throws XMLConfigurationException { 298 299 if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { 301 final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length(); 302 303 if (suffixLength == Constants.SYMBOL_TABLE_PROPERTY.length() && 304 propertyId.endsWith(Constants.SYMBOL_TABLE_PROPERTY)) { 305 fSymbolTable = (SymbolTable)value; 306 } 307 else if (suffixLength == Constants.ERROR_REPORTER_PROPERTY.length() && 308 propertyId.endsWith(Constants.ERROR_REPORTER_PROPERTY)) { 309 fErrorReporter = (XMLErrorReporter)value; 310 } 311 return; 312 } 313 314 } 316 325 public Boolean getFeatureDefault(String featureId) { 326 for (int i = 0; i < RECOGNIZED_FEATURES.length; i++) { 327 if (RECOGNIZED_FEATURES[i].equals(featureId)) { 328 return FEATURE_DEFAULTS[i]; 329 } 330 } 331 return null; 332 } 334 343 public Object getPropertyDefault(String propertyId) { 344 for (int i = 0; i < RECOGNIZED_PROPERTIES.length; i++) { 345 if (RECOGNIZED_PROPERTIES[i].equals(propertyId)) { 346 return PROPERTY_DEFAULTS[i]; 347 } 348 } 349 return null; 350 } 352 356 357 public void setDocumentHandler(XMLDocumentHandler documentHandler) { 358 fDocumentHandler = documentHandler; 359 } 361 362 public XMLDocumentHandler getDocumentHandler() { 363 return fDocumentHandler; 364 } 366 367 371 372 public void setDocumentSource(XMLDocumentSource source){ 373 fDocumentSource = source; 374 } 376 377 public XMLDocumentSource getDocumentSource (){ 378 return fDocumentSource; 379 } 381 382 399 public void startGeneralEntity(String name, 400 XMLResourceIdentifier identifier, 401 String encoding, Augmentations augs) 402 throws XNIException { 403 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 404 fDocumentHandler.startGeneralEntity(name, identifier, encoding, augs); 405 } 406 } 408 425 public void textDecl(String version, String encoding, Augmentations augs) 426 throws XNIException { 427 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 428 fDocumentHandler.textDecl(version, encoding, augs); 429 } 430 } 432 454 public void startDocument(XMLLocator locator, String encoding, 455 NamespaceContext namespaceContext, Augmentations augs) 456 throws XNIException { 457 fNamespaceContext = namespaceContext; 458 459 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 460 fDocumentHandler.startDocument(locator, encoding, namespaceContext, augs); 461 } 462 } 464 477 public void xmlDecl(String version, String encoding, String standalone, Augmentations augs) 478 throws XNIException { 479 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 480 fDocumentHandler.xmlDecl(version, encoding, standalone, augs); 481 } 482 } 484 496 public void doctypeDecl(String rootElement, 497 String publicId, String systemId, Augmentations augs) 498 throws XNIException { 499 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 500 fDocumentHandler.doctypeDecl(rootElement, publicId, systemId, augs); 501 } 502 } 504 512 public void comment(XMLString text, Augmentations augs) throws XNIException { 513 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 514 fDocumentHandler.comment(text, augs); 515 } 516 } 518 535 public void processingInstruction(String target, XMLString data, Augmentations augs) 536 throws XNIException { 537 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 538 fDocumentHandler.processingInstruction(target, data, augs); 539 } 540 } 542 543 558 public void startElement(QName element, XMLAttributes attributes, Augmentations augs) 559 throws XNIException { 560 561 if (fNamespaces) { 562 handleStartElement(element, attributes, augs, false); 563 } 564 else if (fDocumentHandler != null) { 565 fDocumentHandler.startElement(element, attributes, augs); 566 } 567 568 569 } 571 580 public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs) 581 throws XNIException { 582 583 if (fNamespaces) { 584 handleStartElement(element, attributes, augs, true); 585 handleEndElement(element, augs, true); 586 } 587 else if (fDocumentHandler != null) { 588 fDocumentHandler.emptyElement(element, attributes, augs); 589 } 590 591 } 593 601 public void characters(XMLString text, Augmentations augs) throws XNIException { 602 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 603 fDocumentHandler.characters(text, augs); 604 } 605 } 607 620 public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException { 621 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 622 fDocumentHandler.ignorableWhitespace(text, augs); 623 } 624 } 626 634 public void endElement(QName element, Augmentations augs) throws XNIException { 635 636 if (fNamespaces) { 637 handleEndElement(element, augs, false); 638 } 639 else if (fDocumentHandler != null) { 640 fDocumentHandler.endElement(element, augs); 641 } 642 643 } 645 651 public void startCDATA(Augmentations augs) throws XNIException { 652 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 653 fDocumentHandler.startCDATA(augs); 654 } 655 } 657 663 public void endCDATA(Augmentations augs) throws XNIException { 664 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 665 fDocumentHandler.endCDATA(augs); 666 } 667 } 669 675 public void endDocument(Augmentations augs) throws XNIException { 676 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 677 fDocumentHandler.endDocument(augs); 678 } 679 } 681 693 public void endGeneralEntity(String name, Augmentations augs) throws XNIException { 694 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 695 fDocumentHandler.endGeneralEntity(name, augs); 696 } 697 } 699 703 704 protected void handleStartElement(QName element, XMLAttributes attributes, 705 Augmentations augs, 706 boolean isEmpty) throws XNIException { 707 708 fNamespaceContext.pushContext(); 710 711 if (element.prefix == XMLSymbols.PREFIX_XMLNS) { 712 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 713 "ElementXMLNSPrefix", 714 new Object []{element.rawname}, 715 XMLErrorReporter.SEVERITY_FATAL_ERROR); 716 } 717 718 int length = attributes.getLength(); 720 for (int i = 0; i < length; i++) { 721 String localpart = attributes.getLocalName(i); 722 String prefix = attributes.getPrefix(i); 723 if (prefix == XMLSymbols.PREFIX_XMLNS || 726 prefix == XMLSymbols.EMPTY_STRING && localpart == XMLSymbols.PREFIX_XMLNS) { 727 728 String uri = fSymbolTable.addSymbol(attributes.getValue(i)); 730 731 if (prefix == XMLSymbols.PREFIX_XMLNS && localpart == XMLSymbols.PREFIX_XMLNS) { 733 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 734 "CantBindXMLNS", 735 new Object []{attributes.getQName(i)}, 736 XMLErrorReporter.SEVERITY_FATAL_ERROR); 737 } 738 739 if (uri == NamespaceContext.XMLNS_URI) { 741 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 742 "CantBindXMLNS", 743 new Object []{attributes.getQName(i)}, 744 XMLErrorReporter.SEVERITY_FATAL_ERROR); 745 } 746 747 if (localpart == XMLSymbols.PREFIX_XML) { 749 if (uri != NamespaceContext.XML_URI) { 750 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 751 "CantBindXML", 752 new Object []{attributes.getQName(i)}, 753 XMLErrorReporter.SEVERITY_FATAL_ERROR); 754 } 755 } 756 else { 758 if (uri ==NamespaceContext.XML_URI) { 759 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 760 "CantBindXML", 761 new Object []{attributes.getQName(i)}, 762 XMLErrorReporter.SEVERITY_FATAL_ERROR); 763 } 764 } 765 766 prefix = localpart != XMLSymbols.PREFIX_XMLNS ? localpart : XMLSymbols.EMPTY_STRING; 767 768 if(prefixBoundToNullURI(uri, localpart)) { 776 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 777 "EmptyPrefixedAttName", 778 new Object []{attributes.getQName(i)}, 779 XMLErrorReporter.SEVERITY_FATAL_ERROR); 780 continue; 781 } 782 783 fNamespaceContext.declarePrefix(prefix, uri.length() != 0 ? uri : null); 785 786 } 787 } 788 789 String prefix = element.prefix != null 791 ? element.prefix : XMLSymbols.EMPTY_STRING; 792 element.uri = fNamespaceContext.getURI(prefix); 793 if (element.prefix == null && element.uri != null) { 794 element.prefix = XMLSymbols.EMPTY_STRING; 795 } 796 if (element.prefix != null && element.uri == null) { 797 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 798 "ElementPrefixUnbound", 799 new Object []{element.prefix, element.rawname}, 800 XMLErrorReporter.SEVERITY_FATAL_ERROR); 801 } 802 803 for (int i = 0; i < length; i++) { 805 attributes.getName(i, fAttributeQName); 806 String aprefix = fAttributeQName.prefix != null 807 ? fAttributeQName.prefix : XMLSymbols.EMPTY_STRING; 808 String arawname = fAttributeQName.rawname; 809 if (arawname == XMLSymbols.PREFIX_XMLNS) { 810 fAttributeQName.uri = fNamespaceContext.getURI(XMLSymbols.PREFIX_XMLNS); 811 attributes.setName(i, fAttributeQName); 812 } 813 else if (aprefix != XMLSymbols.EMPTY_STRING) { 814 fAttributeQName.uri = fNamespaceContext.getURI(aprefix); 815 if (fAttributeQName.uri == null) { 816 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 817 "AttributePrefixUnbound", 818 new Object []{element.rawname,arawname,aprefix}, 819 XMLErrorReporter.SEVERITY_FATAL_ERROR); 820 } 821 attributes.setName(i, fAttributeQName); 822 } 823 } 824 825 int attrCount = attributes.getLength(); 828 for (int i = 0; i < attrCount - 1; i++) { 829 String auri = attributes.getURI(i); 830 if (auri == null || auri == NamespaceContext.XMLNS_URI) { 831 continue; 832 } 833 String alocalpart = attributes.getLocalName(i); 834 for (int j = i + 1; j < attrCount; j++) { 835 String blocalpart = attributes.getLocalName(j); 836 String buri = attributes.getURI(j); 837 if (alocalpart == blocalpart && auri == buri) { 838 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 839 "AttributeNSNotUnique", 840 new Object []{element.rawname,alocalpart, auri}, 841 XMLErrorReporter.SEVERITY_FATAL_ERROR); 842 } 843 } 844 } 845 846 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 848 if (isEmpty) { 849 fDocumentHandler.emptyElement(element, attributes, augs); 850 } 851 else { 852 fDocumentHandler.startElement(element, attributes, augs); 853 } 854 } 855 856 857 } 859 860 protected void handleEndElement(QName element, Augmentations augs, boolean isEmpty) 861 throws XNIException { 862 863 String eprefix = element.prefix != null ? element.prefix : XMLSymbols.EMPTY_STRING; 865 element.uri = fNamespaceContext.getURI(eprefix); 866 if (element.uri != null) { 867 element.prefix = eprefix; 868 } 869 870 if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { 872 if (!isEmpty) { 873 fDocumentHandler.endElement(element, augs); 874 } 875 } 876 877 fNamespaceContext.popContext(); 879 880 } 882 protected boolean prefixBoundToNullURI(String uri, String localpart) { 885 return (uri == XMLSymbols.EMPTY_STRING && localpart != XMLSymbols.PREFIX_XMLNS); 886 } 888 } | Popular Tags |