2003/04/24 14:33:27
[org.ibex.core.git] / src / org / xwt / ByteStream.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3 import java.io.*;
4 import java.util.*;
5 import org.xwt.util.*;
6 import org.mozilla.javascript.*;
7
8 /** A ByteStream encapsulates a <i>source</i> of a stream of bytes --
9  *  currently either a local file or a Base64-encoded XMLRPC/SOAP element */
10
11 public class ByteStream extends JSObject {
12
13     private byte[] bytes = null;
14     private File file = null;
15
16     private ByteStream() { setSeal(true); }
17
18     public String toString() { return "ByteStream, source=" + (file == null ? "memory" : file.getAbsolutePath()); }
19     public String getClassName() { return "ByteStream"; }
20
21     ByteStream(String filename) {
22         this();
23         this.file = new File(filename);
24     }
25
26     ByteStream(byte[] bytes) {
27         this();
28         this.bytes = bytes;
29     }
30
31     public Object get(String name, Scriptable start) {
32         if (name == null) return null;
33         else if (name.equals("getUTF")) return getUTF;
34         else if (name.equals("getDOM")) return getDOM;
35         else if (name.equals("fileName")) return file == null ? null : file.getAbsolutePath();
36         else return null;
37     }
38
39     /** Helper class for defining functions. */
40     private abstract class JSFunction extends JSObject implements Function {
41         JSFunction() { this.setSeal(true); }
42         public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
43     }
44
45     private class XMLHelper extends XML {
46         Vector obStack = new Vector();
47
48         public XMLHelper() { super(BUFFER_SIZE); }
49
50         public void startElement(XML.Element c) throws XML.SchemaException {
51             JSObject o = new JSObject();
52             o.put("$name", null, c.localName);
53             for(int i=0; i<c.len; i++) o.put(c.keys[i], null, c.vals[i]);
54             o.put("$numchildren", null, new Integer(0));
55             obStack.addElement(o);
56         }
57
58         public void endElement(XML.Element c) throws XML.SchemaException {
59             if (obStack.size() == 1) return;
60             JSObject me = (JSObject)obStack.lastElement();
61             obStack.setSize(obStack.size() - 1);
62             JSObject parent = (JSObject)obStack.lastElement();
63             int numchildren = ((Integer)parent.get("$numchildren", null)).intValue();
64             parent.put("$numchildren", null, new Integer(numchildren + 1));
65             parent.put(numchildren, null, me);
66         }
67
68         public void characters(char[] ch, int start, int length) throws XML.SchemaException {
69             String s = new String(ch, start, length);
70             JSObject parent = (JSObject)obStack.lastElement();
71             int numchildren = ((Integer)parent.get("$numchildren", null)).intValue();
72             Object lastChild = parent.get(numchildren - 1, null);
73             if (lastChild instanceof String) {
74                 parent.put(numchildren - 1, null, lastChild + s);
75             } else {
76                 parent.put("$numchildren", null, new Integer(numchildren + 1));
77                 parent.put(numchildren, null, s);
78             }
79         }
80
81         public void whitespace(char[] ch, int start, int length) {}
82
83         public Scriptable doParse() throws JavaScriptException {
84             try { 
85                 InputStream is = getInputStream();
86                 BufferedReader r = new BufferedReader(new InputStreamReader(is));
87                 parse(r);
88             } catch (XML.XMLException e) {
89                 throw new JavaScriptException("error parsing XML: " + e.toString());
90             } catch (IOException e) {
91                 if (Log.on) Log.log(ByteStream.class, "IO Exception while reading from file");
92                 if (Log.on) Log.log(ByteStream.class, e);
93                 throw new JavaScriptException("error reading from ByteStream");
94             }
95             return obStack.size() >= 1 ? (Scriptable)obStack.elementAt(0) : null;
96         }
97     }
98
99     public InputStream getInputStream() throws IOException {
100         return file == null ?
101             (InputStream)(new ByteArrayInputStream(bytes)) :
102             (InputStream)(new FileInputStream(file));
103     }
104
105     public void writeTo(OutputStream os) throws IOException {
106         InputStream is = getInputStream();
107         byte[] buf = new byte[1024];
108         while(true) {
109             int numread = is.read(buf, 0, 1024);
110             if (numread == -1) break;
111             if (Log.on) Log.log(this, "wrote " + numread + " bytes");
112             os.write(buf, 0, numread);
113         }
114         os.flush();
115
116         // we have to close this because flush() doesn't work on Win32-GCJ
117         os.close();
118     }
119
120     private final JSFunction getDOM = new JSFunction() {
121             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
122                 if (args.length != 0) return null;
123                 return new XMLHelper().doParse();
124             }
125         };
126
127     private final JSFunction getUTF = new JSFunction() {
128             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
129                 if (args.length != 0) return null;
130                 try {
131                     CharArrayWriter caw = new CharArrayWriter();
132                     InputStream is = getInputStream();
133                     BufferedReader r = new BufferedReader(new InputStreamReader(is));
134                     char[] buf = new char[1024];
135                     while(true) {
136                         int numread = r.read(buf, 0, 1024);
137                         if (numread == -1) break;
138                         caw.write(buf, 0, numread);
139                     }
140                     return caw.toString();
141                 } catch (IOException e) {
142                     if (Log.on) Log.log(ByteStream.class, "IO Exception while reading from file");
143                     if (Log.on) Log.log(ByteStream.class, e);
144                     throw new JavaScriptException("error while reading from ByteStream");
145                 }
146             }
147         };
148
149 }
150