1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
10 import org.bouncycastle.util.encoders.Base64;
12 /** Singleton class that provides all functionality in the xwt.* namespace */
13 public final class XWT extends JS.Obj {
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 Object get(Object name) {
21 if (name.equals("parseFloat")) throw new Error("not implemented");
22 else if (name.equals("parseInt")) throw new Error("not implemented");
23 else if (name.equals("alt")) return Surface.alt ? Boolean.TRUE : Boolean.FALSE;
24 else if (name.equals("control")) return Surface.control ? Boolean.TRUE : Boolean.FALSE;
25 else if (name.equals("shift")) return Surface.shift ? Boolean.TRUE : Boolean.FALSE;
26 else if (name.equals("clipboard")) return Platform.getClipBoard();
27 else if (name.equals("static")) return Static.getStatic("");
28 else if (name.equals("button")) {
29 if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
30 else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(1);
31 else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(1);
32 else return new Integer(0);
34 else if (name.equals("encodeURI")) throw new Error("not implemented");
35 else if (name.equals("encodeURIComponent")) throw new Error("not implemented");
36 else if (name.equals("decodeURI")) throw new Error("not implemented");
37 else if (name.equals("decodeURIComponent")) throw new Error("not implemented");
38 else return super.get(name);
41 public void put(Object name, Object value) {
42 if (name.equals("thread") && value != null && value instanceof JS.Function) ThreadMessage.newthread((JS.Function)value);
43 else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
44 else if (name.equals("proxyAuthorization")) {
45 // FIXME: undocumented, possibly insecure
46 Proxy.Authorization.authorization = value.toString();
47 Proxy.Authorization.waitingForUser.release();
48 } else super.put(name, value);
52 put("maxdim", new Integer(Short.MAX_VALUE));
53 put("origin", Main.origin);
54 put("altKeyName", Platform.altKeyName());
55 put("screenWidth", new Integer(Platform.getScreenWidth()));
56 put("screenHeight", new Integer(Platform.getScreenHeight()));
57 put("fileSeparator", File.separator);
58 put("homeDir", System.getProperty("user.home"));
59 put("tempDir", System.getProperty("java.io.tempdir"));
61 put("math", new JS.Obj() { public Object get(Object name) {
62 if ("ceil".equals(name)) return new JS.Function() { public Object _call(JS.Array args)
63 { if (args.elementAt(0) == null) return null;
64 return new Long((long)Math.ceil(Double.parseDouble(args.elementAt(0).toString()))); } };
65 else if ("floor".equals(name)) return new JS.Function() { public Object _call(JS.Array args)
66 { if (args.elementAt(0) == null) return null;
67 return new Long((long)Math.floor(Double.parseDouble(args.elementAt(0).toString()))); } };
68 else if ("round".equals(name)) return new JS.Function() { public Object _call(JS.Array args)
69 { if (args.elementAt(0) == null) return null;
70 return new Long((long)Math.round(Double.parseDouble(args.elementAt(0).toString()))); } };
71 else if ("abs".equals(name)) return new JS.Function() { public Object _call(JS.Array args)
72 { if (args.elementAt(0) == null) return null;
73 return new Long((long)Math.abs(Double.parseDouble(args.elementAt(0).toString()))); } };
74 else if ("min".equals(name)) return new JS.Function() { public Object _call(JS.Array args) {
75 if (args.length() < 2 || args.elementAt(0) == null || args.elementAt(1) == null) return args.elementAt(0);
76 return new Double(Math.min(((Number)args.elementAt(0)).doubleValue(),
77 ((Number)args.elementAt(1)).doubleValue())); } };
78 else if ("max".equals(name)) return new JS.Function() { public Object _call(JS.Array args) {
79 if (args.length() < 2) return args.elementAt(0);
80 return new Double(Math.max(((Number)args.elementAt(0)).doubleValue(),
81 ((Number)args.elementAt(1)).doubleValue())); } };
85 put("newBrowserWindow", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
86 if (args.length() != 1 || args.elementAt(0) == null) return null;
87 Platform.newBrowserWindow(args.elementAt(0).toString());
91 put("yield", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
96 put("theme", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
97 if (args.length() != 2) return null;
98 if (args.elementAt(0) == null || args.elementAt(1) == null) return null;
99 for(int i=1; i<args.length(); i++) {
100 if (args.elementAt(i) instanceof String) {
101 String from = (String)args.elementAt(0);
102 String to = (String)args.elementAt(i);
103 if (Log.on) Log.log(this, "retheming from " + from + " to " + to);
104 Resources.mapFrom.addElement(from);
105 Resources.mapTo.addElement(to);
108 JS.Function callback = args.elementAt(args.length() - 1) instanceof Function ?
109 (Function)args.elementAt(args.length() - 1) : null;
110 Template.retheme(callback);
114 put("println", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
115 if (args.length() != 1) return null;
116 if (Log.on) Log.log(this, JS.getFileAndLine() + " " +
117 (args.elementAt(0) == null ? "null" : args.elementAt(0).toString()));
121 put("date", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
122 Log.log(XWT.class, "date not implemented");
123 //throw new Error("not implemented");
127 put("regexp", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
128 //throw new Error("not implemented");
129 Log.log(XWT.class, "regexp not implemented");
133 put("listfonts", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
134 Object[] fonts = Platform.listFonts();
135 JS.Array ret = new JS.Array();
136 for(int i=0; i<fonts.length; i++) ret.addElement(fonts[i]);
140 put("theme", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
141 Log.log(XWT.class, "xwt.theme() not implemented...");
145 put("xmlrpc", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
146 if (args.length() != 1 || args.elementAt(0) == null) return null;
147 return new XMLRPC(args.elementAt(0).toString(), "");
150 put("soap", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
151 if (args.length() == 1 && args.elementAt(0) != null) return new SOAP(args.elementAt(0).toString(), "", null, null);
152 else if (args.length() == 2 && args.elementAt(0) != null && args.elementAt(1) != null)
153 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), null);
154 else if (args.length() == 3 && args.elementAt(0) != null && args.elementAt(1) != null && args.elementAt(2) != null)
155 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), args.elementAt(2).toString());
159 put("textwidth", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
160 if (args.length() < 1 || args.length() > 2) return null;
161 if (args.elementAt(0) == null || (args.length() == 2 && args.elementAt(1) == null)) return null;
162 String font = args.length() == 1 ? Platform.getDefaultFont() : args.elementAt(0).toString();
163 String text = args.length() == 1 ? args.elementAt(0).toString() : args.elementAt(1).toString();
164 XWF xwf = XWF.getXWF(font);
165 if (xwf == null) return new Integer(Platform.stringWidth(font, text));
166 else return new Integer(xwf.stringWidth(text));
169 put("textheight", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
170 if (args.length() > 1) return null;
171 if (args.length() == 1 && args.elementAt(0) == null) return null;
172 String font = args.length() == 0 || args.elementAt(0) == null ? Platform.getDefaultFont() : args.elementAt(0).toString();
173 XWF xwf = XWF.getXWF(font);
174 if (xwf == null) return new Integer(Platform.getMaxAscent(font) + Platform.getMaxDescent(font));
175 else return new Integer(xwf.getMaxAscent() + xwf.getMaxDescent());
178 put("newBox", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
179 if (args.length() > 0) Log.log(XWT.class, "DEPRECATED: xwt.newBox() with multiple arguments is deprecated; use xwt.newBox().apply()");
180 JS.Function callback = null;
181 for(int i=1; i<args.length(); i++)
182 if (args.elementAt(i) instanceof JS.Function && callback == null)
183 callback = (JS.Function)args.elementAt(i);
184 Box ret = new Box(args.length() == 0 || args.elementAt(0) == null ? "box" : args.elementAt(0).toString(),
185 Template.defaultImportList, callback);
186 for(int i=1; i<args.length(); i++)
187 if (args.elementAt(i) instanceof Box)
188 ret.put(ret.numChildren(), (Box)args.elementAt(i));
189 for(int i=1; i<args.length(); i++)
190 if (args.elementAt(i) instanceof JS && !(args.elementAt(i) instanceof Box) && !(args.elementAt(i) instanceof JS.Function)) {
191 JS s = (JS)args.elementAt(i);
192 Object[] keys = s.keys();
193 for(int j=0; j<keys.length; j++) ret.put(keys[j].toString(), s.get(keys[j].toString()));
198 put("sleep", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
199 if (args != null && (args.length() != 1 || args.elementAt(0) == null)) return null;
200 int i = args == null ? 0 : SpecialBoxProperty.stoi(args.elementAt(0).toString());
205 put("openFile", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
206 if (args.length() != 1) return null;
207 String file = Platform.fileDialog(args.elementAt(0).toString(), false);
208 return file == null ? null : new ByteStream(file);
211 put("saveFile", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
212 if (args.length() != 2) return null;
213 if (!(args.elementAt(1) instanceof ByteStream)) return null;
214 String file = args.elementAt(0).toString();
215 if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
216 file = Platform.fileDialog(file, true);
217 if (file == null) return null;
218 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
221 ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
223 } catch (IOException e) {
224 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
225 if (Log.on) Log.log(ByteStream.class, e);
226 throw new JS.Exn("error while writing a ByteStream to a file");
230 put("saveFileAs", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
231 if (args.length() != 2) return null;
232 if (!(args.elementAt(1) instanceof ByteStream)) return null;
233 String file = args.elementAt(0).toString();
234 file = Platform.fileDialog(file, true);
235 if (file == null) return null;
236 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
238 ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
240 } catch (IOException e) {
241 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
242 if (Log.on) Log.log(ByteStream.class, e);
243 throw new JS.Exn("error while writing a ByteStream to a file");
247 put("utfEncode", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
248 if (args == null || args.length() != 1) return null;
249 return new ByteStream(args.elementAt(0).toString().getBytes());
252 put("parseHTML", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
253 if (args == null || args.length() != 1 || args.elementAt(0) == null) return null;
255 if (args.elementAt(0) instanceof ByteStream) {
256 return HTML.parseReader(new InputStreamReader(((ByteStream)args.elementAt(0)).getInputStream()));
258 return HTML.parseReader(new StringReader(args.elementAt(0).toString()));
260 } catch (IOException e) {
261 if (Log.on) Log.log(HTML.class, "IO Exception while parsing HTML");
262 if (Log.on) Log.log(HTML.class, e);
263 throw new JS.Exn("error while parsing HTML");
268 put("recursivePrintObject", new JS.Function() { public Object _call(JS.Array args) {
269 if (args.length() != 1) return null;
270 recurse("", "", args.elementAt(0));
274 put("loadArchive", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
275 if (!ThreadMessage.suspendThread()) return null;
278 if (args == null || args.length() < 1 || args.elementAt(0) == null) return null;
279 URL u = new URL(args.elementAt(0).toString());
281 JS.Function callback = null;
282 if (args.length() == 2 && args.elementAt(1) != null && args.elementAt(1) instanceof JS.Function)
283 callback = (JS.Function)args.elementAt(1);
285 if (!u.getFile().endsWith(".xwar")) {
286 if (Log.on) Log.log(this, "Error: archive names must end with .xwar: " + u.getFile());
287 throw new JS.Exn("Error: archive names must end with .xwar: " + u.getFile());
290 if (u.getProtocol().equals("http")) {
291 HTTP http = new HTTP(u.toString());
292 if (Main.originAddr == null) {
294 Main.originAddr = InetAddress.getByName(u.getHost());
295 } catch (UnknownHostException e) {
296 if (Log.on) Log.log(this, "couldn't resolve " + u.getHost() + "; proceeding without permissions");
297 Main.originAddr = InetAddress.getByName("0.0.0.0");
300 Main.originAddr = InetAddress.getByName("0.0.0.0");
302 HTTP.HTTPInputStream in = http.GET();
303 Resources.loadArchive(in, in.getContentLength(), callback);
305 } else if (u.getProtocol().equals("file")) {
306 if (Main.originAddr != null) {
307 if (Log.on) Log.log(this, "scripts downloaded from the network may not load xwars from the local filesystem");
308 throw new JS.Exn("scripts downloaded from the network may not load xwars from the local filesystem");
310 Resources.loadArchive(new FileInputStream(u.getFile()), (int)new File(u.getFile()).length(), callback);
313 if (Log.on) Log.log(this, "unknown protocol \"" + u.getProtocol() + "\"");
314 throw new JS.Exn("unknown protocol \"" + u.getProtocol() + "\"");
317 } catch (MalformedURLException me) {
318 if (Log.on) Log.log(this, "Malformed URL: " + args.elementAt(0));
319 if (Log.on) Log.log(this, me);
320 throw new JS.Exn(me.toString());
322 } catch (IOException ioe) {
323 if (Log.on) Log.log(this, "IOException while loading archive:");
324 if (Log.on) Log.log(this, ioe);
325 throw new JS.Exn(ioe.toString());
328 ThreadMessage.resumeThread();
335 put("prefetchImage", new JS.Function() { public Object _call(JS.Array args) throws JS.Exn {
336 if (args == null || args.length() < 1 || args.elementAt(0) == null) return null;
337 Box.getImage(args.elementAt(0).toString(),
338 args.length() > 1 && args.elementAt(1) instanceof JS.Function ? (JS.Function)args.elementAt(1) : null);
343 private static void recurse(String indent, String name, Object o) {
344 if (!name.equals("")) name += " : ";
347 Log.log(JS.getCurrentFunction().getSourceName(), indent + name + "<null>");
349 } else if (o instanceof JS.Array) {
350 Log.log(JS.getCurrentFunction().getSourceName(), indent + name + "<array>");
351 JS.Array na = (JS.Array)o;
352 for(int i=0; i<na.length(); i++)
353 recurse(indent + " ", i + "", na.elementAt(i));
355 } else if (o instanceof JS) {
356 Log.log(JS.getCurrentFunction().getSourceName(), indent + name + "<object>");
358 Object[] keys = s.keys();
359 for(int i=0; i<keys.length; i++)
360 recurse(indent + " ", keys[i].toString(),
361 (keys[i] instanceof Integer) ?
362 s.get(((Integer)keys[i])) : s.get(keys[i].toString()));
365 Log.log(JS.getCurrentFunction().getSourceName(), indent + name + o);
371 public static void sleep(int i) {
372 Thread thread = Thread.currentThread();
373 if (!(thread instanceof ThreadMessage)) {
374 if (Log.on) Log.log(XWT.class, "cannot sleep() or yield() in the foreground thread");
376 ThreadMessage mythread = (ThreadMessage)thread;
377 mythread.done.release();
378 if (i > 0) try { Thread.sleep(i); } catch (Exception e) { }
379 MessageQueue.add(mythread);