2003/09/19 05:26:46
[org.ibex.core.git] / src / org / xwt / ByteStream.java
index cd3982a..69acc89 100644 (file)
@@ -1,94 +1,82 @@
-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2003 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.getAbsolutePath(); }
-    public String getClassName() { return "ByteStream"; }
-
-    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 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 void startElement(String name, String[] keys, Object[] vals, int line, int col) throws SAXException {
-            JSObject o = new JSObject();
-            o.put("$name", null, name);
-            for(int i=0; i<keys.length; i++) o.put(keys[i], null, vals[i]);
-            o.put("$numchildren", null, new Integer(0));
+        public XMLHelper() { super(BUFFER_SIZE); }
+
+        public void startElement(XML.Element c) throws XML.SchemaException {
+            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(String name, int line, int col) throws SAXException {
+        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 content(char[] content, int start, int length, int line, int col) throws SAXException {
-            String s = new String(content, start, length);
-            JSObject parent = (JSObject)obStack.lastElement();
-            int numchildren = ((Integer)parent.get("$numchildren", null)).intValue();
-            Object lastChild = parent.get(numchildren - 1, null);
+        public void characters(char[] ch, int start, int length) throws XML.SchemaException {
+            String s = new String(ch, start, length);
+            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 Scriptable doParse() throws JavaScriptException {
+        public void whitespace(char[] ch, int start, int length) {}
+
+        public JS doParse() throws JS.Exn {
             try { 
                 InputStream is = getInputStream();
                 BufferedReader r = new BufferedReader(new InputStreamReader(is));
                 parse(r);
-            } catch (XML.SAXException e) {
-                throw new JavaScriptException("error parsing XML: " + e.toString());
+            } catch (XML.XMLException e) {
+                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;
         }
     }
 
@@ -113,16 +101,16 @@ public class ByteStream extends JSObject {
         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.Callable getDOM = new JS.Callable() {
+            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.Callable getUTF = new JS.Callable() {
+            public Object call(JS.Array args) throws JS.Exn {
+                if (args.length() != 0) return null;
                 try {
                     CharArrayWriter caw = new CharArrayWriter();
                     InputStream is = getInputStream();
@@ -137,7 +125,7 @@ public class ByteStream extends JSObject {
                 } 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");
                 }
             }
         };