KickJava   Java API By Example, From Geeks To Geeks.

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


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/OrFilter.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 any of it's predicates filters (OR operation).
34  */

35 public class OrFilter implements NodeFilter
36 {
37     /**
38      * The predicates that are to be or'ed together;
39      */

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

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

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

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

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

92     /**
93      * Accept nodes that are acceptable to any of it's predicate filters.
94      * @param node The node to check.
95      */

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