Make HopObject implement the Rhino Wrapper interface.

Replace NativeJavaObject with Wrapper when unwrapping wrapped Java objects.
This commit is contained in:
hns 2003-08-07 11:26:44 +00:00
parent 4b91011578
commit bebce2ad3d
6 changed files with 37 additions and 35 deletions

View file

@ -295,8 +295,8 @@ public class GlobalObject extends ScriptableObject {
public Object getXmlDocument(Object src) { public Object getXmlDocument(Object src) {
try { try {
Object p = src; Object p = src;
if (p instanceof NativeJavaObject) { if (p instanceof Wrapper) {
p = ((NativeJavaObject) p).unwrap(); p = ((Wrapper) p).unwrap();
} }
Object doc = XmlUtils.parseXml(p); Object doc = XmlUtils.parseXml(p);
@ -315,8 +315,8 @@ public class GlobalObject extends ScriptableObject {
public Object getHtmlDocument(Object src) { public Object getHtmlDocument(Object src) {
try { try {
Object p = src; Object p = src;
if (p instanceof NativeJavaObject) { if (p instanceof Wrapper) {
p = ((NativeJavaObject) p).unwrap(); p = ((Wrapper) p).unwrap();
} }
Object doc = helma.util.XmlUtils.parseHtml(p); Object doc = helma.util.XmlUtils.parseHtml(p);

View file

@ -34,7 +34,7 @@ import java.util.Map;
/** /**
* *
*/ */
public class HopObject extends ScriptableObject { public class HopObject extends ScriptableObject implements Wrapper {
static Method hopObjCtor; static Method hopObjCtor;
static { static {
@ -152,6 +152,14 @@ public class HopObject extends ScriptableObject {
return node; 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; String act = null;
if (action != null) { if (action != null) {
if (action instanceof NativeJavaObject) { if (action instanceof Wrapper) {
act = ((NativeJavaObject) action).unwrap().toString(); act = ((Wrapper) action).unwrap().toString();
} else if (!(action instanceof Undefined)) { } else if (!(action instanceof Undefined)) {
act = action.toString(); act = action.toString();
} }
@ -465,8 +473,8 @@ public class HopObject extends ScriptableObject {
node.setSubnodeRelation(value == null ? null : value.toString()); node.setSubnodeRelation(value == null ? null : value.toString());
} }
if (value instanceof NativeJavaObject) { if (value instanceof Wrapper) {
value = ((NativeJavaObject) value).unwrap(); value = ((Wrapper) value).unwrap();
} }
if ((value == null) || (value == Undefined.instance)) { if ((value == null) || (value == Undefined.instance)) {
@ -480,8 +488,6 @@ public class HopObject extends ScriptableObject {
node.setString(name, ScriptRuntime.toString(s)); node.setString(name, ScriptRuntime.toString(s));
} else if (s instanceof MapWrapper) { } else if (s instanceof MapWrapper) {
node.setJavaObject(name, ((MapWrapper) s).unwrap()); node.setJavaObject(name, ((MapWrapper) s).unwrap());
} else if (s instanceof HopObject) {
node.setNode(name, ((HopObject) value).node);
} else { } else {
node.setJavaObject(name, s); node.setJavaObject(name, s);
} }
@ -495,8 +501,6 @@ public class HopObject extends ScriptableObject {
node.setDate(name, (Date) value); node.setDate(name, (Date) value);
} else if (value instanceof INode) { } else if (value instanceof INode) {
node.setNode(name, (INode) value); node.setNode(name, (INode) value);
} else if (value instanceof HopObject) {
node.setNode(name, ((HopObject) value).node);
} else { } else {
node.setJavaObject(name, value); node.setJavaObject(name, value);
} }

View file

@ -142,8 +142,8 @@ public class JavaObject extends NativeJavaObject {
String act = null; String act = null;
if (action != null) { if (action != null) {
if (action instanceof NativeJavaObject) { if (action instanceof Wrapper) {
act = ((NativeJavaObject) action).unwrap().toString(); act = ((Wrapper) action).unwrap().toString();
} else if (!(action instanceof Undefined)) { } else if (!(action instanceof Undefined)) {
act = action.toString(); act = action.toString();
} }

View file

@ -19,7 +19,7 @@ package helma.scripting.rhino;
import org.mozilla.javascript.Context; import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.NativeJavaObject; import org.mozilla.javascript.Wrapper;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -59,8 +59,8 @@ public class MapWrapper extends ScriptableObject {
if (map == null) { if (map == null) {
map = new HashMap(); map = new HashMap();
} }
if (value instanceof NativeJavaObject) { if (value instanceof Wrapper) {
map.put(name, ((NativeJavaObject) value).unwrap()); map.put(name, ((Wrapper) value).unwrap());
} else { } else {
map.put(name, value); map.put(name, value);
} }
@ -126,8 +126,8 @@ public class MapWrapper extends ScriptableObject {
if (map == null) { if (map == null) {
map = new HashMap(); map = new HashMap();
} }
if (value instanceof NativeJavaObject) { if (value instanceof Wrapper) {
map.put(Integer.toString(idx), ((NativeJavaObject) value).unwrap()); map.put(Integer.toString(idx), ((Wrapper) value).unwrap());
} else { } else {
map.put(Integer.toString(idx), value); map.put(Integer.toString(idx), value);
} }

View file

@ -53,7 +53,7 @@ public final class RhinoCore {
long lastUpdate = 0; long lastUpdate = 0;
// the wrap factory // the wrap factory
Wrapper wrapper; WrapFactory wrapper;
// the prototype for path objects // the prototype for path objects
PathWrapper pathProto; PathWrapper pathProto;
@ -70,7 +70,7 @@ public final class RhinoCore {
Context context = Context.enter(); Context context = Context.enter();
context.setCompileFunctionsWithDynamicScope(true); context.setCompileFunctionsWithDynamicScope(true);
wrapper = new Wrapper(); wrapper = new WrapMaker();
context.setWrapFactory(wrapper); context.setWrapFactory(wrapper);
int optLevel = 0; int optLevel = 0;
@ -466,8 +466,8 @@ public final class RhinoCore {
* 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) throws Exception { public Object processXmlRpcResponse (Object what) throws Exception {
if (what instanceof NativeJavaObject) { if (what instanceof Wrapper) {
what = ((NativeJavaObject) what).unwrap(); what = ((Wrapper) what).unwrap();
} }
if (what instanceof NativeObject) { if (what instanceof NativeObject) {
NativeObject no = (NativeObject) what; NativeObject no = (NativeObject) what;
@ -607,8 +607,8 @@ public final class RhinoCore {
for (int i=0; i<skinpath.length; i++) { for (int i=0; i<skinpath.length; i++) {
if (skinpath[i] instanceof HopObject) { if (skinpath[i] instanceof HopObject) {
skinpath[i] = ((HopObject) skinpath[i]).getNode(); skinpath[i] = ((HopObject) skinpath[i]).getNode();
} else if (skinpath[i] instanceof NativeJavaObject) { } else if (skinpath[i] instanceof Wrapper) {
skinpath[i] = ((NativeJavaObject) skinpath[i]).unwrap(); skinpath[i] = ((Wrapper) skinpath[i]).unwrap();
} }
} }
} }
@ -627,8 +627,8 @@ public final class RhinoCore {
for (int i = 0; i < ids.length; i++) { for (int i = 0; i < ids.length; i++) {
Object obj = sp.get(ids[i].toString(), sp); Object obj = sp.get(ids[i].toString(), sp);
if (obj instanceof NativeJavaObject) { if (obj instanceof Wrapper) {
param.put(ids[i], ((NativeJavaObject) obj).unwrap()); param.put(ids[i], ((Wrapper) obj).unwrap());
} else if (obj != Undefined.instance) { } else if (obj != Undefined.instance) {
param.put(ids[i], obj); param.put(ids[i], obj);
} }
@ -744,7 +744,7 @@ public final class RhinoCore {
/** /**
* Object wrapper class * Object wrapper class
*/ */
class Wrapper extends WrapFactory { class WrapMaker extends WrapFactory {
public Object wrap(Context cx, Scriptable scope, Object obj, Class staticType) { public Object wrap(Context cx, Scriptable scope, Object obj, Class staticType) {
// System.err.println ("Wrapping: "+obj); // System.err.println ("Wrapping: "+obj);

View file

@ -265,10 +265,8 @@ public class RhinoEngine implements ScriptingEngine {
return core.processXmlRpcResponse (retval); return core.processXmlRpcResponse (retval);
} else if ((retval == null) || (retval == Undefined.instance)) { } else if ((retval == null) || (retval == Undefined.instance)) {
return null; return null;
} else if (retval instanceof NativeJavaObject) { } else if (retval instanceof Wrapper) {
return ((NativeJavaObject) retval).unwrap(); return ((Wrapper) retval).unwrap();
} else if (retval instanceof HopObject) {
return ((HopObject) retval).getNode();
} else { } else {
return retval; return retval;
} }
@ -378,8 +376,8 @@ public class RhinoEngine implements ScriptingEngine {
if ((prop == null) || (prop == Undefined.instance)) { if ((prop == null) || (prop == Undefined.instance)) {
return null; return null;
} else if (prop instanceof NativeJavaObject) { } else if (prop instanceof Wrapper) {
return ((NativeJavaObject) prop).unwrap(); return ((Wrapper) prop).unwrap();
} else { } else {
return prop; return prop;
} }