summaryrefslogtreecommitdiffstats
path: root/src/uscxml/util/DOM.h
blob: 74133e8d010ed6a72f0b2376220b351227f79741 (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
/**
 *  @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
 */

#ifndef DOMUTILS_H_WK0WAEA7
#define DOMUTILS_H_WK0WAEA7

#include <set>
#include <list>
#include <string>

#include "uscxml/Common.h"
#include <xercesc/util/XMLString.hpp>
#include <xercesc/dom/DOM.hpp>

#define HAS_ATTR(elem, attr) (elem)->hasAttribute(attr)
#define HAS_ATTR_CAST(elem, attr) HAS_ATTR(static_cast<const DOMElement*>(elem), attr)
#define ATTR(elem, attr) std::string(X((elem)->getAttribute(attr)))
#define ATTR_CAST(elem, attr) ATTR(static_cast<const DOMElement*>(elem), attr)
#define TAGNAME(elem) std::string(X((elem)->getTagName()))
#define TAGNAME_CAST(elem) TAGNAME(static_cast<const DOMElement*>(elem))
#define LOCALNAME(elem) std::string(X((elem)->getLocalName()))
#define LOCALNAME_CAST(elem) LOCALNAME(static_cast<const DOMElement*>(elem))

namespace uscxml {

class USCXML_API DOMUtils {
public:

	static const XERCESC_NS::DOMElement* getNearestAncestor(const XERCESC_NS::DOMNode* node, const std::string tagName);
	static bool isDescendant(const XERCESC_NS::DOMNode* s1, const XERCESC_NS::DOMNode* s2);

	static bool hasIntersection(const std::list<XERCESC_NS::DOMElement*>& l1,
	                            const std::list<XERCESC_NS::DOMElement*>& l2);
	static bool isMember(const XERCESC_NS::DOMElement* node, const std::list<XERCESC_NS::DOMElement*>& list);
	static bool isMember(const XERCESC_NS::DOMNode* node, const std::list<XERCESC_NS::DOMNode*>& list);
	static bool isMember(const XERCESC_NS::DOMNode* node, const XERCESC_NS::DOMNodeList* list);

	static std::string xPathForNode(const XERCESC_NS::DOMNode* node, const std::string& ns = "");
	static std::string idForNode(const XERCESC_NS::DOMNode* node);

	static std::list<XERCESC_NS::DOMElement*> inPostFixOrder(const std::set<std::string>& elements,
	        const XERCESC_NS::DOMElement* root,
	        const bool includeEmbeddedDoc = false) {
		return filterElementGeneric(elements, root, POSTFIX, includeEmbeddedDoc, true);
	}

	static std::list<XERCESC_NS::DOMElement*> inDocumentOrder(const std::set<std::string>& elements,
	        const XERCESC_NS::DOMElement* root,
	        const bool includeEmbeddedDoc = false) {
		return filterElementGeneric(elements, root, DOCUMENT, includeEmbeddedDoc, true);
	}

	static std::list<XERCESC_NS::DOMElement*> filterChildElements(const std::string& tagName,
	        const XERCESC_NS::DOMElement* node,
	        bool recurse = false) {
		return filterElementGeneric({ tagName }, node, (recurse ? DOCUMENT : NO_RECURSE), true, false);
	}

	static std::list<XERCESC_NS::DOMElement*> filterChildElements(const std::string& tagName,
	        const std::list<XERCESC_NS::DOMElement*>& nodeSet,
	        bool recurse = false);

	static std::list<XERCESC_NS::DOMNode*> filterChildType(const XERCESC_NS::DOMNode::NodeType type,
	        const XERCESC_NS::DOMNode* node,
	        bool recurse = false) {
		return filterTypeGeneric({ type }, node, (recurse ? DOCUMENT : NO_RECURSE), true, false);

	}

	static std::list<XERCESC_NS::DOMNode*> filterChildType(const XERCESC_NS::DOMNode::NodeType type,
	        const std::list<XERCESC_NS::DOMNode*>& nodeSet,
	        bool recurse = false);

	enum Order {
		POSTFIX,
		DOCUMENT,
		NO_RECURSE
	};

	static void filterElementGeneric(const std::set<std::string>& elements,
	                                 std::list<XERCESC_NS::DOMElement*>& result,
	                                 const XERCESC_NS::DOMElement* root,
	                                 const Order order,
	                                 const bool includeEmbeddedDoc,
	                                 const bool includeRoot);

	static std::list<XERCESC_NS::DOMElement*> filterElementGeneric(const std::set<std::string>& elements,
	        const XERCESC_NS::DOMElement* root,
	        const Order order,
	        const bool includeEmbeddedDoc,
	        const bool includeRoot);

	static void filterTypeGeneric(const std::set<XERCESC_NS::DOMNode::NodeType>& types,
	                              std::list<XERCESC_NS::DOMNode*>& result,
	                              const XERCESC_NS::DOMNode* root,
	                              const Order order,
	                              const bool includeEmbeddedDoc,
	                              const bool includeRoot);

	static std::list<XERCESC_NS::DOMNode*> filterTypeGeneric(const std::set<XERCESC_NS::DOMNode::NodeType>& types,
	        const XERCESC_NS::DOMNode* root,
	        const Order order,
	        const bool includeEmbeddedDoc,
	        const bool includeRoot);

};

// create a prefix from a given element - useful for copying namespace information
#define XML_PREFIX(element) X(element->getPrefix() ? X(element->getPrefix()).str() + ":" : "")

#if 1
/**
 * @todo: More performant XercesStrings
 * https://alfps.wordpress.com/2010/05/27/cppx-xerces-strings-simplified-by-ownership-part-i/
 */

class USCXML_API X {
public :

	X(X const &other) {

		_localForm = other._localForm;
		_unicodeForm = XERCESC_NS::XMLString::replicate(other._unicodeForm);
		_deallocOther = true;
	}

	X(const XMLCh* const toTranscode) {

		if (toTranscode != NULL) {
			// Call the private transcoding method
			char* tmp = XERCESC_NS::XMLString::transcode(toTranscode);
			_localForm = std::string(tmp);
			XERCESC_NS::XMLString::release(&tmp);
		}
		_unicodeForm = NULL;
		_deallocOther = false;
	}

	X(const std::string& fromTranscode) {

		// Call the private transcoding method
		_localForm = fromTranscode;
		_unicodeForm = XERCESC_NS::XMLString::transcode(fromTranscode.c_str());
		_deallocOther = true;
	}

	X(const char* const fromTranscode) {
		// this is most unfortunate but needed with static XMLChars :(
		if (!_xercesIsInit) {
			try {
				::xercesc_3_1::XMLPlatformUtils::Initialize();
				_xercesIsInit = true;
			} catch (const XERCESC_NS::XMLException& toCatch) {
				throw ("Cannot initialize XercesC: " + X(toCatch.getMessage()).str());
			}
		}

		// Call the private transcoding method
		_localForm = fromTranscode;
		_unicodeForm = XERCESC_NS::XMLString::transcode(fromTranscode);
		_deallocOther = true;
	}

	X(char* fromTranscode) {

		// Call the private transcoding method
		_localForm = fromTranscode;
		_unicodeForm = XERCESC_NS::XMLString::transcode(fromTranscode);
		_deallocOther = true;
	}

	X() {

		_unicodeForm = NULL;
		_deallocOther = false;
	}

	~X() {

		if (_deallocOther)
			XERCESC_NS::XMLString::release(&_unicodeForm);
	}

	const std::string& str() const {
		return _localForm;
	}

	operator XMLCh* () const {
		assert(_unicodeForm != NULL); // constructor with XMLCh
		return _unicodeForm;
	}

	void operator=(X const &other) {
		_localForm = other._localForm;
		_unicodeForm = XERCESC_NS::XMLString::replicate(other._unicodeForm);
		_deallocOther = true;
	}

	bool operator==(const X& other) const {
		return (_unicodeForm == other._unicodeForm) != 0;
	}

	bool operator!=(const X& other) const {
		return !(_unicodeForm == other._unicodeForm);
	}

	bool operator<(const X& other) const {
		return XERCESC_NS::XMLString::compareString(_unicodeForm, other._unicodeForm) < 0;
	}

	operator bool () {
		return _localForm.size() > 0;
	}

	operator std::string () {
		return _localForm;
	}

protected:
	friend USCXML_API std::ostream& operator<< (std::ostream& os, const X& data);

private:
	bool _deallocOther;
	std::string _localForm;
	XMLCh* _unicodeForm;
	bool _xercesIsInit = false;
};

#else

class USCXML_API X {
public :
	X() {
	}

	void operator=(X const &other) {
		localForm = other.localForm;
		if (unicodeForm != NULL) {
			XERCESC_NS::XMLString::release(&unicodeForm);
		}
		unicodeForm = XERCESC_NS::XMLString::replicate(other.unicodeForm);
	}

	X(X const &other) {
		localForm = other.localForm;
		unicodeForm = XERCESC_NS::XMLString::replicate(other.unicodeForm);
	}

	X(const char* const toTranscode) {
		if (toTranscode != NULL) {
			localForm = toTranscode;
			unicodeForm = XERCESC_NS::XMLString::transcode(toTranscode);
		}
	}

	X(const XMLCh* toTranscode) {
		if (toTranscode != NULL) {
			unicodeForm = XERCESC_NS::XMLString::replicate(toTranscode);
			localForm = XERCESC_NS::XMLString::transcode(toTranscode);
		}
	}

	X(const std::string& toTranscode) {
		localForm = toTranscode;
		unicodeForm = XERCESC_NS::XMLString::transcode(toTranscode.c_str());
	}

	~X() {
		if (unicodeForm != NULL) {
			XERCESC_NS::XMLString::release(&unicodeForm);
		}
	}

	operator XMLCh* () const {
		return unicodeForm;
	}

	operator const std::string& () {
		return localForm;
	}

	const std::string& str() const {
		return localForm;
	}

	const XMLCh* unicode() const {
		return unicodeForm;
	}


protected:
	friend USCXML_API std::ostream& operator<< (std::ostream& os, const X& data);

private:
	XMLCh* unicodeForm = NULL;
	std::string localForm;

};

#endif

static const X kXMLCharScxml = X("scxml");
static const X kXMLCharState = X("state");
static const X kXMLCharParallel = X("parallel");
static const X kXMLCharTransition = X("transition");
static const X kXMLCharInitial = X("initial");
static const X kXMLCharFinal = X("final");
static const X kXMLCharOnEntry = X("onentry");
static const X kXMLCharOnExit = X("onexit");
static const X kXMLCharHistory = X("history");

static const X kXMLCharRaise = X("raise");
static const X kXMLCharIf = X("if");
static const X kXMLCharElseIf = X("elseif");
static const X kXMLCharElse = X("else");
static const X kXMLCharForEach = X("foreach");
static const X kXMLCharLog = X("log");

static const X kXMLCharDataModel = X("datamodel");
static const X kXMLCharData = X("data");
static const X kXMLCharAssign = X("assign");
static const X kXMLCharContent = X("content");
static const X kXMLCharParam = X("param");
static const X kXMLCharScript = X("script");

static const X kXMLCharInvokeId = X("invokeid");
static const X kXMLCharId = X("id");
static const X kXMLCharIdLocation = X("idlocation");
static const X kXMLCharEvent = X("event");
static const X kXMLCharEventExpr = X("eventexpr");
static const X kXMLCharTarget = X("target");
static const X kXMLCharTargetExpr = X("targetexpr");
static const X kXMLCharSource = X("src");
static const X kXMLCharSourceExpr = X("srcexpr");
static const X kXMLCharType = X("type");
static const X kXMLCharTypeExpr = X("typeexpr");
static const X kXMLCharDelay = X("delay");
static const X kXMLCharDelayExpr = X("delayexpr");
static const X kXMLCharSendId = X("sendid");
static const X kXMLCharSendIdExpr = X("sendidexpr");
static const X kXMLCharCond = X("cond");
static const X kXMLCharLocation = X("location");
static const X kXMLCharArray = X("array");
static const X kXMLCharItem = X("item");
static const X kXMLCharIndex = X("index");
static const X kXMLCharLabel = X("label");
static const X kXMLCharExpr = X("expr");
static const X kXMLCharNameList = X("namelist");
static const X kXMLCharAutoForward = X("autoforward");
static const X kXMLCharName = X("name");
static const X kXMLCharBinding = X("binding");

USCXML_API std::ostream& operator<< (std::ostream& os, const X& xmlString);
USCXML_API std::ostream& operator<< (std::ostream& os, const XERCESC_NS::DOMNode& node);

}


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