1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
6 import java.util.zip.*;
9 import org.bouncycastle.util.encoders.Base64;
11 /** base class for XWT resources */
12 public abstract class Res extends JS {
14 public String getDescriptiveName() { return ""; }
16 /** cache of subresources so that the equality operator works on them */
17 private Hash refCache = null;
19 public Template t = null;
21 public Res getParent() { return null; }
23 /** returns an InputStream containing the Resource's contents */
24 public InputStream getInputStream() throws IOException { return getInputStream(""); }
25 public abstract InputStream getInputStream(String path) throws IOException;
27 /** graft newResource in place of this resource on its parent */
28 public Res graft(Object newResource) { throw new JS.Exn("cannot graft onto this resource"); }
30 /** if the path of this resource does not end with extension, return a new one wit it appended */
31 public Res addExtension(String extension) { return new Ref(this, extension); }
33 public Object[] keys() { throw new JS.Exn("cannot enumerate a resource"); }
34 public void put(Object key, Object val) { throw new JS.Exn("cannot put to a resource"); }
35 public Object get(Object key) {
37 Template t = Template.getTemplate(addExtension(".xwt"));
38 return t == null ? null : t.getStatic();
40 Object ret = refCache == null ? null : refCache.get(key);
41 if (ret != null) return ret;
42 ret = new Ref(this, key);
43 if (refCache == null) refCache = new Hash();
44 refCache.put(key, ret);
48 public static Res stringToRes(String url) { return stringToRes(url, false); }
49 public static Res stringToRes(String url, boolean permitLocalFilesystem) {
50 if (url.indexOf('!') != -1)
51 return (Res)(new Zip(stringToRes(url.substring(0, url.lastIndexOf('!')))).get(url.substring(url.lastIndexOf('!') + 1)));
52 if (url.startsWith("http://")) return new HTTP(url);
53 if (url.startsWith("https://")) return new HTTP(url);
54 if (url.startsWith("file:") && permitLocalFilesystem) return new File(url.substring(5));
55 if (url.startsWith("cab:")) return new CAB(stringToRes(url.substring(4)));
56 if (url.startsWith("data:")) return new ByteArray(Base64.decode(url.substring(5)));
57 if (url.startsWith("utf8:")) return new ByteArray(url.substring(5).getBytes());
58 throw new JS.Exn("invalid resource specifier " + url);
61 /** HTTP or HTTPS resource */
62 public static class HTTP extends Res {
64 HTTP(String url) { this.url = url; }
65 public InputStream getInputStream(String path) throws IOException { return new org.xwt.HTTP(url + path).GET(); }
69 public static class ByteArray extends Res {
71 ByteArray(byte[] bytes) { this.bytes = bytes; }
72 public InputStream getInputStream(String path) throws IOException {
73 if (!"".equals(path)) throw new JS.Exn("can't get subresources of a byte[] resource");
74 return new ByteArrayInputStream(bytes);
79 public static class File extends Res {
81 File(String path) { this.path = path; }
82 public InputStream getInputStream(String rest) throws IOException {
83 return new FileInputStream((path + rest).replace('/', java.io.File.separatorChar)); }
86 /** wrap a Res around a preexisting InputStream */
87 public static class IS extends Res {
89 IS(InputStream parent) { this.parent = parent; }
90 public InputStream getInputStream(String path) {
91 if (!"".equals(path)) throw new JS.Exn("can't access subresources of IS");
96 /** "unwrap" a Zip archive */
97 public static class Zip extends Res {
99 Zip(Res parent) { this.parent = parent; }
100 public InputStream getInputStream(String path) throws IOException {
101 if (path.startsWith("/")) path = path.substring(1);
102 ZipInputStream zis = new ZipInputStream(parent.getInputStream());
103 ZipEntry ze = zis.getNextEntry();
104 while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
105 if (ze == null) throw new JS.Exn("requested file not found in archive");
110 /** what you get when you reference a subresource */
111 public static class Ref extends Res {
114 Ref(Res parent, Object key) { this.parent = parent; this.key = key; }
115 public String getDescriptiveName() {
116 String pdn = parent.getDescriptiveName();
117 return pdn.equals("") ? key.toString() : (pdn + "." + key.toString());
119 public Res addExtension(String extension) {
120 return (key instanceof String && ((String)key).endsWith(extension)) ? this : new Ref(parent, key + extension);
122 public InputStream getInputStream(String path) throws IOException {
123 return parent.getInputStream("/" + key + path);
125 public Res getParent() { return parent; }
126 public Res graft(Object newResource) { return new Graft(parent, key, newResource); }
129 /** shadow resource which replaces the graft */
130 public static class Graft extends Res {
134 Graft(Res graftee, Object key, Object val) { this.graftee = graftee; replaced_key = key; replaced_val = val; }
135 public boolean equals(Object o) { return (this == o || graftee.equals(o)); }
136 public int hashCode() { return graftee.hashCode(); }
137 public InputStream getInputStream(String s) throws IOException { return graftee.getInputStream(s); }
138 public Object get(Object key) { return replaced_key.equals(key) ? replaced_val : graftee.get(key); }
139 public String getDescriptiveName() { return graftee.getDescriptiveName(); }
140 public Res getParent() { return graftee.getParent(); }
143 /** shadow resource which replaces the graft */
144 public static class ProgressWatcher extends Res {
146 JS.Callable callback;
147 ProgressWatcher(Res watchee, JS.Callable callback) { this.watchee = watchee; this.callback = callback; }
148 public InputStream getInputStream(String s) throws IOException {
149 return new FilterInputStream(watchee.getInputStream(s)) {
150 int bytesDownloaded = 0;
151 public int read() throws IOException {
152 int ret = super.read();
153 if (ret != -1) bytesDownloaded++;
156 public int read(byte[] b, int off, int len) throws IOException {
157 int ret = super.read(b, off, len);
158 if (ret != 1) bytesDownloaded += ret;
159 ThreadMessage.newthread(new JS.Callable() { public Object call(JS.Array a) {
160 JS.Array args = new JS.Array();
161 args.addElement(new Integer(bytesDownloaded));
171 /** unpacks a Microsoft CAB file (possibly embedded in another file; we scan for 'MSCF' */
172 public static class CAB extends Res {
174 CAB(Res parent) { this.parent = parent; }
175 private int swap_endian(int i) {
176 return ((i & 0xff) << 24) | ((i & 0xff00) << 8) | ((i & 0xff0000) >>> 8) | (i >>> 24);
178 public InputStream getInputStream(String path) throws IOException {
180 return org.xwt.util.CAB.getFileInputStream(parent.getInputStream(), 2, path);
181 } catch (EOFException eof) {
182 throw new JS.Exn("MSCF header tag not found in file");
183 } catch (IOException ioe) {
184 throw new JS.Exn("IOException while reading file");
189 public Object callMethod(Object method, Array args, boolean checkOnly) throws JS.Exn {
190 if (method.equals("getUTF")) {
191 if (checkOnly) return Boolean.TRUE;
192 if (args.length() != 0) return null;
194 CharArrayWriter caw = new CharArrayWriter();
195 InputStream is = getInputStream();
196 BufferedReader r = new BufferedReader(new InputStreamReader(is));
197 char[] buf = new char[1024];
199 int numread = r.read(buf, 0, 1024);
200 if (numread == -1) break;
201 caw.write(buf, 0, numread);
203 return caw.toString();
204 } catch (IOException e) {
205 if (Log.on) Log.log(Res.class, "IO Exception while reading from file");
206 if (Log.on) Log.log(Res.class, e);
207 throw new JS.Exn("error while reading from Resource");
209 } else if (method.equals("getDOM")) {
210 if (checkOnly) return Boolean.TRUE;
211 if (args.length() != 0) return null;
212 return new XMLHelper().doParse();
214 if (checkOnly) return Boolean.FALSE;
218 private class XMLHelper extends XML {
219 Vector obStack = new Vector();
220 public XMLHelper() { super(BUFFER_SIZE); }
221 public void startElement(XML.Element c) throws XML.SchemaException {
223 o.put("$name", c.localName);
224 for(int i=0; i<c.len; i++) o.put(c.keys[i], c.vals[i]);
225 o.put("$numchildren", new Integer(0));
226 obStack.addElement(o);
228 public void endElement(XML.Element c) throws XML.SchemaException {
229 if (obStack.size() == 1) return;
230 JS me = (JS)obStack.lastElement();
231 obStack.setSize(obStack.size() - 1);
232 JS parent = (JS)obStack.lastElement();
233 int numchildren = ((Integer)parent.get("$numchildren")).intValue();
234 parent.put("$numchildren", new Integer(numchildren + 1));
235 parent.put(new Integer(numchildren), me);
237 public void characters(char[] ch, int start, int length) throws XML.SchemaException {
238 String s = new String(ch, start, length);
239 JS parent = (JS)obStack.lastElement();
240 int numchildren = ((Integer)parent.get("$numchildren")).intValue();
241 Object lastChild = parent.get(new Integer(numchildren - 1));
242 if (lastChild instanceof String) {
243 parent.put(new Integer(numchildren - 1), lastChild + s);
245 parent.put("$numchildren", new Integer(numchildren + 1));
246 parent.put(new Integer(numchildren), s);
249 public void whitespace(char[] ch, int start, int length) {}
250 public JS doParse() throws JS.Exn {
252 InputStream is = getInputStream();
253 BufferedReader r = new BufferedReader(new InputStreamReader(is));
255 } catch (XML.XMLException e) {
256 throw new JS.Exn("error parsing XML: " + e.toString());
257 } catch (IOException e) {
258 if (Log.on) Log.log(this, "IO Exception while reading from file");
259 if (Log.on) Log.log(this, e);
260 throw new JS.Exn("error reading from Resource");
262 return obStack.size() >= 1 ? (JS)obStack.elementAt(0) : null;
266 public void writeTo(OutputStream os) throws IOException {
267 InputStream is = getInputStream();
268 byte[] buf = new byte[1024];
270 int numread = is.read(buf, 0, 1024);
271 if (numread == -1) break;
272 if (Log.on) Log.log(this, "wrote " + numread + " bytes");
273 os.write(buf, 0, numread);
277 // we have to close this because flush() doesn't work on Win32-GCJ