1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
6 import org.ibex.util.*;
7 import org.ibex.graphics.*;
8 import org.ibex.plat.*;
10 import org.ibex.crypto.*;
12 /** Singleton class that provides all functionality in the ibex.* namespace */
13 public final class Ibex extends JS implements JS.Cloneable {
18 public Ibex(Stream rr) { try { this.rr = bless(rr);} catch(JSExn e) { throw new Error("should never happen"); } }
20 public JS resolveString(String str, boolean permitAbsolute) throws JSExn {
21 if (str.indexOf("://") != -1) {
22 if (permitAbsolute) return (Stream)url2res(str);
23 throw new JSExn("absolute URL " + str + " not permitted here");
26 //JS ret = (JS)getAndTriggerTraps("");
29 while(str.indexOf('.') != -1) {
30 String path = str.substring(0, str.indexOf('.'));
31 str = str.substring(str.indexOf('.') + 1);
32 ret = ret.get(JS.S(path));
34 if (!"".equals(str)) ret = ret.get(JS.S(str));
38 /** lets us put multi-level get/put/call keys all in the same method */
39 private class Sub extends JS {
41 Sub(String key) { this.key = key; }
42 public void put(JS key, JS val) throws JSExn { Ibex.this.put(this.key + "." + JS.toString(key), val); }
43 public JS get(JS key) throws JSExn { return Ibex.this.get(this.key + "." + JS.toString(key)); }
44 public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
45 return Ibex.this.callMethod(this.key, a0, a1, a2, rest, nargs);
47 public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
48 return Ibex.this.callMethod(this.key + "." + JS.toString(method), a0, a1, a2, rest, nargs);
51 private Cache subCache = new Cache(20);
52 private Sub getSub(String s) {
53 Sub ret = (Sub)subCache.get(s);
54 if (ret == null) subCache.put(s, ret = new Sub(s));
58 public JS get(JS key) throws JSExn { return JS.isString(key) ? get(JS.toString(key)) : null; }
59 JS get(String name) throws JSExn {
60 if (name instanceof String && ((String)name).length() == 0) return rr;
62 case "math": return ibexMath;
63 case "string": return ibexString;
64 case "date": return METHOD;
65 case "box": return new Box();
66 case "clone": return METHOD;
67 case "bless": return METHOD;
68 case "regexp": return METHOD;
69 case "ui": return getSub("ui");
70 case "ui.font": return getSub("ui.font");
71 case "ui.font.wait": return METHOD;
72 case "ui.font.width": return METHOD;
73 case "ui.font.height": return METHOD;
74 case "ui.font.sansserif": return Main.builtin.get(JS.S("fonts/vera/Vera.ttf"));
75 case "ui.font.monospace": return Main.builtin.get(JS.S("fonts/vera/VeraMono.ttf"));
76 case "ui.font.serif": return Main.builtin.get(JS.S("fonts/vera/VeraSe.ttf"));
77 case "ui.browser": return METHOD;
78 case "ui.mouse": return getSub("ui.mouse");
79 case "ui.mouse.button":
80 if (Surface.button1 && !Surface.button2 && !Surface.button3) return N(1);
81 else if (!Surface.button1 && Surface.button2 && !Surface.button3) return N(2);
82 else if (!Surface.button1 && !Surface.button2 && Surface.button3) return N(3);
84 case "ui.key": return getSub("ui.key");
85 case "ui.key.name": return getSub("ui.key.name");
86 case "ui.key.name.alt": return JS.S(Platform.altKeyName());
87 case "ui.key.alt": return Surface.alt ? T : F;
88 case "ui.key.control": return Surface.control ? T : F;
89 case "ui.key.shift": return Surface.shift ? T : F;
90 case "ui.clipboard": return JS.S((String)Platform.getClipBoard());
91 case "ui.maxdim": return N(Short.MAX_VALUE);
92 case "ui.screen": return getSub("ui.screen");
93 case "ui.screen.width": return N(Platform.getScreenWidth());
94 case "ui.screen.height": return N(Platform.getScreenHeight());
95 case "undocumented": return getSub("undocumented");
96 case "undocumented.initialOrigin": return JS.S(Main.origin);
97 case "undocumented.initialTemplate": return JS.S(Main.initialTemplate);
98 case "thread": return getSub("thread");
99 case "thread.yield": return METHOD;
100 case "thread.sleep": return METHOD;
101 case "stream": return getSub("stream");
102 case "stream.homedir": return url2res("file:" + System.getProperty("user.home"));
103 case "stream.tempdir": return url2res("file:" + System.getProperty("java.io.tempdir"));
104 case "stream.watch": return METHOD;
105 case "stream.unzip": return METHOD;
106 case "stream.uncab": return METHOD;
107 case "stream.cache": return METHOD;
108 case "stream.url": return METHOD;
109 case "stream.parse.html": return METHOD;
110 case "stream.parse.xml": return METHOD;
111 case "stream.parse.utf8": return METHOD;
112 case "net": return getSub("net");
113 case "net.rpc": return getSub("net.rpc");
114 case "net.rpc.xml": return METHOD;
115 case "net.rpc.soap": return METHOD;
116 case "log": return getSub("log");
117 case "log.debug": return METHOD;
118 case "log.info": return METHOD;
119 case "log.warn": return METHOD;
120 case "log.error": return METHOD;
121 case "crypto": return getSub("crypto");
122 case "crypto.rsa": return METHOD;
123 case "crypto.md5": return METHOD;
124 case "crypto.sha1": return METHOD;
125 case "crypto.rc4": return METHOD;
130 public void put(JS key, JS value) throws JSExn { if(JS.isString(key)) put(JS.toString(key),value); else super.put(key,value); }
131 void put(Object name, final JS value) throws JSExn {
133 case "thread": Scheduler.add((Task)value); return;
134 case "ui.clipboard": Platform.setClipBoard(JS.toString(value)); return;
135 case "ui.frame": Platform.createSurface((Box)value, true, true); return;
136 case "ui.window": Platform.createSurface((Box)value, false, true); return;
137 case "undocumented.proxyAuthorization":
138 HTTP.Proxy.Authorization.authorization = value.toString();
139 HTTP.Proxy.Authorization.waitingForUser.release();
142 throw new JSExn("attempted to put unknown property: ibex."+name);
145 public JS callMethod(JS name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
146 if(JS.isString(name)) return callMethod(JS.toString(name),a,b,c,rest,nargs);
147 return super.callMethod(name,a,b,c,rest,nargs);
149 JS callMethod(String name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
152 case "date": return new JSDate(a, b, c, rest, nargs);
153 case "net.rpc.soap": return new SOAP(JS.toString(a), "", JS.toString(b), JS.toString(c));
154 // FIXME support object dumping
155 case "log.debug": JS.debug(a== null ? "**null**" : a.toString()); return null;
156 case "log.info": JS.info(a== null ? "**null**" : a.toString()); return null;
157 case "log.warn": JS.warn(a== null ? "**null**" : a.toString()); return null;
158 case "log.error": JS.error(a== null ? "**null**" : a.toString()); return null;
164 case "thread.yield": sleep(0); return null;
170 if(a == null) throw new JSExn("can't clone the null value");
171 return ((JS)a).jsclone();
172 case "bless": return bless((JS)a);
173 case "ui.browser": Platform.newBrowserWindow(JS.toString(a)); return null;
174 case "stream.unzip": return new Stream.Zip((Stream)a);
175 case "stream.uncab": return new Stream.Cab((Stream)a);
177 try { return new Stream.CachedStream((Stream)a, "resources", true); }
178 catch (Stream.NotCacheableException e) { throw new JSExn("this resource cannot be cached"); }
180 String url = JS.toString(a);
181 if (url.startsWith("http://")) return new Stream.HTTP(url);
182 else if (url.startsWith("https://")) return new Stream.HTTP(url);
183 else if (url.startsWith("data:")) return new Stream.ByteArray(Base64.decode(url.substring(5)), null);
184 else if (url.startsWith("utf8:")) return new Stream.ByteArray(url.substring(5).getBytes(), null);
185 else if (url.startsWith("file:")) {
187 Platform.fileDialog(url.substring(5), false);
189 throw new JSExn("invalid resource specifier " + url);
191 case "thread.sleep": sleep(JS.toInt(a)); return null;
192 case "regexp": return new JSRegexp(a, null);
193 case "net.rpc.xml": return new XMLRPC(JS.toString(a), "");
194 case "crypto.rsa": /* FEATURE */ return null;
195 case "crypto.md5": /* FEATURE */ return null;
196 case "crypto.sha1": /* FEATURE */ return null;
197 case "crypto.rc4": /* FEATURE */ return null;
198 case "stream.parse.html": throw new JSExn("not implemented yet"); //return null;
199 case "stream.parse.xml": new XMLHelper((JS)b).doParse((JS)a); return null;
200 // FIXME backgrounding
201 case "stream.parse.utf8": try { return JS.S(new String(InputStreamToByteArray.convert(Stream.getInputStream(a)))); }
202 catch (Exception e) { Log.warn(this, e); }
207 case "stream.watch": return new Stream.ProgressWatcher((Stream)a, (JS)b);
208 case "regexp": return new JSRegexp(a, b);
212 case "ui.font.height": return N(Font.getFont((Stream)a, JS.toInt(b)).textheight(JS.toString(c)));
213 case "ui.font.wait": throw new Error("FIXME: ibex.ui.font.wait not implemented");
214 case "ui.font.width": return N(Font.getFont((Stream)a, JS.toInt(b)).textwidth(JS.toString(c)));
218 } catch (RuntimeException e) {
219 // FIXME: maybe JSExn should take a second argument, Exception
220 Log.warn(this, "ibex."+name+"() threw: " + e);
221 throw new JSExn("invalid argument for ibex object method "+name+"()");
224 throw new JSExn("invalid number of arguments ("+nargs+") for ibex object method "+name+"()");
227 public Stream url2res(String url) throws JSExn {
228 if (url.startsWith("http://")) return new Stream.HTTP(url);
229 else if (url.startsWith("https://")) return new Stream.HTTP(url);
230 else if (url.startsWith("data:")) return new Stream.ByteArray(Base64.decode(url.substring(5)), null);
231 else if (url.startsWith("utf8:")) return new Stream.ByteArray(url.substring(5).getBytes(), null);
232 else throw new JSExn("invalid resource specifier " + url);
233 // FIXME support file:// via dialog boxes
236 public static void sleep(final int i) throws JSExn {
238 final JS.UnpauseCallback callback = JS.pause();
239 // FEATURE use a single sleeper thread
240 new Thread() { public void run() {
241 try { Thread.sleep(i); } catch (InterruptedException e) { }
242 Scheduler.add(callback);
244 } catch (JS.NotPauseableException npe) {
245 throw new JSExn("you cannot sleep or yield in the foreground thread");
249 public static final JSMath ibexMath = new JSMath() {
250 // FEATURE: find a cleaner way to do this
251 private JS gs = /*new JSScope.Global();*/ null; // FIXME: Global scope
252 public JS get(JS key) throws JSExn {
253 //#switch(JS.toString(key))
254 case "isNaN": return METHOD;
255 case "isFinite": return METHOD;
256 case "NaN": return METHOD;
257 case "Infinity": return METHOD;
259 return super.get(key);
261 public JS callMethod(JS name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
262 //#switch(JS.toString(name))
263 case "isNaN": return gs.callMethod(name,a,b,c,rest,nargs);
264 case "isFinite": return gs.callMethod(name,a,b,c,rest,nargs);
265 case "NaN": return gs.callMethod(name,a,b,c,rest,nargs);
266 case "Infinity": return gs.callMethod(name,a,b,c,rest,nargs);
268 return super.callMethod(name,a,b,c,rest,nargs);
272 public static final JS ibexString = new JS() {
273 private JS gs = /*new JSScope.Global();*/ null; // FIXME: Global scope
274 public JS get(JS key) throws JSExn {
275 //#switch(JS.toString(key))
276 case "parseInt": return METHOD;
277 case "parseFloat": return METHOD;
278 case "decodeURI": return METHOD;
279 case "decodeURIComponent": return METHOD;
280 case "encodeURI": return METHOD;
281 case "encodeURIComponent": return METHOD;
282 case "escape": return METHOD;
283 case "unescape": return METHOD;
284 case "fromCharCode": return METHOD;
286 return super.get(key);
288 public JS callMethod(JS name, JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
289 //#switch(JS.toString(name))
290 case "parseInt": return gs.callMethod(name,a,b,c,rest,nargs);
291 case "parseFloat": return gs.callMethod(name,a,b,c,rest,nargs);
292 case "decodeURI": return gs.callMethod(name,a,b,c,rest,nargs);
293 case "decodeURIComponent": return gs.callMethod(name,a,b,c,rest,nargs);
294 case "encodeURI": return gs.callMethod(name,a,b,c,rest,nargs);
295 case "encodeURIComponent": return gs.callMethod(name,a,b,c,rest,nargs);
296 case "escape": return gs.callMethod(name,a,b,c,rest,nargs);
297 case "unescape": return gs.callMethod(name,a,b,c,rest,nargs);
298 case "fromCharCode": return gs.callMethod(name,a,b,c,rest,nargs);
300 return super.callMethod(name,a,b,c,rest,nargs);
304 private class XMLHelper extends XML {
305 private class Wrapper extends XML.Exn { public JSExn wrapee; public Wrapper(JSExn jse) { super(""); wrapee = jse; } }
306 private JS characters, whitespace, endElement, startElement;
307 public XMLHelper(JS b) throws JSExn {
309 startElement = b.getAndTriggerTraps(JS.S("startElement"));
310 endElement = b.getAndTriggerTraps(JS.S("endElement"));
311 characters = b.getAndTriggerTraps(JS.S("characters"));
312 whitespace = b.getAndTriggerTraps(JS.S("whitespace"));
315 public void startElement(XML.Element c) throws XML.Exn { try {
316 JS attrs = new JS.O();
317 // FIXME attribute URIs? add an additional hash?
318 for(int i=0; i<c.getAttrLen(); i++) attrs.put(JS.S(c.getAttrKey(i)), JS.S(c.getAttrVal(i)));
319 startElement.call(JS.S(c.getLocalName()), attrs, JS.S(c.getUri()), null, 3);
320 } catch (JSExn jse) { throw new Wrapper(jse); } }
322 public void endElement(XML.Element c) throws XML.Exn { try {
323 endElement.call(JS.S(c.getLocalName()), JS.S(c.getUri()), null, null, 2);
324 } catch (JSExn jse) { throw new Wrapper(jse); } }
326 public void characters(char[] ch, int start, int length) throws XML.Exn { try {
327 characters.call(JS.S(new String(ch, start, length)), null, null, null, 1);
328 } catch (JSExn jse) { throw new Wrapper(jse); } }
330 public void whitespace(char[] ch, int start, int length) throws XML.Exn { try {
331 whitespace.call(JS.S(new String(ch, start, length)), null, null, null, 1);
332 } catch (JSExn jse) { throw new Wrapper(jse); } }
334 public void doParse(JS s) throws JSExn {
336 parse(new BufferedReader(new InputStreamReader(Stream.getInputStream(s))));
337 } catch (Wrapper e) {
339 } catch (XML.Exn e) {
340 throw new JSExn("error parsing XML: " + e.toString());
341 } catch (IOException e) {
342 if (Log.on) Log.info(this, "IO Exception while reading from file");
343 if (Log.on) Log.info(this, e);
344 throw new JSExn("error reading from Resource");
349 // FEATURE: move this into builtin.xwar
350 public Blessing bless(JS b) throws JSExn { return new Ibex.Blessing(b, this, null, null); }
351 // FIXME: Does this really need to extends JS.Clone?
352 public static class Blessing extends JS.Clone {
354 private Template t = null;
355 public JS parentkey = null;
356 public Blessing parent = null;
357 private Hash cache = new Hash();
358 public Blessing(JS clonee, Ibex ibex, Blessing parent, JS parentkey) throws JSExn {
359 super(clonee); this.ibex = ibex; this.parentkey = parentkey; this.parent = parent; }
360 public JS get(JS key) throws JSExn {
361 if (JS.isString(key) && JS.toString(key).equals("")) return getStatic();
362 if (cache.get(key) != null) return (JS)cache.get(key);
363 JS ret = new Blessing(clonee.get(key), ibex, this, key);
367 public static Blessing getBlessing(JS js) {
368 while (js instanceof JS.Clone && !(js instanceof Blessing)) js = ((JS.Clone)js).getClonee();
369 if (!(js instanceof Blessing)) return null;
372 public InputStream getImage() throws JSExn {
374 InputStream in = Stream.getInputStream(this);
375 if (in != null) return in;
376 } catch (IOException e) { /* DELIBERATE */ }
377 String[] exts = new String[] { ".png", ".jpeg", ".gif" };
378 for (int i=0; i < exts.length; i++)
380 InputStream in = Stream.getInputStream(parent.get(JS.S(JS.toString(parentkey) + exts[i])));
381 if (in != null) return in;
382 } catch (IOException f) { /* DELIBERATE */ }
385 public JSScope getStatic() {
388 JS res = (JS) parent.get(JS.S(JS.toString(parentkey) + ".t"));
389 t = Template.buildTemplate(res.unclone().toString(), res, ibex);
391 return t.staticScope;
392 } catch (Exception e) {
397 public JS call(JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
399 if (nargs != 1 && nargs != 9999) throw new JSExn("FIXME can only call with one arg");
401 if (t == null) throw new JSExn("No such template " + parentkey);
402 if (nargs == 9999) /*return t;*/ throw new Error("FIXME"); // FIXME: 9999 stuff
403 if(!(a instanceof Box)) throw new JSExn("can only apply templates to boxes");