summaryrefslogtreecommitdiffstats
path: root/src/uscxml/messages/Event.h
blob: 6697bb9c24b51efda9aba8a4057ff4467f15b6c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
 *  @file
 *  @author     2012-2014 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
 *  @copyright  Simplified BSD
 *
 *  @cond
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the FreeBSD license as published by the FreeBSD
 *  project.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 *  You should have received a copy of the FreeBSD license along with this
 *  program. If not, see <http://www.opensource.org/licenses/bsd-license>.
 *  @endcond
 */

#ifndef EVENT_H_6174D929
#define EVENT_H_6174D929

#include "uscxml/messages/Data.h"

#define ERROR_EXECUTION(identifier, cause) \
	uscxml::Event identifier; \
	identifier.data.compound["cause"] = uscxml::Data(cause, uscxml::Data::VERBATIM); \
	identifier.name = "error.execution"; \
	identifier.eventType = uscxml::Event::PLATFORM;

#define ERROR_EXECUTION2(identifier, cause, node) \
	uscxml::Event identifier; \
	identifier.data.compound["cause"] = uscxml::Data(cause, uscxml::Data::VERBATIM); \
	identifier.name = "error.execution"; \
	identifier.data.compound["xpath"] = uscxml::Data(DOMUtils::xPathForNode(node), uscxml::Data::VERBATIM); \
	identifier.eventType = uscxml::Event::PLATFORM;

#define ERROR_COMMUNICATION(identifier, cause) \
	uscxml::Event identifier; \
	identifier.data.compound["cause"] = uscxml::Data(cause, uscxml::Data::VERBATIM); \
	identifier.name = "error.communication"; \
	identifier.eventType = uscxml::Event::PLATFORM;

#define ERROR_COMMUNICATION2(identifier, cause, node) \
	uscxml::Event identifier; \
	identifier.data.compound["cause"] = uscxml::Data(cause, uscxml::Data::VERBATIM); \
	identifier.name = "error.communication"; \
	identifier.data.compound["xpath"] = uscxml::Data(DOMUtils::xPathForNode(node), uscxml::Data::VERBATIM); \
	identifier.eventType = uscxml::Event::PLATFORM;

#define ERROR_EXECUTION_THROW(cause) \
{\
	ERROR_EXECUTION(exc, cause); \
	throw exc;\
}

#define ERROR_EXECUTION_THROW2(cause, node) \
{\
	ERROR_EXECUTION2(exc, cause, node); \
	throw exc;\
}

#define ERROR_COMMUNICATION_THROW(cause) \
{\
	ERROR_COMMUNICATION(exc, cause); \
	throw exc;\
}

#define ERROR_COMMUNICATION_THROW2(cause, node) \
{\
	ERROR_COMMUNICATION(exc, cause, node); \
	throw exc;\
}

namespace uscxml {

class USCXML_API Event {
public:
	enum Type {
		INTERNAL = 1,
		EXTERNAL = 2,
		PLATFORM = 3
	};

	Event() : eventType(INTERNAL), hideSendId(false) {}
	Event(const std::string& name, Type type = INTERNAL) : name(name), eventType(type), hideSendId(false) {}
	Event(const Arabica::DOM::Node<std::string>& xmlString) : eventType(INTERNAL), hideSendId(false) {};
	bool operator< (const Event& other) const     {
		return this < &other;
	}

	bool operator==(const Event& other) const     {
		return (this->name == other.name &&
		        this->sendid == other.sendid &&
		        this->invokeid == other.invokeid &&
		        this->data == other.data);
	}
	bool operator!=(const Event& other) const     {
		return !(*this == other);
	}

	std::string getName() {
		return name;
	}
	void setName(const std::string& name) {
		this->name = name;
	}

	Type getEventType() {
		return eventType;
	}
	void setEventType(const Type type) {
		this->eventType = type;
	}

	std::string getOrigin() {
		return origin;
	}
	void setOrigin(const std::string& origin) {
		this->origin = origin;
	}

	std::string getOriginType() {
		return origintype;
	}
	void setOriginType(const std::string& originType) {
		this->origintype = originType;
	}

	Arabica::DOM::Node<std::string> getDOM() {
		return dom;
	}
	void setDOM(const Arabica::DOM::Node<std::string>& dom) {
		this->dom = dom;
	}

//	Arabica::DOM::Node<std::string> getFirstDOMElement() const;
//	Arabica::DOM::Document<std::string> getStrippedDOM() const;
//
//	static Arabica::DOM::Node<std::string> getFirstDOMElement(const Arabica::DOM::Document<std::string> dom);
//	static Arabica::DOM::Document<std::string> getStrippedDOM(const Arabica::DOM::Document<std::string> dom);

	std::string getRaw() {
		return raw;
	}
	void setRaw(const std::string& raw) {
		this->raw = raw;
	}

	std::string getContent() {
		return content;
	}
	void setContent(const std::string& content) {
		this->content = content;
	}

	std::string getXML() {
		return xml;
	}
	void setXML(const std::string& xml) {
		this->xml = xml;
	}

	std::string getSendId() {
		return sendid;
	}
	void setSendId(const std::string& sendId) {
		this->sendid = sendId;
	}

	std::string getInvokeId() {
		return invokeid;
	}
	void setInvokeId(const std::string& invokeId) {
		this->invokeid = invokeId;
	}

	Data getData() {
		return data;
	}
	void setData(const Data& data) {
		this->data = data;
	}

	void initContent(const std::string& content);

	static Event fromXML(const std::string& xmlString);
	Arabica::DOM::Document<std::string> toDocument();
	std::string toXMLString();

	std::map<std::string, Data>& getNameList() {
		return namelist;
	}
	std::multimap<std::string, Data>& getParams() {
		return params;
	}

	void setNameList(const std::map<std::string, Data>& nameList) {
		this->namelist = nameList;
	}
	void setParams(const std::multimap<std::string, Data>& params) {
		this->params = params;
	}

	typedef std::multimap<std::string, Data> params_t;
	typedef std::map<std::string, Data> namelist_t;

	static bool getParam(params_t params, const std::string& name, Data& target) {
		if (params.find(name) != params.end()) {
			target = params.find(name)->second;
			return true;
		}
		return false;
	}

	static bool getParam(params_t params, const std::string& name, std::list<Data>& target) {
		if (params.find(name) != params.end()) {
			std::pair<params_t::iterator, params_t::iterator> rangeIter = params.equal_range(name);
			while(rangeIter.first != rangeIter.second) {
				target.push_back(rangeIter.first->second);
				rangeIter.first++;
			}
			return true;
		}
		return false;
	}

	template <typename T> static bool getParam(params_t params, const std::string& name, T& target) {
		if (params.find(name) != params.end()) {
			target = boost::lexical_cast<T>(params.find(name)->second.atom);
			return true;
		}
		return false;
	}

	static bool getParam(params_t params, const std::string& name, bool& target) {
		if (params.find(name) != params.end()) {
			target = true;
			if (iequals(params.find(name)->second.atom, "false")) {
				target = false;
			} else if(iequals(params.find(name)->second.atom, "off")) {
				target = false;
			} else if(iequals(params.find(name)->second.atom, "no")) {
				target = false;
			} else if(iequals(params.find(name)->second.atom, "0")) {
				target = false;
			}
			return true;
		}
		return false;
	}

	template <typename T> static bool getParam(params_t params, const std::string& name, std::list<T>& target) {
		if (params.find(name) != params.end()) {
			std::pair<params_t::iterator, params_t::iterator> rangeIter = params.equal_range(name);
			while(rangeIter.first != rangeIter.second) {
				target.push_back(boost::lexical_cast<T>(rangeIter.first->second.atom));
				rangeIter.first++;
			}
			return true;
		}
		return false;
	}


#ifdef SWIGIMPORTED
protected:
#endif

	std::string raw;
	std::string xml;
	std::string name;
	Type eventType;
	std::string origin;
	std::string origintype;
	Arabica::DOM::Node<std::string> dom;
	std::string sendid;
	bool hideSendId;
	std::string invokeid;
	Data data;
	std::string content;
	std::map<std::string, Data> namelist;
	std::multimap<std::string, Data> params;

	friend USCXML_API std::ostream& operator<< (std::ostream& os, const Event& event);
};

USCXML_API std::ostream& operator<< (std::ostream& os, const Event& event);

}

#endif /* end of include guard: EVENT_H_6174D929 */