KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > filters > HasChildFilter


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2003 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/HasChildFilter.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/02/13 20:36:00 $
10
// $Revision: 1.3 $
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.filters;
28
29 import org.htmlparser.Node;
30 import org.htmlparser.NodeFilter;
31 import org.htmlparser.tags.CompositeTag;
32 import org.htmlparser.util.NodeList;
33
34 /**
35  * This class accepts all tags that have a child acceptable to the filter.
36  */

37 public class HasChildFilter
38     implements
39         NodeFilter
40 {
41     /**
42      * The filter to apply to children.
43      */

44     protected NodeFilter mChildFilter;
45
46     /**
47      * Performs a recursive search down the node heirarchy if <code>true</code>.
48      */

49     public boolean mRecursive;
50
51     /**
52      * Creates a new instance of a HasChildFilter.
53      * With no child filter, this would always return <code>false</code>
54      * from {@link #accept}.
55      */

56     public HasChildFilter ()
57     {
58         this (null);
59     }
60
61     /**
62      * Creates a new instance of HasChildFilter that accepts nodes with a direct child acceptable to the filter.
63      * @param filter The filter to apply to the children.
64      */

65     public HasChildFilter (NodeFilter filter)
66     {
67         this (filter, false);
68     }
69
70     /**
71      * Creates a new instance of HasChildFilter that accepts nodes with a child acceptable to the filter.
72      * Of necessity, this applies only to composite tags, i.e. those that can
73      * contain other nodes, for example &lt;HTML&gt;&lt;/HTML&gt;.
74      * @param filter The filter to apply to children.
75      * @param recursive If <code>true</code>, any enclosed node acceptable
76      * to the given filter causes the node being tested to be accepted
77      * (i.e. a recursive scan through the child nodes down the node
78      * heirarchy is performed).
79      */

80     public HasChildFilter (NodeFilter filter, boolean recursive)
81     {
82         setChildFilter (filter);
83         setRecursive (recursive); }
84
85     /**
86      * Get the filter used by this HasParentFilter.
87      * @return The filter to apply to parents.
88      */

89     public NodeFilter getChildFilter ()
90     {
91         return (mChildFilter);
92     }
93     
94     /**
95      * Set the filter for this HasParentFilter.
96      * @param filter The filter to apply to parents in {@link #accept}.
97      */

98     public void setChildFilter (NodeFilter filter)
99     {
100         mChildFilter = filter;
101     }
102
103     /**
104      * Get the recusion setting for the filter.
105      * @return Returns <code>true</code> if the filter is recursive
106      * up the node heirarchy.
107      */

108     public boolean getRecursive ()
109     {
110         return mRecursive;
111     }
112
113     /**
114      * Sets whether the filter is recursive or not.
115      * @param recursive The recursion setting for the filter.
116      */

117     public void setRecursive (boolean recursive)
118     {
119         mRecursive = recursive;
120     }
121
122     /**
123      * Accept tags with children acceptable to the filter.
124      * @param node The node to check.
125      */

126     public boolean accept (Node node)
127     {
128         CompositeTag tag;
129         NodeList children;
130         boolean ret;
131
132         ret = false;
133         if (node instanceof CompositeTag)
134         {
135             tag = (CompositeTag)node;
136             children = tag.getChildren ();
137             if (null != children)
138             {
139                 for (int i = 0; !ret && i < children.size (); i++)
140                     if (getChildFilter ().accept (children.elementAt (i)))
141                         ret = true;
142                 // do recursion after all children checked to get breadth first traversal
143
if (!ret && getRecursive ())
144                     for (int i = 0; !ret && i < children.size (); i++)
145                         if (accept (children.elementAt (i)))
146                             ret = true;
147             }
148         }
149
150         return (ret);
151     }
152 }
153
Popular Tags