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