summaryrefslogtreecommitdiffstats
path: root/src/uscxml/Message.cpp
diff options
context:
space:
mode:
authorStefan Radomski <radomski@tk.informatik.tu-darmstadt.de>2013-02-20 21:13:02 (GMT)
committerStefan Radomski <radomski@tk.informatik.tu-darmstadt.de>2013-02-20 21:13:02 (GMT)
commita56f28b0db56ff3e39f0b50e4c55c52b7aeec696 (patch)
tree41cf67ea5cee9593e86272ab55367653fbd1c2f3 /src/uscxml/Message.cpp
parent7c779099b3acd1fa969dde718299484ebe0d2775 (diff)
downloaduscxml-a56f28b0db56ff3e39f0b50e4c55c52b7aeec696.zip
uscxml-a56f28b0db56ff3e39f0b50e4c55c52b7aeec696.tar.gz
uscxml-a56f28b0db56ff3e39f0b50e4c55c52b7aeec696.tar.bz2
See detailled log
- Builds on windows again - All HTTP requests are no passed into interpreter - New response element to reply with data - Moved basichttp URL - New HTTP servlet invoker to register additional URLs - More bugfixes than I care to mention
Diffstat (limited to 'src/uscxml/Message.cpp')
-rw-r--r--src/uscxml/Message.cpp55
1 files changed, 44 insertions, 11 deletions
diff --git a/src/uscxml/Message.cpp b/src/uscxml/Message.cpp
index 9453508..1466b6a 100644
--- a/src/uscxml/Message.cpp
+++ b/src/uscxml/Message.cpp
@@ -5,6 +5,7 @@
//#include "uscxml/Interpreter.h"
#include <DOM/SAX2DOM/SAX2DOM.hpp>
#include <SAX/helpers/CatchErrorHandler.hpp>
+#include <glog/logging.h>
#ifdef HAS_STRING_H
#include <string.h>
@@ -109,7 +110,7 @@ Arabica::DOM::Document<std::string> Data::toDocument() {
Arabica::DOM::Document<std::string> Event::toDocument() {
Arabica::DOM::DOMImplementation<std::string> domFactory = Arabica::SimpleDOM::DOMImplementation<std::string>::getDOMImplementation();
- Arabica::DOM::Document<std::string> document = Data::toDocument();
+ Arabica::DOM::Document<std::string> document = data.toDocument();
Arabica::DOM::Element<std::string> scxmlMsg = document.getDocumentElement();
@@ -178,17 +179,47 @@ Data Data::fromXML(const std::string& xmlString) {
Data Data::fromJSON(const std::string& jsonString) {
Data data;
+ jsmn_parser p;
- // unimplemented
-// assert(false);
+ jsmntok_t* t = NULL;
- jsmn_parser p;
- jsmntok_t t[1024];
- memset(&t, 0, sizeof(t));
- jsmn_init(&p);
+ // we do not know the number of tokens beforehand, start with something sensible and increase
+ int rv;
+ int frac = 32; // this will get decreased to 16 to first iteration for 1/16 length/token ratio
+ do {
+ jsmn_init(&p);
+
+ frac /= 2;
+ int nrTokens = jsonString.size() / frac;
+ if (t != NULL) {
+ free(t);
+// LOG(INFO) << "Increasing JSON length to token ratio to 1/" << frac;
+ }
+ t = (jsmntok_t*)malloc(nrTokens * sizeof(jsmntok_t));
+ if (t == NULL) {
+ LOG(ERROR) << "Cannot parse JSON, ran out of memory!";
+ return data;
+ }
+ memset(t, 0, nrTokens * sizeof(jsmntok_t));
+
+ rv = jsmn_parse(&p, jsonString.c_str(), t, nrTokens);
+ } while (rv == JSMN_ERROR_NOMEM && frac > 1);
- int rv = jsmn_parse(&p, jsonString.c_str(), t, 1024);
if (rv != 0) {
+ switch (rv) {
+ case JSMN_ERROR_NOMEM:
+ LOG(ERROR) << "Cannot parse JSON, not enough tokens were provided!";
+ break;
+ case JSMN_ERROR_INVAL:
+ LOG(ERROR) << "Cannot parse JSON, invalid character inside JSON string!";
+ break;
+ case JSMN_ERROR_PART:
+ LOG(ERROR) << "Cannot parse JSON, the string is not a full JSON packet, more bytes expected!";
+ break;
+ default:
+ break;
+ }
+ free(t);
return data;
}
@@ -216,7 +247,7 @@ Data Data::fromJSON(const std::string& jsonString) {
// there are no more tokens
if (t[currTok].end == 0 || tokenStack.empty())
break;
-
+
// next token starts after current one => pop
if (t[currTok].end > tokenStack.back().end)
tokenStack.pop_back();
@@ -233,6 +264,8 @@ Data Data::fromJSON(const std::string& jsonString) {
}
} while (true);
+
+ free(t);
return data;
}
@@ -271,7 +304,7 @@ Event Event::fromXML(const std::string& xmlString) {
break;
}
}
- event.compound[key] = Data(value, VERBATIM);
+ event.data.compound[key] = Data(value, Data::VERBATIM);
}
}
}
@@ -401,7 +434,7 @@ std::ostream& operator<< (std::ostream& os, const Event& event) {
if (event.origintype.size() > 0)
os << indent << " origintype: " << event.origintype << std::endl;
_dataIndentation++;
- os << indent << " data: " << (Data)event << std::endl;
+ os << indent << " data: " << event.data << std::endl;
_dataIndentation--;
return os;
}