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.util.BitSet;
037: import java.applet.AppletContext;
038: import rcm.util.Str;
039: import netscape.javascript.JSObject;
040: import java.net.URL;
041:
042: public class Netscape extends Browser implements ScriptInterpreter {
043:
044: JSObject jsobject;
045: // Javascript interpreter
046: BitSet applies = new BitSet();
047:
048: // n-ary apply functions that have already been made
049:
050: public Netscape(AppletContext context) {
051: super (context);
052: init();
053: }
054:
055: public Netscape(AppletContext context, String frameName) {
056: super (context, frameName);
057: init();
058: }
059:
060: private void init() {
061: try {
062: jsobject = JSObject.getWindow(Context.getApplet());
063: } catch (Throwable e) {
064: jsobject = null;
065: }
066: }
067:
068: /*
069: * JavaScript interpreter
070: *
071: */
072:
073: public ScriptInterpreter getScriptInterpreter() {
074: return jsobject != null ? this : null;
075: }
076:
077: public String getLanguage() {
078: return "Javascript";
079: }
080:
081: public Object eval(String expression) throws ScriptException {
082: //System.out.println ("evaluating " + expression);
083: if (jsobject == null)
084: throw new ScriptException("Javascript not available");
085:
086: try {
087: return jsobject.eval(expression);
088: } catch (Throwable e) {
089: throw new ScriptException(e.getMessage());
090: }
091: }
092:
093: static String DBLQUOTE = "\"";
094: static String LINEFEED = "\n";
095: static String BACKSLASH = "\\";
096:
097: public Object lambda(String[] args, String body)
098: throws ScriptException {
099: StringBuffer code = new StringBuffer();
100:
101: makeApply(args.length);
102:
103: // Function ("arg0", "arg1", ..., "argn", body)
104: code.append("Function (");
105: if (args != null)
106: for (int i = 0; i < args.length; ++i) {
107: code.append(DBLQUOTE);
108: code.append(args[i]);
109: code.append(DBLQUOTE + ", ");
110: }
111: code.append(DBLQUOTE);
112:
113: body = Str.replace(body, BACKSLASH, BACKSLASH + BACKSLASH);
114: body = Str.replace(body, DBLQUOTE, BACKSLASH + DBLQUOTE);
115: body = Str.replace(body, LINEFEED, BACKSLASH + LINEFEED);
116: code.append(body);
117: code.append(DBLQUOTE + ")");
118:
119: System.out.println("evaluating\n" + code + "\n");
120: Object func;
121: synchronized (jsobject) {
122: func = eval(code.toString());
123: }
124: System.out.println("lambda " + func);
125: return func;
126: }
127:
128: // Create an application function for n-ary functions:
129: // apply(N+1) = Function ('f', 'a0', 'a1', ..., 'aN-1', 'f (a0, a1, ..., aN-1)')
130: // e.g. apply2 = Function ('f', 'a0', 'f (a0)')
131: void makeApply(int n) {
132: if (applies.get(n))
133: return;
134: applies.set(n);
135:
136: StringBuffer app = new StringBuffer();
137: app.append("Function ('f', ");
138: for (int i = 0; i < n; ++i) {
139: app.append("'a");
140: app.append(String.valueOf(i));
141: app.append("',");
142: }
143: app.append("'return f (");
144: for (int i = 0; i < n; ++i) {
145: if (i > 0)
146: app.append(',');
147: app.append("a");
148: app.append(String.valueOf(i));
149: }
150: app.append(")')");
151:
152: try {
153: set("apply" + (n + 1), eval(app.toString()));
154: } catch (ScriptException e) {
155: throw new RuntimeException(
156: "Internal error: cannot create Javascript apply function:\n"
157: + app.toString());
158: }
159: }
160:
161: public Object apply(Object func, Object[] args)
162: throws ScriptException {
163: if (jsobject == null)
164: throw new ScriptException("Javascript not available");
165:
166: Object[] funcPlusArgs = new Object[1 + args.length];
167: funcPlusArgs[0] = func;
168: System.arraycopy(args, 0, funcPlusArgs, 1, args.length);
169:
170: //System.out.print ("applying ");
171: //for (int i=0; i<funcPlusArgs.length; ++i)
172: // System.out.print (funcPlusArgs + " ");
173: //System.out.println ();
174:
175: Object result;
176: try {
177: synchronized (jsobject) {
178: result = jsobject.call("apply" + funcPlusArgs.length,
179: funcPlusArgs);
180: }
181: } catch (Throwable e) {
182: throw new ScriptException(e.getMessage());
183: }
184:
185: //System.out.println ("returned " + result);
186: return result;
187: }
188:
189: public void set(String name, Object object) {
190: if (jsobject != null)
191: synchronized (jsobject) {
192: jsobject.setMember(name, object);
193: }
194: }
195:
196: public Object get(String name) {
197: if (jsobject == null)
198: return null;
199:
200: synchronized (jsobject) {
201: return jsobject.getMember(name);
202: }
203: }
204:
205: /*
206: * Show pages in browser
207: */
208: public void show(URL url) {
209: // bring the window forward
210: if (frameName != null) {
211: String code = "window.open ('', '" + frameName
212: + "').focus ()";
213: try {
214: eval(code);
215: } catch (ScriptException e) {
216: e.printStackTrace();
217: }
218: }
219:
220: super.show(url);
221: }
222: }
|