Various fixes and missing bits.
[libcxml.git] / src / cxml.h
index 8a7a82cb266b72089fd412a452e6713d0ea6be76..e358b9cdba3f54925b5b2afa5207abd6c3d6e90f 100644 (file)
@@ -1,18 +1,42 @@
+/*
+    Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
 #ifndef LIBCXML_CXML_H
 #define LIBCXML_CXML_H
 
 #include <string>
+#include <sstream>
 #include <list>
 #include <stdint.h>
 #include <boost/shared_ptr.hpp>
 #include <boost/optional.hpp>
-#include <boost/lexical_cast.hpp>
+#include <boost/filesystem.hpp>
 #include <boost/algorithm/string/erase.hpp>
-#include <glibmm.h>
+
+/* Hack for OS X compile failure; see https://bugs.launchpad.net/hugin/+bug/910160 */
+#ifdef check
+#undef check
+#endif
 
 namespace xmlpp {
        class Node;
-       class DomParser;
+       class Document;
 }
 
 namespace cxml {
@@ -41,19 +65,16 @@ private:
        std::string _message;
 };
 
-/** @brief A wrapper for a xmlpp::Node which simplifies parsing */
+class Node;    
+typedef boost::shared_ptr<cxml::Node> NodePtr;
+typedef std::list<NodePtr> NodeList;   
+typedef boost::shared_ptr<const cxml::Node> ConstNodePtr;
+
 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 const * 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.
@@ -88,7 +109,12 @@ public:
        {
                std::string s = string_child (c);
                boost::erase_all (s, " ");
-               return boost::lexical_cast<T> (s);
+               std::stringstream t;
+               t.imbue (std::locale::classic ());
+               t << s;
+               T n;
+               t >> n;
+               return n;
        }
 
        template <class T>
@@ -101,17 +127,15 @@ public:
 
                std::string t = s.get ();
                boost::erase_all (t, " ");
-               return boost::optional<T> (boost::lexical_cast<T> (t));
+               std::stringstream u;
+               u.imbue (std::locale::classic ());
+               u << t;
+               T n;
+               u >> n;
+               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.
         */
@@ -127,7 +151,12 @@ public:
        {
                std::string s = string_attribute (c);
                boost::erase_all (s, " ");
-               return boost::lexical_cast<T> (s);
+               std::stringstream t;
+               t.imbue (std::locale::classic ());
+               t << s;
+               T n;
+               t >> n;
+               return n;
        }
 
        template <class T>
@@ -140,35 +169,132 @@ public:
 
                std::string t = s.get ();
                boost::erase_all (t, " ");
-               return boost::optional<T> (boost::lexical_cast<T> (t));
+               std::stringstream u;
+               u.imbue (std::locale::classic ());
+               u << t;
+               T n;
+               u >> n;
+               return n;
        }
 
-       /** @return The content of this node */
-       std::string content () const;
 
-       /** @return namespace prefix of this node */
-       std::string namespace_prefix () const;
+       /* Setting content */
+
+       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;
+       }
+
+
+       /* Adding attributes */
+
+       void add_string_attribute (std::string name, std::string value);
+       void add_bool_attribute (std::string name, bool value);
+
+       template <class T>
+       void add_number_attribute (std::string name, T value)
+       {
+               std::stringstream u;
+               u.imbue (std::locale::classic ());
+               u << value;
+               add_string_attribute (name, u.str ());
+       }
+
+
+       /* Short-cuts to add nodes with content */
+
+       NodePtr add_string_child (std::string name, std::string content);
+       NodePtr add_bool_child (std::string name, bool content);
+
+       template <class T>
+       NodePtr add_number_child (std::string name, T content)
+       {
+               NodePtr n = add_child (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;
 
-       std::list<boost::shared_ptr<Node> > node_children (std::string) const;
+       /** Add a child node with a given name */
+       NodePtr add_child (std::string name)
+       {
+               NodePtr n (new cxml::Node ());
+               n->set_name (name);
+               _children.push_back (n);
+               return n;
+       }
        
-protected:
-       xmlpp::Node const * _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;
 };
 
-class File : public Node
+class Document : public Node
 {
 public:
-       File (std::string file, std::string root_name);
-       virtual ~File ();
+       Document () {}
+
+       void read_file (boost::filesystem::path);
+       void read_stream (std::istream &);
+       void read_string (std::string);
+
+       void check_root_name (std::string root_name);
+
+       void write_to_file_formatted (boost::filesystem::path) const;
+       void write_to_stream_formatted (std::ostream& stream, std::string coding) const;
+       std::string write_to_string (std::string) const;
 
 private:
-       xmlpp::DomParser* _parser;
+       void write_to_xmlpp_document (xmlpp::Document &) const;
 };
 
 }