1 32 33 package websphinx.workbench; 34 35 import websphinx.*; 36 import java.io.File ; 37 import java.io.IOException ; 38 import java.net.URL ; 39 import java.net.MalformedURLException ; 40 41 public class MirrorAction implements Action, CrawlListener { 42 String directory; 43 boolean useBrowser; 44 45 transient File dir; 46 transient Mirror mirror; 47 48 public MirrorAction (String directory, boolean useBrowser) { 49 this.directory = directory; 50 this.useBrowser = useBrowser; 51 } 52 53 public boolean equals (Object object) { 54 if (! (object instanceof MirrorAction)) 55 return false; 56 MirrorAction a = (MirrorAction)object; 57 return same (a.directory, directory) 58 && a.useBrowser == useBrowser; 59 } 60 61 private boolean same (String s1, String s2) { 62 if (s1 == null || s2 == null) 63 return s1 == s2; 64 else 65 return s1.equals (s2); 66 } 67 68 public String getDirectory () { 69 return directory; 70 } 71 72 public boolean getUseBrowser () { 73 return useBrowser; 74 } 75 76 private void showit () { 77 Browser browser = Context.getBrowser(); 78 if (browser != null) 79 try { 80 browser.show (Link.FileToURL (dir)); 81 } catch (MalformedURLException e) {} 82 } 83 84 public synchronized void visit (Page page) { 85 try { 86 mirror.writePage (page); 87 } catch (IOException e) { 88 throw new RuntimeException (e.toString()); 89 } 90 } 91 92 public void connected (Crawler crawler) { 93 crawler.addCrawlListener (this); 94 } 95 96 public void disconnected (Crawler crawler) { 97 crawler.removeCrawlListener (this); 98 } 99 100 103 public void started (CrawlEvent event){ 104 if (mirror == null) { 105 try { 106 dir = (directory != null) 107 ? new File (directory) 108 : Access.getAccess ().makeTemporaryFile ("mirror", ""); 109 mirror = new Mirror (dir.toString()); 110 111 Crawler crawler = event.getCrawler (); 112 Link[] roots = crawler.getRoots (); 113 for (int i=0; i<roots.length; ++i) 114 mirror.mapDir (roots[i].getURL(), dir.toString()); 115 } catch (IOException e) { 116 System.err.println (e); } 118 } 119 } 120 121 124 public void stopped (CrawlEvent event){ 125 try { 126 if (mirror != null) { 127 mirror.close (); 128 mirror = null; 129 130 if (useBrowser) 131 showit (); 132 } 133 } catch (IOException e) { 134 System.err.println (e); } 136 } 137 138 141 public void cleared (CrawlEvent event){ 142 try { 143 if (mirror != null) { 144 mirror.close (); 145 mirror = null; 146 147 if (useBrowser) 148 showit (); 149 } 150 } catch (IOException e) { 151 System.err.println (e); } 153 } 154 155 158 public void timedOut (CrawlEvent event){ 159 try { 160 if (mirror != null) { 161 mirror.close (); 162 mirror = null; 163 164 if (useBrowser) 165 showit (); 166 } 167 } catch (IOException e) { 168 System.err.println (e); } 170 } 171 172 175 public void paused (CrawlEvent event){ 176 try { 177 if (mirror != null) { 178 mirror.rewrite (); 179 if (useBrowser) 180 showit (); 181 } 182 } catch (IOException e) { 183 System.err.println (e); } 185 } 186 187 } 188 189 | Popular Tags |