nodeimpl.java

来自「JAVA 所有包」· Java 代码 · 共 1,687 行 · 第 1/5 页

JAVA
1,687
字号
     * @since DOM Level 3     */    public void setTextContent(String textContent)        throws DOMException {        setNodeValue(textContent);    }    /**     * Returns whether this node is the same node as the given one.     * <br>This method provides a way to determine whether two      * <code>Node</code> references returned by the implementation reference      * the same object. When two <code>Node</code> references are references      * to the same object, even if through a proxy, the references may be      * used completely interchangably, such that all attributes have the      * same values and calling the same DOM method on either reference      * always has exactly the same effect.     * @param other The node to test against.     * @return Returns <code>true</code> if the nodes are the same,      *   <code>false</code> otherwise.     * @since DOM Level 3     */    public boolean isSameNode(Node other) {        // we do not use any wrapper so the answer is obvious        return this == other;    }         /**     *  DOM Level 3: Experimental     *  This method checks if the specified <code>namespaceURI</code> is the      *  default namespace or not.      *  @param namespaceURI The namespace URI to look for.     *  @return  <code>true</code> if the specified <code>namespaceURI</code>      *   is the default namespace, <code>false</code> otherwise.      * @since DOM Level 3     */    public boolean isDefaultNamespace(String namespaceURI){        // REVISIT: remove casts when DOM L3 becomes REC.        short type = this.getNodeType();        switch (type) {        case Node.ELEMENT_NODE: {                         String namespace = this.getNamespaceURI();            String prefix = this.getPrefix();                        // REVISIT: is it possible that prefix is empty string?            if (prefix == null || prefix.length() == 0) {                if (namespaceURI == null) {                    return (namespace == namespaceURI);                }                return namespaceURI.equals(namespace);            }            if (this.hasAttributes()) {                ElementImpl elem = (ElementImpl)this;                NodeImpl attr = (NodeImpl)elem.getAttributeNodeNS("http://www.w3.org/2000/xmlns/", "xmlns");                if (attr != null) {                    String value = attr.getNodeValue();                    if (namespaceURI == null) {                        return (namespace == value);                    }                    return namespaceURI.equals(value);                }            }            NodeImpl ancestor = (NodeImpl)getElementAncestor(this);            if (ancestor != null) {                return ancestor.isDefaultNamespace(namespaceURI);            }            return false;        }        case Node.DOCUMENT_NODE:{                return((NodeImpl)((Document)this).getDocumentElement()).isDefaultNamespace(namespaceURI);            }        case Node.ENTITY_NODE :        case Node.NOTATION_NODE:        case Node.DOCUMENT_FRAGMENT_NODE:        case Node.DOCUMENT_TYPE_NODE:            // type is unknown            return false;        case Node.ATTRIBUTE_NODE:{                if (this.ownerNode.getNodeType() == Node.ELEMENT_NODE) {                    return ownerNode.isDefaultNamespace(namespaceURI);                }                return false;            }        default:{                   NodeImpl ancestor = (NodeImpl)getElementAncestor(this);                if (ancestor != null) {                    return ancestor.isDefaultNamespace(namespaceURI);                }                return false;            }        }    }    /**     *      * DOM Level 3 - Experimental:     * Look up the prefix associated to the given namespace URI, starting from this node.     *      * @param namespaceURI     * @return the prefix for the namespace     */    public String lookupPrefix(String namespaceURI){                // REVISIT: When Namespaces 1.1 comes out this may not be true        // Prefix can't be bound to null namespace        if (namespaceURI == null) {            return null;        }        short type = this.getNodeType();        switch (type) {        case Node.ELEMENT_NODE: {                String namespace = this.getNamespaceURI(); // to flip out children                 return lookupNamespacePrefix(namespaceURI, (ElementImpl)this);            }        case Node.DOCUMENT_NODE:{                return((NodeImpl)((Document)this).getDocumentElement()).lookupPrefix(namespaceURI);            }        case Node.ENTITY_NODE :        case Node.NOTATION_NODE:        case Node.DOCUMENT_FRAGMENT_NODE:        case Node.DOCUMENT_TYPE_NODE:            // type is unknown            return null;        case Node.ATTRIBUTE_NODE:{                if (this.ownerNode.getNodeType() == Node.ELEMENT_NODE) {                    return ownerNode.lookupPrefix(namespaceURI);                }                return null;            }        default:{                   NodeImpl ancestor = (NodeImpl)getElementAncestor(this);                if (ancestor != null) {                    return ancestor.lookupPrefix(namespaceURI);                }                return null;            }        }    }    /**     * DOM Level 3 - Experimental:     * Look up the namespace URI associated to the given prefix, starting from this node.     * Use lookupNamespaceURI(null) to lookup the default namespace     *      * @param namespaceURI     * @return th URI for the namespace     * @since DOM Level 3     */    public String lookupNamespaceURI(String specifiedPrefix) {        short type = this.getNodeType();        switch (type) {        case Node.ELEMENT_NODE : {                                  String namespace = this.getNamespaceURI();                String prefix = this.getPrefix();                if (namespace !=null) {                    // REVISIT: is it possible that prefix is empty string?                    if (specifiedPrefix== null && prefix==specifiedPrefix) {                        // looking for default namespace                        return namespace;                    } else if (prefix != null && prefix.equals(specifiedPrefix)) {                        // non default namespace                        return namespace;                    }                }                 if (this.hasAttributes()) {                    NamedNodeMap map = this.getAttributes();                    int length = map.getLength();                    for (int i=0;i<length;i++) {                        Node attr = map.item(i);                        String attrPrefix = attr.getPrefix();                        String value = attr.getNodeValue();                        namespace = attr.getNamespaceURI();                        if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) {                            // at this point we are dealing with DOM Level 2 nodes only                            if (specifiedPrefix == null &&                                attr.getNodeName().equals("xmlns")) {                                // default namespace                                return value;                            } else if (attrPrefix !=null &&                                        attrPrefix.equals("xmlns") &&                                       attr.getLocalName().equals(specifiedPrefix)) {                                // non default namespace                                return value;                            }                        }                    }                }                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);                if (ancestor != null) {                    return ancestor.lookupNamespaceURI(specifiedPrefix);                }                return null;            }        case Node.DOCUMENT_NODE : {                   return((NodeImpl)((Document)this).getDocumentElement()).lookupNamespaceURI(specifiedPrefix);            }        case Node.ENTITY_NODE :        case Node.NOTATION_NODE:        case Node.DOCUMENT_FRAGMENT_NODE:        case Node.DOCUMENT_TYPE_NODE:            // type is unknown            return null;        case Node.ATTRIBUTE_NODE:{                if (this.ownerNode.getNodeType() == Node.ELEMENT_NODE) {                    return ownerNode.lookupNamespaceURI(specifiedPrefix);                }                return null;            }        default:{                 NodeImpl ancestor = (NodeImpl)getElementAncestor(this);                if (ancestor != null) {                    return ancestor.lookupNamespaceURI(specifiedPrefix);                }                return null;            }        }    }    Node getElementAncestor (Node currentNode){        Node parent = currentNode.getParentNode();        if (parent != null) {            short type = parent.getNodeType();            if (type == Node.ELEMENT_NODE) {                return parent;            }            return getElementAncestor(parent);        }        return null;    }    String lookupNamespacePrefix(String namespaceURI, ElementImpl el){        String namespace = this.getNamespaceURI();        // REVISIT: if no prefix is available is it null or empty string, or         //          could be both?        String prefix = this.getPrefix();        if (namespace!=null && namespace.equals(namespaceURI)) {            if (prefix != null) {                String foundNamespace =  el.lookupNamespaceURI(prefix);                if (foundNamespace !=null && foundNamespace.equals(namespaceURI)) {                    return prefix;                }            }        }        if (this.hasAttributes()) {            NamedNodeMap map = this.getAttributes();            int length = map.getLength();            for (int i=0;i<length;i++) {                Node attr = map.item(i);                String attrPrefix = attr.getPrefix();                String value = attr.getNodeValue();                namespace = attr.getNamespaceURI();                if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) {                    // DOM Level 2 nodes                    if (((attr.getNodeName().equals("xmlns")) ||                         (attrPrefix !=null && attrPrefix.equals("xmlns")) &&                         value.equals(namespaceURI))) {                        String localname= attr.getLocalName();                        String foundNamespace = el.lookupNamespaceURI(localname);                        if (foundNamespace !=null && foundNamespace.equals(namespaceURI)) {                            return localname;                        }                    }                }            }        }        NodeImpl ancestor = (NodeImpl)getElementAncestor(this);        if (ancestor != null) {            return ancestor.lookupNamespacePrefix(namespaceURI, el);        }        return null;    }    /**     * Tests whether two nodes are equal.     * <br>This method tests for equality of nodes, not sameness (i.e.,      * whether the two nodes are references to the same object) which can be      * tested with <code>Node.isSameNode</code>. All nodes that are the same      * will also be equal, though the reverse may not be true.     * <br>Two nodes are equal if and only if the following conditions are      * satisfied: The two nodes are of the same type.The following string      * attributes are equal: <code>nodeName</code>, <code>localName</code>,      * <code>namespaceURI</code>, <code>prefix</code>, <code>nodeValue</code>     * , <code>baseURI</code>. This is: they are both <code>null</code>, or      * they have the same length and are character for character identical.     * The <code>attributes</code> <code>NamedNodeMaps</code> are equal.      * This is: they are both <code>null</code>, or they have the same      * length and for each node that exists in one map there is a node that      * exists in the other map and is equal, although not necessarily at the      * same index.The <code>childNodes</code> <code>NodeLists</code> are      * equal. This is: they are both <code>null</code>, or they have the      * same length and contain equal nodes at the same index. This is true      * for <code>Attr</code> nodes as for any other type of node. Note that      * normalization can affect equality; to avoid this, nodes should be      * normalized before being compared.      * <br>For two <code>DocumentType</code> nodes to be equal, the following      * conditions must also be satisfied: The following string attributes      * are equal: <code>publicId</code>, <code>systemId</code>,      * <code>internalSubset</code>.The <code>entities</code>      * <code>NamedNodeMaps</code> are equal.The <code>notations</code>      * <code>NamedNodeMaps</code> are equal.      * <br>On the other hand, the following do not affect equality: the      * <code>ownerDocument</code> attribute, the <code>specified</code>      * attribute for <code>Attr</code> nodes, the      * <code>isWhitespaceInElementContent</code> attribute for      * <code>Text</code> nodes, as well as any user data or event listeners      * registered on the nodes.     * @param arg The node to compare equality with.     * @param deep If <code>tr

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?