KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > workbench > ConcatAction


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
39 public class ConcatAction implements Action, CrawlListener {
40     String JavaDoc filename;
41     boolean useBrowser;
42     String JavaDoc prolog, header, footer, divider, epilog;
43
44     transient File JavaDoc file;
45     transient Concatenator concat;
46
47     public ConcatAction (String JavaDoc filename, boolean useBrowser) {
48         this.filename = filename;
49         this.useBrowser = useBrowser;
50     }
51
52     public ConcatAction (String JavaDoc filename, boolean useBrowser,
53                          String JavaDoc prolog, String JavaDoc header, String JavaDoc footer,
54                          String JavaDoc divider, String JavaDoc 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 JavaDoc 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 JavaDoc s1, String JavaDoc s2) {
71         if (s1 == null || s2 == null)
72             return s1 == s2;
73         else
74             return s1.equals (s2);
75     }
76
77     public String JavaDoc 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 JavaDoc e) {
109             throw new RuntimeException JavaDoc (e.toString());
110         }
111     }
112
113     /**
114      * Notify that the crawler started.
115      */

116     public void started (CrawlEvent event){
117         if (concat == null) {
118             try {
119                 file = (filename != null)
120                   ? new File JavaDoc (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 JavaDoc e) {
135                 System.err.println (e); // FIX: use GUI when available
136
}
137         }
138     }
139
140     /**
141      * Notify that the crawler ran out of links to crawl
142      */

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

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

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 JavaDoc e) {
184             System.err.println (e); // FIX: use GUI when available
185
}
186     }
187
188     /**
189      * Notify that the crawler is paused.
190      */

191     public void paused (CrawlEvent event){
192         try {
193             if (concat != null) {
194                 concat.rewrite ();
195                 if (useBrowser)
196                     showit ();
197             }
198         } catch (IOException JavaDoc e) {
199             System.err.println (e); // FIX: use GUI when available
200
}
201     }
202
203 }
204
205
Popular Tags