📄 filterbuilder.java
字号:
out.append ("public class "); out.append (name); Filter.newline (out); Filter.spaces (out, context[0]); out.append ("{"); context[0] = 4; Filter.newline (out); Filter.spaces (out, context[0]); out.append ("public static void main (String args[])"); Filter.newline (out); Filter.spaces (out, context[0]); out.append ("{"); Filter.newline (out); context[0] = 8; names = new String [filters.length]; for (int i = 0; i < names.length; i++) names[i] = filters[i].toJavaCode (out, context); array = "array" + context[2]++; Filter.spaces (out, context[0]); out.append ("NodeFilter[] "); out.append (array); out.append (" = new NodeFilter["); out.append (filters.length); out.append ("];"); Filter.newline (out); for (int i = 0; i < filters.length; i++) { Filter.spaces (out, context[0]); out.append (array); out.append ("["); out.append (i); out.append ("] = "); out.append (names[i]); out.append (";"); Filter.newline (out); } Filter.spaces (out, context[0]); out.append ("FilterBean bean = new FilterBean ();"); Filter.newline (out); Filter.spaces (out, context[0]); out.append ("bean.setFilters ("); out.append (array); out.append (");"); Filter.newline (out); Filter.spaces (out, context[0]); out.append ("if (0 != args.length)"); Filter.newline (out); Filter.spaces (out, context[0]); out.append ("{"); Filter.newline (out); context[0] = 12; Filter.spaces (out, context[0]); out.append ("bean.setURL (args[0]);"); Filter.newline (out); Filter.spaces (out, context[0]); out.append ("System.out.println (bean.getNodes ().toHtml ());"); Filter.newline (out); context[0] = 8; Filter.spaces (out, context[0]); out.append ("}"); Filter.newline (out); Filter.spaces (out, context[0]); out.append ("else"); Filter.newline (out); context[0] = 12; Filter.spaces (out, context[0]); out.append ("System.out.println (\"Usage: java -classpath .:htmlparser.jar "); out.append (name); out.append (" <url>\");"); Filter.newline (out); context[0] = 4; Filter.spaces (out, context[0]); out.append ("}"); Filter.newline (out); context[0] = 0; Filter.spaces (out, context[0]); out.append ("}"); Filter.newline (out); } /** * Extracts a java class name from a file name. * ToDo: make this package-smart somehow. * @param file The name of the file. * @return The name of the class. */ protected String classFromFile (String file) { String filesep; int index; // remove any path filesep = System.getProperty ("file.separator"); index = file.lastIndexOf (filesep); if (-1 != index) file = file.substring (index + filesep.length ()); // remove the extension index = file.indexOf ('.'); if (-1 != index) file = file.substring (0, index); return (file); } /** * Save the workspace contents to file. * @param name The name of the file to save it under. */ public void save (String name) { Filter[] selections; FilterBean bean; StringBuffer buffer; PrintWriter out; String ok = "OK"; selections = getFilters (); if (0 != selections.length) { bean = new FilterBean (); bean.setURL (mURLField.getText ()); bean.setFilters (selections); buffer = new StringBuffer (); makeProgram (classFromFile (name), buffer, bean); try { out = new PrintWriter (new FileWriter (name), true); try { out.write (buffer.toString ()); out.flush (); } finally { out.close (); } } catch (IOException ioe) { ioe.printStackTrace (); } } else // ToDo: grey out save option if nothing to save... JOptionPane.showOptionDialog ( mMainPanel, "No filters to save.", "Oops", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, new String[] { ok }, ok); } /** * The action to take when "New" menu or button pressed. */ protected void newAction () { mMainPanel.removeAll (); mSelection.clear (); relayout (); } /** * The action to take when "Open" menu or button pressed. */ protected void openAction () { FileDialog dialog; File file; dialog = new FileDialog (this); dialog.setMode (FileDialog.LOAD); dialog.setTitle ("Open"); dialog.setDirectory (mHomeDir); dialog.setVisible (true); if (null != dialog.getFile ()) { mHomeDir = dialog.getDirectory (); file = new File (mHomeDir + dialog.getFile ()); open (file.getAbsolutePath ()); setTitle (TITLE + " - " + file.getAbsolutePath ()); } } /** * The action to take when "Save" menu or button pressed. */ protected void saveAction () { String title; int index; File file; FileDialog dialog; title = getTitle (); index = title.indexOf (" - "); if (-1 != index) file = new File (title.substring (index + 3)); else { dialog = new FileDialog (this); dialog.setMode (FileDialog.SAVE); dialog.setTitle ("Save"); dialog.setDirectory (mHomeDir); dialog.setVisible (true); if (null != dialog.getFile ()) { mHomeDir = dialog.getDirectory (); file = new File (mHomeDir + dialog.getFile ()); setTitle (TITLE + " - " + file.getAbsolutePath ()); } else file = null; } if (null != file) save (file.getAbsolutePath ()); } /** * The action to take when "Save As" menu or button pressed. */ protected void saveasAction () { setTitle (TITLE); saveAction (); } /** * The action to take when "Exit" menu or button pressed. */ protected void exitAction () { exitApplication (); } /** * The action to take when "Cut" menu or button pressed. */ protected void cutAction () { String string; StringSelection contents; Clipboard cb; // get the selection string = serializeSelection (); // copy to clipboard contents = new StringSelection (string); cb = Toolkit.getDefaultToolkit ().getSystemClipboard (); cb.setContents (contents, this); // delete the selection deleteSelection (); relayout (); } /** * The action to take when "Copy" menu or button pressed. */ protected void copyAction () { String string; StringSelection contents; Clipboard cb; // get the selection string = serializeSelection (); // copy to clipboard contents = new StringSelection (string); cb = Toolkit.getDefaultToolkit ().getSystemClipboard (); cb.setContents (contents, this); } /** * The action to take when "Paste" menu or button pressed. */ protected void pasteAction () { Clipboard cb; Transferable content; String string; Filter[] filters; Point point; SubFilterList list; // get the text cb = Toolkit.getDefaultToolkit ().getSystemClipboard (); content = cb.getContents (this); if (content.isDataFlavorSupported (DataFlavor.stringFlavor)) { try { string = (String)content.getTransferData (DataFlavor.stringFlavor); // deserialize it and add into the selection filters = Filter.reconstitute (string, new Parser (mURLField.getText ())); // add it to the (single) selected object or main panel if (isSingleSelection () && (null != (list = getEnclosed (getSelection ()[0])))) { for (int i = 0; i < filters.length; i++) list.addFilter (filters[i]); } else { point = new Point (0,0); for (int i = 0; i < filters.length; i++) { filters[i].setLocation (point); mMainPanel.add (filters[i]); point.y += filters[i].getPreferredSize ().height; } } setupMouseListeners (filters); setupDropTargets (filters); relayout (); } catch (Exception e) { e.printStackTrace (); } } } /** * The action to take when "Delete" menu or button pressed. */ protected void deleteAction () { // delete the selection deleteSelection (); relayout (); } /** * The action to take when a filter menu or button pressed. */ protected void filterAction () { String cls; Filter filter; SubFilterList list; Point point; // retrieve the source component placed there by actionPerformed cls = mCurrentComponent.getName (); filter = Filter.instantiate (cls); // need this to get the underlying filter prepped? try { filter = Filter.wrap (filter.getNodeFilter (), new Parser (mURLField.getText ())); } catch (ParserException pe) { pe.printStackTrace (); } // add it to the (single) selected object or main panel if (isSingleSelection () && (null != (list = getEnclosed (getSelection ()[0])))) { insertFilters (new Filter[] {filter}, null, list); } else { point = new Point (50,50); // find where and who to stick it into insertFilters (new Filter[] {filter}, point, null); } } /** * The action to take when "Fetch" menu pressed. */ protected void fetchAction () { JInternalFrame frame; Dimension dimension; Parser parser; NodeList list; // set up an internal frame for the results frame = new JInternalFrame (mURLField.getText ());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -