KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > workbench > WorkbenchApplet


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.applet.*;
36 import java.awt.*;
37 import java.net.URL JavaDoc;
38 import websphinx.Crawler;
39 import websphinx.Tagexp;
40 import rcm.awt.PopupDialog;
41
42 public class WorkbenchApplet extends Applet {
43
44     Workbench workbench;
45
46     public Workbench makeWorkbench () {
47         String JavaDoc openURL = getParameter ("open");
48         String JavaDoc newClassname = getParameter ("new");
49         
50         try {
51             if (openURL != null)
52                 return new Workbench (new URL JavaDoc (getDocumentBase (), openURL));
53             else if (newClassname != null)
54                 return new Workbench ((Crawler)Class.forName (newClassname).newInstance());
55             else
56                 return new Workbench ();
57         } catch (Exception JavaDoc e) {
58             PopupDialog.warn (null,
59                               "Error",
60                               e.toString());
61             throw new Error JavaDoc (e.toString());
62         }
63     }
64
65     public void init () {
66         super.init ();
67
68         String JavaDoc targetName = getParameter ("target");
69         if (targetName != null)
70             Context.setApplet (this, targetName);
71         else
72             Context.setApplet (this);
73
74         workbench = makeWorkbench ();
75
76         String JavaDoc param;
77         if ((param = getParameter ("advanced")) != null)
78             workbench.setAdvancedMode (isTrue (param));
79
80         /*
81         if ((param = getParameter ("graph")) != null)
82             workbench.setGraphVisible (isTrue (param));
83             
84         if ((param = getParameter ("statistics")) != null)
85             workbench.setStatisticsVisible (isTrue (param));
86             
87         if ((param = getParameter ("log")) != null)
88             workbench.setLoggerVisible (isTrue (param));
89         */

90
91         Crawler crawler = workbench.getCrawler();
92
93         String JavaDoc action = getParameter ("action");
94         if (action != null) {
95             String JavaDoc filename = getParameter ("filename");
96             String JavaDoc pattern = getParameter ("pattern");
97             
98             if (action.equalsIgnoreCase ("concatenate"))
99                 crawler.setAction (new ConcatAction (filename, true));
100             else if (action.equalsIgnoreCase ("save"))
101                 crawler.setAction (new MirrorAction (filename, true));
102             else if (action.equalsIgnoreCase ("visualize")) {
103                 crawler.setAction (null);
104                 //workbench.setGraphVisible (true);
105
}
106             else if (action.equalsIgnoreCase ("extract"))
107                 crawler.setAction (new ExtractAction (new Tagexp (pattern),
108                                                   true, filename, false));
109             else if (action.equalsIgnoreCase ("none"))
110                 crawler.setAction (null);
111             else
112                 throw new RuntimeException JavaDoc ("unknown action: " +action);
113         }
114         
115         String JavaDoc urls = getParameter ("urls");
116         if (urls != null)
117             try {
118                 crawler.setRootHrefs (urls);
119             } catch (java.net.MalformedURLException JavaDoc e) {
120                 throw new RuntimeException JavaDoc (e.toString());
121             }
122         
123         String JavaDoc domain = getParameter ("domain");
124         if (domain != null) {
125             if (domain.equalsIgnoreCase ("server"))
126                 crawler.setDomain (Crawler.SERVER);
127             else if (domain.equalsIgnoreCase ("subtree"))
128                 crawler.setDomain (Crawler.SUBTREE);
129             else
130                 crawler.setDomain (Crawler.WEB);
131         }
132         
133         String JavaDoc type = getParameter ("type");
134         if (type != null) {
135             if (type.equalsIgnoreCase ("images+hyperlinks"))
136                 crawler.setLinkType (Crawler.HYPERLINKS_AND_IMAGES);
137             else if (type.equalsIgnoreCase ("all"))
138                 crawler.setLinkType (Crawler.ALL_LINKS);
139             else
140                 crawler.setLinkType (Crawler.WEB);
141         }
142
143         String JavaDoc depth = getParameter ("depth");
144         if (depth != null)
145             crawler.setMaxDepth (Integer.parseInt (depth));
146
147         String JavaDoc dfs = getParameter ("depthfirst");
148         if (dfs != null)
149             crawler.setDepthFirst (isTrue (dfs));
150
151         workbench.setCrawler (crawler);
152
153         setLayout (new BorderLayout ());
154         add ("Center", workbench);
155     }
156
157     private static boolean isTrue (String JavaDoc s) {
158         return s != null &&
159             (s.equalsIgnoreCase ("on")
160              || s.equalsIgnoreCase ("1")
161              || s.equalsIgnoreCase ("yes")
162              || s.equalsIgnoreCase ("true"));
163     }
164 }
165
Popular Tags