⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nodetreewalker.java

📁 html 解析处理代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// HTMLParser Library $Name: v1_6 $ - A java-based parser for HTML// http://sourceforge.org/projects/htmlparser// Copyright (C) 2004 Somik Raha//// Revision Control Information//// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/util/NodeTreeWalker.java,v $// $Author: derrickoswald $// $Date: 2006/06/02 03:14:21 $// $Revision: 1.2 $//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//package org.htmlparser.util;import org.htmlparser.Node;/** * A class for walking a tree of {@link Node} objects, in either a depth-first or breadth-first manner. * The following two diagrams show the represent tree traversal with the two different methods. * <table> *  <tr> *   <th>Depth-first traversal</th> *   <th>Breadth-first traversal</th> *  </tr> *  <tr> *   <img src="http://htmlparser.sourceforge.net/tree-traversal-depth-first.gif" alt="Diagram showing depth-first tree traversal" width="300" height="300" /> *  </tr> *  <tr> *   <img src="http://htmlparser.sourceforge.net/tree-traversal-breadth-first.gif" alt="Diagram showing breadth-first tree traversal" width="300" height="300" /> *  </tr> * </table> * @author  ian_macfarlane */public class NodeTreeWalker implements NodeIterator{    /**     * The root Node element which defines the scope of the current tree to walk.     */    protected Node mRootNode;        /**     * The current Node element, which will be a child of the root Node, or null.     */    protected Node mCurrentNode;        /**     * The next Node element after the current Node element.     * Stored for internal use only.     */    protected Node mNextNode;        /**     * The maximum depth (child-parent links) from which this NodeTreeWalker may be removed from the root Node.     * A value of -1 indicates that there is no depth restriction.     */    protected int mMaxDepth;        /**     * Whether the tree traversal method used is depth-first (default) or breadth-first.     */    protected boolean mDepthFirst;        /**     * Creates a new instance of NodeTreeWalker using depth-first tree traversal, without limits on how deep it may traverse.     * @param rootNode Node The Node to set as the root of the tree.     * @throws NullPointerException if root Node is null.     */    public NodeTreeWalker(Node rootNode)    {        this(rootNode, true, -1);    }        /**     * Creates a new instance of NodeTreeWalker using the specified type of tree traversal, without limits on how deep it may traverse.     * @param rootNode The Node to set as the root of the tree.     * @param depthFirst Whether to use depth-first (true) or breadth-first (false) tree traversal.     * @throws NullPointerException if rootNode is null.     */    public NodeTreeWalker(Node rootNode, boolean depthFirst)    {        this(rootNode, depthFirst, -1);    }        /**     * Creates a new instance of NodeTreeWalker using the specified type of tree traversal and maximum depth from the root Node to traverse.     * @param rootNode The Node to set as the root of the tree.     * @param depthFirst Whether to use depth-first (true) or breadth-first (false) tree traversal.     * @param maxDepth The maximum depth from the root Node that this NodeTreeWalker may traverse. This must be > 0 or equal to -1.     * @throws NullPointerException if rootNode is null.     * @throws IllegalArgumentException maxDepth is not > 0 or equal to -1.     */    public NodeTreeWalker(Node rootNode, boolean depthFirst, int maxDepth)    {        //check maxDepth is valid        if ( ! ((maxDepth >= 1) || (maxDepth == -1)))//if not one of these valid possibilities            throw new IllegalArgumentException("Paramater maxDepth must be > 0 or equal to -1.");        initRootNode(rootNode);//this method also checks if rootNode is valid        this.mDepthFirst = depthFirst;        this.mMaxDepth = maxDepth;    }        /**     * Whether the NodeTreeWalker is currently set to use depth-first or breadth-first tree traversal.     * @return True if depth-first tree-traversal is used, or false if breadth-first tree-traversal is being used.     */    public boolean isDepthFirst()    {        return (this.mDepthFirst);    }        /**     * Sets whether the NodeTreeWalker should use depth-first or breadth-first tree traversal.     * @param depthFirst Whether to use depth-first (true) or breadth-first (false) tree traversal.     */    public void setDepthFirst(boolean depthFirst)    {        if (this.mDepthFirst != depthFirst)//if we are changing search pattern            this.mNextNode = null;        this.mDepthFirst = depthFirst;    }        /**     * The maximum depth (number of child-parent links) below the root Node that this NodeTreeWalker may traverse.     * @return The maximum depth that this NodeTreeWalker can traverse to.     */    public int getMaxDepth()    {        return (this.mMaxDepth);    }        /**     * Removes any restrictions in place that prevent this NodeTreeWalker from traversing beyond a certain depth.     */    public void removeMaxDepthRestriction()    {        this.mMaxDepth = -1;    }        /**     * Get the root Node that defines the scope of the tree to traverse.     * @return The root Node.     */    public Node getRootNode()    {        return (this.mRootNode);    }        /**     * Get the Node in the tree that the NodeTreeWalker is current at.     * @return The current Node.     */    public Node getCurrentNode()    {        return (this.mCurrentNode);    }        /**     * Sets the current Node as the root Node.     * Resets the current position in the tree.     * @throws NullPointerException if the current Node is null (i.e. if the tree traversal has not yet begun).     */    public void setCurrentNodeAsRootNode() throws NullPointerException    {        if (this.mCurrentNode == null)            throw new NullPointerException("Current Node is null, cannot set as root Node.");        initRootNode(this.mCurrentNode);    }        /**     * Sets the specified Node as the root Node.     * Resets the current position in the tree.     * @param rootNode The Node to set as the root of the tree.     * @throws NullPointerException if rootNode is null.     */    public void setRootNode(Node rootNode) throws NullPointerException    {        initRootNode(rootNode);    }        /**     * Resets the current position in the tree,     * such that calling <code>nextNode()</code> will return the first Node again.     */    public void reset()    {        this.mCurrentNode = null;        this.mNextNode = null;    }        /**     * Traverses to the next Node from the current Node, using either depth-first or breadth-first tree traversal as appropriate.     * @return The next Node from the current Node.     */    public Node nextNode()    {

⌨️ 快捷键说明

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