summaryrefslogtreecommitdiffstats
path: root/src/uscxml/Message.cpp
blob: c79e08ae4c1d5ad037cd47e386016fe0996277aa (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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/**
 *  @file
 *  @author     2012-2013 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
 */

#include "uscxml/config.h"

#include "uscxml/Common.h"
#include "uscxml/Message.h"
#include "uscxml/DOMUtils.h"
#include <DOM/SAX2DOM/SAX2DOM.hpp>
#include <SAX/helpers/CatchErrorHandler.hpp>
#include <glog/logging.h>

#include <boost/algorithm/string.hpp>

#ifdef HAS_STRING_H
#include <string.h>
#endif

extern "C" {
#include "jsmn.h" // minimal json parser
}

namespace uscxml {

static int _dataIndentation = 1;

Blob::~Blob() {
	free(data);
}

Blob::Blob(size_t _size) {
	data = (char*)malloc(_size);
	memset(data, 0, _size);
	size = _size;
}

Blob::Blob(void* _data, size_t _size, const std::string& _mimeType, bool adopt) {
	if (adopt) {
		data = (char*)_data;
	} else {
		data = (char*)malloc(_size);
		memcpy(data, _data, _size);
	}
	mimeType = _mimeType;
	size = _size;
}

Data::Data(const char* _data, size_t _size, const std::string& mimeType, bool adopt) {
	binary = boost::shared_ptr<Blob>(new Blob((void*)_data, _size, mimeType, adopt));
}

Data::Data(const Arabica::DOM::Node<std::string>& dom) {
	// we may need to convert some keys to arrays if we have the same name as an element
	std::map<std::string, std::list<Data> > arrays;
//  Interpreter::dump(dom);

	if (dom.hasAttributes()) {
		Arabica::DOM::NamedNodeMap<std::string> attributes = dom.getAttributes();
		for (int i = 0; i < attributes.getLength(); i++) {
			Arabica::DOM::Node<std::string> attribute = attributes.item(i);
//      Interpreter::dump(attribute);

			assert(attribute.getNodeType() == Arabica::DOM::Node_base::ATTRIBUTE_NODE);
			std::string key = attribute.getLocalName();
			std::string value = attribute.getNodeValue();
			compound[key] = Data(value, VERBATIM);
		}
	}

	if (dom.hasChildNodes()) {
		Arabica::DOM::NodeList<std::string> children = dom.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			Arabica::DOM::Node<std::string> child = children.item(i);
//      Interpreter::dump(child);
			std::string key;
			switch (child.getNodeType()) {
			case Arabica::DOM::Node_base::ELEMENT_NODE:
				key = TAGNAME(child);
				break;
			case Arabica::DOM::Node_base::ATTRIBUTE_NODE:
				key = ((Arabica::DOM::Attr<std::string>)child).getName();
				break;
			case Arabica::DOM::Node_base::TEXT_NODE:
			default:
				break;
			}
			if (key.length() == 0)
				continue;

			if (compound.find(key) != compound.end()) {
				// we already have such a key .. make it an array after we processed all children
				arrays[key].push_back(Data(child));
			} else {
				compound[key] = Data(child);
			}
		}
	} else {
		atom = dom.getNodeValue();
		type = VERBATIM;
	}

	std::map<std::string, std::list<Data> >::iterator arrayIter = arrays.begin();
	while(arrayIter != arrays.end()) {
		assert(compound.find(arrayIter->first) != compound.end());
		Data arrayData;
		arrays[arrayIter->first].push_front(compound[arrayIter->first]);
		arrayData.array = arrays[arrayIter->first];
		compound[arrayIter->first] = arrayData;
	}
}

Arabica::DOM::Document<std::string> Data::toDocument() {
	Arabica::DOM::DOMImplementation<std::string> domFactory = Arabica::SimpleDOM::DOMImplementation<std::string>::getDOMImplementation();
	Arabica::DOM::Document<std::string> document = domFactory.createDocument("http://www.w3.org/2005/07/scxml", "message", 0);
	Arabica::DOM::Element<std::string> scxmlMsg = document.getDocumentElement();
	scxmlMsg.setPrefix("scxml");
	scxmlMsg.setAttribute("version", "1.0");

	if (compound.size() > 0 || array.size() > 0) {
		Arabica::DOM::Element<std::string> payloadElem = document.createElementNS("http://www.w3.org/2005/07/scxml", "scxml:payload");
		scxmlMsg.appendChild(payloadElem);

		// we do not support nested attibutes
		if (compound.size() > 0) {
			std::map<std::string, Data>::iterator compoundIter = compound.begin();
			while(compoundIter != compound.end()) {
				if (compoundIter->second.atom.size() > 0) {
					Arabica::DOM::Element<std::string> propertyElem = document.createElementNS("http://www.w3.org/2005/07/scxml", "scxml:property");
					propertyElem.setAttribute("name", compoundIter->first);
					Arabica::DOM::Text<std::string> textElem = document.createTextNode(compoundIter->second.atom);
					propertyElem.appendChild(textElem);
					payloadElem.appendChild(propertyElem);
				}
				compoundIter++;
			}
		}
	}
	return document;
}

//Arabica::DOM::Node<std::string> Event::getFirstDOMElement() const {
//	return getFirstDOMElement(dom);
//}
//
//Arabica::DOM::Document<std::string> Event::getStrippedDOM() const {
//	return getStrippedDOM(dom);
//}

//Arabica::DOM::Node<std::string> Event::getFirstDOMElement(const Arabica::DOM::Document<std::string> dom) {
//	Arabica::DOM::Node<std::string> data = dom.getDocumentElement().getFirstChild();
//	while (data) {
//		if (data.getNodeType() == Arabica::DOM::Node_base::TEXT_NODE) {
//			std::string trimmed = boost::trim_copy(data.getNodeValue());
//			if (trimmed.length() == 0) {
//				data = data.getNextSibling();
//				continue;
//			}
//		}
//		break;
//	}
//	return data;
//}
//
//Arabica::DOM::Document<std::string> Event::getStrippedDOM(const Arabica::DOM::Document<std::string> dom) {
//	Arabica::DOM::DOMImplementation<std::string> domFactory = Arabica::SimpleDOM::DOMImplementation<std::string>::getDOMImplementation();
//	Arabica::DOM::Document<std::string> document = domFactory.createDocument("", "", 0);
//	if (dom) {
//		document.getDocumentElement().appendChild(document.importNode(getFirstDOMElement(dom), true));
//	}
//	return document;
//}

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::Element<std::string> scxmlMsg = document.getDocumentElement();


	scxmlMsg.setAttribute("source", origin);
	scxmlMsg.setAttribute("name", name);

	return document;
}

Arabica::DOM::Document<std::string> SendRequest::toDocument() {
	Arabica::DOM::DOMImplementation<std::string> domFactory = Arabica::SimpleDOM::DOMImplementation<std::string>::getDOMImplementation();
	Arabica::DOM::Document<std::string> document = Event::toDocument();
	Arabica::DOM::Element<std::string> scxmlMsg = document.getDocumentElement();

	// add params and namelist
	if (params.size() > 0 || namelist.size() > 0) {
		Arabica::DOM::NodeList<std::string> payload = scxmlMsg.getElementsByTagName("scxml:payload");
		if (payload.getLength() == 0) {
			Arabica::DOM::Element<std::string> payloadElem = document.createElementNS("http://www.w3.org/2005/07/scxml", "scxml:payload");
			scxmlMsg.appendChild(payloadElem);
		}
		Arabica::DOM::Node<std::string> payloadElem = scxmlMsg.getElementsByTagName("scxml:payload").item(0);

		// add parameters
		std::multimap<std::string, Data>::iterator paramIter = params.begin();
		while(paramIter != params.end()) {
			Arabica::DOM::Element<std::string> propertyElem = document.createElementNS("http://www.w3.org/2005/07/scxml", "scxml:property");
			propertyElem.setAttribute("name", paramIter->first);
			// this is simplified - Data might be more elaborate than a simple string atom
			Arabica::DOM::Text<std::string> textElem = document.createTextNode(paramIter->second.atom);
			propertyElem.appendChild(textElem);
			payloadElem.appendChild(propertyElem);
			paramIter++;
		}

		// add namelist elements
		std::map<std::string, Data>::iterator namelistIter = namelist.begin();
		while(namelistIter != namelist.end()) {
			Arabica::DOM::Element<std::string> propertyElem = document.createElementNS("http://www.w3.org/2005/07/scxml", "scxml:property");
			propertyElem.setAttribute("name", namelistIter->first);
			// this is simplified - Data might be more elaborate than a simple string atom
			Arabica::DOM::Text<std::string> textElem = document.createTextNode(namelistIter->second.atom);
			propertyElem.appendChild(textElem);
			payloadElem.appendChild(propertyElem);
			namelistIter++;
		}

	}

	scxmlMsg.setAttribute("sendid", sendid);

	return document;
}

Arabica::DOM::Document<std::string> InvokeRequest::toDocument() {
	Arabica::DOM::DOMImplementation<std::string> domFactory = Arabica::SimpleDOM::DOMImplementation<std::string>::getDOMImplementation();
	Arabica::DOM::Document<std::string> document = Event::toDocument();
	Arabica::DOM::Element<std::string> scxmlMsg = document.getDocumentElement();

	scxmlMsg.setAttribute("invokeid", invokeid);

	return document;
}

Data Data::fromXML(const std::string& xmlString) {
	return Data();
}

Data Data::fromJSON(const std::string& jsonString) {
	Data data;

	std::string trimmed = jsonString;
	boost::trim(trimmed);

	if (trimmed.length() == 0)
		return data;

	if (trimmed.find_first_of("{[") != 0)
		return data;

	jsmn_parser p;

	jsmntok_t* t = NULL;

	// we do not know the number of tokens beforehand, start with something sensible and increase
	int rv;
	int frac = 16; // length/token ratio
	do {
		jsmn_init(&p);

		frac /= 2;
		int nrTokens = trimmed.size() / frac;
		if (t != NULL) {
			free(t);
//      LOG(INFO) << "Increasing JSON length to token ratio to 1/" << frac;
		}
		t = (jsmntok_t*)malloc((nrTokens + 1) * sizeof(jsmntok_t));
		if (t == NULL) {
			LOG(ERROR) << "Cannot parse JSON, ran out of memory!";
			return data;
		}
		memset(t, 0, (nrTokens + 1) * sizeof(jsmntok_t));

		rv = jsmn_parse(&p, trimmed.c_str(), t, nrTokens);
	} while (rv == JSMN_ERROR_NOMEM && frac > 1);

	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;
	}

	if (t[0].end != trimmed.length())
		return data;

//	jsmntok_t* token = t;
//	while(token->end) {
//		std::cout << trimmed.substr(token->start, token->end - token->start) << std::endl;
//		std::cout << "------" << std::endl;
//		token++;
//	}

	std::list<Data*> dataStack;
	std::list<jsmntok_t> tokenStack;
	dataStack.push_back(&data);

	size_t currTok = 0;
	do {
//		jsmntok_t t2 = t[currTok];
//		std::string value = trimmed.substr(t[currTok].start, t[currTok].end - t[currTok].start);
		switch (t[currTok].type) {
		case JSMN_STRING:
			dataStack.back()->type = Data::VERBATIM;
		case JSMN_PRIMITIVE:
			dataStack.back()->atom = trimmed.substr(t[currTok].start, t[currTok].end - t[currTok].start);
			dataStack.pop_back();
			currTok++;
			break;
		case JSMN_OBJECT:
		case JSMN_ARRAY:
			tokenStack.push_back(t[currTok]);
			currTok++;
			break;
		}

//		t2 = t[currTok];
//		value = trimmed.substr(t[currTok].start, t[currTok].end - t[currTok].start);

		// there are no more tokens
		if (t[currTok].end == 0 || tokenStack.empty())
			break;

		// next token starts after current one => pop
		while (t[currTok].end > tokenStack.back().end) {
			tokenStack.pop_back();
			dataStack.pop_back();
		}

		if (tokenStack.back().type == JSMN_OBJECT && (t[currTok].type == JSMN_PRIMITIVE || t[currTok].type == JSMN_STRING)) {
			// grab key and push new data
			dataStack.push_back(&(dataStack.back()->compound[trimmed.substr(t[currTok].start, t[currTok].end - t[currTok].start)]));
			currTok++;
		}
		if (tokenStack.back().type == JSMN_ARRAY) {
			// push new index
			dataStack.back()->array.push_back(Data());
			dataStack.push_back(&(dataStack.back()->array.back()));
		}

	} while (true);

	free(t);
	return data;
}

void Event::initContent(const std::string& content) {
	// try to parse as JSON
	Data json = Data::fromJSON(content);
	if (json) {
		data = json;
		return;
	}

	// try to parse as XML
	Arabica::SAX2DOM::Parser<std::string> parser;
	Arabica::SAX::CatchErrorHandler<std::string> errorHandler;
	parser.setErrorHandler(errorHandler);

	std::istringstream is(content);
	Arabica::SAX::InputSource<std::string> inputSource;
	inputSource.setByteStream(is);
	if (parser.parse(inputSource)) {
		dom = parser.getDocument();
		return;
	}

	this->content = content;
}

Event Event::fromXML(const std::string& xmlString) {
	Arabica::SAX2DOM::Parser<std::string> eventParser;
	Arabica::SAX::CatchErrorHandler<std::string> errorHandler;
	eventParser.setErrorHandler(errorHandler);

	std::istringstream is(xmlString);
	Arabica::SAX::InputSource<std::string> inputSource;
	inputSource.setByteStream(is);

	Event event;
	if(eventParser.parse(inputSource) && eventParser.getDocument().hasChildNodes()) {
		Arabica::DOM::Element<std::string> scxmlMsg = eventParser.getDocument().getDocumentElement();
		if (HAS_ATTR(scxmlMsg, "name"))
			event.name = ATTR(scxmlMsg, "name");
		if (HAS_ATTR(scxmlMsg, "sendid"))
			event.sendid = ATTR(scxmlMsg, "sendid");

		Arabica::DOM::NodeList<std::string> payloads = scxmlMsg.getElementsByTagName("scxml:payload");
		if (payloads.getLength() > 0) {
			Arabica::DOM::Node<std::string> payload = payloads.item(0);
			if (payload.getNodeType() == Arabica::DOM::Node_base::ELEMENT_NODE) {
				Arabica::DOM::Element<std::string> payloadElem = (Arabica::DOM::Element<std::string>)payload;
				Arabica::DOM::NodeList<std::string> properties = payloadElem.getElementsByTagName("scxml:property");
				if (properties.getLength() > 0) {
					for (int i = 0; i < properties.getLength(); i++) {
						if (HAS_ATTR(properties.item(i), "name")) {
							std::string key = ATTR(properties.item(i), "name");
							std::string value;
							Arabica::DOM::NodeList<std::string> childs = properties.item(i).getChildNodes();
							for (int j = 0; j < childs.getLength(); j++) {
								if (childs.item(j).getNodeType() == Arabica::DOM::Node_base::TEXT_NODE) {
									value = childs.item(j).getNodeValue();
									break;
								}
							}
							event.data.compound[key] = Data(value, Data::VERBATIM);
						}
					}
				}
			}
		}
	}
	return event;
}

SendRequest SendRequest::fromXML(const std::string& xmlString) {
	Event::fromXML(xmlString);
	return SendRequest();
}

InvokeRequest InvokeRequest::fromXML(const std::string& xmlString) {
	Event::fromXML(xmlString);
	return InvokeRequest();
}

#ifndef SWIGJAVA
std::ostream& operator<< (std::ostream& os, const InvokeRequest& invokeReq) {

	std::string indent;
	for (int i = 0; i < _dataIndentation; i++) {
		indent += "  ";
	}

	os << indent << "InvokeReq" << (invokeReq.autoForward ? " with autoforward" : "") << std::endl;

	if (invokeReq.type.size() > 0)
		os << indent << "  type: " << invokeReq.type << std::endl;

	if (invokeReq.src.size() > 0)
		os<< indent  << "  src: " << invokeReq.src << std::endl;

	if (invokeReq.namelist.size() > 0) {
		os << indent << "  namelist: " << std::endl;
		InvokeRequest::namelist_t::const_iterator namelistIter = invokeReq.namelist.begin();
		while(namelistIter != invokeReq.namelist.end()) {
			os << indent << "    " << namelistIter->first << ": " << namelistIter->second << std::endl;
			namelistIter++;
		}
	}

	if (invokeReq.params.size() > 0) {
		os << indent << "  params: " << std::endl;
		SendRequest::params_t::const_iterator paramIter = invokeReq.params.begin();
		while(paramIter != invokeReq.params.end()) {
			os << indent << "    " << paramIter->first << ": " << paramIter->second << std::endl;
			paramIter++;
		}
	}

	if (invokeReq.content.size() > 0)
		os << indent << "  content: " << invokeReq.content << std::endl;

	_dataIndentation++;
	os << (Event)invokeReq;
	_dataIndentation--;
	return os;

}
#endif


#ifndef SWIGJAVA
std::ostream& operator<< (std::ostream& os, const SendRequest& sendReq) {

	std::string indent;
	for (int i = 0; i < _dataIndentation; i++) {
		indent += "  ";
	}

	os << indent << "SendReq" << std::endl;

	if (sendReq.target.size() > 0)
		os << indent << "  target: " << sendReq.target << std::endl;

	if (sendReq.type.size() > 0)
		os << indent << "  type: " << sendReq.type << std::endl;

	if (sendReq.delayMs > 0)
		os<< indent  << "  delay: " << sendReq.delayMs << std::endl;

	if (sendReq.namelist.size() > 0) {
		os << indent << "  namelist: " << std::endl;
		SendRequest::namelist_t::const_iterator namelistIter = sendReq.namelist.begin();
		while(namelistIter != sendReq.namelist.end()) {
			os << indent << "    " << namelistIter->first << ": " << namelistIter->second << std::endl;
			namelistIter++;
		}
	}

	if (sendReq.params.size() > 0) {
		os << indent << "  params: " << std::endl;
		SendRequest::params_t::const_iterator paramIter = sendReq.params.begin();
		while(paramIter != sendReq.params.end()) {
			os << indent << "    " << paramIter->first << ": " << paramIter->second << std::endl;
			paramIter++;
		}
	}

	if (sendReq.content.size() > 0)
		os << indent << "  content: " << sendReq.content << std::endl;

	_dataIndentation++;
	os << (Event)sendReq;
	_dataIndentation--;
	return os;

}
#endif

#ifndef SWIGJAVA
std::ostream& operator<< (std::ostream& os, const Event& event) {
	std::string indent;
	for (int i = 0; i < _dataIndentation; i++) {
		indent += "  ";
	}

	os << indent << (event.eventType == Event::EXTERNAL ? "External" : "Internal") << " Event " << (event.dom ? "with DOM attached" : "") << std::endl;

	if (event.name.size() > 0)
		os << indent << "  name: " << event.name << std::endl;
	if (event.origin.size() > 0)
		os << indent << "  origin: " << event.origin << std::endl;
	if (event.origintype.size() > 0)
		os << indent << "  origintype: " << event.origintype << std::endl;
	_dataIndentation++;
	os << indent << "  data: " << event.data << std::endl;
	_dataIndentation--;
	return os;
}
#endif

#ifndef SWIGJAVA
std::ostream& operator<< (std::ostream& os, const Data& data) {
	os << Data::toJSON(data);
	return os;
}
#endif

std::string Data::toJSON(const Data& data) {
	std::stringstream os;
	std::string indent;
	for (int i = 0; i < _dataIndentation; i++) {
		indent += "  ";
	}
	if (false) {
	} else if (data.compound.size() > 0) {
		int longestKey = 0;
		std::map<std::string, Data>::const_iterator compoundIter = data.compound.begin();
		while(compoundIter != data.compound.end()) {
			if (compoundIter->first.size() > longestKey)
				longestKey = compoundIter->first.size();
			compoundIter++;
		}
		std::string keyPadding;
		for (unsigned int i = 0; i < longestKey; i++)
			keyPadding += " ";

		std::string seperator;
		os << std::endl << indent << "{";
		compoundIter = data.compound.begin();
		while(compoundIter != data.compound.end()) {
			os << seperator << std::endl << indent << "  \"" << compoundIter->first << "\": " << keyPadding.substr(0, longestKey - compoundIter->first.size());
			_dataIndentation += 1;
			os << compoundIter->second;
			_dataIndentation -= 1;
			seperator = ", ";
			compoundIter++;
		}
		os << std::endl << indent << "}";
	} else if (data.array.size() > 0) {

		std::string seperator;
		os << std::endl << indent << "[";
		std::list<Data>::const_iterator arrayIter = data.array.begin();
		while(arrayIter != data.array.end()) {
			_dataIndentation += 1;
			os << seperator << *arrayIter;
			_dataIndentation -= 1;
			seperator = ", ";
			arrayIter++;
		}
		os << "]";
	} else if (data.atom.size() > 0) {
		if (data.type == Data::VERBATIM) {
			os << "\"" << data.atom << "\"";
		} else {
			os << data.atom;
		}
	} else if (data.node) {
		if (data.type == Data::VERBATIM) {
			os << "";
		} else {
			os << data.atom;
		}
	} else {
		os << "undefined";
	}
	return os.str();
}

}