treewalkerimpl.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 544 行 · 第 1/2 页

JAVA
544
字号
        if (fCurrentNode == null) return null;                Node result = getFirstChild(fCurrentNode);                if (result != null) {            fCurrentNode = result;            return result;        }                result = getNextSibling(fCurrentNode);                if (result != null) {            fCurrentNode = result;            return result;        }                        // return parent's 1st sibling.        Node parent = getParentNode(fCurrentNode);        while (parent != null) {            result = getNextSibling(parent);            if (result != null) {                fCurrentNode = result;                return result;            } else {                parent = getParentNode(parent);            }        }                // end , return null        return null;    }        /** Internal function.     *  Return the parent Node, from the input node     *  after applying filter, whatToshow.     *  The current node is not consulted or set.     */    Node getParentNode(Node node) {                if (node == null || node == fRoot) return null;                Node newNode = node.getParentNode();        if (newNode == null)  return null;                                 int accept = acceptNode(newNode);                if (accept == NodeFilter.FILTER_ACCEPT)            return newNode;        else         //if (accept == NodeFilter.SKIP_NODE) // and REJECT too.        {            return getParentNode(newNode);        }                    }        /** Internal function.     *  Return the nextSibling Node, from the input node     *  after applying filter, whatToshow.     *  The current node is not consulted or set.     */    Node getNextSibling(Node node) {		return getNextSibling(node, fRoot);	}    /** Internal function.     *  Return the nextSibling Node, from the input node     *  after applying filter, whatToshow.     *  NEVER TRAVERSES ABOVE THE SPECIFIED ROOT NODE.      *  The current node is not consulted or set.     */    Node getNextSibling(Node node, Node root) {                if (node == null || node == root) return null;                Node newNode = node.getNextSibling();        if (newNode == null) {                            newNode = node.getParentNode();                            if (newNode == null || newNode == root)  return null;                             int parentAccept = acceptNode(newNode);                            if (parentAccept==NodeFilter.FILTER_SKIP) {                return getNextSibling(newNode, root);            }                            return null;        }                int accept = acceptNode(newNode);                if (accept == NodeFilter.FILTER_ACCEPT)            return newNode;        else         if (accept == NodeFilter.FILTER_SKIP) {            Node fChild = getFirstChild(newNode);            if (fChild == null) {                return getNextSibling(newNode, root);            }            return fChild;        }        else         //if (accept == NodeFilter.REJECT_NODE)         {            return getNextSibling(newNode, root);        }            } // getNextSibling(Node node) {        /** Internal function.     *  Return the previous sibling Node, from the input node     *  after applying filter, whatToshow.     *  The current node is not consulted or set.     */    Node getPreviousSibling(Node node) {		return getPreviousSibling(node, fRoot);	}    /** Internal function.     *  Return the previousSibling Node, from the input node     *  after applying filter, whatToshow.	 *  NEVER TRAVERSES ABOVE THE SPECIFIED ROOT NODE.      *  The current node is not consulted or set.     */    Node getPreviousSibling(Node node, Node root) {                if (node == null || node == root) return null;                Node newNode = node.getPreviousSibling();        if (newNode == null) {                            newNode = node.getParentNode();            if (newNode == null || newNode == root)  return null;                             int parentAccept = acceptNode(newNode);                            if (parentAccept==NodeFilter.FILTER_SKIP) {                return getPreviousSibling(newNode, root);            }                        return null;        }                int accept = acceptNode(newNode);                if (accept == NodeFilter.FILTER_ACCEPT)            return newNode;        else         if (accept == NodeFilter.FILTER_SKIP) {            Node fChild =  getLastChild(newNode);            if (fChild == null) {                return getPreviousSibling(newNode, root);            }            return fChild;        }        else         //if (accept == NodeFilter.REJECT_NODE)         {            return getPreviousSibling(newNode, root);        }            } // getPreviousSibling(Node node) {        /** Internal function.     *  Return the first child Node, from the input node     *  after applying filter, whatToshow.     *  The current node is not consulted or set.     */    Node getFirstChild(Node node) {        if (node == null) return null;                if ( !fEntityReferenceExpansion             && node.getNodeType() == Node.ENTITY_REFERENCE_NODE)            return null;        Node newNode = node.getFirstChild();        if (newNode == null)  return null;        int accept = acceptNode(newNode);                if (accept == NodeFilter.FILTER_ACCEPT)            return newNode;        else         if (accept == NodeFilter.FILTER_SKIP            && newNode.hasChildNodes())         {            Node fChild = getFirstChild(newNode);                        if (fChild == null) {                return getNextSibling(newNode, node);            }            return fChild;        }        else         //if (accept == NodeFilter.REJECT_NODE)         {            return getNextSibling(newNode, node);        }                    }       /** Internal function.     *  Return the last child Node, from the input node     *  after applying filter, whatToshow.     *  The current node is not consulted or set.     */    Node getLastChild(Node node) {                if (node == null) return null;                if ( !fEntityReferenceExpansion             && node.getNodeType() == Node.ENTITY_REFERENCE_NODE)            return null;                    Node newNode = node.getLastChild();        if (newNode == null)  return null;                 int accept = acceptNode(newNode);                if (accept == NodeFilter.FILTER_ACCEPT)            return newNode;        else         if (accept == NodeFilter.FILTER_SKIP            && newNode.hasChildNodes())         {            Node lChild = getLastChild(newNode);            if (lChild == null) {                return getPreviousSibling(newNode, node);            }            return lChild;        }        else         //if (accept == NodeFilter.REJECT_NODE)         {            return getPreviousSibling(newNode, node);        }                    }        /** Internal function.      *  The node whatToShow and the filter are combined into one result. */    short acceptNode(Node node) {        /***         7.1.2.4. Filters and whatToShow flags          Iterator and TreeWalker apply whatToShow flags before applying Filters. If a node is rejected by the         active whatToShow flags, a Filter will not be called to evaluate that node. When a node is rejected by         the active whatToShow flags, children of that node will still be considered, and Filters may be called to         evaluate them.         ***/                        if (fNodeFilter == null) {            if ( ( fWhatToShow & (1 << node.getNodeType()-1)) != 0) {                return NodeFilter.FILTER_ACCEPT;            } else {                return NodeFilter.FILTER_SKIP;            }        } else {            if ((fWhatToShow & (1 << node.getNodeType()-1)) != 0 ) {                return fNodeFilter.acceptNode(node);            } else {                // What to show has failed. See above excerpt from spec.                // Equivalent to FILTER_SKIP.                return NodeFilter.FILTER_SKIP;            }        }    } }

⌨️ 快捷键说明

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