1 27 package org.htmlparser.parserapplications.filterbuilder.wrappers; 28 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 32 import javax.swing.JComboBox ; 33 import javax.swing.JTextArea ; 34 import javax.swing.border.BevelBorder ; 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.NodeFilter; 42 import org.htmlparser.Parser; 43 import org.htmlparser.filters.RegexFilter; 44 import org.htmlparser.parserapplications.filterbuilder.Filter; 45 46 49 public class RegexFilterWrapper 50 extends 51 Filter 52 implements 53 ActionListener , 54 DocumentListener 55 { 56 59 public static Object [][] mMap; 60 static 61 { 62 mMap = new Object [3][]; 63 mMap[0] = new Object [2]; 64 mMap[0][0] = "MATCH"; 65 mMap[0][1] = new Integer (RegexFilter.MATCH); 66 mMap[1] = new Object [2]; 67 mMap[1][0] = "LOOKINGAT"; 68 mMap[1][1] = new Integer (RegexFilter.LOOKINGAT); 69 mMap[2] = new Object [2]; 70 mMap[2][0] = "FIND"; 71 mMap[2][1] = new Integer (RegexFilter.FIND); 72 } 73 74 77 protected RegexFilter mFilter; 78 79 82 protected JTextArea mPattern; 83 84 87 protected JComboBox mStrategy; 88 89 92 public RegexFilterWrapper () 93 { 94 mFilter = new RegexFilter (); 95 96 mPattern = new JTextArea (2, 20); 98 mPattern.setBorder (new BevelBorder (BevelBorder.LOWERED)); 99 add (mPattern); 100 mPattern.getDocument ().addDocumentListener (this); 101 mPattern.setText (mFilter.getPattern ()); 102 103 mStrategy = new JComboBox (); 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 117 public String getDescription () 118 { 119 return ("Nodes containing regex"); 120 } 121 122 public String 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 } 154 155 public String toJavaCode (StringBuffer out, int[] context) 156 { 157 String 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 strategyToString (int strategy) 182 { 183 for (int i =0; i < mMap.length; i++) 184 if (strategy == ((Integer )mMap[i][1]).intValue ()) 185 return ((String )mMap[i][0]); 186 throw new IllegalArgumentException ("unknown strategy constant - " + strategy); 187 } 188 189 public int stringToStrategy (String strategy) 190 { 191 for (int i =0; i < mMap.length; i++) 192 if (strategy.equalsIgnoreCase ((String )mMap[i][0])) 193 return (((Integer )mMap[i][1]).intValue ()); 194 throw new IllegalArgumentException ("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 )mMap[i][1]).intValue ()) 201 return (i); 202 throw new IllegalArgumentException ("unknown strategy constant - " + strategy); 203 } 204 205 public int indexToStrategy (int index) 206 { 207 return (((Integer )mMap[index][1]).intValue ()); 208 } 209 210 214 public boolean accept (Node node) 215 { 216 return (mFilter.accept (node)); 217 } 218 219 223 226 public void actionPerformed (ActionEvent event) 227 { 228 Object source; 229 230 source = event.getSource (); 231 if (source == mStrategy) 232 mFilter.setStrategy (indexToStrategy (mStrategy.getSelectedIndex ())); 233 } 234 235 239 public void insertUpdate (DocumentEvent e) 240 { 241 Document doc; 242 243 doc = e.getDocument (); 244 try 245 { 246 mFilter.setPattern (doc.getText (0, doc.getLength ())); 247 } 248 catch (BadLocationException ble) 249 { 250 ble.printStackTrace (); 251 } 252 } 253 254 public void removeUpdate (DocumentEvent e) 255 { 256 Document doc; 257 258 doc = e.getDocument (); 259 try 260 { 261 mFilter.setPattern (doc.getText (0, doc.getLength ())); 262 } 263 catch (BadLocationException ble) 264 { 265 ble.printStackTrace (); 266 } 267 } 268 269 public void changedUpdate (DocumentEvent e) 270 { 271 } 273 } 274 | Popular Tags |