diff options
author | Stefan Radomski <github@mintwerk.de> | 2014-05-14 07:04:40 (GMT) |
---|---|---|
committer | Stefan Radomski <github@mintwerk.de> | 2014-05-14 07:04:40 (GMT) |
commit | d20f1b19fb89ae2797e8e0b2e191a2465ec14082 (patch) | |
tree | 89976b139d4244b6c3da581b5fb627b5e78f44ea | |
parent | 10f9156d73701257879b7355b696bd72d7c2fb23 (diff) | |
parent | e437da537c85d82e26665837ee7e66a35080eb1e (diff) | |
download | uscxml-d20f1b19fb89ae2797e8e0b2e191a2465ec14082.zip uscxml-d20f1b19fb89ae2797e8e0b2e191a2465ec14082.tar.gz uscxml-d20f1b19fb89ae2797e8e0b2e191a2465ec14082.tar.bz2 |
Merge pull request #28 from sradomski/master
More work on Rhino
5 files changed, 244 insertions, 56 deletions
diff --git a/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAData.java b/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAData.java new file mode 100644 index 0000000..56d7090 --- /dev/null +++ b/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAData.java @@ -0,0 +1,110 @@ +package org.uscxml.datamodel.ecmascript; + +import org.mozilla.javascript.Scriptable; +import org.uscxml.Data; + +public class ECMAData implements Scriptable { + + protected Data data; + protected Scriptable parent; + protected Scriptable prototype; + + public ECMAData(Data data) { + this.data = data; + } + + @Override + public String getClassName() { + return "Data"; + } + + public Object unwrap(Data data) { + if (data.atom.length() > 0) { + return data.atom; + } + return new ECMAData(data); + + } + + @Override + public Object get(String name, Scriptable start) { + if (data.compound.containsKey(name)) + return unwrap(data.compound.get(name)); + return NOT_FOUND; + } + + @Override + public Object get(int index, Scriptable start) { + if (data.array.size() > index) + return unwrap(data.array.get(index)); + return NOT_FOUND; + } + + @Override + public boolean has(String name, Scriptable start) { + return data.compound.containsKey(name); + } + + @Override + public boolean has(int index, Scriptable start) { + return data.array.size() > index; + } + + @Override + public void put(String name, Scriptable start, Object value) { + } + + @Override + public void put(int index, Scriptable start, Object value) { + } + + @Override + public void delete(String name) { + } + + @Override + public void delete(int index) { + } + + @Override + public Scriptable getPrototype() { + return prototype; + } + + @Override + public void setPrototype(Scriptable prototype) { + this.prototype = prototype; + } + + @Override + public Scriptable getParentScope() { + return parent; + } + + @Override + public void setParentScope(Scriptable parent) { + this.parent = parent; + } + + @Override + public Object[] getIds() { + return data.compound.keySet().toArray(); + } + + @Override + public Object getDefaultValue(Class<?> hint) { + return "[object Data]"; + } + + @Override + public boolean hasInstance(Scriptable instance) { + Scriptable proto = instance.getPrototype(); + while (proto != null) { + if (proto.equals(this)) + return true; + proto = proto.getPrototype(); + } + return false; + } + +} diff --git a/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAEvent.java b/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAEvent.java index e876d80..58fff72 100644 --- a/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAEvent.java +++ b/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAEvent.java @@ -1,69 +1,126 @@ package org.uscxml.datamodel.ecmascript; -import org.mozilla.javascript.ScriptableObject; -import org.mozilla.javascript.annotations.JSConstructor; -import org.mozilla.javascript.annotations.JSGetter; +import java.util.HashMap; +import java.util.Map; + +import org.mozilla.javascript.Scriptable; import org.uscxml.Data; -import org.uscxml.DataMap; import org.uscxml.Event; -import org.uscxml.Event.Type; - -public class ECMAEvent { +import org.uscxml.ParamPair; +import org.uscxml.ParamPairVector; - private Event event; +public class ECMAEvent implements Scriptable { - private static final long serialVersionUID = -5241020609349204355L; + protected Event event; + protected Scriptable parent; + protected Scriptable prototype; + protected Map<String, Object> members = new HashMap<String, Object>(); + public ECMAEvent(Event event) { this.event = event; + + Data data = new Data(event.getData()); + + // insert params into event.data + ParamPairVector ppv = event.getParamPairs(); + for (int i = 0; i < ppv.size(); i++) { + ParamPair pp = ppv.get(i); + data.compound.put(pp.getFirst(), new Data(pp.getSecond())); + } + + members.put("type", event.getEventType().toString()); + members.put("data", new ECMAData(data)); + members.put("sendid", event.getSendId()); + members.put("origin", event.getOrigin()); + members.put("originType", event.getOriginType()); + // add others as necessary + + } + + @Override + public String getClassName() { + return "Event"; + } + + @Override + public Object get(String name, Scriptable start) { + if (members.containsKey(name)) + return members.get(name); + return NOT_FOUND; + } + + @Override + public Object get(int index, Scriptable start) { + return NOT_FOUND; + } + + @Override + public boolean has(String name, Scriptable start) { + return (members.containsKey(name)); } - public String getName() { - return event.getName(); + @Override + public boolean has(int index, Scriptable start) { + return false; } - public Type getEventType() { - return event.getEventType(); + @Override + public void put(String name, Scriptable start, Object value) { } - public String getOrigin() { - return event.getOrigin(); + @Override + public void put(int index, Scriptable start, Object value) { } - public String getOriginType() { - return event.getOriginType(); + @Override + public void delete(String name) { } - public String getContent() { - return event.getContent(); + @Override + public void delete(int index) { } - public String getXML() { - return event.getXML(); + @Override + public Scriptable getPrototype() { + return prototype; } - public String getSendId() { - return event.getSendId(); + @Override + public void setPrototype(Scriptable prototype) { + this.prototype = prototype; } - public String getInvokeId() { - return event.getInvokeId(); + @Override + public Scriptable getParentScope() { + return parent; } - public Data getData() { - return new Data(event.getData()); + @Override + public void setParentScope(Scriptable parent) { + this.parent = parent; } - public DataMap getNameList() { - return event.getNameList(); + @Override + public Object[] getIds() { + return members.keySet().toArray(); } - /* - @JSGetter - public SWIGTYPE_p_std__multimapT_std__string_uscxml__Data_t getParams() { - // TODO Auto-generated method stub - return super.getParams(); + @Override + public Object getDefaultValue(Class<?> hint) { + return "[object Event]"; } -*/ - + + @Override + public boolean hasInstance(Scriptable instance) { + Scriptable proto = instance.getPrototype(); + while (proto != null) { + if (proto.equals(this)) + return true; + proto = proto.getPrototype(); + } + + return false; + } + } diff --git a/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAScriptDataModel.java b/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAScriptDataModel.java index 6fee8ec..e3f4f2c 100644 --- a/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAScriptDataModel.java +++ b/contrib/java/src/org/uscxml/datamodel/ecmascript/ECMAScriptDataModel.java @@ -79,7 +79,8 @@ public class ECMAScriptDataModel extends JavaDataModel { newDM.ctx = Context.enter(); try { newDM.scope = newDM.ctx.initStandardObjects(); -// ScriptableObject.defineClass(newDM.scope, ECMAEvent.class); +// ScriptableObject.defineClass(newDM.scope, ECMAEvent.class); +// ScriptableObject.defineClass(newDM.scope, ECMAEventScriptable.class); } catch (Exception e) { System.err.println(e); } @@ -114,20 +115,21 @@ public class ECMAScriptDataModel extends JavaDataModel { * datamodel. */ + Data data = new Data(event.getData()); + // Object[] args = { event }; // Scriptable ecmaEvent = ctx.newObject(scope, "Event", args); +// ECMAEvent ecmaEvent = new ECMAEvent(event); +// NativeJavaObject njo = new NativeJavaObject(scope, ecmaEvent, ECMAEvent.class, true); + ECMAEvent ecmaEvent = new ECMAEvent(event); - NativeJavaObject njo = new NativeJavaObject(scope, ecmaEvent, ECMAEvent.class, true); - -// for (Object key : njo.getIds()) { + +// for (Object key : ecmaEvent.getIds()) { // System.out.println(key); // } - scope.put("_event", scope, njo); - Data data = new Data(getStringAsData("_event")); - -// System.out.println(data); + scope.put("_event", scope, ecmaEvent); } @Override diff --git a/contrib/java/src/org/uscxml/tests/TestW3CECMA.java b/contrib/java/src/org/uscxml/tests/TestW3CECMA.java index 2b2a36e..4c77a11 100644 --- a/contrib/java/src/org/uscxml/tests/TestW3CECMA.java +++ b/contrib/java/src/org/uscxml/tests/TestW3CECMA.java @@ -16,19 +16,19 @@ public class TestW3CECMA { ECMAScriptDataModel datamodel = new ECMAScriptDataModel(); Factory.getInstance().registerDataModel(datamodel); - Interpreter interpreter = Interpreter.fromURI("/Users/sradomski/Documents/TK/Code/uscxml/test/w3c/ecma/test176.scxml"); - interpreter.interpret(); - System.exit(0); +// Interpreter interpreter = Interpreter.fromURI("/Users/sradomski/Documents/TK/Code/uscxml/test/w3c/ecma/test176.scxml"); +// interpreter.interpret(); +// System.exit(0); -// File dir = new File(testDir); -// File[] filesList = dir.listFiles(); -// for (File file : filesList) { -// if (file.isFile() && file.getName().endsWith(".scxml")) { -// System.out.println("### " + file.getName() + " #####"); -// Interpreter interpreter = Interpreter.fromURI(file.getAbsolutePath()); -// interpreter.interpret(); -// } -// } + File dir = new File(testDir); + File[] filesList = dir.listFiles(); + for (File file : filesList) { + if (file.isFile() && file.getName().endsWith(".scxml")) { + System.out.println("### " + file.getName() + " #####"); + Interpreter interpreter = Interpreter.fromURI(file.getAbsolutePath()); + interpreter.interpret(); + } + } } diff --git a/src/bindings/swig/java/uscxml.i b/src/bindings/swig/java/uscxml.i index ae20eb3..ab46783 100644 --- a/src/bindings/swig/java/uscxml.i +++ b/src/bindings/swig/java/uscxml.i @@ -96,6 +96,8 @@ using namespace Arabica::DOM; %template(DataList) std::list<uscxml::Data>; %template(StringSet) std::set<std::string>; +%template(ParamPair) std::pair<std::string, uscxml::Data>; +%template(ParamPairVector) std::vector<std::pair<std::string, uscxml::Data> >; %rename Data DataNative; # %typemap(jstype) uscxml::Data "Data" @@ -109,6 +111,23 @@ using namespace Arabica::DOM; %feature("director") uscxml::JavaInvoker; %feature("director") uscxml::JavaDataModel; +// translate param multimap to Map<String, List<Data> > +%rename(getParamsNative) uscxml::Event::getParams(); +%javamethodmodifiers uscxml::Event::getParams() "private"; + +%extend uscxml::Event { + std::vector<std::pair<std::string, Data> > getParamPairs() { + std::vector<std::pair<std::string, Data> > pairs; + std::multimap<std::string, Data>::iterator paramPairIter = self->getParams().begin(); + while(paramPairIter != self->getParams().end()) { + pairs.push_back(*paramPairIter); + paramPairIter++; + } + return pairs; + } +}; + + // Provide an iterable interface for maps // http://stackoverflow.com/questions/9465856/no-iterator-for-java-when-using-swig-with-cs-stdmap |