1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
10 import org.xwt.translators.*;
11 import org.bouncycastle.util.encoders.Base64;
13 /** Singleton class that provides all functionality in the xwt.* namespace */
14 public final class XWT extends JS {
17 public XWT(Res rr) { this.rr = rr; }
19 private Cache subCache = new Cache(20);
20 private Sub getSub(String s) {
21 Sub ret = (Sub)subCache.get(s);
22 if (ret == null) subCache.put(s, ret = new Sub(s));
26 /** lets us put multi-level get/put/call keys all in the same method */
27 private class Sub extends JS {
29 Sub(String key) { this.key = key; }
30 public String toString() { return "XWTSUB " + key; }
31 public void put(Object key, Object val) throws JSExn { XWT.this.put(this.key + "." + key, val); }
32 public Object get(Object key) throws JSExn { return XWT.this.get(this.key + "." + key); }
33 public boolean coerceToBoolean() {
34 if (key.equals("ui.key.alt")) return Surface.alt ? true : false;
35 return super.coerceToBoolean();
37 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
38 return XWT.this.callMethod(this.key, a0, a1, a2, rest, nargs);
40 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
41 return XWT.this.callMethod(this.key + "." + method, a0, a1, a2, rest, nargs);
45 public Object get(Object name) throws JSExn {
46 if (name instanceof String && ((String)name).length() == 0) return rr;
48 case "math": return xwtMath;
49 case "string": return xwtString;
50 case "date": return METHOD;
51 case "origin": return Main.origin; // FIXME not in ref
52 case "box": return new Box();
53 case "apply": return METHOD;
54 case "graft": return METHOD;
55 case "clone": return METHOD;
56 case "regexp": return METHOD;
57 case "font": return getSub("font");
58 case "font.sansserif": return Main.builtin.get("fonts/vera/Vera.ttf");
59 case "font.monospace": return Main.builtin.get("fonts/vera/VeraMono.ttf");
60 case "font.serif": return Main.builtin.get("fonts/vera/VeraSe.ttf");
61 case "ui": return getSub("ui");
62 case "ui.browser": return METHOD;
63 case "ui.mouse": return getSub("ui.mouse");
64 case "ui.mouse.button":
65 if (Surface.button1 && !Surface.button2 && !Surface.button3) return N(1);
66 else if (!Surface.button1 && Surface.button2 && !Surface.button3) return N(2);
67 else if (!Surface.button1 && !Surface.button2 && Surface.button3) return N(3);
69 case "ui.key": return getSub("ui.key");
70 case "ui.key.alt": return getSub("ui.key.alt");
71 case "ui.key.alt.name": return Platform.altKeyName();
72 case "ui.key.control": return Surface.control ? T : F;
73 case "ui.key.shift": return Surface.shift ? T : F;
74 case "ui.clipboard": return Platform.getClipBoard();
75 case "ui.maxdim": return new Integer(Short.MAX_VALUE);
76 case "ui.screen": return getSub("ui.screen");
77 case "ui.screen.width": return new Integer(Platform.getScreenWidth());
78 case "ui.screen.height": return new Integer(Platform.getScreenHeight());
79 case "undocumented": return getSub("undocumented");
80 case "undocumented.internal": return getSub("undocumented.internal");
81 case "thread": return getSub("thread");
82 case "thread.yield": return METHOD;
83 case "thread.sleep": return METHOD;
84 case "stream": return getSub("stream");
85 case "stream.home": return url2res("file:" + System.getProperty("user.home"));
86 case "stream.temp": return url2res("file:" + System.getProperty("java.io.tempdir"));
87 case "stream.watch": return METHOD;
88 case "stream.unzip": return METHOD;
89 case "stream.uncab": return METHOD;
90 case "stream.cache": return METHOD;
91 case "stream.url": return METHOD;
92 case "stream.parse.html": return METHOD;
93 case "stream.parse.xml": return METHOD;
94 case "stream.parse.utf8": return METHOD;
95 case "rpc": return getSub("rpc");
96 case "rpc.xml": return METHOD;
97 case "rpc.soap": return METHOD;
98 case "log": return getSub("log");
99 case "log.debug": return METHOD;
100 case "log.info": return METHOD;
101 case "log.warn": return METHOD;
102 case "log.error": return METHOD;
103 case "crypto": return getSub("crypto");
104 case "crypto.rsa": return METHOD;
105 case "crypto.md5": return METHOD;
106 case "crypto.sha1": return METHOD;
107 case "crypto.rc4": return METHOD;
109 return super.get(name);
112 public void put(Object name, final Object value) throws JSExn {
114 case "thread": Scheduler.add((JSFunction)value); return;
115 case "ui.clipboard": Platform.setClipBoard((String)value); return;
116 case "ui.frame": Platform.createSurface((Box)value, true, true); return;
117 case "ui.window": Platform.createSurface((Box)value, false, true); return;
118 case "undocumented.internal.proxyAuthorization":
119 HTTP.Proxy.Authorization.authorization = value.toString();
120 HTTP.Proxy.Authorization.waitingForUser.release(); return;
123 throw new JSExn("attempted to put unknown property: xwt."+name);
126 public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
129 case "date": return new JSDate(a, b, c, rest, nargs);
130 case "rpc.soap": return new SOAP((String)a, "", (String)b, (String)c);
132 if (a instanceof Box) throw new JSExn("can't graft onto Boxes");
133 if (a instanceof String) throw new JSExn("can't graft onto Strings");
134 if (a instanceof Number) throw new JSExn("can't graft onto Numbers");
135 if (a instanceof Res) return new Res.Graft((Res)a, b, c);
136 // FEATURE: grafting onto JS
137 throw new JSExn("cannot graft onto "+a.getClass());
143 case "thread.yield": sleep(0); return null;
148 case "ui.browser": Platform.newBrowserWindow((String)a); return null;
149 case "clone": return new XWT((Res)a);
150 case "stream.unzip": return new Res.Zip((Res)a);
151 case "stream.uncab": return new Res.Cab((Res)a);
152 case "stream.cache": try { return new Res.CachedRes((Res)a, "resources", true); }
153 catch (Res.NotCacheableException e) { throw new JSExn("this resource cannot be cached"); }
154 case "stream.url": return url2res((String)a);
155 case "thread.sleep": sleep(JS.toInt(a)); return null;
156 case "log.debug": JS.log(this, a== null ? "**null**" : a.toString()); return null;
157 case "log.info": JS.log(this, a== null ? "**null**" : a.toString()); return null;
158 case "log.warn": JS.log(this, a== null ? "**null**" : a.toString()); return null;
159 case "log.error": JS.log(this, a== null ? "**null**" : a.toString()); return null;
160 case "regexp": return new JSRegexp(a, null);
161 case "rpc.xml": return new XMLRPC((String)a, "");
162 case "crypto.rsa": /* FEATURE */ return null;
163 case "crypto.md5": /* FEATURE */ return null;
164 case "crypto.sha1": /* FEATURE */ return null;
165 case "crypto.rc4": /* FEATURE */ return null;
166 case "stream.parse.html": /* FIXME */ return null;
167 case "stream.parse.xml": /* FIXME -- SAX NOT DOM!! */ return null;
168 case "stream.parse.utf8": /* FIXME */ return null;
173 case "stream.watch": return new Res.ProgressWatcher((Res)a, (JSFunction)b);
175 if (b instanceof Res) Template.getTemplate((Res)b).apply((Box)a, XWT.this);
177 JS to = (JS)a, from = (JS)b; Object k;
178 for (Enumeration e = from.keys(); e.hasMoreElements();) {
179 k = e.nextElement(); to.put(k, from.get(k));
186 } catch (RuntimeException e) {
187 // FIXME: maybe JSExn should take a second argument, Exception
188 Log.info(this, "xwt."+name+"() threw: "+e);
189 throw new JSExn("invalid argument for xwt object method "+name+"()");
192 throw new JSExn("invalid number of arguments for xwt object method "+name+"()");
195 public Res url2res(String url) throws JSExn {
196 if (url.startsWith("http://")) return new Res.HTTP(url);
197 else if (url.startsWith("https://")) return new Res.HTTP(url);
198 else if (url.startsWith("data:")) return new Res.ByteArray(Base64.decode(url.substring(5)), null);
199 else if (url.startsWith("utf8:")) return new Res.ByteArray(url.substring(5).getBytes(), null);
200 else throw new JSExn("invalid resource specifier " + url);
201 // FIXME support file:// via dialog boxes
204 public static void sleep(final int i) throws JSExn {
206 final JS.UnpauseCallback callback = JS.pause();
207 final long currentTime = System.currentTimeMillis();
208 new Thread() { public void run() {
209 try { Thread.sleep(i); } catch (InterruptedException e) { }
210 Scheduler.add(callback);
212 } catch (JS.NotPauseableException npe) {
213 throw new JSExn("you cannot sleep or yield in the foreground thread");
217 public static final JSMath xwtMath = new JSMath() {
218 private JS gs = new JSScope.Global();
219 public String toString() { return "XWTMATH"; }
220 public Object get(Object key) throws JSExn {
222 case "isNaN": return gs.get("isNaN");
223 case "isFinite": return gs.get("isFinite");
224 case "NaN": return gs.get("NaN");
225 case "Infinity": return gs.get("Infinity");
227 return super.get(key);
231 public static final JS xwtString = new JS() {
232 private JS gs = new JSScope.Global();
233 public void put(Object key, Object val) { }
234 public Object get(Object key) throws JSExn {
236 case "parseInt": return gs.get("parseInt");
237 case "parseFloat": return gs.get("parseFloat");
238 case "decodeURI": return gs.get("decodeURI");
239 case "decodeURIComponent": return gs.get("decodeURIComponent");
240 case "encodeURI": return gs.get("encodeURI");
241 case "encodeURIComponent": return gs.get("encodeURIComponent");
242 case "escape": return gs.get("escape");
243 case "unescape": return gs.get("unescape");
244 case "fromCharCode": return gs.get("stringFromCharCode");
250 private class XMLHelper extends XML {
251 Vector obStack = new Vector();
252 public XMLHelper() { super(BUFFER_SIZE); }
253 public void startElement(XML.Element c) throws XML.Exn {
256 o.put("$name", c.getLocalName());
257 for(int i=0; i < c.getAttrLen(); i++) o.put(c.getAttrKey(i), c.getAttrVal(i));
258 o.put("$numchildren", new Integer(0));
259 obStack.addElement(o);
260 } catch (JSExn jse) {
261 throw new Error("this should never happen");
264 public void endElement(XML.Element c) throws XML.Exn {
266 if (obStack.size() == 1) return;
267 JS me = (JS)obStack.lastElement();
268 obStack.setSize(obStack.size() - 1);
269 JS parent = (JS)obStack.lastElement();
270 int numchildren = ((Integer)parent.get("$numchildren")).intValue();
271 parent.put("$numchildren", new Integer(numchildren + 1));
272 parent.put(new Integer(numchildren), me);
273 } catch (JSExn jse) {
274 throw new Error("this should never happen");
277 public void characters(char[] ch, int start, int length) throws XML.Exn {
279 String s = new String(ch, start, length);
280 JS parent = (JS)obStack.lastElement();
281 int numchildren = ((Integer)parent.get("$numchildren")).intValue();
282 Object lastChild = parent.get(new Integer(numchildren - 1));
283 if (lastChild instanceof String) {
284 parent.put(new Integer(numchildren - 1), lastChild + s);
286 parent.put("$numchildren", new Integer(numchildren + 1));
287 parent.put(new Integer(numchildren), s);
289 } catch (JSExn jse) {
290 throw new Error("this should never happen");
293 public void whitespace(char[] ch, int start, int length) {}
294 public JS doParse(InputStream is) throws JSExn {
296 BufferedReader r = new BufferedReader(new InputStreamReader(is));
298 } catch (XML.Exn e) {
299 throw new JSExn("error parsing XML: " + e.toString());
300 } catch (IOException e) {
301 if (Log.on) Log.info(this, "IO Exception while reading from file");
302 if (Log.on) Log.info(this, e);
303 throw new JSExn("error reading from Resource");
305 return obStack.size() >= 1 ? (JS)obStack.elementAt(0) : null;