1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
9 import org.mozilla.javascript.*;
10 import org.bouncycastle.util.encoders.Base64;
12 /** Singleton class that provides all functionality in the xwt.* namespace */
13 public final class XWT extends JSObject {
15 public static final XWT singleton = new XWT();
17 /** each key is a string representing a filename which the user has already given XWT permission to write to */
18 private static Hashtable safeFiles = new Hashtable();
20 public String getClassName() { return "XWT"; }
21 private XWT() { setSeal(true); }
23 public Object get(String name, Scriptable start) {
24 if (name == null) return null;
25 else if (name.equals("maxdim")) return new Integer(Short.MAX_VALUE);
26 else if (name.equals("parseFloat")) return JSObject.defaultObjects.get("parseFloat", null);
27 else if (name.equals("parseInt")) return JSObject.defaultObjects.get("parseInt", null);
28 else if (name.equals("alt")) return Surface.alt ? Boolean.TRUE : Boolean.FALSE;
29 else if (name.equals("control")) return Surface.control ? Boolean.TRUE : Boolean.FALSE;
30 else if (name.equals("shift")) return Surface.shift ? Boolean.TRUE : Boolean.FALSE;
31 else if (name.equals("date")) return date;
32 else if (name.equals("listfonts")) return listfonts;
33 else if (name.equals("regexp")) return regexp;
34 else if (name.equals("sleep")) return sleep;
35 else if (name.equals("yield")) return yield;
36 else if (name.equals("newBrowserWindow")) return newBrowserWindow;
37 else if (name.equals("textwidth")) return textwidth;
38 else if (name.equals("textheight")) return textheight;
39 else if (name.equals("newBox")) return newBox;
40 else if (name.equals("soap")) return soap;
41 else if (name.equals("xmlrpc")) return xmlrpc;
42 else if (name.equals("origin")) return Main.origin;
43 else if (name.equals("clipboard")) return Platform.getClipBoard();
44 else if (name.equals("altKeyName")) return Platform.altKeyName();
45 else if (name.equals("screenWidth")) return new Integer(Platform.getScreenWidth());
46 else if (name.equals("screenHeight")) return new Integer(Platform.getScreenHeight());
47 else if (name.equals("static")) return Static.getStatic("");
48 else if (name.equals("theme")) return theme;
49 else if (name.equals("openFile")) return openFile;
50 else if (name.equals("saveFile")) return saveFile;
51 else if (name.equals("saveFileAs")) return saveFileAs;
52 else if (name.equals("utfEncode")) return utfEncode;
53 else if (name.equals("fileSeparator")) return File.separator;
54 else if (name.equals("homeDir")) return System.getProperty("user.home");
55 else if (name.equals("tempDir")) return System.getProperty("java.io.tempdir");
56 else if (name.equals("recursivePrintObject")) return recursivePrintObject;
57 else if (name.equals("parseHTML")) return parseHTML;
58 else if (name.equals("button")) {
59 if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
60 else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(1);
61 else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(1);
62 else return new Integer(0);
64 else if (name.equals("println")) return println;
65 else if (name.equals("math")) return org.xwt.util.JSObject.defaultObjects.get("Math", null);
66 else if (name.equals("loadArchive")) return loadArchive;
67 else if (name.equals("prefetchImage")) return prefetchImage;
68 else if (name.equals("prefs")) return prefs;
69 else if (name.equals("encodeURI")) return JSObject.defaultObjects.get("encodeURI", null);
70 else if (name.equals("encodeURIComponent")) return JSObject.defaultObjects.get("encodeURIComponent", null);
71 else if (name.equals("decodeURI")) return JSObject.defaultObjects.get("decodeURI", null);
72 else if (name.equals("decodeURIComponent")) return JSObject.defaultObjects.get("decodeURIComponent", null);
73 else return super.get(name, start);
76 public void put(String name, Scriptable start, Object value) {
77 if (name == null) return;
78 else if (name.equals("thread") && value != null && value instanceof Function) ThreadMessage.newthread((Function)value);
79 else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
81 // FIXME: undocumented, possibly insecure
82 else if (name.equals("proxyAuthorization")) {
83 Proxy.Authorization.authorization = value.toString();
84 Proxy.Authorization.waitingForUser.release();
87 else super.put(name, start, value);
91 // Prefs Object //////////////////////////////////////////////////////////////////////////
93 static Scriptable prefsRPC = new XMLRPC("http://megacz:mypassword@localhost/RPC2", "prefs");
95 private static final JSObject prefs = new JSObject(false, true) {
96 public Object get(String name, Scriptable start) {
97 if (name.equals("get")) return prefsGet;
98 else if (name.equals("list")) return prefsList;
99 else if (name.equals("put")) return prefsPut;
104 private static final JSObject.JSFunction prefsGet = new JSObject.JSFunction() {
105 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
106 if (args.length != 1 || args[0] == null) return null;
108 return ((Function)prefsRPC.get("get", null)).call(cx, null, null, new Object[] { args[0] });
109 } catch (JavaScriptException jse) {
110 Object val = jse.getValue();
111 if (val instanceof JSObject && new Integer(1).equals(((JSObject)val).get("faultCode")))
114 } catch (Exception e) {
116 throw new JavaScriptException(e.toString());
121 private static final JSObject.JSFunction prefsPut = new JSObject.JSFunction() {
122 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
123 if (args.length < 2 || args[0] == null) return null;
125 return ((Function)prefsRPC.get("put", null)).call(cx, null, null, new Object[] { args[0].toString(), args[1] });
126 } catch (JavaScriptException jse) {
127 Object val = jse.getValue();
128 if (val instanceof JSObject && new Integer(1).equals(((JSObject)val).get("faultCode")))
131 } catch (Exception e) {
133 throw new JavaScriptException(e.toString());
138 private static final JSObject.JSFunction prefsList = new JSObject.JSFunction() {
139 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
140 if (args.length < 2 || args[0] == null) return null;
142 return ((Function)prefsRPC.get("list", null)).call(cx, null, null, new Object[] { args[0].toString(), args[1] });
143 } catch (JavaScriptException jse) {
144 Object val = jse.getValue();
145 if (val instanceof JSObject && new Integer(1).equals(((JSObject)val).get("faultCode")))
148 } catch (Exception e) {
150 throw new JavaScriptException(e.toString());
155 private static final JSObject.JSFunction prefsInvoke = new JSObject.JSFunction() {
156 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
157 if (args.length < 2 || args[0] == null) return null;
159 return ((Function)prefs.get("invoke")).call(cx, null, null, new Object[] { args[0].toString(), args[1], "megacz", "mypassword" });
160 } catch (JavaScriptException jse) {
161 Object val = jse.getValue();
162 if (val instanceof JSObject && new Integer(1).equals(((JSObject)val).get("faultCode")))
165 } catch (Exception e) {
167 throw new JavaScriptException(e.toString());
174 // JSFunction Instances ///////////////////////////////////////////////////////////////////
176 private static final JSObject.JSFunction newBrowserWindow = new JSObject.JSFunction() {
177 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
178 if (args.length != 1 || args[0] == null) return null;
179 Platform.newBrowserWindow(args[0].toString());
184 public static final JSObject.JSFunction yield = new JSObject.JSFunction() {
185 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
186 sleep.call(cx, null, null, null);
191 private static final JSObject.JSFunction println = new JSObject.JSFunction() {
192 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
193 if (args.length == 1)
195 String source = cx.interpreterSourceFile;
196 if (source.endsWith("._")) source = source.substring(0, source.length() - 2);
197 Log.log(source, args[0] == null ? "null" : args[0].toString());
203 private static final JSObject.JSFunction date = new JSObject.JSFunction() {
204 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
205 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "Date", args);
206 } catch (Exception e) {
207 if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
208 if (Log.on) Log.log(this, e);
214 private static final JSObject.JSFunction regexp = new JSObject.JSFunction() {
215 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
216 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "RegExp", args);
217 } catch (Exception e) {
218 if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
219 if (Log.on) Log.log(this, e);
225 private static final JSObject.JSFunction listfonts = new JSObject.JSFunction() {
226 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
227 return Context.enter().newArray(org.xwt.util.JSObject.defaultObjects, Platform.listFonts());
231 private static final JSObject.JSFunction theme = new JSObject.JSFunction() {
232 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
233 if (args.length != 2) return null;
234 if (args[0] == null || args[1] == null) return null;
236 for(int i=1; i<args.length; i++) {
237 if (args[i] instanceof String) {
238 String from = (String)args[0];
239 String to = (String)args[i];
240 if (Log.on) Log.log(this, "retheming from " + from + " to " + to);
241 Resources.mapFrom.addElement(from);
242 Resources.mapTo.addElement(to);
246 Function callback = args[args.length - 1] instanceof Function ? (Function)args[args.length - 1] : null;
247 Template.retheme(callback);
252 private static final JSObject.JSFunction xmlrpc = new JSObject.JSFunction() {
253 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
254 if (args.length != 1 || args[0] == null) return null;
255 return new XMLRPC(args[0].toString(), "");
259 private static final JSObject.JSFunction soap = new JSObject.JSFunction() {
260 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
261 if (args.length == 1 && args[0] != null) return new SOAP(args[0].toString(), "", null, null);
262 else if (args.length == 2 && args[0] != null && args[1] != null)
263 return new SOAP(args[0].toString(), "", args[1].toString(), null);
264 else if (args.length == 3 && args[0] != null && args[1] != null && args[2] != null)
265 return new SOAP(args[0].toString(), "", args[1].toString(), args[2].toString());
270 private static final JSObject.JSFunction textwidth = new JSObject.JSFunction() {
271 public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) throws JavaScriptException {
272 if (args.length < 1 || args.length > 2) return null;
273 if (args[0] == null || (args.length == 2 && args[1] == null)) return null;
274 String font = args.length == 1 ? Platform.getDefaultFont() : args[0].toString();
275 String text = args.length == 1 ? args[0].toString() : args[1].toString();
276 XWF xwf = XWF.getXWF(font);
277 if (xwf == null) return new Integer(Platform.stringWidth(font, text));
278 else return new Integer(xwf.stringWidth(text));
283 private static final JSObject.JSFunction textheight = new JSObject.JSFunction() {
284 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
285 if (args.length > 1) return null;
286 if (args.length == 1 && args[0] == null) return null;
287 String font = args.length == 0 || args[0] == null ? Platform.getDefaultFont() : args[0].toString();
288 XWF xwf = XWF.getXWF(font);
289 if (xwf == null) return new Integer(Platform.getMaxAscent(font) + Platform.getMaxDescent(font));
290 else return new Integer(xwf.getMaxAscent() + xwf.getMaxDescent());
294 private static final JSObject.JSFunction newBox = new JSObject.JSFunction() {
295 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
298 if (Log.on) Log.log(XWT.class, "DEPRECATED: xwt.newBox() with multiple arguments is deprecated; use xwt.newBox().apply() " +
299 Context.enter().interpreterSourceFile + ":" + Context.enter().interpreterLine);
301 Function callback = null;
302 for(int i=1; i<args.length; i++)
303 if (args[i] instanceof Function && callback == null)
304 callback = (Function)args[i];
306 Box ret = new Box(args.length == 0 || args[0] == null ? "box" : args[0].toString(), Template.defaultImportList, callback);
308 for(int i=1; i<args.length; i++)
309 if (args[i] instanceof Box)
310 ret.put(ret.numChildren(), null, (Box)args[i]);
311 for(int i=1; i<args.length; i++)
312 if (args[i] instanceof Scriptable && !(args[i] instanceof Box) && !(args[i] instanceof Function)) {
313 Scriptable s = (Scriptable)args[i];
314 Object[] keys = s.getIds();
316 // FIXME: need to ensure that this is putGlobally(), but still run traps...
317 for(int j=0; j<keys.length; j++) ret.put(keys[j].toString(), null, s.get(keys[j].toString(), s));
323 private static final JSObject.JSFunction sleep = new JSObject.JSFunction() {
324 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
325 if (args != null && (args.length != 1 || args[0] == null)) return null;
326 int i = args == null ? 0 : SpecialBoxProperty.stoi(args[0].toString());
328 Thread thread = Thread.currentThread();
329 if (!(thread instanceof ThreadMessage)) {
330 if (Log.on) Log.log(this, "cannot sleep() or yield() in the foreground thread");
333 ThreadMessage mythread = (ThreadMessage)thread;
334 mythread.done.release();
336 if (i > 0) try { Thread.sleep(i); } catch (Exception e) { }
338 MessageQueue.add(mythread);
344 private static final JSObject.JSFunction openFile = new JSObject.JSFunction() {
345 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
346 if (args.length != 1) return null;
347 String file = Platform.fileDialog(args[0].toString(), false);
348 return file == null ? null : new ByteStream(file);
352 private static final JSObject.JSFunction saveFile = new JSObject.JSFunction() {
353 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
354 if (args.length != 2) return null;
355 if (!(args[1] instanceof ByteStream)) return null;
356 String file = args[0].toString();
357 if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
358 file = Platform.fileDialog(file, true);
359 if (file == null) return null;
360 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
363 ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
365 } catch (IOException e) {
366 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
367 if (Log.on) Log.log(ByteStream.class, e);
368 throw new JavaScriptException("error while writing a ByteStream to a file");
373 private static final JSObject.JSFunction saveFileAs = new JSObject.JSFunction() {
374 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
375 if (args.length != 2) return null;
376 if (!(args[1] instanceof ByteStream)) return null;
377 String file = args[0].toString();
378 file = Platform.fileDialog(file, true);
379 if (file == null) return null;
380 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
382 ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
384 } catch (IOException e) {
385 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
386 if (Log.on) Log.log(ByteStream.class, e);
387 throw new JavaScriptException("error while writing a ByteStream to a file");
392 private static final JSObject.JSFunction utfEncode = new JSObject.JSFunction() {
393 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
394 if (args == null || args.length != 1) return null;
395 return new ByteStream(args[0].toString().getBytes());
400 private static final JSObject.JSFunction parseHTML = new JSObject.JSFunction() {
401 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
402 if (args == null || args.length != 1 || args[0] == null) return null;
404 if (args[0] instanceof ByteStream) {
405 return HTML.parseReader(new InputStreamReader(((ByteStream)args[0]).getInputStream()));
407 return HTML.parseReader(new StringReader(args[0].toString()));
409 } catch (IOException e) {
410 if (Log.on) Log.log(HTML.class, "IO Exception while parsing HTML");
411 if (Log.on) Log.log(HTML.class, e);
412 throw new JavaScriptException("error while parsing HTML");
417 private static void recurse(String indent, String name, Object o, Context cx) {
418 if (!name.equals("")) name += " : ";
421 Log.log(cx.interpreterSourceFile, indent + name + "<null>");
423 } else if (o instanceof NativeArray) {
424 Log.log(cx.interpreterSourceFile, indent + name + "<array>");
425 NativeArray na = (NativeArray)o;
426 for(int i=0; i<na.jsGet_length(); i++)
427 recurse(indent + " ", i + "", na.get(i, null), cx);
429 } else if (!(o instanceof NativeDate) && (o instanceof JSObject || o instanceof ScriptableObject)) {
430 Log.log(cx.interpreterSourceFile, indent + name + "<object>");
431 Scriptable s = (Scriptable)o;
432 Object[] keys = s.getIds();
433 for(int i=0; i<keys.length; i++)
434 recurse(indent + " ", keys[i].toString(),
435 keys[i] instanceof Integer ? s.get(((Integer)keys[i]).intValue(), null) : s.get(keys[i].toString(), null), cx);
438 Log.log(cx.interpreterSourceFile, indent + name + o);
443 static final JSObject.JSFunction recursivePrintObject = new JSObject.JSFunction() {
444 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) {
445 if (args == null || args.length != 1) return null;
446 recurse("", "", args[0], cx);
451 private static final JSObject.JSFunction loadArchive = new JSObject.JSFunction() {
452 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
453 if (!ThreadMessage.suspendThread()) return null;
456 if (args == null || args.length < 1 || args[0] == null) return null;
457 URL u = new URL(args[0].toString());
459 Function callback = null;
460 if (args.length == 2 && args[1] != null && args[1] instanceof Function) callback = (Function)args[1];
462 if (!u.getFile().endsWith(".xwar")) {
463 if (Log.on) Log.log(this, "Error: archive names must end with .xwar: " + u.getFile());
464 throw new JavaScriptException("Error: archive names must end with .xwar: " + u.getFile());
467 if (u.getProtocol().equals("http")) {
468 HTTP http = new HTTP(u.toString());
469 if (Main.originAddr == null) {
471 Main.originAddr = InetAddress.getByName(u.getHost());
472 } catch (UnknownHostException e) {
473 if (Log.on) Log.log(this, "couldn't resolve " + u.getHost() + "; proceeding without permissions");
474 Main.originAddr = InetAddress.getByName("0.0.0.0");
477 Main.originAddr = InetAddress.getByName("0.0.0.0");
479 HTTP.HTTPInputStream in = http.GET();
480 Resources.loadArchive(in, in.getContentLength(), callback);
482 } else if (u.getProtocol().equals("file")) {
483 if (Main.originAddr != null) {
484 if (Log.on) Log.log(this, "scripts downloaded from the network may not load xwars from the local filesystem");
485 throw new JavaScriptException("scripts downloaded from the network may not load xwars from the local filesystem");
487 Resources.loadArchive(new FileInputStream(u.getFile()), (int)new File(u.getFile()).length(), callback);
490 if (Log.on) Log.log(this, "unknown protocol \"" + u.getProtocol() + "\"");
491 throw new JavaScriptException("unknown protocol \"" + u.getProtocol() + "\"");
494 } catch (MalformedURLException me) {
495 if (Log.on) Log.log(this, "Malformed URL: " + args[0]);
496 if (Log.on) Log.log(this, me);
497 throw new JavaScriptException(me.toString());
499 } catch (IOException ioe) {
500 if (Log.on) Log.log(this, "IOException while loading archive:");
501 if (Log.on) Log.log(this, ioe);
502 throw new JavaScriptException(ioe.toString());
505 ThreadMessage.resumeThread();
512 private static final JSObject.JSFunction prefetchImage = new JSObject.JSFunction() {
513 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
514 if (args == null || args.length < 1 || args[0] == null) return null;
515 Box.getImage(args[0].toString(), args.length > 1 && args[1] instanceof Function ? (Function)args[1] : null);