X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fcore%2FIbex.java;h=5efc608bb39f895335671f09fafdde962f4ccbca;hb=HEAD;hp=a157239095811e08be0a67a382135c474951efa1;hpb=0db31fc0dc88749cd8022790fd475df8b7b06c27;p=org.ibex.core.git diff --git a/src/org/ibex/core/Ibex.java b/src/org/ibex/core/Ibex.java index a157239..5efc608 100644 --- a/src/org/ibex/core/Ibex.java +++ b/src/org/ibex/core/Ibex.java @@ -19,7 +19,7 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { private final JS rr; private static final JS.Method METHOD = new JS.Method(); - public Ibex(Stream rr) { try { this.rr = bless(rr);} catch(JSExn e) { throw new Error("should never happen: " + e); } } + public Ibex(Fountain rr) { try { this.rr = bless(rr);} catch(JSExn e) { throw new Error("should never happen: " + e); } } public JS resolveString(String str, boolean permitAbsolute) throws JSExn { if (str.indexOf("://") != -1) { @@ -45,8 +45,8 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { Sub(JS key) { this.key = key; } public void put(JS key, JS val) throws JSExn { Ibex.this.put(JSU.S(JSU.toString(this.key) + "." + JSU.toString(key)), val); } public JS get(JS key) throws JSExn { return Ibex.this.get(JSU.S(JSU.toString(this.key) + "." + JSU.toString(key))); } - public JS call(JS[] args) throws JSExn { return Ibex.this.call(this.key, args); } public JS call(JS method, JS[] args) throws JSExn { + if (method == null) return super.call(null, args); return Ibex.this.call(JSU.S(JSU.toString(this.key) + "." + JSU.toString(method)), args); } } @@ -74,9 +74,9 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { case "ui.font.wait": return METHOD; case "ui.font.width": return METHOD; case "ui.font.height": return METHOD; - case "ui.font.sansserif": return Main.builtin.get(JSU.S("fonts/vera/Vera.ttf")); - case "ui.font.monospace": return Main.builtin.get(JSU.S("fonts/vera/VeraMono.ttf")); - case "ui.font.serif": return Main.builtin.get(JSU.S("fonts/vera/VeraSe.ttf")); + case "ui.font.sansserif": return Main.vera; + case "ui.font.monospace": return Main.vera; + case "ui.font.serif": return Main.vera; case "ui.browser": return METHOD; case "ui.mouse": return getSub(name); case "ui.mouse.button": @@ -132,7 +132,7 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { public void put(JS name, JS value) throws JSExn { //#switch(JSU.toString(name)) - case "thread": Scheduler.add((Callable)value); return; + case "thread": Platform.Scheduler.add((Callable)value); return; case "ui.clipboard": Platform.setClipBoard(JSU.toString(value)); return; case "ui.frame": Platform.createSurface((Box)value, true, true); return; case "ui.window": Platform.createSurface((Box)value, false, true); return; @@ -173,17 +173,17 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { return new JS.Clone((JS)args[0]); case "bless": return bless((JS)args[0]); case "ui.browser": Platform.newBrowserWindow(JSU.toString(args[0])); return null; - case "stream.unzip": return args[0] == null ? null : new Stream.Zip((Stream)args[0]); + case "stream.unzip": return args[0] == null ? null : new Fountain.Zip((Fountain)args[0]); //case "stream.uncab": return a == null ? null : new Stream.Cab(a); case "stream.cache": - try { return args[0] == null ? null : new Stream.CachedStream((Stream)args[0], "resources", true); } - catch (Stream.NotCacheableException e) { throw new JSExn("this resource cannot be cached"); } + //try { return args[0] == null ? null : new Fountain.CachedStream((Stream)args[0], "resources", true); } + //catch (Stream.NotCacheableException e) { throw new JSExn("this resource cannot be cached"); } case "stream.url": { String url = JSU.toString(args[0]); - if (url.startsWith("http://")) return new Stream.HTTP(url); - else if (url.startsWith("https://")) return new Stream.HTTP(url); - else if (url.startsWith("data:")) return new Stream.ByteArray(Encode.fromBase64(url.substring(5)), null); - else if (url.startsWith("utf8:")) return new Stream.ByteArray(url.substring(5).getBytes(), null); + if (url.startsWith("http://")) return new Fountain.HTTP(url); + else if (url.startsWith("https://")) return new Fountain.HTTP(url); + else if (url.startsWith("data:")) return new Fountain.ByteArray(Encode.fromBase64(url.substring(5)), null); + else if (url.startsWith("utf8:")) return new Fountain.ByteArray(url.substring(5).getBytes(), null); else if (url.startsWith("file:")) { // FIXME Platform.fileDialog(url.substring(5), false); @@ -201,13 +201,21 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { case "stream.parse.xml": if(args[0] == null) return null; new XMLHelper(args[1]).doParse(args[0]); return null; // FIXME backgrounding case "stream.parse.utf8": if(args[0] == null) return null; - try { return JSU.S(new String(InputStreamToByteArray.convert(args[0].getInputStream()))); } + try { return JSU.S(new String(InputStreamToByteArray.convert(JSU.getInputStream(args[0])))); } catch (Exception e) { Log.warn(this, e); } //#end break; case 2: //#switch(JSU.toString(method)) - case "stream.watch": return new Stream.ProgressWatcher((Stream)args[0], args[1]); + case "stream.watch": + final JS func = args[1]; + return new Fountain.ProgressWatcher((Fountain)args[0], + new Callable() { + public Object run(Object o) throws Exception { + JS[] args = (JS[])o; + return func.call(null, args); + } + }); case "regexp": return new JSRegexp(args[0], args[1]); //#end case 3: @@ -228,10 +236,10 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { } public JS url2res(String url) throws JSExn { - if (url.startsWith("http://")) return new Stream.HTTP(url); - else if (url.startsWith("https://")) return new Stream.HTTP(url); - else if (url.startsWith("data:")) return new Stream.ByteArray(Encode.fromBase64(url.substring(5)), null); - else if (url.startsWith("utf8:")) return new Stream.ByteArray(url.substring(5).getBytes(), null); + if (url.startsWith("http://")) return new Fountain.HTTP(url); + else if (url.startsWith("https://")) return new Fountain.HTTP(url); + else if (url.startsWith("data:")) return new Fountain.ByteArray(Encode.fromBase64(url.substring(5)), null); + else if (url.startsWith("utf8:")) return new Fountain.ByteArray(url.substring(5).getBytes(), null); else throw new JSExn("invalid resource specifier " + url); // FIXME support file:// via dialog boxes } @@ -242,7 +250,7 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { // FEATURE use a single sleeper thread new Thread() { public void run() { try { Thread.sleep(i); } catch (InterruptedException e) { } - Scheduler.add(callback); + Platform.Scheduler.add(callback); } }.start(); } catch (Pausable.NotPausableException npe) { throw new JSExn("you cannot sleep or yield in the foreground thread"); @@ -324,29 +332,29 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { callargs3[0] = JSU.S(c.getLocalName()); callargs3[1] = attrs; callargs3[2] = JSU.S(c.getUri()); - startElement.call(callargs3); + startElement.call(null, callargs3); } catch (JSExn jse) { throw new Wrapper(jse); } finally { callargs3[0] = callargs3[1] = callargs3[2] = null; } } public void endElement(Tree.Element c) throws XML.Exn { try { callargs2[0] = JSU.S(c.getLocalName()); callargs2[1] = JSU.S(c.getUri()); - endElement.call(callargs2); + endElement.call(null, callargs2); } catch (JSExn jse) { throw new Wrapper(jse); } finally { callargs2[0] = callargs2[1] = null; } } public void characters(char[] ch, int start, int length) throws XML.Exn { try { callargs1[0] = JSU.S(new String(ch, start, length)); - characters.call(callargs1); + characters.call(null, callargs1); } catch (JSExn jse) { throw new Wrapper(jse); } finally { callargs1[0] = null; } } public void whitespace(char[] ch, int start, int length) throws XML.Exn { try { callargs1[0] = JSU.S(new String(ch, start, length)); - whitespace.call(callargs1); + whitespace.call(null, callargs1); } catch (JSExn jse) { throw new Wrapper(jse); } finally { callargs1[0] = null; } } public void doParse(JS s) throws JSExn { try { - parse(new BufferedReader(new InputStreamReader(s.getInputStream()))); + parse(new BufferedReader(new InputStreamReader(JSU.getInputStream(s)))); } catch (Wrapper e) { throw e.wrapee; } catch (XML.Exn e) { @@ -362,40 +370,40 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { // FEATURE: move this into builtin.xwar public Blessing bless(JS b) throws JSExn { return new Ibex.Blessing(b, this, null, null); } // JS:FIXME: This doesn't properly handle traps - public static class Blessing extends JS.Obj { + public static class Blessing extends JS.Clone { private Ibex ibex; private Template t = null; public JS parentkey = null; public Blessing parent = null; public JS clonee; - private Basket.Map cache = new Basket.HashMap(); + private Basket.Map cache = new Basket.Hash(); public Blessing(JS clonee, Ibex ibex, Blessing parent, JS parentkey) throws JSExn { - this.clonee = clonee; this.ibex = ibex; this.parentkey = parentkey; this.parent = parent; } + super(clonee); this.clonee = clonee; this.ibex = ibex; this.parentkey = parentkey; this.parent = parent; } public JS get(JS key) throws JSExn { if (JSU.isString(key) && JSU.toString(key).equals("")) return getStatic(); if (cache.get(key) != null) return (JS)cache.get(key); - JS ret = new Blessing(clonee.get(key), ibex, this, key); + JS ret = new Blessing(super.get(key), ibex, this, key); cache.put(key, ret); return ret; } public static Blessing getBlessing(JS js) { - // CHECKME: is unclone() good enough or do we need getClonee() in Cloneable? - while (js instanceof JS.Cloneable && !(js instanceof Blessing)) js = js.unclone(); + while (js instanceof JS.Clone && !(js instanceof Blessing)) js = js.unclone(); if (!(js instanceof Blessing)) return null; return (Blessing)js; } // FEATURE: This is a gross hack public InputStream getImage() throws JSExn { try { - InputStream in = getInputStream(); + InputStream in = JSU.getInputStream(this); if (in != null) return in; } catch (IOException e) { /* DELIBERATE */ } String[] exts = new String[] { ".png", ".jpeg", ".gif" }; - for (int i=0; i < exts.length; i++) + for (int i=0; i < exts.length; i++) { try { - InputStream in = parent.get(JSU.S(JSU.toString(parentkey) + exts[i])).getInputStream(); + InputStream in = JSU.getInputStream(parent.get(JSU.S(JSU.toString(parentkey) + exts[i]))); if (in != null) return in; } catch (IOException f) { /* DELIBERATE */ } + } return null; } public JS getStatic() throws JSExn { @@ -416,7 +424,8 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { for(Blessing b = parent; b.parentkey != null; b = b.parent) s = JSU.str(b.parentkey) + "." + s; return s; } - public JS call(JS[] args) throws JSExn { + public JS call(JS method, JS[] args) throws JSExn { + if (method != null) return super.call(method, args); if (args.length != 1) throw new JSExn("can only call a template with one arg"); getStatic(); if (t == null) throw new JSExn("No such template " + JSU.str(parentkey)); @@ -430,8 +439,6 @@ public final class Ibex extends JS.Obj implements JS.Cloneable { if (t == null) throw new JSExn("No such template " + JSU.str(parentkey)); return t; } - // JS:FIXME: Blessing shouldn't need to roll its own JS.Clone implementation - public InputStream getInputStream() throws JSExn, IOException { return clonee.getInputStream(); } } }