X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FXMLRPC.java;h=cdf478969835fcbb377093734e5f482acc200abd;hb=f3ad8b6cba43f3c5364dc2cd9f1d050c1f48f167;hp=6f50328c181412f56f0e627f3ebfb0800c609b54;hpb=44ba228826e0d6a6c4ad3ed0072b90e05622ee21;p=org.ibex.js.git diff --git a/src/org/ibex/js/XMLRPC.java b/src/org/ibex/js/XMLRPC.java index 6f50328..cdf4789 100644 --- a/src/org/ibex/js/XMLRPC.java +++ b/src/org/ibex/js/XMLRPC.java @@ -1,11 +1,12 @@ -// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + package org.ibex.js; import java.io.*; -import java.util.*; import org.ibex.net.*; import org.ibex.util.*; -import org.ibex.crypto.*; /** * An XML-RPC client implemented as a JavaScript Host Object. See the @@ -29,7 +30,7 @@ import org.ibex.crypto.*; * convert. * */ -public class XMLRPC extends JS { +public class XMLRPC extends JS.Immutable { public XMLRPC(String url, String method) { this.http = url.startsWith("stdio:") ? HTTP.stdio : new HTTP(url); @@ -38,16 +39,20 @@ public class XMLRPC extends JS { } public XMLRPC(String url, String method, XMLRPC httpSource) { this.http = httpSource.http; this.url = url; this.method = method; } - public Object get(Object name) { - return new XMLRPC(url, (method.equals("") ? "" : method + ".") + name.toString(), this); } + public JS get(JS name) throws JSExn { + return new XMLRPC(url, (method.equals("") ? "" : method + ".") + JSU.toString(name), this); } /** this holds character content as we read it in -- since there is only one per instance, we don't support mixed content */ - protected AccessibleCharArrayWriter content = new AccessibleCharArrayWriter(100); + protected PublicCharArrayWriter content = new PublicCharArrayWriter(100); + public static final class PublicCharArrayWriter extends CharArrayWriter { + public PublicCharArrayWriter(int i) { super(i); } + public char[] buf; + }; protected String url = null; ///< the url to connect to protected String method = null; ///< the method name to invoke on the remove server protected HTTP http = null; ///< the HTTP connection to use - private Hash tracker; ///< used to detect multi-ref data + private Basket.Map tracker; ///< used to detect multi-ref data protected boolean fault = false; ///< True iff the return value is a fault (and should be thrown as an exception) @@ -72,39 +77,40 @@ public class XMLRPC extends JS { * popped off the stack and inserted into the struct (third * element on stack). */ - protected Vec objects = null; + protected Basket.Array objects = null; + private void setLast(Object o) { objects.set(objects.size() - 1, o); } // Recieve //////////////////////////////////////////////////////////////// private class Helper extends XML { - public Helper() { super(BUFFER_SIZE); } + public Helper() { super(BUFFER_SIZE, true); } - public void startElement(XML.Element c) { + public void startElement(Tree.Element c) { content.reset(); //#switch(c.getLocalName()) case "fault": fault = true; - case "struct": objects.setElementAt(new JS(), objects.size() - 1); - case "array": objects.setElementAt(null, objects.size() - 1); - case "value": objects.addElement(""); + case "struct": setLast(new JS.Obj()); + case "array": setLast(null); + case "value": objects.add(""); //#end } - public void endElement(XML.Element c) { + public void endElement(Tree.Element c) { //#switch(c.getLocalName()) - case "int": objects.setElementAt(new Integer(new String(content.getBuf(), 0, content.size())), objects.size() - 1); - case "i4": objects.setElementAt(new Integer(new String(content.getBuf(), 0, content.size())), objects.size() - 1); - case "boolean": objects.setElementAt(content.getBuf()[0] == '1' ? Boolean.TRUE : Boolean.FALSE, objects.size() - 1); - case "string": objects.setElementAt(new String(content.getBuf(), 0, content.size()), objects.size() - 1); - case "double": objects.setElementAt(new Double(new String(content.getBuf(), 0, content.size())), objects.size() - 1); + case "int": setLast(new Integer(new String(content.buf, 0, content.size()))); + case "i4": setLast(new Integer(new String(content.buf, 0, content.size()))); + case "boolean": setLast(content.buf[0] == '1' ? Boolean.TRUE : Boolean.FALSE); + case "string": setLast(new String(content.buf, 0, content.size())); + case "double": setLast(new Double(new String(content.buf, 0, content.size()))); case "base64": - objects.setElementAt(new Fountain.ByteArray(Base64.decode(new String(content.getBuf(), 0, content.size())), - null), objects.size() - 1); - case "name": objects.addElement(new String(content.getBuf(), 0, content.size())); - case "value": if ("".equals(objects.lastElement())) - objects.setElementAt(new String(content.getBuf(), 0, content.size()), objects.size() - 1); + setLast(new Fountain.ByteArray(Encode.fromBase64(new String(content.buf, 0, content.size())), + null)); + case "name": objects.add(new String(content.buf, 0, content.size())); + case "value": if ("".equals(objects.peek())) + setLast(new String(content.buf, 0, content.size())); case "dateTime.iso8601": - String s = new String(content.getBuf(), 0, content.size()); + String s = new String(content.buf, 0, content.size()); // strip whitespace int i=0; @@ -122,33 +128,34 @@ public class XMLRPC extends JS { (double)0 ); nd.setTime(JSDate.internalUTC(date)); - objects.setElementAt(nd, objects.size() - 1); + setLast(nd); } catch (Exception e) { throw new RuntimeException("ibex.net.rpc.xml.recieve.malformedDateTag" + "the server sent a tag which was malformed: " + s); } case "member": - Object memberValue = objects.elementAt(objects.size() - 1); - String memberName = (String)objects.elementAt(objects.size() - 2); - JS struct = (JS)objects.elementAt(objects.size() - 3); + JS memberValue = (JS)objects.get(objects.size() - 1); + JS memberName = (JS)objects.get(objects.size() - 2); + JS struct = (JS)objects.get(objects.size() - 3); try { struct.put(memberName, memberValue); } catch (JSExn e) { throw new Error("this should never happen"); } - objects.setSize(objects.size() - 2); + objects.pop(); + objects.pop(); case "data": int i; - for(i=objects.size() - 1; objects.elementAt(i) != null; i--); + for(i=objects.size() - 1; objects.get(i) != null; i--); JSArray arr = new JSArray(); try { - for(int j = i + 1; j\n"); content.append(" \n"); - for(int i=0; i\n"); - appendObject(args.elementAt(i), content); + appendObject(args.get(i), content); content.append(" \n"); } content.append(" \n"); @@ -221,7 +228,7 @@ public class XMLRPC extends JS { System.arraycopy(buf, 0, writebuf, 0, numread); } sb.append(" "); - sb.append(new String(Base64.encode(writebuf))); + sb.append(new String(Encode.toBase64(writebuf))); sb.append("\n"); } sb.append("\n \n"); @@ -272,22 +279,22 @@ public class XMLRPC extends JS { } else if (o instanceof JSArray) { if (tracker.get(o) != null) throw new JSExn("attempted to send multi-ref data structure via XML-RPC"); - tracker.put(o, Boolean.TRUE); + tracker.put(o, JSU.B(true)); sb.append(" \n"); JSArray a = (JSArray)o; - for(int i=0; i\n"); } else if (o instanceof JS) { if (tracker.get(o) != null) throw new JSExn("attempted to send multi-ref data structure via XML-RPC"); - tracker.put(o, Boolean.TRUE); + tracker.put(o, JSU.B(true)); JS j = (JS)o; sb.append(" \n"); Enumeration e = j.keys(); - while(e.hasMoreElements()) { - Object key = e.nextElement(); + while (e.hasNext()) { + Object key = e.next(); sb.append(" " + key + "\n"); - appendObject(j.get(key), sb); + appendObject(j.get((JS)key), sb); sb.append(" \n"); } sb.append(" \n"); @@ -302,7 +309,7 @@ public class XMLRPC extends JS { // Call Sequence ////////////////////////////////////////////////////////////////////////// /* FIXME this has been disabled to make XMLRPC usable without Scheduler - public final Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { + public final Object call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn { JSArray args = new JSArray(); for(int i=0; i