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 <iostream>
26 
27 #include "uscxml/config.h"
28 #include "uscxml/Common.h"
29 #include <xercesc/util/XMLString.hpp>
30 #include <xercesc/dom/DOM.hpp>
31 
32 
33 /*
34 #define TAGNAME_CAST(elem) ((Arabica::DOM::Element<std::string>)elem).getTagName()
35 #define LOCALNAME_CAST(elem) ((Arabica::DOM::Element<std::string>)elem).getLocalName()
36 #define ATTR_CAST(elem, attr) ((Arabica::DOM::Element<std::string>)elem).getAttribute(attr)
37 #define ATTR_NODE_CAST(elem, attr) ((Arabica::DOM::Element<std::string>)elem).getAttributeNode(attr)
38 #define HAS_ATTR_CAST(elem, attr) ((Arabica::DOM::Element<std::string>)elem).hasAttribute(attr)
39 
40 #define TAGNAME(elem) elem.getTagName()
41 #define LOCALNAME(elem) elem.getLocalName()
42 #define ATTR(elem, attr) elem.getAttribute(attr)
43 #define ATTR_NODE(elem, attr) elem.getAttributeNode(attr)
44 */
45 
46 #define HAS_ATTR(elem, attr) (elem)->hasAttribute(X(attr))
47 #define HAS_ATTR_CAST(elem, attr) HAS_ATTR(static_cast<const DOMElement*>(elem), attr)
48 #define ATTR(elem, attr) std::string(X((elem)->getAttribute(X(attr))))
49 #define ATTR_CAST(elem, attr) ATTR(static_cast<const DOMElement*>(elem), attr)
50 #define TAGNAME(elem) std::string(X((elem)->getTagName()))
51 #define TAGNAME_CAST(elem) TAGNAME(static_cast<const DOMElement*>(elem))
52 #define LOCALNAME(elem) std::string(X((elem)->getLocalName()))
53 #define LOCALNAME_CAST(elem) LOCALNAME(static_cast<const DOMElement*>(elem))
54 
55 
56 
57 namespace uscxml {
58 
59 class USCXML_API DOMUtils {
60 public:
61 
62  static const XERCESC_NS::DOMNode* getNearestAncestor(const XERCESC_NS::DOMNode* node, const std::string tagName);
63  static bool isDescendant(const XERCESC_NS::DOMNode* s1, const XERCESC_NS::DOMNode* s2);
64 
65 
66  static bool hasIntersection(const std::list<XERCESC_NS::DOMElement*>& l1,
67  const std::list<XERCESC_NS::DOMElement*>& l2);
68  static bool isMember(const XERCESC_NS::DOMElement* node, const std::list<XERCESC_NS::DOMElement*>& list);
69  static bool isMember(const XERCESC_NS::DOMNode* node, const std::list<XERCESC_NS::DOMNode*>& list);
70  static bool isMember(const XERCESC_NS::DOMNode* node, const XERCESC_NS::DOMNodeList* list);
71 
72  static std::string xPathForNode(const XERCESC_NS::DOMNode* node,
73  const std::string& ns = "");
74  static std::string idForNode(const XERCESC_NS::DOMNode* node);
75 
76  static std::list<XERCESC_NS::DOMNode*> getElementsByType(const XERCESC_NS::DOMNode* root,
77  XERCESC_NS::DOMNode::NodeType type);
78 
79  static std::list<XERCESC_NS::DOMElement*> inPostFixOrder(const std::set<std::string>& elements,
80  const XERCESC_NS::DOMElement* root,
81  const bool includeEmbeddedDoc = false);
82 
83  static std::list<XERCESC_NS::DOMElement*> inDocumentOrder(const std::set<std::string>& elements,
84  const XERCESC_NS::DOMElement* root,
85  const bool includeEmbeddedDoc = false);
86 
87  static std::list<XERCESC_NS::DOMElement*> filterChildElements(const std::string& tagName,
88  const XERCESC_NS::DOMElement* node,
89  bool recurse = false);
90 
91  static std::list<XERCESC_NS::DOMElement*> filterChildElements(const std::string& tagName,
92  const std::list<XERCESC_NS::DOMElement*>& nodeSet,
93  bool recurse = false);
94 
95  static std::list<XERCESC_NS::DOMNode*> filterChildType(const XERCESC_NS::DOMNode::NodeType type,
96  const XERCESC_NS::DOMNode* node,
97  bool recurse = false);
98 
99  static std::list<XERCESC_NS::DOMNode*> filterChildType(const XERCESC_NS::DOMNode::NodeType type,
100  const std::list<XERCESC_NS::DOMNode*>& nodeSet,
101  bool recurse = false);
102 
103 protected:
104  static void inPostFixOrder(const std::set<std::string>& elements,
105  const XERCESC_NS::DOMElement* root,
106  const bool includeEmbeddedDoc,
107  std::list<XERCESC_NS::DOMElement*>& nodes);
108 
109  static void inDocumentOrder(const std::set<std::string>& elements,
110  const XERCESC_NS::DOMElement* root,
111  const bool includeEmbeddedDoc,
112  std::list<XERCESC_NS::DOMElement*>& nodes);
113 
114 
115 };
116 
117 // create a prefix from a given element - useful for copying namespace information
118 #define XML_PREFIX(element) X(element->getPrefix() ? X(element->getPrefix()).str() + ":" : "")
119 
120 class USCXML_API X {
121 public :
122 
123  X(X const &other) {
124  _localForm = other._localForm;
125  _otherForm = XERCESC_NS::XMLString::replicate(other._otherForm);
126  _deallocOther = true;
127  }
128  void operator=(X const &other) { // did we maybe leak before?
129  _localForm = other._localForm;
130  _otherForm = XERCESC_NS::XMLString::replicate(other._otherForm);
131  _deallocOther = true;
132  }
133 
134  X(const XMLCh* const toTranscode) {
135  if (toTranscode != NULL) {
136  // Call the private transcoding method
137  char* tmp = XERCESC_NS::XMLString::transcode(toTranscode);
138  _localForm = std::string(tmp);
139  XERCESC_NS::XMLString::release(&tmp);
140  }
141  _otherForm = NULL;
142  _deallocOther = false;
143  }
144 
145  X(const std::string& fromTranscode) {
146  // Call the private transcoding method
147  _localForm = fromTranscode;
148  _otherForm = XERCESC_NS::XMLString::transcode(fromTranscode.c_str());
149  _deallocOther = true;
150  }
151 
152  X(const char* const fromTranscode) {
153  // Call the private transcoding method
154  _localForm = fromTranscode;
155  _otherForm = XERCESC_NS::XMLString::transcode(fromTranscode);
156  _deallocOther = true;
157  }
158 
159  X(char* fromTranscode) {
160  // Call the private transcoding method
161  _localForm = fromTranscode;
162  _otherForm = XERCESC_NS::XMLString::transcode(fromTranscode);
163  _deallocOther = true;
164  }
165 
166  X() {
167  _otherForm = NULL;
168  _deallocOther = false;
169  }
170 
171  ~X() {
172  if (_deallocOther)
173  XERCESC_NS::XMLString::release(&_otherForm);
174  }
175 
176  const std::string& str() const {
177  return _localForm;
178  }
179 
180  operator const XMLCh* () {
181  assert(_otherForm != NULL); // constructor with XMLCh
182  return _otherForm;
183  }
184 
185  operator bool () {
186  return _localForm.size() > 0;
187  }
188 
189  operator std::string () {
190  return _localForm;
191  }
192 
193 protected:
194  friend USCXML_API std::ostream& operator<< (std::ostream& os, const X& data);
195 
196 private:
197  bool _deallocOther;
198  std::string _localForm;
199  XMLCh* _otherForm;
200 };
201 
202 USCXML_API std::ostream& operator<< (std::ostream& os, const X& xmlString);
203 USCXML_API std::ostream& operator<< (std::ostream& os, const XERCESC_NS::DOMNode& node);
204 
205 }
206 
207 
208 #endif /* end of include guard: DOMUTILS_H_WK0WAEA7 */
Definition: InterpreterIssue.cpp:33
Definition: DOM.h:59
Definition: DOM.h:120