* Allow HopObjects to be used as arguments in XML-RPC calls, interpreting

them as dicts and ignoring child nodes. Resolves bug #345
  <http://helma.org/bugs/show_bug.cgi?id=345>
This commit is contained in:
hns 2007-11-08 11:03:41 +00:00
parent e0b814216e
commit ac4e78cb17

View file

@ -477,49 +477,49 @@ public final class RhinoCore implements ScopeProvider {
* Convert an input argument from Java to the scripting runtime * Convert an input argument from Java to the scripting runtime
* representation. * representation.
*/ */
public Object processXmlRpcArgument (Object what) { public Object processXmlRpcArgument (Object arg) {
if (what == null) if (arg == null)
return null; return null;
if (what instanceof Vector) { if (arg instanceof Vector) {
Vector v = (Vector) what; Vector v = (Vector) arg;
Object[] a = v.toArray(); Object[] a = v.toArray();
for (int i=0; i<a.length; i++) { for (int i=0; i<a.length; i++) {
a[i] = processXmlRpcArgument(a[i]); a[i] = processXmlRpcArgument(a[i]);
} }
return Context.getCurrentContext().newArray(global, a); return Context.getCurrentContext().newArray(global, a);
} }
if (what instanceof Hashtable) { if (arg instanceof Hashtable) {
Hashtable t = (Hashtable) what; Hashtable t = (Hashtable) arg;
for (Enumeration e=t.keys(); e.hasMoreElements(); ) { for (Enumeration e=t.keys(); e.hasMoreElements(); ) {
Object key = e.nextElement(); Object key = e.nextElement();
t.put(key, processXmlRpcArgument(t.get(key))); t.put(key, processXmlRpcArgument(t.get(key)));
} }
return Context.toObject(new SystemMap(t), global); return Context.toObject(new SystemMap(t), global);
} }
if (what instanceof String) if (arg instanceof String)
return what; return arg;
if (what instanceof Number) if (arg instanceof Number)
return what; return arg;
if (what instanceof Boolean) if (arg instanceof Boolean)
return what; return arg;
if (what instanceof Date) { if (arg instanceof Date) {
Date d = (Date) what; Date d = (Date) arg;
Object[] args = { new Long(d.getTime()) }; Object[] args = { new Long(d.getTime()) };
return Context.getCurrentContext().newObject(global, "Date", args); return Context.getCurrentContext().newObject(global, "Date", args);
} }
return Context.toObject(what, global); return Context.toObject(arg, global);
} }
/** /**
* convert a JavaScript Object object to a generic Java object stucture. * convert a JavaScript Object object to a generic Java object stucture.
*/ */
public Object processXmlRpcResponse (Object what) { public Object processXmlRpcResponse (Object arg) {
// unwrap if argument is a Wrapper // unwrap if argument is a Wrapper
if (what instanceof Wrapper) { if (arg instanceof Wrapper) {
what = ((Wrapper) what).unwrap(); arg = ((Wrapper) arg).unwrap();
} }
if (what instanceof NativeObject) { if (arg instanceof NativeObject) {
NativeObject no = (NativeObject) what; NativeObject no = (NativeObject) arg;
Object[] ids = no.getIds(); Object[] ids = no.getIds();
Hashtable ht = new Hashtable(ids.length*2); Hashtable ht = new Hashtable(ids.length*2);
for (int i=0; i<ids.length; i++) { for (int i=0; i<ids.length; i++) {
@ -531,39 +531,52 @@ public final class RhinoCore implements ScopeProvider {
} }
} }
} }
what = ht; return ht;
} else if (what instanceof NativeArray) { } else if (arg instanceof NativeArray) {
NativeArray na = (NativeArray) what; NativeArray na = (NativeArray) arg;
Number n = (Number) na.get("length", na); Number n = (Number) na.get("length", na);
int l = n.intValue(); int l = n.intValue();
Vector retval = new Vector(l); Vector retval = new Vector(l);
for (int i=0; i<l; i++) { for (int i=0; i<l; i++) {
retval.add(i, processXmlRpcResponse(na.get(i, na))); retval.add(i, processXmlRpcResponse(na.get(i, na)));
} }
what = retval; return retval;
} else if (what instanceof Map) { } else if (arg instanceof Map) {
Map map = (Map) what; Map map = (Map) arg;
Hashtable ht = new Hashtable(map.size()*2); Hashtable ht = new Hashtable(map.size()*2);
for (Iterator it=map.entrySet().iterator(); it.hasNext();) { for (Iterator it=map.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next(); Map.Entry entry = (Map.Entry) it.next();
ht.put(entry.getKey().toString(), ht.put(entry.getKey().toString(),
processXmlRpcResponse(entry.getValue())); processXmlRpcResponse(entry.getValue()));
} }
what = ht; return ht;
} else if (what instanceof Number) { } else if (arg instanceof Number) {
Number n = (Number) what; Number n = (Number) arg;
if (what instanceof Float || what instanceof Long) { if (arg instanceof Float || arg instanceof Long) {
what = new Double(n.doubleValue()); return new Double(n.doubleValue());
} else if (!(what instanceof Double)) { } else if (!(arg instanceof Double)) {
what = new Integer(n.intValue()); return new Integer(n.intValue());
} }
} else if (what instanceof Scriptable) { } else if (arg instanceof INode) {
Scriptable s = (Scriptable) what; // interpret HopObject as object/dict
INode n = (INode) arg;
Hashtable ht = new Hashtable();
Enumeration props = n.properties();
while (props.hasMoreElements()) {
String key = (String) props.nextElement();
IProperty prop = n.get(key);
if (prop != null) {
ht.put(key, processXmlRpcResponse(prop.getValue()));
}
}
return ht;
} else if (arg instanceof Scriptable) {
Scriptable s = (Scriptable) arg;
if ("Date".equals(s.getClassName())) { if ("Date".equals(s.getClassName())) {
what = new Date((long) ScriptRuntime.toNumber(s)); return new Date((long) ScriptRuntime.toNumber(s));
} }
} }
return what; return arg;
} }