KickJava   Java API By Example, From Geeks To Geeks.

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


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/HasAttributeFilterWrapper.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.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import javax.swing.JCheckBox JavaDoc;
37 import javax.swing.JComboBox JavaDoc;
38 import javax.swing.JTextArea JavaDoc;
39 import javax.swing.border.BevelBorder JavaDoc;
40 import javax.swing.event.DocumentEvent JavaDoc;
41 import javax.swing.event.DocumentListener JavaDoc;
42 import javax.swing.text.BadLocationException JavaDoc;
43 import javax.swing.text.Document JavaDoc;
44
45 import org.htmlparser.Attribute;
46 import org.htmlparser.Node;
47 import org.htmlparser.NodeFilter;
48 import org.htmlparser.Parser;
49 import org.htmlparser.Tag;
50 import org.htmlparser.filters.HasAttributeFilter;
51 import org.htmlparser.parserapplications.filterbuilder.Filter;
52 import org.htmlparser.tags.CompositeTag;
53 import org.htmlparser.util.NodeIterator;
54 import org.htmlparser.util.NodeList;
55 import org.htmlparser.util.ParserException;
56
57 /**
58  * Wrapper for HasAttributeFilters.
59  */

60 public class HasAttributeFilterWrapper
61     extends
62         Filter
63     implements
64         ActionListener JavaDoc,
65         DocumentListener JavaDoc
66 {
67     /**
68      * The underlying filter.
69      */

70     protected HasAttributeFilter mFilter;
71
72     /**
73      * Combo box for attribute name.
74      */

75     protected JComboBox JavaDoc mAttributeName;
76
77     /**
78      * The check box for has value.
79      */

80     protected JCheckBox JavaDoc mValued;
81
82     /**
83      * Value to check for.
84      */

85     protected JTextArea JavaDoc mAttributeValue;
86
87     /**
88      * Create a wrapper over a new HasAttributeFilter.
89      */

90     public HasAttributeFilterWrapper ()
91     {
92         String JavaDoc value;
93
94         mFilter = new HasAttributeFilter ();
95
96         // add the attribute name choice
97
mAttributeName = new JComboBox JavaDoc ();
98         mAttributeName.setEditable (true);
99         add (mAttributeName);
100         mAttributeName.addItem (mFilter.getAttributeName ());
101         mAttributeName.addActionListener (this);
102
103         // add the valued flag
104
value = mFilter.getAttributeValue ();
105         mValued = new JCheckBox JavaDoc ("Has Value");
106         add (mValued);
107         mValued.setSelected (null != value);
108         mValued.addActionListener (this);
109
110         // add the value pattern
111
mAttributeValue = new JTextArea JavaDoc (2, 20);
112         mAttributeValue.setBorder (new BevelBorder JavaDoc (BevelBorder.LOWERED));
113         add (mAttributeValue);
114         if (null != value)
115             mAttributeValue.setText (value);
116         else
117             mAttributeValue.setVisible (false);
118         mAttributeValue.getDocument ().addDocumentListener (this);
119     }
120
121     //
122
// local methods
123
//
124

125     protected void addAttributes (Set JavaDoc set, Node node)
126     {
127         Vector JavaDoc attributes;
128         Attribute attribute;
129         String JavaDoc name;
130         NodeList children;
131
132         if (node instanceof Tag)
133         {
134             attributes = ((Tag)node).getAttributesEx ();
135             for (int i = 1; i < attributes.size (); i++)
136             {
137                 attribute = (Attribute)attributes.elementAt (i);
138                 name = attribute.getName ();
139                 if (null != name)
140                     set.add (name);
141             }
142             if (node instanceof CompositeTag)
143             {
144                 children = ((CompositeTag)node).getChildren ();
145                 if (null != children)
146                     for (int i = 0; i < children.size (); i++)
147                         addAttributes (set, children.elementAt (i));
148             }
149         }
150     }
151
152     protected void addAttributeValues (Set JavaDoc set, Node node)
153     {
154         Vector JavaDoc attributes;
155         Attribute attribute;
156         String JavaDoc value;
157         NodeList children;
158
159         if (node instanceof Tag)
160         {
161             attributes = ((Tag)node).getAttributesEx ();
162             for (int i = 1; i < attributes.size (); i++)
163             {
164                 attribute = (Attribute)attributes.elementAt (i);
165                 if (null != attribute.getName ())
166                 {
167                     value = attribute.getValue ();
168                     if (null != value)
169                         set.add (value);
170                 }
171             }
172             if (node instanceof CompositeTag)
173             {
174                 children = ((CompositeTag)node).getChildren ();
175                 if (null != children)
176                     for (int i = 0; i < children.size (); i++)
177                         addAttributeValues (set, children.elementAt (i));
178             }
179         }
180     }
181
182     //
183
// Filter overrides and concrete implementations
184
//
185

186     public String JavaDoc getDescription ()
187     {
188         return ("Has attribute");
189     }
190
191     public String JavaDoc getIconSpec ()
192     {
193         return ("images/HasAttributeFilter.gif");
194     }
195
196     public NodeFilter getNodeFilter ()
197     {
198         HasAttributeFilter ret;
199         
200         ret = new HasAttributeFilter ();
201         ret.setAttributeName (mFilter.getAttributeName ());
202         ret.setAttributeValue (mFilter.getAttributeValue ());
203             
204         return (ret);
205     }
206
207     public void setNodeFilter (NodeFilter filter, Parser context)
208     {
209         Set JavaDoc set;
210         String JavaDoc name;
211         String JavaDoc value;
212
213         mFilter = (HasAttributeFilter)filter;
214         set = new HashSet JavaDoc ();
215         context.reset ();
216         try
217         {
218             for (NodeIterator iterator = context.elements (); iterator.hasMoreNodes (); )
219                 addAttributes (set, iterator.nextNode ());
220         }
221         catch (ParserException pe)
222         {
223             // oh well, we tried
224
}
225         for (Iterator JavaDoc iterator = set.iterator (); iterator.hasNext (); )
226             mAttributeName.addItem (iterator.next ());
227         name = mFilter.getAttributeName ();
228         if (!name.equals (""))
229             mAttributeName.setSelectedItem (name);
230         value = mFilter.getAttributeValue ();
231         if (null != value)
232         {
233             mValued.setSelected (true);
234             mAttributeValue.setVisible (true);
235             mAttributeValue.setText (value);
236         }
237         else
238         {
239             mValued.setSelected (false);
240             mAttributeValue.setVisible (false);
241         }
242     }
243
244     public NodeFilter[] getSubNodeFilters ()
245     {
246         return (new NodeFilter[0]);
247     }
248
249     public void setSubNodeFilters (NodeFilter[] filters)
250     {
251         // should we complain?
252
}
253
254     public String JavaDoc toJavaCode (StringBuffer JavaDoc out, int[] context)
255     {
256         String JavaDoc ret;
257         
258         ret = "filter" + context[1]++;
259         spaces (out, context[0]);
260         out.append ("HasAttributeFilter ");
261         out.append (ret);
262         out.append (" = new HasAttributeFilter ();");
263         newline (out);
264         spaces (out, context[0]);
265         out.append (ret);
266         out.append (".setAttributeName (\"");
267         out.append (mFilter.getAttributeName ());
268         out.append ("\");");
269         newline (out);
270         if (null != mFilter.getAttributeValue ())
271         {
272             spaces (out, context[0]);
273             out.append (ret);
274             out.append (".setAttributeValue (\"");
275             out.append (mFilter.getAttributeValue ());
276             out.append ("\");");
277             newline (out);
278         }
279         
280         return (ret);
281     }
282
283     //
284
// NodeFilter interface
285
//
286

287     public boolean accept (Node node)
288     {
289         return (mFilter.accept (node));
290     }
291
292     //
293
// ActionListener interface
294
//
295

296     /**
297      * Invoked when an action occurs on the combo box.
298      */

299     public void actionPerformed (ActionEvent JavaDoc event)
300     {
301         Object JavaDoc source;
302         Object JavaDoc[] selection;
303         boolean valued;
304
305         source = event.getSource ();
306         if (source == mAttributeName)
307         {
308             selection = mAttributeName.getSelectedObjects ();
309             if ((null != selection) && (0 != selection.length))
310                 mFilter.setAttributeName ((String JavaDoc)selection[0]);
311         }
312         else if (source == mValued)
313         {
314             valued = mValued.isSelected ();
315             if (valued)
316             {
317                 mFilter.setAttributeValue (mAttributeValue.getText ());
318                 mAttributeValue.setVisible (true);
319             }
320             else
321             {
322                 mAttributeValue.setVisible (false);
323                 mAttributeValue.setText ("");
324                 mFilter.setAttributeValue (null);
325             }
326         }
327     }
328
329     //
330
// DocumentListener interface
331
//
332

333     public void insertUpdate (DocumentEvent JavaDoc e)
334     {
335         Document JavaDoc doc;
336         
337         doc = e.getDocument ();
338         try
339         {
340             mFilter.setAttributeValue (doc.getText (0, doc.getLength ()));
341         }
342         catch (BadLocationException JavaDoc ble)
343         {
344             ble.printStackTrace ();
345         }
346     }
347
348     public void removeUpdate (DocumentEvent JavaDoc e)
349     {
350         Document JavaDoc doc;
351         
352         doc = e.getDocument ();
353         try
354         {
355             mFilter.setAttributeValue (doc.getText (0, doc.getLength ()));
356         }
357         catch (BadLocationException JavaDoc ble)
358         {
359             ble.printStackTrace ();
360         }
361     }
362
363     public void changedUpdate (DocumentEvent JavaDoc e)
364     {
365         // plain text components don't fire these events
366
}
367 }
368
Popular Tags