KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > workbench > LinkPredicateEditor


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 LinkPredicateEditor extends Panel { // FIX: consider implementing java.beans.PropertyEditor
40

41     LinkFeatureChoice choice;
42
43     /**
44      * Make a LinkPredicateEditor.
45      */

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