stream cleanup
[org.ibex.core.git] / src / org / ibex / core / Ibex.java
index 58969ae..66d9189 100644 (file)
@@ -15,11 +15,11 @@ public final class Ibex extends JS implements JS.Cloneable {
     // FIXME remove this
     private final JS rr;
 
-    public Ibex(Stream rr) { try { this.rr = bless(rr);} catch(JSExn e) { throw new Error("should never happen"); } }
+    public Ibex(Stream 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) {
-            if (permitAbsolute) return (Stream)url2res(str);
+            if (permitAbsolute) return url2res(str);
             throw new JSExn("absolute URL " + str + " not permitted here");
         }
         // root-relative
@@ -171,10 +171,10 @@ public final class Ibex extends JS implements JS.Cloneable {
                         return ((JS)a).jsclone();
                     case "bless": return bless((JS)a);
                     case "ui.browser": Platform.newBrowserWindow(JS.toString(a)); return null;
-                    case "stream.unzip": return new Stream.Zip((Stream)a);
-                    case "stream.uncab": return new Stream.Cab((Stream)a);
+                    case "stream.unzip": return a == null ? null : new Stream.Zip(a);
+                    case "stream.uncab": return a == null ? null : new Stream.Cab(a);
                     case "stream.cache":
-                        try { return new Stream.CachedStream((Stream)a, "resources", true); }
+                        try { return a == null ? null : new Stream.CachedStream(a, "resources", true); }
                         catch (Stream.NotCacheableException e) { throw new JSExn("this resource cannot be cached"); }
                     case "stream.url": {
                         String url = JS.toString(a);
@@ -196,22 +196,23 @@ public final class Ibex extends JS implements JS.Cloneable {
                     case "crypto.sha1": /* FEATURE */ return null;
                     case "crypto.rc4": /* FEATURE */ return null;
                     case "stream.parse.html": throw new JSExn("not implemented yet"); //return null;
-                    case "stream.parse.xml": new XMLHelper((JS)b).doParse((JS)a); return null;
+                    case "stream.parse.xml": if(a == null) return null; new XMLHelper(b).doParse(a); return null;
                         // FIXME backgrounding
-                    case "stream.parse.utf8": try { return JS.S(new String(InputStreamToByteArray.convert(Stream.getInputStream(a)))); }
+                    case "stream.parse.utf8": if(a == null) return null;
+                                              try { return JS.S(new String(InputStreamToByteArray.convert(a.getInputStream()))); }
                                               catch (Exception e) { Log.warn(this, e); }
                     //#end
                     break;
                 case 2:
                     //#switch(name)
-                    case "stream.watch": return new Stream.ProgressWatcher((Stream)a, (JS)b);
+                    case "stream.watch": return new Stream.ProgressWatcher(a, b);
                     case "regexp": return new JSRegexp(a, b);
                     //#end
                 case 3:
                     //#switch(name)
-                    case "ui.font.height": return N(Font.getFont((Stream)a, JS.toInt(b)).textheight(JS.toString(c)));
+                    case "ui.font.height": return N(Font.getFont(a, JS.toInt(b)).textheight(JS.toString(c)));
                     case "ui.font.wait": throw new Error("FIXME: ibex.ui.font.wait not implemented");
-                    case "ui.font.width": return N(Font.getFont((Stream)a, JS.toInt(b)).textwidth(JS.toString(c)));
+                    case "ui.font.width": return N(Font.getFont(a, JS.toInt(b)).textwidth(JS.toString(c)));
                     //#end
                     break;
             }
@@ -224,7 +225,7 @@ public final class Ibex extends JS implements JS.Cloneable {
         throw new JSExn("invalid number of arguments ("+nargs+") for ibex object method "+name+"()");
     }
 
-    public Stream url2res(String url) throws JSExn {
+    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(Base64.decode(url.substring(5)), null);
@@ -333,7 +334,7 @@ public final class Ibex extends JS implements JS.Cloneable {
 
         public void doParse(JS s) throws JSExn {
             try { 
-                parse(new BufferedReader(new InputStreamReader(Stream.getInputStream(s))));
+                parse(new BufferedReader(new InputStreamReader(s.getInputStream())));
             } catch (Wrapper e) {
                 throw e.wrapee;
             } catch (XML.Exn e) {
@@ -370,15 +371,16 @@ public final class Ibex extends JS implements JS.Cloneable {
             if (!(js instanceof Blessing)) return null;
             return (Blessing)js;
         }
+        // FEATURE: This is a gross hack
         public InputStream getImage() throws JSExn {
             try {
-                InputStream in = Stream.getInputStream(this);
+                InputStream in = getInputStream();
                 if (in != null) return in;
             } catch (IOException e) { /* DELIBERATE */ }
             String[] exts = new String[] { ".png", ".jpeg", ".gif" };
             for (int i=0; i < exts.length; i++)
                 try {
-                    InputStream in = Stream.getInputStream(parent.get(JS.S(JS.toString(parentkey) + exts[i])));
+                    InputStream in = parent.get(JS.S(JS.toString(parentkey) + exts[i])).getInputStream();
                     if (in != null) return in;
                 } catch (IOException f) { /* DELIBERATE */ }
             return null;
@@ -387,9 +389,8 @@ public final class Ibex extends JS implements JS.Cloneable {
             try {
                 if (t == null) {
                     // FEATURE: Might want to handle the ".t" part better
-                    JS res = (JS) parent.get(JS.S(JS.toString(parentkey) + ".t"));
-                    // FIXME: need a better description (Stream.toString())
-                    t = Template.buildTemplate(JS.toString(parentkey), res, ibex);
+                    JS res = parent.get(JS.S(JS.toString(parentkey) + ".t"));
+                    t = Template.buildTemplate(description(), res, ibex);
                 }
                 return t != null ? t.staticScope : null;
             } catch (Exception e) {
@@ -397,6 +398,11 @@ public final class Ibex extends JS implements JS.Cloneable {
                 return null;
             }
         }
+        private String description() {
+            String s = JS.debugToString(parentkey);
+            for(Blessing b = parent; b != null; b = b.parent) s = JS.debugToString(parentkey) + "." + s;
+            return s;
+        }
         public JS call(JS a, JS b, JS c, JS[] rest, int nargs) throws JSExn {
             if (nargs != 1) throw new JSExn("can only call a template with one arg");
             getStatic();