2003/09/19 05:26:47
[org.ibex.core.git] / src / org / xwt / Res.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3
4 import java.io.*;
5 import org.xwt.js.*;
6
7 // FIXME: ByteStream fileName property
8 /** base class for XWT resources */
9 public abstract class Res extends JS {
10
11     public String toString() { return "Resource, source=FIXME"; }
12
13     public final InputStream getInputStream() { return getInputStream(""); }
14
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"); } 
19
20     public abstract InputStream getInputStream(String path) { return getInputStream(""); }
21     public abstract Res addExtension(String extension);
22
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");
30     }
31
32     /** HTTP or HTTPS resource */
33     public static class HTTP extends Res {
34         private String url;
35         HTTP(String url) { this.url = url; }
36         public InputStream getInputStream(String path) { return new HTTP(url + path).GET(); }
37     }
38
39     /** wrap a Res around a preexisting InputStream */
40     public static class IS extends Res {
41         InputStream parent;
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");
46             return parent;
47         }
48     }
49
50     /** "unwrap" a Zip archive */
51     public static class Zip extends Res {
52         private Res parent;
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");
59             return zis;
60         }
61     }
62
63     /** what you get when you reference a subresource */
64     public static class Ref extends Res {
65         Res parent;
66         Object key;
67         Ref(Res parent, Object key) { this.parent = parent; this.key = key; }
68         public InputStream getInputStream(path) {
69             return parent.getInputStream("/" + key + path);
70         }
71         public Res graft(Object newResource) { return new Graft(parent, key, newResource); }
72     }
73
74     /** shadow resource which replaces the graft */
75     public static class Graft extends Res {
76         Res graftee;
77         Object replaced_key;
78         Object replaced_val;
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);
84         }
85     }
86
87     /////////////// bytestream
88
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;
93             try {
94                 CharArrayWriter caw = new CharArrayWriter();
95                 InputStream is = getInputStream();
96                 BufferedReader r = new BufferedReader(new InputStreamReader(is));
97                 char[] buf = new char[1024];
98                 while(true) {
99                     int numread = r.read(buf, 0, 1024);
100                     if (numread == -1) break;
101                     caw.write(buf, 0, numread);
102                 }
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");
108             }
109         } else if (name.equals("getDOM")) {
110             if (checkOnly) return Boolean.TRUE;
111             if (args.length() != 0) return null;
112             return new XMLHelper().doParse();
113         }
114         if (checkOnly) return Boolean.FALSE;
115         return null;
116     }
117
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 {
122             JS o = new JS.Obj();
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);
127         }
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);
136         }
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);
144             } else {
145                 parent.put("$numchildren", new Integer(numchildren + 1));
146                 parent.put(new Integer(numchildren), s);
147             }
148         }
149         public void whitespace(char[] ch, int start, int length) {}
150         public JS doParse() throws JS.Exn {
151             try { 
152                 InputStream is = getInputStream();
153                 BufferedReader r = new BufferedReader(new InputStreamReader(is));
154                 parse(r);
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");
161             }
162             return obStack.size() >= 1 ? (JS)obStack.elementAt(0) : null;
163         }
164     }
165
166     public void writeTo(OutputStream os) throws IOException {
167         InputStream is = getInputStream();
168         byte[] buf = new byte[1024];
169         while(true) {
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);
174         }
175         os.flush();
176
177         // we have to close this because flush() doesn't work on Win32-GCJ
178         os.close();
179     }
180 }