001: /*
002: * WebSphinx web-crawling toolkit
003: *
004: * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
020: * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
021: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
022: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
023: * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
024: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
025: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
026: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
027: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
029: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: *
031: */
032:
033: package websphinx.workbench;
034:
035: import websphinx.*;
036: import java.awt.*;
037: import rcm.awt.Constrain;
038: import rcm.awt.PopupDialog;
039:
040: public class CrawlerEditor extends Panel {
041:
042: Crawler crawler;
043:
044: Label domainLabel;
045: Choice domainChoice;
046:
047: Label typeLabel;
048: Choice typeChoice;
049:
050: Label urlLabel;
051: TextComponent urlField;
052:
053: Label depthLabel;
054: Label depthLabel2;
055: TextComponent depthField;
056:
057: Choice searchOrderChoice;
058:
059: public CrawlerEditor() {
060: setLayout(new GridBagLayout());
061:
062: Constrain.add(this , domainLabel = new Label("Crawl:"),
063: Constrain.labelLike(0, 0));
064: Constrain.add(this , domainChoice = new Choice(), Constrain
065: .labelLike(1, 0, 2));
066: domainChoice.addItem("the subtree");
067: domainChoice.addItem("the server");
068: domainChoice.addItem("the Web");
069:
070: Constrain.add(this , typeLabel = new Label("Using:"), Constrain
071: .labelLike(3, 0));
072: Constrain.add(this , typeChoice = new Choice(), Constrain
073: .fieldLike(4, 0));
074: typeChoice.addItem("hyperlinks");
075: typeChoice.addItem("images+hyperlinks");
076: typeChoice.addItem("all links");
077:
078: Constrain.add(this , urlLabel = new Label("Starting URLs:"),
079: Constrain.labelLike(0, 1));
080: Constrain.add(this , urlField = new TextArea(3, 40), Constrain
081: .areaLike(1, 1, 4));
082:
083: Constrain.add(this , depthLabel = new Label("Depth:"), Constrain
084: .labelLike(0, 2));
085: Constrain.add(this , depthField = new TextField(4), Constrain
086: .fieldLike(1, 2));
087: Constrain.add(this , depthLabel2 = new Label(" hops"),
088: Constrain.labelLike(2, 2));
089: Constrain.add(this , searchOrderChoice = new Choice(), Constrain
090: .fieldLike(4, 2));
091: searchOrderChoice.addItem("Depth first");
092: searchOrderChoice.addItem("Breadth first");
093: }
094:
095: public boolean handleEvent(Event event) {
096: if (event.id == Event.ACTION_EVENT) {
097: if (event.target == domainChoice)
098: configureDomain();
099: else if (event.target == urlField)
100: configureURL();
101: else if (event.target == depthField)
102: configureDepth();
103: else if (event.target == searchOrderChoice)
104: configureDepthFirst();
105: else
106: return super .handleEvent(event);
107: } else if (event.id == Event.LOST_FOCUS) {
108: if (event.target == urlField)
109: configureURL();
110: else if (event.target == depthField)
111: configureDepth();
112: else
113: return super .handleEvent(event);
114: } else
115: return super .handleEvent(event);
116:
117: return true;
118: }
119:
120: public void setCrawler(Crawler crawler) {
121: this .crawler = crawler;
122:
123: String[] domain = crawler.getDomain();
124: if (domain == Crawler.SERVER)
125: domainChoice.select(1);
126: else if (domain == Crawler.SUBTREE)
127: domainChoice.select(0);
128: else
129: domainChoice.select(2);
130:
131: String[] type = crawler.getLinkType();
132: if (type == Crawler.HYPERLINKS_AND_IMAGES)
133: typeChoice.select(1);
134: else if (type == Crawler.ALL_LINKS)
135: typeChoice.select(2);
136: else
137: typeChoice.select(0);
138:
139: urlField.setText(crawler.getRootHrefs());
140: depthField.setText(String.valueOf(crawler.getMaxDepth()));
141: searchOrderChoice.select(crawler.getDepthFirst() ? 0 : 1);
142: }
143:
144: public Crawler getCrawler() {
145: if (configureDomain() && configureType() && configureURL()
146: && configureDepth() && configureDepthFirst())
147: return crawler;
148: else
149: return null;
150: }
151:
152: boolean configureDomain() {
153: switch (domainChoice.getSelectedIndex()) {
154: case 2: // the Web
155: crawler.setDomain(Crawler.WEB);
156: break;
157: case 1: // a server
158: crawler.setDomain(Crawler.SERVER);
159: break;
160: case 0: // a subtree
161: crawler.setDomain(Crawler.SUBTREE);
162: break;
163: default:
164: throw new RuntimeException("unknown state "
165: + domainChoice.getSelectedIndex());
166: }
167: return true;
168: }
169:
170: boolean configureType() {
171: switch (typeChoice.getSelectedIndex()) {
172: case 0:
173: crawler.setLinkType(Crawler.HYPERLINKS);
174: break;
175: case 1:
176: crawler.setLinkType(Crawler.HYPERLINKS_AND_IMAGES);
177: break;
178: case 2:
179: crawler.setLinkType(Crawler.ALL_LINKS);
180: break;
181: default:
182: throw new RuntimeException("unknown state "
183: + typeChoice.getSelectedIndex());
184: }
185: return true;
186: }
187:
188: String lastURL = null;
189:
190: boolean configureURL() {
191: String hrefs = urlField.getText();
192: try {
193: crawler.setRootHrefs(hrefs);
194: lastURL = hrefs;
195: return true;
196: } catch (java.net.MalformedURLException ex) {
197: if (lastURL == null || !lastURL.equals(hrefs)) {
198: PopupDialog.warn(this , "Error",
199: "Improperly formed URL:\n" + hrefs);
200: urlField.selectAll();
201: urlField.requestFocus();
202: }
203: lastURL = hrefs;
204: return false;
205: }
206: }
207:
208: String lastDepth = null;
209:
210: boolean configureDepth() {
211: String depth = depthField.getText();
212: try {
213: crawler.setMaxDepth(Integer.parseInt(depth));
214: lastDepth = depth;
215: return true;
216: } catch (NumberFormatException ex) {
217: if (lastDepth == null || !lastDepth.equals(depth)) {
218: PopupDialog.warn(this , "Error",
219: "Depth must be an integer");
220: depthField.selectAll();
221: depthField.requestFocus();
222: }
223: lastDepth = depth;
224: return false;
225: }
226: }
227:
228: boolean configureDepthFirst() {
229: crawler
230: .setDepthFirst(searchOrderChoice.getSelectedIndex() == 0);
231: return true;
232: }
233: }
|