Missing #include.
[libcxml.git] / src / cxml.cc
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 #include <sstream>
21 #include <iostream>
22 #include <boost/filesystem.hpp>
23 #include <boost/algorithm/string.hpp>
24 #include <libxml++/libxml++.h>
25 #include "cxml.h"
26
27 using std::pair;
28 using std::list;
29 using std::istream;
30 using std::string;
31 using std::make_pair;
32 using std::map;
33 using std::stringstream;
34 using std::ostream;
35 using boost::shared_ptr;
36 using boost::optional;
37
38 cxml::Node::Node ()
39 {
40
41 }
42
43 cxml::Node::Node (xmlpp::Node const * node)
44 {
45         read (node);
46 }
47
48 void
49 cxml::Node::read (xmlpp::Node const * node)
50 {
51         _name = node->get_name ();
52         _namespace_prefix = node->get_namespace_prefix ();
53         
54         xmlpp::Node::NodeList content = node->get_children ();
55         for (xmlpp::Node::NodeList::const_iterator i = content.begin(); i != content.end(); ++i) {
56                 xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (*i);
57                 if (v) {
58                         _content += v->get_content ();
59                 } else {
60                         _children.push_back (cxml::NodePtr (new cxml::Node (*i)));
61                 }
62         }
63
64         xmlpp::Element const * element = dynamic_cast<xmlpp::Element const *> (node);
65         if (element) {
66                 xmlpp::Element::AttributeList attributes = element->get_attributes ();
67                 for (list<xmlpp::Attribute*>::const_iterator i = attributes.begin(); i != attributes.end(); ++i) {
68                         _attributes.push_back (make_pair ((*i)->get_name(), (*i)->get_value ()));
69                 }
70         }
71 }
72
73 void
74 cxml::Node::write (xmlpp::Node* parent, map<cxml::ConstNodePtr, xmlpp::Node*>* mapping) const
75 {
76         xmlpp::Element* node = parent->add_child (_name, _namespace_prefix);
77         if (mapping) {
78                 (*mapping)[shared_from_this()] = node;
79         }
80
81         if (!_content.empty ()) {
82                 node->add_child_text (_content);
83         }
84
85         for (KeyValueList::const_iterator i = _namespace_declarations.begin(); i != _namespace_declarations.end(); ++i) {
86                 node->set_namespace_declaration (i->first, i->second);
87         }
88         
89         for (KeyValueList::const_iterator i = _attributes.begin(); i != _attributes.end(); ++i) {
90                 node->set_attribute (i->first, i->second);
91         }
92
93         for (NodeList::const_iterator i = _children.begin(); i != _children.end(); ++i) {
94                 (*i)->write (node);
95         }
96 }
97
98 cxml::NodePtr
99 cxml::Node::child (string name) const
100 {
101         NodeList n = children (name);
102         if (n.size() > 1) {
103                 throw cxml::Error ("duplicate XML tag " + name);
104         } else if (n.empty ()) {
105                 throw cxml::Error ("missing XML tag " + name + " in " + _name);
106         }
107         
108         return n.front ();
109 }
110
111 cxml::NodePtr
112 cxml::Node::optional_child (string name) const
113 {
114         NodeList n = children (name);
115         if (n.size() > 1) {
116                 throw cxml::Error ("duplicate XML tag " + name);
117         } else if (n.empty ()) {
118                 return NodePtr ();
119         }
120         
121         return n.front ();
122 }
123
124 cxml::NodeList
125 cxml::Node::children () const
126 {
127         return _children;
128 }
129
130 cxml::NodeList
131 cxml::Node::children (string name) const
132 {
133         NodeList n;
134         
135         for (NodeList::const_iterator i = _children.begin(); i != _children.end(); ++i) {
136                 if ((*i)->name() == name) {
137                         n.push_back (*i);
138                 }
139         }
140
141         _taken.push_back (name);
142         return n;
143 }
144
145 string
146 cxml::Node::string_child (string c) const
147 {
148         return child(c)->content ();
149 }
150
151 optional<string>
152 cxml::Node::optional_string_child (string c) const
153 {
154         NodeList nodes = children (c);
155         if (nodes.size() > 1) {
156                 throw cxml::Error ("duplicate XML tag " + c);
157         }
158
159         if (nodes.empty ()) {
160                 return optional<string> ();
161         }
162
163         return nodes.front()->content();
164 }
165
166 bool
167 cxml::Node::bool_child (string c) const
168 {
169         string const s = string_child (c);
170         return (s == "1" || s == "yes");
171 }
172
173 optional<bool>
174 cxml::Node::optional_bool_child (string c) const
175 {
176         optional<string> s = optional_string_child (c);
177         if (!s) {
178                 return optional<bool> ();
179         }
180         
181         return (s.get() == "1" || s.get() == "yes");
182 }
183
184 void
185 cxml::Node::ignore_child (string name) const
186 {
187         _taken.push_back (name);
188 }
189
190 string
191 cxml::Node::string_attribute (string name) const
192 {
193         KeyValueList::const_iterator i = _attributes.begin ();
194         while (i != _attributes.end() && i->first != name) {
195                 ++i;
196         }
197         
198         if (i == _attributes.end ()) {
199                 throw cxml::Error ("missing attribute");
200         }
201
202         return i->second;
203 }
204
205 optional<string>
206 cxml::Node::optional_string_attribute (string name) const
207 {
208         KeyValueList::const_iterator i;
209         while (i != _attributes.end() && i->first != name) {
210                 ++i;
211         }
212         
213         if (i == _attributes.end ()) {
214                 return optional<string> ();
215         }
216
217         return i->second;
218 }
219
220 bool
221 cxml::Node::bool_attribute (string name) const
222 {
223         string const s = string_attribute (name);
224         return (s == "1" || s == "yes");
225 }
226
227 optional<bool>
228 cxml::Node::optional_bool_attribute (string name) const
229 {
230         optional<string> s = optional_string_attribute (name);
231         if (!s) {
232                 return optional<bool> ();
233         }
234
235         return (s.get() == "1" || s.get() == "yes");
236 }
237
238 void
239 cxml::Node::done () const
240 {
241         for (NodeList::const_iterator i = _children.begin(); i != _children.end(); ++i) {
242                 if (find (_taken.begin(), _taken.end(), (*i)->name()) == _taken.end ()) {
243                         throw cxml::Error ("unexpected XML node " + (*i)->name());
244                 }
245         }
246 }
247
248 string
249 cxml::Node::content () const
250 {
251         return _content;
252 }
253
254 string
255 cxml::Node::namespace_prefix () const
256 {
257         return _namespace_prefix;
258 }
259
260 void
261 cxml::Node::set_attribute (string name, string value)
262 {
263         KeyValueList::iterator i = _attributes.begin ();
264         while (i != _attributes.end() && i->first != name) {
265                 ++i;
266         }
267
268         if (i != _attributes.end ()) {
269                 _attributes.erase (i);
270         }
271                 
272         _attributes.push_back (make_pair (name, value));
273 }
274
275 void
276 cxml::Node::set_namespace_declaration (string uri, string ns)
277 {
278         KeyValueList::iterator i = _namespace_declarations.begin ();
279         while (i != _namespace_declarations.end() && i->second != ns) {
280                 ++i;
281         }
282
283         if (i != _namespace_declarations.end ()) {
284                 _namespace_declarations.erase (i);
285         }
286
287         _namespace_declarations.push_back (make_pair (uri, ns));
288 }
289
290 cxml::NodePtr
291 cxml::read_file (boost::filesystem::path file)
292 {
293         if (!boost::filesystem::exists (file)) {
294                 throw cxml::Error ("XML file does not exist");
295         }
296
297         xmlpp::DomParser parser;
298         parser.parse_file (file.string ());
299         cxml::NodePtr node (new cxml::Node);
300         node->read (parser.get_document()->get_root_node ());
301         return node;
302 }
303
304 cxml::NodePtr
305 cxml::read_stream (istream& stream)
306 {
307         xmlpp::DomParser parser;
308         parser.parse_stream (stream);
309         cxml::NodePtr node (new cxml::Node);
310         node->read (parser.get_document()->get_root_node ());
311         return node;
312 }
313
314 cxml::NodePtr
315 cxml::read_string (string s)
316 {
317         xmlpp::DomParser parser;
318         stringstream t (s);
319         parser.parse_stream (t);
320         cxml::NodePtr node (new cxml::Node);
321         node->read (parser.get_document()->get_root_node ());
322         return node;
323 }
324
325 void
326 cxml::write_to_xmlpp_document (cxml::ConstNodePtr node, xmlpp::Document& doc, map<cxml::ConstNodePtr, xmlpp::Node*>* mapping)
327 {
328         xmlpp::Element* root = doc.create_root_node (node->name ());
329         if (mapping) {
330                 (*mapping)[node] = root;
331         }
332
333         cxml::KeyValueList namespace_declarations = node->namespace_declarations ();
334         for (cxml::KeyValueList::const_iterator i = namespace_declarations.begin(); i != namespace_declarations.end(); ++i) {
335                 root->set_namespace_declaration (i->first, i->second);
336         }
337
338         cxml::NodeList children = node->children ();
339         for (cxml::NodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
340                 (*i)->write (root, mapping);
341         }
342 }
343
344 void
345 cxml::write_to_file (cxml::ConstNodePtr node, boost::filesystem::path path)
346 {
347         xmlpp::Document doc;
348         write_to_xmlpp_document (node, doc);
349         doc.write_to_file (path.string ());
350 }
351
352 void
353 cxml::write_to_file_formatted (cxml::ConstNodePtr node, boost::filesystem::path path)
354 {
355         xmlpp::Document doc;
356         write_to_xmlpp_document (node, doc);
357         doc.write_to_file_formatted (path.string ());
358 }
359
360 void
361 cxml::write_to_stream_formatted (cxml::ConstNodePtr node, ostream& stream)
362 {
363         xmlpp::Document doc;
364         write_to_xmlpp_document (node, doc);
365         doc.write_to_stream_formatted (stream);
366 }
367
368 string
369 cxml::write_to_string (cxml::ConstNodePtr node)
370 {
371         xmlpp::Document doc;
372         write_to_xmlpp_document (node, doc);
373         return doc.write_to_string ();
374 }
375
376 string
377 cxml::write_to_string_formatted (cxml::ConstNodePtr node)
378 {
379         xmlpp::Document doc;
380         write_to_xmlpp_document (node, doc);
381         return doc.write_to_string_formatted ();
382 }
383