2003/09/24 07:33:32
[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 Res resourceRoot = null;
17
18     public static final XWT singleton = new XWT();
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("control")) return Surface.control ? Boolean.TRUE : Boolean.FALSE;
28         else if (name.equals("shift")) return Surface.shift ? Boolean.TRUE : Boolean.FALSE;
29         else if (name.equals("clipboard")) return Platform.getClipBoard();
30         else if (name.equals("origin")) return Main.origin;
31         else if (name.equals("maxdim")) return new Integer(Short.MAX_VALUE);
32         else if (name.equals("altKeyName")) return Platform.altKeyName();
33         else if (name.equals("screenWidth")) return new Integer(Platform.getScreenWidth());
34         else if (name.equals("screenHeight")) return new Integer(Platform.getScreenHeight());
35         else if (name.equals("fileSeparator")) return File.separator;
36         else if (name.equals("homeDir")) return System.getProperty("user.home");
37         else if (name.equals("tempDir")) return System.getProperty("java.io.tempdir");
38         else if (name.equals("math")) return xwtMath;
39         else if (name.equals("string")) return xwtString;
40         else if (name.equals("parseInt")) return xwtString.get("parseInt");
41         else if (name.equals("parseFloat")) return xwtString.get("parseFloat");
42         else if (name.equals("button")) {
43             if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
44             else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(1);
45             else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(1);
46             else return new Integer(0);
47         }
48         else return super.get(name);
49     }
50
51     public void put(Object name, Object value) {
52         if (name.equals("thread") && value != null && value instanceof JS.Callable) ThreadMessage.newthread((JS.Callable)value);
53         else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
54         else if (name.equals("proxyAuthorization")) {
55             // FIXME: undocumented, possibly insecure
56             HTTP.Proxy.Authorization.authorization = value.toString();
57             HTTP.Proxy.Authorization.waitingForUser.release();
58         } else super.put(name, value);
59     }
60
61     public Object callMethod(Object method, JS.Array args, boolean checkOnly) {
62
63         if (method.equals("newBrowserWindow")) {
64             if (checkOnly) return Boolean.TRUE;
65             if (args.length() != 1 || args.elementAt(0) == null) return null;
66             Platform.newBrowserWindow(args.elementAt(0).toString());
67             return null;
68
69         } else if (method.equals("yield")) {
70             if (checkOnly) return Boolean.TRUE;
71             sleep(0);
72             return null;
73
74         } else if (method.equals("load")) {
75             if (checkOnly) return Boolean.TRUE;
76             return Res.stringToRes(args.elementAt(0).toString());
77
78         } else if (method.equals("println")) {
79             if (checkOnly) return Boolean.TRUE;
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         } else if (method.equals("date")) {
85             if (checkOnly) return Boolean.TRUE;
86             Log.log(XWT.class, "date not implemented");
87             return null;
88
89         } else if (method.equals("regexp")) {
90             if (checkOnly) return Boolean.TRUE;
91             return new Regexp(args);
92
93         } else if (method.equals("xmlrpc")) {
94             if (checkOnly) return Boolean.TRUE;
95             if (args.length() != 1 || args.elementAt(0) == null) return null;
96             return new XMLRPC(args.elementAt(0).toString(), "");
97
98         } else if (method.equals("soap")) {
99             if (checkOnly) return Boolean.TRUE;
100             if (args.length() == 1 && args.elementAt(0) != null) return new SOAP(args.elementAt(0).toString(), "", null, null);
101             else if (args.length() == 2 && args.elementAt(0) != null && args.elementAt(1) != null)
102                 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), null);
103             else if (args.length() == 3 && args.elementAt(0) != null && args.elementAt(1) != null && args.elementAt(2) != null)
104                 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), args.elementAt(2).toString());
105             else return null;
106
107         } else if (method.equals("newBox")) {
108             if (checkOnly) return Boolean.TRUE;
109             return new Box();
110
111         } else if (method.equals("sleep")) {
112             if (checkOnly) return Boolean.TRUE;
113             if (args != null && (args.length() != 1 || args.elementAt(0) == null)) return null;
114             int i = args == null ? 0 : Box.stoi(args.elementAt(0).toString());
115             sleep(i);
116             return null;
117
118         } else if (method.equals("openFile")) {
119             if (checkOnly) return Boolean.TRUE;
120             if (args.length() != 1) return null;
121             String file = Platform.fileDialog(args.elementAt(0).toString(), false);
122             return file == null ? null : new Res.stringToResource("file:" + file);
123
124         } else if (method.equals("saveFile")) {
125             if (checkOnly) return Boolean.TRUE;
126             // FIXME
127             /*
128             if (args.length() != 2) return null;
129             if (!(args.elementAt(1) instanceof ByteStream)) return null;
130             String file = args.elementAt(0).toString();
131             if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
132                 file = Platform.fileDialog(file, true);
133                 if (file == null) return null;
134                 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
135             }
136             try {
137                 ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
138                 return null;
139             } catch (IOException e) {
140                 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
141                 if (Log.on) Log.log(ByteStream.class, e);
142                 throw new JS.Exn("error while writing a ByteStream to a file");
143             }
144             */
145
146         } else if (method.equals("saveFileAs")) {
147             // FIXME
148             /*
149             if (checkOnly) return Boolean.TRUE;
150             if (args.length() != 2) return null;
151             if (!(args.elementAt(1) instanceof ByteStream)) return null;
152             String file = args.elementAt(0).toString();
153             file = Platform.fileDialog(file, true);
154             if (file == null) return null;
155             safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
156             try {
157                 ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
158                 return null;
159             } catch (IOException e) {
160                 if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
161                 if (Log.on) Log.log(ByteStream.class, e);
162                 throw new JS.Exn("error while writing a ByteStream to a file");
163             }
164             */
165
166         } else if (method.equals("parseHTML")) {
167             if (checkOnly) return Boolean.TRUE;
168                 if (args == null || args.length() != 1 || args.elementAt(0) == null) return null;
169                 try {
170                     if (args.elementAt(0) instanceof Res) {
171                         return HTML.parseReader(new InputStreamReader(((Res)args.elementAt(0)).getInputStream()));
172                     } else {
173                         return HTML.parseReader(new StringReader(args.elementAt(0).toString()));
174                     }
175                 } catch (IOException e) {
176                     if (Log.on) Log.log(HTML.class, "IO Exception while parsing HTML");
177                     if (Log.on) Log.log(HTML.class, e);
178                     throw new JS.Exn("error while parsing HTML");
179                 }
180
181         } else if (method.equals("recursivePrintObject")) {
182             if (checkOnly) return Boolean.TRUE;
183             if (args.length() != 1) return null;
184             recurse("", "", args.elementAt(0));
185             return null;
186         }
187             
188         if (checkOnly) return Boolean.FALSE;
189         return null;
190     }
191
192     private static void recurse(String indent, String name, Object o) {
193         if (!name.equals("")) name += " : ";
194
195         if (o == null) {
196             Log.logJS(indent + name + "<null>");
197
198         } else if (o instanceof JS.Array) {
199             Log.logJS(indent + name + "<array>");
200             JS.Array na = (JS.Array)o;
201             for(int i=0; i<na.length(); i++)
202                 recurse(indent + "  ", i + "", na.elementAt(i));
203
204         } else if (o instanceof JS) {
205             Log.logJS(indent + name + "<object>");
206             JS s = (JS)o;
207             Object[] keys = s.keys();
208             for(int i=0; i<keys.length; i++)
209                 if (keys[i] != null)
210                     recurse(indent + "  ", keys[i].toString(),
211                             (keys[i] instanceof Integer) ?
212                             s.get(((Integer)keys[i])) : s.get(keys[i].toString()));
213
214         } else {
215             Log.logJS(indent + name + o);
216
217         }
218     }
219
220     public static void sleep(int i) {
221         java.lang.Thread thread = java.lang.Thread.currentThread();
222         if (!(thread instanceof ThreadMessage)) {
223             if (Log.on) Log.log(XWT.class, "cannot sleep() or yield() in the foreground thread");
224         } else {
225             ThreadMessage mythread = (ThreadMessage)thread;
226             mythread.done.release();
227             if (i > 0) try { java.lang.Thread.sleep(i); } catch (Exception e) { }
228             Message.Q.add(mythread);
229             mythread.go.block();
230         }
231     }
232     
233     private static class XWTMath extends JS.Obj {
234         public XWTMath() {
235             JS gs = new JS.GlobalScope();
236             put("isNaN",gs.get("isNaN"));
237             put("isFinite",gs.get("isFinite"));
238             put("NaN",gs.get("NaN"));
239             put("Infinity",gs.get("Infinity"));
240             setSeal(true);
241         }
242         public Object get(Object key) {
243             Object ret = super.get(key);
244             if(ret == null) ret = JS.Math.get(key);
245             return ret;
246         }
247     }
248     private static class XWTString extends JS.Obj {
249         public XWTString() {
250             JS gs = new JS.GlobalScope();
251             put("parseInt",gs.get("parseInt"));
252             put("parseFloat",gs.get("parseFloat"));
253             put("decodeURI",gs.get("decodeURI"));
254             put("decodeURIComponent",gs.get("decodeURIComponent"));
255             put("encodeURI",gs.get("encodeURI"));
256             put("encodeURIComponent",gs.get("encodeURIComponent"));
257             put("escape",gs.get("escape"));
258             put("unescape",gs.get("unescape"));
259             put("fromCharCode",gs.get("stringFromCharCode"));
260             setSeal(true);
261         }
262     }
263 }