2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef LIBCXML_CXML_H
21 #define LIBCXML_CXML_H
26 #include <boost/shared_ptr.hpp>
27 #include <boost/optional.hpp>
28 #include <boost/filesystem.hpp>
29 #include <boost/algorithm/string/erase.hpp>
31 /* Hack for OS X compile failure; see https://bugs.launchpad.net/hugin/+bug/910160 */
43 /** @brief An error */
44 class Error : public std::exception
47 /** Construct an Error exception.
48 * @param message Error message.
50 Error (std::string const & message) : _message (message) {}
52 /** Error destructor */
55 /** @return error message. Caller must not free the returned
58 char const * what () const throw () {
59 return _message.c_str ();
67 /** @brief A wrapper for a xmlpp::Node which simplifies parsing */
73 /** Construct a Node from an xmlpp::Node. This class will
74 * not destroy the xmlpp::Node.
75 * @param node xmlpp::Node.
77 Node (xmlpp::Node* node);
79 std::string name () const;
81 /* A set of methods which look up a child of this node by
82 * its name, and return its contents as some type or other.
84 * If, for example, this object has been created with
85 * a node named "Fred", we might have the following XML:
91 * string_child ("Jim") would return "42"
92 * number_child<int64_t> ("Jim") would return 42.
95 * The methods not marked "optional" will throw an exception
96 * if the child node is not present. The "optional" methods
97 * will return an empty boost::optional<> in that case.
99 * All methods will also throw an exception if there is more
100 * than one of the specified child node.
103 std::string string_child (std::string c) const;
104 boost::optional<std::string> optional_string_child (std::string) const;
106 bool bool_child (std::string) const;
107 boost::optional<bool> optional_bool_child (std::string) const;
110 T number_child (std::string c) const
112 std::string s = string_child (c);
113 boost::erase_all (s, " ");
115 t.imbue (std::locale::classic ());
123 boost::optional<T> optional_number_child (std::string c) const
125 boost::optional<std::string> s = optional_string_child (c);
127 return boost::optional<T> ();
130 std::string t = s.get ();
131 boost::erase_all (t, " ");
133 u.imbue (std::locale::classic ());
140 /** This will mark a child as to be ignored when calling done() */
141 void ignore_child (std::string) const;
143 /** Check whether all children of this Node have been looked up
144 * or passed to ignore_child(). If not, an exception is thrown.
148 /* These methods look for an attribute of this node, in the
149 * same way as the child methods do.
152 std::string string_attribute (std::string) const;
153 boost::optional<std::string> optional_string_attribute (std::string) const;
155 bool bool_attribute (std::string) const;
156 boost::optional<bool> optional_bool_attribute (std::string) const;
159 T number_attribute (std::string c) const
161 std::string s = string_attribute (c);
162 boost::erase_all (s, " ");
164 t.imbue (std::locale::classic ());
172 boost::optional<T> optional_number_attribute (std::string c) const
174 boost::optional<std::string> s = optional_string_attribute (c);
176 return boost::optional<T> ();
179 std::string t = s.get ();
180 boost::erase_all (t, " ");
182 u.imbue (std::locale::classic ());
189 /** @return The content of this node */
190 std::string content () const;
192 /** @return namespace URI of this node */
193 std::string namespace_uri () const;
195 /** @return namespace prefix of this node */
196 std::string namespace_prefix () const;
198 boost::shared_ptr<Node> node_child (std::string) const;
199 boost::shared_ptr<Node> optional_node_child (std::string) const;
201 std::list<boost::shared_ptr<Node> > node_children (std::string) const;
203 xmlpp::Node* node () const {
211 mutable std::list<std::string> _taken;
214 typedef boost::shared_ptr<cxml::Node> NodePtr;
215 typedef boost::shared_ptr<const cxml::Node> ConstNodePtr;
217 class Document : public Node
221 Document (std::string root_name);
222 Document (std::string root_name, boost::filesystem::path);
224 virtual ~Document ();
226 void read_file (boost::filesystem::path);
227 void read_stream (std::istream &);
228 void read_string (std::string);
230 std::string root_name () const {
235 void take_root_node ();
237 xmlpp::DomParser* _parser;
238 std::string _root_name;