📄 filterbuilder.java
字号:
frame.setClosable (true); frame.setResizable (true); dimension = mOutput.getSize (); frame.setBounds (0, 0, dimension.width, dimension.height); list = new NodeList (); try { parser = new Parser (mURLField.getText ()); try { for (NodeIterator iterator = parser.elements (); iterator.hasMoreNodes (); ) list.add (iterator.nextNode ()); } catch (EncodingChangeException ece) { list.removeAll (); parser.reset (); for (NodeIterator iterator = parser.elements (); iterator.hasMoreNodes (); ) list.add (iterator.nextNode ()); } } catch (ParserException pe) { pe.printStackTrace (); } JTree tree = new JTree (new HtmlTreeModel (list)); tree.setRootVisible (false); tree.setCellRenderer (new HtmlTreeCellRenderer ()); JScrollPane treeView = new JScrollPane (tree); frame.setContentPane (new JScrollPane ( treeView, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED)); mOutput.add (frame, new Integer (1)); try { frame.setSelected (true); } catch (PropertyVetoException pve) { pve.printStackTrace (); } frame.show (); } /** * The action to take when "Execute" menu or button pressed. */ protected void executeAction () { Filter[] selections; FilterBean bean; JInternalFrame frame; Dimension dimension;// JTextPane text; selections = getSelection (); if (0 == selections.length) selections = getFilters (); if (0 != selections.length) { bean = new FilterBean (); bean.setURL (mURLField.getText ()); bean.setFilters (selections); // set up an internal frame for the results frame = new JInternalFrame (bean.getURL ()); frame.setClosable (true); frame.setResizable (true); dimension = mOutput.getSize (); frame.setBounds (0, 0, dimension.width, dimension.height / 2); JTree tree = new JTree (new HtmlTreeModel (bean.getNodes ())); tree.setRootVisible (false); tree.setCellRenderer (new HtmlTreeCellRenderer ()); JScrollPane treeView = new JScrollPane (tree); frame.setContentPane (new JScrollPane ( treeView, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED));// text = new JTextPane ();// text.setText (bean.getNodes ().toHtml ());// frame.setContentPane (new JScrollPane (// text,// ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,// ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED)); mOutput.add (frame, new Integer(2)); // layer 2? try { frame.setSelected (true); } catch (PropertyVetoException pve) { pve.printStackTrace (); } frame.show (); } } /** * The action to take when "Instructions" menu pressed. */ protected void instructionsAction () { String instructions = "<html>" + "Enter the target URL in the text box at the bottom of the window.<br>" + "Choose 'Fetch Page' from the Operations menu to see the whole page.<br>" + "Pick filters from the Filter menu or drag them from the toolbar.<br>" + "Filters such as And, Or, Not, HasParent, HasChild and HasSibling contain other filters:<br>" + "<ul><li>drag new filters into their blank areas at the bottom</li>" + "<li>cut an existing filter and paste into a selected filter</li></ul>" + "Build the filter incrementally, choosing 'Execute Filter' to test the selected filter.<br>" + "Save creates a .java file that runs the top level filter.<br>" + "Right click on a filter displays a pop-up menu.<br>" + "Double click on a blue item in the result pane expands the tree." + "</html>"; String close = "Close"; JOptionPane.showOptionDialog ( // not .showMessageDialog( mMainPanel, instructions, "FilterBuilder Instructons", // remove this: JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, // and remove rest of these: null, new String[] { close }, close); } /** * The action to take when "Filtering" menu pressed. */ protected void filteringAction () { String instructions = "<html>" + "The HTML Parser filter subsystem extracts items from a web page,<br>" + "corresponding to the use-case 'I want this little piece of information from http://yadda'.<br>" + "The web page is considered a heirarchical tree of nodes. Usually the root node is <html>,<br>" + "intermediate level nodes are <div> and <table> for example,<br>" + "and leaf nodes are things like text or <img>.<br>" + "Any node that isn't the root node has a 'parent' node.<br>" + "Leaf nodes, by definition, have no 'children'.<br>" + "A filter is a Java class that answers the simple question:<br>" + "<pre>Is this node acceptable? True or false.</pre><br>" + "Some filters know the answer just by looking at the node,<br>" + "while others must ask other filters, sometimes looking up or down the node heirarchy.<br>" + "<b>The FilterBuilder is a program for making other programs that use filters.</b><br>" + "By combining different types of filters, specific nodes can be isolated from the<br>" + "target web page.<br>" + "The results are usually passed on to another part of the users program<br>" + "that does something useful with them.<br>" + "The filters available include:<br>" + "<ul>" + "<li>AndFilter - The main 'combining' filter, answers <code>true</code> only if<br>" + "all it's subfilters (predicates) are <code>true</code>.</li>" + "<li>OrFilter - A 'combining' filter that answers <code>true</code> if<br>" + "any of it's subfilters (predicates) are <code>true</code>.</li>" + "<li>NotFilter - A 'reversing' filter that answers <code>true</code> if<br>" + "it's subfilter (predicate) is <code>false</code>.</li>" + "<li>StringFilter - A 'leaf' filter that answers <code>true</code> if<br>" + "the node is text and it contains a certain sequence of characters.<br>" + "It can be made case insensitive, but in this case a 'locale' must be<br>" + "supplied as a context for upper-case conversion.</li>" + "<li>RegexFilter - A 'leaf' filter that answers <code>true</code> if<br>" + "the node is text and it contains a certain pattern (regular expression).<br>" + "Regular expressions are descibed in the java.util.regex.Pattern class documentation.</li>" + "<li>TagNameFilter - A filter that answers <code>true</code> if<br>" + "the node is a tag and it has a certain name," + "i.e. <div> would match the name <code>DIV</code>.</li>" + "<li>NodeClassFilter - A filter that answers <code>true</code> if<br>" + "the node is a certain tag class. Not recommended, use TagNameFilter instead.</li>" + "<li>HasAttributeFilter - A filter that answers <code>true</code> if<br>" + "the node is a tag and it has a certain attribute,<br>" + "i.e. <script language=javascript> would match the attribute <code>LANGUAGE</code>.<br>" + "It can be further restricted to have a certain attribute value as well,<br>" + "i.e. 'javascript' in this example.</li>" + "<li>HasParentFilter - A filter that answers <code>true</code> if<br>" + "the node is a child of a node that is acceptable to a certain filter.<br>" + "This can be made recursive, which means the acceptable parent can be<br>" + "further up the heirarchy than just the immediate parent node.</li>" + "<li>HasChildFilter - A filter that answers <code>true</code> if<br>" + "the node is a parent of a node that is acceptable to a certain filter.<br>" + "This can be made recursive, which means the acceptable child can be<br>" + "further down the heirarchy than just the immediate children nodes.</li>" + "<li>HasSiblingFilter - A filter that answers <code>true</code> if<br>" + "the node is a sibling (they have a common parent) of a node that is<br>" + "acceptable to a certain filter.</li>" + "</ul>" + "</html>"; String close = "Close"; JOptionPane.showOptionDialog ( // not .showMessageDialog( mMainPanel, instructions, "FilterBuilder Instructons", // remove this: JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, // and remove rest of these: null, new String[] { close }, close); } /** * The action to take when "Tutorial" menu pressed. */ protected void tutorialAction () { String instructions = "<html>" + "To get the title text from a page:<br>" + "<ul><li>Choose 'New' from the File menu.</li>" + "<li>Choose 'AndFilter' from the Filter menu.</li>" + "<li>Select the And filter so it is highlighted.</li>" + "<li>Choose 'HasParent' from the Filter menu.</li>" + "<li>Toggle the 'Recursive' checkbox on in the HasParent filter.</li>" + "<li>Select the HasParent filter so it is highlighted.</li>" + "<li>Choose 'TagName' from the Filter menu.<br>" + "<i>Alternatively, you can drag the TagName filter (icon Hello-BOB)<br>" + "from the toolbar and drop inside the HasParent filter</i></li>" + "<li>Choose 'TITLE' from the TagName combo-box.</li>" + "<li>Select the And filter and choose 'Execute Filter' from the<br>" + "Operations menu to test it.</li>" + "<li>If there is unwanted non-text nodes in the result<br>" + "select the And filter and choose 'RegexFilter' from the Filter menu.</li>" + "<li>Test it again, as above.</li>" + "<li>Choose 'Save' from the File menu and enter a filename like GetTitle.java</li>" + "<li>Compile the java file and run it.</li></ul>" + "</html>"; String close = "Close"; JOptionPane.showOptionDialog ( // not .showMessageDialog( mMainPanel, instructions, "FilterBuilder Tutorial", // remove this: JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, // and remove rest of these: null, new String[] { close }, close); } /** * The action to take when "Hints" menu pressed. */ protected void hintsAction () { String instructions = "<html>" + "Hints:<br>" + "<ul><li>There is no undo yet, so save often.</li>" + "<li>Recursive HasParent and HasChild filters can be costly.</li>" + "<li>RegexFilter is more expensive than StringFilter.</li>" + "<li>The order of predicates in And and Or filters matters for performance,<br>" + "put cheap tests first.</li>" + "<li>The same node may show up more than once in the results,<br>" + "and at more than one nesting depth, depending on the filter used.</li>" + "<li>Typing in a tag name in the TagName filter is not recommended,<br>" + "since extraneous characters can be added. Use an item from the list instead.</li></ul>" + "</html>"; String close = "Close"; JOptionPane.showOptionDialog ( // not .showMessageDialog( mMainPanel, instructions, "FilterBuilder Hints", // remove this: JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, // and remove rest of these: null, new String[] { close }, close); } /** * The action to take when "About" menu or button pressed. */ protected void aboutAction () { String close = "Close"; JOptionPane.showOptionDialog ( // not .showMessageDialog( mMainPanel, "<html><center><font color=black>The HTML Parser <font color=blue><b>FilterBuilder</b></font><br><i>by Derrick Oswald</i> <b>DerrickOswald@users.sourceforge.net</b><br>http://htmlparser.org<br><br><font size=-2>Copyright © 2005</font></center></html>", "About FilterBuilder", // remove this: JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, // and remove rest of these: null, new String[] { close }, close); } /** * The action to take when "Expand" menu chosen. */ public void expandAction () { setExpanded (getSelection (), true, false); } /** * The action to take when "Collapse" menu chosen. */ public void collapseAction () { setExpanded (getSelection (), false, false); } /** * The action to take when "Expand All" menu chosen. */ public void expandAllAction () { setExpanded (getSelection (), true, true); } /** * The action to take when "Collapse" menu chosen. */ public void collapseAllAction () { setExpanded (getSelection (), false, true); } /** * Set up mouse listeners. * Sets <code>this</code> up to listen to each command * in the list as a MouseListener. * Recursively descends the tree adding to all contained elements also. * @param filters The container with commands in it. */ public void setupMouseListeners (Filter[] filters) { SubFilterList list; for (int i = 0; i < filters.length; i++) { // set us up as a mouse listener on it ((Component)filters[i]).addMouseListener (this); ((Component)filters[i]).addMouseMotionListener (this); list = getEnclosed (filters[i]); if (null != list) setupMouseListeners (list.getFilters ()); } } /** * Set up drop targets. * Recursively descends the filter tree and sets up * the filter lists as drop targets. * @param filters The container with filters in it. */ public void setupDropTargets (Filter[] filters) { SubFilterList list; Component[] components; for (int i = 0; i < filters.length; i++) { list = getEnclosed (filters[i]); if (null != list) { components = list.getDropTargets (); for (int j = 0; j < components.length; j++) new DropTarget (components[j], this); setupDropTargets (list.getFilters ()); } } } /** * Expand or collapse filters, possibly recursively. * @param filters The list of filters to expand or collapse. * @param expanded If <code>true</code> the filters are expanded, * otherwise they are collapsed. * @param recursive If <code>true</code> the filters are processed * recursively. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -