Don't use xmlpp as the data storage; put things in cxml::Node members
[libcxml.git] / src / cxml.h
index 8be978f431e05314c0a4c3277c6a0a3300eeb725..0542c4e8d2a869e61b85b5b2f568aa62842a4052 100644 (file)
 #undef check
 #endif
 
-#include <glibmm.h>
-
 namespace xmlpp {
        class Node;
-       class DomParser;
 }
 
 namespace cxml {
@@ -67,21 +64,15 @@ private:
 };
 
 class Node;    
-typedef std::list<boost::shared_ptr<Node> > NodeList;  
+typedef boost::shared_ptr<cxml::Node> NodePtr;
+typedef std::list<NodePtr> NodeList;   
+typedef boost::shared_ptr<const cxml::Node> ConstNodePtr;
 
-/** @brief A wrapper for a xmlpp::Node which simplifies parsing */
 class Node
 {
 public:
        Node ();
-       
-       /** Construct a Node from an xmlpp::Node.  This class will
-        *  not destroy the xmlpp::Node.
-        *  @param node xmlpp::Node.
-        */
-       Node (xmlpp::Node* node);
-
-       std::string name () const;
+       Node (xmlpp::Node const *);
 
        /* A set of methods which look up a child of this node by
         * its name, and return its contents as some type or other.
@@ -142,14 +133,7 @@ public:
                return n;
        }
                
-       /** This will mark a child as to be ignored when calling done() */
-       void ignore_child (std::string) const;
-
-       /** Check whether all children of this Node have been looked up
-        *  or passed to ignore_child().  If not, an exception is thrown.
-        */
-       void done () const;
-
+       
        /* These methods look for an attribute of this node, in the
         * same way as the child methods do.
         */
@@ -191,56 +175,108 @@ public:
                return n;
        }
 
-       /** @return The content of this node */
-       std::string content () const;
 
-       /** @return namespace URI of this node */
-       std::string namespace_uri () const;
+       /* Setting content */
 
-       /** @return namespace prefix of this node */
-       std::string namespace_prefix () const;
+       void set_string_content (std::string content);
+       void set_bool_content (bool content);
+
+       template <class T>
+       void set_number_content (T content)
+       {
+               std::stringstream u;
+               u.imbue (std::locale::classic ());
+               u << content;
+               u >> _content;
+       }
+
+
+       /* Short-cuts to add nodes with content */
+
+       NodePtr add_string (std::string name, std::string content);
+       NodePtr add_bool (std::string name, bool content);
+
+       template <class T>
+       NodePtr add_number (std::string name, T content)
+       {
+               NodePtr n = add (name);
+               n->set_number_content (content);
+               return n;
+       }
+
+       
+       /* Access to child nodes */
 
        boost::shared_ptr<Node> node_child (std::string) const;
        boost::shared_ptr<Node> optional_node_child (std::string) const;
-
        NodeList node_children (std::string) const;
 
-       xmlpp::Node* node () const {
-               return _node;
+       /** Add a child node with a given name */
+       NodePtr add (std::string name)
+       {
+               NodePtr n (new cxml::Node ());
+               n->set_name (name);
+               _children.push_back (n);
+               return n;
        }
        
-protected:
-       xmlpp::Node* _node;
+       /** @return The content of this node */
+       std::string content () const;
+       /** @return namespace URI of this node */
+       std::string namespace_uri () const;
+       /** @return namespace prefix of this node */
+       std::string namespace_prefix () const;
+       /** This will mark a child as to be ignored when calling done() */
+       void ignore_child (std::string) const;
+       /** Check whether all children of this Node have been looked up
+        *  or passed to ignore_child().  If not, an exception is thrown.
+        */
+       void done () const;
+       /** Set the name of the node.
+        *  @param n New name.
+        */
+       void set_name (std::string n) {
+               _name = n;
+       }
+       /** @return Node name */
+       std::string name () const {
+               return _name;
+       }
+
+       /* We use xmlpp for parsing and writing XML out; these
+          methods help with that.
+       */
+       void read (xmlpp::Node const *);
+       void write (xmlpp::Node *) const;
+
+protected:     
+       NodeList _children;
        
 private:
-       mutable std::list<Glib::ustring> _taken;
+       std::string _name;
+       std::string _content;
+       std::string _namespace_uri;
+       std::string _namespace_prefix;
+       std::list<std::pair<std::string, std::string> > _attributes;
+       mutable std::list<std::string> _taken;
 };
 
-typedef boost::shared_ptr<cxml::Node> NodePtr;
-typedef boost::shared_ptr<const cxml::Node> ConstNodePtr;
-
 class Document : public Node
 {
 public:
-       Document ();
-       Document (std::string root_name);
-       Document (std::string root_name, boost::filesystem::path);
-
-       virtual ~Document ();
+       Document () {}
 
        void read_file (boost::filesystem::path);
        void read_stream (std::istream &);
        void read_string (std::string);
-       
-       std::string root_name () const {
-               return _root_name;
-       }
-              
+
+       void check_root_name (std::string root_name);
+
+       void write_to_file_formatted (boost::filesystem::path) const;
+       void write_to_string (std::string) const;
+
 private:
-       void take_root_node ();
-       
-       xmlpp::DomParser* _parser;
-       std::string _root_name;
+       void write_to_xmlpp_document (xmlpp::Document &) const;
 };
 
 }