ARC SDK
XMLNode.h
1 // -*- indent-tabs-mode: nil -*-
2 
3 #ifndef __ARC_XMLNODE_H__
4 #define __ARC_XMLNODE_H__
5 
6 #include <iostream>
7 #include <string>
8 #include <list>
9 #include <vector>
10 #include <map>
11 
12 #include <libxml/xmlmemory.h>
13 #include <libxml/tree.h>
14 #include <libxml/parser.h>
15 #include <libxml/xpath.h>
16 #include <libxml/xpathInternals.h>
17 #include <libxml/xmlschemas.h>
18 
19 namespace Arc {
20 
24  class XMLNode;
25 
27 
28  class NS
29  : public std::map<std::string, std::string> {
30  public:
32  NS(void) {}
34  NS(const char *prefix, const char *uri) {
35  operator[](prefix) = uri;
36  }
38 
40  NS(const char *nslist[][2]) {
41  for (int n = 0; nslist[n][0]; ++n)
42  operator[](nslist[n][0]) = nslist[n][1];
43  }
45  NS(const std::map<std::string, std::string>& nslist)
46  : std::map<std::string, std::string>(nslist) {}
47  };
48 
49  typedef std::list<XMLNode> XMLNodeList;
50 
52 
61  class XMLNode {
62  friend bool MatchXMLName(const XMLNode& node1, const XMLNode& node2);
63  friend bool MatchXMLName(const XMLNode& node, const char *name);
64  friend bool MatchXMLName(const XMLNode& node, const std::string& name);
65  friend bool MatchXMLNamespace(const XMLNode& node1, const XMLNode& node2);
66  friend bool MatchXMLNamespace(const XMLNode& node, const char *uri);
67  friend bool MatchXMLNamespace(const XMLNode& node, const std::string& uri);
68  friend class XMLNodeContainer;
69 
70  protected:
71  xmlNodePtr node_;
73 
74  bool is_owner_;
78 
82  XMLNode(xmlNodePtr node)
83  : node_(node),
84  is_owner_(false),
85  is_temporary_(false) {}
86 
88  static void LogError(void * ctx, const char * msg, ...);
89 
91  bool Validate(xmlSchemaPtr schema, std::string &err_msg);
92 
93  public:
95 
97  XMLNode(void)
98  : node_(NULL),
99  is_owner_(false),
100  is_temporary_(false) {}
102 
105  XMLNode(const XMLNode& node)
106  : node_(node.node_),
107  is_owner_(false),
108  is_temporary_(false) {}
110 
111  XMLNode(const std::string& xml);
113 
114  XMLNode(const char *xml, int len = -1);
116  XMLNode(long ptr_addr);
118 
120  XMLNode(const NS& ns, const char *name);
122 
123  ~XMLNode(void);
125 
127  void New(XMLNode& node) const;
129 
142  void Exchange(XMLNode& node);
144 
149  void Move(XMLNode& node);
151 
158  void Swap(XMLNode& node);
160  operator bool(void) const {
161  return ((node_ != NULL) && (!is_temporary_));
162  }
164  bool operator!(void) const {
165  return ((node_ == NULL) || is_temporary_);
166  }
168  bool operator==(const XMLNode& node) {
169  return ((node_ == node.node_) && (node_ != NULL));
170  }
172  bool operator!=(const XMLNode& node) {
173  return ((node_ != node.node_) || (node_ == NULL));
174  }
176  bool Same(const XMLNode& node) {
177  return operator==(node);
178  }
180  bool operator==(bool val) {
181  return ((bool)(*this) == val);
182  }
184  bool operator!=(bool val) {
185  return ((bool)(*this) != val);
186  }
188  bool operator==(const std::string& str) {
189  return ((std::string)(*this) == str);
190  }
192  bool operator!=(const std::string& str) {
193  return ((std::string)(*this) != str);
194  }
196  bool operator==(const char *str) {
197  return ((std::string)(*this) == str);
198  }
200  bool operator!=(const char *str) {
201  return ((std::string)(*this) != str);
202  }
204 
205  XMLNode Child(int n = 0);
207 
215  XMLNode operator[](const char *name) const;
217 
218  XMLNode operator[](const std::string& name) const {
219  return operator[](name.c_str());
220  }
222 
230  XMLNode operator[](int n) const;
232 
233  void operator++(void);
235 
236  void operator--(void);
238  int Size(void) const;
240  XMLNode Get(const std::string& name) const {
241  return operator[](name.c_str());
242  }
244  std::string Name(void) const;
246  std::string Prefix(void) const;
248  std::string FullName(void) const {
249  return Prefix() + ":" + Name();
250  }
252  std::string Namespace(void) const;
254 
257  void Prefix(const std::string& prefix, int recursion = 0);
259  void StripNamespace(int recursion = 0);
261  void Name(const char *name);
263  void Name(const std::string& name) {
264  Name(name.c_str());
265  }
267  void GetXML(std::string& out_xml_str, bool user_friendly = false) const;
269 
272  void GetXML(std::string& out_xml_str, const std::string& encoding, bool user_friendly = false) const;
274  void GetDoc(std::string& out_xml_str, bool user_friendly = false) const;
276  operator std::string(void) const;
278  XMLNode& operator=(const char *content);
280  XMLNode& operator=(const std::string& content) {
281  return operator=(content.c_str());
282  }
284  void Set(const std::string& content) {
285  operator=(content.c_str());
286  }
288 
290  XMLNode& operator=(const XMLNode& node);
291  // Returns list of all attributes of node
292  // std::list<XMLNode> Attributes(void);
294  XMLNode Attribute(int n = 0);
296  XMLNode Attribute(const char *name);
298  XMLNode Attribute(const std::string& name) {
299  return Attribute(name.c_str());
300  }
302  XMLNode NewAttribute(const char *name);
304  XMLNode NewAttribute(const std::string& name) {
305  return NewAttribute(name.c_str());
306  }
308  int AttributesSize(void) const;
310 
319  void Namespaces(const NS& namespaces, bool keep = false, int recursion = -1);
321  NS Namespaces(void);
323  std::string NamespacePrefix(const char *urn);
325 
328  XMLNode NewChild(const char *name, int n = -1, bool global_order = false);
330  XMLNode NewChild(const std::string& name, int n = -1, bool global_order = false) {
331  return NewChild(name.c_str(), n, global_order);
332  }
334 
335  XMLNode NewChild(const char *name, const NS& namespaces, int n = -1, bool global_order = false);
337  XMLNode NewChild(const std::string& name, const NS& namespaces, int n = -1, bool global_order = false) {
338  return NewChild(name.c_str(), namespaces, n, global_order);
339  }
341 
343  XMLNode NewChild(const XMLNode& node, int n = -1, bool global_order = false);
345  void Replace(const XMLNode& node);
347 
349  void Destroy(void);
351 
356  XMLNodeList Path(const std::string& path);
358 
367  XMLNodeList XPathLookup(const std::string& xpathExpr, const NS& nsList);
369  XMLNode GetRoot(void);
371  XMLNode Parent(void);
373  bool SaveToFile(const std::string& file_name) const;
375  bool SaveToStream(std::ostream& out) const;
377  bool ReadFromFile(const std::string& file_name);
379  bool ReadFromStream(std::istream& in);
380  // Remove all eye-candy information leaving only informational parts
381  // void Purify(void);.
383  bool Validate(const std::string &schema_file, std::string &err_msg);
385  bool Validate(XMLNode schema_doc, std::string &err_msg);
386  };
387 
389  std::ostream& operator<<(std::ostream& out, const XMLNode& node);
391  std::istream& operator>>(std::istream& in, XMLNode& node);
392 
394 
396  private:
397  std::vector<XMLNode*> nodes_;
398  public:
400  XMLNodeContainer(void);
402 
406  ~XMLNodeContainer(void);
410 
411  void Add(const XMLNode&);
413  void Add(const std::list<XMLNode>&);
415 
417  void AddNew(const XMLNode&);
419  void AddNew(const std::list<XMLNode>&);
421  int Size(void) const;
423  XMLNode operator[](int);
425  std::list<XMLNode> Nodes(void);
426  };
427 
429  bool MatchXMLName(const XMLNode& node1, const XMLNode& node2);
430 
432  bool MatchXMLName(const XMLNode& node, const char *name);
433 
435  bool MatchXMLName(const XMLNode& node, const std::string& name);
436 
438  bool MatchXMLNamespace(const XMLNode& node1, const XMLNode& node2);
439 
441  bool MatchXMLNamespace(const XMLNode& node, const char *uri);
442 
444  bool MatchXMLNamespace(const XMLNode& node, const std::string& uri);
445 
448 } // namespace Arc
449 
450 #endif /* __ARC_XMLNODE_H__ */
Arc namespace contains all core ARC classes.
Definition: ArcConfig.h:11
static void LogError(void *ctx, const char *msg,...)
void operator--(void)
Convenience operator to switch to previous element of same name.
Container for multiple XMLNode elements.
Definition: XMLNode.h:395
XMLNode GetRoot(void)
Get the root node from any child node of the tree.
void Replace(const XMLNode &node)
Makes a copy of supplied XML node and makes this instance refer to it.
void Destroy(void)
Destroys underlying XML element.
void StripNamespace(int recursion=0)
Removes namespace prefix from XML node(s).
int AttributesSize(void) const
Returns number of attributes of node.
Wrapper for LibXML library Tree interface.
Definition: XMLNode.h:61
Class to represent an XML namespace.
Definition: XMLNode.h:28
XMLNode Parent(void)
Get the parent node from any child node of the tree.
int Size(void) const
Returns number of children nodes.
bool operator==(const std::string &str)
This operator is needed to avoid ambiguity.
Definition: XMLNode.h:188
void New(XMLNode &node) const
Creates a copy of XML (sub)tree.
int Size(void) const
Return number of refered/stored nodes.
std::string FullName(void) const
Returns prefix:name of XML node.
Definition: XMLNode.h:248
bool operator==(const char *str)
This operator is needed to avoid ambiguity.
Definition: XMLNode.h:196
void operator++(void)
Convenience operator to switch to next element of same name.
bool MatchXMLName(const XMLNode &node1, const XMLNode &node2)
Returns true if underlying XML elements have same names.
NS(void)
Constructor creates empty namespace.
Definition: XMLNode.h:32
XMLNode Attribute(const std::string &name)
Returns XMLNode instance representing first attribute of node with specified by name.
Definition: XMLNode.h:298
XMLNode NewChild(const std::string &name, int n=-1, bool global_order=false)
Same as NewChild(const char*,int,bool).
Definition: XMLNode.h:330
bool operator!=(bool val)
This operator is needed to avoid ambiguity.
Definition: XMLNode.h:184
friend bool MatchXMLNamespace(const XMLNode &node1, const XMLNode &node2)
Returns true if underlying XML elements belong to same namespaces.
XMLNodeContainer(void)
Default constructor.
XMLNode NewChild(const std::string &name, const NS &namespaces, int n=-1, bool global_order=false)
Same as NewChild(const char*,const NS&,int,bool).
Definition: XMLNode.h:337
void Add(const XMLNode &)
Link XML subtree refered by node to container.
XMLNode Attribute(int n=0)
Returns XMLNode instance reresenting n-th attribute of node.
XMLNodeList Path(const std::string &path)
Collects nodes corresponding to specified path.
XMLNode Child(int n=0)
Returns XMLNode instance representing n-th child of XML element.
bool operator==(const XMLNode &node)
Returns true if &#39;node&#39; represents same XML element.
Definition: XMLNode.h:168
bool ReadFromStream(std::istream &in)
Read XML document from stream and associate it with this node.
bool MatchXMLNamespace(const XMLNode &node1, const XMLNode &node2)
Returns true if underlying XML elements belong to same namespaces.
std::string Namespace(void) const
Returns namespace URI of XML node.
void Name(const std::string &name)
Assigns new name to XML node.
Definition: XMLNode.h:263
std::list< XMLNode > Nodes(void)
Returns all stored nodes.
bool operator==(bool val)
This operator is needed to avoid ambiguity.
Definition: XMLNode.h:180
bool SaveToFile(const std::string &file_name) const
Save string representation of node to file.
void Exchange(XMLNode &node)
Exchanges XML (sub)trees.
bool operator!(void) const
Returns true if instance does not point to XML element - invalid instance.
Definition: XMLNode.h:164
XMLNode NewChild(const char *name, int n=-1, bool global_order=false)
Creates new child XML element at specified position with specified name.
bool is_owner_
If true node is owned by this instance - hence released in destructor.
Definition: XMLNode.h:74
bool is_temporary_
This variable is reserved for future use.
Definition: XMLNode.h:76
XMLNode NewAttribute(const char *name)
Creates new attribute with specified name.
bool ReadFromFile(const std::string &file_name)
Read XML document from file and associate it with this node.
void GetXML(std::string &out_xml_str, bool user_friendly=false) const
Fills argument with this instance XML subtree textual representation.
std::ostream & operator<<(std::ostream &, const Period &)
Prints a Period-object to the given ostream – typically cout.
void Swap(XMLNode &node)
Swaps XML (sub)trees to which this and node refer.
bool Validate(xmlSchemaPtr schema, std::string &err_msg)
XMLNode(const XMLNode &node)
Copies existing instance.
Definition: XMLNode.h:105
std::string Name(void) const
Returns name of XML node.
bool operator!=(const XMLNode &node)
Returns false if &#39;node&#39; represents same XML element.
Definition: XMLNode.h:172
bool SaveToStream(std::ostream &out) const
Save string representation of node to stream.
void Move(XMLNode &node)
Moves content of this XML (sub)tree to node.
void GetDoc(std::string &out_xml_str, bool user_friendly=false) const
Fills out_xml_str with whole XML document textual representation.
XMLNode operator[](int)
Returns n-th node in a store.
XMLNode(void)
Constructor of invalid node.
Definition: XMLNode.h:97
XMLNode Get(const std::string &name) const
Same as operator[]().
Definition: XMLNode.h:240
bool operator!=(const std::string &str)
This operator is needed to avoid ambiguity.
Definition: XMLNode.h:192
std::string Prefix(void) const
Returns namespace prefix of XML node.
~XMLNode(void)
Destructor.
XMLNode operator[](const char *name) const
Returns XMLNode instance representing first child element with specified name.
friend bool MatchXMLName(const XMLNode &node1, const XMLNode &node2)
Returns true if underlying XML elements have same names.
XMLNode operator[](const std::string &name) const
Returns XMLNode instance representing first child element with specified name.
Definition: XMLNode.h:218
XMLNodeContainer & operator=(const XMLNodeContainer &)
Same as copy constructor with current nodes being deleted first.
NS Namespaces(void)
Returns namespaces known at this node.
std::istream & operator>>(std::istream &in, XMLNode &node)
Read into XMLNode from input stream.
XMLNodeList XPathLookup(const std::string &xpathExpr, const NS &nsList)
Uses XPath to look up XML tree.
XMLNode & operator=(const char *content)
Sets textual content of node. All existing children nodes are discarded.
XMLNode(xmlNodePtr node)
Protected constructor for inherited classes.
Definition: XMLNode.h:82
void Set(const std::string &content)
Same as operator=. Used for bindings.
Definition: XMLNode.h:284
XMLNode & operator=(const std::string &content)
Sets textual content of node. All existing children nodes are discarded.
Definition: XMLNode.h:280
bool Same(const XMLNode &node)
Returns true if &#39;node&#39; represents same XML element - for bindings.
Definition: XMLNode.h:176
XMLNode NewAttribute(const std::string &name)
Creates new attribute with specified name.
Definition: XMLNode.h:304
std::string NamespacePrefix(const char *urn)
Returns prefix of specified namespace or empty string if no such namespace.
NS(const char *prefix, const char *uri)
Constructor creates namespace with one entry.
Definition: XMLNode.h:34
void AddNew(const XMLNode &)
Copy XML subtree referenced by node to container.
NS(const char *nslist[][2])
Constructor creates namespace with multiple entries.
Definition: XMLNode.h:40
NS(const std::map< std::string, std::string > &nslist)
Constructor creates namespace with multiple entries.
Definition: XMLNode.h:45
bool operator!=(const char *str)
This operator is needed to avoid ambiguity.
Definition: XMLNode.h:200