/** 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 */
}
/** 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();
}
/** 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(); }
}