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("encodeURI")) return JSObject.defaultObjects.get("encodeURI", null);
69 else if (name.equals("encodeURIComponent")) return JSObject.defaultObjects.get("encodeURIComponent", null);
70 else if (name.equals("decodeURI")) return JSObject.defaultObjects.get("decodeURI", null);
71 else if (name.equals("decodeURIComponent")) return JSObject.defaultObjects.get("decodeURIComponent", null);
72 else return super.get(name, start);
75 public void put(String name, Scriptable start, Object value) {
76 if (name == null) return;
77 else if (name.equals("thread") && value != null && value instanceof Function) ThreadMessage.newthread((Function)value);
78 else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
80 // FIXME: undocumented, possibly insecure
81 else if (name.equals("proxyAuthorization")) {
82 Proxy.Authorization.authorization = value.toString();
83 Proxy.Authorization.waitingForUser.release();
86 else super.put(name, start, value);
90 // JSFunction Instances ///////////////////////////////////////////////////////////////////
92 private static final JSObject.JSFunction newBrowserWindow = new JSObject.JSFunction() {
93 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
94 if (args.length != 1 || args[0] == null) return null;
95 Platform.newBrowserWindow(args[0].toString());
100 public static final JSObject.JSFunction yield = new JSObject.JSFunction() {
101 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
102 sleep.call(cx, null, null, null);
107 private static final JSObject.JSFunction println = new JSObject.JSFunction() {
108 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
109 if (args.length == 1)
111 String source = cx.interpreterSourceFile;
112 if (source.endsWith("._")) source = source.substring(0, source.length() - 2);
113 Log.log(source, args[0] == null ? "null" : args[0].toString());
119 private static final JSObject.JSFunction date = new JSObject.JSFunction() {
120 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
121 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "Date", args);
122 } catch (Exception e) {
123 if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
124 if (Log.on) Log.log(this, e);
130 private static final JSObject.JSFunction regexp = new JSObject.JSFunction() {
131 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
132 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "RegExp", args);
133 } catch (Exception e) {
134 if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
135 if (Log.on) Log.log(this, e);
141 private static final JSObject.JSFunction listfonts = new JSObject.JSFunction() {
142 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
143 return Context.enter().newArray(org.xwt.util.JSObject.defaultObjects, Platform.listFonts());
147 private static final JSObject.JSFunction theme = new JSObject.JSFunction() {
148 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
149 if (args.length != 2) return null;
150 if (args[0] == null || args[1] == null) return null;
151 Template.retheme(args[0].toString(), args[1].toString());
156 private static final JSObject.JSFunction xmlrpc = new JSObject.JSFunction() {
157 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
158 if (args.length != 1 || args[0] == null) return null;
159 return new XMLRPC(args[0].toString(), "");
163 private static final JSObject.JSFunction soap = new JSObject.JSFunction() {
164 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
165 if (args.length == 1 && args[0] != null) return new SOAP(args[0].toString(), "", null, null);
166 else if (args.length == 2 && args[0] != null && args[1] != null)
167 return new SOAP(args[0].toString(), "", args[1].toString(), null);
168 else if (args.length == 3 && args[0] != null && args[1] != null && args[2] != null)
169 return new SOAP(args[0].toString(), "", args[1].toString(), args[2].toString());
174 private static final JSObject.JSFunction textwidth = new JSObject.JSFunction() {
175 public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) throws JavaScriptException {
176 if (args.length < 1 || args.length > 2) return null;
177 if (args[0] == null || (args.length == 2 && args[1] == null)) return null;
178 String font = args.length == 1 ? Platform.getDefaultFont() : args[0].toString();
179 String text = args.length == 1 ? args[0].toString() : args[1].toString();
180 XWF xwf = XWF.getXWF(font);
181 if (xwf == null) return new Integer(Platform.stringWidth(font, text));
182 else return new Integer(xwf.stringWidth(text));
187 private static final JSObject.JSFunction textheight = new JSObject.JSFunction() {
188 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
189 if (args.length > 1) return null;
190 if (args.length == 1 && args[0] == null) return null;
191 String font = args.length == 0 || args[0] == null ? Platform.getDefaultFont() : args[0].toString();
192 XWF xwf = XWF.getXWF(font);
193 if (xwf == null) return new Integer(Platform.getMaxAscent(font) + Platform.getMaxDescent(font));
194 else return new Integer(xwf.getMaxAscent() + xwf.getMaxDescent());
198 private static final JSObject.JSFunction newBox = new JSObject.JSFunction() {
199 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
202 if (Log.on) Log.log(XWT.class, "DEPRECATED: xwt.newBox() with multiple arguments is deprecated; use xwt.newBox().apply() " +
203 Context.enter().interpreterSourceFile + ":" + Context.enter().interpreterLine);
205 Function callback = null;
206 for(int i=1; i<args.length; i++)
207 if (args[i] instanceof Function && callback == null)
208 callback = (Function)args[i];
210 Box ret = new Box(args.length == 0 || args[0] == null ? "box" : args[0].toString(), Template.defaultImportList, callback);
212 for(int i=1; i<args.length; i++)
213 if (args[i] instanceof Box)
214 ret.put(ret.numChildren(), null, (Box)args[i]);
215 for(int i=1; i<args.length; i++)
216 if (args[i] instanceof Scriptable && !(args[i] instanceof Box) && !(args[i] instanceof Function)) {
217 Scriptable s = (Scriptable)args[i];
218 Object[] keys = s.getIds();
220 // FIXME: need to ensure that this is putGlobally(), but still run traps...
221 for(int j=0; j<keys.length; j++) ret.put(keys[j].toString(), null, s.get(keys[j].toString(), s));
227 private static final JSObject.JSFunction sleep = new JSObject.JSFunction() {
228 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
229 if (args != null && (args.length != 1 || args[0] == null)) return null;
230 int i = args == null ? 0 : SpecialBoxProperty.stoi(args[0].toString());
232 Thread thread = Thread.currentThread();
233 if (!(thread instanceof ThreadMessage)) {
234 if (Log.on) Log.log(this, "cannot sleep() or yield() in the foreground thread");
237 ThreadMessage mythread = (ThreadMessage)thread;
238 mythread.done.release();
240 if (i > 0) try { Thread.sleep(i); } catch (Exception e) { }
242 MessageQueue.add(mythread);
248 private static final JSObject.JSFunction openFile = new JSObject.JSFunction() {
249 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
250 if (args.length != 1) return null;
251 String file = Platform.fileDialog(args[0].toString(), false);
252 return file == null ? null : new ByteStream(file);
256 private static final JSObject.JSFunction saveFile = new JSObject.JSFunction() {
257 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
258 if (args.length != 2) return null;
259 if (!(args[1] instanceof ByteStream)) return null;
260 String file = args[0].toString();
261 if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
262 file = Platform.fileDialog(file, true);
263 if (file == null) return null;
264 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
267 ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
269 } catch (IOException e) {
270 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
271 if (Log.on) Log.log(ByteStream.class, e);
272 throw new JavaScriptException("error while writing a ByteStream to a file");
277 private static final JSObject.JSFunction saveFileAs = new JSObject.JSFunction() {
278 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
279 if (args.length != 2) return null;
280 if (!(args[1] instanceof ByteStream)) return null;
281 String file = args[0].toString();
282 file = Platform.fileDialog(file, true);
283 if (file == null) return null;
284 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
286 ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
288 } catch (IOException e) {
289 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
290 if (Log.on) Log.log(ByteStream.class, e);
291 throw new JavaScriptException("error while writing a ByteStream to a file");
296 private static final JSObject.JSFunction utfEncode = new JSObject.JSFunction() {
297 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
298 if (args == null || args.length != 1) return null;
299 return new ByteStream(args[0].toString().getBytes());
304 private static final JSObject.JSFunction parseHTML = new JSObject.JSFunction() {
305 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
306 if (args == null || args.length != 1 || args[0] == null) return null;
308 if (args[0] instanceof ByteStream) {
309 return HTML.parseReader(new InputStreamReader(((ByteStream)args[0]).getInputStream()));
311 return HTML.parseReader(new StringReader(args[0].toString()));
313 } catch (IOException e) {
314 if (Log.on) Log.log(HTML.class, "IO Exception while parsing HTML");
315 if (Log.on) Log.log(HTML.class, e);
316 throw new JavaScriptException("error while parsing HTML");
321 private static void recurse(String indent, String name, Object o, Context cx) {
322 if (!name.equals("")) name += " : ";
325 Log.log(cx.interpreterSourceFile, indent + name + "<null>");
327 } else if (o instanceof NativeArray) {
328 Log.log(cx.interpreterSourceFile, indent + name + "<array>");
329 NativeArray na = (NativeArray)o;
330 for(int i=0; i<na.jsGet_length(); i++)
331 recurse(indent + " ", i + "", na.get(i, null), cx);
333 } else if (!(o instanceof NativeDate) && (o instanceof JSObject || o instanceof ScriptableObject)) {
334 Log.log(cx.interpreterSourceFile, indent + name + "<object>");
335 Scriptable s = (Scriptable)o;
336 Object[] keys = s.getIds();
337 for(int i=0; i<keys.length; i++)
338 recurse(indent + " ", keys[i].toString(),
339 keys[i] instanceof Integer ? s.get(((Integer)keys[i]).intValue(), null) : s.get(keys[i].toString(), null), cx);
342 Log.log(cx.interpreterSourceFile, indent + name + o);
347 private static final JSObject.JSFunction recursivePrintObject = new JSObject.JSFunction() {
348 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
349 if (args == null || args.length != 1) return null;
350 recurse("", "", args[0], cx);
355 private static final JSObject.JSFunction loadArchive = new JSObject.JSFunction() {
356 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
357 if (!ThreadMessage.suspendThread()) return null;
360 if (args == null || args.length < 1 || args[0] == null) return null;
361 URL u = new URL(args[0].toString());
363 Function callback = null;
364 if (args.length == 2 && args[1] != null && args[1] instanceof Function) callback = (Function)args[1];
366 if (!u.getFile().endsWith(".xwar")) {
367 if (Log.on) Log.log(this, "Error: archive names must end with .xwar: " + u.getFile());
368 throw new JavaScriptException("Error: archive names must end with .xwar: " + u.getFile());
371 if (u.getProtocol().equals("http")) {
372 HTTP http = new HTTP(u.toString());
373 if (Main.originAddr == null) {
375 Main.originAddr = InetAddress.getByName(u.getHost());
376 } catch (UnknownHostException e) {
377 if (Log.on) Log.log(this, "couldn't resolve " + u.getHost() + "; proceeding without permissions");
378 Main.originAddr = InetAddress.getByName("0.0.0.0");
381 Main.originAddr = InetAddress.getByName("0.0.0.0");
383 HTTP.HTTPInputStream in = http.GET();
384 Resources.loadArchive(in, in.getContentLength(), callback);
386 } else if (u.getProtocol().equals("file")) {
387 if (Main.originAddr != null) {
388 if (Log.on) Log.log(this, "scripts downloaded from the network may not load xwars from the local filesystem");
389 throw new JavaScriptException("scripts downloaded from the network may not load xwars from the local filesystem");
391 Resources.loadArchive(new FileInputStream(u.getFile()), (int)new File(u.getFile()).length(), callback);
394 if (Log.on) Log.log(this, "unknown protocol \"" + u.getProtocol() + "\"");
395 throw new JavaScriptException("unknown protocol \"" + u.getProtocol() + "\"");
398 } catch (MalformedURLException me) {
399 if (Log.on) Log.log(this, "Malformed URL: " + args[0]);
400 if (Log.on) Log.log(this, me);
401 throw new JavaScriptException(me.toString());
403 } catch (IOException ioe) {
404 if (Log.on) Log.log(this, "IOException while loading archive:");
405 if (Log.on) Log.log(this, ioe);
406 throw new JavaScriptException(ioe.toString());
409 ThreadMessage.resumeThread();
416 private static final JSObject.JSFunction prefetchImage = new JSObject.JSFunction() {
417 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
418 if (args == null || args.length < 1 || args[0] == null) return null;
419 Box.getImage(args[0].toString(), args.length > 1 && args[1] instanceof Function ? (Function)args[1] : null);