diff options
author | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2013-08-13 17:30:58 (GMT) |
---|---|---|
committer | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2013-08-13 17:30:58 (GMT) |
commit | cf45e4bf71dd2a8b27c50d247c3f09a1e22e1fa9 (patch) | |
tree | d0561246efce6d170050b64d48f8b49316f149af /src/bindings | |
parent | 422dedee98e956a7f4cffa69a4ba0a34716dec7f (diff) | |
download | uscxml-cf45e4bf71dd2a8b27c50d247c3f09a1e22e1fa9.zip uscxml-cf45e4bf71dd2a8b27c50d247c3f09a1e22e1fa9.tar.gz uscxml-cf45e4bf71dd2a8b27c50d247c3f09a1e22e1fa9.tar.bz2 |
More work on java datamodel interface
Diffstat (limited to 'src/bindings')
-rw-r--r-- | src/bindings/swig/java/org/uscxml/Data.java | 88 | ||||
-rw-r--r-- | src/bindings/swig/java/uscxml.i | 89 |
2 files changed, 175 insertions, 2 deletions
diff --git a/src/bindings/swig/java/org/uscxml/Data.java b/src/bindings/swig/java/org/uscxml/Data.java new file mode 100644 index 0000000..ce295af --- /dev/null +++ b/src/bindings/swig/java/org/uscxml/Data.java @@ -0,0 +1,88 @@ +package org.uscxml; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public class Data { + public Map<String, Data> compound = new HashMap<String, Data>(); + public List<Data> array = new LinkedList<Data>(); + public String atom; + public Type type = Type.INTERPRETED; + + public enum Type { + VERBATIM, INTERPRETED + } + + public static Data fromJSON(String jsonString) { + return new Data(DataNative.fromJSON(jsonString)); + } + + public Data() { + } + + public Data(String atom, Data.Type type) { + this.atom = atom; + this.type = type; + } + + public Data(DataNative nativeData) { + if (!nativeData.getCompound().empty()) { + // data is a key value compound + for(String key : nativeData.getCompound()) { + this.compound.put(key, new Data(nativeData.getCompound().get(key))); + } + } else if (!nativeData.getArray().isEmpty()) { + // data is an array + for (int i = 0; i < nativeData.getArray().size(); i++) { + this.array.add(new Data(nativeData.getArray().get(i))); + } + } else { + // data is a single atom + this.atom = nativeData.getAtom(); + if (nativeData.getType() == DataNative.Type.INTERPRETED) { + this.type = Type.INTERPRETED; + } else { + this.type = Type.VERBATIM; + } + } + } + + @Override + public String toString() { + return toJSON(); + } + + public String toJSON() { + DataNative nativeData = toNative(this); + return DataNative.toJSON(nativeData); + } + + public static DataNative toNative(Data data) { + DataNative nativeData = new DataNative(); + //nativeData.swigCMemOwn = false; + if (data.compound != null && !data.compound.isEmpty()) { + DataMap nativeDataMap = new DataMap(); + for (String key : data.compound.keySet()) { + nativeDataMap.set(key, toNative(data.compound.get(key))); + } + nativeData.setCompound(nativeDataMap); + } else if (data.array != null && !data.array.isEmpty()) { + DataList nativeDataList = new DataList(); + for (Data item : data.array) { + nativeDataList.add(toNative(item)); + } + nativeData.setArray(nativeDataList); + } else { + nativeData.setAtom(data.atom); + if (data.type == Type.INTERPRETED) { + nativeData.setType(DataNative.Type.INTERPRETED); + } else { + nativeData.setType(DataNative.Type.VERBATIM); + } + } + return nativeData; + } + +} diff --git a/src/bindings/swig/java/uscxml.i b/src/bindings/swig/java/uscxml.i index 7f496cd..d469498 100644 --- a/src/bindings/swig/java/uscxml.i +++ b/src/bindings/swig/java/uscxml.i @@ -9,10 +9,12 @@ %include <stl.i> %include <std_map.i> +%include "std_string.i" %include <inttypes.i> %include "stl_set.i" %include "stl_list.i" + %include <boost_shared_ptr.i> typedef uscxml::Data Data; @@ -50,10 +52,18 @@ typedef uscxml::SendRequest SendRequest; #include "../../../uscxml/Message.h" #include "../../../uscxml/Factory.h" #include "../../../uscxml/Interpreter.h" + +//#include <DOM/Document.hpp> +//#include <DOM/Node.hpp> +//#include <DOM/Element.hpp> +//#include <DOM/Attr.hpp> +//#include <DOM/Text.hpp> + #include "JavaInvoker.h" #include "JavaDataModel.h" using namespace uscxml; +using namespace Arabica::DOM; #include "JavaInvoker.cpp" #include "JavaDataModel.cpp" @@ -64,25 +74,88 @@ using namespace uscxml; %ignore uscxml::SCXMLParser; %ignore uscxml::InterpreterImpl; +%ignore create(); + %ignore uscxml::Interpreter::getDelayQueue(); %ignore uscxml::JavaInvoker::create(InterpreterImpl*); -%ignore uscxml::JavaDataModel::create(InterpreterImpl*); +%ignore uscxml::JavaDataModel::create(InterpreterImpl*); %ignore uscxml::JavaDataModel::init(const Arabica::DOM::Element<std::string>&, const Arabica::DOM::Document<std::string>&, const std::string&); %ignore uscxml::JavaDataModel::init(const std::string&, const Data&); %ignore uscxml::JavaDataModel::assign(const Arabica::DOM::Element<std::string>&, const Arabica::DOM::Document<std::string>&, const std::string&); %ignore uscxml::JavaDataModel::assign(const std::string&, const Data&); %ignore uscxml::JavaDataModel::eval(const Arabica::DOM::Element<std::string>&, const std::string&); -%template(DataMap) std::map<std::string, uscxml::Data>; +%ignore uscxml::Event::Event(const Arabica::DOM::Node<std::string>&); +%ignore uscxml::Event::getStrippedDOM; +%ignore uscxml::Event::getFirstDOMElement; +%ignore uscxml::Event::getDOM(); +%ignore uscxml::Event::setDOM(const Arabica::DOM::Document<std::string>&); +%ignore uscxml::Event::toDocument(); + %template(DataList) std::list<uscxml::Data>; %template(StringSet) std::set<std::string>; +%rename Data DataNative; +# %typemap(jstype) uscxml::Data "Data" +# %typemap(javaout) uscxml::Data { +# return new Data(new DataNative($jnicall, $owner)); +# } + +# %typemap(javadirectorin) uscxml::Data "new Data($jniinput)" +# %typemap(javadirectorout) uscxml::Data "new Data($jniinput)" %feature("director") uscxml::JavaInvoker; %feature("director") uscxml::JavaDataModel; +// Provide an iterable interface for maps +// http://stackoverflow.com/questions/9465856/no-iterator-for-java-when-using-swig-with-cs-stdmap + +%typemap(javainterfaces) MapKeyIterator "java.util.Iterator<String>" +%typemap(javacode) MapKeyIterator %{ + public void remove() throws UnsupportedOperationException { + throw new UnsupportedOperationException(); + } + + public String next() throws java.util.NoSuchElementException { + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + return nextImpl(); + } +%} + +%javamethodmodifiers MapKeyIterator::nextImpl "private"; +%inline %{ + struct MapKeyIterator { + typedef std::map<std::string, Data> map_t; + MapKeyIterator(const map_t& m) : it(m.begin()), map(m) {} + bool hasNext() const { + return it != map.end(); + } + + const std::string nextImpl() { + const std::pair<std::string, Data>& ret = *it++; + return ret.first; + } + private: + map_t::const_iterator it; + const map_t& map; + }; +%} +%typemap(javainterfaces) std::map<std::string, Data> "Iterable<String>" + +%newobject std::map<std::string, uscxml::Data>::iterator() const; +%extend std::map<std::string, uscxml::Data> { + MapKeyIterator *iterator() const { + return new MapKeyIterator(*$self); + } +} + +%template(DataMap) std::map<std::string, uscxml::Data>; + + //*********************************************** // Parse the header file to generate wrappers //*********************************************** @@ -90,6 +163,18 @@ using namespace uscxml; %include "../../../uscxml/Factory.h" %include "../../../uscxml/Message.h" %include "../../../uscxml/Interpreter.h" + +# %include <DOM/Document.hpp> +# %include <DOM/Node.hpp> +# %include <DOM/Element.hpp> +# %include <DOM/Attr.hpp> +# %include <DOM/Text.hpp> + %include "JavaInvoker.h" %include "JavaDataModel.h" +# %template(XMLDocument) Arabica::DOM::Document<std::string>; +# %template(XMLNode) Arabica::DOM::Node<std::string>; +# %template(XMLElement) Arabica::DOM::Element<std::string>; +# %template(XMLAttr) Arabica::DOM::Attr<std::string>; +# %template(XMLText) Arabica::DOM::Text<std::string>; |