parentnode.java

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

JAVA
1,032
字号
            // otherwise request a cache object            fNodeListCache = ownerDocument.getNodeListCache(this);        }        if (fNodeListCache.fLength == -1) { // is the cached length invalid ?            int l;            ChildNode n;            // start from the cached node if we have one            if (fNodeListCache.fChildIndex != -1 &&                fNodeListCache.fChild != null) {                l = fNodeListCache.fChildIndex;                n = fNodeListCache.fChild;            } else {                n = firstChild;                l = 0;            }            while (n != null) {                l++;                n = n.nextSibling;            }            fNodeListCache.fLength = l;        }        return fNodeListCache.fLength;    } // nodeListGetLength():int    /**     * NodeList method: Count the immediate children of this node     * @return int     */    public int getLength() {        return nodeListGetLength();    }    /**     * Return the Nth immediate child of this node, or null if the index is     * out of bounds.  Use to implement NodeList.item().     * @param index int     */    private Node nodeListItem(int index) {        if (fNodeListCache == null) {            // get rid of trivial case            if (firstChild == lastChild()) {                return index == 0 ? firstChild : null;            }            // otherwise request a cache object            fNodeListCache = ownerDocument.getNodeListCache(this);        }        int i = fNodeListCache.fChildIndex;        ChildNode n = fNodeListCache.fChild;        boolean firstAccess = true;        // short way        if (i != -1 && n != null) {            firstAccess = false;            if (i < index) {                while (i < index && n != null) {                    i++;                    n = n.nextSibling;                }            }            else if (i > index) {                while (i > index && n != null) {                    i--;                    n = n.previousSibling();                }            }        }        else {            // long way            if (index < 0) {                return null;            }            n = firstChild;            for (i = 0; i < index && n != null; i++) {                n = n.nextSibling;            }        }        // release cache if reaching last child or first child        if (!firstAccess && (n == firstChild || n == lastChild())) {            fNodeListCache.fChildIndex = -1;            fNodeListCache.fChild = null;            ownerDocument.freeNodeListCache(fNodeListCache);            // we can keep using the cache until it is actually reused            // fNodeListCache will be nulled by the pool (document) if that            // happens.            // fNodeListCache = null;        }        else {            // otherwise update it            fNodeListCache.fChildIndex = i;            fNodeListCache.fChild = n;        }        return n;    } // nodeListItem(int):Node    /**     * NodeList method: Return the Nth immediate child of this node, or     * null if the index is out of bounds.     * @return org.w3c.dom.Node     * @param index int     */    public Node item(int index) {        return nodeListItem(index);    } // item(int):Node    /**     * Create a NodeList to access children that is use by subclass elements     * that have methods named getLength() or item(int).  ChildAndParentNode     * optimizes getChildNodes() by implementing NodeList itself.  However if     * a subclass Element implements methods with the same name as the NodeList     * methods, they will override the actually methods in this class.     * <p>     * To use this method, the subclass should implement getChildNodes() and     * have it call this method.  The resulting NodeList instance maybe     * shared and cached in a transient field, but the cached value must be     * cleared if the node is cloned.     */    protected final NodeList getChildNodesUnoptimized() {        if (needsSyncChildren()) {            synchronizeChildren();        }        return new NodeList() {                /**                 * @see NodeList.getLength()                 */                public int getLength() {                    return nodeListGetLength();                } // getLength():int                                /**                 * @see NodeList.item(int)                 */                public Node item(int index) {                    return nodeListItem(index);                } // item(int):Node            };    } // getChildNodesUnoptimized():NodeList    //    // DOM2: methods, getters, setters    //    /**     * Override default behavior to call normalize() on this Node's     * children. It is up to implementors or Node to override normalize()     * to take action.     */    public void normalize() {        // No need to normalize if already normalized.        if (isNormalized()) {            return;        }        if (needsSyncChildren()) {            synchronizeChildren();        }        ChildNode kid;        for (kid = firstChild; kid != null; kid = kid.nextSibling) {            kid.normalize();        }        isNormalized(true);    }    /**     * DOM Level 3 WD- Experimental.     * Override inherited behavior from NodeImpl to support deep equal.     */    public boolean isEqualNode(Node arg) {        if (!super.isEqualNode(arg)) {            return false;        }        // there are many ways to do this test, and there isn't any way        // better than another. Performance may vary greatly depending on        // the implementations involved. This one should work fine for us.        Node child1 = getFirstChild();        Node child2 = arg.getFirstChild();        while (child1 != null && child2 != null) {            if (!((NodeImpl) child1).isEqualNode(child2)) {                return false;            }            child1 = child1.getNextSibling();            child2 = child2.getNextSibling();        }        if (child1 != child2) {            return false;        }        return true;    }    //    // Public methods    //    /**     * Override default behavior so that if deep is true, children are also     * toggled.     * @see Node     * <P>     * Note: this will not change the state of an EntityReference or its     * children, which are always read-only.     */    public void setReadOnly(boolean readOnly, boolean deep) {        super.setReadOnly(readOnly, deep);        if (deep) {            if (needsSyncChildren()) {                synchronizeChildren();            }            // Recursively set kids            for (ChildNode mykid = firstChild;                 mykid != null;                 mykid = mykid.nextSibling) {                if (mykid.getNodeType() != Node.ENTITY_REFERENCE_NODE) {                    mykid.setReadOnly(readOnly,true);                }            }        }    } // setReadOnly(boolean,boolean)    //    // Protected methods    //    /**     * Override this method in subclass to hook in efficient     * internal data structure.     */    protected void synchronizeChildren() {        // By default just change the flag to avoid calling this method again        needsSyncChildren(false);    }    /**     * Checks the normalized state of this node after inserting a child.     * If the inserted child causes this node to be unnormalized, then this     * node is flagged accordingly.     * The conditions for changing the normalized state are:     * <ul>     * <li>The inserted child is a text node and one of its adjacent siblings     * is also a text node.     * <li>The inserted child is is itself unnormalized.     * </ul>     *     * @param insertedChild the child node that was inserted into this node     *     * @throws NullPointerException if the inserted child is <code>null</code>     */    void checkNormalizationAfterInsert(ChildNode insertedChild) {        // See if insertion caused this node to be unnormalized.        if (insertedChild.getNodeType() == Node.TEXT_NODE) {            ChildNode prev = insertedChild.previousSibling();            ChildNode next = insertedChild.nextSibling;            // If an adjacent sibling of the new child is a text node,            // flag this node as unnormalized.            if ((prev != null && prev.getNodeType() == Node.TEXT_NODE) ||                (next != null && next.getNodeType() == Node.TEXT_NODE)) {                isNormalized(false);            }        }        else {            // If the new child is not normalized,            // then this node is inherently not normalized.            if (!insertedChild.isNormalized()) {                isNormalized(false);            }        }    } // checkNormalizationAfterInsert(ChildNode)    /**     * Checks the normalized of this node after removing a child.     * If the removed child causes this node to be unnormalized, then this     * node is flagged accordingly.     * The conditions for changing the normalized state are:     * <ul>     * <li>The removed child had two adjacent siblings that were text nodes.     * </ul>     *     * @param previousSibling the previous sibling of the removed child, or     * <code>null</code>     */    void checkNormalizationAfterRemove(ChildNode previousSibling) {        // See if removal caused this node to be unnormalized.        // If the adjacent siblings of the removed child were both text nodes,        // flag this node as unnormalized.        if (previousSibling != null &&            previousSibling.getNodeType() == Node.TEXT_NODE) {            ChildNode next = previousSibling.nextSibling;            if (next != null && next.getNodeType() == Node.TEXT_NODE) {                isNormalized(false);            }        }    } // checkNormalizationAfterRemove(Node)    //    // Serialization methods    //    /** Serialize object. */    private void writeObject(ObjectOutputStream out) throws IOException {        // synchronize chilren        if (needsSyncChildren()) {            synchronizeChildren();        }        // write object        out.defaultWriteObject();    } // writeObject(ObjectOutputStream)    /** Deserialize object. */    private void readObject(ObjectInputStream ois)        throws ClassNotFoundException, IOException {        // perform default deseralization        ois.defaultReadObject();        // hardset synchildren - so we don't try to sync - it does not make any        // sense to try to synchildren when we just deserialize object.        needsSyncChildren(false);    } // readObject(ObjectInputStream)    /*     * a class to store some user data along with its handler     */    class UserDataRecord implements Serializable {        /** Serialization version. */        private static final long serialVersionUID = 3258126977134310455L;                Object fData;        UserDataHandler fHandler;        UserDataRecord(Object data, UserDataHandler handler) {            fData = data;            fHandler = handler;        }    }} // class ParentNode

⌨️ 快捷键说明

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