KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > workbench > MirrorAction


1 /*
2  * WebSphinx web-crawling toolkit
3  *
4  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
20  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
23  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33 package websphinx.workbench;
34
35 import websphinx.*;
36 import java.io.File JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.net.URL JavaDoc;
39 import java.net.MalformedURLException JavaDoc;
40
41 public class MirrorAction implements Action, CrawlListener {
42     String JavaDoc directory;
43     boolean useBrowser;
44
45     transient File JavaDoc dir;
46     transient Mirror mirror;
47
48     public MirrorAction (String JavaDoc directory, boolean useBrowser) {
49         this.directory = directory;
50         this.useBrowser = useBrowser;
51     }
52
53     public boolean equals (Object JavaDoc 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 JavaDoc s1, String JavaDoc s2) {
62         if (s1 == null || s2 == null)
63             return s1 == s2;
64         else
65             return s1.equals (s2);
66     }
67
68     public String JavaDoc 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 JavaDoc e) {}
82     }
83
84     public synchronized void visit (Page page) {
85         try {
86             mirror.writePage (page);
87         } catch (IOException JavaDoc e) {
88             throw new RuntimeException JavaDoc (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     /**
101      * Notify that the crawler started.
102      */

103     public void started (CrawlEvent event){
104         if (mirror == null) {
105             try {
106                 dir = (directory != null)
107                   ? new File JavaDoc (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 JavaDoc e) {
116                 System.err.println (e); // FIX: use GUI when available
117
}
118         }
119     }
120
121     /**
122      * Notify that the crawler ran out of links to crawl
123      */

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 JavaDoc e) {
134             System.err.println (e); // FIX: use GUI when available
135
}
136     }
137
138     /**
139      * Notify that the crawler's state was cleared.
140      */

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 JavaDoc e) {
151             System.err.println (e); // FIX: use GUI when available
152
}
153     }
154
155     /**
156      * Notify that the crawler timed out.
157      */

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 JavaDoc e) {
168             System.err.println (e); // FIX: use GUI when available
169
}
170     }
171
172     /**
173      * Notify that the crawler is paused.
174      */

175     public void paused (CrawlEvent event){
176         try {
177             if (mirror != null) {
178                 mirror.rewrite ();
179                 if (useBrowser)
180                     showit ();
181             }
182         } catch (IOException JavaDoc e) {
183             System.err.println (e); // FIX: use GUI when available
184
}
185     }
186
187 }
188
189
Popular Tags