7e7e5fef69d6c4f7ab83c5bcd44b0e3cda2e99df
[org.ibex.core.git] / src / org / xwt / XWT.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3
4 import java.io.*;
5 import java.net.*;
6 import java.text.*;
7 import java.util.*;
8 import org.xwt.js.*;
9 import org.xwt.util.*;
10 import org.bouncycastle.util.encoders.Base64;
11
12 /** Singleton class that provides all functionality in the xwt.* namespace */
13 public final class XWT extends JS.Obj {
14
15     public Res resourceRoot = null;
16
17     public static final XWT singleton = new XWT();
18     private final JS xwtMath = new XWTMath();
19     private final JS xwtString = new XWTString();
20
21     /** each key is a string representing a filename which the user has already given XWT permission to write to */
22     private static Hashtable safeFiles = new Hashtable();
23
24     public Object get(Object name) {
25         if (name.equals("alt")) return Surface.alt ? Boolean.TRUE : Boolean.FALSE;
26         else if (name.equals("control")) return Surface.control ? Boolean.TRUE : Boolean.FALSE;
27         else if (name.equals("shift")) return Surface.shift ? Boolean.TRUE : Boolean.FALSE;
28         else if (name.equals("clipboard")) return Platform.getClipBoard();
29         else if (name.equals("static")) return Static.getStatic("");
30         else if (name.equals("button")) {
31             if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
32             else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(1);
33             else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(1);
34             else return new Integer(0);
35         }
36         else return super.get(name);
37     }
38
39     public void put(Object name, Object value) {
40         if (name.equals("thread") && value != null && value instanceof JS.Callable) ThreadMessage.newthread((JS.Callable)value);
41         else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
42         else if (name.equals("proxyAuthorization")) {
43             // FIXME: undocumented, possibly insecure
44             HTTP.Proxy.Authorization.authorization = value.toString();
45             HTTP.Proxy.Authorization.waitingForUser.release();
46         } else super.put(name, value);
47     }
48
49     private XWT() {
50         super.put("maxdim", new Integer(Short.MAX_VALUE));
51         super.put("origin", Main.origin);
52         super.put("altKeyName", Platform.altKeyName());
53         super.put("screenWidth", new Integer(Platform.getScreenWidth()));
54         super.put("screenHeight", new Integer(Platform.getScreenHeight()));
55         super.put("fileSeparator", File.separator);
56         super.put("homeDir", System.getProperty("user.home"));
57         super.put("tempDir", System.getProperty("java.io.tempdir"));
58         super.put("math", xwtMath);
59         super.put("string", xwtString);
60
61         super.put("newBrowserWindow", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
62             if (args.length() != 1 || args.elementAt(0) == null) return null;
63             Platform.newBrowserWindow(args.elementAt(0).toString());
64             return null;
65         }});
66
67         super.put("parseInt",xwtString.get("parseInt"));
68         super.put("parseFloat",xwtString.get("parseFloat"));
69         
70         super.put("yield", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
71             sleep(0);
72             return null;
73         }});
74
75         super.put("load", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
76             return Res.stringToRes(args.elementAt(0).toString());
77         }});
78
79         super.put("println", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
80             if (args.length() != 1) return null;
81             if (Log.on) Log.logJS(this, (args.elementAt(0) == null ? "**null**" : args.elementAt(0).toString()));
82             return null;
83         }});
84
85         super.put("date", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
86             Log.log(XWT.class, "date not implemented");
87             //throw new Error("not implemented");
88             return null;
89         }});
90
91         super.put("regexp", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
92             return new Regexp(args);
93         }});
94
95         super.put("listfonts", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
96             Object[] fonts = Platform.listFonts();
97             JS.Array ret = new JS.Array();
98             for(int i=0; i<fonts.length; i++) ret.addElement(fonts[i]);
99             return ret;
100         }});
101
102         super.put("xmlrpc", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
103             if (args.length() != 1 || args.elementAt(0) == null) return null;
104             return new XMLRPC(args.elementAt(0).toString(), "");
105         }});
106
107         super.put("soap", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
108             if (args.length() == 1 && args.elementAt(0) != null) return new SOAP(args.elementAt(0).toString(), "", null, null);
109             else if (args.length() == 2 && args.elementAt(0) != null && args.elementAt(1) != null)
110                 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), null);
111             else if (args.length() == 3 && args.elementAt(0) != null && args.elementAt(1) != null && args.elementAt(2) != null)
112                 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), args.elementAt(2).toString());
113             else return null;
114         }});
115
116         super.put("textwidth", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
117             if (args.length() < 1 || args.length() > 2) return null;
118             if (args.elementAt(0) == null || (args.length() == 2 && args.elementAt(1) == null)) return null;
119             String font = args.length() == 1 ? Platform.getDefaultFont() : args.elementAt(0).toString();
120             String text = args.length() == 1 ? args.elementAt(0).toString() : args.elementAt(1).toString();
121             return new Integer(Platform.stringWidth(font, text));
122         }});
123
124         super.put("textheight", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
125             if (args.length() > 1) return null;
126             if (args.length() == 1 && args.elementAt(0) == null) return null;
127             String font = args.length() == 0 || args.elementAt(0) == null ? Platform.getDefaultFont() : args.elementAt(0).toString();
128             return new Integer(Platform.getMaxAscent(font) + Platform.getMaxDescent(font));
129         }});
130         
131         super.put("newBox", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
132             if (args.length() > 0) Log.log(XWT.class, "DEPRECATED: xwt.newBox() with multiple arguments is deprecated; use xwt.newBox().apply()");
133             JS.Callable callback = null;
134             for(int i=1; i<args.length(); i++)
135                 if (args.elementAt(i) instanceof JS.Callable && callback == null)
136                     callback = (JS.Callable)args.elementAt(i);
137             Box ret = new Box();
138             if (!(args.length() == 0 || args.elementAt(0) == null))
139                 Template.getTemplate(args.elementAt(0).toString(),
140                                      Template.defaultImportList).apply(ret, null, null, callback, 0, 1, resourceRoot);
141             for(int i=1; i<args.length(); i++)
142                 if (args.elementAt(i) instanceof Box)
143                     ret.put(ret.numChildren(), (Box)args.elementAt(i));
144             for(int i=1; i<args.length(); i++)
145                 if (args.elementAt(i) instanceof JS && !(args.elementAt(i) instanceof Box) && !(args.elementAt(i) instanceof JS.Callable)) {
146                     JS s = (JS)args.elementAt(i);
147                     Object[] keys = s.keys();
148                     for(int j=0; j<keys.length; j++) ret.put(keys[j].toString(), s.get(keys[j].toString()));
149                 }
150             return ret;
151         }});
152
153         super.put("sleep", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
154             if (args != null && (args.length() != 1 || args.elementAt(0) == null)) return null;
155             int i = args == null ? 0 : Box.stoi(args.elementAt(0).toString());
156             sleep(i);
157             return null;
158         }});
159
160         /* FIXME
161         super.put("openFile", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
162             if (args.length() != 1) return null;
163             String file = Platform.fileDialog(args.elementAt(0).toString(), false);
164             return file == null ? null : new ByteStream(file);
165         }});
166
167         super.put("saveFile", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
168             if (args.length() != 2) return null;
169             if (!(args.elementAt(1) instanceof ByteStream)) return null;
170             String file = args.elementAt(0).toString();
171             if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
172                 file = Platform.fileDialog(file, true);
173                 if (file == null) return null;
174                 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
175             }
176             try {
177                 ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
178                 return null;
179             } catch (IOException e) {
180                 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
181                 if (Log.on) Log.log(ByteStream.class, e);
182                 throw new JS.Exn("error while writing a ByteStream to a file");
183             }
184         }});
185
186         super.put("saveFileAs", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
187             if (args.length() != 2) return null;
188             if (!(args.elementAt(1) instanceof ByteStream)) return null;
189             String file = args.elementAt(0).toString();
190             file = Platform.fileDialog(file, true);
191             if (file == null) return null;
192             safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
193             try {
194                 ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
195                 return null;
196             } catch (IOException e) {
197                 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
198                 if (Log.on) Log.log(ByteStream.class, e);
199                 throw new JS.Exn("error while writing a ByteStream to a file");
200             }
201         }});
202
203         super.put("utfEncode", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
204             if (args == null || args.length() != 1) return null;
205             return new ByteStream(args.elementAt(0).toString().getBytes());
206         }});
207
208         super.put("parseHTML", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
209                 if (args == null || args.length() != 1 || args.elementAt(0) == null) return null;
210                 try {
211                     if (args.elementAt(0) instanceof ByteStream) {
212                         return HTML.parseReader(new InputStreamReader(((ByteStream)args.elementAt(0)).getInputStream()));
213                     } else {
214                         return HTML.parseReader(new StringReader(args.elementAt(0).toString()));
215                     }
216                 } catch (IOException e) {
217                     if (Log.on) Log.log(HTML.class, "IO Exception while parsing HTML");
218                     if (Log.on) Log.log(HTML.class, e);
219                     throw new JS.Exn("error while parsing HTML");
220                 }
221             }
222         });
223         */    
224     super.put("recursivePrintObject", new JS.Callable() { public Object call(JS.Array args) {
225         if (args.length() != 1) return null;
226         recurse("", "", args.elementAt(0));
227         return null;
228     }});
229
230     super.put("loadArchive", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
231         if (!ThreadMessage.suspendThread()) return null;
232         try {
233             if (args == null || args.length() < 1 || args.elementAt(0) == null) return null;
234             URL u = new URL(args.elementAt(0).toString());
235             
236             JS.Callable callback = null;
237             if (args.length() == 2 && args.elementAt(1) != null && args.elementAt(1) instanceof JS.Callable)
238                 callback = (JS.Callable)args.elementAt(1);
239             
240             if (!u.getFile().endsWith(".xwar")) {
241                 if (Log.on) Log.log(this, "Error: archive names must end with .xwar: " + u.getFile());
242                 throw new JS.Exn("Error: archive names must end with .xwar: " + u.getFile());
243             }
244             
245             if (u.getProtocol().equals("http")) {
246                 HTTP http = new HTTP(u.toString());
247                 if (Main.originAddr == null) {
248                     try {
249                         Main.originHost = u.getHost();
250                         Main.originAddr = InetAddress.getByName(Main.originHost);
251                     } catch (UnknownHostException e) {
252                         if (Log.on) Log.log(this, "couldn't resolve " + u.getHost() + "; proceeding without permissions");
253                         Main.originAddr = InetAddress.getByName("0.0.0.0");
254                     }
255                 } else {
256                     Main.originAddr = InetAddress.getByName("0.0.0.0");
257                 }
258                 HTTP.HTTPInputStream in = http.GET();
259                 Resources.loadArchive(in, in.getContentLength(), callback);
260                 
261             } else if (u.getProtocol().equals("file")) {
262                 if (Main.originAddr != null) {
263                     if (Log.on) Log.log(this, "scripts downloaded from the network may not load xwars from the local filesystem");
264                     throw new JS.Exn("scripts downloaded from the network may not load xwars from the local filesystem");
265                 }
266                 Resources.loadArchive(new FileInputStream(u.getFile()), (int)new File(u.getFile()).length(), callback);
267                 
268             } else {
269                 if (Log.on) Log.log(this, "unknown protocol \"" + u.getProtocol() + "\"");
270                 throw new JS.Exn("unknown protocol \"" + u.getProtocol() + "\"");
271             }
272             
273         } catch (MalformedURLException me) {
274             if (Log.on) Log.log(this, "Malformed URL: " + args.elementAt(0));
275             if (Log.on) Log.log(this, me);
276             throw new JS.Exn(me.toString());
277             
278         } catch (IOException ioe) {
279             if (Log.on) Log.log(this, "IOException while loading archive:");
280             if (Log.on) Log.log(this, ioe);
281             throw new JS.Exn(ioe.toString());
282             
283         } finally {
284             ThreadMessage.resumeThread();
285             
286         }
287         return null;
288     }});
289
290     super.put("prefetchImage", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn {
291         if (args == null || args.length() < 1 || args.elementAt(0) == null) return null;
292         ImageDecoder.getImageDecoder(args.elementAt(0).toString(),
293                                      args.length() > 1 && args.elementAt(1) instanceof JS.Callable ? (JS.Callable)args.elementAt(1) : null);
294         return null;
295     }});
296     }
297
298     private static void recurse(String indent, String name, Object o) {
299         if (!name.equals("")) name += " : ";
300
301         if (o == null) {
302             Log.logJS(indent + name + "<null>");
303
304         } else if (o instanceof JS.Array) {
305             Log.logJS(indent + name + "<array>");
306             JS.Array na = (JS.Array)o;
307             for(int i=0; i<na.length(); i++)
308                 recurse(indent + "  ", i + "", na.elementAt(i));
309
310         } else if (o instanceof JS) {
311             Log.logJS(indent + name + "<object>");
312             JS s = (JS)o;
313             Object[] keys = s.keys();
314             for(int i=0; i<keys.length; i++)
315                 recurse(indent + "  ", keys[i].toString(),
316                         (keys[i] instanceof Integer) ?
317                         s.get(((Integer)keys[i])) : s.get(keys[i].toString()));
318
319         } else {
320             Log.logJS(indent + name + o);
321
322         }
323     }
324
325
326     public static void sleep(int i) {
327         java.lang.Thread thread = java.lang.Thread.currentThread();
328         if (!(thread instanceof ThreadMessage)) {
329             if (Log.on) Log.log(XWT.class, "cannot sleep() or yield() in the foreground thread");
330         } else {
331             ThreadMessage mythread = (ThreadMessage)thread;
332             mythread.done.release();
333             if (i > 0) try { java.lang.Thread.sleep(i); } catch (Exception e) { }
334             MessageQueue.add(mythread);
335             mythread.go.block();
336         }
337     }
338     
339     private static class XWTMath extends JS.Obj {
340         public XWTMath() {
341             JS gs = new JS.GlobalScope();
342             put("isNaN",gs.get("isNaN"));
343             put("isFinite",gs.get("isFinite"));
344             put("NaN",gs.get("NaN"));
345             put("Infinity",gs.get("Infinity"));
346             setSeal(true);
347         }
348         public Object get(Object key) {
349             Object ret = super.get(key);
350             if(ret == null) ret = JS.Math.get(key);
351             return ret;
352         }
353     }
354     private static class XWTString extends JS.Obj {
355         public XWTString() {
356             JS gs = new JS.GlobalScope();
357             put("parseInt",gs.get("parseInt"));
358             put("parseFloat",gs.get("parseFloat"));
359             put("decodeURI",gs.get("decodeURI"));
360             put("decodeURIComponent",gs.get("decodeURIComponent"));
361             put("encodeURI",gs.get("encodeURI"));
362             put("encodeURIComponent",gs.get("encodeURIComponent"));
363             put("escape",gs.get("escape"));
364             put("unescape",gs.get("unescape"));
365             put("fromCharCode",gs.get("stringFromCharCode"));
366             setSeal(true);
367         }
368     }
369 }