nodeimpl.java
来自「JAVA 所有包」· Java 代码 · 共 1,687 行 · 第 1/5 页
JAVA
1,687 行
// Find the ancestor of each node, and the distance each node is from // its ancestor. // During this traversal, look for ancestor/descendent relationships // between the 2 nodes in question. // We do this now, so that we get this info correct for attribute nodes // and their children. Node node; Node thisAncestor = this; Node otherAncestor = other; int thisDepth=0; int otherDepth=0; for (node=this; node != null; node = node.getParentNode()) { thisDepth +=1; if (node == other) // The other node is an ancestor of this one. return (DOCUMENT_POSITION_CONTAINS | DOCUMENT_POSITION_PRECEDING); thisAncestor = node; } for (node=other; node!=null; node=node.getParentNode()) { otherDepth +=1; if (node == this) // The other node is a descendent of the reference node. return (DOCUMENT_POSITION_IS_CONTAINED | DOCUMENT_POSITION_FOLLOWING); otherAncestor = node; } int thisAncestorType = thisAncestor.getNodeType(); int otherAncestorType = otherAncestor.getNodeType(); Node thisNode = this; Node otherNode = other; // Special casing for ENTITY, NOTATION, DOCTYPE and ATTRIBUTES // LM: should rewrite this. switch (thisAncestorType) { case Node.NOTATION_NODE: case Node.ENTITY_NODE: { DocumentType container = thisOwnerDoc.getDoctype(); if (container == otherAncestor) return (DOCUMENT_POSITION_CONTAINS | DOCUMENT_POSITION_PRECEDING); switch (otherAncestorType) { case Node.NOTATION_NODE: case Node.ENTITY_NODE: { if (thisAncestorType != otherAncestorType) // the nodes are of different types return ((thisAncestorType>otherAncestorType) ? DOCUMENT_POSITION_PRECEDING:DOCUMENT_POSITION_FOLLOWING); else { // the nodes are of the same type. Find order. if (thisAncestorType == Node.NOTATION_NODE) if (((NamedNodeMapImpl)container.getNotations()).precedes(otherAncestor,thisAncestor)) return (DOCUMENT_POSITION_PRECEDING | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); else return (DOCUMENT_POSITION_FOLLOWING | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); else if (((NamedNodeMapImpl)container.getEntities()).precedes(otherAncestor,thisAncestor)) return (DOCUMENT_POSITION_PRECEDING | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); else return (DOCUMENT_POSITION_FOLLOWING | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); } } } thisNode = thisAncestor = thisOwnerDoc; break; } case Node.DOCUMENT_TYPE_NODE: { if (otherNode == thisOwnerDoc) return (DOCUMENT_POSITION_PRECEDING | DOCUMENT_POSITION_CONTAINS); else if (thisOwnerDoc!=null && thisOwnerDoc==otherOwnerDoc) return (DOCUMENT_POSITION_FOLLOWING); break; } case Node.ATTRIBUTE_NODE: { thisNode = ((AttrImpl)thisAncestor).getOwnerElement(); if (otherAncestorType==Node.ATTRIBUTE_NODE) { otherNode = ((AttrImpl)otherAncestor).getOwnerElement(); if (otherNode == thisNode) { if (((NamedNodeMapImpl)thisNode.getAttributes()).precedes(other,this)) return (DOCUMENT_POSITION_PRECEDING | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); else return (DOCUMENT_POSITION_FOLLOWING | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); } } // Now, find the ancestor of the element thisDepth=0; for (node=thisNode; node != null; node=node.getParentNode()) { thisDepth +=1; if (node == otherNode) { // The other node is an ancestor of the owning element return (DOCUMENT_POSITION_CONTAINS | DOCUMENT_POSITION_PRECEDING); } thisAncestor = node; } } } switch (otherAncestorType) { case Node.NOTATION_NODE: case Node.ENTITY_NODE: { DocumentType container = thisOwnerDoc.getDoctype(); if (container == this) return (DOCUMENT_POSITION_IS_CONTAINED | DOCUMENT_POSITION_FOLLOWING); otherNode = otherAncestor = thisOwnerDoc; break; } case Node.DOCUMENT_TYPE_NODE: { if (thisNode == otherOwnerDoc) return (DOCUMENT_POSITION_FOLLOWING | DOCUMENT_POSITION_IS_CONTAINED); else if (otherOwnerDoc!=null && thisOwnerDoc==otherOwnerDoc) return (DOCUMENT_POSITION_PRECEDING); break; } case Node.ATTRIBUTE_NODE: { otherDepth=0; otherNode = ((AttrImpl)otherAncestor).getOwnerElement(); for (node=otherNode; node != null; node=node.getParentNode()) { otherDepth +=1; if (node == thisNode) // The other node is a descendent of the reference // node's element return DOCUMENT_POSITION_FOLLOWING | DOCUMENT_POSITION_IS_CONTAINED; otherAncestor = node; } } } // thisAncestor and otherAncestor must be the same at this point, // otherwise, the original nodes are disconnected if (thisAncestor != otherAncestor) { int thisAncestorNum, otherAncestorNum; thisAncestorNum = ((NodeImpl)thisAncestor).getNodeNumber(); otherAncestorNum = ((NodeImpl)otherAncestor).getNodeNumber(); if (thisAncestorNum > otherAncestorNum) return DOCUMENT_POSITION_DISCONNECTED | DOCUMENT_POSITION_FOLLOWING | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC; else return DOCUMENT_POSITION_DISCONNECTED | DOCUMENT_POSITION_PRECEDING | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC; } // Go up the parent chain of the deeper node, until we find a node // with the same depth as the shallower node if (thisDepth > otherDepth) { for (int i=0; i<thisDepth - otherDepth; i++) thisNode = thisNode.getParentNode(); // Check if the node we have reached is in fact "otherNode". This can // happen in the case of attributes. In this case, otherNode // "precedes" this. if (thisNode == otherNode) { return DOCUMENT_POSITION_PRECEDING; } } else { for (int i=0; i<otherDepth - thisDepth; i++) otherNode = otherNode.getParentNode(); // Check if the node we have reached is in fact "thisNode". This can // happen in the case of attributes. In this case, otherNode // "follows" this. if (otherNode == thisNode) return DOCUMENT_POSITION_FOLLOWING; } // We now have nodes at the same depth in the tree. Find a common // ancestor. Node thisNodeP, otherNodeP; for (thisNodeP=thisNode.getParentNode(), otherNodeP=otherNode.getParentNode(); thisNodeP!=otherNodeP;) { thisNode = thisNodeP; otherNode = otherNodeP; thisNodeP = thisNodeP.getParentNode(); otherNodeP = otherNodeP.getParentNode(); } // At this point, thisNode and otherNode are direct children of // the common ancestor. // See whether thisNode or otherNode is the leftmost for (Node current=thisNodeP.getFirstChild(); current!=null; current=current.getNextSibling()) { if (current==otherNode) { return DOCUMENT_POSITION_PRECEDING; } else if (current==thisNode) { return DOCUMENT_POSITION_FOLLOWING; } } // REVISIT: shouldn't get here. Should probably throw an // exception return 0; } /** * This attribute returns the text content of this node and its * descendants. When it is defined to be null, setting it has no effect. * When set, any possible children this node may have are removed and * replaced by a single <code>Text</code> node containing the string * this attribute is set to. On getting, no serialization is performed, * the returned string does not contain any markup. No whitespace * normalization is performed, the returned string does not contain the * element content whitespaces . Similarly, on setting, no parsing is * performed either, the input string is taken as pure textual content. * <br>The string returned is made of the text content of this node * depending on its type, as defined below: * <table border='1'> * <tr> * <th>Node type</th> * <th>Content</th> * </tr> /** * This attribute returns the text content of this node and its * descendants. When it is defined to be null, setting it has no effect. * When set, any possible children this node may have are removed and * replaced by a single <code>Text</code> node containing the string * this attribute is set to. On getting, no serialization is performed, * the returned string does not contain any markup. No whitespace * normalization is performed, the returned string does not contain the * element content whitespaces . Similarly, on setting, no parsing is * performed either, the input string is taken as pure textual content. * <br>The string returned is made of the text content of this node * depending on its type, as defined below: * <table border='1'> * <tr> * <th>Node type</th> * <th>Content</th> * </tr> * <tr> * <td valign='top' rowspan='1' colspan='1'> * ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, * DOCUMENT_FRAGMENT_NODE</td> * <td valign='top' rowspan='1' colspan='1'>concatenation of the <code>textContent</code> * attribute value of every child node, excluding COMMENT_NODE and * PROCESSING_INSTRUCTION_NODE nodes</td> * </tr> * <tr> * <td valign='top' rowspan='1' colspan='1'>ATTRIBUTE_NODE, TEXT_NODE, * CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODE</td> * <td valign='top' rowspan='1' colspan='1'> * <code>nodeValue</code></td> * </tr> * <tr> * <td valign='top' rowspan='1' colspan='1'>DOCUMENT_NODE, DOCUMENT_TYPE_NODE, NOTATION_NODE</td> * <td valign='top' rowspan='1' colspan='1'> * null</td> * </tr> * </table> * @exception DOMException * NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly. * @exception DOMException * DOMSTRING_SIZE_ERR: Raised when it would return more characters than * fit in a <code>DOMString</code> variable on the implementation * platform. * @since DOM Level 3 */ public String getTextContent() throws DOMException { return getNodeValue(); // overriden in some subclasses } // internal method taking a StringBuffer in parameter void getTextContent(StringBuffer buf) throws DOMException { String content = getNodeValue(); if (content != null) { buf.append(content); } } /** * This attribute returns the text content of this node and its * descendants. When it is defined to be null, setting it has no effect. * When set, any possible children this node may have are removed and * replaced by a single <code>Text</code> node containing the string * this attribute is set to. On getting, no serialization is performed, * the returned string does not contain any markup. No whitespace * normalization is performed, the returned string does not contain the * element content whitespaces . Similarly, on setting, no parsing is * performed either, the input string is taken as pure textual content. * <br>The string returned is made of the text content of this node * depending on its type, as defined below: * <table border='1'> * <tr> * <th>Node type</th> * <th>Content</th> * </tr> * <tr> * <td valign='top' rowspan='1' colspan='1'> * ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, * DOCUMENT_FRAGMENT_NODE</td> * <td valign='top' rowspan='1' colspan='1'>concatenation of the <code>textContent</code> * attribute value of every child node, excluding COMMENT_NODE and * PROCESSING_INSTRUCTION_NODE nodes</td> * </tr> * <tr> * <td valign='top' rowspan='1' colspan='1'>ATTRIBUTE_NODE, TEXT_NODE, * CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODE</td> * <td valign='top' rowspan='1' colspan='1'> * <code>nodeValue</code></td> * </tr> * <tr> * <td valign='top' rowspan='1' colspan='1'>DOCUMENT_NODE, DOCUMENT_TYPE_NODE, NOTATION_NODE</td> * <td valign='top' rowspan='1' colspan='1'> * null</td> * </tr> * </table> * @exception DOMException * NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly. * @exception DOMException * DOMSTRING_SIZE_ERR: Raised when it would return more characters than * fit in a <code>DOMString</code> variable on the implementation * platform.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?