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 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);
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);
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);
74 parent.put("$numchildren", null, new Integer(numchildren + 1));
75 parent.put(numchildren, null, s);
79 public Scriptable doParse() throws JavaScriptException {
81 InputStream is = getInputStream();
82 BufferedReader r = new BufferedReader(new InputStreamReader(is));
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");
91 return obStack.size() >= 1 ? (Scriptable)obStack.elementAt(0) : null;
95 public InputStream getInputStream() throws IOException {
97 (InputStream)(new ByteArrayInputStream(bytes)) :
98 (InputStream)(new FileInputStream(file));
101 public void writeTo(OutputStream os) throws IOException {
102 InputStream is = getInputStream();
103 byte[] buf = new byte[1024];
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);
112 // we have to close this because flush() doesn't work on Win32-GCJ
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();
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;
127 CharArrayWriter caw = new CharArrayWriter();
128 InputStream is = getInputStream();
129 BufferedReader r = new BufferedReader(new InputStreamReader(is));
130 char[] buf = new char[1024];
132 int numread = r.read(buf, 0, 1024);
133 if (numread == -1) break;
134 caw.write(buf, 0, numread);
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");