2003/10/15 21:43:01
[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 super.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("watchProgress")) {
84             if (checkOnly) return Boolean.TRUE;
85             return new Res.ProgressWatcher((Res)args.elementAt(0), (JS.Callable)args.elementAt(1));
86
87         } else if (method.equals("yield")) {
88             if (checkOnly) return Boolean.TRUE;
89             sleep(0);
90             return null;
91
92         } else if (method.equals("load")) {
93             if (checkOnly) return Boolean.TRUE;
94             return Res.stringToRes(args.elementAt(0).toString());
95
96         } else if (method.equals("println")) {
97             if (checkOnly) return Boolean.TRUE;
98             if (args.length() != 1) return null;
99             if (Log.on) Log.logJS(this, (args.elementAt(0) == null ? "**null**" : args.elementAt(0).toString()));
100             return null;
101
102         } else if (method.equals("date")) {
103             if (checkOnly) return Boolean.TRUE;
104             Log.log(XWT.class, "date not implemented");
105             return null;
106
107         } else if (method.equals("regexp")) {
108             if (checkOnly) return Boolean.TRUE;
109             return new Regexp(args);
110
111         } else if (method.equals("xmlrpc")) {
112             if (checkOnly) return Boolean.TRUE;
113             if (args.length() != 1 || args.elementAt(0) == null) return null;
114             return new XMLRPC(args.elementAt(0).toString(), "");
115
116         } else if (method.equals("soap")) {
117             if (checkOnly) return Boolean.TRUE;
118             if (args.length() == 1 && args.elementAt(0) != null) return new SOAP(args.elementAt(0).toString(), "", null, null);
119             else if (args.length() == 2 && args.elementAt(0) != null && args.elementAt(1) != null)
120                 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), null);
121             else if (args.length() == 3 && args.elementAt(0) != null && args.elementAt(1) != null && args.elementAt(2) != null)
122                 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), args.elementAt(2).toString());
123             else return null;
124
125         } else if (method.equals("sleep")) {
126             if (checkOnly) return Boolean.TRUE;
127             if (args != null && (args.length() != 1 || args.elementAt(0) == null)) return null;
128             int i = args == null ? 0 : Box.stoi(args.elementAt(0).toString());
129             sleep(i);
130             return null;
131
132         } else if (method.equals("openFile")) {
133             if (checkOnly) return Boolean.TRUE;
134             if (args.length() != 1) return null;
135             String file = Platform.fileDialog(args.elementAt(0).toString(), false);
136             return file == null ? null : Res.stringToRes("file:" + file);
137
138         } else if (method.equals("saveFile")) {
139             if (checkOnly) return Boolean.TRUE;
140             // FIXME
141             /*
142             if (args.length() != 2) return null;
143             if (!(args.elementAt(1) instanceof ByteStream)) return null;
144             String file = args.elementAt(0).toString();
145             if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
146                 file = Platform.fileDialog(file, true);
147                 if (file == null) return null;
148                 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
149             }
150             try {
151                 ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
152                 return null;
153             } catch (IOException e) {
154                 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
155                 if (Log.on) Log.log(ByteStream.class, e);
156                 throw new JS.Exn("error while writing a ByteStream to a file");
157             }
158             */
159
160         } else if (method.equals("saveFileAs")) {
161             // FIXME
162             /*
163             if (checkOnly) return Boolean.TRUE;
164             if (args.length() != 2) return null;
165             if (!(args.elementAt(1) instanceof ByteStream)) return null;
166             String file = args.elementAt(0).toString();
167             file = Platform.fileDialog(file, true);
168             if (file == null) return null;
169             safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
170             try {
171                 ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
172                 return null;
173             } catch (IOException e) {
174                 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
175                 if (Log.on) Log.log(ByteStream.class, e);
176                 throw new JS.Exn("error while writing a ByteStream to a file");
177             }
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 }