7 #ifndef ZEN_XML_DOM_HEADER_82085720723894567204564256
8 #define ZEN_XML_DOM_HEADER_82085720723894567204564256
14 #include "cvrt_text.h"
23 struct PrivateConstruction {};
28 template <
class String>
29 XmlElement(
const String& name,
XmlElement* parentElement, PrivateConstruction) : name_(utfCvrtTo<std::string>(name)), parent_(parentElement) {}
36 template <
class String>
37 String
getNameAs()
const {
return utfCvrtTo<String>(name_); }
62 template <
class String,
class T>
65 auto it = attributes.find(utfCvrtTo<std::string>(name));
66 return it == attributes.end() ?
false :
readText(it->second, value);
76 template <
class String,
class T>
79 std::string attrValue;
81 attributes[utfCvrtTo<std::string>(name)] = attrValue;
88 template <
class String>
89 void removeAttribute(
const String& name) { attributes.erase(utfCvrtTo<std::string>(name)); }
96 template <
class String>
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));
112 template <
class String>
115 auto it = childElementsSorted.find(utfCvrtTo<std::string>(name));
116 return it == childElementsSorted.end() ?
nullptr : &*(it->second);
120 template <
class String>
126 template <
class IterTy,
129 class PtrIter :
public std::iterator<std::input_iterator_tag, T>,
private AccessPolicy
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_); }
146 template <
class T,
class IterTy>
147 T& objectRef(
const IterTy& it) {
return *(it->second); }
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;
163 template <
class String>
164 std::pair<ChildIterConst2, ChildIterConst2>
getChildren(
const String& name)
const {
return childElementsSorted.equal_range(utfCvrtTo<std::string>(name)); }
167 template <
class String>
168 std::pair<ChildIter2, ChildIter2>
getChildren(
const String& name) {
return childElementsSorted.equal_range(utfCvrtTo<std::string>(name)); }
172 template <
class T,
class IterTy>
173 T& objectRef(
const IterTy& it) {
return **it; }
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;
188 std::pair<ChildIterConst, ChildIterConst>
getChildren()
const {
return std::make_pair(childElements.begin(), childElements.end()); }
191 std::pair<ChildIter, ChildIter>
getChildren() {
return std::make_pair(childElements.begin(), childElements.end()); }
199 typedef std::map<std::string, std::string>::const_iterator AttrIter;
210 std::pair<AttrIter, AttrIter> getAttributes()
const {
return std::make_pair(attributes.begin(), attributes.end()); }
213 void swap(XmlElement& other)
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);
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; });
228 XmlElement(
const XmlElement&);
229 XmlElement& operator=(
const XmlElement&);
233 std::map<std::string, std::string> attributes;
234 std::vector<std::shared_ptr<XmlElement>> childElements;
235 std::multimap<std::string, std::shared_ptr<XmlElement>> childElementsSorted;
253 XmlDoc() : version_(
"1.0"), encoding_(
"UTF-8"), rootElement(
"Root", nullptr,
XmlElement::PrivateConstruction()) {}
256 XmlDoc& operator=(
XmlDoc&& tmp) { swap(tmp);
return *
this; }
263 template <
class String>
264 XmlDoc(String rootName) : version_(
"1.0"), encoding_(
"UTF-8"), rootElement(rootName, nullptr,
XmlElement::PrivateConstruction()) {}
275 template <
class String>
282 template <
class String>
283 void setVersion(
const String& version) { version_ = utfCvrtTo<std::string>(version); }
289 template <
class String>
296 template <
class String>
297 void setEncoding(
const String& encoding) { encoding_ = utfCvrtTo<std::string>(encoding); }
303 template <
class String>
310 template <
class String>
311 void setStandalone(
const String& standalone) { standalone_ = utfCvrtTo<std::string>(standalone); }
316 version_ .swap(other.version_);
317 encoding_ .swap(other.encoding_);
318 standalone_.swap(other.standalone_);
319 rootElement.swap(other.rootElement);
326 std::string version_;
327 std::string encoding_;
328 std::string standalone_;
330 XmlElement rootElement;
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