MIRA
XMLDom.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_XMLDOM_H_
48 #define _MIRA_XMLDOM_H_
49 
50 #include <iostream>
51 #include <string>
52 #ifndef Q_MOC_RUN
53 #include <boost/noncopyable.hpp>
54 #endif
55 
56 #include <error/Exceptions.h>
57 #include <platform/Types.h>
58 #include <utils/Path.h>
59 #include <utils/ToString.h>
60 
61 struct _xmlNode;
62 struct _xmlAttr;
63 struct _xmlDoc;
64 
65 namespace mira {
66 
68 
73 class MIRA_BASE_EXPORT XMLDom : boost::noncopyable
74 {
75 private:
76  enum
77  {
78  TEXT_NODE,
79  COMMENT_NODE
80  };
81 public:
84 
86  typedef std::pair<std::string, std::string> Attribute;
87 
89  struct NameSpace
90  {
91  NameSpace() {}
92  NameSpace(const std::string& p, const std::string& h="") :
93  prefix(p),
94  href(h) {}
95 
96  std::string prefix;
97  std::string href;
98  };
99 
101 
102 public:
105 
106  // forward decl.
109 
115  {
116  public:
120  typedef std::string value_type;
123  typedef const std::string* pointer;
125  typedef const std::string& reference;
127  typedef ptrdiff_t difference_type;
129  typedef std::bidirectional_iterator_tag iterator_category;
130 
135 
141  const_attribute_iterator(_xmlNode* iNode, _xmlAttr* iAttribute);
142 
148  bool operator==(const const_attribute_iterator& other) const;
154  bool operator!=(const const_attribute_iterator& other) const;
158  const_attribute_iterator& operator++();
162  const_attribute_iterator& operator--();
163 
168  Attribute operator*() const;
173  std::string name() const;
178  std::string value() const;
183  NameSpace nameSpace() const;
184 
185  private:
187  _xmlNode* mNode;
189  _xmlAttr* mAttribute;
190  };
191 
197  {
198  public:
202  typedef std::string value_type;
205  typedef std::string* pointer;
207  typedef std::string& reference;
209  typedef ptrdiff_t difference_type;
211  typedef std::bidirectional_iterator_tag iterator_category;
212 
217 
223  attribute_iterator(_xmlNode* iNode, _xmlAttr* iAttribute);
224 
229  const attribute_iterator& operator=(const std::string& value);
235  bool operator==(const attribute_iterator& other) const;
241  bool operator!=(const attribute_iterator& other) const;
245  attribute_iterator& operator++();
249  attribute_iterator& operator--();
250 
255  Attribute operator*() const;
260  std::string name() const;
265  std::string value() const;
270  NameSpace nameSpace() const;
271 
272  private:
273  friend class sibling_iterator; // sibling_iterator needs direct access on in remove
274 
276  _xmlNode* mNode;
278  _xmlAttr* mAttribute;
279  };
280 
285  template <int Type>
287  {
288  public:
292  typedef std::string value_type;
295  typedef const std::string* pointer;
297  typedef const std::string& reference;
299  typedef ptrdiff_t difference_type;
301  typedef std::bidirectional_iterator_tag iterator_category;
302 
307 
312  const_data_iterator(_xmlNode* iNode);
313 
319  bool operator==(const const_data_iterator& other) const;
325  bool operator!=(const const_data_iterator& other) const;
329  const_data_iterator& operator++();
333  const_data_iterator& operator--();
334 
339  std::string operator*() const;
340 
341  private:
342 
343  friend class sibling_iterator;
345  _xmlNode* mNode;
346  };
347 
351  template <int Type>
353  {
354  public:
358  typedef std::string value_type;
361  typedef std::string* pointer;
363  typedef std::string& reference;
365  typedef ptrdiff_t difference_type;
367  typedef std::bidirectional_iterator_tag iterator_category;
368 
369 
373  data_iterator();
374 
379  data_iterator(_xmlNode* iNode);
380 
385  const data_iterator& operator=(const std::string& value);
391  bool operator==(const data_iterator& other) const;
397  bool operator!=(const data_iterator& other) const;
401  data_iterator& operator++();
405  data_iterator& operator--();
406 
411  std::string operator*() const;
412 
417  data_iterator remove();
418 
419  private:
421  _xmlNode* mNode;
422  };
423 
447 
453  {
454  public:
460  iterator_base(_xmlNode* node, const std::string& name);
465  std::string operator*() const;
466 
470  void setName(const std::string& name);
471 
477  std::string uri() const;
478 
483  void setUri(const std::string& uri);
484 
489  NameSpace nameSpace() const;
490 
495  uint32 line() const;
496 
501  const_sibling_iterator cparent() const;
506  const_sibling_iterator parent() const { return cparent(); }
511  const_sibling_iterator cbegin() const;
516  const_sibling_iterator begin() const { return cbegin(); }
523  const_sibling_iterator cbegin(const std::string& name) const;
530  const_sibling_iterator begin(const std::string& name) const {
531  return cbegin(name);
532  }
537  const_sibling_iterator cend() const;
542  const_sibling_iterator end() const { return cend(); }
547  const_content_iterator content_cbegin() const;
552  const_content_iterator content_cend() const;
557  const_content_iterator content_begin() const { return content_cbegin(); }
562  const_content_iterator content_end() const { return content_cend(); }
563 
568  const_comment_iterator comment_cbegin() const;
573  const_comment_iterator comment_cend() const;
578  const_comment_iterator comment_begin() const { return comment_cbegin(); }
583  const_comment_iterator comment_end() const { return comment_cend(); }
584 
589  const_attribute_iterator attribute_cbegin() const;
594  const_attribute_iterator attribute_cend() const;
600  {
601  return attribute_cbegin();
602  }
608  {
609  return attribute_cend();
610  }
616  const_attribute_iterator find_attribute(const std::string& name) const;
622  bool has_attribute(const std::string& name) const
623  {
624  return find_attribute(name) != attribute_end();
625  }
632  template <typename T>
633  T get_attribute(const std::string& name) const
634  {
635  const_attribute_iterator a = find_attribute(name);
636  if ( a == attribute_end() )
637  MIRA_THROW(XInvalidConfig, "The attribute '" << name
638  << "' could not be found in node '" << *(*this) << "'");
639  return fromString<T>((*a).second);
640  }
641 
650  template <typename T>
651  T get_attribute(const std::string& name, const T& defaultValue) const
652  {
653  const_attribute_iterator a = find_attribute(name);
654  if ( a == attribute_end() )
655  return defaultValue;
656  return fromString<T>((*a).second);
657  }
658 
659  protected:
661  _xmlNode* mNode;
663  std::string mName;
664  };
665 
672  {
673  public:
677  friend class sibling_iterator;
679  typedef std::string value_type;
681  typedef const std::string* pointer;
683  typedef const std::string& reference;
685  typedef ptrdiff_t difference_type;
687  typedef std::bidirectional_iterator_tag iterator_category;
688 
697  const_sibling_iterator(_xmlNode* iNode, const std::string& name = "");
698 
704  iterator_base(other.mNode, other.mName)
705  {
706  }
707 
713  bool operator==(const const_sibling_iterator& other) const {
714  return mNode == other.mNode;
715  }
721  bool operator!=(const const_sibling_iterator& other) const {
722  return mNode != other.mNode;
723  }
727  const_sibling_iterator& operator++();
731  const_sibling_iterator& operator+=(std::size_t increment);
735  const_sibling_iterator operator+(std::size_t increment) const;
739  const_sibling_iterator& operator--();
740 
751  const_sibling_iterator find(const std::string& name, std::size_t nth = 0) const;
752  };
753 
759  {
760  public:
764  typedef std::string value_type;
767  typedef std::string* pointer;
769  typedef std::string& reference;
771  typedef ptrdiff_t difference_type;
773  typedef std::bidirectional_iterator_tag iterator_category;
774 
783  sibling_iterator(_xmlNode* iNode, const std::string& name = "");
784 
790  iterator_base(other.mNode, other.mName)
791  {
792  }
793 
799  bool operator==(const sibling_iterator& other) const {
800  return mNode == other.mNode;
801  }
807  bool operator!=(const sibling_iterator& other) const {
808  return mNode != other.mNode;
809  }
810 
814  operator const_sibling_iterator() const
815  {
816  return const_sibling_iterator(mNode, mName);
817  }
818 
824  const sibling_iterator& operator=(const std::string& name);
828  sibling_iterator& operator++();
832  sibling_iterator& operator+=(std::size_t increment);
836  sibling_iterator operator+(std::size_t increment) const;
840  sibling_iterator& operator--();
841 
846  sibling_iterator parent();
851  sibling_iterator begin();
859  sibling_iterator begin(const std::string& name);
864  sibling_iterator end();
865 
876  sibling_iterator find(const std::string& name, std::size_t nth = 0);
877 
882  content_iterator content_begin();
887  content_iterator content_end();
892  comment_iterator comment_begin();
897  comment_iterator comment_end();
902  attribute_iterator attribute_begin();
907  attribute_iterator attribute_end();
913  attribute_iterator find_attribute(const std::string& name);
919  XMLDom::sibling_iterator& add_attribute(const Attribute& attribute);
926  XMLDom::sibling_iterator& add_attribute(const std::string& name,
927  const std::string& value);
934  template <typename T>
935  XMLDom::sibling_iterator& add_attribute(const std::string& name, const T& value)
936  {
937  return add_attribute(name, toString(value));
938  }
939 
948  template <typename T>
949  XMLDom::sibling_iterator& add_attribute(const std::string& name, const T& value,
950  int precision)
951  {
952  return add_attribute(name, toString(value,precision));
953  }
954 
960  void remove_attribute(attribute_iterator it);
961 
967  XMLDom::sibling_iterator& add_comment(const std::string& comment);
973  XMLDom::sibling_iterator& add_content(const std::string& content);
974 
980  XMLDom::sibling_iterator& add_cdata_content(const std::string& cdata_content);
981 
987  XMLDom::sibling_iterator add_child(const std::string& name, const NameSpace& ns = NameSpace());
999  XMLDom::sibling_iterator insert_before(const XMLDom::const_sibling_iterator& node);
1005  XMLDom::sibling_iterator insert_before(const std::string& name, const NameSpace& ns = NameSpace());
1011  XMLDom::sibling_iterator insert_after(const XMLDom::const_sibling_iterator& node);
1017  XMLDom::sibling_iterator insert_after(const std::string& name, const NameSpace& ns = NameSpace());
1029  XMLDom::sibling_iterator replace(const std::string& name, const NameSpace& ns = NameSpace());
1035  XMLDom::sibling_iterator& insert_comment_before(const XMLDom::const_comment_iterator& comment);
1041  XMLDom::sibling_iterator& insert_comment_before(const std::string& comment);
1047  XMLDom::sibling_iterator& insert_comment_after(const XMLDom::const_comment_iterator& comment);
1053  XMLDom::sibling_iterator& insert_comment_after(const std::string& comment);
1059  XMLDom::sibling_iterator& insert_content_before(const XMLDom::const_content_iterator& content);
1065  XMLDom::sibling_iterator& insert_content_before(const std::string& content);
1071  XMLDom::sibling_iterator& insert_content_after(const XMLDom::const_content_iterator& content);
1077  XMLDom::sibling_iterator& insert_content_after(const std::string& content);
1083  XMLDom::sibling_iterator replace_by_comment(const XMLDom::const_comment_iterator& node);
1089  XMLDom::sibling_iterator replace_by_comment(const std::string& comment);
1095  XMLDom::sibling_iterator replace_by_content(const XMLDom::const_content_iterator& node);
1101  XMLDom::sibling_iterator replace_by_content(const std::string& content);
1106  XMLDom::sibling_iterator remove();
1107  };
1108 
1110 
1114 
1115 public:
1118 
1123  XMLDom(const std::string& rootNodeName = "root");
1124 
1126  ~XMLDom();
1127 
1129  XMLDom(XMLDom&& other) noexcept;
1130 
1132  XMLDom& operator=(XMLDom&& other) noexcept;
1133 
1135 
1136 public:
1137 
1146  void clear();
1147 
1148 public:
1151 
1157  void loadFromString(const std::string& buffer);
1158 
1167  void loadFromFile(const Path& filename,
1168  bool resolve = true);
1169 
1179  void saveToFile(const Path& filename,
1180  const std::string& encoding = "UTF-8",
1181  bool resolve = true) const;
1182 
1191  void saveToFile(const Path& filename,
1192  bool resolve) const;
1193 
1201  std::string saveToString(const std::string& encoding = "UTF-8") const;
1202 
1204 
1205 public:
1208 
1210  const_sibling_iterator croot() const;
1211 
1213  const_sibling_iterator root() const { return croot(); }
1214 
1216  sibling_iterator root();
1217 
1219 
1224  std::string uri() const;
1228  void setUri(const std::string& uri);
1229 
1234  std::string encoding() const;
1235 
1236 private:
1238  void loadFromPath(const Path& filename);
1239 
1241  void saveToPath(const Path& filename,
1242  const std::string& encoding) const;
1243 
1244  _xmlDoc* mXMLDocument;
1245 };
1246 
1248 
1249 }
1250 
1251 #endif
std::string value_type
STL-conform typedefs.
Definition: XMLDom.h:765
T get_attribute(const std::string &name) const
Return an attribute value cast to a given type.
Definition: XMLDom.h:633
std::string value_type
STL-conform typedefs.
Definition: XMLDom.h:121
_xmlNode * mNode
The node pointer.
Definition: XMLDom.h:661
const_attribute_iterator attribute_begin() const
Get the const iterator to the first attribute.
Definition: XMLDom.h:599
std::string value_type
STL-conform typedefs.
Definition: XMLDom.h:359
Iterator for iterating over data nodes.
Definition: XMLDom.h:352
const_sibling_iterator root() const
Return a const sibling_iterator to the root node of the XML document.
Definition: XMLDom.h:1213
A STL conform wrapper for libxml2 to read XML files as DOM.
Definition: XMLDom.h:73
bool has_attribute(const std::string &name) const
Returns true if node contains the attribute with name name.
Definition: XMLDom.h:622
Typedefs for OS independent basic data types.
const std::string & reference
The reference type.
Definition: XMLDom.h:297
ptrdiff_t difference_type
The difference type.
Definition: XMLDom.h:127
std::string * pointer
The pointer type.
Definition: XMLDom.h:361
const_sibling_iterator end() const
Get the const end node sibling_iterator.
Definition: XMLDom.h:542
std::string & reference
The reference type.
Definition: XMLDom.h:769
data_iterator< COMMENT_NODE > comment_iterator
The iterator for comments.
Definition: XMLDom.h:441
Const iterator for iterating over data nodes.
Definition: XMLDom.h:286
std::string & reference
The reference type.
Definition: XMLDom.h:363
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
boost::filesystem::path Path
Typedef of a Path (shorter version for boost::filesystem::path)
Definition: Path.h:69
std::string value_type
The value type.
Definition: XMLDom.h:679
const std::string & reference
The reference type.
Definition: XMLDom.h:683
std::bidirectional_iterator_tag iterator_category
The iterator category.
Definition: XMLDom.h:687
const_content_iterator content_end() const
Get the const end content iterator.
Definition: XMLDom.h:562
std::string * pointer
The pointer type.
Definition: XMLDom.h:205
const std::string * pointer
The pointer type.
Definition: XMLDom.h:681
const_comment_iterator comment_end() const
Get the const end comment iterator.
Definition: XMLDom.h:583
Iterator for iterating over attributes.
Definition: XMLDom.h:196
Contains toString and fromString functions for converting data types to strings and the other way rou...
NameSpace(const std::string &p, const std::string &h="")
Definition: XMLDom.h:92
bool operator==(const const_sibling_iterator &other) const
Compare two iterators.
Definition: XMLDom.h:713
const_content_iterator content_begin() const
Get the const iterator to the first content.
Definition: XMLDom.h:557
const_data_iterator< TEXT_NODE > const_content_iterator
The const iterator for content.
Definition: XMLDom.h:436
ptrdiff_t difference_type
The difference type.
Definition: XMLDom.h:365
Base iterator for iterating over xml nodes with the same parent (siblings)
Definition: XMLDom.h:452
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:81
const_sibling_iterator begin(const std::string &name) const
Get the const sibling_same_name_iterator to the first sub node with the given name.
Definition: XMLDom.h:530
std::string toString(const T &value, int precision=-1)
Converts any data type to string (the data type must support the stream << operator).
Definition: ToString.h:256
Const sibling_iterator for iterating over xml nodes that have the same parent (siblings) ...
Definition: XMLDom.h:671
sibling_iterator iterator
typedefs for backward compatibility
Definition: XMLDom.h:1112
Const iterator for iterating over attributes.
Definition: XMLDom.h:114
Namespace information of a node.
Definition: XMLDom.h:89
const std::string * pointer
The pointer type.
Definition: XMLDom.h:295
const std::string & reference
The reference type.
Definition: XMLDom.h:125
Commonly used exception classes.
std::pair< std::string, std::string > Attribute
An XML attribute.
Definition: XMLDom.h:86
bool operator!=(const sibling_iterator &other) const
Compare two iterators.
Definition: XMLDom.h:807
const_sibling_iterator(const const_sibling_iterator &other)
Copy-constructor.
Definition: XMLDom.h:703
std::string value_type
STL-conform typedefs.
Definition: XMLDom.h:203
std::bidirectional_iterator_tag iterator_category
The iterator category.
Definition: XMLDom.h:301
std::bidirectional_iterator_tag iterator_category
The iterator category.
Definition: XMLDom.h:367
const_sibling_iterator begin() const
Get the const sibling_iterator to the first sub node.
Definition: XMLDom.h:516
const std::string * pointer
The pointer type.
Definition: XMLDom.h:123
bool operator==(const ImgIteratorBase &a, const ImgIteratorBase &b)
Definition: ImgIterator.h:225
Iterator for iterating over xml nodes that have the same parent (sibligs)
Definition: XMLDom.h:758
std::string mName
the node name (optional)
Definition: XMLDom.h:663
NameSpace()
Definition: XMLDom.h:91
XMLDom::sibling_iterator & add_attribute(const std::string &name, const T &value)
Add an attribute to this node.
Definition: XMLDom.h:935
ptrdiff_t difference_type
The difference type.
Definition: XMLDom.h:771
data_iterator< TEXT_NODE > content_iterator
The iterator for content.
Definition: XMLDom.h:446
const_sibling_iterator const_iterator
Definition: XMLDom.h:1113
const_sibling_iterator parent() const
Get the const parent node sibling_iterator for this node.
Definition: XMLDom.h:506
std::string & reference
The reference type.
Definition: XMLDom.h:207
std::bidirectional_iterator_tag iterator_category
The iterator category.
Definition: XMLDom.h:211
T get_attribute(const std::string &name, const T &defaultValue) const
Return an attribute value casted to a given type If the attribute could not be found the given defaul...
Definition: XMLDom.h:651
ptrdiff_t difference_type
The difference type.
Definition: XMLDom.h:685
std::string prefix
Definition: XMLDom.h:96
sibling_iterator(const sibling_iterator &other)
Copy-constructor.
Definition: XMLDom.h:789
Functions for modifying file system paths.
PropertyHint precision(int p)
Sets the attribute "precision".
Definition: PropertyHint.h:285
bool operator!=(const ImgIteratorBase &a, const ImgIteratorBase &b)
Definition: ImgIterator.h:228
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
std::string * pointer
The pointer type.
Definition: XMLDom.h:767
const_data_iterator< COMMENT_NODE > const_comment_iterator
STL-conform typedefs.
Definition: XMLDom.h:431
bool operator!=(const const_sibling_iterator &other) const
Compare two iterators.
Definition: XMLDom.h:721
std::string value_type
STL-conform typedefs.
Definition: XMLDom.h:293
std::string href
Definition: XMLDom.h:97
const_attribute_iterator attribute_end() const
Get the const end attribute iterator.
Definition: XMLDom.h:607
ptrdiff_t difference_type
The difference type.
Definition: XMLDom.h:299
XMLDom::sibling_iterator & add_attribute(const std::string &name, const T &value, int precision)
Add an attribute to this node.
Definition: XMLDom.h:949
ptrdiff_t difference_type
The difference type.
Definition: XMLDom.h:209
bool operator==(const sibling_iterator &other) const
Compare two iterators.
Definition: XMLDom.h:799
const_comment_iterator comment_begin() const
Get the const iterator to the first comment.
Definition: XMLDom.h:578
std::bidirectional_iterator_tag iterator_category
The iterator category.
Definition: XMLDom.h:129
std::bidirectional_iterator_tag iterator_category
The iterator category.
Definition: XMLDom.h:773