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