2003/09/24 07:33:32
[org.ibex.core.git] / src / org / xwt / Res.java
index 3f70589..77b502b 100644 (file)
@@ -43,12 +43,13 @@ public abstract class Res extends JS {
         return ret;
     }
 
-    public static Res stringToRes(String url) {
+    public static Res stringToRes(String url) { return stringToRes(url, false); }
+    public static Res stringToRes(String url, boolean permitLocalFilesystem) {
         if (url.indexOf('!') != -1)
             return (Res)(new Zip(stringToRes(url.substring(0, url.lastIndexOf('!')))).get(url.substring(url.lastIndexOf('!') + 1)));
         if (url.startsWith("http://")) return new HTTP(url);
         if (url.startsWith("https://")) return new HTTP(url);
-        if (url.startsWith("file:")) return new File(url.substring(5));
+        if (url.startsWith("file:") && permitLocalFilesystem) return new File(url.substring(5));
         if (url.startsWith("cab:")) return new CAB(stringToRes(url.substring(4)));
         throw new JS.Exn("invalid resource specifier");
     }
@@ -70,7 +71,6 @@ public abstract class Res extends JS {
         }
     }
 
-    // FIXME: dangerous
     /** a file */
     public static class File extends Res {
         private String path;
@@ -94,10 +94,11 @@ public abstract class Res extends JS {
         private Res parent;
         Zip(Res parent) { this.parent = parent; }
         public InputStream getInputStream(String path) throws IOException {
+            if (path.startsWith("/")) path = path.substring(1);
             ZipInputStream zis = new ZipInputStream(parent.getInputStream());
             ZipEntry ze = zis.getNextEntry();
             while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
-            if (ze == null) throw new JS.Exn("zip file not found in archive");
+            if (ze == null) throw new JS.Exn("requested file not found in archive");
             return zis;
         }
     }
@@ -108,7 +109,7 @@ public abstract class Res extends JS {
         Object key;
         Ref(Res parent, Object key) { this.parent = parent; this.key = key; }
         public Res addExtension(String extension) {
-            return (key instanceof String && ((String)key).endsWith(extension)) ? this : new Ref(this, extension);
+            return (key instanceof String && ((String)key).endsWith(extension)) ? this : new Ref(parent, key + extension);
         }
         public InputStream getInputStream(String path) throws IOException {
             return parent.getInputStream("/" + key + path);