DDS  ver. 3.4
XMLHelper.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 // XML Helper header
4 //
5 #ifndef XMLHELPER_H
6 #define XMLHELPER_H
7 
8 // Xerces-C++ headers
9 #include <xercesc/dom/DOM.hpp>
10 #include <xercesc/dom/DOMElement.hpp>
11 #include <xercesc/util/XMLString.hpp>
12 
13 // STD headers
14 #include <sstream>
15 #include <stdexcept>
16 #include <string>
17 
18 // MiscCommon
19 #include "MiscUtils.h"
20 
21 namespace MiscCommon
22 {
28  namespace XMLHelper
29  {
36  {
37  public:
38  XMLCh* m_xmlString;
39 
41  : m_xmlString(NULL)
42  {
43  }
44 
45  smart_XMLCh(const char* _Str)
46  {
47  m_xmlString = xercesc::XMLString::transcode(_Str);
48  }
49 
50  smart_XMLCh(const XMLCh* const _XMLCh)
51  {
52  if (_XMLCh)
53  m_xmlString = xercesc::XMLString::replicate(_XMLCh);
54  }
55 
57  {
58  Release();
59  }
60 
61  operator XMLCh*() const
62  {
63  return m_xmlString;
64  }
65 
66  std::string ToString() const
67  {
68  if (!m_xmlString)
69  return std::string();
70  char* szTmp = xercesc::XMLString::transcode(m_xmlString);
71  if (!szTmp)
72  return std::string();
73  std::string strRetVal(szTmp);
74  xercesc::XMLString::release(&szTmp);
75  return strRetVal;
76  }
77 
78  bool operator==(const std::string& _Val)
79  {
80  return (_Val == this->ToString());
81  }
82 
83  XMLCh* operator&()
84  {
85  return m_xmlString;
86  }
87 
88  void Release()
89  {
90  if (NULL != m_xmlString)
91  xercesc::XMLString::release(&m_xmlString);
92  }
93  };
94  inline bool operator==(const std::string& _Val1, const smart_XMLCh& _Val2)
95  {
96  return (_Val1 == _Val2.ToString());
97  }
106  template <class _T>
107  void get_attr_value(const xercesc::DOMElement* _element, const char* _attr, _T* _data)
108  {
109  smart_XMLCh attr_name(_attr);
110  smart_XMLCh xmlTmpStr(_element->getAttribute(attr_name));
111  std::istringstream str(xmlTmpStr.ToString());
112  str >> *_data;
113  }
123  template <>
124  inline void get_attr_value<bool>(const xercesc::DOMElement* _element, const char* _attr, bool* _data)
125  {
126  smart_XMLCh attr_name(_attr);
127  smart_XMLCh xmlTmpStr(_element->getAttribute(attr_name));
128  std::string str(xmlTmpStr.ToString());
130  *_data = !(str.empty() || ("false" == str));
131  }
132  // TODO: Simplify this template by implementing traits
138  template <class _T>
139  inline xercesc::DOMNode* GetSingleNodeByName(const _T* _Val, const std::string& _NodeName);
145  template <class _T>
146  inline xercesc::DOMNodeList* GetNodesByName(const _T* _Val, const std::string& _NodeName);
152  template <>
153  inline xercesc::DOMNodeList* GetNodesByName(const xercesc::DOMNode* _ParentNode, const std::string& _NodeName)
154  {
155  if (!_ParentNode)
156  return NULL;
157  const smart_XMLCh ElementName(_NodeName.c_str());
158 
159  const xercesc::DOMElement* element(NULL);
160  if (xercesc::DOMNode::ELEMENT_NODE == _ParentNode->getNodeType())
161  {
162  if (xercesc::DOMNode::ELEMENT_NODE == _ParentNode->getNodeType())
163  element = dynamic_cast<const xercesc::DOMElement*>(_ParentNode);
164  else
165  return NULL;
166  }
167 
168  return (element->getElementsByTagName(ElementName));
169  }
178  template <>
179  inline xercesc::DOMNode* GetSingleNodeByName(const xercesc::DOMDocument* _Doc, const std::string& _NodeName)
180  {
181  if (!_Doc)
182  return NULL;
183  const smart_XMLCh ElementName(_NodeName.c_str());
184 
185  const xercesc::DOMNodeList* list = _Doc->getElementsByTagName(ElementName);
186  if (!list)
187  return NULL;
188  return list->item(0);
189  }
198  template <>
199  inline xercesc::DOMNode* GetSingleNodeByName(const xercesc::DOMNode* _node, const std::string& _NodeName)
200  {
201  const xercesc::DOMNodeList* list = GetNodesByName(_node, _NodeName);
202  if (!list)
203  return NULL;
204  return list->item(0);
205  }
215  template <class _T>
216  inline xercesc::DOMNode* GetSingleNodeByName_Ex(const _T* _Node,
217  const std::string& _NodeName) throw(std::exception)
218  {
219  xercesc::DOMNode* node = GetSingleNodeByName(_Node, _NodeName.c_str());
220  if (!node)
221  throw(std::runtime_error("can't find XML element \"" + _NodeName + "\""));
222  return node;
223  }
232  template <class _T>
233  void get_node_value(const xercesc::DOMNode* _parent_node, const char* _node_name, _T* _data)
234  {
235  xercesc::DOMNode* node(GetSingleNodeByName(_parent_node, _node_name));
236  if (!node)
237  return;
238  xercesc::DOMNode* child(node->getFirstChild());
239  if (!child)
240  return;
241  smart_XMLCh xmlTmpStr(child->getNodeValue());
242  std::istringstream ss(xmlTmpStr.ToString());
243  ss >> *_data;
244  }
254  template <>
255  inline void get_node_value<bool>(const xercesc::DOMNode* _parent_node, const char* _node_name, bool* _data)
256  {
257  xercesc::DOMNode* node(GetSingleNodeByName(_parent_node, _node_name));
258  if (!node)
259  return;
260  xercesc::DOMNode* child(node->getFirstChild());
261  if (!child)
262  return;
263  smart_XMLCh xmlTmpStr(child->getNodeValue());
264  std::string str(xmlTmpStr.ToString());
266  *_data = !(str.empty() || ("false" == str));
267  }
268  }; // namespace XMLHelper
269 }; // namespace MiscCommon
270 
271 #endif
void get_node_value< bool >(const xercesc::DOMNode *_parent_node, const char *_node_name, bool *_data)
A specialization of the get_node_value template function with the bool type – xml value: true or fals...
Definition: XMLHelper.h:255
xercesc::DOMNode * GetSingleNodeByName_Ex(const _T *_Node, const std::string &_NodeName)
A helper template function, which wraps GetSingleNodeByName template-functions.
Definition: XMLHelper.h:216
smart-wrapper around XMLCh class
Definition: XMLHelper.h:35
smart_XMLCh(const XMLCh *const _XMLCh)
Definition: XMLHelper.h:50
_T & to_lower(_T &_str)
convert string to lower case.
Definition: MiscUtils.h:249
void get_node_value(const xercesc::DOMNode *_parent_node, const char *_node_name, _T *_data)
A template function, which helps to retrieve a value of xml node.
Definition: XMLHelper.h:233
bool operator==(const std::string &_Val1, const smart_XMLCh &_Val2)
Definition: XMLHelper.h:94
XMLCh * operator &()
Definition: XMLHelper.h:83
bool operator==(const std::string &_Val)
Definition: XMLHelper.h:78
xercesc::DOMNode * GetSingleNodeByName(const _T *_Val, const std::string &_NodeName)
Returns a Node by the given name. (basic template without implementation)
void get_attr_value< bool >(const xercesc::DOMElement *_element, const char *_attr, bool *_data)
A specialization of the get_attr_value template function with the bool type – xml value: true or fals...
Definition: XMLHelper.h:124
#define _T(s)
Use TCHAR instead of char or wchar_t. It will be appropriately translated.
Definition: def.h:85
void Release()
Definition: XMLHelper.h:88
XMLCh * m_xmlString
Definition: XMLHelper.h:38
smart_XMLCh(const char *_Str)
Definition: XMLHelper.h:45
smart_XMLCh()
Definition: XMLHelper.h:40
void get_attr_value(const xercesc::DOMElement *_element, const char *_attr, _T *_data)
A template function, which helps to retrieve different types of attributes from an XML file.
Definition: XMLHelper.h:107
std::string ToString() const
Definition: XMLHelper.h:66
xercesc::DOMNodeList * GetNodesByName(const _T *_Val, const std::string &_NodeName)
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21
~smart_XMLCh()
Definition: XMLHelper.h:56