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