X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FRes.java;h=53da6ffd090f82f19354de1883d1abb7f6d83e75;hb=d3ccf61eac01b22a5133b876b9e19581015de13e;hp=b2ae73b84fac15edda437122f66fdc31516b4943;hpb=67eeff476179a91ae930ea89cbecde22132ca532;p=org.ibex.core.git diff --git a/src/org/xwt/Res.java b/src/org/xwt/Res.java index b2ae73b..53da6ff 100644 --- a/src/org/xwt/Res.java +++ b/src/org/xwt/Res.java @@ -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! @@ -168,6 +171,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) {