2003/11/17 01:53:25
[org.ibex.core.git] / src / org / xwt / Res.java
index b2ae73b..0652e09 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)));
-        else if (url.startsWith("utf8:")) return new ByteArray(url.substring(5).getBytes());
-        throw new JS.Exn("invalid resource specifier " + url);
-    }
-
-
     // Base Class //////////////////////////////////////////////////////////////////////
 
     public String typeName() { return "resource"; }
@@ -57,8 +44,8 @@ public abstract class Res extends JS {
 
     // Caching //////////////////////////////////////////////////////////////////////
 
-    private static class NotCacheableException extends Exception { }
-    private static NotCacheableException notCacheable = new NotCacheableException();
+    public static class NotCacheableException extends Exception { }
+    public static NotCacheableException notCacheable = new NotCacheableException();
 
     /** if it makes sense to cache a resource, the resource must return a unique key */
     public String getCacheKey() throws NotCacheableException { throw notCacheable; }
@@ -68,9 +55,12 @@ public abstract class Res extends JS {
     public static class CachedRes extends Res {
         private Res parent;
         private boolean disk = false;
-        public String getCacheKey() throws NotCacheableException { return parent.getCacheKey(); }
+        private String key;
+        public String getCacheKey() throws NotCacheableException { return key; }
         private Hash cachedInputStreams = new Hash();
-        public CachedRes(Res p, String s, boolean d) { this.parent = p; this.disk = d; }
+        public CachedRes(Res p, String s, boolean d) throws NotCacheableException {
+            this.parent = p; this.disk = d; this.key = p.getCacheKey();
+        }
         public InputStream getInputStream(String path) throws IOException {
             CachedInputStream cis = (CachedInputStream)cachedInputStreams.get(path);
             if (cis == null) {
@@ -80,7 +70,7 @@ public abstract class Res extends JS {
                                          java.io.File.separatorChar + ".xwt" +
                                          java.io.File.separatorChar + "caches" +
                                          java.io.File.separatorChar +
-                                         new String(Base64.encode(parent.getCacheKey().getBytes())));
+                                         new String(Base64.encode(key.getBytes())));
                     Log.log(this, "caching resource in " + f);
                     new java.io.File(f.getParent()).mkdirs();
                     if (f.exists()) return new FileInputStream(f);
@@ -98,7 +88,7 @@ public abstract class Res extends JS {
     /** HTTP or HTTPS resource */
     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; }
+        HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
         public String getCacheKey() throws NotCacheableException { return url; }
         public InputStream getInputStream(String path) throws IOException { return new org.xwt.HTTP(url + path).GET(); }
     }
@@ -107,7 +97,7 @@ public abstract class Res extends JS {
     public static class ByteArray extends Res {
         private byte[] bytes;
         private String cacheKey = null;
-        ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }}
+        ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
         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");
@@ -119,7 +109,7 @@ public abstract class Res extends JS {
     public static class File extends Res {
         private String path;
         File(String path) {
-            while (path.endsWith(java.io.File.separatorChar)) path = path.substring(0, path.length() - 1);
+            while (path.endsWith(java.io.File.separatorChar + "")) path = path.substring(0, path.length() - 1);
             this.path = path;
         }
         public String getCacheKey() throws NotCacheableException { throw notCacheable; }  // already on the disk!
@@ -168,6 +158,7 @@ public abstract class Res extends JS {
     public static class Ref extends Res {
         Res parent;
         Object key;
+        public String toString() { return parent.toString() + "/" + key; }
         Ref(Res parent, Object key) { this.parent = parent; this.key = key; }
         public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "/" + key; }
         public Res addExtension(String extension) {
@@ -194,10 +185,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;
                     }