1 27 package org.htmlparser.parserapplications.filterbuilder.wrappers; 28 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.Set ; 34 import java.util.Vector ; 35 36 import javax.swing.JCheckBox ; 37 import javax.swing.JComboBox ; 38 import javax.swing.JTextArea ; 39 import javax.swing.border.BevelBorder ; 40 import javax.swing.event.DocumentEvent ; 41 import javax.swing.event.DocumentListener ; 42 import javax.swing.text.BadLocationException ; 43 import javax.swing.text.Document ; 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 60 public class HasAttributeFilterWrapper 61 extends 62 Filter 63 implements 64 ActionListener , 65 DocumentListener 66 { 67 70 protected HasAttributeFilter mFilter; 71 72 75 protected JComboBox mAttributeName; 76 77 80 protected JCheckBox mValued; 81 82 85 protected JTextArea mAttributeValue; 86 87 90 public HasAttributeFilterWrapper () 91 { 92 String value; 93 94 mFilter = new HasAttributeFilter (); 95 96 mAttributeName = new JComboBox (); 98 mAttributeName.setEditable (true); 99 add (mAttributeName); 100 mAttributeName.addItem (mFilter.getAttributeName ()); 101 mAttributeName.addActionListener (this); 102 103 value = mFilter.getAttributeValue (); 105 mValued = new JCheckBox ("Has Value"); 106 add (mValued); 107 mValued.setSelected (null != value); 108 mValued.addActionListener (this); 109 110 mAttributeValue = new JTextArea (2, 20); 112 mAttributeValue.setBorder (new BevelBorder (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 125 protected void addAttributes (Set set, Node node) 126 { 127 Vector attributes; 128 Attribute attribute; 129 String 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 set, Node node) 153 { 154 Vector attributes; 155 Attribute attribute; 156 String 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 186 public String getDescription () 187 { 188 return ("Has attribute"); 189 } 190 191 public String 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 set; 210 String name; 211 String value; 212 213 mFilter = (HasAttributeFilter)filter; 214 set = new HashSet (); 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 } 225 for (Iterator 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 } 253 254 public String toJavaCode (StringBuffer out, int[] context) 255 { 256 String 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 287 public boolean accept (Node node) 288 { 289 return (mFilter.accept (node)); 290 } 291 292 296 299 public void actionPerformed (ActionEvent event) 300 { 301 Object source; 302 Object [] 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 )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 333 public void insertUpdate (DocumentEvent e) 334 { 335 Document doc; 336 337 doc = e.getDocument (); 338 try 339 { 340 mFilter.setAttributeValue (doc.getText (0, doc.getLength ())); 341 } 342 catch (BadLocationException ble) 343 { 344 ble.printStackTrace (); 345 } 346 } 347 348 public void removeUpdate (DocumentEvent e) 349 { 350 Document doc; 351 352 doc = e.getDocument (); 353 try 354 { 355 mFilter.setAttributeValue (doc.getText (0, doc.getLength ())); 356 } 357 catch (BadLocationException ble) 358 { 359 ble.printStackTrace (); 360 } 361 } 362 363 public void changedUpdate (DocumentEvent e) 364 { 365 } 367 } 368 | Popular Tags |