* convert.
* </ol>
*/
-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;
*
* 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
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("struct")) objects.setElementAt(new JS(), objects.size() - 1);
else if (c.localName.equals("array")) objects.setElementAt(null, objects.size() - 1);
else if (c.localName.equals("value")) objects.addElement("");
}
else if (c.localName.equals("double"))
objects.setElementAt(new Double(new String(content.getBuf(), 0, content.size())), objects.size() - 1);
- // FIXME
- /*
else if (c.localName.equals("base64"))
- objects.setElementAt(new ByteStream(Base64.decode(new String(content.getBuf(), 0, content.size()))), objects.size() - 1);
- */
+ objects.setElementAt(new Res.ByteArray(Base64.decode(new String(content.getBuf(), 0, content.size())), null), objects.size() - 1);
else if (c.localName.equals("name"))
objects.addElement(new String(content.getBuf(), 0, content.size()));
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);
- }
- */
+ 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 {
+ 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);
+ }
} else if (c.localName.equals("member")) {
Object memberValue = objects.elementAt(objects.size() - 1);
} else if (c.localName.equals("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; j<objects.size(); j++) arr.put(new Integer(j - i - 1), objects.elementAt(j));
objects.setElementAt(arr, i);
objects.setSize(i + 1);
// Methods to make outbound XML-RPC request ///////////////////////////////////////////////////
/** Appends the XML-RPC representation of <code>o</code> to <code>sb</code> */
- 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()) {
sb.append(((Boolean)o).booleanValue() ? "1" : "0");
sb.append("</boolean></value>\n");
- //FIXME
- /*
- } else if (o instanceof ByteStream) {
+ } else if (o instanceof Res) {
try {
sb.append(" <value><base64>\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);
} 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) {
sb.append(" <value><string>");
}
sb.append("</string></value>\n");
- /*
- } else if (o instanceof org.xwt.js.Date) {
+ } else if (o instanceof JSDate) {
sb.append(" <value><dateTime.iso8601>");
- 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);
if (d.getSeconds() < 10) sb.append('0');
sb.append(d.getSeconds());
sb.append("</dateTime.iso8601></value>\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(" <value><array><data>\n");
- JS.Array a = (JS.Array)o;
+ JSArray a = (JSArray)o;
for(int i=0; i<a.length(); i++) appendObject(a.elementAt(i), sb);
sb.append(" </data></array></value>\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(" <value><struct>\n");
- Object[] ids = j.keys();
- for(int i=0; i<ids.length; i++) {
- sb.append(" <member><name>" + ids[i] + "</name>\n");
- appendObject(j.get(ids[i].toString()), sb);
+ Enumeration e = j.keys();
+ while(e.hasMoreElements()) {
+ Object key = e.nextElement();
+ sb.append(" <member><name>" + key + "</name>\n");
+ appendObject(j.get(key), sb);
sb.append(" </member>\n");
}
sb.append(" </struct></value>\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();
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));
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("<?xml version=\"1.0\"?>\n");
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<nargs; i++) args.addElement(i==0?a0:i==1?a1:i==2?a2:rest[i-3]);
+ return call(args);
+ }
+ public final Object call(final JSArray args) throws JSExn {
try {
- return call2(args);
- } catch (IOException se) {
- if (Log.on) Log.log(this, se);
- throw new JS.Exn("socket exception: " + se);
-
- } catch (JS.Exn jse) {
- if (Log.on) Log.log(this, jse.toString());
- throw jse;
- } finally {
- ThreadMessage.resumeThread();
+ final JS.UnpauseCallback callback = JS.pause();
+ new java.lang.Thread() {
+ public void run() {
+ try {
+ final Object ret = call_(args);
+ Scheduler.add(new Scheduler.Task() {
+ public void perform() {
+ try {
+ callback.unpause(null);
+ } catch (JS.PausedException pe) {
+ // okay
+ }
+ }
+ });
+ } catch (IOException se) {
+ if (Log.on) Log.log(this, se);
+ throw new JSExn("socket exception: " + se);
+ }
+ } }.start();
+ return null;
+ } catch (NotPauseableException npe) {
+ throw new JSExn("cannot invoke an XML-RPC call in the foreground thread");
}
-
}
/** When you get a property from an XMLRPC, it just returns another XMLRPC with the property name tacked onto methodname. */
public AccessibleCharArrayWriter(int i) { super(i); }
}
- /** private filter class to make sure that network transfers don't interfere with UI responsiveness */
- private static class Filter extends FilterInputStream {
- public Filter(InputStream is) { super(is); }
- public int read() throws IOException {
- java.lang.Thread.yield();
- while(MessageQueue.nonThreadEventsInQueue > 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);
- }
- }
-
}