1 32 33 package websphinx.workbench; 34 35 import websphinx.*; 36 import java.io.File ; 37 import java.io.IOException ; 38 39 public class ConcatAction implements Action, CrawlListener { 40 String filename; 41 boolean useBrowser; 42 String prolog, header, footer, divider, epilog; 43 44 transient File file; 45 transient Concatenator concat; 46 47 public ConcatAction (String filename, boolean useBrowser) { 48 this.filename = filename; 49 this.useBrowser = useBrowser; 50 } 51 52 public ConcatAction (String filename, boolean useBrowser, 53 String prolog, String header, String footer, 54 String divider, String epilog) { 55 this (filename, useBrowser); 56 this.prolog = prolog; 57 this.header = header; 58 this.footer = footer; 59 this.divider = divider; 60 this.epilog = epilog; 61 } 62 63 public boolean equals (Object object) { 64 if (! (object instanceof ConcatAction)) 65 return false; 66 ConcatAction a = (ConcatAction)object; 67 return same (a.filename, filename) && a.useBrowser == useBrowser; 68 } 69 70 private boolean same (String s1, String s2) { 71 if (s1 == null || s2 == null) 72 return s1 == s2; 73 else 74 return s1.equals (s2); 75 } 76 77 public String getFilename () { 78 return filename; 79 } 80 81 public boolean getUseBrowser () { 82 return useBrowser; 83 } 84 85 private transient boolean oldSync; 86 87 public void connected (Crawler crawler) { 88 oldSync = crawler.getSynchronous (); 89 crawler.setSynchronous (true); 90 crawler.addCrawlListener (this); 91 } 92 93 public void disconnected (Crawler crawler) { 94 crawler.setSynchronous (oldSync); 95 crawler.removeCrawlListener (this); 96 } 97 98 99 private void showit () { 100 Browser browser = Context.getBrowser(); 101 if (browser != null) 102 browser.show (file); 103 } 104 105 public synchronized void visit (Page page) { 106 try { 107 concat.writePage (page); 108 } catch (IOException e) { 109 throw new RuntimeException (e.toString()); 110 } 111 } 112 113 116 public void started (CrawlEvent event){ 117 if (concat == null) { 118 try { 119 file = (filename != null) 120 ? new File (filename) 121 : Access.getAccess ().makeTemporaryFile ("concat", ".html"); 122 concat = new Concatenator (file.toString()); 123 124 if (prolog != null) 125 concat.setProlog (prolog); 126 if (header != null) 127 concat.setPageHeader (header); 128 if (footer != null) 129 concat.setPageFooter (footer); 130 if (divider != null) 131 concat.setDivider (divider); 132 if (epilog != null) 133 concat.setEpilog (epilog); 134 } catch (IOException e) { 135 System.err.println (e); } 137 } 138 } 139 140 143 public void stopped (CrawlEvent event){ 144 if (concat != null) { 145 try { 146 concat.close (); 147 concat = null; 148 if (useBrowser) 149 showit (); 150 } catch (IOException e) { 151 System.err.println (e); } 153 } 154 } 155 156 159 public void cleared (CrawlEvent event){ 160 try { 161 if (concat != null) { 162 concat.close (); 163 concat = null; 164 if (useBrowser) 165 showit (); 166 } 167 } catch (IOException e) { 168 System.err.println (e); } 170 } 171 172 175 public void timedOut (CrawlEvent event){ 176 try { 177 if (concat != null) { 178 concat.close (); 179 concat = null; 180 if (useBrowser) 181 showit (); 182 } 183 } catch (IOException e) { 184 System.err.println (e); } 186 } 187 188 191 public void paused (CrawlEvent event){ 192 try { 193 if (concat != null) { 194 concat.rewrite (); 195 if (useBrowser) 196 showit (); 197 } 198 } catch (IOException e) { 199 System.err.println (e); } 201 } 202 203 } 204 205 | Popular Tags |