DOM.h
Go to the documentation of this file.
1 
20 #ifndef DOMUTILS_H_WK0WAEA7
21 #define DOMUTILS_H_WK0WAEA7
22 
23 #include <set>
24 #include <list>
25 #include <string>
26 
27 #include "uscxml/Common.h"
28 #include <xercesc/util/XMLString.hpp>
29 #include <xercesc/dom/DOM.hpp>
30 
31 #define HAS_ATTR(elem, attr) (elem)->hasAttribute(attr)
32 #define HAS_ATTR_CAST(elem, attr) HAS_ATTR(static_cast<const DOMElement*>(elem), attr)
33 #define ATTR(elem, attr) std::string(X((elem)->getAttribute(attr)))
34 #define ATTR_CAST(elem, attr) ATTR(static_cast<const DOMElement*>(elem), attr)
35 #define TAGNAME(elem) std::string(X((elem)->getTagName()))
36 #define TAGNAME_CAST(elem) TAGNAME(static_cast<const DOMElement*>(elem))
37 #define LOCALNAME(elem) std::string(X((elem)->getLocalName()))
38 #define LOCALNAME_CAST(elem) LOCALNAME(static_cast<const DOMElement*>(elem))
39 
40 namespace uscxml {
41 
42 class USCXML_API DOMUtils {
43 public:
44 
45  static const XERCESC_NS::DOMElement* getNearestAncestor(const XERCESC_NS::DOMNode* node, const std::string tagName);
46  static bool isDescendant(const XERCESC_NS::DOMNode* s1, const XERCESC_NS::DOMNode* s2);
47 
48  static bool hasIntersection(const std::list<XERCESC_NS::DOMElement*>& l1,
49  const std::list<XERCESC_NS::DOMElement*>& l2);
50  static bool isMember(const XERCESC_NS::DOMElement* node, const std::list<XERCESC_NS::DOMElement*>& list);
51  static bool isMember(const XERCESC_NS::DOMNode* node, const std::list<XERCESC_NS::DOMNode*>& list);
52  static bool isMember(const XERCESC_NS::DOMNode* node, const XERCESC_NS::DOMNodeList* list);
53 
54  static std::string xPathForNode(const XERCESC_NS::DOMNode* node, const std::string& ns = "");
55  static std::string idForNode(const XERCESC_NS::DOMNode* node);
56 
57  static std::list<XERCESC_NS::DOMElement*> inPostFixOrder(const std::set<std::string>& elements,
58  const XERCESC_NS::DOMElement* root,
59  const bool includeEmbeddedDoc = false) {
60  return filterElementGeneric(elements, root, POSTFIX, includeEmbeddedDoc, true);
61  }
62 
63  static std::list<XERCESC_NS::DOMElement*> inDocumentOrder(const std::set<std::string>& elements,
64  const XERCESC_NS::DOMElement* root,
65  const bool includeEmbeddedDoc = false) {
66  return filterElementGeneric(elements, root, DOCUMENT, includeEmbeddedDoc, true);
67  }
68 
69  static std::list<XERCESC_NS::DOMElement*> filterChildElements(const std::string& tagName,
70  const XERCESC_NS::DOMElement* node,
71  bool recurse = false) {
72  return filterElementGeneric({ tagName }, node, (recurse ? DOCUMENT : NO_RECURSE), true, false);
73  }
74 
75  static std::list<XERCESC_NS::DOMElement*> filterChildElements(const std::string& tagName,
76  const std::list<XERCESC_NS::DOMElement*>& nodeSet,
77  bool recurse = false);
78 
79  static std::list<XERCESC_NS::DOMNode*> filterChildType(const XERCESC_NS::DOMNode::NodeType type,
80  const XERCESC_NS::DOMNode* node,
81  bool recurse = false) {
82  return filterTypeGeneric({ type }, node, (recurse ? DOCUMENT : NO_RECURSE), true, false);
83 
84  }
85 
86  static std::list<XERCESC_NS::DOMNode*> filterChildType(const XERCESC_NS::DOMNode::NodeType type,
87  const std::list<XERCESC_NS::DOMNode*>& nodeSet,
88  bool recurse = false);
89 
90  enum Order {
91  POSTFIX,
92  DOCUMENT,
93  NO_RECURSE
94  };
95 
96  static void filterElementGeneric(const std::set<std::string>& elements,
97  std::list<XERCESC_NS::DOMElement*>& result,
98  const XERCESC_NS::DOMElement* root,
99  const Order order,
100  const bool includeEmbeddedDoc,
101  const bool includeRoot);
102 
103  static std::list<XERCESC_NS::DOMElement*> filterElementGeneric(const std::set<std::string>& elements,
104  const XERCESC_NS::DOMElement* root,
105  const Order order,
106  const bool includeEmbeddedDoc,
107  const bool includeRoot);
108 
109  static void filterTypeGeneric(const std::set<XERCESC_NS::DOMNode::NodeType>& types,
110  std::list<XERCESC_NS::DOMNode*>& result,
111  const XERCESC_NS::DOMNode* root,
112  const Order order,
113  const bool includeEmbeddedDoc,
114  const bool includeRoot);
115 
116  static std::list<XERCESC_NS::DOMNode*> filterTypeGeneric(const std::set<XERCESC_NS::DOMNode::NodeType>& types,
117  const XERCESC_NS::DOMNode* root,
118  const Order order,
119  const bool includeEmbeddedDoc,
120  const bool includeRoot);
121 
122 };
123 
124 // create a prefix from a given element - useful for copying namespace information
125 #define XML_PREFIX(element) X(element->getPrefix() ? X(element->getPrefix()).str() + ":" : "")
126 
127 #if 1
128 
133 class USCXML_API X {
134 public :
135 
136  X(X const &other) {
137 
138  _localForm = other._localForm;
139  _unicodeForm = XERCESC_NS::XMLString::replicate(other._unicodeForm);
140  _deallocOther = true;
141  }
142 
143  X(const XMLCh* const toTranscode) {
144 
145  if (toTranscode != NULL) {
146  // Call the private transcoding method
147  char* tmp = XERCESC_NS::XMLString::transcode(toTranscode);
148  _localForm = std::string(tmp);
149  XERCESC_NS::XMLString::release(&tmp);
150  }
151  _unicodeForm = NULL;
152  _deallocOther = false;
153  }
154 
155  X(const std::string& fromTranscode) {
156 
157  // Call the private transcoding method
158  _localForm = fromTranscode;
159  _unicodeForm = XERCESC_NS::XMLString::transcode(fromTranscode.c_str());
160  _deallocOther = true;
161  }
162 
163  X(const char* const fromTranscode) {
164  // this is most unfortunate but needed with static XMLChars :(
165  if (!_xercesIsInit) {
166  try {
167  ::xercesc_3_1::XMLPlatformUtils::Initialize();
168  _xercesIsInit = true;
169  } catch (const XERCESC_NS::XMLException& toCatch) {
170  throw ("Cannot initialize XercesC: " + X(toCatch.getMessage()).str());
171  }
172  }
173 
174  // Call the private transcoding method
175  _localForm = fromTranscode;
176  _unicodeForm = XERCESC_NS::XMLString::transcode(fromTranscode);
177  _deallocOther = true;
178  }
179 
180  X(char* fromTranscode) {
181 
182  // Call the private transcoding method
183  _localForm = fromTranscode;
184  _unicodeForm = XERCESC_NS::XMLString::transcode(fromTranscode);
185  _deallocOther = true;
186  }
187 
188  X() {
189 
190  _unicodeForm = NULL;
191  _deallocOther = false;
192  }
193 
194  ~X() {
195 
196  if (_deallocOther)
197  XERCESC_NS::XMLString::release(&_unicodeForm);
198  }
199 
200  const std::string& str() const {
201  return _localForm;
202  }
203 
204  operator XMLCh* () const {
205  assert(_unicodeForm != NULL); // constructor with XMLCh
206  return _unicodeForm;
207  }
208 
209  void operator=(X const &other) {
210  _localForm = other._localForm;
211  _unicodeForm = XERCESC_NS::XMLString::replicate(other._unicodeForm);
212  _deallocOther = true;
213  }
214 
215  bool operator==(const X& other) const {
216  return (_unicodeForm == other._unicodeForm) != 0;
217  }
218 
219  bool operator!=(const X& other) const {
220  return !(_unicodeForm == other._unicodeForm);
221  }
222 
223  bool operator<(const X& other) const {
224  return XERCESC_NS::XMLString::compareString(_unicodeForm, other._unicodeForm) < 0;
225  }
226 
227  operator bool () {
228  return _localForm.size() > 0;
229  }
230 
231  operator std::string () {
232  return _localForm;
233  }
234 
235 protected:
236  friend USCXML_API std::ostream& operator<< (std::ostream& os, const X& data);
237 
238 private:
239  bool _deallocOther;
240  std::string _localForm;
241  XMLCh* _unicodeForm;
242  bool _xercesIsInit = false;
243 };
244 
245 #else
246 
247 class USCXML_API X {
248 public :
249  X() {
250  }
251 
252  void operator=(X const &other) {
253  localForm = other.localForm;
254  if (unicodeForm != NULL) {
255  XERCESC_NS::XMLString::release(&unicodeForm);
256  }
257  unicodeForm = XERCESC_NS::XMLString::replicate(other.unicodeForm);
258  }
259 
260  X(X const &other) {
261  localForm = other.localForm;
262  unicodeForm = XERCESC_NS::XMLString::replicate(other.unicodeForm);
263  }
264 
265  X(const char* const toTranscode) {
266  if (toTranscode != NULL) {
267  localForm = toTranscode;
268  unicodeForm = XERCESC_NS::XMLString::transcode(toTranscode);
269  }
270  }
271 
272  X(const XMLCh* toTranscode) {
273  if (toTranscode != NULL) {
274  unicodeForm = XERCESC_NS::XMLString::replicate(toTranscode);
275  localForm = XERCESC_NS::XMLString::transcode(toTranscode);
276  }
277  }
278 
279  X(const std::string& toTranscode) {
280  localForm = toTranscode;
281  unicodeForm = XERCESC_NS::XMLString::transcode(toTranscode.c_str());
282  }
283 
284  ~X() {
285  if (unicodeForm != NULL) {
286  XERCESC_NS::XMLString::release(&unicodeForm);
287  }
288  }
289 
290  operator XMLCh* () const {
291  return unicodeForm;
292  }
293 
294  operator const std::string& () {
295  return localForm;
296  }
297 
298  const std::string& str() const {
299  return localForm;
300  }
301 
302  const XMLCh* unicode() const {
303  return unicodeForm;
304  }
305 
306 
307 protected:
308  friend USCXML_API std::ostream& operator<< (std::ostream& os, const X& data);
309 
310 private:
311  XMLCh* unicodeForm = NULL;
312  std::string localForm;
313 
314 };
315 
316 #endif
317 
318 static const X kXMLCharScxml = X("scxml");
319 static const X kXMLCharState = X("state");
320 static const X kXMLCharParallel = X("parallel");
321 static const X kXMLCharTransition = X("transition");
322 static const X kXMLCharInitial = X("initial");
323 static const X kXMLCharFinal = X("final");
324 static const X kXMLCharOnEntry = X("onentry");
325 static const X kXMLCharOnExit = X("onexit");
326 static const X kXMLCharHistory = X("history");
327 
328 static const X kXMLCharRaise = X("raise");
329 static const X kXMLCharIf = X("if");
330 static const X kXMLCharElseIf = X("elseif");
331 static const X kXMLCharElse = X("else");
332 static const X kXMLCharForEach = X("foreach");
333 static const X kXMLCharLog = X("log");
334 
335 static const X kXMLCharDataModel = X("datamodel");
336 static const X kXMLCharData = X("data");
337 static const X kXMLCharAssign = X("assign");
338 static const X kXMLCharContent = X("content");
339 static const X kXMLCharParam = X("param");
340 static const X kXMLCharScript = X("script");
341 
342 static const X kXMLCharInvokeId = X("invokeid");
343 static const X kXMLCharId = X("id");
344 static const X kXMLCharIdLocation = X("idlocation");
345 static const X kXMLCharEvent = X("event");
346 static const X kXMLCharEventExpr = X("eventexpr");
347 static const X kXMLCharTarget = X("target");
348 static const X kXMLCharTargetExpr = X("targetexpr");
349 static const X kXMLCharSource = X("src");
350 static const X kXMLCharSourceExpr = X("srcexpr");
351 static const X kXMLCharType = X("type");
352 static const X kXMLCharTypeExpr = X("typeexpr");
353 static const X kXMLCharDelay = X("delay");
354 static const X kXMLCharDelayExpr = X("delayexpr");
355 static const X kXMLCharSendId = X("sendid");
356 static const X kXMLCharSendIdExpr = X("sendidexpr");
357 static const X kXMLCharCond = X("cond");
358 static const X kXMLCharLocation = X("location");
359 static const X kXMLCharArray = X("array");
360 static const X kXMLCharItem = X("item");
361 static const X kXMLCharIndex = X("index");
362 static const X kXMLCharLabel = X("label");
363 static const X kXMLCharExpr = X("expr");
364 static const X kXMLCharNameList = X("namelist");
365 static const X kXMLCharAutoForward = X("autoforward");
366 static const X kXMLCharName = X("name");
367 static const X kXMLCharBinding = X("binding");
368 
369 USCXML_API std::ostream& operator<< (std::ostream& os, const X& xmlString);
370 USCXML_API std::ostream& operator<< (std::ostream& os, const XERCESC_NS::DOMNode& node);
371 
372 }
373 
374 
375 #endif /* end of include guard: DOMUTILS_H_WK0WAEA7 */
Definition: Breakpoint.cpp:26
Definition: DOM.h:42
Definition: DOM.h:133