Better error.
[libcxml.git] / src / cxml.cc
index 2a755bf8751afed7131bc93f830ddb0b5316e6d3..1d98529bdd0b08f49a08140355a085c79024eb94 100644 (file)
@@ -1,6 +1,24 @@
+/*
+    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.
+
+*/
+
 #include <sstream>
 #include <iostream>
-#include <boost/lexical_cast.hpp>
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
 #include <libxml++/libxml++.h>
@@ -15,14 +33,21 @@ cxml::Node::Node ()
 
 }
 
-cxml::Node::Node (xmlpp::Node const * node)
+cxml::Node::Node (xmlpp::Node* node)
        : _node (node)
 {
 
 }
 
+string
+cxml::Node::name () const
+{
+       assert (_node);
+       return _node->get_name ();
+}
+
 shared_ptr<cxml::Node>
-cxml::Node::node_child (string name)
+cxml::Node::node_child (string name) const
 {
        list<shared_ptr<cxml::Node> > n = node_children (name);
        if (n.size() > 1) {
@@ -34,8 +59,21 @@ cxml::Node::node_child (string name)
        return n.front ();
 }
 
+shared_ptr<cxml::Node>
+cxml::Node::optional_node_child (string name) const
+{
+       list<shared_ptr<cxml::Node> > n = node_children (name);
+       if (n.size() > 1) {
+               throw cxml::Error ("duplicate XML tag " + name);
+       } else if (n.empty ()) {
+               return shared_ptr<cxml::Node> ();
+       }
+       
+       return n.front ();
+}
+
 list<shared_ptr<cxml::Node> >
-cxml::Node::node_children (string name)
+cxml::Node::node_children (string name) const
 {
        /* XXX: using find / get_path should work here, but I can't follow
           how get_path works.
@@ -55,13 +93,13 @@ cxml::Node::node_children (string name)
 }
 
 string
-cxml::Node::string_child (string c)
+cxml::Node::string_child (string c) const
 {
        return node_child(c)->content ();
 }
 
 optional<string>
-cxml::Node::optional_string_child (string c)
+cxml::Node::optional_string_child (string c) const
 {
        list<shared_ptr<Node> > nodes = node_children (c);
        if (nodes.size() > 1) {
@@ -76,14 +114,14 @@ cxml::Node::optional_string_child (string c)
 }
 
 bool
-cxml::Node::bool_child (string c)
+cxml::Node::bool_child (string c) const
 {
        string const s = string_child (c);
        return (s == "1" || s == "yes");
 }
 
 optional<bool>
-cxml::Node::optional_bool_child (string c)
+cxml::Node::optional_bool_child (string c) const
 {
        optional<string> s = optional_string_child (c);
        if (!s) {
@@ -94,29 +132,29 @@ cxml::Node::optional_bool_child (string c)
 }
 
 void
-cxml::Node::ignore_child (string name)
+cxml::Node::ignore_child (string name) const
 {
        _taken.push_back (name);
 }
 
 string
-cxml::Node::string_attribute (string name)
+cxml::Node::string_attribute (string name) const
 {
        xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
        if (!e) {
-               throw cxml::Error ("missing attribute");
+               throw cxml::Error ("missing attribute " + name);
        }
        
        xmlpp::Attribute* a = e->get_attribute (name);
        if (!a) {
-               throw cxml::Error ("missing attribute");
+               throw cxml::Error ("missing attribute " + name);
        }
 
        return a->get_value ();
 }
 
 optional<string>
-cxml::Node::optional_string_attribute (string name)
+cxml::Node::optional_string_attribute (string name) const
 {
        xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
        if (!e) {
@@ -132,14 +170,14 @@ cxml::Node::optional_string_attribute (string name)
 }
 
 bool
-cxml::Node::bool_attribute (string name)
+cxml::Node::bool_attribute (string name) const
 {
        string const s = string_attribute (name);
        return (s == "1" || s == "yes");
 }
 
 optional<bool>
-cxml::Node::optional_bool_attribute (string name)
+cxml::Node::optional_bool_attribute (string name) const
 {
        optional<string> s = optional_string_attribute (name);
        if (!s) {
@@ -150,7 +188,7 @@ cxml::Node::optional_bool_attribute (string name)
 }
 
 void
-cxml::Node::done ()
+cxml::Node::done () const
 {
        xmlpp::Node::NodeList c = _node->get_children ();
        for (xmlpp::Node::NodeList::iterator i = c.begin(); i != c.end(); ++i) {
@@ -161,7 +199,7 @@ cxml::Node::done ()
 }
 
 string
-cxml::Node::content ()
+cxml::Node::content () const
 {
        string content;
        
@@ -176,25 +214,79 @@ cxml::Node::content ()
        return content;
 }
 
-cxml::File::File (string file, string root_name)
+string
+cxml::Node::namespace_uri () const
+{
+       return _node->get_namespace_uri ();
+}
+
+string
+cxml::Node::namespace_prefix () const
+{
+       return _node->get_namespace_prefix ();
+}
+
+cxml::Document::Document (string root_name)
+       : _root_name (root_name)
+{
+       _parser = new xmlpp::DomParser;
+}
+
+cxml::Document::Document (string root_name, boost::filesystem::path file)
+       : _root_name (root_name)
+{
+       _parser = new xmlpp::DomParser ();
+       read_file (file);
+}
+
+cxml::Document::Document ()
+{
+       _parser = new xmlpp::DomParser ();
+}
+
+cxml::Document::~Document ()
+{
+       delete _parser;
+}
+
+void
+cxml::Document::read_file (filesystem::path file)
 {
        if (!filesystem::exists (file)) {
-               throw cxml::Error ("XML file does not exist");
+               throw cxml::Error ("XML file " + file.string() + " does not exist");
        }
        
-       _parser = new xmlpp::DomParser;
-       _parser->parse_file (file);
+       _parser->parse_file (file.string ());
+       take_root_node ();
+}
+
+void
+cxml::Document::read_stream (istream& stream)
+{
+       _parser->parse_stream (stream);
+       take_root_node ();
+}
+
+void
+cxml::Document::read_string (string s)
+{
+       stringstream t (s);
+       _parser->parse_stream (t);
+       take_root_node ();
+}
+
+void
+cxml::Document::take_root_node ()
+{
        if (!_parser) {
                throw cxml::Error ("could not parse XML");
        }
 
        _node = _parser->get_document()->get_root_node ();
-       if (_node->get_name() != root_name) {
-               throw cxml::Error ("unrecognised root node");
+       if (!_root_name.empty() && _node->get_name() != _root_name) {
+               throw cxml::Error ("unrecognised root node " + _node->get_name() + "(expecting " + _root_name + ")");
+       } else if (_root_name.empty ()) {
+               _root_name = _node->get_name ();
        }
 }
 
-cxml::File::~File ()
-{
-       delete _parser;
-}