Don't use xmlpp as the data storage; put things in cxml::Node members
[libcxml.git] / src / cxml.h
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #ifndef LIBCXML_CXML_H
21 #define LIBCXML_CXML_H
22
23 #include <string>
24 #include <list>
25 #include <stdint.h>
26 #include <boost/shared_ptr.hpp>
27 #include <boost/optional.hpp>
28 #include <boost/filesystem.hpp>
29 #include <boost/algorithm/string/erase.hpp>
30
31 /* Hack for OS X compile failure; see https://bugs.launchpad.net/hugin/+bug/910160 */
32 #ifdef check
33 #undef check
34 #endif
35
36 namespace xmlpp {
37         class Node;
38 }
39
40 namespace cxml {
41
42 /** @brief An error */
43 class Error : public std::exception
44 {
45 public:
46         /** Construct an Error exception.
47          *  @param message Error message.
48          */
49         Error (std::string const & message) : _message (message) {}
50
51         /** Error destructor */
52         ~Error () throw () {}
53
54         /** @return error message.  Caller must not free the returned
55          *  value.
56          */
57         char const * what () const throw () {
58                 return _message.c_str ();
59         }
60
61 private:
62         /** error message */
63         std::string _message;
64 };
65
66 class Node;     
67 typedef boost::shared_ptr<cxml::Node> NodePtr;
68 typedef std::list<NodePtr> NodeList;    
69 typedef boost::shared_ptr<const cxml::Node> ConstNodePtr;
70
71 class Node
72 {
73 public:
74         Node ();
75         Node (xmlpp::Node const *);
76
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.
79          *
80          * If, for example, this object has been created with
81          * a node named "Fred", we might have the following XML:
82          *
83          * <Fred>
84          *   <Jim>42</Jim>
85          * </Fred>
86          *
87          * string_child ("Jim") would return "42"
88          * number_child<int64_t> ("Jim") would return 42.
89          * ...and so on.
90          *
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.
94          *
95          * All methods will also throw an exception if there is more
96          * than one of the specified child node.
97          */
98
99         std::string string_child (std::string c) const;
100         boost::optional<std::string> optional_string_child (std::string) const;
101
102         bool bool_child (std::string) const;
103         boost::optional<bool> optional_bool_child (std::string) const;
104
105         template <class T>
106         T number_child (std::string c) const
107         {
108                 std::string s = string_child (c);
109                 boost::erase_all (s, " ");
110                 std::stringstream t;
111                 t.imbue (std::locale::classic ());
112                 t << s;
113                 T n;
114                 t >> n;
115                 return n;
116         }
117
118         template <class T>
119         boost::optional<T> optional_number_child (std::string c) const
120         {
121                 boost::optional<std::string> s = optional_string_child (c);
122                 if (!s) {
123                         return boost::optional<T> ();
124                 }
125
126                 std::string t = s.get ();
127                 boost::erase_all (t, " ");
128                 std::stringstream u;
129                 u.imbue (std::locale::classic ());
130                 u << t;
131                 T n;
132                 u >> n;
133                 return n;
134         }
135                 
136         
137         /* These methods look for an attribute of this node, in the
138          * same way as the child methods do.
139          */
140
141         std::string string_attribute (std::string) const;
142         boost::optional<std::string> optional_string_attribute (std::string) const;
143
144         bool bool_attribute (std::string) const;
145         boost::optional<bool> optional_bool_attribute (std::string) const;
146
147         template <class T>
148         T number_attribute (std::string c) const
149         {
150                 std::string s = string_attribute (c);
151                 boost::erase_all (s, " ");
152                 std::stringstream t;
153                 t.imbue (std::locale::classic ());
154                 t << s;
155                 T n;
156                 t >> n;
157                 return n;
158         }
159
160         template <class T>
161         boost::optional<T> optional_number_attribute (std::string c) const
162         {
163                 boost::optional<std::string> s = optional_string_attribute (c);
164                 if (!s) {
165                         return boost::optional<T> ();
166                 }
167
168                 std::string t = s.get ();
169                 boost::erase_all (t, " ");
170                 std::stringstream u;
171                 u.imbue (std::locale::classic ());
172                 u << t;
173                 T n;
174                 u >> n;
175                 return n;
176         }
177
178
179         /* Setting content */
180
181         void set_string_content (std::string content);
182         void set_bool_content (bool content);
183
184         template <class T>
185         void set_number_content (T content)
186         {
187                 std::stringstream u;
188                 u.imbue (std::locale::classic ());
189                 u << content;
190                 u >> _content;
191         }
192
193
194         /* Short-cuts to add nodes with content */
195
196         NodePtr add_string (std::string name, std::string content);
197         NodePtr add_bool (std::string name, bool content);
198
199         template <class T>
200         NodePtr add_number (std::string name, T content)
201         {
202                 NodePtr n = add (name);
203                 n->set_number_content (content);
204                 return n;
205         }
206
207         
208         /* Access to child nodes */
209
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;
213
214         /** Add a child node with a given name */
215         NodePtr add (std::string name)
216         {
217                 NodePtr n (new cxml::Node ());
218                 n->set_name (name);
219                 _children.push_back (n);
220                 return n;
221         }
222         
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.
233          */
234         void done () const;
235         /** Set the name of the node.
236          *  @param n New name.
237          */
238         void set_name (std::string n) {
239                 _name = n;
240         }
241         /** @return Node name */
242         std::string name () const {
243                 return _name;
244         }
245
246         /* We use xmlpp for parsing and writing XML out; these
247            methods help with that.
248         */
249         void read (xmlpp::Node const *);
250         void write (xmlpp::Node *) const;
251
252 protected:      
253         NodeList _children;
254         
255 private:
256         std::string _name;
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;
262 };
263
264 class Document : public Node
265 {
266 public:
267         Document () {}
268
269         void read_file (boost::filesystem::path);
270         void read_stream (std::istream &);
271         void read_string (std::string);
272
273         void check_root_name (std::string root_name);
274
275         void write_to_file_formatted (boost::filesystem::path) const;
276         void write_to_string (std::string) const;
277
278 private:
279         void write_to_xmlpp_document (xmlpp::Document &) const;
280 };
281
282 }
283
284 #endif