X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FXMLRPC.java;h=39fc30d0698ec32c361d50c077952c6128c8c1b3;hb=0f94de16d032aa04fcfa50056f7964de2fcd2d58;hp=282d9ef7754df1c19047cdfdf573813460573196;hpb=77f51cd3e157cf6fd2ae85ce774444bb85ea7b81;p=org.ibex.core.git diff --git a/src/org/xwt/XMLRPC.java b/src/org/xwt/XMLRPC.java index 282d9ef..39fc30d 100644 --- a/src/org/xwt/XMLRPC.java +++ b/src/org/xwt/XMLRPC.java @@ -1,4 +1,4 @@ -// 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.*; @@ -30,9 +30,7 @@ import org.bouncycastle.util.encoders.Base64; * convert. * */ -class XMLRPC extends JS.Callable { - - public Object[] keys() { throw new Error("not implemented"); } +class XMLRPC extends JS { /** the url to connect to */ protected String url = null; @@ -57,10 +55,10 @@ class XMLRPC extends JS.Callable { * * If an <array> tag is encountered, a null is pushed onto the * stack. When a </data> is encountered, we search back on the - * stack to the last null, replace it with a NativeJS.Array, and + * stack to the last null, replace it with a NativeJSArray, and * insert into it all elements above it on the stack. * - * If a <struct> tag is encountered, a JSObject is pushed + * If a <struct> tag is encountered, a JSect is pushed * onto the stack. If a <name> tag is encountered, its CDATA is * pushed onto the stack. When a </member> is encountered, the * name (second element on stack) and value (top of stack) are @@ -83,81 +81,65 @@ class XMLRPC extends JS.Callable { public void startElement(XML.Element c) { content.reset(); - if (c.localName.equals("fault")) fault = true; - else if (c.localName.equals("struct")) objects.setElementAt(new JS.Obj(), objects.size() - 1); - else if (c.localName.equals("array")) objects.setElementAt(null, objects.size() - 1); - else if (c.localName.equals("value")) objects.addElement(""); + //#switch(c.localName) + 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(""); + //#end } public void endElement(XML.Element c) { - - if (c.localName.equals("int") || c.localName.equals("i4")) - objects.setElementAt(new Integer(new String(content.getBuf(), 0, content.size())), objects.size() - 1); - - else if (c.localName.equals("boolean")) - objects.setElementAt(content.getBuf()[0] == '1' ? Boolean.TRUE : Boolean.FALSE, objects.size() - 1); - - else if (c.localName.equals("string")) - objects.setElementAt(new String(content.getBuf(), 0, content.size()), objects.size() - 1); - - else if (c.localName.equals("double")) - objects.setElementAt(new Double(new String(content.getBuf(), 0, content.size())), objects.size() - 1); - - else if (c.localName.equals("base64")) - objects.setElementAt(new ByteStream(Base64.decode(new String(content.getBuf(), 0, content.size()))), objects.size() - 1); - - else if (c.localName.equals("name")) - objects.addElement(new String(content.getBuf(), 0, content.size())); - - else if (c.localName.equals("value") && "".equals(objects.lastElement())) + //#switch(c.localName) + 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 "base64": objects.setElementAt(new Res.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); - - else if (c.localName.equals("dateTime.iso8601")) { - throw new Error("not implemented"); - /* - String s = new String(content.getBuf(), 0, content.size()); - - // strip whitespace - int i=0; - while(Character.isWhitespace(s.charAt(i))) i++; - if (i > 0) s = s.substring(i); - - try { - NativeDate nd = (NativeDate)JS.Thread.enter().newObject(org.xwt.util.JS.Obj.defaultObjects, "Date"); - double date = NativeDate.date_msecFromDate(Double.valueOf(s.substring(0, 4)).doubleValue(), - Double.valueOf(s.substring(4, 6)).doubleValue() - 1, - Double.valueOf(s.substring(6, 8)).doubleValue(), - Double.valueOf(s.substring(9, 11)).doubleValue(), - Double.valueOf(s.substring(12, 14)).doubleValue(), - Double.valueOf(s.substring(15, 17)).doubleValue(), - (double)0 - ); - nd.jsFunction_setTime(NativeDate.internalUTC(date)); - objects.setElementAt(nd, objects.size() - 1); - - } catch (Exception e) { - if (Log.on) Log.log(this, "error parsing date : " + s); - if (Log.on) Log.log(this, e); - } - */ + case "dateTime.iso8601": + String s = new String(content.getBuf(), 0, content.size()); + + // strip whitespace + int i=0; + while(Character.isWhitespace(s.charAt(i))) i++; + if (i > 0) s = s.substring(i); - } else if (c.localName.equals("member")) { + try { + JSDate nd = new JSDate(); + double date = JSDate.date_msecFromDate(Double.valueOf(s.substring(0, 4)).doubleValue(), + Double.valueOf(s.substring(4, 6)).doubleValue() - 1, + Double.valueOf(s.substring(6, 8)).doubleValue(), + Double.valueOf(s.substring(9, 11)).doubleValue(), + Double.valueOf(s.substring(12, 14)).doubleValue(), + Double.valueOf(s.substring(15, 17)).doubleValue(), + (double)0 + ); + nd.setTime(JSDate.internalUTC(date)); + objects.setElementAt(nd, objects.size() - 1); + + } catch (Exception e) { + if (Log.on) Log.log(this, "error parsing date : " + s); + if (Log.on) Log.log(this, e); + } + 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); struct.put(memberName, memberValue); objects.setSize(objects.size() - 2); - - } else if (c.localName.equals("data")) { + case "data": int i; for(i=objects.size() - 1; objects.elementAt(i) != null; i--); - JS.Array arr = new JS.Array(); + JSArray arr = new JSArray(); for(int j = i + 1; jo to sb */ - void appendObject(Object o, StringBuffer sb) throws JS.Exn { + void appendObject(Object o, StringBuffer sb) throws JSExn { if (o == null) { - throw new JS.Exn("attempted to send a null value via XML-RPC"); + throw new JSExn("attempted to send a null value via XML-RPC"); } else if (o instanceof Number) { if ((double)((Number)o).intValue() == ((Number)o).doubleValue()) { @@ -196,10 +178,10 @@ class XMLRPC extends JS.Callable { sb.append(((Boolean)o).booleanValue() ? "1" : "0"); sb.append("\n"); - } else if (o instanceof ByteStream) { + } else if (o instanceof Res) { try { sb.append(" \n"); - InputStream is = ((ByteStream)o).getInputStream(); + InputStream is = ((Res)o).getInputStream(); byte[] buf = new byte[54]; while(true) { int numread = is.read(buf, 0, 54); @@ -217,7 +199,7 @@ class XMLRPC extends JS.Callable { } catch (IOException e) { if (Log.on) Log.log(this, "caught IOException while attempting to send a ByteStream via XML-RPC"); if (Log.on) Log.log(this, e); - throw new JS.Exn("caught IOException while attempting to send a ByteStream via XML-RPC"); + throw new JSExn("caught IOException while attempting to send a ByteStream via XML-RPC"); } } else if (o instanceof String) { @@ -240,12 +222,9 @@ class XMLRPC extends JS.Callable { } sb.append("\n"); - /* - } else if (o instanceof org.xwt.js.Date) { + } else if (o instanceof JSDate) { sb.append(" "); - FIXME - org.xwt.js.Date d = (org.xwt.js.Date)o; - Date d = new Date(nd.getRawTime()); + java.util.Date d = new java.util.Date(((JSDate)o).getRawTime()); sb.append(d.getYear() + 1900); if (d.getMonth() + 1 < 10) sb.append('0'); sb.append(d.getMonth() + 1); @@ -261,38 +240,36 @@ class XMLRPC extends JS.Callable { if (d.getSeconds() < 10) sb.append('0'); sb.append(d.getSeconds()); sb.append("\n"); - */ - } else if (o instanceof JS.Array) { - if (tracker.get(o) != null) throw new JS.Exn("attempted to send multi-ref data structure via XML-RPC"); + } 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); sb.append(" \n"); - JS.Array a = (JS.Array)o; + JSArray a = (JSArray)o; for(int i=0; i\n"); } else if (o instanceof JS) { - if (tracker.get(o) != null) throw new JS.Exn("attempted to send multi-ref data structure via XML-RPC"); + if (tracker.get(o) != null) throw new JSExn("attempted to send multi-ref data structure via XML-RPC"); tracker.put(o, Boolean.TRUE); JS j = (JS)o; sb.append(" \n"); - Object[] ids = j.keys(); - for(int i=0; i" + ids[i] + "\n"); - appendObject(j.get(ids[i].toString()), sb); + Enumeration e = j.keys(); + while(e.hasMoreElements()) { + Object key = e.nextElement(); + sb.append(" " + key + "\n"); + appendObject(j.get(key), sb); sb.append(" \n"); } sb.append(" \n"); } else { - throw new JS.Exn("attempt to send object of type " + o.getClass().getName() + " via XML-RPC"); + throw new JSExn("attempt to send object of type " + o.getClass().getName() + " via XML-RPC"); } } - // this is synchronized in case multiple threads try to make a call on the same object... in the future, change this - // behavior to use pipelining. - public synchronized Object call2(JS.Array args) throws JS.Exn, IOException { + public Object call_(JSArray args) throws JSExn, IOException { if (Log.verbose) Log.log(this, "call to " + url + " : " + methodname); if (tracker == null) tracker = new Hash(); @@ -301,18 +278,18 @@ class XMLRPC extends JS.Callable { if (objects == null) objects = new Vec(); else objects.setSize(0); - String content = send(args, http); + final String content = send(args, http); if (Log.verbose) { String s; BufferedReader br2 = new BufferedReader(new StringReader(content)); while ((s = br2.readLine()) != null) Log.log(this, "send: " + s); } - HTTP.HTTPInputStream is = http.POST("text/xml", content); + InputStream is = http.POST("text/xml", content); try { BufferedReader br = !Log.verbose ? - new BufferedReader(new InputStreamReader(new Filter(is))) : - new BufferedReader(new FilterReader(new InputStreamReader(new Filter(is))) { + new BufferedReader(new InputStreamReader(is)) : + new BufferedReader(new FilterReader(new InputStreamReader(is)) { public int read() throws IOException { int i = super.read(); if (Log.on) Log.log(this, "recv: " + ((char)i)); @@ -327,13 +304,13 @@ class XMLRPC extends JS.Callable { return ret; } }); - return recieve(br); + return null; } finally { is.close(); } } - protected String send(JS.Array args, HTTP http) throws JS.Exn, IOException { + protected String send(JSArray args, HTTP http) throws JSExn, IOException { StringBuffer content = new StringBuffer(); content.append("\r\n"); content.append("\n"); @@ -352,42 +329,55 @@ class XMLRPC extends JS.Callable { return content.toString(); } - protected Object recieve(BufferedReader br) throws JS.Exn, IOException { + protected Object recieve(BufferedReader br) throws JSExn, IOException { // parse XML reply try { new Helper().parse(br); } catch (XML.XMLException e) { if (Log.on) Log.log(this, "reply from server was not well-formed XML: " + e); - throw new JS.Exn("reply from server was not well-formed XML: " + e); + throw new JSExn("reply from server was not well-formed XML: " + e); } - if (fault) throw new JS.Exn(objects.elementAt(0)); + if (fault) throw new JSExn(objects.elementAt(0)); if (objects.size() == 0) return null; return objects.elementAt(0); } - public final Object call(JS.Array args) throws JS.Exn { - - if (!ThreadMessage.suspendThread()) return null; - + public final Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { + JSArray args = new JSArray(); + for(int i=0; i 0) try { java.lang.Thread.sleep(100); } catch (Exception e) { }; - return super.read(); - } - public int read(byte[] b) throws IOException { - java.lang.Thread.yield(); - while(MessageQueue.nonThreadEventsInQueue > 0) try { java.lang.Thread.sleep(100); } catch (Exception e) { }; - return super.read(b); - } - public int read(byte[] b, int i, int j) throws IOException { - java.lang.Thread.yield(); - while(MessageQueue.nonThreadEventsInQueue > 0) try { java.lang.Thread.sleep(100); } catch (Exception e) { }; - return super.read(b, i, j); - } - } - }