zen::Xml
Simple C++ XML Processing
 All Classes Namespaces Functions Variables Pages
dom.h
1 // **************************************************************************
2 // * This file is part of the FreeFileSync project. It is distributed under *
3 // * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
4 // * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved *
5 // **************************************************************************
6 
7 #ifndef ZEN_XML_DOM_HEADER_82085720723894567204564256
8 #define ZEN_XML_DOM_HEADER_82085720723894567204564256
9 
10 #include <string>
11 #include <vector>
12 #include <memory>
13 #include <map>
14 #include "cvrt_text.h" //"readText/writeText"
15 
16 namespace zen
17 {
18 class XmlDoc;
19 
22 {
23  struct PrivateConstruction {};
24 public:
25  //Construct an empty XML element
26  //This constructor should be private, however std::make_shared() requires public access
27  //Therefore at least prevent users from calling it via private dummy type PrivateConstruction
28  template <class String>
29  XmlElement(const String& name, XmlElement* parentElement, PrivateConstruction) : name_(utfCvrtTo<std::string>(name)), parent_(parentElement) {}
30 
32 
36  template <class String>
37  String getNameAs() const { return utfCvrtTo<String>(name_); }
38 
40 
44  template <class T>
45  bool getValue(T& value) const { return readStruc(*this, value); }
46 
48 
51  template <class T>
52  void setValue(const T& value) { writeStruc(value, *this); }
53 
55 
62  template <class String, class T>
63  bool getAttribute(const String& name, T& value) const
64  {
65  auto it = attributes.find(utfCvrtTo<std::string>(name));
66  return it == attributes.end() ? false : readText(it->second, value);
67  }
68 
70 
76  template <class String, class T>
77  void setAttribute(const String& name, const T& value)
78  {
79  std::string attrValue;
80  writeText(value, attrValue);
81  attributes[utfCvrtTo<std::string>(name)] = attrValue;
82  }
83 
85 
88  template <class String>
89  void removeAttribute(const String& name) { attributes.erase(utfCvrtTo<std::string>(name)); }
90 
92 
96  template <class String>
97  XmlElement& addChild(const String& name)
98  {
99  std::string utf8Name = utfCvrtTo<std::string>(name);
100  auto newElement = std::make_shared<XmlElement>(utf8Name, this, PrivateConstruction());
101  childElements.push_back(newElement);
102  childElementsSorted.insert(std::make_pair(utf8Name, newElement));
103  return *newElement;
104  }
105 
107 
112  template <class String>
113  const XmlElement* getChild(const String& name) const
114  {
115  auto it = childElementsSorted.find(utfCvrtTo<std::string>(name));
116  return it == childElementsSorted.end() ? nullptr : &*(it->second);
117  }
118 
120  template <class String>
121  XmlElement* getChild(const String& name)
122  {
123  return const_cast<XmlElement*>(static_cast<const XmlElement*>(this)->getChild(name));
124  }
125 
126  template < class IterTy, //underlying iterator type
127  class T, //target object type
128  class AccessPolicy > //access policy: see AccessPtrMap
129  class PtrIter : public std::iterator<std::input_iterator_tag, T>, private AccessPolicy //get rid of shared_ptr indirection
130  {
131  public:
132  PtrIter(IterTy it) : it_(it) {}
133  PtrIter(const PtrIter& other) : it_(other.it_) {}
134  PtrIter& operator++() { ++it_; return *this; }
135  PtrIter operator++(int) { PtrIter tmp(*this); operator++(); return tmp; }
136  inline friend bool operator==(const PtrIter& lhs, const PtrIter& rhs) { return lhs.it_ == rhs.it_; }
137  inline friend bool operator!=(const PtrIter& lhs, const PtrIter& rhs) { return !(lhs == rhs); }
138  T& operator* () { return AccessPolicy::template objectRef<T>(it_); }
139  T* operator->() { return &AccessPolicy::template objectRef<T>(it_); }
140  private:
141  IterTy it_;
142  };
143 
144  struct AccessPtrMap
145  {
146  template <class T, class IterTy>
147  T& objectRef(const IterTy& it) { return *(it->second); }
148  };
149 
150  typedef PtrIter<std::multimap<std::string, std::shared_ptr<XmlElement>>::iterator, XmlElement, AccessPtrMap> ChildIter2;
151  typedef PtrIter<std::multimap<std::string, std::shared_ptr<XmlElement>>::const_iterator, const XmlElement, AccessPtrMap> ChildIterConst2;
152 
154 
163  template <class String>
164  std::pair<ChildIterConst2, ChildIterConst2> getChildren(const String& name) const { return childElementsSorted.equal_range(utfCvrtTo<std::string>(name)); }
165 
167  template <class String>
168  std::pair<ChildIter2, ChildIter2> getChildren(const String& name) { return childElementsSorted.equal_range(utfCvrtTo<std::string>(name)); }
169 
170  struct AccessPtrVec
171  {
172  template <class T, class IterTy>
173  T& objectRef(const IterTy& it) { return **it; }
174  };
175 
176  typedef PtrIter<std::vector<std::shared_ptr<XmlElement>>::iterator, XmlElement, AccessPtrVec> ChildIter;
177  typedef PtrIter<std::vector<std::shared_ptr<XmlElement>>::const_iterator, const XmlElement, AccessPtrVec> ChildIterConst;
178 
180 
188  std::pair<ChildIterConst, ChildIterConst> getChildren() const { return std::make_pair(childElements.begin(), childElements.end()); }
189 
191  std::pair<ChildIter, ChildIter> getChildren() { return std::make_pair(childElements.begin(), childElements.end()); }
192 
194  XmlElement* parent() { return parent_; };
196  const XmlElement* parent() const { return parent_; };
197 
198 
199  typedef std::map<std::string, std::string>::const_iterator AttrIter;
200 
201  /* -> disabled documentation extraction
202  \brief Get all attributes associated with the element.
203  \code
204  auto iterPair = elem.getAttributes();
205  for (auto it = iterPair.first; it != iterPair.second; ++it)
206  std::cout << "name: " << it->first << " value: " << it->second << "\n";
207  \endcode
208  \return A pair of STL begin/end iterators to access all attributes sequentially as a list of name/value pairs of std::string.
209  */
210  std::pair<AttrIter, AttrIter> getAttributes() const { return std::make_pair(attributes.begin(), attributes.end()); }
211 
212  //Transactionally swap two elements. -> disabled documentation extraction
213  void swap(XmlElement& other)
214  {
215  name_ .swap(other.name_);
216  value_ .swap(other.value_);
217  attributes.swap(other.attributes);
218  childElements.swap(other.childElements);
219  childElementsSorted.swap(other.childElementsSorted);
220  //std::swap(parent_, other.parent_); -> parent is physical location; update children's parent reference instead:
221  std::for_each( childElements.begin(), childElements.end(), [&](const std::shared_ptr<XmlElement>& child) { child->parent_ = this; });
222  std::for_each(other.childElements.begin(), other.childElements.end(), [&](const std::shared_ptr<XmlElement>& child) { child->parent_ = &other; });
223  }
224 
225 private:
226  friend class XmlDoc;
227 
228  XmlElement(const XmlElement&); //not implemented
229  XmlElement& operator=(const XmlElement&); //
230 
231  std::string name_;
232  std::string value_;
233  std::map<std::string, std::string> attributes;
234  std::vector<std::shared_ptr<XmlElement>> childElements; //all child elements in order of creation
235  std::multimap<std::string, std::shared_ptr<XmlElement>> childElementsSorted; //alternate key: sorted by element name
236  XmlElement* parent_;
237 };
238 
239 
240 //XmlElement::setValue<T>() calls zen::writeStruc() which calls XmlElement::setValue() ... => these two specializations end the circle
241 template <> inline
242 void XmlElement::setValue(const std::string& value) { value_ = value; }
243 
244 template <> inline
245 bool XmlElement::getValue(std::string& value) const { value = value_; return true; }
246 
247 
249 class XmlDoc
250 {
251 public:
253  XmlDoc() : version_("1.0"), encoding_("UTF-8"), rootElement("Root", nullptr, XmlElement::PrivateConstruction()) {}
254 
255  XmlDoc(XmlDoc&& tmp) : rootElement("Root", nullptr, XmlElement::PrivateConstruction()) { swap(tmp); }
256  XmlDoc& operator=(XmlDoc&& tmp) { swap(tmp); return *this; }
257 
258  //Setup an empty XML document
263  template <class String>
264  XmlDoc(String rootName) : version_("1.0"), encoding_("UTF-8"), rootElement(rootName, nullptr, XmlElement::PrivateConstruction()) {}
265 
267  const XmlElement& root() const { return rootElement; }
269  XmlElement& root() { return rootElement; }
270 
272 
275  template <class String>
276  String getVersionAs() const { return utfCvrtTo<String>(version_); }
277 
279 
282  template <class String>
283  void setVersion(const String& version) { version_ = utfCvrtTo<std::string>(version); }
284 
286 
289  template <class String>
290  String getEncodingAs() const { return utfCvrtTo<String>(encoding_); }
291 
293 
296  template <class String>
297  void setEncoding(const String& encoding) { encoding_ = utfCvrtTo<std::string>(encoding); }
298 
300 
303  template <class String>
304  String getStandaloneAs() const { return utfCvrtTo<String>(standalone_); }
305 
307 
310  template <class String>
311  void setStandalone(const String& standalone) { standalone_ = utfCvrtTo<std::string>(standalone); }
312 
313  //Transactionally swap two elements. -> disabled documentation extraction
314  void swap(XmlDoc& other)
315  {
316  version_ .swap(other.version_);
317  encoding_ .swap(other.encoding_);
318  standalone_.swap(other.standalone_);
319  rootElement.swap(other.rootElement);
320  }
321 
322 private:
323  XmlDoc(const XmlDoc&); //not implemented, thanks to XmlElement::parent_
324  XmlDoc& operator=(const XmlDoc&); //
325 
326  std::string version_;
327  std::string encoding_;
328  std::string standalone_;
329 
330  XmlElement rootElement;
331 };
332 
333 }
334 
335 #endif //ZEN_XML_DOM_HEADER_82085720723894567204564256
const XmlElement & root() const
Get a const reference to the document's root element.
Definition: dom.h:267
const XmlElement * parent() const
Get parent XML element, may be nullptr for root element.
Definition: dom.h:196
bool getValue(T &value) const
Get the value of this element as a user type.
Definition: dom.h:45
void removeAttribute(const String &name)
Remove the attribute with the given name.
Definition: dom.h:89
std::pair< ChildIterConst2, ChildIterConst2 > getChildren(const String &name) const
Access all child elements with the given name via STL iterators.
Definition: dom.h:164
void setEncoding(const String &encoding)
Set the encoding used in the XML declaration.
Definition: dom.h:297
std::pair< ChildIterConst, ChildIterConst > getChildren() const
Access all child elements sequentially via STL iterators.
Definition: dom.h:188
bool readText(const std::string &input, T &value)
Convert text to user data - used by XML elements and attributes.
Definition: cvrt_text.h:216
An XML element.
Definition: dom.h:21
XmlDoc(String rootName)
Definition: dom.h:264
std::pair< ChildIter, ChildIter > getChildren()
Definition: dom.h:191
XmlElement * getChild(const String &name)
Definition: dom.h:121
bool getAttribute(const String &name, T &value) const
Retrieve an attribute by name.
Definition: dom.h:63
void setVersion(const String &version)
Set the version used in the XML declaration.
Definition: dom.h:283
void setStandalone(const String &standalone)
Set the standalone string used in the XML declaration.
Definition: dom.h:311
The complete XML document.
Definition: dom.h:249
XmlDoc()
Default constructor setting up an empty XML document with a standard declaration: <...
Definition: dom.h:253
String getEncodingAs() const
Get the encoding used in the XML declaration.
Definition: dom.h:290
XmlElement & addChild(const String &name)
Create a new child element and return a reference to it.
Definition: dom.h:97
XmlElement & root()
Get a reference to the document's root element.
Definition: dom.h:269
const XmlElement * getChild(const String &name) const
Retrieve a child element with the given name.
Definition: dom.h:113
bool readStruc(const XmlElement &input, T &value)
Convert XML element to structured user data.
Definition: cvrt_struc.h:205
XmlElement * parent()
Get parent XML element, may be nullptr for root element.
Definition: dom.h:194
void setValue(const T &value)
Set the value of this element.
Definition: dom.h:52
String getVersionAs() const
Get the version used in the XML declaration.
Definition: dom.h:276
void setAttribute(const String &name, const T &value)
Create or update an XML attribute.
Definition: dom.h:77
void writeStruc(const T &value, XmlElement &output)
Convert structured user data into an XML element.
Definition: cvrt_struc.h:198
std::pair< ChildIter2, ChildIter2 > getChildren(const String &name)
Definition: dom.h:168
String getStandaloneAs() const
Get the standalone string used in the XML declaration.
Definition: dom.h:304
String getNameAs() const
Retrieve the name of this XML element.
Definition: dom.h:37
void writeText(const T &value, std::string &output)
Convert user data into text - used by XML elements and attributes.
Definition: cvrt_text.h:209