KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > matuschek > examples > StopDownload


1 package net.matuschek.examples;
2
3 /*********************************************
4     Copyright (c) 2001 by Daniel Matuschek
5 *********************************************/

6
7 import java.net.URL JavaDoc;
8
9 import net.matuschek.spider.WebRobot;
10 import net.matuschek.spider.WebRobotCallback;
11
12 import org.apache.log4j.BasicConfigurator;
13
14 /**
15  * This example program shows how it is possible
16  * to use the WebRobotCallback to stop after a given number
17  * of documents has been retrieved.
18  *
19  * @author Daniel Matuschek
20  * @version $Revision: 1.3 $
21  */

22 public class StopDownload {
23
24   class DownloadStopper implements WebRobotCallback {
25     
26     /** maximal number of documents to retrieve */
27     int max = 0;
28
29     /** current number of retrieved documents */
30     int count = 0;
31     
32     /** WebRobot to control */
33     WebRobot robot = null;
34
35
36     public DownloadStopper(int max, WebRobot robot) {
37       this.max = max;
38       this.robot = robot;
39     }
40     
41     /**
42      * Increases the number of retrieved documents and stops
43      * the robot, if the number has reached the maximum
44      */

45     public void webRobotRetrievedDoc(String JavaDoc url, int size) {
46       count ++;
47       if (count >= max) {
48     robot.stopRobot();
49       }
50     }
51
52     // ignore these methods
53
public void webRobotDone() {};
54     public void webRobotSleeping(boolean sleeping) {}
55     public void webRobotUpdateQueueStatus(int length) {}
56   }
57
58
59
60   public StopDownload() {
61   }
62
63
64
65   public void run() throws Exception JavaDoc {
66     WebRobot robby = new WebRobot();
67     robby.setStartURL(new URL JavaDoc("http://www.matuschek.net"));
68     robby.setMaxDepth(1);
69     robby.setSleepTime(0);
70
71     // download only the first 5 documents
72
DownloadStopper stopit = new DownloadStopper(5,robby);
73     robby.setWebRobotCallback(stopit);
74
75     robby.run();
76   }
77
78
79   public static void main(String JavaDoc[] args)
80     throws Exception JavaDoc
81   {
82     BasicConfigurator.configure();
83     StopDownload stopper = new StopDownload();
84     stopper.run();
85   }
86 }
87   
88
Popular Tags