KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > workbench > Netscape


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.util.BitSet JavaDoc;
37 import java.applet.AppletContext JavaDoc;
38 import rcm.util.Str;
39 import netscape.javascript.JSObject;
40 import java.net.URL JavaDoc;
41
42 public class Netscape extends Browser implements ScriptInterpreter {
43
44     JSObject jsobject;
45         // Javascript interpreter
46
BitSet JavaDoc applies = new BitSet JavaDoc();
47         // n-ary apply functions that have already been made
48

49     public Netscape (AppletContext JavaDoc context) {
50         super (context);
51         init ();
52     }
53
54     public Netscape (AppletContext JavaDoc context, String JavaDoc frameName) {
55         super (context, frameName);
56         init ();
57     }
58
59     private void init () {
60         try {
61             jsobject = JSObject.getWindow (Context.getApplet ());
62         } catch (Throwable JavaDoc e) {
63             jsobject = null;
64         }
65     }
66
67     /*
68      * JavaScript interpreter
69      *
70      */

71
72     public ScriptInterpreter getScriptInterpreter () {
73         return jsobject != null ? this : null;
74     }
75
76     public String JavaDoc getLanguage () {
77         return "Javascript";
78     }
79
80     public Object JavaDoc eval (String JavaDoc expression) throws ScriptException {
81         //System.out.println ("evaluating " + expression);
82
if (jsobject == null)
83             throw new ScriptException ("Javascript not available");
84             
85         try {
86             return jsobject.eval (expression);
87         } catch (Throwable JavaDoc e) {
88             throw new ScriptException (e.getMessage ());
89         }
90     }
91
92     static String JavaDoc DBLQUOTE = "\"";
93     static String JavaDoc LINEFEED = "\n";
94     static String JavaDoc BACKSLASH = "\\";
95     
96     public Object JavaDoc lambda (String JavaDoc[] args, String JavaDoc body) throws ScriptException {
97         StringBuffer JavaDoc code = new StringBuffer JavaDoc ();
98
99         makeApply (args.length);
100
101         // Function ("arg0", "arg1", ..., "argn", body)
102
code.append ("Function (");
103         if (args != null)
104             for (int i=0; i<args.length; ++i) {
105                 code.append (DBLQUOTE);
106                 code.append (args[i]);
107                 code.append (DBLQUOTE + ", ");
108             }
109         code.append (DBLQUOTE);
110         
111         body = Str.replace (body, BACKSLASH, BACKSLASH+BACKSLASH);
112         body = Str.replace (body, DBLQUOTE, BACKSLASH+DBLQUOTE);
113         body = Str.replace (body, LINEFEED, BACKSLASH+LINEFEED);
114         code.append (body);
115         code.append (DBLQUOTE + ")");
116
117         System.out.println ("evaluating\n"+code+"\n");
118         Object JavaDoc func;
119         synchronized (jsobject) {
120           func = eval (code.toString());
121         }
122         System.out.println ("lambda " + func);
123         return func;
124     }
125
126     // Create an application function for n-ary functions:
127
// apply(N+1) = Function ('f', 'a0', 'a1', ..., 'aN-1', 'f (a0, a1, ..., aN-1)')
128
// e.g. apply2 = Function ('f', 'a0', 'f (a0)')
129
void makeApply (int n) {
130         if (applies.get (n))
131             return;
132         applies.set (n);
133
134         StringBuffer JavaDoc app = new StringBuffer JavaDoc ();
135         app.append ("Function ('f', ");
136         for (int i=0; i<n; ++i) {
137             app.append ("'a");
138             app.append (String.valueOf(i));
139             app.append ("',");
140         }
141         app.append ("'return f (");
142         for (int i=0; i<n; ++i) {
143             if (i > 0)
144                 app.append (',');
145             app.append ("a");
146             app.append (String.valueOf(i));
147         }
148         app.append (")')");
149
150         try {
151             set ("apply" + (n+1), eval (app.toString()));
152         } catch (ScriptException e) {
153             throw new RuntimeException JavaDoc ("Internal error: cannot create Javascript apply function:\n" + app.toString());
154         }
155     }
156
157     public Object JavaDoc apply (Object JavaDoc func, Object JavaDoc[] args) throws ScriptException {
158         if (jsobject == null)
159             throw new ScriptException ("Javascript not available");
160             
161         Object JavaDoc[] funcPlusArgs = new Object JavaDoc[1 + args.length];
162         funcPlusArgs[0] = func;
163         System.arraycopy (args, 0, funcPlusArgs, 1, args.length);
164         
165         //System.out.print ("applying ");
166
//for (int i=0; i<funcPlusArgs.length; ++i)
167
// System.out.print (funcPlusArgs + " ");
168
//System.out.println ();
169

170         Object JavaDoc result;
171         try {
172           synchronized (jsobject) {
173             result = jsobject.call ("apply" + funcPlusArgs.length, funcPlusArgs);
174           }
175         } catch (Throwable JavaDoc e) {
176             throw new ScriptException (e.getMessage ());
177         }
178
179         //System.out.println ("returned " + result);
180
return result;
181     }
182
183     public void set (String JavaDoc name, Object JavaDoc object) {
184         if (jsobject != null)
185           synchronized (jsobject) {
186             jsobject.setMember (name, object);
187           }
188     }
189
190     public Object JavaDoc get (String JavaDoc name) {
191         if (jsobject == null)
192           return null;
193
194         synchronized (jsobject) {
195             return jsobject.getMember (name);
196         }
197     }
198
199     /*
200      * Show pages in browser
201      */

202     public void show (URL JavaDoc url) {
203         // bring the window forward
204
if (frameName != null) {
205             String JavaDoc code = "window.open ('', '" + frameName + "').focus ()";
206              try {
207                 eval (code);
208             } catch (ScriptException e) {
209                 e.printStackTrace ();
210             }
211         }
212
213         super.show (url);
214     }
215 }
216
Popular Tags