KickJava   Java API By Example, From Geeks To Geeks.

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


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/HasParentFilter.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/02/13 20:36:00 $
10
// $Revision: 1.6 $
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.Tag;
32
33 /**
34  * This class accepts all tags that have a parent acceptable to another filter.
35  * It can be set to operate recursively, that is perform a scan up the node
36  * heirarchy looking for any ancestor that matches the predicate filter.
37  * End tags are not considered to be children of any tag.
38  */

39 public class HasParentFilter
40     implements
41         NodeFilter
42 {
43     /**
44      * The filter to apply to the parent.
45      */

46     public NodeFilter mParentFilter;
47
48     /**
49      * Performs a recursive search up the node heirarchy if <code>true</code>.
50      */

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

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

67     public HasParentFilter (NodeFilter filter)
68     {
69         this (filter, false);
70     }
71
72     /**
73      * Creates a new instance of HasParentFilter that accepts nodes with a parent acceptable to the filter.
74      * @param filter The filter to apply to the parent.
75      * @param recursive If <code>true</code>, any enclosing node acceptable
76      * to the given filter causes the node being tested to be accepted
77      * (i.e. a recursive scan through the parent nodes up the node
78      * heirarchy is performed).
79      */

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

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

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

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

118     public void setRecursive (boolean recursive)
119     {
120         mRecursive = recursive;
121     }
122
123     /**
124      * Accept tags with parent acceptable to the filter.
125      * If recursion is enabled, each parent in turn up to
126      * the topmost enclosing node is checked.
127      * Recursion only proceeds while no parent satisfies the
128      * filter.
129      * @param node The node to check.
130      */

131     public boolean accept (Node node)
132     {
133         Node parent;
134         boolean ret;
135
136         ret = false;
137         if (!(node instanceof Tag) || !((Tag)node).isEndTag ())
138         {
139             parent = node.getParent ();
140             if ((null != parent) && (null != getParentFilter ()))
141             {
142                 ret = getParentFilter ().accept (parent);
143                 if (!ret && getRecursive ())
144                     ret = accept (parent);
145             }
146         }
147
148         return (ret);
149     }
150 }
151
Popular Tags