Make HopObject implement the Rhino Wrapper interface.
Replace NativeJavaObject with Wrapper when unwrapping wrapped Java objects.
This commit is contained in:
parent
4b91011578
commit
bebce2ad3d
6 changed files with 37 additions and 35 deletions
|
@ -295,8 +295,8 @@ public class GlobalObject extends ScriptableObject {
|
|||
public Object getXmlDocument(Object src) {
|
||||
try {
|
||||
Object p = src;
|
||||
if (p instanceof NativeJavaObject) {
|
||||
p = ((NativeJavaObject) p).unwrap();
|
||||
if (p instanceof Wrapper) {
|
||||
p = ((Wrapper) p).unwrap();
|
||||
}
|
||||
Object doc = XmlUtils.parseXml(p);
|
||||
|
||||
|
@ -315,8 +315,8 @@ public class GlobalObject extends ScriptableObject {
|
|||
public Object getHtmlDocument(Object src) {
|
||||
try {
|
||||
Object p = src;
|
||||
if (p instanceof NativeJavaObject) {
|
||||
p = ((NativeJavaObject) p).unwrap();
|
||||
if (p instanceof Wrapper) {
|
||||
p = ((Wrapper) p).unwrap();
|
||||
}
|
||||
Object doc = helma.util.XmlUtils.parseHtml(p);
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ import java.util.Map;
|
|||
/**
|
||||
*
|
||||
*/
|
||||
public class HopObject extends ScriptableObject {
|
||||
public class HopObject extends ScriptableObject implements Wrapper {
|
||||
static Method hopObjCtor;
|
||||
|
||||
static {
|
||||
|
@ -152,6 +152,14 @@ public class HopObject extends ScriptableObject {
|
|||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the wrapped Node. Implements unwrap() in interface Wrapper.
|
||||
*
|
||||
*/
|
||||
public Object unwrap() {
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
|
@ -241,8 +249,8 @@ public class HopObject extends ScriptableObject {
|
|||
String act = null;
|
||||
|
||||
if (action != null) {
|
||||
if (action instanceof NativeJavaObject) {
|
||||
act = ((NativeJavaObject) action).unwrap().toString();
|
||||
if (action instanceof Wrapper) {
|
||||
act = ((Wrapper) action).unwrap().toString();
|
||||
} else if (!(action instanceof Undefined)) {
|
||||
act = action.toString();
|
||||
}
|
||||
|
@ -465,8 +473,8 @@ public class HopObject extends ScriptableObject {
|
|||
node.setSubnodeRelation(value == null ? null : value.toString());
|
||||
}
|
||||
|
||||
if (value instanceof NativeJavaObject) {
|
||||
value = ((NativeJavaObject) value).unwrap();
|
||||
if (value instanceof Wrapper) {
|
||||
value = ((Wrapper) value).unwrap();
|
||||
}
|
||||
|
||||
if ((value == null) || (value == Undefined.instance)) {
|
||||
|
@ -480,8 +488,6 @@ public class HopObject extends ScriptableObject {
|
|||
node.setString(name, ScriptRuntime.toString(s));
|
||||
} else if (s instanceof MapWrapper) {
|
||||
node.setJavaObject(name, ((MapWrapper) s).unwrap());
|
||||
} else if (s instanceof HopObject) {
|
||||
node.setNode(name, ((HopObject) value).node);
|
||||
} else {
|
||||
node.setJavaObject(name, s);
|
||||
}
|
||||
|
@ -495,8 +501,6 @@ public class HopObject extends ScriptableObject {
|
|||
node.setDate(name, (Date) value);
|
||||
} else if (value instanceof INode) {
|
||||
node.setNode(name, (INode) value);
|
||||
} else if (value instanceof HopObject) {
|
||||
node.setNode(name, ((HopObject) value).node);
|
||||
} else {
|
||||
node.setJavaObject(name, value);
|
||||
}
|
||||
|
|
|
@ -142,8 +142,8 @@ public class JavaObject extends NativeJavaObject {
|
|||
String act = null;
|
||||
|
||||
if (action != null) {
|
||||
if (action instanceof NativeJavaObject) {
|
||||
act = ((NativeJavaObject) action).unwrap().toString();
|
||||
if (action instanceof Wrapper) {
|
||||
act = ((Wrapper) action).unwrap().toString();
|
||||
} else if (!(action instanceof Undefined)) {
|
||||
act = action.toString();
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ package helma.scripting.rhino;
|
|||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
import org.mozilla.javascript.ScriptableObject;
|
||||
import org.mozilla.javascript.NativeJavaObject;
|
||||
import org.mozilla.javascript.Wrapper;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -59,8 +59,8 @@ public class MapWrapper extends ScriptableObject {
|
|||
if (map == null) {
|
||||
map = new HashMap();
|
||||
}
|
||||
if (value instanceof NativeJavaObject) {
|
||||
map.put(name, ((NativeJavaObject) value).unwrap());
|
||||
if (value instanceof Wrapper) {
|
||||
map.put(name, ((Wrapper) value).unwrap());
|
||||
} else {
|
||||
map.put(name, value);
|
||||
}
|
||||
|
@ -126,8 +126,8 @@ public class MapWrapper extends ScriptableObject {
|
|||
if (map == null) {
|
||||
map = new HashMap();
|
||||
}
|
||||
if (value instanceof NativeJavaObject) {
|
||||
map.put(Integer.toString(idx), ((NativeJavaObject) value).unwrap());
|
||||
if (value instanceof Wrapper) {
|
||||
map.put(Integer.toString(idx), ((Wrapper) value).unwrap());
|
||||
} else {
|
||||
map.put(Integer.toString(idx), value);
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ public final class RhinoCore {
|
|||
long lastUpdate = 0;
|
||||
|
||||
// the wrap factory
|
||||
Wrapper wrapper;
|
||||
WrapFactory wrapper;
|
||||
|
||||
// the prototype for path objects
|
||||
PathWrapper pathProto;
|
||||
|
@ -70,7 +70,7 @@ public final class RhinoCore {
|
|||
Context context = Context.enter();
|
||||
|
||||
context.setCompileFunctionsWithDynamicScope(true);
|
||||
wrapper = new Wrapper();
|
||||
wrapper = new WrapMaker();
|
||||
context.setWrapFactory(wrapper);
|
||||
|
||||
int optLevel = 0;
|
||||
|
@ -466,8 +466,8 @@ public final class RhinoCore {
|
|||
* convert a JavaScript Object object to a generic Java object stucture.
|
||||
*/
|
||||
public Object processXmlRpcResponse (Object what) throws Exception {
|
||||
if (what instanceof NativeJavaObject) {
|
||||
what = ((NativeJavaObject) what).unwrap();
|
||||
if (what instanceof Wrapper) {
|
||||
what = ((Wrapper) what).unwrap();
|
||||
}
|
||||
if (what instanceof NativeObject) {
|
||||
NativeObject no = (NativeObject) what;
|
||||
|
@ -607,8 +607,8 @@ public final class RhinoCore {
|
|||
for (int i=0; i<skinpath.length; i++) {
|
||||
if (skinpath[i] instanceof HopObject) {
|
||||
skinpath[i] = ((HopObject) skinpath[i]).getNode();
|
||||
} else if (skinpath[i] instanceof NativeJavaObject) {
|
||||
skinpath[i] = ((NativeJavaObject) skinpath[i]).unwrap();
|
||||
} else if (skinpath[i] instanceof Wrapper) {
|
||||
skinpath[i] = ((Wrapper) skinpath[i]).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -627,8 +627,8 @@ public final class RhinoCore {
|
|||
|
||||
for (int i = 0; i < ids.length; i++) {
|
||||
Object obj = sp.get(ids[i].toString(), sp);
|
||||
if (obj instanceof NativeJavaObject) {
|
||||
param.put(ids[i], ((NativeJavaObject) obj).unwrap());
|
||||
if (obj instanceof Wrapper) {
|
||||
param.put(ids[i], ((Wrapper) obj).unwrap());
|
||||
} else if (obj != Undefined.instance) {
|
||||
param.put(ids[i], obj);
|
||||
}
|
||||
|
@ -744,7 +744,7 @@ public final class RhinoCore {
|
|||
/**
|
||||
* Object wrapper class
|
||||
*/
|
||||
class Wrapper extends WrapFactory {
|
||||
class WrapMaker extends WrapFactory {
|
||||
|
||||
public Object wrap(Context cx, Scriptable scope, Object obj, Class staticType) {
|
||||
// System.err.println ("Wrapping: "+obj);
|
||||
|
|
|
@ -265,10 +265,8 @@ public class RhinoEngine implements ScriptingEngine {
|
|||
return core.processXmlRpcResponse (retval);
|
||||
} else if ((retval == null) || (retval == Undefined.instance)) {
|
||||
return null;
|
||||
} else if (retval instanceof NativeJavaObject) {
|
||||
return ((NativeJavaObject) retval).unwrap();
|
||||
} else if (retval instanceof HopObject) {
|
||||
return ((HopObject) retval).getNode();
|
||||
} else if (retval instanceof Wrapper) {
|
||||
return ((Wrapper) retval).unwrap();
|
||||
} else {
|
||||
return retval;
|
||||
}
|
||||
|
@ -378,8 +376,8 @@ public class RhinoEngine implements ScriptingEngine {
|
|||
|
||||
if ((prop == null) || (prop == Undefined.instance)) {
|
||||
return null;
|
||||
} else if (prop instanceof NativeJavaObject) {
|
||||
return ((NativeJavaObject) prop).unwrap();
|
||||
} else if (prop instanceof Wrapper) {
|
||||
return ((Wrapper) prop).unwrap();
|
||||
} else {
|
||||
return prop;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue