KickJava   Java API By Example, From Geeks To Geeks.

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


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/StringFilter.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/02/13 20:36:00 $
10
// $Revision: 1.5 $
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 java.util.Locale JavaDoc;
30
31 import org.htmlparser.Node;
32 import org.htmlparser.NodeFilter;
33 import org.htmlparser.Text;
34
35 /**
36  * This class accepts all string nodes containing the given string.
37  * This is a fairly simplistic filter, so for more sophisticated
38  * string matching, for example newline and whitespace handling,
39  * use a {@link RegexFilter} instead.
40  */

41 public class StringFilter
42     implements
43         NodeFilter
44 {
45     /**
46      * The string to search for.
47      */

48     protected String JavaDoc mPattern;
49
50     /**
51      * The string to really search for (converted to uppercase if necessary).
52      */

53     protected String JavaDoc mUpperPattern;
54
55     /**
56      * Case sensitive toggle.
57      * If <code>true</code> strings are compared with case sensitivity.
58      */

59     protected boolean mCaseSensitive;
60
61     /**
62      * The locale to use converting to uppercase in case insensitive searches.
63      */

64     protected Locale JavaDoc mLocale;
65
66     /**
67      * Creates a new instance of StringFilter that accepts all string nodes.
68      */

69     public StringFilter ()
70     {
71         this ("", false);
72     }
73
74     /**
75      * Creates a new instance of a StringFilter that accepts string nodes containing a certain string.
76      * The comparison is case insensitive, with conversions done using the default <code>Locale</code>.
77      * @param pattern The pattern to search for.
78      */

79     public StringFilter (String JavaDoc pattern)
80     {
81         this (pattern, false);
82     }
83
84     /**
85      * Creates a new instance of a StringFilter that accepts string nodes containing a certain string.
86      * @param pattern The pattern to search for.
87      * @param case_sensitive If <code>true</code>, comparisons are performed
88      * respecting case, with conversions done using the default <code>Locale</code>.
89      */

90     public StringFilter (String JavaDoc pattern, boolean case_sensitive)
91     {
92         this (pattern, case_sensitive, null);
93     }
94     
95     /**
96      * Creates a new instance of a StringFilter that accepts string nodes containing a certain string.
97      * @param pattern The pattern to search for.
98      * @param case_sensitive If <code>true</code>, comparisons are performed
99      * respecting case.
100      * @param locale The locale to use when converting to uppercase.
101      * If <code>null</code>, the default <code>Locale</code> is used.
102      */

103     public StringFilter (String JavaDoc pattern, boolean case_sensitive, Locale JavaDoc locale)
104     {
105         mPattern = pattern;
106         mCaseSensitive = case_sensitive;
107         mLocale = (null == locale) ? Locale.getDefault () : locale;
108         setUpperPattern ();
109     }
110
111     //
112
// protected methods
113
//
114

115     /**
116      * Set the real (upper case) comparison string.
117      */

118     protected void setUpperPattern ()
119     {
120         if (getCaseSensitive ())
121             mUpperPattern = getPattern ();
122         else
123             mUpperPattern = getPattern ().toUpperCase (getLocale ());
124    }
125
126     /**
127      * Get the case sensitivity.
128      * @return Returns the case sensitivity.
129      */

130     public boolean getCaseSensitive ()
131     {
132         return (mCaseSensitive);
133     }
134
135     /**
136      * Set case sensitivity on or off.
137      * @param sensitive If <code>false</code> searches for the
138      * string are case insensitive.
139      */

140     public void setCaseSensitive (boolean sensitive)
141     {
142         mCaseSensitive = sensitive;
143         setUpperPattern ();
144     }
145
146     /**
147      * Get the locale for uppercase conversion.
148      * @return Returns the locale.
149      */

150     public Locale JavaDoc getLocale ()
151     {
152         return (mLocale);
153     }
154
155     /**
156      * Set the locale for uppercase conversion.
157      * @param locale The locale to set.
158      */

159     public void setLocale (Locale JavaDoc locale)
160     {
161         mLocale = locale;
162         setUpperPattern ();
163     }
164
165     /**
166      * Get the search pattern.
167      * @return Returns the pattern.
168      */

169     public String JavaDoc getPattern ()
170     {
171         return (mPattern);
172     }
173
174     /**
175      * Set the search pattern.
176      * @param pattern The pattern to set.
177      */

178     public void setPattern (String JavaDoc pattern)
179     {
180         mPattern = pattern;
181         setUpperPattern ();
182     }
183
184     //
185
// NodeFilter interface
186
//
187

188     /**
189      * Accept string nodes that contain the string.
190      * @param node The node to check.
191      * @return <code>true</code> if <code>node</code> is a {@link Text} node
192      * and contains the pattern string, <code>false</code> otherwise.
193      */

194     public boolean accept (Node node)
195     {
196         String JavaDoc string;
197         boolean ret;
198         
199         ret = false;
200         if (node instanceof Text)
201         {
202             string = ((Text)node).getText ();
203             if (!getCaseSensitive ())
204                 string = string.toUpperCase (getLocale ());
205             ret = (-1 != string.indexOf (mUpperPattern));
206         }
207
208         return (ret);
209     }
210 }
211
Popular Tags