2003/11/19 02:40:17
[org.ibex.core.git] / src / org / xwt / Res.java
index 53da6ff..ae2b50f 100644 (file)
@@ -12,19 +12,6 @@ import org.bouncycastle.util.encoders.Base64;
 /** Base class for XWT resources */
 public abstract class Res extends JS {
 
-
-    // Public Static //////////////////////////////////////////////////////////////////////
-
-    // FIXME: move to XWT.load()?
-    public static Res fromString(String url) {
-        if (url.startsWith("http://")) return new HTTP(url);
-        else if (url.startsWith("https://")) return new HTTP(url);
-        else if (url.startsWith("data:")) return new ByteArray(Base64.decode(url.substring(5)), null);
-        else if (url.startsWith("utf8:")) return new ByteArray(url.substring(5).getBytes(), null);
-        throw new JS.Exn("invalid resource specifier " + url);
-    }
-
-
     // Base Class //////////////////////////////////////////////////////////////////////
 
     public String typeName() { return "resource"; }
@@ -70,6 +57,7 @@ public abstract class Res extends JS {
         private boolean disk = false;
         private String key;
         public String getCacheKey() throws NotCacheableException { return key; }
+        public String toString() { return key; }
         private Hash cachedInputStreams = new Hash();
         public CachedRes(Res p, String s, boolean d) throws NotCacheableException {
             this.parent = p; this.disk = d; this.key = p.getCacheKey();
@@ -102,6 +90,7 @@ public abstract class Res extends JS {
     public static class HTTP extends Res {
         private String url;
         HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
+        public String toString() { return url; }
         public String getCacheKey() throws NotCacheableException { return url; }
         public InputStream getInputStream(String path) throws IOException { return new org.xwt.HTTP(url + path).GET(); }
     }
@@ -111,9 +100,10 @@ public abstract class Res extends JS {
         private byte[] bytes;
         private String cacheKey = null;
         ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
+        public String toString() { return "byte[]"; }
         public String getCacheKey() throws NotCacheableException { return cacheKey; }
         public InputStream getInputStream(String path) throws IOException {
-            if (!"".equals(path)) throw new JS.Exn("can't get subresources of a byte[] resource");
+            if (!"".equals(path)) throw new JSExn("can't get subresources of a byte[] resource");
             return new ByteArrayInputStream(bytes);
         }
     }
@@ -125,6 +115,7 @@ public abstract class Res extends JS {
             while (path.endsWith(java.io.File.separatorChar + "")) path = path.substring(0, path.length() - 1);
             this.path = path;
         }
+        public String toString() { return "file:" + path; }
         public String getCacheKey() throws NotCacheableException { throw notCacheable; }  // already on the disk!
         public InputStream getInputStream(String rest) throws IOException {
             return new FileInputStream((path + rest).replace('/', java.io.File.separatorChar)); }
@@ -134,6 +125,7 @@ public abstract class Res extends JS {
     public static class Zip extends Res {
         private Res parent;
         Zip(Res parent) { this.parent = parent; }
+        public String toString() { return parent.toString() + "!zip"; }
         public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
         public InputStream getInputStream(String path) throws IOException {
             if (path.startsWith("/")) path = path.substring(1);
@@ -141,7 +133,7 @@ public abstract class Res extends JS {
             ZipInputStream zis = new ZipInputStream(pis);
             ZipEntry ze = zis.getNextEntry();
             while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
-            if (ze == null) throw new JS.Exn("requested file (" + path + ") not found in archive");
+            if (ze == null) throw new JSExn("requested file (" + path + ") not found in archive");
             return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
         }
     }
@@ -150,6 +142,7 @@ public abstract class Res extends JS {
     public static class Cab extends Res {
         private Res parent;
         Cab(Res parent) { this.parent = parent; }
+        public String toString() { return parent.toString() + "!cab"; }
         public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
         public InputStream getInputStream(String path) throws IOException {
             if (path.startsWith("/")) path = path.substring(1);
@@ -159,12 +152,13 @@ public abstract class Res extends JS {
 
     /** the Builtin resource */
     public static class Builtin extends Res {
-       public Builtin() { };
-       public String getCacheKey() throws NotCacheableException { throw notCacheable; }    // not cacheable
-       public InputStream getInputStream(String path) throws IOException {
-           if (!path.equals("")) throw new IOException("the builtin resource has no subresources");
-           return Platform.getBuiltinInputStream();
-       }
+        public Builtin() { };
+        public String getCacheKey() throws NotCacheableException { throw notCacheable; }    // not cacheable
+        public String toString() { return "builtin:"; }
+        public InputStream getInputStream(String path) throws IOException {
+            if (!path.equals("")) throw new IOException("the builtin resource has no subresources");
+            return Platform.getBuiltinInputStream();
+        }
     }
 
     /** what you get when you reference a subresource */
@@ -184,6 +178,7 @@ public abstract class Res extends JS {
         final Res watchee;
         JSFunction callback;
         ProgressWatcher(Res watchee, JSFunction callback) { this.watchee = watchee; this.callback = callback; }
+        public String toString() { return watchee.toString(); }
         public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
         public InputStream getInputStream(String s) throws IOException {
             final InputStream is = watchee.getInputStream(s);
@@ -198,10 +193,8 @@ public abstract class Res extends JS {
                         int ret = super.read(b, off, len);
                         if (ret != 1) bytesDownloaded += ret;
                         Scheduler.add(new Scheduler.Task() { public void perform() {
-                            JSArray args = new JSArray();
-                            args.addElement(new Integer(bytesDownloaded));
-                            args.addElement(new Integer(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0));
-                            callback.call(args);
+                            callback.call(N(bytesDownloaded),
+                                          N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);
                         } });
                         return ret;
                     }