1 27 package org.htmlparser.parserapplications.filterbuilder; 28 29 import java.awt.*; 30 import java.io.ByteArrayInputStream ; 31 import java.io.ByteArrayOutputStream ; 32 import java.io.IOException ; 33 import java.io.ObjectInputStream ; 34 import java.io.ObjectOutputStream ; 35 import java.util.Hashtable ; 36 import java.util.Vector ; 37 38 import javax.swing.*; 39 import javax.swing.border.*; 40 41 import org.htmlparser.NodeFilter; 42 import org.htmlparser.Parser; 43 import org.htmlparser.parserapplications.filterbuilder.layouts.VerticalLayoutManager; 44 45 49 public abstract class Filter 50 extends 51 JComponent 52 implements 53 NodeFilter 54 { 55 60 public static Filter instantiate (String class_name) 61 { 62 Filter ret; 63 64 ret = null; 65 try 66 { 67 Class cls = Class.forName (class_name); 68 ret = (Filter)cls.newInstance (); 69 mWrappers.put (ret.getNodeFilter ().getClass ().getName (), class_name); 70 } 71 catch (ClassNotFoundException cnfe) 72 { 73 System.out.println ("can't find class " + class_name); 74 } 75 catch (InstantiationException ie) 76 { 77 System.out.println ("can't instantiate class " + class_name); 78 } 79 catch (IllegalAccessException ie) 80 { 81 System.out.println ("class " + class_name + " has no public constructor"); 82 } 83 catch (ClassCastException cce) 84 { 85 System.out.println ("class " + class_name + " is not a Filter"); 86 } 87 88 return (ret); 89 } 90 91 95 protected static Hashtable mWrappers = new Hashtable (); 96 97 104 public Filter () 105 { 106 JLabel label; 107 Dimension dimension; 108 Insets insets; 109 110 setToolTipText (getDescription ()); 111 setLayout (new VerticalLayoutManager ()); 115 setSelected (false); 116 label = new JLabel (getDescription (), getIcon (), SwingConstants.LEFT); 117 label.setBackground (Color.green); 118 label.setAlignmentX (Component.LEFT_ALIGNMENT); 119 label.setHorizontalAlignment (SwingConstants.LEFT); 120 add (label); 121 dimension = label.getMaximumSize (); 122 insets = getInsets (); 123 dimension.setSize (dimension.width + insets.left + insets.right, dimension.height + insets.top + insets.bottom); 124 setSize (dimension); 125 } 126 127 131 public abstract String getDescription (); 132 133 137 public abstract NodeFilter getNodeFilter (); 138 139 147 public abstract void setNodeFilter (NodeFilter filter, Parser context); 148 149 153 public abstract NodeFilter[] getSubNodeFilters (); 154 155 159 public abstract void setSubNodeFilters (NodeFilter[] filters); 160 161 171 public abstract String toJavaCode (StringBuffer out, int[] context); 172 173 179 public Icon getIcon () 180 { 181 ImageIcon ret; 182 183 ret = null; 184 try 185 { 186 ret = new ImageIcon (getClass ().getResource (getIconSpec ())); 187 } 188 catch (NullPointerException npe) 189 { 190 System.err.println ("can't find icon " + getIconSpec ()); 191 } 192 193 return (ret); 194 } 195 196 200 public abstract String getIconSpec (); 201 202 206 210 public String toString () 211 { 212 return (getDescription () + " [" + this.getClass ().getName () + "]"); 213 } 214 215 219 public static byte[] pickle (Object object) 220 throws 221 IOException 222 { 223 ByteArrayOutputStream bos; 224 ObjectOutputStream oos; 225 byte[] ret; 226 227 bos = new ByteArrayOutputStream (); 228 oos = new ObjectOutputStream (bos); 229 oos.writeObject (object); 230 oos.close (); 231 ret = bos.toByteArray (); 232 233 return (ret); 234 } 235 236 public static Object unpickle (byte[] data) 237 throws 238 IOException , 239 ClassNotFoundException 240 { 241 ByteArrayInputStream bis; 242 ObjectInputStream ois; 243 Object ret; 244 245 bis = new ByteArrayInputStream (data); 246 ois = new ObjectInputStream (bis); 247 ret = ois.readObject (); 248 ois.close (); 249 250 return (ret); 251 } 252 253 public static String serialize (byte[] data) 254 { 255 String string; 256 StringBuffer ret; 257 258 ret = new StringBuffer (data.length * 2); 259 260 for (int i = 0; i < data.length; i++) 261 { 262 string = Integer.toString (0xff & data[i], 16); 263 if (string.length () < 2) 264 ret.append ("0"); 265 ret.append (string); 266 } 267 268 return (ret.toString ()); 269 } 270 271 public static byte[] deserialize (String string) 272 { 273 byte[] ret; 274 275 ret = new byte[string.length () / 2]; 276 277 for (int i = 0; i < string.length (); i += 2) 278 ret[i/2] = (byte)Integer.parseInt (string.substring (i, i + 2), 16); 280 return (ret); 281 } 282 283 287 public static String deconstitute (Filter[] filters) throws IOException 288 { 289 StringBuffer ret; 290 291 ret = new StringBuffer (1024); 292 for (int i = 0; i < filters.length; i++) 293 { 294 ret.append ("["); 295 ret.append (serialize (pickle (filters[i].getNodeFilter ()))); 296 ret.append ("]"); 297 } 298 299 return (ret.toString ()); 300 } 301 302 307 public static Filter[] reconstitute (String string, Parser context) 308 { 309 Filter[] ret; 310 Vector vector; 311 int index; 312 String code; 313 Object object; 314 Filter filter; 315 316 vector = new Vector (); 317 try 318 { 319 while (string.startsWith ("[")) 320 { 321 index = string.indexOf (']'); 322 if (-1 != index) 323 { 324 code = string.substring (1, index); 325 string = string.substring (index + 1); 326 object = unpickle (deserialize (code)); 327 if (object instanceof NodeFilter) 328 { 329 filter = wrap ((NodeFilter)object, context); 330 if (null != filter) 331 vector.addElement (filter); 332 } 333 else 334 break; 335 } 336 else 337 break; 338 } 339 } 340 catch (Exception e) 341 { 342 e.printStackTrace (); 343 } 344 345 ret = new Filter[vector.size ()]; 346 vector.copyInto (ret); 347 348 return (ret); 349 } 350 351 357 protected static SubFilterList getEnclosed (Component component) 358 { 359 Component[] list; 360 361 if (component instanceof Container) 362 { 363 list = ((Container)component).getComponents (); 364 for (int i = 0; i < list.length; i++) 365 if (list[i] instanceof SubFilterList) 366 return ((SubFilterList)list[i]); 367 } 368 369 return (null); 370 } 371 372 379 public static Filter wrap (NodeFilter filter, Parser context) 380 { 381 String class_name; 382 NodeFilter[] filters; 383 SubFilterList list; 384 Filter ret; 385 386 ret = null; 387 388 class_name = filter.getClass ().getName (); 389 class_name = (String )mWrappers.get (class_name); 390 if (null != class_name) 391 { 392 try 393 { 394 ret = Filter.instantiate (class_name); 395 ret.setNodeFilter (filter, context); 396 filters = ret.getSubNodeFilters (); 398 if (0 != filters.length) 399 { 400 list = getEnclosed (ret); 401 if (null != list) 402 { 403 ret.setSubNodeFilters (new NodeFilter[0]); for (int i = 0; i < filters.length; i++) 405 list.addFilter (wrap (filters[i], context)); 406 } 407 else 408 throw new IllegalStateException ("filter can't have subnodes without a SubFilterList on the wrapper"); 409 } 410 } 411 catch (Exception e) 412 { 413 e.printStackTrace (); 414 } 415 } 416 else 417 System.out.println (class_name + " is not registered for wrapping."); 418 419 return (ret); 420 } 421 422 427 public void setSelected (boolean selected) 428 { 429 if (selected) 430 setBorder ( 431 new CompoundBorder ( 432 new EtchedBorder (), 433 new CompoundBorder ( 434 new LineBorder(Color.blue, 2), 435 new EmptyBorder (1, 1, 1, 1)))); 436 else 437 setBorder ( 438 new CompoundBorder ( 439 new EtchedBorder (), 440 new EmptyBorder (3,3,3,3))); 441 } 442 443 450 public void setExpanded (boolean expanded) 451 { 452 Component[] components; 453 454 components = getComponents (); 455 for (int i = 0; i < components.length; i++) 456 if (!(components[i] instanceof JLabel)) 457 components[i].setVisible (expanded); 458 } 459 460 public static void spaces (StringBuffer out, int count) 461 { 462 for (int i = 0; i < count; i++) 463 out.append (' '); 464 } 465 466 public static void newline (StringBuffer out) 467 { 468 out.append ('\n'); 469 } 470 } 471 | Popular Tags |