KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > workbench > PagePredicateEditor


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 java.awt.*;
36 import websphinx.*;
37 import rcm.awt.Constrain;
38
39 public class PagePredicateEditor extends Panel { // FIX: consider implementing java.beans.PropertyEditor
40

41     PageFeatureChoice choice;
42
43     /**
44      * Make a PagePredicateEditor.
45      */

46     public PagePredicateEditor () {
47         setLayout (new GridBagLayout ());
48         choice = new PageFeatureChoice ();
49         Constrain.add (this, choice, Constrain.labelLike (0, 0));
50         Constrain.add (this, choice.getArgs(), Constrain.areaLike (0, 1));
51         setPagePredicate (null);
52     }
53
54     public void setPagePredicate (PagePredicate pred) {
55         // FIX: handle compound predicates
56
choice.setPagePredicate (pred);
57     }
58
59     public PagePredicate getPagePredicate () {
60         return choice.getPagePredicate ();
61     }
62
63 }
64
65 class PageFeatureChoice extends FeatureChoice {
66     PageFeatureArgs args = new PageFeatureArgs ();
67
68     final static String JavaDoc NULL_FEATURE = "all pages";
69     final static String JavaDoc URL_FEATURE = "URL";
70     final static String JavaDoc HTML_FEATURE = "HTML";
71     final static String JavaDoc TEXT_FEATURE = "text";
72     final static String JavaDoc LABEL_FEATURE = "labels";
73     final static String JavaDoc TITLE_FEATURE = "title";
74     final static String JavaDoc SCRIPT_FEATURE = "script";
75
76     public PageFeatureChoice () {
77         addItem (NULL_FEATURE);
78         addItem (LABEL_FEATURE);
79         addItem (TITLE_FEATURE);
80         addItem (URL_FEATURE);
81         addItem (TEXT_FEATURE);
82         addItem (HTML_FEATURE);
83         addItem (SCRIPT_FEATURE);
84     }
85     
86     public void setPagePredicate (PagePredicate pred) {
87         PagePredicate neg = null;
88         if (pred instanceof DualPredicate) {
89             neg = (PagePredicate)((DualPredicate)pred).getNegativePredicate ();
90             pred = (PagePredicate)((DualPredicate)pred).getPositivePredicate ();
91         }
92
93         if (pred == null) {
94             select (NULL_FEATURE);
95         }
96         else if (pred instanceof URLPredicate) {
97             URLPredicate urlpred = (URLPredicate)pred;
98             URLPredicate urlneg = (URLPredicate)neg;
99             select (URL_FEATURE);
100             args.setURLPattern (urlpred.getPattern ().toString());
101             args.setURLNegPattern (urlneg != null
102                                    ? urlneg.getPattern ().toString ()
103                                    : "");
104         }
105         else if (pred instanceof ContentPredicate) {
106             ContentPredicate contpred = (ContentPredicate)pred;
107             ContentPredicate contneg = (ContentPredicate)neg;
108             if (contpred.getOverHTML()) {
109                 select (HTML_FEATURE);
110                 args.setHTMLPattern (contpred.getPattern ().toString());
111                 args.setHTMLNegPattern (contneg != null
112                                         ? contneg.getPattern ().toString ()
113                                         : "");
114             }
115             else {
116                 select (TEXT_FEATURE);
117                 args.setTextPattern (contpred.getPattern ().toString());
118                 args.setTextNegPattern (contneg != null
119                                         ? contneg.getPattern ().toString ()
120                                         : "");
121             }
122         }
123         else if (pred instanceof TitlePredicate) {
124             TitlePredicate titlepred = (TitlePredicate)pred;
125             TitlePredicate titleneg = (TitlePredicate)neg;
126             select (TITLE_FEATURE);
127             args.setTitlePattern (titlepred.getPattern ().toString());
128             args.setTitleNegPattern (titleneg != null
129                                    ? titleneg.getPattern ().toString ()
130                                    : "");
131         }
132         else if (pred instanceof LabelPredicate) {
133             LabelPredicate labelpred = (LabelPredicate)pred;
134             select (LABEL_FEATURE);
135             args.setOrTerms (labelpred.getOrTerms());
136             args.setLabels (labelpred.getLabels());
137         }
138         else if (pred instanceof Script) {
139             Script script = (Script)pred;
140             select (SCRIPT_FEATURE);
141             args.setScript (script.getScript ());
142         }
143         else {
144             select (NULL_FEATURE);
145         }
146     }
147
148     public Panel getArgs () {
149         return args;
150     }
151
152     public PagePredicate getPagePredicate () {
153         String JavaDoc feat = getSelectedItem ();
154         if (feat.equals (URL_FEATURE))
155             return makeSingleOrDual (new URLPredicate (new Wildcard (args.getURLPattern())),
156                                      args.getURLNegPattern().length() == 0
157                                      ? null
158                                      : new URLPredicate (new Wildcard (args.getURLNegPattern())));
159         else if (feat.equals (HTML_FEATURE))
160             return makeSingleOrDual (new ContentPredicate (new Tagexp (args.getHTMLPattern()), true),
161                                      args.getHTMLNegPattern().length() == 0
162                                      ? null
163                                      : new ContentPredicate (new Tagexp (args.getHTMLNegPattern()), true));
164         else if (feat.equals (TEXT_FEATURE))
165             return makeSingleOrDual (new ContentPredicate (new Regexp (args.getTextPattern()), false),
166                                      args.getTextNegPattern().length() == 0
167                                      ? null
168                                      : new ContentPredicate (new Regexp (args.getTextNegPattern()), false));
169         else if (feat.equals (TITLE_FEATURE))
170             return makeSingleOrDual (new TitlePredicate (new Regexp (args.getTitlePattern())),
171                                      args.getTitleNegPattern().length() == 0
172                                      ? null
173                                      : new TitlePredicate (new Regexp (args.getTitleNegPattern())));
174         else if (feat.equals (LABEL_FEATURE))
175             return new LabelPredicate (args.getLabels(), args.getOrTerms());
176         else if (feat.equals (SCRIPT_FEATURE))
177             return new Script (args.getScript (), false);
178         else
179             return null;
180     }
181
182     private static PagePredicate makeSingleOrDual (PagePredicate positive,
183                                                    PagePredicate negative) {
184         return negative == null
185             ? positive
186             : new DualPredicate (positive, negative);
187     }
188 }
189
190 class PageFeatureArgs extends Panel {
191
192     TextField urlPattern;
193     TextField urlNegPattern;
194     TextField textPattern;
195     TextField textNegPattern;
196     TextField htmlPattern;
197     TextField htmlNegPattern;
198     TextField titlePattern;
199     TextField titleNegPattern;
200     TextField labels;
201     Choice orTerms;
202     TextArea script;
203
204     final static String JavaDoc ANY_TERMS = "any";
205     final static String JavaDoc ALL_TERMS = "all";
206
207     public PageFeatureArgs () {
208         Panel panel;
209
210         setLayout (new CardLayout ());
211
212         add (PageFeatureChoice.NULL_FEATURE, panel = new Panel ());
213
214         add (PageFeatureChoice.URL_FEATURE, panel = Constrain.makeConstrainedPanel (1, 4));
215         Constrain.add (panel, new Label (" matches the wildcard expression "), Constrain.labelLike (0, 0));
216         Constrain.add (panel, urlPattern = new TextField (), Constrain.fieldLike (0, 1));
217         Constrain.add (panel, new Label (" but not the expression "), Constrain.labelLike (0, 2));
218         Constrain.add (panel, urlNegPattern = new TextField (), Constrain.fieldLike (0, 3));
219
220         add (PageFeatureChoice.HTML_FEATURE, panel = Constrain.makeConstrainedPanel (1, 4));
221         Constrain.add (panel, new Label (" matches the HTML tag expression "), Constrain.labelLike (0, 0));
222         Constrain.add (panel, htmlPattern = new TextField (), Constrain.fieldLike (0, 1));
223         Constrain.add (panel, new Label (" but not the expression "), Constrain.labelLike (0, 2));
224         Constrain.add (panel, htmlNegPattern = new TextField (), Constrain.fieldLike (0, 3));
225
226         add (PageFeatureChoice.TEXT_FEATURE, panel = Constrain.makeConstrainedPanel (1, 4));
227         Constrain.add (panel, new Label (" matches the regular expression "), Constrain.labelLike (0, 0));
228         Constrain.add (panel, textPattern = new TextField (), Constrain.fieldLike (0, 1));
229         Constrain.add (panel, new Label (" but not the expression "), Constrain.labelLike (0, 2));
230         Constrain.add (panel, textNegPattern = new TextField (), Constrain.fieldLike (0, 3));
231
232         add (PageFeatureChoice.TITLE_FEATURE, panel = Constrain.makeConstrainedPanel (1, 4));
233         Constrain.add (panel, new Label (" matches the regular expression "), Constrain.labelLike (0, 0));
234         Constrain.add (panel, titlePattern = new TextField (), Constrain.fieldLike (0, 1));
235         Constrain.add (panel, new Label (" but not the expression "), Constrain.labelLike (0, 2));
236         Constrain.add (panel, titleNegPattern = new TextField (), Constrain.fieldLike (0, 3));
237
238         add (PageFeatureChoice.LABEL_FEATURE, panel = Constrain.makeConstrainedPanel (3, 2));
239         Constrain.add (panel, new Label (" include "), Constrain.labelLike (0, 0));
240         Constrain.add (panel, orTerms = new Choice (), Constrain.labelLike (1, 0));
241         orTerms.addItem (ANY_TERMS);
242         orTerms.addItem (ALL_TERMS);
243         orTerms.select (ANY_TERMS);
244         Constrain.add (panel, new Label (" of the labels "), Constrain.labelLike (2, 0));
245         Constrain.add (panel, labels = new TextField (), Constrain.fieldLike (0, 1, 3));
246
247         ScriptInterpreter interp = Context.getScriptInterpreter ();
248         if (interp != null) {
249             add (PageFeatureChoice.SCRIPT_FEATURE,
250                  panel = Constrain.makeConstrainedPanel (1, 2));
251             Constrain.add (panel, new Label (interp.getLanguage() + " Function (crawler, page)"),
252                            Constrain.labelLike (0, 0));
253             Constrain.add (panel, script = new TextArea ("return true;\n"),
254                            Constrain.areaLike (0, 1));
255         }
256         else {
257             add (PageFeatureChoice.SCRIPT_FEATURE,
258                  panel = Constrain.makeConstrainedPanel (1, 1));
259             Constrain.add (panel, new Label ("No scripting language is available."),
260                            Constrain.labelLike (0, 0));
261         }
262     }
263
264     public void setURLPattern (String JavaDoc pattern) {
265         urlPattern.setText (pattern);
266     }
267
268     public String JavaDoc getURLPattern () {
269         return urlPattern.getText ();
270     }
271
272     public void setURLNegPattern (String JavaDoc pattern) {
273         urlNegPattern.setText (pattern);
274     }
275
276     public String JavaDoc getURLNegPattern () {
277         return urlNegPattern.getText ();
278     }
279
280     public void setTextPattern (String JavaDoc pattern) {
281         textPattern.setText (pattern);
282     }
283
284     public String JavaDoc getTextPattern () {
285         return textPattern.getText ();
286     }
287
288     public void setTextNegPattern (String JavaDoc pattern) {
289         textNegPattern.setText (pattern);
290     }
291
292     public String JavaDoc getTextNegPattern () {
293         return textNegPattern.getText ();
294     }
295
296     public void setHTMLPattern (String JavaDoc pattern) {
297         htmlPattern.setText (pattern);
298     }
299
300     public String JavaDoc getHTMLPattern () {
301         return htmlPattern.getText ();
302     }
303
304     public void setHTMLNegPattern (String JavaDoc pattern) {
305         htmlNegPattern.setText (pattern);
306     }
307
308     public String JavaDoc getHTMLNegPattern () {
309         return htmlNegPattern.getText ();
310     }
311
312     public void setTitlePattern (String JavaDoc pattern) {
313         titlePattern.setText (pattern);
314     }
315
316     public String JavaDoc getTitlePattern () {
317         return titlePattern.getText ();
318     }
319
320     public void setTitleNegPattern (String JavaDoc pattern) {
321         titleNegPattern.setText (pattern);
322     }
323
324     public String JavaDoc getTitleNegPattern () {
325         return titleNegPattern.getText ();
326     }
327
328     public void setLabels (String JavaDoc pattern) {
329         labels.setText (pattern);
330     }
331
332     public String JavaDoc getLabels () {
333         return labels.getText ();
334     }
335
336     public void setOrTerms (boolean orTerms) {
337         this.orTerms.select (orTerms ? ANY_TERMS : ALL_TERMS);
338     }
339
340     public boolean getOrTerms () {
341         return orTerms.getSelectedItem ().equals (ANY_TERMS);
342     }
343
344     public void setScript (String JavaDoc script) {
345         this.script.setText (script);
346     }
347
348     public String JavaDoc getScript () {
349         return script != null ? script.getText () : null;
350     }
351
352 }
353
Popular Tags