1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
7 // FIXME: ByteStream fileName property
8 /** base class for XWT resources */
9 public abstract class Res extends JS {
11 public String toString() { return "Resource, source=FIXME"; }
13 public final InputStream getInputStream() { return getInputStream(""); }
15 public Res graft(Object newResource) { throw new JS.Exn("cannot graft onto this resource"); }
16 public Object get(Object key) { return new Ref(this, key); }
17 public void put(Object key, Object val) { throw new JS.Exn("cannot put to a resource"); }
18 public Object[] keys() { throw new JS.Exn("cannot enumerate a resource"); }
20 public abstract InputStream getInputStream(String path) { return getInputStream(""); }
21 public abstract Res addExtension(String extension);
23 public static Res stringToRes(String url) {
24 if (url.indexOf('!') == -1)
25 return new Zip(stringToRes(url.substring(0, url.lastIndexOf('!'))),
26 url.substring(url.lastIndexOf('!') + 1));
27 if (url.startsWith("http://")) return new HTTP(url);
28 if (url.startsWith("https://")) return new HTTP(url);
29 throw new JS.Exn("invalid resource specifier");
32 /** HTTP or HTTPS resource */
33 public static class HTTP extends Res {
35 HTTP(String url) { this.url = url; }
36 public InputStream getInputStream(String path) { return new HTTP(url + path).GET(); }
39 /** wrap a Res around a preexisting InputStream */
40 public static class IS extends Res {
42 IS(InputStream parent) { this.parent = parent; }
43 public InputStream getInputStream() { return parent; }
44 public InputStream getInputStream(String path) {
45 if (!"".equals(path)) throw new JS.Exn("can't access subresources of IS");
50 /** "unwrap" a Zip archive */
51 public static class Zip extends Res {
53 Zip(Res parent) { this.parent = parent; }
54 public InputStream getInputStream(String path) {
55 ZipInputStream zis = new ZipInputStream(parent.getInputStream());
56 ZipEntry ze = zis.getNextEntry();
57 while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
58 if (ze == null) throw new JS.Exn("zip file not found in archive");
63 /** what you get when you reference a subresource */
64 public static class Ref extends Res {
67 Ref(Res parent, Object key) { this.parent = parent; this.key = key; }
68 public InputStream getInputStream(path) {
69 return parent.getInputStream("/" + key + path);
71 public Res graft(Object newResource) { return new Graft(parent, key, newResource); }
74 /** shadow resource which replaces the graft */
75 public static class Graft extends Res {
79 Graft(Res graftee, Object key, Object val) {
80 this.graftee = graftee; replaced_key = key; replaced_val = val; }
81 public boolean equals(Object o) { return (this == o || graftee.equals(o)); }
82 public Object get(Object key) {
83 return replaced_key.equals(key) ? replaced_val : graftee.get(key);
87 /////////////// bytestream
89 public Object callMethod(Object method, Array args, boolean checkOnly) throws JS.Exn {
90 if (method.equals("getUTF")) {
91 if (checkOnly) return Boolean.TRUE;
92 if (args.length() != 0) return null;
94 CharArrayWriter caw = new CharArrayWriter();
95 InputStream is = getInputStream();
96 BufferedReader r = new BufferedReader(new InputStreamReader(is));
97 char[] buf = new char[1024];
99 int numread = r.read(buf, 0, 1024);
100 if (numread == -1) break;
101 caw.write(buf, 0, numread);
103 return caw.toString();
104 } catch (IOException e) {
105 if (Log.on) Log.log(ByteStream.class, "IO Exception while reading from file");
106 if (Log.on) Log.log(ByteStream.class, e);
107 throw new JS.Exn("error while reading from ByteStream");
109 } else if (name.equals("getDOM")) {
110 if (checkOnly) return Boolean.TRUE;
111 if (args.length() != 0) return null;
112 return new XMLHelper().doParse();
114 if (checkOnly) return Boolean.FALSE;
118 private class XMLHelper extends XML {
119 Vector obStack = new Vector();
120 public XMLHelper() { super(BUFFER_SIZE); }
121 public void startElement(XML.Element c) throws XML.SchemaException {
123 o.put("$name", c.localName);
124 for(int i=0; i<c.len; i++) o.put(c.keys[i], c.vals[i]);
125 o.put("$numchildren", new Integer(0));
126 obStack.addElement(o);
128 public void endElement(XML.Element c) throws XML.SchemaException {
129 if (obStack.size() == 1) return;
130 JS me = (JS)obStack.lastElement();
131 obStack.setSize(obStack.size() - 1);
132 JS parent = (JS)obStack.lastElement();
133 int numchildren = ((Integer)parent.get("$numchildren")).intValue();
134 parent.put("$numchildren", new Integer(numchildren + 1));
135 parent.put(new Integer(numchildren), me);
137 public void characters(char[] ch, int start, int length) throws XML.SchemaException {
138 String s = new String(ch, start, length);
139 JS parent = (JS)obStack.lastElement();
140 int numchildren = ((Integer)parent.get("$numchildren")).intValue();
141 Object lastChild = parent.get(new Integer(numchildren - 1));
142 if (lastChild instanceof String) {
143 parent.put(new Integer(numchildren - 1), lastChild + s);
145 parent.put("$numchildren", new Integer(numchildren + 1));
146 parent.put(new Integer(numchildren), s);
149 public void whitespace(char[] ch, int start, int length) {}
150 public JS doParse() throws JS.Exn {
152 InputStream is = getInputStream();
153 BufferedReader r = new BufferedReader(new InputStreamReader(is));
155 } catch (XML.XMLException e) {
156 throw new JS.Exn("error parsing XML: " + e.toString());
157 } catch (IOException e) {
158 if (Log.on) Log.log(ByteStream.class, "IO Exception while reading from file");
159 if (Log.on) Log.log(ByteStream.class, e);
160 throw new JS.Exn("error reading from ByteStream");
162 return obStack.size() >= 1 ? (JS)obStack.elementAt(0) : null;
166 public void writeTo(OutputStream os) throws IOException {
167 InputStream is = getInputStream();
168 byte[] buf = new byte[1024];
170 int numread = is.read(buf, 0, 1024);
171 if (numread == -1) break;
172 if (Log.on) Log.log(this, "wrote " + numread + " bytes");
173 os.write(buf, 0, numread);
177 // we have to close this because flush() doesn't work on Win32-GCJ