KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > visitors > NodeVisitor


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Somik Raha
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/visitors/NodeVisitor.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/05/24 16:18:36 $
10
// $Revision: 1.38 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.visitors;
28
29 import org.htmlparser.Remark;
30 import org.htmlparser.Text;
31 import org.htmlparser.Tag;
32
33 /**
34  * The base class for the 'Visitor' pattern.
35  * Classes that wish to use <code>visitAllNodesWith()</code> will subclass
36  * this class and provide implementations for methods they are interested in
37  * processing.<p>
38  * The operation of <code>visitAllNodesWith()</code> is to call
39  * <code>beginParsing()</code>, then <code>visitXXX()</code> according to the
40  * types of nodes encountered in depth-first order and finally
41  * <code>finishedParsing()</code>.<p>
42  * Typical code to print all the link tags:
43  * <pre>
44  * import org.htmlparser.Parser;
45  * import org.htmlparser.tags.LinkTag;
46  * import org.htmlparser.util.ParserException;
47  * import org.htmlparser.visitors.NodeVisitor;
48  *
49  * public class Visitor extends NodeVisitor
50  * {
51  * public Visitor ()
52  * {
53  * }
54  * public void visitTag (Tag tag)
55  * {
56  * if (tag instanceof LinkTag)
57  * System.out.println (tag);
58  * }
59  * public static void main (String[] args) throws ParserException
60  * {
61  * Parser parser = new Parser ("http://cbc.ca");
62  * Visitor visitor = new Visitor ();
63  * parser.visitAllNodesWith (visitor);
64  * }
65  * }
66  * </pre>
67  */

68 public abstract class NodeVisitor
69 {
70     private boolean mRecurseChildren;
71     private boolean mRecurseSelf;
72
73     /**
74      * Creates a node visitor that recurses itself and it's children.
75      */

76     public NodeVisitor ()
77     {
78         this (true);
79     }
80     
81     /**
82      * Creates a node visitor that recurses itself and it's children
83      * only if <code>recurseChildren</code> is <code>true</code>.
84      * @param recurseChildren If <code>true</code>, the visitor will
85      * visit children, otherwise only the top level nodes are recursed.
86      */

87     public NodeVisitor (boolean recurseChildren)
88     {
89         this (recurseChildren, true);
90     }
91     
92     /**
93      * Creates a node visitor that recurses itself only if
94      * <code>recurseSelf</code> is <code>true</code> and it's children
95      * only if <code>recurseChildren</code> is <code>true</code>.
96      * @param recurseChildren If <code>true</code>, the visitor will
97      * visit children, otherwise only the top level nodes are recursed.
98      * @param recurseSelf If <code>true</code>, the visitor will
99      * visit the top level node.
100      */

101     public NodeVisitor (boolean recurseChildren, boolean recurseSelf)
102     {
103         mRecurseChildren = recurseChildren;
104         mRecurseSelf = recurseSelf;
105     }
106
107     /**
108      * Override this method if you wish to do special
109      * processing prior to the start of parsing.
110      */

111     public void beginParsing ()
112     {
113     }
114
115     /**
116      * Called for each <code>Tag</code> visited.
117      * @param tag The tag being visited.
118      */

119     public void visitTag (Tag tag)
120     {
121     }
122     
123     /**
124      * Called for each <code>Tag</code> visited that is an end tag.
125      * @param tag The end tag being visited.
126      */

127     public void visitEndTag (Tag tag)
128     {
129     }
130     
131     /**
132      * Called for each <code>StringNode</code> visited.
133      * @param string The string node being visited.
134      */

135     public void visitStringNode (Text string)
136     {
137     }
138     
139     /**
140      * Called for each <code>RemarkNode</code> visited.
141      * @param remark The remark node being visited.
142      */

143     public void visitRemarkNode (Remark remark)
144     {
145     }
146
147     /**
148      * Override this method if you wish to do special
149      * processing upon completion of parsing.
150      */

151     public void finishedParsing ()
152     {
153     }
154
155     /**
156      * Depth traversal predicate.
157      * @return <code>true</code> if children are to be visited.
158      */

159     public boolean shouldRecurseChildren ()
160     {
161         return (mRecurseChildren);
162     }
163     
164     /**
165      * Self traversal predicate.
166      * @return <code>true</code> if a node itself is to be visited.
167      */

168     public boolean shouldRecurseSelf ()
169     {
170         return (mRecurseSelf);
171     }
172 }
173
Popular Tags