// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
+
package org.xwt;
import java.io.*;
import java.util.*;
+import org.xwt.js.*;
import org.xwt.util.*;
-import org.mozilla.javascript.*;
/** A ByteStream encapsulates a <i>source</i> of a stream of bytes --
* currently either a local file or a Base64-encoded XMLRPC/SOAP element */
-
-public class ByteStream extends JSObject {
+public class ByteStream extends JS.Obj {
private byte[] bytes = null;
private File file = null;
private ByteStream() { setSeal(true); }
+ ByteStream(String filename) { this(); this.file = new File(filename); }
+ ByteStream(byte[] bytes) { this(); this.bytes = bytes; }
public String toString() { return "ByteStream, source=" + (file == null ? "memory" : file.getAbsolutePath()); }
- public String getClassName() { return "ByteStream"; }
-
- ByteStream(String filename) {
- this();
- this.file = new File(filename);
- }
- ByteStream(byte[] bytes) {
- this();
- this.bytes = bytes;
- }
-
- public Object get(String name, Scriptable start) {
- if (name == null) return null;
- else if (name.equals("getUTF")) return getUTF;
+ public Object get(Object name) {
+ if (name.equals("getUTF")) return getUTF;
else if (name.equals("getDOM")) return getDOM;
else if (name.equals("fileName")) return file == null ? null : file.getAbsolutePath();
else return null;
}
- /** Helper class for defining functions. */
- private abstract class JSFunction extends JSObject implements Function {
- JSFunction() { this.setSeal(true); }
- public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
- }
-
private class XMLHelper extends XML {
Vector obStack = new Vector();
public XMLHelper() { super(BUFFER_SIZE); }
public void startElement(XML.Element c) throws XML.SchemaException {
- JSObject o = new JSObject();
- o.put("$name", null, c.localName);
- for(int i=0; i<c.len; i++) o.put(c.keys[i], null, c.vals[i]);
- o.put("$numchildren", null, new Integer(0));
+ JS o = new JS.Obj();
+ o.put("$name", c.localName);
+ for(int i=0; i<c.len; i++) o.put(c.keys[i], c.vals[i]);
+ o.put("$numchildren", new Integer(0));
obStack.addElement(o);
}
public void endElement(XML.Element c) throws XML.SchemaException {
if (obStack.size() == 1) return;
- JSObject me = (JSObject)obStack.lastElement();
+ JS me = (JS)obStack.lastElement();
obStack.setSize(obStack.size() - 1);
- JSObject parent = (JSObject)obStack.lastElement();
- int numchildren = ((Integer)parent.get("$numchildren", null)).intValue();
- parent.put("$numchildren", null, new Integer(numchildren + 1));
- parent.put(numchildren, null, me);
+ JS parent = (JS)obStack.lastElement();
+ int numchildren = ((Integer)parent.get("$numchildren")).intValue();
+ parent.put("$numchildren", new Integer(numchildren + 1));
+ parent.put(new Integer(numchildren), me);
}
public void characters(char[] ch, int start, int length) throws XML.SchemaException {
String s = new String(ch, start, length);
- JSObject parent = (JSObject)obStack.lastElement();
- int numchildren = ((Integer)parent.get("$numchildren", null)).intValue();
- Object lastChild = parent.get(numchildren - 1, null);
+ JS parent = (JS)obStack.lastElement();
+ int numchildren = ((Integer)parent.get("$numchildren")).intValue();
+ Object lastChild = parent.get(new Integer(numchildren - 1));
if (lastChild instanceof String) {
- parent.put(numchildren - 1, null, lastChild + s);
+ parent.put(new Integer(numchildren - 1), lastChild + s);
} else {
- parent.put("$numchildren", null, new Integer(numchildren + 1));
- parent.put(numchildren, null, s);
+ parent.put("$numchildren", new Integer(numchildren + 1));
+ parent.put(new Integer(numchildren), s);
}
}
public void whitespace(char[] ch, int start, int length) {}
- public Scriptable doParse() throws JavaScriptException {
+ public JS doParse() throws JS.Exn {
try {
InputStream is = getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
parse(r);
} catch (XML.XMLException e) {
- throw new JavaScriptException("error parsing XML: " + e.toString());
+ throw new JS.Exn("error parsing XML: " + e.toString());
} catch (IOException e) {
if (Log.on) Log.log(ByteStream.class, "IO Exception while reading from file");
if (Log.on) Log.log(ByteStream.class, e);
- throw new JavaScriptException("error reading from ByteStream");
+ throw new JS.Exn("error reading from ByteStream");
}
- return obStack.size() >= 1 ? (Scriptable)obStack.elementAt(0) : null;
+ return obStack.size() >= 1 ? (JS)obStack.elementAt(0) : null;
}
}
os.close();
}
- private final JSFunction getDOM = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length != 0) return null;
+ private final JS.Function getDOM = new JS.Function() {
+ public Object _call(JS.Array args) throws JS.Exn {
+ if (args.length() != 0) return null;
return new XMLHelper().doParse();
}
};
- private final JSFunction getUTF = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length != 0) return null;
+ private final JS.Function getUTF = new JS.Function() {
+ public Object _call(JS.Array args) throws JS.Exn {
+ if (args.length() != 0) return null;
try {
CharArrayWriter caw = new CharArrayWriter();
InputStream is = getInputStream();
} catch (IOException e) {
if (Log.on) Log.log(ByteStream.class, "IO Exception while reading from file");
if (Log.on) Log.log(ByteStream.class, e);
- throw new JavaScriptException("error while reading from ByteStream");
+ throw new JS.Exn("error while reading from ByteStream");
}
}
};