xml_parser.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <interop/external/rapidxml.hpp>
13 #include <interop/external/rapidxml_utils.hpp>
14 #include <interop/external/rapidxml_print.hpp>
15 #include <list>
16 #include "interop/util/assert.h"
18 #include "interop/util/exception.h"
19 
20 namespace illumina { namespace interop { namespace xml
21 {
22 
24  typedef rapidxml::xml_node<> *xml_node_ptr;
26  typedef rapidxml::xml_attribute<> *xml_attr_ptr;
27 
30  {
31  public:
34  {
35  rapidxml::xml_node<>* decl = m_doc.allocate_node(rapidxml::node_declaration);
36  decl->append_attribute(m_doc.allocate_attribute("version", "1.0"));
37  m_doc.append_node(decl);
38  }
45  xml_node_ptr add_node(xml_node_ptr parent, const char* name)
46  {
47  rapidxml::xml_node<>* child = m_doc.allocate_node(rapidxml::node_element, name);
48  parent->append_node(child);
49  return child;
50  }
56  xml_node_ptr add_node(const char* name)
57  {
58  return add_node(&m_doc, name);
59  }
67  template<typename T>
68  xml_node_ptr add_node(xml_node_ptr parent, const char* name, const T& val)
69  {
70  m_backing.push_back(util::lexical_cast<std::string>(val));
71  rapidxml::xml_node<>* child = m_doc.allocate_node(rapidxml::node_element, name, m_backing.back().c_str());
72  parent->append_node(child);
73  return child;
74  }
82  template<typename T>
83  void add_attribute(xml_node_ptr parent, const char* name, const T& val)
84  {
85  m_backing.push_back(util::lexical_cast<std::string>(val));
86  parent->append_attribute(m_doc.allocate_attribute(name, m_backing.back().c_str()));
87  }
94  friend std::ostream& operator<<(std::ostream& out, const xml_document& doc)
95  {
96  out << doc.m_doc;
97  return out;
98  }
99 
100  private:
101  rapidxml::xml_document<> m_doc;
102  std::list<std::string> m_backing;
103  };
112  template<class T>
113  bool set_data_with_default(xml_node_ptr p_node, const char* target, T &val, const T default_val)
114  {
115  INTEROP_ASSERT(p_node != 0);
116  p_node = p_node->first_node(target);
117  if (p_node == 0)
118  {
119  val = default_val;
120  return false;
121  }
122  val = util::lexical_cast<T>(p_node->value());
123  return true;
124  }
125 
133  template<class T>
134  void set_data_for_target(xml_node_ptr p_node, const std::string &target, T &val)
135  {
136  if (p_node == 0)
137  INTEROP_THROW(missing_xml_element_exception, "Cannot find parent");
138  p_node = p_node->first_node(target.c_str());
139  if (p_node == 0)
140  INTEROP_THROW(missing_xml_element_exception, "Cannot find node: " << target);
141  val = util::lexical_cast<T>(p_node->value());
142  }
143 
152  template<class T>
153  void set_data_for_target(xml_node_ptr p_node, const std::string &target, T &val, const T default_val)
154  {
155  if (p_node == 0)
156  INTEROP_THROW(missing_xml_element_exception, "Cannot find parent for target: " << target);
157  p_node = p_node->first_node(target.c_str());
158  if (p_node == 0) val = default_val;
159  else val = util::lexical_cast<T>(p_node->value());
160  }
161 
169  template<class T>
170  bool set_data(xml_node_ptr p_node, const std::string &target, T &val)
171  {
172  if (p_node == 0)
173  INTEROP_THROW(missing_xml_element_exception, "Cannot find node: " << target);
174  if (p_node->name() == target)
175  {
176  val = util::lexical_cast<T>(p_node->value());
177  return true;
178  }
179  return false;
180  }
181 
187  template<class T>
188  void set_data(xml_node_ptr p_attr, T &val)
189  {
190  if (p_attr == 0)
191  INTEROP_THROW(missing_xml_element_exception, "Cannot find node");
192  val = util::lexical_cast<T>(p_attr->value());
193  }
194 
202  template<class T>
203  bool set_data(xml_attr_ptr p_attr, const std::string &target, T &val)
204  {
205  if (p_attr == 0)
206  INTEROP_THROW(missing_xml_element_exception, "Cannot find attribute: " << target);
207  if (p_attr->name() == target)
208  {
209  std::string tmp = p_attr->value();
210  if(tmp[0] == '\"' && tmp[tmp.length()-1] == '\"')
211  {
212  tmp = tmp.substr(1, tmp.length()-1);
213  }
214  val = util::lexical_cast<T>(tmp);
215  return true;
216  }
217  return false;
218  }
219 
226  template<class T>
227  void set_data_from_attribute(xml_node_ptr p_node, const char* target, T &val)
228  {
229  INTEROP_ASSERT(p_node != 0);
230  xml_attr_ptr p_attr = p_node->first_attribute(target);
231  if (p_attr == 0)
232  INTEROP_THROW(missing_xml_element_exception, "Cannot find attribute: " << target);
233  std::string tmp = p_attr->value();
234  if(tmp[0] == '\"' && tmp[tmp.length()-1] == '\"')
235  {
236  tmp = tmp.substr(1, tmp.length()-1);
237  }
238  val = util::lexical_cast<T>(tmp);
239  }
240 
249  template<class T>
250  bool set_data(xml_node_ptr p_node, const std::string &target, const std::string &child, std::vector<T> &val)
251  {
252  if (p_node == 0)
253  INTEROP_THROW(missing_xml_element_exception, "Cannot find node: " << target);
254  if (p_node->name() == target)
255  {
256  val.clear();
257  for (rapidxml::xml_node<> *p_name = p_node->first_node(); p_name; p_name = p_name->next_sibling())
258  {
259  if (p_name->name() != child) continue;
260  val.push_back(util::lexical_cast<T>(p_name->value()));
261  }
262  return true;
263  }
264  return false;
265  }
266 
267 }}}
268 
269 
xml_document()
Definition: xml_parser.h:33
bool set_data_with_default(xml_node_ptr p_node, const char *target, T &val, const T default_val)
Definition: xml_parser.h:113
rapidxml::xml_attribute * xml_attr_ptr
Definition: xml_parser.h:26
Definition: enum_description.h:15
bool set_data(xml_node_ptr p_node, const std::string &target, T &val)
Definition: xml_parser.h:170
xml_node_ptr add_node(xml_node_ptr parent, const char *name)
Definition: xml_parser.h:45
void add_attribute(xml_node_ptr parent, const char *name, const T &val)
Definition: xml_parser.h:83
#define INTEROP_THROW(EXCEPTION, MESSAGE)
Definition: exception.h:18
void set_data_for_target(xml_node_ptr p_node, const std::string &target, T &val)
Definition: xml_parser.h:134
void set_data_from_attribute(xml_node_ptr p_node, const char *target, T &val)
Definition: xml_parser.h:227
#define INTEROP_ASSERT(TST)
Definition: assert.h:21
friend std::ostream & operator<<(std::ostream &out, const xml_document &doc)
Definition: xml_parser.h:94
rapidxml::xml_node * xml_node_ptr
Definition: xml_parser.h:24
Definition: enums.h:301
xml_node_ptr add_node(const char *name)
Definition: xml_parser.h:56
Definition: xml_parser.h:29
xml_node_ptr add_node(xml_node_ptr parent, const char *name, const T &val)
Definition: xml_parser.h:68
Destination lexical_cast(const Source &src)
Definition: lexical_cast.h:264