domnormalizer.java
来自「JAVA 所有包」· Java 代码 · 共 1,336 行 · 第 1/5 页
JAVA
1,336 行
// --------------------------------------------------------- // Fix up namespaces for element: per DOM L3 // Need to consider the following cases: // // case 1: <xsl:stylesheet xmlns:xsl="http://xsl"> // We create another element body bound to the "http://xsl" namespace // as well as namespace attribute rebounding xsl to another namespace. // <xsl:body xmlns:xsl="http://another"> // Need to make sure that the new namespace decl value is changed to // "http://xsl" // // --------------------------------------------------------- // check if prefix/namespace is correct for current element // --------------------------------------------------------- uri = element.getNamespaceURI(); prefix = element.getPrefix(); if (uri != null) { // Element has a namespace uri = fSymbolTable.addSymbol(uri); prefix = (prefix == null || prefix.length() == 0) ? XMLSymbols.EMPTY_STRING :fSymbolTable.addSymbol(prefix); if (fNamespaceContext.getURI(prefix) == uri) { // The xmlns:prefix=namespace or xmlns="default" was declared at parent. // The binder always stores mapping of empty prefix to "". } else { // the prefix is either undeclared // or // conflict: the prefix is bound to another URI addNamespaceDecl(prefix, uri, element); fLocalNSBinder.declarePrefix(prefix, uri); fNamespaceContext.declarePrefix(prefix, uri); } } else { // Element has no namespace if (element.getLocalName() == null) { // Error: DOM Level 1 node! if (fNamespaceValidation) { String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "NullLocalElementName", new Object[]{element.getNodeName()}); reportDOMError(fErrorHandler, fError, fLocator, msg, DOMError.SEVERITY_FATAL_ERROR, "NullLocalElementName"); } else { String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "NullLocalElementName", new Object[]{element.getNodeName()}); reportDOMError(fErrorHandler, fError, fLocator, msg, DOMError.SEVERITY_ERROR, "NullLocalElementName"); } } else { // uri=null and no colon (DOM L2 node) uri = fNamespaceContext.getURI(XMLSymbols.EMPTY_STRING); if (uri !=null && uri.length() > 0) { // undeclare default namespace declaration (before that element // bound to non-zero length uir), but adding xmlns="" decl addNamespaceDecl (XMLSymbols.EMPTY_STRING, XMLSymbols.EMPTY_STRING, element); fLocalNSBinder.declarePrefix(XMLSymbols.EMPTY_STRING, XMLSymbols.EMPTY_STRING); fNamespaceContext.declarePrefix(XMLSymbols.EMPTY_STRING, XMLSymbols.EMPTY_STRING); } } } // ----------------------------------------- // Fix up namespaces for attributes: per DOM L3 // check if prefix/namespace is correct the attributes // ----------------------------------------- if (attributes != null) { // clone content of the attributes attributes.cloneMap(fAttributeList); for (int i = 0; i < fAttributeList.size(); i++) { Attr attr = (Attr) fAttributeList.elementAt(i); fLocator.fRelatedNode = attr; if (DEBUG) { System.out.println("==>[ns-fixup] process attribute: "+attr.getNodeName()); } // normalize attribute value attr.normalize(); value = attr.getValue(); name = attr.getNodeName(); uri = attr.getNamespaceURI(); // make sure that value is never null. if (value == null) { value=XMLSymbols.EMPTY_STRING; } if (uri != null) { // attribute has namespace !=null prefix = attr.getPrefix(); prefix = (prefix == null || prefix.length() == 0) ? XMLSymbols.EMPTY_STRING :fSymbolTable.addSymbol(prefix); /*String localpart =*/ fSymbolTable.addSymbol( attr.getLocalName()); // --------------------------------------- // skip namespace declarations // --------------------------------------- // REVISIT: can we assume that "uri" is from some symbol // table, and compare by reference? -SG if (uri != null && uri.equals(NamespaceContext.XMLNS_URI)) { continue; } //--------------------------------------- // check if value of the attribute is namespace well-formed //--------------------------------------- if (fDocument.errorChecking && ((fConfiguration.features & DOMConfigurationImpl.WELLFORMED) != 0)) { isAttrValueWF(fErrorHandler, fError, fLocator, attributes, (AttrImpl)attr, attr.getValue(), fDocument.isXML11Version()); if (fDocument.isXMLVersionChanged()){ boolean wellformed=CoreDocumentImpl.isXMLName(attr.getNodeName() , fDocument.isXML11Version()); if (!wellformed){ String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "wf-invalid-character-in-node-name", new Object[]{"Attribute", attr.getNodeName()}); reportDOMError(fErrorHandler, fError, fLocator, msg, DOMError.SEVERITY_ERROR, "wf-invalid-character-in-node-name"); } } } // --------------------------------------- // remove default attributes // --------------------------------------- /* if (removeDefault(attr, attributes)) { continue; } */ // XML 1.0 Attribute value normalization //value = normalizeAttributeValue(value, attr); // reset id-attributes ((AttrImpl)attr).setIdAttribute(false); uri = fSymbolTable.addSymbol(uri); // find if for this prefix a URI was already declared String declaredURI = fNamespaceContext.getURI(prefix); if (prefix == XMLSymbols.EMPTY_STRING || declaredURI != uri) { // attribute has no prefix (default namespace decl does not apply to attributes) // OR // attribute prefix is not declared // OR // conflict: attribute has a prefix that conficlicts with a binding // already active in scope name = attr.getNodeName(); // Find if any prefix for attributes namespace URI is available // in the scope String declaredPrefix = fNamespaceContext.getPrefix(uri); if (declaredPrefix !=null && declaredPrefix !=XMLSymbols.EMPTY_STRING) { // use the prefix that was found (declared previously for this URI prefix = declaredPrefix; } else { if (prefix != XMLSymbols.EMPTY_STRING && fLocalNSBinder.getURI(prefix) == null) { // the current prefix is not null and it has no in scope declaration // use this prefix } else { // find a prefix following the pattern "NS" +index (starting at 1) // make sure this prefix is not declared in the current scope. int counter = 1; prefix = fSymbolTable.addSymbol(PREFIX +counter++); while (fLocalNSBinder.getURI(prefix)!=null) { prefix = fSymbolTable.addSymbol(PREFIX +counter++); } } // add declaration for the new prefix addNamespaceDecl(prefix, uri, element); value = fSymbolTable.addSymbol(value); fLocalNSBinder.declarePrefix(prefix, value); fNamespaceContext.declarePrefix(prefix, uri); } // change prefix for this attribute attr.setPrefix(prefix); } } else { // attribute uri == null // XML 1.0 Attribute value normalization //value = normalizeAttributeValue(value, attr); // reset id-attributes ((AttrImpl)attr).setIdAttribute(false); if (attr.getLocalName() == null) { // It is an error if document has DOM L1 nodes. if (fNamespaceValidation) { String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "NullLocalAttrName", new Object[]{attr.getNodeName()}); reportDOMError(fErrorHandler, fError, fLocator, msg, DOMError.SEVERITY_FATAL_ERROR, "NullLocalAttrName"); } else { String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "NullLocalAttrName", new Object[]{attr.getNodeName()}); reportDOMError(fErrorHandler, fError, fLocator, msg, DOMError.SEVERITY_ERROR, "NullLocalAttrName"); } } else { // uri=null and no colon // no fix up is needed: default namespace decl does not // --------------------------------------- // remove default attributes // --------------------------------------- // removeDefault(attr, attributes); } } } } // end loop for attributes } /** * Adds a namespace attribute or replaces the value of existing namespace * attribute with the given prefix and value for URI. * In case prefix is empty will add/update default namespace declaration. * * @param prefix * @param uri * @exception IOException */ protected final void addNamespaceDecl(String prefix, String uri, ElementImpl element){ if (DEBUG) { System.out.println("[ns-fixup] addNamespaceDecl ["+prefix+"]"); } if (prefix == XMLSymbols.EMPTY_STRING) { if (DEBUG) { System.out.println("=>add xmlns=\""+uri+"\" declaration"); } element.setAttributeNS(NamespaceContext.XMLNS_URI, XMLSymbols.PREFIX_XMLNS, uri); } else { if (DEBUG) { System.out.println("=>add xmlns:"+prefix+"=\""+uri+"\" declaration"); } element.setAttributeNS(NamespaceContext.XMLNS_URI, "xmlns:"+prefix, uri); } } // // Methods for well-formness checking // /** * Check if CDATA section is well-formed * @param datavalue * @param isXML11Version = true if XML 1.1 */ public static final void isCDataWF(DOMErrorHandler errorHandler, DOMErrorImpl error, DOMLocatorImpl locator, String datavalue, boolean isXML11Version) { if (datavalue == null || (datavalue.length() == 0) ) { return; } char [] dataarray = datavalue.toCharArray();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?