KickJava   Java API By Example, From Geeks To Geeks.

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


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/AndFilter.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/02/13 20:36:00 $
10
// $Revision: 1.2 $
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
32 /**
33  * Accepts nodes matching all of it's predicate filters (AND operation).
34  */

35 public class AndFilter
36     implements
37         NodeFilter
38 {
39     /**
40      * The predicates that are to be and'ed together;
41      */

42     protected NodeFilter[] mPredicates;
43
44     /**
45      * Creates a new instance of an AndFilter.
46      * With no predicates, this would always answer <code>true</code>
47      * to {@link #accept}.
48      * @see #setPredicates
49      */

50     public AndFilter ()
51     {
52         setPredicates (null);
53     }
54
55     /**
56      * Creates a new instance of an AndFilter that accepts nodes acceptable to both filters.
57      * @param left One filter.
58      * @param right The other filter.
59      */

60     public AndFilter (NodeFilter left, NodeFilter right)
61     {
62         NodeFilter[] predicates;
63         
64         predicates = new NodeFilter[2];
65         predicates[0] = left;
66         predicates[1] = right;
67         setPredicates (predicates);
68     }
69
70     /**
71      * Get the predicates used by this AndFilter.
72      * @return The predicates currently in use.
73      */

74     public NodeFilter[] getPredicates ()
75     {
76         return (mPredicates);
77     }
78     
79     /**
80      * Set the predicates for this AndFilter.
81      * @param predicates The list of predidcates to use in {@link #accept}.
82      */

83     public void setPredicates (NodeFilter[] predicates)
84     {
85         if (null == predicates)
86             predicates = new NodeFilter[0];
87         mPredicates = predicates;
88     }
89
90     //
91
// NodeFilter interface
92
//
93

94     /**
95      * Accept nodes that are acceptable to all of it's predicate filters.
96      * @param node The node to check.
97      */

98     public boolean accept (Node node)
99     {
100         boolean ret;
101         
102         ret = true;
103         
104         for (int i = 0; ret && (i < mPredicates.length); i++)
105             if (!mPredicates[i].accept (node))
106                 ret = false;
107             
108         return (ret);
109     }
110 }
111
Popular Tags