KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > workbench > CrawlerEditor


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.awt.*;
37 import rcm.awt.Constrain;
38 import rcm.awt.PopupDialog;
39
40 public class CrawlerEditor extends Panel {
41
42     Crawler crawler;
43
44     Label domainLabel;
45     Choice domainChoice;
46
47     Label typeLabel;
48     Choice typeChoice;
49
50     Label urlLabel;
51     TextComponent urlField;
52
53     Label depthLabel;
54     Label depthLabel2;
55     TextComponent depthField;
56
57     Choice searchOrderChoice;
58
59     public CrawlerEditor () {
60         setLayout (new GridBagLayout ());
61
62         Constrain.add (this, domainLabel = new Label("Crawl:"), Constrain.labelLike (0, 0));
63         Constrain.add (this, domainChoice = new Choice (), Constrain.labelLike (1, 0, 2));
64         domainChoice.addItem ("the subtree");
65         domainChoice.addItem ("the server");
66         domainChoice.addItem ("the Web");
67
68         Constrain.add (this, typeLabel = new Label("Using:"), Constrain.labelLike (3, 0));
69         Constrain.add (this, typeChoice = new Choice (), Constrain.fieldLike (4, 0));
70         typeChoice.addItem ("hyperlinks");
71         typeChoice.addItem ("images+hyperlinks");
72         typeChoice.addItem ("all links");
73
74         Constrain.add (this, urlLabel = new Label ("Starting URLs:"), Constrain.labelLike (0, 1));
75         Constrain.add (this, urlField = new TextArea(3, 40), Constrain.areaLike (1, 1, 4));
76         
77         Constrain.add (this, depthLabel = new Label ("Depth:"), Constrain.labelLike (0, 2));
78         Constrain.add (this, depthField = new TextField (4), Constrain.fieldLike (1, 2));
79         Constrain.add (this, depthLabel2 = new Label (" hops"), Constrain.labelLike (2, 2));
80         Constrain.add (this, searchOrderChoice = new Choice (), Constrain.fieldLike (4, 2));
81         searchOrderChoice.addItem ("Depth first");
82         searchOrderChoice.addItem ("Breadth first");
83     }
84
85     public boolean handleEvent (Event event) {
86         if (event.id == Event.ACTION_EVENT) {
87             if (event.target == domainChoice)
88                 configureDomain ();
89             else if (event.target == urlField)
90                 configureURL ();
91             else if (event.target == depthField)
92                 configureDepth ();
93             else if (event.target == searchOrderChoice)
94                 configureDepthFirst ();
95             else
96                 return super.handleEvent (event);
97         }
98         else if (event.id == Event.LOST_FOCUS) {
99             if (event.target == urlField)
100                 configureURL ();
101             else if (event.target == depthField)
102                 configureDepth ();
103             else
104                 return super.handleEvent (event);
105         }
106         else
107             return super.handleEvent (event);
108
109         return true;
110     }
111
112     public void setCrawler (Crawler crawler) {
113         this.crawler = crawler;
114
115         String JavaDoc[] domain = crawler.getDomain ();
116         if (domain == Crawler.SERVER)
117             domainChoice.select (1);
118         else if (domain == Crawler.SUBTREE)
119             domainChoice.select (0);
120         else
121             domainChoice.select (2);
122
123         String JavaDoc[] type = crawler.getLinkType ();
124         if (type == Crawler.HYPERLINKS_AND_IMAGES)
125             typeChoice.select (1);
126         else if (type == Crawler.ALL_LINKS)
127             typeChoice.select (2);
128         else
129             typeChoice.select (0);
130
131         urlField.setText (crawler.getRootHrefs ());
132         depthField.setText (String.valueOf (crawler.getMaxDepth ()));
133         searchOrderChoice.select (crawler.getDepthFirst () ? 0 : 1);
134     }
135
136     public Crawler getCrawler () {
137         if (configureDomain ()
138             && configureType ()
139             && configureURL ()
140             && configureDepth ()
141             && configureDepthFirst ())
142             return crawler;
143         else
144             return null;
145     }
146
147     boolean configureDomain () {
148         switch (domainChoice.getSelectedIndex ()) {
149             case 2: // the Web
150
crawler.setDomain (Crawler.WEB);
151                 break;
152             case 1: // a server
153
crawler.setDomain (Crawler.SERVER);
154                 break;
155             case 0: // a subtree
156
crawler.setDomain (Crawler.SUBTREE);
157                 break;
158             default:
159                 throw new RuntimeException JavaDoc ("unknown state " + domainChoice.getSelectedIndex ());
160         }
161         return true;
162     }
163
164     boolean configureType () {
165         switch (typeChoice.getSelectedIndex ()) {
166             case 0:
167                 crawler.setLinkType (Crawler.HYPERLINKS);
168                 break;
169             case 1:
170                 crawler.setLinkType (Crawler.HYPERLINKS_AND_IMAGES);
171                 break;
172             case 2:
173                 crawler.setLinkType (Crawler.ALL_LINKS);
174                 break;
175             default:
176                 throw new RuntimeException JavaDoc ("unknown state " + typeChoice.getSelectedIndex ());
177         }
178         return true;
179     }
180
181     String JavaDoc lastURL = null;
182
183     boolean configureURL () {
184         String JavaDoc hrefs = urlField.getText();
185         try {
186             crawler.setRootHrefs (hrefs);
187             lastURL = hrefs;
188             return true;
189         } catch (java.net.MalformedURLException JavaDoc ex) {
190             if (lastURL == null || !lastURL.equals (hrefs)) {
191                 PopupDialog.warn (this, "Error", "Improperly formed URL:\n" + hrefs);
192                 urlField.selectAll ();
193                 urlField.requestFocus ();
194             }
195             lastURL = hrefs;
196             return false;
197         }
198     }
199
200     String JavaDoc lastDepth = null;
201
202     boolean configureDepth () {
203         String JavaDoc depth = depthField.getText ();
204         try {
205             crawler.setMaxDepth (Integer.parseInt (depth));
206             lastDepth = depth;
207             return true;
208         } catch (NumberFormatException JavaDoc ex) {
209             if (lastDepth == null || !lastDepth.equals (depth)) {
210                 PopupDialog.warn (this, "Error", "Depth must be an integer");
211                 depthField.selectAll ();
212                 depthField.requestFocus ();
213             }
214             lastDepth = depth;
215             return false;
216         }
217     }
218
219     boolean configureDepthFirst () {
220         crawler.setDepthFirst (searchOrderChoice.getSelectedIndex () == 0);
221         return true;
222     }
223 }
224
Popular Tags