summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/datamodel/xpath/XPathDataModel.cpp
blob: f874c86a72ccee84f1a82f9045ab5b2f5c929024 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include "uscxml/Common.h"
#include "XPathDataModel.h"

#include "uscxml/Message.h"
#include <glog/logging.h>

#ifdef BUILD_AS_PLUGINS
#include <Pluma/Connector.hpp>
#endif

namespace uscxml {

using namespace Arabica::XPath;
using namespace Arabica::DOM;

#ifdef BUILD_AS_PLUGINS
PLUMA_CONNECTOR
bool connect(pluma::Host& host) {
	host.add( new XPathDataModelProvider() );
	return true;
}
#endif

XPathDataModel::XPathDataModel() {
}

boost::shared_ptr<DataModelImpl> XPathDataModel::create(InterpreterImpl* interpreter) {
	boost::shared_ptr<XPathDataModel> dm = boost::shared_ptr<XPathDataModel>(new XPathDataModel());
	dm->_interpreter = interpreter;
//	dm->_xpath.setVariableResolver(_varResolver);
//	dm->_xpath->setVariableCompileTimeResolver(_varCTResolver);
//	dm->_xpath->setNamespaceContext(interpreter->getNSContext());

	dm->_funcResolver.setInterpreter(interpreter);
	dm->_xpath.setFunctionResolver(dm->_funcResolver);
	dm->_xpath.setVariableResolver(dm->_varResolver);

	dm->_domFactory = Arabica::SimpleDOM::DOMImplementation<std::string>::getDOMImplementation();
	dm->_doc = dm->_domFactory.createDocument("http://www.w3.org/2005/07/scxml", "", 0);
	dm->_datamodel = dm->_doc.createElement("datamodel");
	dm->_doc.appendChild(dm->_datamodel);
	
	Element<std::string> ioProcElem = dm->_doc.createElement("data");
	ioProcElem.setAttribute("id", "_ioprocessors");
	std::map<std::string, IOProcessor>::const_iterator ioProcIter = interpreter->getIOProcessors().begin();
	while(ioProcIter != interpreter->getIOProcessors().end()) {
		Element<std::string> ioProc = dm->_doc.createElement("processor");
		ioProc.setAttribute("name", ioProcIter->first);
		
		Data ioProcData = ioProcIter->second.getDataModelVariables();
		Element<std::string> ioProcLoc = dm->_doc.createElement("location");
		Text<std::string> ioProcLocText = dm->_doc.createTextNode(ioProcData.compound["location"].atom);
		ioProcLoc.appendChild(ioProcLocText);
		ioProc.appendChild(ioProcLoc);
		ioProcElem.appendChild(ioProc);
		
		ioProcIter++;
	}
	dm->_datamodel.appendChild(ioProcElem);

	NodeSet<std::string> ioProcNodeSet;
	ioProcNodeSet.push_back(ioProcElem);
	dm->_varResolver.setVariable("_ioprocessors", ioProcNodeSet);
	
	
	Element<std::string> sessIdElem = dm->_doc.createElement("data");
	sessIdElem.setAttribute("id", "_sessionid");
	Text<std::string> sessIdText = dm->_doc.createTextNode(interpreter->getSessionId());
	sessIdElem.appendChild(sessIdText);
	dm->_datamodel.appendChild(sessIdElem);

	NodeSet<std::string> sessIdNodeSet;
	sessIdNodeSet.push_back(sessIdElem);
	dm->_varResolver.setVariable("_sessionid", sessIdNodeSet);

	
	Element<std::string> nameElem = dm->_doc.createElement("data");
	nameElem.setAttribute("id", "_name");
	Text<std::string> nameText = dm->_doc.createTextNode(interpreter->getName());
	nameElem.appendChild(nameText);
	dm->_datamodel.appendChild(nameElem);
	
	NodeSet<std::string> nameNodeSet;
	nameNodeSet.push_back(nameElem);
	dm->_varResolver.setVariable("_name", nameNodeSet);

	return dm;
}

XPathDataModel::~XPathDataModel() {
}

void XPathDataModel::pushContext() {
}

void XPathDataModel::popContext() {
}

void XPathDataModel::initialize() {
}

void XPathDataModel::setEvent(const Event& event) {
	Element<std::string> eventElem = _doc.createElement("data");
	eventElem.setAttribute("id", "_event");

	Element<std::string> eventDataElem = _doc.createElement("data");
	
	NodeSet<std::string> eventNodeSet;
	if (event.params.size() > 0) {
		std::multimap<std::string, std::string>::const_iterator paramIter = event.params.begin();
		while(paramIter != event.params.end()) {
			Element<std::string> eventParamElem = _doc.createElement("data");
			Text<std::string> eventParamText = _doc.createTextNode(paramIter->second);

			eventParamElem.setAttribute("id", paramIter->first);
			eventParamElem.appendChild(eventParamText);
			eventDataElem.appendChild(eventParamElem);
			paramIter++;
		}
	}
	if (event.namelist.size() > 0) {
		std::map<std::string, std::string>::const_iterator namelistIter = event.namelist.begin();
		while(namelistIter != event.namelist.end()) {
			Element<std::string> eventNamelistElem = _doc.createElement("data");
			Text<std::string> eventNamelistText = _doc.createTextNode(namelistIter->second);

			eventNamelistElem.setAttribute("id", namelistIter->first);
			eventNamelistElem.appendChild(eventNamelistText);
			eventDataElem.appendChild(eventNamelistElem);
			namelistIter++;
		}
	}
	if (event.content.size() > 0) {
		eventDataElem.setNodeValue(event.content);
	}
	
	eventElem.appendChild(eventDataElem);
	eventNodeSet.push_back(eventElem);
	
//	std::cout << eventElem << std::endl;
	
	// do we need to replace an existing event?
	Node<std::string> oldEventElem = _datamodel.getFirstChild();
	while(oldEventElem) {
		if (oldEventElem.getNodeType() == Node_base::ELEMENT_NODE) {
			if (HAS_ATTR(oldEventElem, "id") && boost::iequals(ATTR(oldEventElem, "id"), "_event"))
				break;
		}
		oldEventElem = oldEventElem.getNextSibling();
	}
	
	if (oldEventElem) {
		_datamodel.replaceChild(eventElem, oldEventElem);
	} else {
		_datamodel.appendChild(eventElem);
	}
	_varResolver.setVariable("_event", eventNodeSet);
}

Data XPathDataModel::getStringAsData(const std::string& content) {
	Data data;
	return data;
}

bool XPathDataModel::validate(const std::string& location, const std::string& schema) {
	return true;
}

uint32_t XPathDataModel::getLength(const std::string& expr) {
  return 0;
}

void XPathDataModel::eval(const std::string& expr) {
	XPathValue<std::string> result = _xpath.evaluate_expr(expr, _doc);
}

bool XPathDataModel::isDeclared(const std::string& expr) {
	return true;
}

bool XPathDataModel::evalAsBool(const std::string& expr) {
	XPathValue<std::string> result = _xpath.evaluate_expr(expr, _doc);
	return result.asBool();
}

std::string XPathDataModel::evalAsString(const std::string& expr) {
	XPathValue<std::string> result = _xpath.evaluate_expr(expr, _doc);
	return result.asString();
}

double XPathDataModel::evalAsNumber(const std::string& expr) {
	XPathValue<std::string> result = _xpath.evaluate_expr(expr, _doc);
	return result.asNumber();
}

void XPathDataModel::assign(const std::string& location,
														const Document<std::string>& doc,
														const Arabica::DOM::Element<std::string>& dataElem) {
	XPathValue<std::string> key = _xpath.evaluate_expr(location, _doc);
	NodeSet<std::string> nodeSet;
	nodeSet.push_back(doc.getDocumentElement());
	assign(key, nodeSet, dataElem);
}

void XPathDataModel::assign(const std::string& location,
														const Data& data,
														const Arabica::DOM::Element<std::string>& dataElem) {
//	assert(false);
//	std::cout << location << " = " << data << std::endl;
}

void XPathDataModel::assign(const std::string& location,
														const std::string& expr,
														const Arabica::DOM::Element<std::string>& dataElem) {
	std::string realExpr = (HAS_ATTR(dataElem, "expr") ? ATTR(dataElem, "expr") : expr);
	XPathValue<std::string> key = _xpath.evaluate_expr(location, _doc);
	XPathValue<std::string> value = _xpath.evaluate_expr(realExpr, _doc);
	assign(key, value, dataElem);
}

void XPathDataModel::init(const std::string& location,
													const Document<std::string>& doc,
													const Arabica::DOM::Element<std::string>& dataElem) {
	Element<std::string> container = _doc.createElement("data");
	container.setAttribute("id", location);
	Element<std::string> data = doc.getDocumentElement();
	if (data.hasChildNodes()) {
		Node<std::string> dataClone = _doc.importNode(data, true);
		container.appendChild(dataClone);
	}
	_datamodel.appendChild(container);
	
	// put data element into nodeset and bind to xpath variable
	NodeSet<std::string> nodeSet;
	nodeSet.push_back(container);
	_varResolver.setVariable(location, nodeSet);
}

void XPathDataModel::init(const std::string& location,
													const std::string& expr,
													const Arabica::DOM::Element<std::string>& dataElem) {
	Element<std::string> data = _doc.createElement("data");
	data.setAttribute("id", location);
	
	if (expr.length() > 0) {
		Text<std::string> textNode = _doc.createTextNode(expr.c_str());
		data.appendChild(textNode);
		_datamodel.appendChild(data);
	}

	// put data element into nodeset and bind to xpath variable
	NodeSet<std::string> nodeSet;
	nodeSet.push_back(data);
	_varResolver.setVariable(location, nodeSet);
}

void XPathDataModel::init(const std::string& location,
													const Data& data,
													const Arabica::DOM::Element<std::string>& dataElem) {
	assert(false);
}

void XPathDataModel::assign(XPathValue<std::string>& key,
														const XPathValue<std::string>& value,
														const Arabica::DOM::Element<std::string>& assignElem) {
	switch (value.type()) {
		case STRING:
			assign(key, value.asString(), assignElem);
			break;
		case BOOL:
			assign(key, value.asBool(), assignElem);
			break;
		case NUMBER:
			assign(key, value.asNumber(), assignElem);
			break;
		case NODE_SET:
			assign(key, value.asNodeSet(), assignElem);
			break;
		case ANY:
			throw Event("error.execution", Event::PLATFORM);
	}
}

void XPathDataModel::assign(XPathValue<std::string>& key,
														const std::string& value,
														const Arabica::DOM::Element<std::string>& assignElem) {
	switch (key.type()) {
		case NODE_SET: {
			if (key.asNodeSet().size() == 0)
				return;
			Node<std::string> node = key.asNodeSet()[0];
			switch (node.getNodeType()) {
				case Node_base::ATTRIBUTE_NODE: {
					Attr<std::string> attr(node);
					attr.setValue(value);
					break;
				}
				case Node_base::TEXT_NODE: {
					Text<std::string> text(node);
					text.setNodeValue(value);
					break;
				}
				case Node_base::ELEMENT_NODE: {
					Element<std::string> element(node);
					if (HAS_ATTR(assignElem, "type") && boost::iequals(ATTR(assignElem, "type"), "addattribute")) {
						// addattribute: Add an attribute with the name specified by 'attr'
						// and value specified by 'expr' to the node specified by 'location'.
						if (!HAS_ATTR(assignElem, "attr"))
							throw Event("error.execution", Event::PLATFORM);
						element.setAttribute(ATTR(assignElem, "attr"), value);
					}
					break;
				}
				default:
					throw Event("error.execution", Event::PLATFORM);
					break;
			}
			break;
		}
		case STRING:
		case BOOL:
		case NUMBER:
		case ANY:
			throw Event("error.execution", Event::PLATFORM);
			break;
		default:
			break;
	}
}

void XPathDataModel::assign(XPathValue<std::string>& key,
														const double value,
														const Arabica::DOM::Element<std::string>& assignElem) {
}

void XPathDataModel::assign(XPathValue<std::string>& key,
														const bool value,
														const Arabica::DOM::Element<std::string>& assignElem) {
}

void XPathDataModel::assign(XPathValue<std::string>& key,
														const NodeSet<std::string>& value,
														const Arabica::DOM::Element<std::string>& assignElem) {
	switch (key.type()) {
		case NODE_SET: {
			if (key.asNodeSet().size() == 0)
				return;
			Node<std::string> node = key.asNodeSet()[0];
			switch (node.getNodeType()) {
				case Node_base::ELEMENT_NODE: {
					Element<std::string> element(node);
					if (false) {
					} else if (HAS_ATTR(assignElem, "type") && boost::iequals(ATTR(assignElem, "type"), "firstchild")) {
						// firstchild: Insert the value specified by 'expr' before all of the children at 'location'.
						for (int i = value.size(); i; i--) {
							Node<std::string> importedNode = _doc.importNode(value[i-1], true);
							element.insertBefore(importedNode, element.getFirstChild());
						}
					} else if (HAS_ATTR(assignElem, "type") && boost::iequals(ATTR(assignElem, "type"), "lastchild")) {
						// lastchild: Insert the value specified by 'expr' after all of the children at 'location'.
						for (int i = 0; i < value.size(); i++) {
							Node<std::string> importedNode = _doc.importNode(value[i], true);
							element.appendChild(importedNode);
						}
					} else if (HAS_ATTR(assignElem, "type") && boost::iequals(ATTR(assignElem, "type"), "previoussibling")) {
						// previoussibling: Insert the value specified by 'expr' before the
						// node specified by 'location', keeping the same parent.
						Node<std::string> parent = element.getParentNode();
						if (!parent)
							throw Event("error.execution", Event::PLATFORM);
						for (int i = 0; i < value.size(); i++) {
							Node<std::string> importedNode = _doc.importNode(value[i], true);
							parent.insertBefore(importedNode, element);
						}
					} else if (HAS_ATTR(assignElem, "type") && boost::iequals(ATTR(assignElem, "type"), "nextsibling")) {
						// nextsibling: Insert the value specified by 'expr' after the node
						// specified by 'location', keeping the same parent.
						Node<std::string> parent = element.getParentNode();
						if (!parent)
							throw Event("error.execution", Event::PLATFORM);
						for (int i = value.size(); i; i--) {
							Node<std::string> importedNode = _doc.importNode(value[i-1], true);
							Node<std::string> nextSibling = element.getNextSibling();
							if (nextSibling) {
								parent.insertBefore(importedNode, element.getNextSibling());
							} else {
								parent.appendChild(importedNode);
							}
						}
					} else if (HAS_ATTR(assignElem, "type") && boost::iequals(ATTR(assignElem, "type"), "replace")) {
						// replace: Replace the node specified by 'location' by the value specified by 'expr'.
						Node<std::string> parent = element.getParentNode();
						if (!parent)
							throw Event("error.execution", Event::PLATFORM);
						if (value.size() != 1)
							throw Event("error.execution", Event::PLATFORM);
						Node<std::string> importedNode = _doc.importNode(value[0], true);
						parent.replaceChild(importedNode, element);
					} else if (HAS_ATTR(assignElem, "type") && boost::iequals(ATTR(assignElem, "type"), "delete")) {
						// delete: Delete the node specified by 'location'. ('expr' is ignored.).
						Node<std::string> parent = element.getParentNode();
						if (!parent)
							throw Event("error.execution", Event::PLATFORM);
						parent.removeChild(element);
					} else {
						// replacechildren: Replace all the children at 'location' with the value specified by 'expr'.
						while(element.hasChildNodes())
							element.removeChild(element.getChildNodes().item(0));
						for (int i = 0; i < value.size(); i++) {
							Node<std::string> importedNode = _doc.importNode(value[i], true);
							element.appendChild(importedNode);
						}
					}
					break;
				}
				default:
					throw Event("error.execution", Event::PLATFORM);
					break;
			}
			break;
		}
		case STRING:
		case BOOL:
		case NUMBER:
		case ANY:
			throw Event("error.execution", Event::PLATFORM);
			break;
	}
}

XPathValue<std::string>
	NodeSetVariableResolver::resolveVariable(const std::string& namepaceUri,
																					 const std::string& name) const {
	std::map<std::string, NodeSet<std::string> >::const_iterator n = _variables.find(name);
	if(n == _variables.end()) {
		throw Event("error.execution");
	}
	return XPathValue<std::string>(new NodeSetValue<std::string>(n->second));
}

XPathFunction<std::string>*
	XPathFunctionResolver::resolveFunction(const std::string& namespace_uri,
																				 const std::string& name,
																				 const std::vector<XPathExpression<std::string> >& argExprs) const {
	if (boost::iequals(name, "in")) {
		return new XPathFunctionIn(1, -1, argExprs, _interpreter);
	}
	return _xpathFuncRes.resolveFunction(namespace_uri, name, argExprs);
}
	
std::vector<std::pair<std::string, std::string> > XPathFunctionResolver::validNames() const {
	std::vector<std::pair<std::string, std::string> > names = _xpathFuncRes.validNames();
	names.push_back(std::make_pair("", "In"));
	return names;
}

bool XPathFunctionIn::doEvaluate(const Node<std::string>& context,
																 const ExecutionContext<std::string>& executionContext) const {
	for (int i = 0; i < argCount(); i++) {
		XPathValue<std::string> stateName = arg(i, context, executionContext);
		if (stateName.type() == STRING) {
			if (!Interpreter::isMember(_interpreter->getState(stateName.asString()), _interpreter->getConfiguration())) {
				return false;
			}
		}
	}
	return true;
}

}