Scopira 20080306

xml.h

00001 
00002 /*
00003  *  Copyright (c) 2003-2007    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013 
00014 #ifndef __INLUCDED__SCOPIRA_TOOL_XML_H_
00015 #define __INLUCDED__SCOPIRA_TOOL_XML_H_
00016 
00017 #include <libxml/parser.h>
00018 
00019 #include <string>
00020 
00021 #include <scopira/tool/object.h>
00022 #include <scopira/tool/export.h>
00023 
00024 // THIS FILE HAS BEEN FULLY DOCUMENTED
00025 
00026 namespace scopira
00027 {
00028   namespace tool
00029   {
00030     class xml_doc;
00031     class xml_node;
00032     class xml_attrib;
00033     // forward
00034     class property;
00035   }
00036 }
00037 
00038 //
00039 // TIPS:
00040 //  use xmllint.
00041 //  or xmlstarlet val
00042 //
00043 
00050 class scopira::tool::xml_doc : public scopira::tool::object
00051 {
00052   private:
00053     xmlDocPtr dm_doc;
00054 
00055   public:
00057 
00062     xml_doc(void) : dm_doc(0) { }
00064     ~xml_doc() { clear(); }
00065 
00067     xmlDocPtr get_ptr(void) const { return dm_doc; }
00068 
00073     bool is_null(void) const { return dm_doc == 0; }
00078     bool is_valid(void) const { return dm_doc != 0; }
00079 
00084     const char * get_c_name(void) const { return dm_doc->name; }
00089     std::string get_name(void) const
00090       { return std::string(reinterpret_cast<const char*>(dm_doc->name)); }
00091 
00096     SCOPIRA_EXPORT void clear(void);
00097 
00102     SCOPIRA_EXPORT void init(const std::string &rootnodename);
00103 
00109     SCOPIRA_EXPORT void add_pi(const std::string &name, const std::string &content = "");
00110 
00119     SCOPIRA_EXPORT void load_ptr(xmlDocPtr newdoc);
00120 
00128     SCOPIRA_EXPORT bool load_file(const std::string &filename);
00129 
00136     SCOPIRA_EXPORT bool load_string(const std::string &data);
00137 
00144     SCOPIRA_EXPORT bool load_c_str(const char *str);
00145 
00151     SCOPIRA_EXPORT void load_prop(property *p);
00152 
00157     SCOPIRA_EXPORT xml_node get_root(void) const;
00158 
00166     SCOPIRA_EXPORT bool save_file(const std::string &filename, bool compress = false) const;
00167 
00174     SCOPIRA_EXPORT bool save_string(std::string &out) const;
00175 
00181     SCOPIRA_EXPORT void save_prop(property *p) const;
00182 
00183   private:
00184     // diable
00185     xml_doc(const xml_doc &) { }
00186 };
00187 
00195 class scopira::tool::xml_node
00196 {
00197   public:
00199     enum {
00200       element_c = XML_ELEMENT_NODE,
00201       //attrib_c = XML_ATTRIBUTE_NODE,
00202       text_c = XML_TEXT_NODE,
00203     };
00204 
00205   private:
00206     xmlNodePtr dm_node;
00207 
00208   public:
00213     xml_node(void) : dm_node(0) { }
00218     explicit xml_node(xmlNodePtr ptr) : dm_node(ptr) { }
00219 
00221     xmlNodePtr get_ptr(void) const { return dm_node; }
00222 
00227     int get_type(void) const { return dm_node->type; }
00228 
00233     const char * get_c_name(void) const
00234       { return reinterpret_cast<const char*>(dm_node->name); }
00239     std::string get_name(void) const
00240       { return std::string(reinterpret_cast<const char*>(dm_node->name)); }
00241 
00248     const char * get_c_content(void) const
00249       { return reinterpret_cast<const char *>(dm_node->content); }
00250 
00255     bool is_null(void) const { return dm_node == 0; }
00260     bool is_valid(void) const { return dm_node != 0; }
00261 
00266     void next(void) { dm_node = dm_node->next; }
00272     SCOPIRA_EXPORT void next_type(int typ);
00278     SCOPIRA_EXPORT void next_name(const std::string &name);
00279 
00284     SCOPIRA_EXPORT int count_children(const std::string &name) const;
00285 
00291     xml_node get_first_child(void) const
00292       { return xml_node(dm_node->children); }
00298     SCOPIRA_EXPORT xml_node get_first_child_type(int typ) const;
00304     SCOPIRA_EXPORT xml_node get_first_child_name(const std::string &name) const;
00305 
00310     SCOPIRA_EXPORT xml_attrib get_first_attrib(void) const;
00319     SCOPIRA_EXPORT const char * get_attrib_c_text(const std::string &aname) const;
00328     SCOPIRA_EXPORT const char * get_child_c_text(const std::string &nname) const;
00329 
00334     SCOPIRA_EXPORT bool has_attrib(const std::string &aname) const;
00335 
00340     SCOPIRA_EXPORT xml_node add_child(const std::string &name);
00341 
00346     SCOPIRA_EXPORT xml_node add_comment(const std::string &comment_contents);
00347 
00353     SCOPIRA_EXPORT void clear_this(void);
00358     SCOPIRA_EXPORT void add_attrib(const std::string &key, const std::string &val);
00363     SCOPIRA_EXPORT xml_node add_content_child(const std::string &content);
00369     SCOPIRA_EXPORT void clear_content_children(void);
00370 
00379     SCOPIRA_EXPORT const char * get_text_c_content(void) const;
00380     
00393     SCOPIRA_EXPORT const char * get_leaf_text_content(const std::string &pathnodespec) const;
00394     
00395     // Mass loader helpers
00396 
00402     SCOPIRA_EXPORT void load_string_attrib(const std::string &name, std::string &outval);
00407     template <class I>
00408       void children_load_xml(const std::string &elementname, I iter) const {
00409         for (xml_node sub = get_first_child_name(elementname);
00410             sub.is_valid(); sub.next_name(elementname), ++iter)
00411           (*iter).load_xml(sub);
00412       }
00417     template <class I>
00418       void children_load_attrib(const std::string &elementname,
00419           const std::string &attribname, I iter) const {
00420         for (xml_node sub = get_first_child_name(elementname);
00421             sub.is_valid(); sub.next_name(elementname), ++iter)
00422           sub.load_string_attrib(attribname, *iter);
00423       }
00428     template <class I>
00429       void children_load_text(const std::string &elementname, I iter) const {
00430         for (xml_node sub = get_first_child_name(elementname);
00431             sub.is_valid(); sub.next_name(elementname), ++iter)
00432           *iter = sub.get_text_c_content();
00433       }
00434 };
00435 
00441 class scopira::tool::xml_attrib
00442 {
00443   private:
00444     xmlAttrPtr dm_attr;
00445 
00446   public:
00451     xml_attrib(void) : dm_attr(0) { }
00456     xml_attrib(xmlAttrPtr ptr) : dm_attr(ptr) { }
00457 
00459     xmlAttrPtr get_ptr(void) const { return dm_attr; }
00460 
00465     const char * get_c_name(void) const
00466       { return reinterpret_cast<const char*>(dm_attr->name); }
00471     std::string get_name(void) const
00472       { return std::string(reinterpret_cast<const char*>(dm_attr->name)); }
00473 
00478     bool is_null(void) const { return dm_attr == 0; }
00483     bool is_valid(void) const { return dm_attr != 0; }
00484 
00489     void next(void) { dm_attr = dm_attr->next; }
00490 
00496     SCOPIRA_EXPORT const char * get_text_c_content(void) const;
00497 };
00498 
00499 #endif
00500