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 */
42 /** @brief An error */
43 class Error : public std::exception
46 /** Construct an Error exception.
47 * @param message Error message.
49 Error (std::string const & message) : _message (message) {}
51 /** Error destructor */
54 /** @return error message. Caller must not free the returned
57 char const * what () const throw () {
58 return _message.c_str ();
67 typedef boost::shared_ptr<cxml::Node> NodePtr;
68 typedef std::list<NodePtr> NodeList;
69 typedef boost::shared_ptr<const cxml::Node> ConstNodePtr;
75 Node (xmlpp::Node const *);
77 /* A set of methods which look up a child of this node by
78 * its name, and return its contents as some type or other.
80 * If, for example, this object has been created with
81 * a node named "Fred", we might have the following XML:
87 * string_child ("Jim") would return "42"
88 * number_child<int64_t> ("Jim") would return 42.
91 * The methods not marked "optional" will throw an exception
92 * if the child node is not present. The "optional" methods
93 * will return an empty boost::optional<> in that case.
95 * All methods will also throw an exception if there is more
96 * than one of the specified child node.
99 std::string string_child (std::string c) const;
100 boost::optional<std::string> optional_string_child (std::string) const;
102 bool bool_child (std::string) const;
103 boost::optional<bool> optional_bool_child (std::string) const;
106 T number_child (std::string c) const
108 std::string s = string_child (c);
109 boost::erase_all (s, " ");
111 t.imbue (std::locale::classic ());
119 boost::optional<T> optional_number_child (std::string c) const
121 boost::optional<std::string> s = optional_string_child (c);
123 return boost::optional<T> ();
126 std::string t = s.get ();
127 boost::erase_all (t, " ");
129 u.imbue (std::locale::classic ());
137 /* These methods look for an attribute of this node, in the
138 * same way as the child methods do.
141 std::string string_attribute (std::string) const;
142 boost::optional<std::string> optional_string_attribute (std::string) const;
144 bool bool_attribute (std::string) const;
145 boost::optional<bool> optional_bool_attribute (std::string) const;
148 T number_attribute (std::string c) const
150 std::string s = string_attribute (c);
151 boost::erase_all (s, " ");
153 t.imbue (std::locale::classic ());
161 boost::optional<T> optional_number_attribute (std::string c) const
163 boost::optional<std::string> s = optional_string_attribute (c);
165 return boost::optional<T> ();
168 std::string t = s.get ();
169 boost::erase_all (t, " ");
171 u.imbue (std::locale::classic ());
179 /* Setting content */
181 void set_string_content (std::string content);
182 void set_bool_content (bool content);
185 void set_number_content (T content)
188 u.imbue (std::locale::classic ());
194 /* Short-cuts to add nodes with content */
196 NodePtr add_string (std::string name, std::string content);
197 NodePtr add_bool (std::string name, bool content);
200 NodePtr add_number (std::string name, T content)
202 NodePtr n = add (name);
203 n->set_number_content (content);
208 /* Access to child nodes */
210 boost::shared_ptr<Node> node_child (std::string) const;
211 boost::shared_ptr<Node> optional_node_child (std::string) const;
212 NodeList node_children (std::string) const;
214 /** Add a child node with a given name */
215 NodePtr add (std::string name)
217 NodePtr n (new cxml::Node ());
219 _children.push_back (n);
223 /** @return The content of this node */
224 std::string content () const;
225 /** @return namespace URI of this node */
226 std::string namespace_uri () const;
227 /** @return namespace prefix of this node */
228 std::string namespace_prefix () const;
229 /** This will mark a child as to be ignored when calling done() */
230 void ignore_child (std::string) const;
231 /** Check whether all children of this Node have been looked up
232 * or passed to ignore_child(). If not, an exception is thrown.
235 /** Set the name of the node.
238 void set_name (std::string n) {
241 /** @return Node name */
242 std::string name () const {
246 /* We use xmlpp for parsing and writing XML out; these
247 methods help with that.
249 void read (xmlpp::Node const *);
250 void write (xmlpp::Node *) const;
257 std::string _content;
258 std::string _namespace_uri;
259 std::string _namespace_prefix;
260 std::list<std::pair<std::string, std::string> > _attributes;
261 mutable std::list<std::string> _taken;
264 class Document : public Node
269 void read_file (boost::filesystem::path);
270 void read_stream (std::istream &);
271 void read_string (std::string);
273 void check_root_name (std::string root_name);
275 void write_to_file_formatted (boost::filesystem::path) const;
276 void write_to_string (std::string) const;
279 void write_to_xmlpp_document (xmlpp::Document &) const;