1 32 33 package websphinx.workbench; 34 35 import websphinx.*; 36 import java.util.BitSet ; 37 import java.applet.AppletContext ; 38 import rcm.util.Str; 39 import netscape.javascript.JSObject; 40 import java.net.URL ; 41 42 public class Netscape extends Browser implements ScriptInterpreter { 43 44 JSObject jsobject; 45 BitSet applies = new BitSet (); 47 49 public Netscape (AppletContext context) { 50 super (context); 51 init (); 52 } 53 54 public Netscape (AppletContext context, String frameName) { 55 super (context, frameName); 56 init (); 57 } 58 59 private void init () { 60 try { 61 jsobject = JSObject.getWindow (Context.getApplet ()); 62 } catch (Throwable e) { 63 jsobject = null; 64 } 65 } 66 67 71 72 public ScriptInterpreter getScriptInterpreter () { 73 return jsobject != null ? this : null; 74 } 75 76 public String getLanguage () { 77 return "Javascript"; 78 } 79 80 public Object eval (String expression) throws ScriptException { 81 if (jsobject == null) 83 throw new ScriptException ("Javascript not available"); 84 85 try { 86 return jsobject.eval (expression); 87 } catch (Throwable e) { 88 throw new ScriptException (e.getMessage ()); 89 } 90 } 91 92 static String DBLQUOTE = "\""; 93 static String LINEFEED = "\n"; 94 static String BACKSLASH = "\\"; 95 96 public Object lambda (String [] args, String body) throws ScriptException { 97 StringBuffer code = new StringBuffer (); 98 99 makeApply (args.length); 100 101 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 func; 119 synchronized (jsobject) { 120 func = eval (code.toString()); 121 } 122 System.out.println ("lambda " + func); 123 return func; 124 } 125 126 void makeApply (int n) { 130 if (applies.get (n)) 131 return; 132 applies.set (n); 133 134 StringBuffer app = new StringBuffer (); 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 ("Internal error: cannot create Javascript apply function:\n" + app.toString()); 154 } 155 } 156 157 public Object apply (Object func, Object [] args) throws ScriptException { 158 if (jsobject == null) 159 throw new ScriptException ("Javascript not available"); 160 161 Object [] funcPlusArgs = new Object [1 + args.length]; 162 funcPlusArgs[0] = func; 163 System.arraycopy (args, 0, funcPlusArgs, 1, args.length); 164 165 170 Object result; 171 try { 172 synchronized (jsobject) { 173 result = jsobject.call ("apply" + funcPlusArgs.length, funcPlusArgs); 174 } 175 } catch (Throwable e) { 176 throw new ScriptException (e.getMessage ()); 177 } 178 179 return result; 181 } 182 183 public void set (String name, Object object) { 184 if (jsobject != null) 185 synchronized (jsobject) { 186 jsobject.setMember (name, object); 187 } 188 } 189 190 public Object get (String name) { 191 if (jsobject == null) 192 return null; 193 194 synchronized (jsobject) { 195 return jsobject.getMember (name); 196 } 197 } 198 199 202 public void show (URL url) { 203 if (frameName != null) { 205 String 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 |