📄 filter.java
字号:
// HTMLParser Library $Name: v1_6_20060319 $ - A java-based parser for HTML// http://sourceforge.org/projects/htmlparser// Copyright (C) 2005 Derrick Oswald//// Revision Control Information//// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/parserapplications/filterbuilder/Filter.java,v $// $Author: derrickoswald $// $Date: 2005/04/12 11:27:42 $// $Revision: 1.2 $//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//package org.htmlparser.parserapplications.filterbuilder;import java.awt.*;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.Hashtable;import java.util.Vector;import javax.swing.*;import javax.swing.border.*;import org.htmlparser.NodeFilter;import org.htmlparser.Parser;import org.htmlparser.parserapplications.filterbuilder.layouts.VerticalLayoutManager;/** * Base class for all filters. * Provides common functionality applicable to all filters. */public abstract class Filter extends JComponent implements NodeFilter{ /** * Create a new filter from the class name. * @param class_name The class to instatiate. * @return The constructed filter object. */ public static Filter instantiate (String class_name) { Filter ret; ret = null; try { Class cls = Class.forName (class_name); ret = (Filter)cls.newInstance (); mWrappers.put (ret.getNodeFilter ().getClass ().getName (), class_name); } catch (ClassNotFoundException cnfe) { System.out.println ("can't find class " + class_name); } catch (InstantiationException ie) { System.out.println ("can't instantiate class " + class_name); } catch (IllegalAccessException ie) { System.out.println ("class " + class_name + " has no public constructor"); } catch (ClassCastException cce) { System.out.println ("class " + class_name + " is not a Filter"); } return (ret); } /** * Map from cilter class to wrapper. * Populated as part of each wrapper being loaded. */ protected static Hashtable mWrappers = new Hashtable (); /** * Create a filter. * Set up the default display. * Only a border with the label of the filter name, * returned by <code>getDescription()</code>, * and an icon, returned by <code>getIcon()</code>. */ public Filter () { JLabel label; Dimension dimension; Insets insets; setToolTipText (getDescription ()); // none of these quite does it: // new BoxLayout (this, BoxLayout.Y_AXIS)); // new GridLayout (0, 1)); setLayout (new VerticalLayoutManager ()); setSelected (false); label = new JLabel (getDescription (), getIcon (), SwingConstants.LEFT); label.setBackground (Color.green); label.setAlignmentX (Component.LEFT_ALIGNMENT); label.setHorizontalAlignment (SwingConstants.LEFT); add (label); dimension = label.getMaximumSize (); insets = getInsets (); dimension.setSize (dimension.width + insets.left + insets.right, dimension.height + insets.top + insets.bottom); setSize (dimension); } /** * Get the name of the filter. * @return A descriptive name for the filter. */ public abstract String getDescription (); /** * Get the underlying node filter object. * @return The node filter object suitable for serialization. */ public abstract NodeFilter getNodeFilter (); /** * Assign the underlying node filter for this wrapper. * @param filter The filter to wrap. * @param context The parser to use for conditioning this filter. * Some filters need contextual information to provide to the user, * i.e. for tag names or attribute names or values, * so the Parser context is provided. */ public abstract void setNodeFilter (NodeFilter filter, Parser context); /** * Get the underlying node filter's subordinate filters. * @return The node filter object's contained filters. */ public abstract NodeFilter[] getSubNodeFilters (); /** * Assign the underlying node filter's subordinate filters. * @param filters The filters to insert into the underlying node filter. */ public abstract void setSubNodeFilters (NodeFilter[] filters); /** * Convert this filter into Java code. * Output whatever text necessary and return the variable name. * @param out The output buffer. * @param context Three integers as follows: * <li>indent level - the number of spaces to insert at the beginning of each line</li> * <li>filter number - the next available filter number</li> * <li>filter array number - the next available array of filters number</li> * @return The variable name to use when referencing this filter (usually "filter" + context[1]++) */ public abstract String toJavaCode (StringBuffer out, int[] context); /** * Get the icon for the filter. * Loads the resource specified by * <code>getIconSpec()</code> as an icon. * @return The icon or null if it was not found. */ public Icon getIcon () { ImageIcon ret; ret = null; try { ret = new ImageIcon (getClass ().getResource (getIconSpec ())); } catch (NullPointerException npe) { System.err.println ("can't find icon " + getIconSpec ()); } return (ret); } /** * Get the resource name for the icon. * @return The icon resource specification. */ public abstract String getIconSpec (); // // Component overrides // /** * Returns a string representation of this component and its values. * @return A string representation of this component. */ public String toString () { return (getDescription () + " [" + this.getClass ().getName () + "]"); } // // utilities // /** * Serialize an object to a byte array. * @param object The object to be pickled. * @return The serialized object. * @exception IOException If the output stream complains (unlikely). */ public static byte[] pickle (Object object) throws IOException { ByteArrayOutputStream bos; ObjectOutputStream oos; byte[] ret; bos = new ByteArrayOutputStream (); oos = new ObjectOutputStream (bos); oos.writeObject (object); oos.close (); ret = bos.toByteArray (); return (ret); } /** * Reconstitute a serialized object. * @param data The pickled object. * @return The reconstituted object. * @exception IOException If the input stream complains. * @exception ClassNotFoundException If the serialized object class cannot * be located. */ public static Object unpickle (byte[] data) throws IOException, ClassNotFoundException { ByteArrayInputStream bis; ObjectInputStream ois;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -