2003/12/13 00:44:41
[org.ibex.core.git] / src / org / xwt / XWT.java
1 // FIXME
2 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
3 package org.xwt;
4
5 import java.io.*;
6 import java.net.*;
7 import java.text.*;
8 import java.util.*;
9 import org.xwt.js.*;
10 import org.xwt.util.*;
11 import org.xwt.translators.*;
12 import org.bouncycastle.util.encoders.Base64;
13
14 /** Singleton class that provides all functionality in the xwt.* namespace */
15 public final class XWT extends JS {
16
17     public final Res rr;
18     public XWT(Res rr) { this.rr = rr; }
19
20     /** lets us put multi-level get/put/call keys all in the same method */
21     private class Sub extends JS {
22         String key;
23         Sub(String key) { this.key = key; }
24         public String toString() { return "XWTSUB " + key; }
25         public void put(Object key, Object val) { XWT.this.put(this.key + "." + key, val); }
26         public Object get(Object key) { return XWT.this.get(this.key + "." + key); }
27         public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
28             return XWT.this.callMethod(this.key, a0, a1, a2, rest, nargs);
29         }
30         public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
31             return XWT.this.callMethod(this.key + "." + method, a0, a1, a2, rest, nargs);
32         }
33     }
34
35     public Object get(Object name) {
36         //#switch(name)
37         case "math": return xwtMath;
38         case "string": return xwtString;
39         case "date": return METHOD;
40         case "origin": return Main.origin;
41         case "box": return new Box();
42         case "log": return new Sub("log");
43         case "ui": return new Sub("ui");
44         case "thread": return new Sub("thread");
45         case "font": return new Sub("font");
46         case "font.sansserif": return Main.builtin.get("fonts/vera/Vera.ttf");
47         case "font.monospace": return Main.builtin.get("fonts/vera/VeraMono.ttf");
48         case "font.serif": return Main.builtin.get("fonts/vera/VeraSe.ttf");
49         case "ui.key": return new Sub("ui.key");
50         case "ui.key.alt": return Surface.alt ? Boolean.TRUE : Boolean.FALSE;
51         case "ui.key.control": return Surface.control ? Boolean.TRUE : Boolean.FALSE;
52         case "ui.key.shift": return Surface.shift ? Boolean.TRUE : Boolean.FALSE;
53         case "ui.clipboard": return Platform.getClipBoard();
54         case "ui.maxdim": return new Integer(Short.MAX_VALUE);
55         case "ui.key.name": return new Sub("ui.key.name");
56         case "ui.key.name.alt": return Platform.altKeyName();
57         case "ui.screen": return new Sub("ui.screen");
58         case "ui.screen.width": return new Integer(Platform.getScreenWidth());
59         case "ui.screen.height": return new Integer(Platform.getScreenHeight());
60         case "fs.home": return System.getProperty("user.home");
61         case "fs.temp": return System.getProperty("java.io.tempdir");
62         case "ui.mouse": return new Sub("ui.mouse");
63         case "res": return new Sub("res");
64         case "ui.mouse.button":
65             if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
66             else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(2);
67             else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(3);
68             else return ZERO;
69         case "undocumented": return new Sub("undocumented");
70         case "undocumented.internal": return new Sub("undocumented.internal");
71         case "thread.yield": return METHOD;
72         case "thread.sleep": return METHOD;
73         case "res.watch": return METHOD;
74         case "res.unzip": return METHOD;
75         case "res.uncab": return METHOD;
76         case "res.cache": return METHOD;
77         case "res.url": return METHOD;
78         case "soap": return METHOD;
79         case "apply": return METHOD;
80         case "graft": return METHOD;
81         case "ui.browser": return METHOD;
82         case "clone": return METHOD;
83         case "log.println": return METHOD;
84         case "log.dump": return METHOD;
85         case "regexp": return METHOD;
86         case "rpc.xml": return METHOD;
87         case "rpc.soap": return METHOD;
88         case "crypto.rsa": return METHOD;
89         case "crypto.md5": return METHOD;
90         case "crypto.sha1": return METHOD;
91         case "crypto.rc4": return METHOD;
92         case "stream.parse.html": return METHOD;
93         case "stream.parse.xml": return METHOD;
94         case "stream.parse.utf8": return METHOD;
95         //#end
96         return rr.get(name);
97     }
98
99     public void put(Object name, final Object value) {
100         //#switch(name)
101         case "thread":
102             Scheduler.add(new Scheduler.Task() {
103                     public void perform() throws JSExn {
104                         try {
105                             JS.invokePauseable((JSFunction)value);
106                         } catch (JS.PausedException pe) {
107                             // okay; wait for ourselves to be re-enqueued
108                         }
109                     }
110                 });
111         case "ui.clipboard": Platform.setClipBoard((String)value);
112         case "ui.frame": Platform.createSurface((Box)value, true, true);
113         case "ui.window": Platform.createSurface((Box)value, false, true);
114         case "undocumented.internal.proxyAuthorization":
115             HTTP.Proxy.Authorization.authorization = value.toString();
116             HTTP.Proxy.Authorization.waitingForUser.release();
117         //#end
118     }
119
120     public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
121         if (name.equals("date")) return new JSDate(a, b, c, rest, nargs);
122         else if (nargs == 0 && name.equals("thread.yield")) { sleep(0); return null; }
123         else if (nargs == 2) {
124             //#switch(name)
125             case "res.watch": return new Res.ProgressWatcher((Res)a, (JSFunction)b);
126             case "soap": /* return new SOAP((String)a, "", (String)b, null); */
127             case "apply":
128                 Template.getTemplate((Res)b).apply((Box)a, XWT.this);
129                 return a;
130             //#end
131         } else if (nargs == 3 && name.equals("soap")) {
132             /*
133             if (name.equals("soap"))
134                 return new SOAP((String)a, "", (String)b, (String)c);
135             */
136
137         } else if (nargs == 1) {
138             //#switch(name)
139             case "ui.browser": Platform.newBrowserWindow((String)a); return null;
140             case "clone": return new XWT((Res)a);
141             case "res.unzip": return new Res.Zip((Res)a);
142             case "res.uncab": return new Res.Cab((Res)a);
143             case "res.cache": try { return new Res.CachedRes((Res)a, "resources", true); }
144                               catch (Res.NotCacheableException e) { throw new JSExn("this resource cannot be cached"); }
145             case "res.url":
146                 String url = (String)a;
147                 if (url.startsWith("http://")) return new Res.HTTP(url);
148                 else if (url.startsWith("https://")) return new Res.HTTP(url);
149                 else if (url.startsWith("data:")) return new Res.ByteArray(Base64.decode(url.substring(5)), null);
150                 else if (url.startsWith("utf8:")) return new Res.ByteArray(url.substring(5).getBytes(), null);
151                 throw new JSExn("invalid resource specifier " + url);
152             case "thread.sleep": sleep(JS.toInt(a)); return null;
153             case "log.println": Log.logJS(this, a== null ? "**null**" : a.toString()); return null;
154             case "log.dump": Log.recursiveLog("","",a); return null;
155             case "regexp": return new JSRegexp(a, null);
156             case "rpc.xml": return new XMLRPC((String)a, "");
157             case "rpc.soap": /* return new SOAP((String)a, "", null, null); */
158             case "crypto.rsa": /* FEATURE */ break;
159             case "crypto.md5": /* FEATURE */ break;
160             case "crypto.sha1": /* FEATURE */ break;
161             case "crypto.rc4": /* FEATURE */ break;
162             case "stream.parse.html": /* FEATURE */ break;
163             case "stream.parse.xml": /* FEATURE */ break;
164             case "stream.parse.utf8": /* FEATURE */ break;
165             //#end
166         }
167         return null;
168     }
169
170     public static void sleep(final int i) throws JSExn {
171         try {
172             final JS.UnpauseCallback callback = JS.pause();
173             final long currentTime = System.currentTimeMillis();
174             new Thread() {
175                 public void run() {
176                     try { Thread.sleep(i); } catch (InterruptedException e) { }
177                     Scheduler.add(new Scheduler.Task() {
178                             public void perform() throws JSExn {
179                                 callback.unpause(null);
180                             }
181                         });
182                 }
183             }.start();
184         } catch (JS.NotPauseableException npe) {
185             throw new JSExn("you cannot sleep or yield in the foreground thread");
186         }
187     }
188     
189     public static final JSMath xwtMath = new JSMath() {
190             private JS gs = new JSScope.Global();
191             public String toString() { return "XWTMATH"; }
192             public Object get(Object key) throws JSExn {
193                 //#switch(key)
194                 case "isNaN": return gs.get("isNaN");
195                 case "isFinite": return gs.get("isFinite");
196                 case "NaN": return gs.get("NaN");
197                 case "Infinity": return gs.get("Infinity");
198                 //#end
199                 return super.get(key);
200             }
201         };
202
203     public static final JS xwtString = new JS() {
204             private JS gs = new JSScope.Global();
205             public void put(Object key, Object val) { }
206             public Object get(Object key) throws JSExn {
207                 //#switch(key)
208                 case "parseInt": return gs.get("parseInt");
209                 case "parseFloat": return gs.get("parseFloat");
210                 case "decodeURI": return gs.get("decodeURI");
211                 case "decodeURIComponent": return gs.get("decodeURIComponent");
212                 case "encodeURI": return gs.get("encodeURI");
213                 case "encodeURIComponent": return gs.get("encodeURIComponent");
214                 case "escape": return gs.get("escape");
215                 case "unescape": return gs.get("unescape");
216                 case "fromCharCode": return gs.get("stringFromCharCode");
217                 //#end
218                 return null;
219             }
220         };
221
222     private class XMLHelper extends XML {
223         Vector obStack = new Vector();
224         public XMLHelper() { super(BUFFER_SIZE); }
225         public void startElement(XML.Element c) throws XML.SchemaException {
226             try {
227                 JS o = new JS();
228                 o.put("$name", c.localName);
229                 for(int i=0; i<c.len; i++) o.put(c.keys[i], c.vals[i]);
230                 o.put("$numchildren", new Integer(0));
231                 obStack.addElement(o);
232             } catch (JSExn jse) {
233                 throw new Error("this should never happen");
234             }
235         }
236         public void endElement(XML.Element c) throws XML.SchemaException {
237             try {
238                 if (obStack.size() == 1) return;
239                 JS me = (JS)obStack.lastElement();
240                 obStack.setSize(obStack.size() - 1);
241                 JS parent = (JS)obStack.lastElement();
242                 int numchildren = ((Integer)parent.get("$numchildren")).intValue();
243                 parent.put("$numchildren", new Integer(numchildren + 1));
244                 parent.put(new Integer(numchildren), me);
245             } catch (JSExn jse) {
246                 throw new Error("this should never happen");
247             }
248         }
249         public void characters(char[] ch, int start, int length) throws XML.SchemaException {
250             try {
251                 String s = new String(ch, start, length);
252                 JS parent = (JS)obStack.lastElement();
253                 int numchildren = ((Integer)parent.get("$numchildren")).intValue();
254                 Object lastChild = parent.get(new Integer(numchildren - 1));
255                 if (lastChild instanceof String) {
256                     parent.put(new Integer(numchildren - 1), lastChild + s);
257                 } else {
258                     parent.put("$numchildren", new Integer(numchildren + 1));
259                     parent.put(new Integer(numchildren), s);
260                 }
261             } catch (JSExn jse) {
262                 throw new Error("this should never happen");
263             }
264         }
265         public void whitespace(char[] ch, int start, int length) {}
266         public JS doParse(InputStream is) throws JSExn {
267             try { 
268                 BufferedReader r = new BufferedReader(new InputStreamReader(is));
269                 parse(r);
270             } catch (XML.XMLException e) {
271                 throw new JSExn("error parsing XML: " + e.toString());
272             } catch (IOException e) {
273                 if (Log.on) Log.log(this, "IO Exception while reading from file");
274                 if (Log.on) Log.log(this, e);
275                 throw new JSExn("error reading from Resource");
276             }
277             return obStack.size() >= 1 ? (JS)obStack.elementAt(0) : null;
278         }
279     }
280 }