1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
6 import java.util.zip.*;
9 import org.bouncycastle.util.encoders.Base64;
12 /** Base class for XWT resources */
13 public abstract class Res extends JS {
15 // Base Class //////////////////////////////////////////////////////////////////////
17 public String typeName() { return "resource"; }
19 /** so that we get the same subresource each time */
20 private Hash refCache = null;
22 public Template t = null;
24 public final InputStream getInputStream() throws IOException { return getInputStream(""); }
25 public abstract InputStream getInputStream(String path) throws IOException;
27 public Res addExtension(String extension) { return new Ref(this, extension); }
29 public Object get(Object key) {
31 Template t = Template.getTemplate(addExtension(".xwt"));
32 return t == null ? null : t.getStatic();
34 Object ret = refCache == null ? null : refCache.get(key);
35 if (ret != null) return ret;
36 ret = new Ref(this, key);
37 if (refCache == null) refCache = new Hash();
38 refCache.put(key, ret);
44 // Caching //////////////////////////////////////////////////////////////////////
46 public static class NotCacheableException extends Exception { }
47 public static NotCacheableException notCacheable = new NotCacheableException();
49 /** if it makes sense to cache a resource, the resource must return a unique key */
50 public String getCacheKey() throws NotCacheableException { throw notCacheable; }
52 /** subclass from this if you want a CachedInputStream for each path */
53 public static class CachedRes extends Res {
55 private boolean disk = false;
57 public String getCacheKey() throws NotCacheableException { return key; }
58 public String toString() { return key; }
59 private Hash cachedInputStreams = new Hash();
60 public CachedRes(Res p, String s, boolean d) throws NotCacheableException {
61 this.parent = p; this.disk = d; this.key = p.getCacheKey();
63 public InputStream getInputStream(String path) throws IOException {
64 CachedInputStream cis = (CachedInputStream)cachedInputStreams.get(path);
66 java.io.File f = null;
68 f = new java.io.File(System.getProperty("user.home") +
69 java.io.File.separatorChar + ".xwt" +
70 java.io.File.separatorChar + "caches" +
71 java.io.File.separatorChar +
72 new String(Base64.encode(key.getBytes())));
73 Log.log(this, "caching resource in " + f);
74 new java.io.File(f.getParent()).mkdirs();
75 if (f.exists()) return new FileInputStream(f);
77 cis = new CachedInputStream(parent.getInputStream(path), f);
78 cachedInputStreams.put(path, cis);
80 return cis.getInputStream();
85 // Useful Subclasses //////////////////////////////////////////////////////////////////////
87 /** HTTP or HTTPS resource */
88 public static class HTTP extends Res {
90 HTTP(String url) { while (url.endsWith("/")) url = url.substring(0, url.length() - 1); this.url = url; }
91 public String toString() { return url; }
92 public String getCacheKey() throws NotCacheableException { return url; }
93 public InputStream getInputStream(String path) throws IOException { return new org.xwt.HTTP(url + path).GET(); }
97 public static class ByteArray extends Res {
99 private String cacheKey = null;
100 ByteArray(byte[] bytes, String cacheKey) { this.bytes = bytes; this.cacheKey = cacheKey; }
101 public String toString() { return "byte[]"; }
102 public String getCacheKey() throws NotCacheableException { return cacheKey; }
103 public InputStream getInputStream(String path) throws IOException {
104 if (!"".equals(path)) throw new IOException("can't get subresources of a byte[] resource");
105 return new ByteArrayInputStream(bytes);
110 public static class File extends Res {
113 while (path.endsWith(java.io.File.separatorChar + "")) path = path.substring(0, path.length() - 1);
116 public String toString() { return "file:" + path; }
117 public String getCacheKey() throws NotCacheableException { throw notCacheable; } // already on the disk!
118 public InputStream getInputStream(String rest) throws IOException {
119 return new FileInputStream((path + rest).replace('/', java.io.File.separatorChar)); }
122 /** "unwrap" a Zip archive */
123 public static class Zip extends Res {
125 Zip(Res parent) { this.parent = parent; }
126 public String toString() { return parent.toString() + "!zip"; }
127 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!zip:"; }
128 public InputStream getInputStream(String path) throws IOException {
129 if (path.startsWith("/")) path = path.substring(1);
130 InputStream pis = parent.getInputStream();
131 ZipInputStream zis = new ZipInputStream(pis);
132 ZipEntry ze = zis.getNextEntry();
133 while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
134 if (ze == null) throw new IOException("requested file (" + path + ") not found in archive");
135 return new KnownLength.KnownLengthInputStream(zis, (int)ze.getSize());
139 /** "unwrap" a Cab archive */
140 public static class Cab extends Res {
142 Cab(Res parent) { this.parent = parent; }
143 public String toString() { return parent.toString() + "!cab"; }
144 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "!cab:"; }
145 public InputStream getInputStream(String path) throws IOException {
146 if (path.startsWith("/")) path = path.substring(1);
147 return new org.xwt.translators.MSPack(parent.getInputStream()).getInputStream(path);
151 /** the Builtin resource */
152 public static class Builtin extends Res {
153 public Builtin() { };
154 public String getCacheKey() throws NotCacheableException { throw notCacheable; } // not cacheable
155 public String toString() { return "builtin:"; }
156 public InputStream getInputStream(String path) throws IOException {
157 if (!path.equals("")) throw new IOException("the builtin resource has no subresources");
158 return Platform.getBuiltinInputStream();
162 /** what you get when you reference a subresource */
163 public static class Ref extends Res {
166 public String toString() { return parent.toString() + "/" + key; }
167 Ref(Res parent, Object key) { this.parent = parent; this.key = key; }
168 public String getCacheKey() throws NotCacheableException { return parent.getCacheKey() + "/" + key; }
169 public Res addExtension(String extension) {
170 return ((String)key).endsWith(extension) ? this : new Ref(parent, key + extension); }
171 public InputStream getInputStream(String path) throws IOException { return parent.getInputStream("/" + key + path); }
174 /** shadow resource which replaces the graft */
175 public static class ProgressWatcher extends Res {
178 ProgressWatcher(Res watchee, JSFunction callback) { this.watchee = watchee; this.callback = callback; }
179 public String toString() { return watchee.toString(); }
180 public String getCacheKey() throws NotCacheableException { return watchee.getCacheKey(); }
181 public InputStream getInputStream(String s) throws IOException {
182 final InputStream is = watchee.getInputStream(s);
183 return new FilterInputStream(is) {
184 int bytesDownloaded = 0;
185 public int read() throws IOException {
186 int ret = super.read();
187 if (ret != -1) bytesDownloaded++;
190 public int read(byte[] b, int off, int len) throws IOException {
191 int ret = super.read(b, off, len);
192 if (ret != 1) bytesDownloaded += ret;
193 Scheduler.add(new Scheduler.Task() { public void perform() throws Exception {
194 callback.call(N(bytesDownloaded),
195 N(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0), null, null, 2);