1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
6 import org.mozilla.javascript.*;
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 */
11 public class ByteStream extends JSObject {
13 private byte[] bytes = null;
14 private File file = null;
16 private ByteStream() { setSeal(true); }
18 public String toString() { return "ByteStream, source=" + (file == null ? "memory" : file.getAbsolutePath()); }
19 public String getClassName() { return "ByteStream"; }
21 ByteStream(String filename) {
23 this.file = new File(filename);
26 ByteStream(byte[] bytes) {
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();
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; }
45 private class XMLHelper extends XML {
46 Vector obStack = new Vector();
48 public XMLHelper() { super(BUFFER_SIZE); }
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);
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);
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);
76 parent.put("$numchildren", null, new Integer(numchildren + 1));
77 parent.put(numchildren, null, s);
81 public void whitespace(char[] ch, int start, int length) {}
83 public Scriptable doParse() throws JavaScriptException {
85 InputStream is = getInputStream();
86 BufferedReader r = new BufferedReader(new InputStreamReader(is));
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");
95 return obStack.size() >= 1 ? (Scriptable)obStack.elementAt(0) : null;
99 public InputStream getInputStream() throws IOException {
100 return file == null ?
101 (InputStream)(new ByteArrayInputStream(bytes)) :
102 (InputStream)(new FileInputStream(file));
105 public void writeTo(OutputStream os) throws IOException {
106 InputStream is = getInputStream();
107 byte[] buf = new byte[1024];
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);
116 // we have to close this because flush() doesn't work on Win32-GCJ
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();
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;
131 CharArrayWriter caw = new CharArrayWriter();
132 InputStream is = getInputStream();
133 BufferedReader r = new BufferedReader(new InputStreamReader(is));
134 char[] buf = new char[1024];
136 int numread = r.read(buf, 0, 1024);
137 if (numread == -1) break;
138 caw.write(buf, 0, numread);
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");