2003/11/03 07:36:40
[org.ibex.core.git] / src / org / xwt / Res.java
index 954f290..3bf88ee 100644 (file)
@@ -11,7 +11,7 @@ import org.bouncycastle.util.encoders.Base64;
 /** base class for XWT resources */
 public abstract class Res extends JS {
 
-    public String getDescriptiveName() { return ""; }
+    public abstract String getDescriptiveName();
     public String typeName() { return "resource"; }
 
     /** cache of subresources so that the equality operator works on them */
@@ -87,13 +87,36 @@ public abstract class Res extends JS {
     }
 
     /** subclass from this if you want a CachedInputStream for each path */
-    public static abstract class CachedRes extends Res {
+    public static class CachedRes extends Res {
+        private Res parent;
+        private boolean disk = false;
+
+        // FIXME: security concern here
+        private String subdir = null;
+
+        public String getDescriptiveName() { return parent.getDescriptiveName(); }
         private Hash cachedInputStreams = new Hash();
-        abstract InputStream _getInputStream(String path) throws IOException;
-        public final InputStream getInputStream(String path) throws IOException {
+        public CachedRes(Res parent, String subdir, boolean disk) {
+            this.parent = parent; this.disk = disk; this.subdir = subdir;
+        }
+        public InputStream getInputStream(String path) throws IOException {
             CachedInputStream cis = (CachedInputStream)cachedInputStreams.get(path);
             if (cis == null) {
-                cis = new CachedInputStream(_getInputStream(path));
+                java.io.File f = null;
+                if (disk) {
+                    // FIXME ugly
+                    // FIXME need separate hash for disk/nondisk
+                    f = new java.io.File(System.getProperty("user.home") +
+                                         java.io.File.separatorChar + ".xwt" +
+                                         java.io.File.separatorChar + "caches" +
+                                         java.io.File.separatorChar + subdir +
+                                         java.io.File.separatorChar +
+                                         new String(Base64.encode(parent.getDescriptiveName().getBytes())));
+                    Log.log(this, "caching resource in " + f);
+                    new java.io.File(f.getParent()).mkdirs();
+                    if (f.exists()) return new FileInputStream(f);
+                }
+                cis = new CachedInputStream(parent.getInputStream(path), f);
                 cachedInputStreams.put(path, cis);
             }
             return cis.getInputStream();
@@ -101,11 +124,11 @@ public abstract class Res extends JS {
     }
 
     /** HTTP or HTTPS resource */
-    public static class HTTP extends CachedRes {
+    public static class HTTP extends Res {
         private String url;
         HTTP(String url) { this.url = url; }
         public String getDescriptiveName() { return url; }
-        public InputStream _getInputStream(String path) throws IOException {
+        public InputStream getInputStream(String path) throws IOException {
             return new org.xwt.HTTP(url + path).GET(); }
     }