Back to index

nordugrid-arc-nox  1.1.0~rc6
XMLNode.h
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifndef __ARC_XMLNODE_H__
00004 #define __ARC_XMLNODE_H__
00005 
00006 #include <iostream>
00007 #include <string>
00008 #include <list>
00009 #include <vector>
00010 #include <map>
00011 
00012 #include <libxml/xmlmemory.h>
00013 #include <libxml/tree.h>
00014 #include <libxml/parser.h>
00015 #include <libxml/xpath.h>
00016 #include <libxml/xpathInternals.h>
00017 
00018 
00019 namespace Arc {
00020 
00021   class XMLNode;
00022 
00023   class NS
00024     : public std::map<std::string, std::string> {
00025   public:
00027     NS(void) {}
00029     NS(const char *prefix, const char *uri) {
00030       operator[](prefix) = uri;
00031     }
00033     /*** Array is made of prexif and URI pairs and must be NULL terminated */
00034     NS(const char *nslist[][2]) {
00035       for (int n = 0; nslist[n][0]; ++n)
00036         operator[](nslist[n][0]) = nslist[n][1];
00037     }
00038     NS(const std::map<std::string, std::string>& nslist)
00039       : std::map<std::string, std::string>(nslist) {}
00040   };
00041 
00042   typedef std::list<XMLNode> XMLNodeList;
00043 
00045 
00054   class XMLNode {
00055     friend bool MatchXMLName(const XMLNode& node1, const XMLNode& node2);
00056     friend bool MatchXMLName(const XMLNode& node, const char *name);
00057     friend bool MatchXMLName(const XMLNode& node, const std::string& name);
00058     friend bool MatchXMLNamespace(const XMLNode& node1, const XMLNode& node2);
00059     friend bool MatchXMLNamespace(const XMLNode& node, const char *uri);
00060     friend bool MatchXMLNamespace(const XMLNode& node, const std::string& uri);
00061     friend class XMLNodeContainer;
00062 
00063   protected:
00064     xmlNodePtr node_;
00068     bool is_owner_;
00070     bool is_temporary_;
00076     XMLNode(xmlNodePtr node)
00077       : node_(node),
00078         is_owner_(false),
00079         is_temporary_(false) {}
00080   public:
00084     XMLNode(void)
00085       : node_(NULL),
00086         is_owner_(false),
00087         is_temporary_(false) {}
00092     XMLNode(const XMLNode& node)
00093       : node_(node.node_),
00094         is_owner_(false),
00095         is_temporary_(false) {}
00098     XMLNode(const std::string& xml);
00100     XMLNode(const char *xml, int len = -1);
00102     XMLNode(long ptr_addr);
00106     XMLNode(const NS& ns, const char *name);
00109     ~XMLNode(void);
00113     void New(XMLNode& node) const;
00128     void Exchange(XMLNode& node);
00135     void Move(XMLNode& node);
00141     void Swap(XMLNode& node);
00143     operator bool(void) const {
00144       return ((node_ != NULL) && (!is_temporary_));
00145     }
00147     bool operator!(void) const {
00148       return ((node_ == NULL) || is_temporary_);
00149     }
00151     bool operator==(const XMLNode& node) {
00152       return ((node_ == node.node_) && (node_ != NULL));
00153     }
00155     bool operator!=(const XMLNode& node) {
00156       return ((node_ != node.node_) || (node_ == NULL));
00157     }
00159     bool Same(const XMLNode& node) {
00160       return operator==(node);
00161     }
00163     bool operator==(bool val) {
00164       return ((bool)(*this) == val);
00165     }
00167     bool operator!=(bool val) {
00168       return ((bool)(*this) != val);
00169     }
00171     bool operator==(const std::string& str) {
00172       return ((std::string)(*this) == str);
00173     }
00175     bool operator!=(const std::string& str) {
00176       return ((std::string)(*this) != str);
00177     }
00179     bool operator==(const char *str) {
00180       return ((std::string)(*this) == str);
00181     }
00183     bool operator!=(const char *str) {
00184       return ((std::string)(*this) != str);
00185     }
00188     XMLNode Child(int n = 0);
00199     XMLNode operator[](const char *name) const;
00201     XMLNode operator[](const std::string& name) const {
00202       return operator[](name.c_str());
00203     }
00213     XMLNode operator[](int n) const;
00216     void operator++(void);
00219     void operator--(void);
00221     int Size(void) const;
00223     XMLNode Get(const std::string& name) const {
00224       return operator[](name.c_str());
00225     }
00227     std::string Name(void) const;
00229     std::string Prefix(void) const;
00231     std::string FullName(void) const {
00232       return Prefix() + ":" + Name();
00233     }
00235     std::string Namespace(void) const;
00237     void Name(const char *name);
00239     void Name(const std::string& name) {
00240       Name(name.c_str());
00241     }
00243     void GetXML(std::string& out_xml_str, bool user_friendly = false) const;
00247     void GetXML(std::string& out_xml_str, const std::string& encoding, bool user_friendly = false) const;
00249     void GetDoc(std::string& out_xml_str, bool user_friendly = false) const;
00251     operator std::string(void) const;
00253     XMLNode& operator=(const char *content);
00255     XMLNode& operator=(const std::string& content) {
00256       return operator=(content.c_str());
00257     }
00259     void Set(const std::string& content) {
00260       operator=(content.c_str());
00261     }
00265     XMLNode& operator=(const XMLNode& node);
00267     // std::list<XMLNode> Attributes(void);
00269     XMLNode Attribute(int n = 0);
00271     XMLNode Attribute(const char *name);
00273     XMLNode Attribute(const std::string& name) {
00274       return Attribute(name.c_str());
00275     }
00277     XMLNode NewAttribute(const char *name);
00279     XMLNode NewAttribute(const std::string& name) {
00280       return NewAttribute(name.c_str());
00281     }
00283     int AttributesSize(void) const;
00294     void Namespaces(const NS& namespaces, bool keep = false, int recursion = -1);
00296     NS Namespaces(void);
00299     std::string NamespacePrefix(const char *urn);
00304     XMLNode NewChild(const char *name, int n = -1, bool global_order = false);
00306     XMLNode NewChild(const std::string& name, int n = -1, bool global_order = false) {
00307       return NewChild(name.c_str(), n, global_order);
00308     }
00311     XMLNode NewChild(const char *name, const NS& namespaces, int n = -1, bool global_order = false);
00313     XMLNode NewChild(const std::string& name, const NS& namespaces, int n = -1, bool global_order = false) {
00314       return NewChild(name.c_str(), namespaces, n, global_order);
00315     }
00319     XMLNode NewChild(const XMLNode& node, int n = -1, bool global_order = false);
00321     void Replace(const XMLNode& node);
00325     void Destroy(void);
00332     XMLNodeList Path(const std::string& path);
00340     XMLNodeList XPathLookup(const std::string& xpathExpr, const NS& nsList);
00342     XMLNode GetRoot(void);
00344     XMLNode Parent(void);
00346     bool SaveToFile(const std::string& file_name) const;
00348     bool SaveToStream(std::ostream& out) const;
00350     bool ReadFromFile(const std::string& file_name);
00352     bool ReadFromStream(std::istream& in);
00356     bool Validate(const std::string &schema_file, std::string &err_msg);
00357   };
00358 
00359   std::ostream& operator<<(std::ostream& out, const XMLNode& node);
00360   std::istream& operator>>(std::istream& in, XMLNode& node);
00361 
00363   class XMLNodeContainer {
00364   private:
00365     std::vector<XMLNode*> nodes_;
00366   public:
00368     XMLNodeContainer(void);
00373     XMLNodeContainer(const XMLNodeContainer&);
00374     ~XMLNodeContainer(void);
00376     XMLNodeContainer& operator=(const XMLNodeContainer&);
00379     void Add(const XMLNode&);
00381     void Add(const std::list<XMLNode>&);
00385     void AddNew(const XMLNode&);
00387     void AddNew(const std::list<XMLNode>&);
00389     int Size(void) const;
00391     XMLNode operator[](int);
00393     std::list<XMLNode> Nodes(void);
00394   };
00395 
00397   bool MatchXMLName(const XMLNode& node1, const XMLNode& node2);
00398 
00400   bool MatchXMLName(const XMLNode& node, const char *name);
00401 
00403   bool MatchXMLName(const XMLNode& node, const std::string& name);
00404 
00406   bool MatchXMLNamespace(const XMLNode& node1, const XMLNode& node2);
00407 
00409   bool MatchXMLNamespace(const XMLNode& node, const char *uri);
00410 
00412   bool MatchXMLNamespace(const XMLNode& node, const std::string& uri);
00413 
00414 
00415 } // namespace Arc
00416 
00417 #endif /* __ARC_XMLNODE_H__ */