7 #include <boost/shared_ptr.hpp>
8 #include <boost/optional.hpp>
9 #include <boost/lexical_cast.hpp>
10 #include <boost/algorithm/string/erase.hpp>
20 /** @brief An error */
21 class Error : public std::exception
24 /** Construct an Error exception.
25 * @param message Error message.
27 Error (std::string const & message) : _message (message) {}
29 /** Error destructor */
32 /** @return error message. Caller must not free the returned
35 char const * what () const throw () {
36 return _message.c_str ();
44 /** @brief A wrapper for a xmlpp::Node which simplifies parsing */
50 /** Construct a Node from an xmlpp::Node. This class will
51 * not destroy the xmlpp::Node.
52 * @param node xmlpp::Node.
54 Node (xmlpp::Node const * node);
56 std::string name () const;
58 /* A set of methods which look up a child of this node by
59 * its name, and return its contents as some type or other.
61 * If, for example, this object has been created with
62 * a node named "Fred", we might have the following XML:
68 * string_child ("Jim") would return "42"
69 * number_child<int64_t> ("Jim") would return 42.
72 * The methods not marked "optional" will throw an exception
73 * if the child node is not present. The "optional" methods
74 * will return an empty boost::optional<> in that case.
76 * All methods will also throw an exception if there is more
77 * than one of the specified child node.
80 std::string string_child (std::string c) const;
81 boost::optional<std::string> optional_string_child (std::string) const;
83 bool bool_child (std::string) const;
84 boost::optional<bool> optional_bool_child (std::string) const;
87 T number_child (std::string c) const
89 std::string s = string_child (c);
90 boost::erase_all (s, " ");
91 return boost::lexical_cast<T> (s);
95 boost::optional<T> optional_number_child (std::string c) const
97 boost::optional<std::string> s = optional_string_child (c);
99 return boost::optional<T> ();
102 std::string t = s.get ();
103 boost::erase_all (t, " ");
104 return boost::optional<T> (boost::lexical_cast<T> (t));
107 /** This will mark a child as to be ignored when calling done() */
108 void ignore_child (std::string) const;
110 /** Check whether all children of this Node have been looked up
111 * or passed to ignore_child(). If not, an exception is thrown.
115 /* These methods look for an attribute of this node, in the
116 * same way as the child methods do.
119 std::string string_attribute (std::string) const;
120 boost::optional<std::string> optional_string_attribute (std::string) const;
122 bool bool_attribute (std::string) const;
123 boost::optional<bool> optional_bool_attribute (std::string) const;
126 T number_attribute (std::string c) const
128 std::string s = string_attribute (c);
129 boost::erase_all (s, " ");
130 return boost::lexical_cast<T> (s);
134 boost::optional<T> optional_number_attribute (std::string c) const
136 boost::optional<std::string> s = optional_string_attribute (c);
138 return boost::optional<T> ();
141 std::string t = s.get ();
142 boost::erase_all (t, " ");
143 return boost::optional<T> (boost::lexical_cast<T> (t));
146 /** @return The content of this node */
147 std::string content () const;
149 boost::shared_ptr<Node> node_child (std::string) const;
150 boost::shared_ptr<Node> optional_node_child (std::string) const;
152 std::list<boost::shared_ptr<Node> > node_children (std::string) const;
155 xmlpp::Node const * _node;
158 mutable std::list<Glib::ustring> _taken;
161 class File : public Node
164 File (std::string file, std::string root_name);
168 xmlpp::DomParser* _parser;