📄 xmlnamespacebinder.java
字号:
/** * Character content. * * @param text The content. * @param augs Additional information that may include infoset augmentations * * @throws XNIException Thrown by handler to signal an error. */ public void characters(XMLString text, Augmentations augs) throws XNIException { if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { fDocumentHandler.characters(text, augs); } } // characters(XMLString) /** * Ignorable whitespace. For this method to be called, the document * source must have some way of determining that the text containing * only whitespace characters should be considered ignorable. For * example, the validator can determine if a length of whitespace * characters in the document are ignorable based on the element * content model. * * @param text The ignorable whitespace. * @param augs Additional information that may include infoset augmentations * * @throws XNIException Thrown by handler to signal an error. */ public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException { if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { fDocumentHandler.ignorableWhitespace(text, augs); } } // ignorableWhitespace(XMLString) /** * The end of an element. * * @param element The name of the element. * @param augs Additional information that may include infoset augmentations * * @throws XNIException Thrown by handler to signal an error. */ public void endElement(QName element, Augmentations augs) throws XNIException { if (fNamespaces) { handleEndElement(element, augs, false); } else if (fDocumentHandler != null) { fDocumentHandler.endElement(element, augs); } } // endElement(QName) /** * The start of a CDATA section. * @param augs Additional information that may include infoset augmentations * * @throws XNIException Thrown by handler to signal an error. */ public void startCDATA(Augmentations augs) throws XNIException { if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { fDocumentHandler.startCDATA(augs); } } // startCDATA() /** * The end of a CDATA section. * @param augs Additional information that may include infoset augmentations * * @throws XNIException Thrown by handler to signal an error. */ public void endCDATA(Augmentations augs) throws XNIException { if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { fDocumentHandler.endCDATA(augs); } } // endCDATA() /** * The end of the document. * @param augs Additional information that may include infoset augmentations * * @throws XNIException Thrown by handler to signal an error. */ public void endDocument(Augmentations augs) throws XNIException { if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { fDocumentHandler.endDocument(augs); } } // endDocument() /** * This method notifies the end of a general entity. * <p> * <strong>Note:</strong> This method is not called for entity references * appearing as part of attribute values. * * @param name The name of the entity. * @param augs Additional information that may include infoset augmentations * * @exception XNIException * Thrown by handler to signal an error. */ public void endGeneralEntity(String name, Augmentations augs) throws XNIException { if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { fDocumentHandler.endGeneralEntity(name, augs); } } // endEntity(String) // // Protected methods // /** Handles start element. */ protected void handleStartElement(QName element, XMLAttributes attributes, Augmentations augs, boolean isEmpty) throws XNIException { // add new namespace context fNamespaceContext.pushContext(); if (element.prefix == XMLSymbols.PREFIX_XMLNS) { fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, "ElementXMLNSPrefix", new Object[]{element.rawname}, XMLErrorReporter.SEVERITY_FATAL_ERROR); } // search for new namespace bindings int length = attributes.getLength(); for (int i = 0; i < length; i++) { String localpart = attributes.getLocalName(i); String prefix = attributes.getPrefix(i); // when it's of form xmlns="..." or xmlns:prefix="...", // it's a namespace declaration. but prefix:xmlns="..." isn't. if (prefix == XMLSymbols.PREFIX_XMLNS || prefix == XMLSymbols.EMPTY_STRING && localpart == XMLSymbols.PREFIX_XMLNS) { // get the internalized value of this attribute String uri = fSymbolTable.addSymbol(attributes.getValue(i)); // 1. "xmlns" can't be bound to any namespace if (prefix == XMLSymbols.PREFIX_XMLNS && localpart == XMLSymbols.PREFIX_XMLNS) { fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, "CantBindXMLNS", new Object[]{attributes.getQName(i)}, XMLErrorReporter.SEVERITY_FATAL_ERROR); } // 2. the namespace for "xmlns" can't be bound to any prefix if (uri == NamespaceContext.XMLNS_URI) { fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, "CantBindXMLNS", new Object[]{attributes.getQName(i)}, XMLErrorReporter.SEVERITY_FATAL_ERROR); } // 3. "xml" can't be bound to any other namespace than it's own if (localpart == XMLSymbols.PREFIX_XML) { if (uri != NamespaceContext.XML_URI) { fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, "CantBindXML", new Object[]{attributes.getQName(i)}, XMLErrorReporter.SEVERITY_FATAL_ERROR); } } // 4. the namespace for "xml" can't be bound to any other prefix else { if (uri ==NamespaceContext.XML_URI) { fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, "CantBindXML", new Object[]{attributes.getQName(i)}, XMLErrorReporter.SEVERITY_FATAL_ERROR); } } prefix = localpart != XMLSymbols.PREFIX_XMLNS ? localpart : XMLSymbols.EMPTY_STRING; // http://www.w3.org/TR/1999/REC-xml-names-19990114/#dt-prefix // We should only report an error if there is a prefix, // that is, the local part is not "xmlns". -SG // Since this is an error condition in XML 1.0, // and should be relatively uncommon in XML 1.1, // making this test into a method call to reuse code // should be acceptable. - NG if(prefixBoundToNullURI(uri, localpart)) { fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, "EmptyPrefixedAttName", new Object[]{attributes.getQName(i)}, XMLErrorReporter.SEVERITY_FATAL_ERROR); continue; } // declare prefix in context fNamespaceContext.declarePrefix(prefix, uri.length() != 0 ? uri : null); } } // bind the element String prefix = element.prefix != null ? element.prefix : XMLSymbols.EMPTY_STRING; element.uri = fNamespaceContext.getURI(prefix); if (element.prefix == null && element.uri != null) { element.prefix = XMLSymbols.EMPTY_STRING; } if (element.prefix != null && element.uri == null) { fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, "ElementPrefixUnbound", new Object[]{element.prefix, element.rawname}, XMLErrorReporter.SEVERITY_FATAL_ERROR); } // bind the attributes for (int i = 0; i < length; i++) { attributes.getName(i, fAttributeQName); String aprefix = fAttributeQName.prefix != null ? fAttributeQName.prefix : XMLSymbols.EMPTY_STRING; String arawname = fAttributeQName.rawname; if (arawname == XMLSymbols.PREFIX_XMLNS) { fAttributeQName.uri = fNamespaceContext.getURI(XMLSymbols.PREFIX_XMLNS); attributes.setName(i, fAttributeQName); } else if (aprefix != XMLSymbols.EMPTY_STRING) { fAttributeQName.uri = fNamespaceContext.getURI(aprefix); if (fAttributeQName.uri == null) { fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, "AttributePrefixUnbound", new Object[]{element.rawname,arawname,aprefix}, XMLErrorReporter.SEVERITY_FATAL_ERROR); } attributes.setName(i, fAttributeQName); } } // verify that duplicate attributes don't exist // Example: <foo xmlns:a='NS' xmlns:b='NS' a:attr='v1' b:attr='v2'/> int attrCount = attributes.getLength(); for (int i = 0; i < attrCount - 1; i++) { String auri = attributes.getURI(i); if (auri == null || auri == NamespaceContext.XMLNS_URI) { continue; } String alocalpart = attributes.getLocalName(i); for (int j = i + 1; j < attrCount; j++) { String blocalpart = attributes.getLocalName(j); String buri = attributes.getURI(j); if (alocalpart == blocalpart && auri == buri) { fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, "AttributeNSNotUnique", new Object[]{element.rawname,alocalpart, auri}, XMLErrorReporter.SEVERITY_FATAL_ERROR); } } } // call handler if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { if (isEmpty) { fDocumentHandler.emptyElement(element, attributes, augs); } else { fDocumentHandler.startElement(element, attributes, augs); } } } // handleStartElement(QName,XMLAttributes,boolean) /** Handles end element. */ protected void handleEndElement(QName element, Augmentations augs, boolean isEmpty) throws XNIException { // bind element String eprefix = element.prefix != null ? element.prefix : XMLSymbols.EMPTY_STRING; element.uri = fNamespaceContext.getURI(eprefix); if (element.uri != null) { element.prefix = eprefix; } // call handlers if (fDocumentHandler != null && !fOnlyPassPrefixMappingEvents) { if (!isEmpty) { fDocumentHandler.endElement(element, augs); } } // pop context fNamespaceContext.popContext(); } // handleEndElement(QName,boolean) // returns true iff the given prefix is bound to "" *and* // this is disallowed by the version of XML namespaces in use. protected boolean prefixBoundToNullURI(String uri, String localpart) { return (uri == XMLSymbols.EMPTY_STRING && localpart != XMLSymbols.PREFIX_XMLNS); } // prefixBoundToNullURI(String, String): boolean} // class XMLNamespaceBinder
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -