2002/10/03 23:40:38
[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 void startElement(String name, String[] keys, Object[] vals, int line, int col) throws SAXException {
49             JSObject o = new JSObject();
50             o.put("$name", null, name);
51             for(int i=0; i<keys.length; i++) o.put(keys[i], null, vals[i]);
52             o.put("$numchildren", null, new Integer(0));
53             obStack.addElement(o);
54         }
55
56         public void endElement(String name, int line, int col) throws SAXException {
57             if (obStack.size() == 1) return;
58             JSObject me = (JSObject)obStack.lastElement();
59             obStack.setSize(obStack.size() - 1);
60             JSObject parent = (JSObject)obStack.lastElement();
61             int numchildren = ((Integer)parent.get("$numchildren", null)).intValue();
62             parent.put("$numchildren", null, new Integer(numchildren + 1));
63             parent.put(numchildren, null, me);
64         }
65
66         public void content(char[] content, int start, int length, int line, int col) throws SAXException {
67             String s = new String(content, start, length);
68             JSObject parent = (JSObject)obStack.lastElement();
69             int numchildren = ((Integer)parent.get("$numchildren", null)).intValue();
70             Object lastChild = parent.get(numchildren - 1, null);
71             if (lastChild instanceof String) {
72                 parent.put(numchildren - 1, null, lastChild + s);
73             } else {
74                 parent.put("$numchildren", null, new Integer(numchildren + 1));
75                 parent.put(numchildren, null, s);
76             }
77         }
78
79         public Scriptable doParse() throws JavaScriptException {
80             try { 
81                 InputStream is = getInputStream();
82                 BufferedReader r = new BufferedReader(new InputStreamReader(is));
83                 parse(r);
84             } catch (XML.SAXException e) {
85                 throw new JavaScriptException("error parsing XML: " + e.toString());
86             } catch (IOException e) {
87                 if (Log.on) Log.log(ByteStream.class, "IO Exception while reading from file");
88                 if (Log.on) Log.log(ByteStream.class, e);
89                 throw new JavaScriptException("error reading from ByteStream");
90             }
91             return obStack.size() >= 1 ? (Scriptable)obStack.elementAt(0) : null;
92         }
93     }
94
95     public InputStream getInputStream() throws IOException {
96         return file == null ?
97             (InputStream)(new ByteArrayInputStream(bytes)) :
98             (InputStream)(new FileInputStream(file));
99     }
100
101     public void writeTo(OutputStream os) throws IOException {
102         InputStream is = getInputStream();
103         byte[] buf = new byte[1024];
104         while(true) {
105             int numread = is.read(buf, 0, 1024);
106             if (numread == -1) break;
107             if (Log.on) Log.log(this, "wrote " + numread + " bytes");
108             os.write(buf, 0, numread);
109         }
110         os.flush();
111
112         // we have to close this because flush() doesn't work on Win32-GCJ
113         os.close();
114     }
115
116     private final JSFunction getDOM = new JSFunction() {
117             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
118                 if (args.length != 0) return null;
119                 return new XMLHelper().doParse();
120             }
121         };
122
123     private final JSFunction getUTF = new JSFunction() {
124             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
125                 if (args.length != 0) return null;
126                 try {
127                     CharArrayWriter caw = new CharArrayWriter();
128                     InputStream is = getInputStream();
129                     BufferedReader r = new BufferedReader(new InputStreamReader(is));
130                     char[] buf = new char[1024];
131                     while(true) {
132                         int numread = r.read(buf, 0, 1024);
133                         if (numread == -1) break;
134                         caw.write(buf, 0, numread);
135                     }
136                     return caw.toString();
137                 } catch (IOException e) {
138                     if (Log.on) Log.log(ByteStream.class, "IO Exception while reading from file");
139                     if (Log.on) Log.log(ByteStream.class, e);
140                     throw new JavaScriptException("error while reading from ByteStream");
141                 }
142             }
143         };
144
145 }
146