2003/11/13 07:46:41
[org.ibex.core.git] / src / org / xwt / Res.java
index b2ae73b..df0813f 100644 (file)
@@ -19,8 +19,8 @@ public abstract class Res extends JS {
     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());
+        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);
     }
 
@@ -57,8 +57,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 +68,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 +83,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 +101,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 +110,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 +122,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!