ADTF  3.18.2
workspace/conan/dev_essential/1.3.3/dw/stable/package/37682420cd166e229516a41c8d6a139a0b13e1e1/include/a_util/xml/dom.h
Go to the documentation of this file.
1 
16 #ifndef A_UTILS_UTIL_XML_DOM_HEADER_INCLUDED_
17 #define A_UTILS_UTIL_XML_DOM_HEADER_INCLUDED_
18 
19 #include <a_util/base/enums.h>
21 
22 #include <list>
23 #include <map>
24 #include <stdexcept>
25 
26 namespace a_util {
27 namespace xml {
28 class DOM;
29 class DOMElement;
30 
32 class DOMException : public std::runtime_error {
33 public:
38  DOMException(const std::string& what);
39 };
40 
42 typedef std::list<DOMElement> DOMElementList;
44 typedef std::map<std::string, std::string> DOMAttributes;
45 
47 class DOMElement {
48 public:
51 
54 
59  DOMElement(const DOMElement& element);
60 
66  DOMElement& operator=(const DOMElement& element);
67 
74  std::string getAttribute(const std::string& name,
75  const std::string& def_value = std::string()) const;
76 
82 
88  DOMElement getChild(const std::string& name) const;
89 
95 
100  std::string getData() const;
101 
106  DOM getDocument() const;
107 
112  std::string getName() const;
113 
119 
124  std::string getPath() const;
125 
134  bool setAttribute(const std::string& name, const std::string& strValue);
135 
143  bool setData(const std::string& data);
144 
152  bool setName(const std::string& name);
153 
159  bool hasAttribute(const std::string& name) const;
160 
166  bool eraseAttribute(const std::string& name);
167 
208  bool findNode(const std::string& query, DOMElement& element_ptr) const;
209 
218  bool findNodes(const std::string& query, DOMElementList& elements) const;
219 
225  bool sortNodes(const std::string& query, SortingOrder order);
226 
235  bool sortNodes(const std::string& query, SortingOrder order, std::size_t number_of_nodes);
236 
245  std::size_t sortNodes(const std::string& query,
246  const std::string& attribute,
247  SortingOrder order);
248 
261  bool sortNodes(const std::string& query,
262  const std::string& attribute,
263  SortingOrder order,
264  std::size_t number_of_nodes);
265 
271  DOMElement createChild(const std::string& name);
272 
279  DOMElement createChildBefore(const std::string& name, const DOMElement& before);
280 
286  bool removeChild(const std::string& name);
287 
292  bool isNull() const;
293 
294 private:
301  friend bool operator==(const DOMElement& lhs, const DOMElement& rhs);
302 
303 private:
304  class Implementation;
306  friend class DOM;
307 };
308 
315 bool operator!=(const DOMElement& lhs, const DOMElement& rhs);
316 
318 class DOM {
319 public:
321  DOM();
322 
324  ~DOM();
325 
330  DOM(const DOM& dom);
331 
337  DOM& operator=(const DOM& dom);
338 
344  bool load(const std::string& file_path);
345 
351  bool save(const std::string& file_path) const;
352 
358  bool fromString(const std::string& xml);
359 
364  std::string toString() const;
365 
370  void reset();
371 
377 
386  bool findNode(const std::string& query, DOMElement& element_ptr) const;
387 
396  bool findNodes(const std::string& query, DOMElementList& elements) const;
397 
402  std::string getLastError() const;
403 
408  bool isNull() const;
409 
410 private:
411  class Implementation;
413  friend class DOMElement;
414 };
415 
422 bool operator==(const DOM& lhs, const DOM& rhs);
423 
430 bool operator!=(const DOM& lhs, const DOM& rhs);
431 
432 } // namespace xml
433 } // namespace a_util
434 
435 #endif // A_UTILS_UTIL_XML_DOM_HEADER_INCLUDED_
bool removeChild(const std::string &name)
Erases the first child of that name in this element.
bool findNodes(const std::string &query, DOMElementList &elements) const
Finds nodes based on a query.
DOMElement & operator=(const DOMElement &element)
Copy assignment operator.
DOMElement(const DOMElement &element)
Copy constructor.
DOMElement getParent() const
Get this elements parent element.
DOMElement createChild(const std::string &name)
Creates a new child element.
bool sortNodes(const std::string &query, SortingOrder order, std::size_t number_of_nodes)
Sort all queried nodes by name example:
bool setAttribute(const std::string &name, const std::string &strValue)
Sets an attribute.
std::string getData() const
Get the data of the element.
DOMElement createChildBefore(const std::string &name, const DOMElement &before)
Creates a new child element before an existing child element.
std::string getAttribute(const std::string &name, const std::string &def_value=std::string()) const
Returns the value of an attribute with name.
DOMElementList getChildren() const
Get a list of all child elements of the element.
std::size_t sortNodes(const std::string &query, const std::string &attribute, SortingOrder order)
Sort the queried nodes by attribute example:
std::string getPath() const
Get current path of the element.
bool findNode(const std::string &query, DOMElement &element_ptr) const
Finds a node based on a query.
bool setName(const std::string &name)
Sets the name of the element.
bool setData(const std::string &data)
Sets the data of the element.
std::string getName() const
Get the name of the element.
bool sortNodes(const std::string &query, SortingOrder order)
Sort the queried nodes by name example:
DOMElement getChild(const std::string &name) const
Get the first child mathing the name of this element.
friend bool operator==(const DOMElement &lhs, const DOMElement &rhs)
Comparison operator.
bool sortNodes(const std::string &query, const std::string &attribute, SortingOrder order, std::size_t number_of_nodes)
Sort all queried nodes by attribute example:
bool isNull() const
Check whether this DOMElement is empty.
DOMAttributes getAttributes() const
Get a map of all attributes of this element.
bool hasAttribute(const std::string &name) const
Checks if an attribute exists.
DOM getDocument() const
Get the DOM document this element belongs to.
bool eraseAttribute(const std::string &name)
Removes an attribute.
DOMException(const std::string &what)
CTOR.
DOMElement getRoot() const
Get root element of the dom.
DOM(const DOM &dom)
Copy constructor.
bool findNodes(const std::string &query, DOMElementList &elements) const
Finds nodes based on a query.
std::string toString() const
Creates an xml string representation of the dom.
std::string getLastError() const
Get the last error description occurred.
DOM & operator=(const DOM &dom)
Copy assignment.
bool findNode(const std::string &query, DOMElement &element_ptr) const
Finds a node based on a query See DOMElement::findNode() for the syntax.
bool fromString(const std::string &xml)
Create the dom from an input string.
bool load(const std::string &file_path)
Loads an xml file.
bool isNull() const
Check whether this DOM is empty.
void reset()
Resets the instance to the empty state.
bool save(const std::string &file_path) const
save document to an xml file
Several enums for project wide use.
bool operator==(const DOM &lhs, const DOM &rhs)
Comparison operator.
bool operator!=(const DOMElement &lhs, const DOMElement &rhs)
Incomparison operator.
Serves as the root component, with common functionality documented in core functionality.
Definition: base.h:24
SortingOrder
Flags for functions that need to sort elements.
Definition: enums.h:23
Common include for StackPtr functionality.