📄 filterbuilder.java
字号:
panel.add (toolbar); getContentPane().setLayout (new BorderLayout (0,0)); getContentPane ().add (BorderLayout.NORTH, panel); // URL entry mURLField = new JTextField (); mURLField.setToolTipText ("Enter the URL to view");// mTextField.addActionListener (this); mURLField.setText ("http://sourceforge.org/projects/htmlparser"); getContentPane().add (BorderLayout.SOUTH, mURLField); // application setup setTitle (TITLE); setDefaultCloseOperation (WindowConstants.DO_NOTHING_ON_CLOSE); setSize (640, 480); setVisible (false); // main panel mMainPanel.setLayout (new NullLayoutManager ()); mMainScroller = new JScrollPane ( mMainPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); split = new JSplitPane (); pane = new JScrollPane (); pane.setViewportView (mMainScroller); split.setLeftComponent (pane); mOutput = new JDesktopPane (); split.setRightComponent (mOutput); getContentPane().add (BorderLayout.CENTER, split); // shenanigans to get the splitter bar at the midpoint setVisible (true); split.setDividerLocation (0.5); setVisible (false); // listeners addWindowListener (this); setIconImage (Toolkit.getDefaultToolkit ().getImage ("images/program16.gif")); addMouseListener (this); addMouseMotionListener (this); // clipboard buffer mSelection = new Vector (); } /** * Creates a new instance of an FilterBuilder environment with the given title. * @param title the title for the new frame. * @see #FilterBuilder() */ public FilterBuilder (String title) { this (); setTitle (title); } /** * Makes menu and toolbar items for commands. * @param name The name of the command. * @param description A description for the tooltip. * @param text The text for the menu. * @param mnemonic The navigation mnemonic. * @param key Accelerator key. * @param toolbar The toolbar to add the button to. * @param menu The menu to add the menu item to. */ protected void makeMenuButton ( String name, String description, String text, int mnemonic, KeyStroke key, JToolBar toolbar, JMenu menu) { JButton button; JMenuItem item; ImageIcon icon; String command; command = name.toLowerCase (); try { icon = new ImageIcon (getURL ("images/" + command + ".gif")); } catch (java.net.MalformedURLException error) { icon = null; } item = new JMenuItem (); item.setText (text); item.setActionCommand (command + "Action"); item.setAccelerator (key); item.setMnemonic (mnemonic); item.setIcon (icon); item.addActionListener (this); menu.add (item); if (null != toolbar) { button = new JButton (); button.setDefaultCapable (false); button.setToolTipText (description); button.setMnemonic (mnemonic); button.setActionCommand (command + "Action"); button.setMargin (new Insets (0, 0, 0, 0)); button.setIcon (icon); button.addActionListener (this); toolbar.add (button); } } /** * Get a url for the given resource specification. * @param spec The name of the resource. * @return The fully formed URL. * @exception MalformedURLException In the case that the document base * or name of the resource cannot be turned into a URL. */ protected URL getURL (String spec) throws MalformedURLException { URL ret; if (null == (ret = getClass ().getResource (spec))) if ((null != mDocumentBase) && (-1 == spec.indexOf ("//"))) ret = new URL (mDocumentBase, spec); else ret = new URL (spec); return ret; } /** * Creates a new button for the given class. * @param class_name The name of the Filter class. * @return A fully functional button with name, tool tip, * icon and drag recognizer. */ public JButton makeFilterButton (String class_name) { Filter filter; JButton ret; ret = new JButton (); filter = Filter.instantiate (class_name); if (null != filter) { ret.setName (class_name); // filter.getNodeFilter ().getClass ().getName ()); ret.setToolTipText (filter.getDescription ()); ret.setMargin (new Insets (0, 0, 0, 0)); ret.setIcon (filter.getIcon ()); mDragSource.createDefaultDragGestureRecognizer ( ret, DnDConstants.ACTION_MOVE, this); ret.setActionCommand ("filterAction"); ret.addActionListener (this); } return (ret); } /** * Add a filter to the GUI. * Adds the filter specified by class_name to the menu, toolbar and * starts listening for actions. * @param menu The menu to add the filter to. * @param toolbar The toolbar to add the filter to. * @param class_name The class name for the filter wrapper. * From the wrapper, the NodeFilter, description and icon can be obtained. */ public void addFilter (JMenu menu, JToolBar toolbar, String class_name) { Filter filter; filter = Filter.instantiate (class_name); if (null != filter) { String name; String description; Icon icon; String text; JMenuItem item; name = filter.getNodeFilter ().getClass ().getName (); description = filter.getDescription (); icon = filter.getIcon (); text = name.substring (name.lastIndexOf ('.') + 1); item = new JMenuItem (); item.setName (class_name); item.setText (text); item.setActionCommand ("filterAction");// item.setAccelerator (key);// item.setMnemonic (mnemonic); item.setToolTipText (description); item.setIcon (icon); item.addActionListener (this); menu.add (item); toolbar.add (makeFilterButton (class_name)); } } /** * Adds a set of filters to the main panel or a sublist. * Sets up the GUI components as drop targets and mouse listeners, * and performs a relayout to display them. * @param filters The filter wrappers to add. * @param point The point at which to start adding (list == null). * @param list The list to add to (point not used), or <code>null</code> * for the main panel. */ protected void insertFilters (Filter[] filters, Point point, SubFilterList list) { Dimension dimension; if (null == list) { for (int i = 0; i < filters.length; i++) { filters[i].setLocation (point); mMainPanel.add (filters[i]); dimension = filters[i].getPreferredSize (); point.y += dimension.height; } } else for (int i = 0; i < filters.length; i++) list.addFilter (filters[i]); setupDropTargets (filters); setupMouseListeners (filters); relayout (); } /** * Sets the position of the mouse in the component. * * @param point The point where the mouse position is. */ protected void setBasePoint (Point point) { mBasePoint = point; } /** * Gets the current base point of the mouse pointer. * This value is used to offset the drag position * to maintain the mouse position at the same * relative position within the card while dragging. * * @return The current base point of the mouse pointer. */ protected Point getBasePoint () { return (mBasePoint); } /** * Get the enclosing sub filter list if any. * @param component The component that's supposedly enclosed. * @return The enclosing component or <code>null</code> otherwise. */ protected SubFilterList getEnclosing (Component component) { do component = component.getParent (); while ( (null != component) && !(component instanceof SubFilterList)); return ((SubFilterList)component); } /** * Get the enclosed sub filter list if any. * @param component The component that's supposedly enclosing the list. * @return The enclosed component or <code>null</code> otherwise. */ protected SubFilterList getEnclosed (Component component) { Component[] list; if (component instanceof Container) { list = ((Container)component).getComponents (); for (int i = 0; i < list.length; i++) if (list[i] instanceof SubFilterList) return ((SubFilterList)list[i]); } return (null); } /** * Makes a program like: * <pre> * // Generated by FilterBuilder. http://htmlparser.org * // [aced0005737200206f72672e68746d6c7061727365722e66696c746572732e416e6446696c74657224c30516b2b7b2120200015b000b6d5072656469636174657374001c5b4c6f72672f68746d6c7061727365722f4e6f646546696c7465723b78707572001c5b4c6f72672e68746d6c7061727365722e4e6f646546696c7465723b8f17479b1d5f7992020000787000000002737200246f72672e68746d6c7061727365722e66696c746572732e5461674e616d6546696c746572b28b2601a614890f0200014c00056d4e616d657400124c6a6176612f6c616e672f537472696e673b78707400044d455441737200296f72672e68746d6c7061727365722e66696c746572732e48617341747472696275746546696c74657296abdfb3b0714cda0200024c000a6d41747472696275746571007e00064c00066d56616c756571007e000678707400046e616d6570] * * import org.htmlparser.*; * import org.htmlparser.filters.*; * import org.htmlparser.beans.*; * import org.htmlparser.util.*; * * public class Test * { * public static void main (String args[]) * { * TagNameFilter filter0 = new TagNameFilter (); * filter0.setName ("META"); * HasAttributeFilter filter1 = new HasAttributeFilter (); * filter1.setAttributeName ("name"); * NodeFilter[] array0 = new NodeFilter[2]; * array0[0] = filter0; * array0[1] = filter1; * AndFilter filter2 = new AndFilter (); * filter2.setPredicates (array0); * NodeFilter[] array1 = new NodeFilter[1]; * array1[0] = filter2; * FilterBean bean = new FilterBean (); * bean.setFilters (array1); * if (0 != args.length) * { * bean.setURL (args[0]); * System.out.println (bean.getNodes ().toHtml ()); * } * else * System.out.println ("Usage: java -classpath .:htmlparser.jar Test <url>"); * } * } * </pre> * @param name The name of the class. * @param out The buffer to append to. * @param bean The bean to extract the filters from to make the program. */ protected void makeProgram (String name, StringBuffer out, FilterBean bean) { // so we need to keep track of filters and arrays of filters to give them unique numbers // each Filter is responsible for outputting it's code and returning it's variable name int[] context; // 0 - indent, 1 - next filter variable #, 2 - next array of filters variable # String[] names; Filter[] filters; String array; filters = (Filter[])bean.getFilters (); context = new int[3]; context[0] = 0; Filter.spaces (out, context[0]); out.append ("// Generated by FilterBuilder. http://htmlparser.org"); Filter.newline (out); Filter.spaces (out, context[0]); out.append ("// "); try { out.append (Filter.deconstitute (filters)); } catch (IOException ioe) { ioe.printStackTrace (); } Filter.newline (out); Filter.newline (out); Filter.spaces (out, context[0]); out.append ("import org.htmlparser.*;"); Filter.newline (out); Filter.spaces (out, context[0]); out.append ("import org.htmlparser.filters.*;"); Filter.newline (out); Filter.spaces (out, context[0]); out.append ("import org.htmlparser.beans.*;"); Filter.newline (out); Filter.spaces (out, context[0]); out.append ("import org.htmlparser.util.*;"); Filter.newline (out); Filter.newline (out); Filter.spaces (out, context[0]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -