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 {
16 public final Stream rr;
17 public XWT(Stream 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;
118 Surface s = Platform.createSurface(b,true, true);
119 if(b.get("titlebar") != null) s.setTitleBarText(JS.toString(b.get("titlebar")));
121 case "ui.window": Platform.createSurface((Box)value, false, true); return;
122 case "undocumented.internal.proxyAuthorization":
123 HTTP.Proxy.Authorization.authorization = value.toString();
124 HTTP.Proxy.Authorization.waitingForUser.release(); return;
127 throw new JSExn("attempted to put unknown property: xwt."+name);
130 public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
133 case "date": return new JSDate(a, b, c, rest, nargs);
134 case "rpc.soap": return new SOAP((String)a, "", (String)b, (String)c);
136 if (a instanceof Box) throw new JSExn("can't graft onto Boxes");
137 if (a instanceof String) throw new JSExn("can't graft onto Strings");
138 if (a instanceof Number) throw new JSExn("can't graft onto Numbers");
139 if (a instanceof Stream) return new Stream.Graft((Stream)a, b, c);
140 // FEATURE: grafting onto JS
141 throw new JSExn("cannot graft onto "+a.getClass());
147 case "thread.yield": sleep(0); return null;
152 case "ui.browser": Platform.newBrowserWindow((String)a); return null;
153 case "clone": return new XWT((Stream)a);
154 case "stream.unzip": return new Stream.Zip((Stream)a);
155 case "stream.uncab": return new Stream.Cab((Stream)a);
156 case "stream.cache": try { return new Stream.CachedStream((Stream)a, "resources", true); }
157 catch (Stream.NotCacheableException e) { throw new JSExn("this resource cannot be cached"); }
158 case "stream.url": return url2res((String)a);
159 case "thread.sleep": sleep(JS.toInt(a)); return null;
160 case "log.debug": JS.log(this, a== null ? "**null**" : a.toString()); return null;
161 case "log.info": JS.log(this, a== null ? "**null**" : a.toString()); return null;
162 case "log.warn": JS.log(this, a== null ? "**null**" : a.toString()); return null;
163 case "log.error": JS.log(this, a== null ? "**null**" : a.toString()); return null;
164 case "regexp": return new JSRegexp(a, null);
165 case "rpc.xml": return new XMLRPC((String)a, "");
166 case "crypto.rsa": /* FEATURE */ return null;
167 case "crypto.md5": /* FEATURE */ return null;
168 case "crypto.sha1": /* FEATURE */ return null;
169 case "crypto.rc4": /* FEATURE */ return null;
170 case "stream.parse.html": /* FIXME */ return null;
171 case "stream.parse.xml": /* FIXME -- SAX NOT DOM!! */ return null;
172 case "stream.parse.utf8": /* FIXME */ return null;
177 case "stream.watch": return new Stream.ProgressWatcher((Stream)a, (JSFunction)b);
179 if (b instanceof Stream) Template.getTemplate((Stream)b).apply((Box)a, XWT.this);
181 JS to = (JS)a, from = (JS)b; Object k;
182 for (Enumeration e = from.keys(); e.hasMoreElements();) {
183 k = e.nextElement(); to.put(k, from.get(k));
187 case "regexp": return new JSRegexp(a, b);
191 } catch (RuntimeException e) {
192 // FIXME: maybe JSExn should take a second argument, Exception
193 Log.info(this, "xwt."+name+"() threw: " + e);
194 throw new JSExn("invalid argument for xwt object method "+name+"()");
197 throw new JSExn("invalid number of arguments for xwt object method "+name+"()");
200 public Stream url2res(String url) throws JSExn {
201 if (url.startsWith("http://")) return new Stream.HTTP(url);
202 else if (url.startsWith("https://")) return new Stream.HTTP(url);
203 else if (url.startsWith("data:")) return new Stream.ByteArray(Base64.decode(url.substring(5)), null);
204 else if (url.startsWith("utf8:")) return new Stream.ByteArray(url.substring(5).getBytes(), null);
205 else throw new JSExn("invalid resource specifier " + url);
206 // FIXME support file:// via dialog boxes
209 public static void sleep(final int i) throws JSExn {
211 final JS.UnpauseCallback callback = JS.pause();
212 final long currentTime = System.currentTimeMillis();
213 new Thread() { public void run() {
214 try { Thread.sleep(i); } catch (InterruptedException e) { }
215 Scheduler.add(callback);
217 } catch (JS.NotPauseableException npe) {
218 throw new JSExn("you cannot sleep or yield in the foreground thread");
222 public static final JSMath xwtMath = new JSMath() {
223 private JS gs = new JSScope.Global();
224 public String toString() { return "XWTMATH"; }
225 public Object get(Object key) throws JSExn {
227 case "isNaN": return gs.get("isNaN");
228 case "isFinite": return gs.get("isFinite");
229 case "NaN": return gs.get("NaN");
230 case "Infinity": return gs.get("Infinity");
232 return super.get(key);
236 public static final JS xwtString = new JS() {
237 private JS gs = new JSScope.Global();
238 public void put(Object key, Object val) { }
239 public Object get(Object key) throws JSExn {
241 case "parseInt": return gs.get("parseInt");
242 case "parseFloat": return gs.get("parseFloat");
243 case "decodeURI": return gs.get("decodeURI");
244 case "decodeURIComponent": return gs.get("decodeURIComponent");
245 case "encodeURI": return gs.get("encodeURI");
246 case "encodeURIComponent": return gs.get("encodeURIComponent");
247 case "escape": return gs.get("escape");
248 case "unescape": return gs.get("unescape");
249 case "fromCharCode": return gs.get("stringFromCharCode");
255 private class XMLHelper extends XML {
256 Vector obStack = new Vector();
257 public XMLHelper() { super(BUFFER_SIZE); }
258 public void startElement(XML.Element c) throws XML.Exn {
261 o.put("$name", c.getLocalName());
262 for(int i=0; i < c.getAttrLen(); i++) o.put(c.getAttrKey(i), c.getAttrVal(i));
263 o.put("$numchildren", new Integer(0));
264 obStack.addElement(o);
265 } catch (JSExn jse) {
266 throw new Error("this should never happen");
269 public void endElement(XML.Element c) throws XML.Exn {
271 if (obStack.size() == 1) return;
272 JS me = (JS)obStack.lastElement();
273 obStack.setSize(obStack.size() - 1);
274 JS parent = (JS)obStack.lastElement();
275 int numchildren = ((Integer)parent.get("$numchildren")).intValue();
276 parent.put("$numchildren", new Integer(numchildren + 1));
277 parent.put(new Integer(numchildren), me);
278 } catch (JSExn jse) {
279 throw new Error("this should never happen");
282 public void characters(char[] ch, int start, int length) throws XML.Exn {
284 String s = new String(ch, start, length);
285 JS parent = (JS)obStack.lastElement();
286 int numchildren = ((Integer)parent.get("$numchildren")).intValue();
287 Object lastChild = parent.get(new Integer(numchildren - 1));
288 if (lastChild instanceof String) {
289 parent.put(new Integer(numchildren - 1), lastChild + s);
291 parent.put("$numchildren", new Integer(numchildren + 1));
292 parent.put(new Integer(numchildren), s);
294 } catch (JSExn jse) {
295 throw new Error("this should never happen");
298 public void whitespace(char[] ch, int start, int length) {}
299 public JS doParse(InputStream is) throws JSExn {
301 BufferedReader r = new BufferedReader(new InputStreamReader(is));
303 } catch (XML.Exn e) {
304 throw new JSExn("error parsing XML: " + e.toString());
305 } catch (IOException e) {
306 if (Log.on) Log.info(this, "IO Exception while reading from file");
307 if (Log.on) Log.info(this, e);
308 throw new JSExn("error reading from Resource");
310 return obStack.size() >= 1 ? (JS)obStack.elementAt(0) : null;