/** 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 */
public Res addExtension(String extension) { return new Ref(this, extension); }
public Object[] keys() { throw new JS.Exn("cannot enumerate a resource"); }
- public void put(Object key, Object val) { throw new JS.Exn("cannot put to a resource"); }
+ public Object put(Object key, Object val) { throw new JS.Exn("cannot put to a resource"); }
public Object get(Object key) {
if ("".equals(key)) {
Template t = Template.getTemplate(addExtension(".xwt"));
}
if (url.startsWith("http://")) return new HTTP(url);
if (url.startsWith("https://")) return new HTTP(url);
- if (url.startsWith("cab:")) return new CAB(stringToRes(url.substring(4)));
+ if (url.startsWith("cab:")) return new Cab(stringToRes(url.substring(4)));
if (url.startsWith("data:")) return new ByteArray(Base64.decode(url.substring(5)));
if (url.startsWith("utf8:")) return new ByteArray(url.substring(5).getBytes());
throw new JS.Exn("invalid resource specifier " + url);
}
/** 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(); }
}
}
}
+ /** "unwrap" a Cab archive */
+ public static class Cab extends Res {
+ private Res parent;
+ Cab(Res parent) { this.parent = parent; }
+ public String getDescriptiveName() { return "cab[" + parent.getDescriptiveName() + "]"; }
+ public InputStream getInputStream(String path) throws IOException {
+ // FIXME: knownlength
+ if (path.startsWith("/")) path = path.substring(1);
+ return new org.xwt.translators.MSPack(parent.getInputStream()).getInputStream(path);
+ }
+ }
+
/** the Builtin resource */
public static class Builtin extends Res {
public Builtin() { };
/** shadow resource which replaces the graft */
public static class ProgressWatcher extends Res {
final Res watchee;
- JS.CompiledFunction callback;
- ProgressWatcher(Res watchee, JS.CompiledFunction callback) { this.watchee = watchee; this.callback = callback; }
+ Function callback;
+ ProgressWatcher(Res watchee, Function callback) { this.watchee = watchee; this.callback = callback; }
public String getDescriptiveName() { return watchee.getDescriptiveName(); }
public InputStream getInputStream(String s) throws IOException {
final InputStream is = watchee.getInputStream(s);
JS.Array args = new JS.Array();
args.addElement(new Integer(bytesDownloaded));
args.addElement(new Integer(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0));
- new JS.Thread(callback, null, args).resume();
+ // FIXME
+ //new JS.Context(callback, null, args).resume();
} });
return ret;
}
}
}
- /** unpacks a Microsoft CAB file (possibly embedded in another file; we scan for 'MSCF' */
- public static class CAB extends Res {
- private Res parent;
- CAB(Res parent) { this.parent = parent; }
- private int swap_endian(int i) {
- return ((i & 0xff) << 24) | ((i & 0xff00) << 8) | ((i & 0xff0000) >>> 8) | (i >>> 24);
- }
- public InputStream getInputStream(String path) throws IOException {
- try {
- return org.xwt.util.CAB.getFileInputStream(parent.getInputStream(), 2, path);
- } catch (EOFException eof) {
- throw new JS.Exn("MSCF header tag not found in file");
- } catch (IOException ioe) {
- throw new JS.Exn("IOException while reading file");
- }
- }
- }
-
public Object callMethod(Object method, Array args, boolean checkOnly) throws JS.Exn {
if (method.equals("getUTF")) {
if (checkOnly) return Boolean.TRUE;