KickJava   Java API By Example, From Geeks To Geeks.

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


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/NodeClassFilterWrapper.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 import java.util.Iterator JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import javax.swing.JComboBox JavaDoc;
35 //import javax.swing.event.DocumentEvent;
36
//import javax.swing.event.DocumentListener;
37
//import javax.swing.text.BadLocationException;
38
//import javax.swing.text.Document;
39

40 import org.htmlparser.Node;
41 import org.htmlparser.NodeFactory;
42 import org.htmlparser.NodeFilter;
43 import org.htmlparser.Parser;
44 import org.htmlparser.PrototypicalNodeFactory;
45 import org.htmlparser.Tag;
46 import org.htmlparser.filters.NodeClassFilter;
47 import org.htmlparser.parserapplications.filterbuilder.Filter;
48
49 /**
50  * Wrapper for NodeClassFilters.
51  */

52 public class NodeClassFilterWrapper
53     extends
54         Filter
55     implements
56         ActionListener JavaDoc
57 // ,
58
// DocumentListener
59
{
60     /**
61      * The underlying filter.
62      */

63     protected NodeClassFilter mFilter;
64
65     /**
66      * Combo box for strategy.
67      */

68     protected JComboBox JavaDoc mClass;
69
70     /**
71      * Create a wrapper over a new NodeClassFilter.
72      */

73     public NodeClassFilterWrapper ()
74     {
75         mFilter = new NodeClassFilter ();
76
77         // add the strategy choice
78
mClass = new JComboBox JavaDoc ();
79         mClass.addItem ("");
80         add (mClass);
81         mClass.addActionListener (this);
82     }
83
84     //
85
// Filter overrides and concrete implementations
86
//
87

88     public String JavaDoc getDescription ()
89     {
90         return ("Nodes of class");
91     }
92
93     public String JavaDoc getIconSpec ()
94     {
95         return ("images/NodeClassFilter.gif");
96     }
97
98     public NodeFilter getNodeFilter ()
99     {
100         NodeClassFilter ret;
101         
102         ret = new NodeClassFilter ();
103         ret.setMatchClass (mFilter.getMatchClass ());
104             
105         return (ret);
106     }
107
108     public void setNodeFilter (NodeFilter filter, Parser context)
109     {
110         NodeFactory factory;
111         PrototypicalNodeFactory proto;
112         Set JavaDoc names;
113         String JavaDoc name;
114         Tag tag;
115
116         mFilter = (NodeClassFilter)filter;
117
118         factory = context.getNodeFactory ();
119         if (factory instanceof PrototypicalNodeFactory)
120         {
121             proto = (PrototypicalNodeFactory)factory;
122             // iterate over the classes
123
names = proto.getTagNames ();
124             for (Iterator JavaDoc iterator = names.iterator (); iterator.hasNext (); )
125             {
126                 name = (String JavaDoc)iterator.next ();
127                 tag = proto.get (name);
128                 mClass.addItem (tag.getClass ().getName ());
129             }
130         }
131         mClass.setSelectedItem (mFilter.getMatchClass ().getName ());
132     }
133
134     public NodeFilter[] getSubNodeFilters ()
135     {
136         return (new NodeFilter[0]);
137     }
138
139     public void setSubNodeFilters (NodeFilter[] filters)
140     {
141         // should we complain?
142
}
143
144     public String JavaDoc toJavaCode (StringBuffer JavaDoc out, int[] context)
145     {
146         String JavaDoc ret;
147
148         ret = "filter" + context[1]++;
149         spaces (out, context[0]);
150         out.append ("NodeClassFilter ");
151         out.append (ret);
152         out.append (" = new NodeClassFilter ();");
153         newline (out);
154         spaces (out, context[0]);
155         out.append ("try { ");
156         out.append (ret);
157         out.append (".setMatchClass (Class.forName (\"");
158         out.append (mFilter.getMatchClass ().getName ());
159         out.append ("\")); } catch (ClassNotFoundException cnfe) { cnfe.printStackTrace (); }");
160         newline (out);
161         
162         return (ret);
163     }
164
165     //
166
// NodeFilter interface
167
//
168

169     public boolean accept (Node node)
170     {
171         return (mFilter.accept (node));
172     }
173
174     //
175
// ActionListener interface
176
//
177

178     /**
179      * Invoked when an action occurs on the combo box.
180      */

181     public void actionPerformed (ActionEvent JavaDoc event)
182     {
183         Object JavaDoc source;
184
185         source = event.getSource ();
186         if (source == mClass)
187             try
188             {
189                 mFilter.setMatchClass (Class.forName ((String JavaDoc)mClass.getSelectedItem ()));
190             }
191             catch (ClassNotFoundException JavaDoc cnfe)
192             {
193                 cnfe.printStackTrace ();
194             }
195     }
196
197 // //
198
// // DocumentListener interface
199
// //
200
//
201
// public void insertUpdate (DocumentEvent e)
202
// {
203
// Document doc;
204
//
205
// doc = e.getDocument ();
206
// try
207
// {
208
// mFilter.setPattern (doc.getText (0, doc.getLength ()));
209
// }
210
// catch (BadLocationException ble)
211
// {
212
// ble.printStackTrace ();
213
// }
214
// }
215
//
216
// public void removeUpdate (DocumentEvent e)
217
// {
218
// Document doc;
219
//
220
// doc = e.getDocument ();
221
// try
222
// {
223
// mFilter.setPattern (doc.getText (0, doc.getLength ()));
224
// }
225
// catch (BadLocationException ble)
226
// {
227
// ble.printStackTrace ();
228
// }
229
// }
230
//
231
// public void changedUpdate (DocumentEvent e)
232
// {
233
// // plain text components don't fire these events
234
// }
235
}
236
Popular Tags