2003/10/28 10:10:17
[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.xwt.translators.*;
11 import org.bouncycastle.util.encoders.Base64;
12
13 /** Singleton class that provides all functionality in the xwt.* namespace */
14 public final class XWT extends JS.Obj {
15
16     public final Res rr;
17     public XWT(Res rr) { this.rr = rr; }
18
19     private final JS xwtMath = new XWTMath();
20     private final JS xwtString = new XWTString();
21
22     /** each key is a string representing a filename which the user has already given XWT permission to write to */
23     private static Hashtable safeFiles = new Hashtable();
24
25     public Object get(Object name) {
26         if (name.equals("alt")) return Surface.alt ? Boolean.TRUE : Boolean.FALSE;
27         else if (name.equals("rr")) return rr;
28         else if (name.equals("box")) return new Box();
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("clipboard")) return Platform.getClipBoard();
32         else if (name.equals("origin")) return Main.origin;
33         else if (name.equals("maxdim")) return new Integer(Short.MAX_VALUE);
34         else if (name.equals("altKeyName")) return Platform.altKeyName();
35         else if (name.equals("screenWidth")) return new Integer(Platform.getScreenWidth());
36         else if (name.equals("screenHeight")) return new Integer(Platform.getScreenHeight());
37         else if (name.equals("fileSeparator")) return File.separator;
38         else if (name.equals("homeDir")) return System.getProperty("user.home");
39         else if (name.equals("tempDir")) return System.getProperty("java.io.tempdir");
40         else if (name.equals("math")) return xwtMath;
41         else if (name.equals("string")) return xwtString;
42         else if (name.equals("parseInt")) return xwtString.get("parseInt");
43         else if (name.equals("parseFloat")) return xwtString.get("parseFloat");
44         else if (name.equals("button")) {
45             if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
46             else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(1);
47             else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(1);
48             else return new Integer(0);
49         }
50         else return rr.get(name);
51     }
52
53     public void put(Object name, Object value) {
54         if (name.equals("thread") && value != null && value instanceof JS.Callable) ThreadMessage.newthread((JS.Callable)value);
55         else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
56         else if (name.equals("frame")) Platform.createSurface((Box)value, true, true);
57         else if (name.equals("window")) Platform.createSurface((Box)value, false, true);
58         else if (name.equals("proxyAuthorization")) {
59             HTTP.Proxy.Authorization.authorization = value.toString();
60             HTTP.Proxy.Authorization.waitingForUser.release();
61         } else super.put(name, value);
62     }
63
64     public Object callMethod(Object method, JS.Array args, boolean checkOnly) {
65
66         if (method.equals("newBrowserWindow")) {
67             if (checkOnly) return Boolean.TRUE;
68             if (args.length() != 1 || args.elementAt(0) == null) return null;
69             Platform.newBrowserWindow(args.elementAt(0).toString());
70             return null;
71
72         } else if (method.equals("clone")) {
73             if (checkOnly) return Boolean.TRUE;
74             return new XWT((Res)args.elementAt(0));
75
76         } else if (method.equals("graft")) {
77             if (checkOnly) return Boolean.TRUE;
78             if (args.elementAt(0) instanceof Box) throw new JS.Exn("can't graft onto Boxes (yet)");
79             if (args.elementAt(0) instanceof Number) throw new JS.Exn("can't graft onto Numbers (yet)");
80             if (args.elementAt(0) instanceof String) throw new JS.Exn("can't graft onto Strings (yet)");
81             if (args.elementAt(0) instanceof Res)
82                 return new Res.Graft((Res)args.elementAt(0), args.elementAt(1), args.elementAt(2));
83             return new JS.Graft((JS)args.elementAt(0), args.elementAt(1), args.elementAt(2));
84
85         } else if (method.equals("unzip")) {
86             if (checkOnly) return Boolean.TRUE;
87             return new Res.Zip((Res)args.elementAt(0));
88
89         } else if (method.equals("watchProgress")) {
90             if (checkOnly) return Boolean.TRUE;
91             return new Res.ProgressWatcher((Res)args.elementAt(0), (JS.Callable)args.elementAt(1));
92
93         } else if (method.equals("yield")) {
94             if (checkOnly) return Boolean.TRUE;
95             sleep(0);
96             return null;
97
98         } else if (method.equals("load")) {
99             if (checkOnly) return Boolean.TRUE;
100             return Res.stringToRes(args.elementAt(0).toString());
101
102         } else if (method.equals("println")) {
103             if (checkOnly) return Boolean.TRUE;
104             if (args.length() != 1) return null;
105             if (Log.on) Log.logJS(this, (args.elementAt(0) == null ? "**null**" : args.elementAt(0).toString()));
106             return null;
107
108         } else if (method.equals("date")) {
109             if (checkOnly) return Boolean.TRUE;
110             Log.log(XWT.class, "date not implemented");
111             return null;
112
113         } else if (method.equals("regexp")) {
114             if (checkOnly) return Boolean.TRUE;
115             return new Regexp(args);
116
117         } else if (method.equals("apply")) {
118             if (checkOnly) return Boolean.TRUE;
119             Box b = (Box)args.elementAt(0);
120             Template.getTemplate((Res)args.elementAt(1)).apply(b, null, this);
121             return b;
122
123         } else if (method.equals("xmlrpc")) {
124             if (checkOnly) return Boolean.TRUE;
125             if (args.length() != 1 || args.elementAt(0) == null) return null;
126             return new XMLRPC(args.elementAt(0).toString(), "");
127
128         } else if (method.equals("soap")) {
129             if (checkOnly) return Boolean.TRUE;
130             if (args.length() == 1 && args.elementAt(0) != null) return new SOAP(args.elementAt(0).toString(), "", null, null);
131             else if (args.length() == 2 && args.elementAt(0) != null && args.elementAt(1) != null)
132                 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), null);
133             else if (args.length() == 3 && args.elementAt(0) != null && args.elementAt(1) != null && args.elementAt(2) != null)
134                 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), args.elementAt(2).toString());
135             else return null;
136
137         } else if (method.equals("sleep")) {
138             if (checkOnly) return Boolean.TRUE;
139             if (args != null && (args.length() != 1 || args.elementAt(0) == null)) return null;
140             int i = args == null ? 0 : Box.stoi(args.elementAt(0).toString());
141             sleep(i);
142             return null;
143
144         } else if (method.equals("openFile")) {
145             if (checkOnly) return Boolean.TRUE;
146             if (args.length() != 1) return null;
147             String file = Platform.fileDialog(args.elementAt(0).toString(), false);
148             return file == null ? null : new Res.File(file);
149
150         } else if (method.equals("saveFile") || method.equals("saveFileAs")) {
151             if (checkOnly) return Boolean.TRUE;
152             if (args.length() != 2) return null;
153             if (!(args.elementAt(1) instanceof Res)) return null;
154             String file = args.elementAt(0).toString();
155             if (method.equals("saveFileAs") ||
156                 safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
157                 file = Platform.fileDialog(file, true);
158                 // FIXME: throw exception here
159                 if (file == null) return null;
160                 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
161             }
162             try {
163                 InputStream is = ((Res)args.elementAt(1)).getInputStream();
164                 FileOutputStream out = new FileOutputStream(file);
165                 byte[] buffer = new byte[1024 * 16];
166                 while(true) {
167                     int numread = is.read(buffer, 0, buffer.length);
168                     if (numread == -1) break;
169                     out.write(buffer, 0, numread);
170                 }
171                 is.close();
172                 out.close();
173                 return null;
174             } catch (IOException e) {
175                 if (Log.on) Log.log(XWT.class, "IO Exception while writing a ByteStream to a file");
176                 if (Log.on) Log.log(XWT.class, e);
177                 throw new JS.Exn("error while writing a Resource to a file");
178             }
179
180         } else if (method.equals("parseHTML")) {
181             if (checkOnly) return Boolean.TRUE;
182                 if (args == null || args.length() != 1 || args.elementAt(0) == null) return null;
183                 try {
184                     if (args.elementAt(0) instanceof Res) {
185                         return HTML.parseReader(new InputStreamReader(((Res)args.elementAt(0)).getInputStream()));
186                     } else {
187                         return HTML.parseReader(new StringReader(args.elementAt(0).toString()));
188                     }
189                 } catch (IOException e) {
190                     if (Log.on) Log.log(HTML.class, "IO Exception while parsing HTML");
191                     if (Log.on) Log.log(HTML.class, e);
192                     throw new JS.Exn("error while parsing HTML");
193                 }
194
195         } else if (method.equals("recursivePrintObject")) {
196             if (checkOnly) return Boolean.TRUE;
197             if (args.length() != 1) return null;
198             recurse("", "", args.elementAt(0));
199             return null;
200         }
201             
202         if (checkOnly) return Boolean.FALSE;
203         return null;
204     }
205
206     private static void recurse(String indent, String name, Object o) {
207         if (!name.equals("")) name += " : ";
208
209         if (o == null) {
210             Log.logJS(indent + name + "<null>");
211
212         } else if (o instanceof JS.Array) {
213             Log.logJS(indent + name + "<array>");
214             JS.Array na = (JS.Array)o;
215             for(int i=0; i<na.length(); i++)
216                 recurse(indent + "  ", i + "", na.elementAt(i));
217
218         } else if (o instanceof JS) {
219             Log.logJS(indent + name + "<object>");
220             JS s = (JS)o;
221             Object[] keys = s.keys();
222             for(int i=0; i<keys.length; i++)
223                 if (keys[i] != null)
224                     recurse(indent + "  ", keys[i].toString(),
225                             (keys[i] instanceof Integer) ?
226                             s.get(((Integer)keys[i])) : s.get(keys[i].toString()));
227
228         } else {
229             Log.logJS(indent + name + o);
230
231         }
232     }
233
234     public static void sleep(int i) {
235         java.lang.Thread thread = java.lang.Thread.currentThread();
236         if (!(thread instanceof ThreadMessage)) {
237             if (Log.on) Log.log(XWT.class, "cannot sleep() or yield() in the foreground thread");
238         } else {
239             ThreadMessage mythread = (ThreadMessage)thread;
240             mythread.done.release();
241             if (i > 0) try { java.lang.Thread.sleep(i); } catch (Exception e) { }
242             Message.Q.add(mythread);
243             mythread.go.block();
244         }
245     }
246     
247     private static class XWTMath extends JS.Obj {
248         public XWTMath() {
249             JS gs = new JS.GlobalScope();
250             put("isNaN",gs.get("isNaN"));
251             put("isFinite",gs.get("isFinite"));
252             put("NaN",gs.get("NaN"));
253             put("Infinity",gs.get("Infinity"));
254             setSeal(true);
255         }
256         public Object get(Object key) {
257             Object ret = super.get(key);
258             if(ret == null) ret = JS.Math.get(key);
259             return ret;
260         }
261     }
262     private static class XWTString extends JS.Obj {
263         public XWTString() {
264             JS gs = new JS.GlobalScope();
265             put("parseInt",gs.get("parseInt"));
266             put("parseFloat",gs.get("parseFloat"));
267             put("decodeURI",gs.get("decodeURI"));
268             put("decodeURIComponent",gs.get("decodeURIComponent"));
269             put("encodeURI",gs.get("encodeURI"));
270             put("encodeURIComponent",gs.get("encodeURIComponent"));
271             put("escape",gs.get("escape"));
272             put("unescape",gs.get("unescape"));
273             put("fromCharCode",gs.get("stringFromCharCode"));
274             setSeal(true);
275         }
276     }
277 }