KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > parserapplications > filterbuilder > wrappers > RegexFilterWrapper


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2005 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/parserapplications/filterbuilder/wrappers/RegexFilterWrapper.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/02/13 20:43:06 $
10
// $Revision: 1.1 $
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.parserapplications.filterbuilder.wrappers;
28
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31
32 import javax.swing.JComboBox JavaDoc;
33 import javax.swing.JTextArea JavaDoc;
34 import javax.swing.border.BevelBorder JavaDoc;
35 import javax.swing.event.DocumentEvent JavaDoc;
36 import javax.swing.event.DocumentListener JavaDoc;
37 import javax.swing.text.BadLocationException JavaDoc;
38 import javax.swing.text.Document JavaDoc;
39
40 import org.htmlparser.Node;
41 import org.htmlparser.NodeFilter;
42 import org.htmlparser.Parser;
43 import org.htmlparser.filters.RegexFilter;
44 import org.htmlparser.parserapplications.filterbuilder.Filter;
45
46 /**
47  * Wrapper for RegexFilters.
48  */

49 public class RegexFilterWrapper
50     extends
51         Filter
52     implements
53         ActionListener JavaDoc,
54         DocumentListener JavaDoc
55 {
56     /**
57      * Mapping for RegexFilter constants to strings.
58      */

59     public static Object JavaDoc[][] mMap;
60     static
61     {
62         mMap = new Object JavaDoc[3][];
63         mMap[0] = new Object JavaDoc[2];
64         mMap[0][0] = "MATCH";
65         mMap[0][1] = new Integer JavaDoc (RegexFilter.MATCH);
66         mMap[1] = new Object JavaDoc[2];
67         mMap[1][0] = "LOOKINGAT";
68         mMap[1][1] = new Integer JavaDoc (RegexFilter.LOOKINGAT);
69         mMap[2] = new Object JavaDoc[2];
70         mMap[2][0] = "FIND";
71         mMap[2][1] = new Integer JavaDoc (RegexFilter.FIND);
72     }
73
74     /**
75      * The underlying filter.
76      */

77     protected RegexFilter mFilter;
78
79     /**
80      * Text to check for
81      */

82     protected JTextArea JavaDoc mPattern;
83
84     /**
85      * Combo box for strategy.
86      */

87     protected JComboBox JavaDoc mStrategy;
88
89     /**
90      * Create a wrapper over a new RegexFilter.
91      */

92     public RegexFilterWrapper ()
93     {
94         mFilter = new RegexFilter ();
95
96         // add the text pattern
97
mPattern = new JTextArea JavaDoc (2, 20);
98         mPattern.setBorder (new BevelBorder JavaDoc (BevelBorder.LOWERED));
99         add (mPattern);
100         mPattern.getDocument ().addDocumentListener (this);
101         mPattern.setText (mFilter.getPattern ());
102
103         // add the strategy choice
104
mStrategy = new JComboBox JavaDoc ();
105         mStrategy.addItem ("MATCH");
106         mStrategy.addItem ("LOOKINGAT");
107         mStrategy.addItem ("FIND");
108         add (mStrategy);
109         mStrategy.addActionListener (this);
110         mStrategy.setSelectedIndex (strategyToIndex (mFilter.getStrategy ()));
111     }
112
113     //
114
// Filter overrides and concrete implementations
115
//
116

117     public String JavaDoc getDescription ()
118     {
119         return ("Nodes containing regex");
120     }
121
122     public String JavaDoc getIconSpec ()
123     {
124         return ("images/RegexFilter.gif");
125     }
126
127     public NodeFilter getNodeFilter ()
128     {
129         RegexFilter ret;
130         
131         ret = new RegexFilter ();
132         ret.setStrategy (mFilter.getStrategy ());
133         ret.setPattern (mFilter.getPattern ());
134             
135         return (ret);
136     }
137
138     public void setNodeFilter (NodeFilter filter, Parser context)
139     {
140         mFilter = (RegexFilter)filter;
141         mPattern.setText (mFilter.getPattern ());
142         mStrategy.setSelectedIndex (strategyToIndex (mFilter.getStrategy ()));
143     }
144
145     public NodeFilter[] getSubNodeFilters ()
146     {
147         return (new NodeFilter[0]);
148     }
149
150     public void setSubNodeFilters (NodeFilter[] filters)
151     {
152         // should we complain?
153
}
154
155     public String JavaDoc toJavaCode (StringBuffer JavaDoc out, int[] context)
156     {
157         String JavaDoc ret;
158
159         ret = "filter" + context[1]++;
160         spaces (out, context[0]);
161         out.append ("RegexFilter ");
162         out.append (ret);
163         out.append (" = new RegexFilter ();");
164         newline (out);
165         spaces (out, context[0]);
166         out.append (ret);
167         out.append (".setStrategy (RegexFilter.");
168         out.append (strategyToString (mFilter.getStrategy ()));
169         out.append (");");
170         newline (out);
171         spaces (out, context[0]);
172         out.append (ret);
173         out.append (".setPattern (\"");
174         out.append (mFilter.getPattern ());
175         out.append ("\");");
176         newline (out);
177         
178         return (ret);
179     }
180
181     public String JavaDoc strategyToString (int strategy)
182     {
183         for (int i =0; i < mMap.length; i++)
184             if (strategy == ((Integer JavaDoc)mMap[i][1]).intValue ())
185                 return ((String JavaDoc)mMap[i][0]);
186         throw new IllegalArgumentException JavaDoc ("unknown strategy constant - " + strategy);
187     }
188
189     public int stringToStrategy (String JavaDoc strategy)
190     {
191         for (int i =0; i < mMap.length; i++)
192             if (strategy.equalsIgnoreCase ((String JavaDoc)mMap[i][0]))
193                 return (((Integer JavaDoc)mMap[i][1]).intValue ());
194         throw new IllegalArgumentException JavaDoc ("unknown strategy constant - " + strategy);
195     }
196
197     public int strategyToIndex (int strategy)
198     {
199         for (int i =0; i < mMap.length; i++)
200             if (strategy == ((Integer JavaDoc)mMap[i][1]).intValue ())
201                 return (i);
202         throw new IllegalArgumentException JavaDoc ("unknown strategy constant - " + strategy);
203     }
204
205     public int indexToStrategy (int index)
206     {
207         return (((Integer JavaDoc)mMap[index][1]).intValue ());
208     }
209
210     //
211
// NodeFilter interface
212
//
213

214     public boolean accept (Node node)
215     {
216         return (mFilter.accept (node));
217     }
218
219     //
220
// ActionListener interface
221
//
222

223     /**
224      * Invoked when an action occurs on the combo box.
225      */

226     public void actionPerformed (ActionEvent JavaDoc event)
227     {
228         Object JavaDoc source;
229
230         source = event.getSource ();
231         if (source == mStrategy)
232             mFilter.setStrategy (indexToStrategy (mStrategy.getSelectedIndex ()));
233     }
234
235     //
236
// DocumentListener interface
237
//
238

239     public void insertUpdate (DocumentEvent JavaDoc e)
240     {
241         Document JavaDoc doc;
242         
243         doc = e.getDocument ();
244         try
245         {
246             mFilter.setPattern (doc.getText (0, doc.getLength ()));
247         }
248         catch (BadLocationException JavaDoc ble)
249         {
250             ble.printStackTrace ();
251         }
252     }
253
254     public void removeUpdate (DocumentEvent JavaDoc e)
255     {
256         Document JavaDoc doc;
257         
258         doc = e.getDocument ();
259         try
260         {
261             mFilter.setPattern (doc.getText (0, doc.getLength ()));
262         }
263         catch (BadLocationException JavaDoc ble)
264         {
265             ble.printStackTrace ();
266         }
267     }
268
269     public void changedUpdate (DocumentEvent JavaDoc e)
270     {
271         // plain text components don't fire these events
272
}
273 }
274
Popular Tags