View Javadoc
1 // $Id: BatchWorker.java,v 1.1.1.1 2003/03/09 15:21:07 powerpete Exp $ 2 package jface.batchget; 3 4 /*** 5 * The <tt>BatchWorker</tt> processes one unique url and downloads all links 6 * specified by a {@link Filter}. It uses the {@link Downloader} to download 7 * the links. 8 * 9 * @author Moritz Petersen 10 * @version $Revision: 1.1.1.1 $ 11 */ 12 public class BatchWorker implements Runnable 13 { 14 private String url; 15 private Filter filter; 16 private String destDir; 17 18 /*** 19 * Creates a new <tt>BatchWorker</tt> for the given <tt>url</tt>. 20 * All links will be passed through the <tt>filter</tt>, to identify the 21 * links to download. They will be saved into the <tt>destDir</tt>. 22 * 23 * @param url The url that represents the HTML file. All links of 24 * this file will be passed through the <tt>filter</tt> to 25 * identify the downloadable links. 26 * @param filter The filter identifies the downloadable links. 27 * @param destDir The links will be downloaded into this directory. 28 */ 29 public BatchWorker(String url, Filter filter, String destDir) 30 { 31 this.url = url; 32 this.filter = filter; 33 this.destDir = destDir; 34 } 35 36 /*** 37 * Starts the downloading process. 38 */ 39 public void run() 40 { 41 String[] hrefs = filter.apply(new HtmlLoader().fetch(url)); 42 for (int i = 0; i < hrefs.length; i++) 43 { 44 // new Thread(new DownloadWorker(url, hrefs[i], destDir)).start(); 45 new DownloadWorker(url, hrefs[i], destDir).run(); 46 } 47 } 48 49 /*** 50 * A helper class to download each link. 51 */ 52 private class DownloadWorker implements Runnable 53 { 54 private String url; 55 private String href; 56 private String destDir; 57 58 /*** 59 * Creates a new <tt>DownloadWorker</tt>. The link will be 60 * specified by the <tt>url</tt> and a relative <tt>href</tt>. 61 * Both will be combined and downloaded into the <tt>destDir</tt>. 62 * The <tt>url</tt> is the complete url of the HTML page, including 63 * the filename. 64 * 65 * @param url The url of the HTML webpage, including the 66 * filename. 67 * @param href The relative reference of a link to download. 68 * @param destDir The destination directory where the link is saved. 69 */ 70 public DownloadWorker(String url, String href, String destDir) 71 { 72 this.url = url; 73 this.href = href; 74 this.destDir = destDir; 75 } 76 77 /*** 78 * Runs the download process. 79 */ 80 public void run() 81 { 82 try 83 { 84 UrlBuffer buffer = new UrlBuffer(UrlUtils.stripFileName(url)); 85 buffer.appendUrl(href); 86 Downloader downloader = new Downloader(buffer.toString()); 87 downloader.downloadTo(destDir + UrlUtils.getFileName(href)); 88 } 89 catch (Exception e) 90 { 91 e.printStackTrace(); 92 } 93 } 94 } 95 }

This page was automatically generated by Maven