Fix GPL boilerplate. Add locked_sstream dependency. Remove
[libcxml.git] / src / cxml.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libcxml.
5
6     libcxml is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libcxml is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libcxml.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include <boost/filesystem.hpp>
22 #include <boost/algorithm/string.hpp>
23 #include <libxml++/libxml++.h>
24 #include "cxml.h"
25
26 using std::string;
27 using std::list;
28 using boost::shared_ptr;
29 using boost::optional;
30
31 cxml::Node::Node ()
32         : _node (0)
33 {
34
35 }
36
37 cxml::Node::Node (xmlpp::Node* node)
38         : _node (node)
39 {
40
41 }
42
43 string
44 cxml::Node::name () const
45 {
46         assert (_node);
47         return _node->get_name ();
48 }
49
50 shared_ptr<cxml::Node>
51 cxml::Node::node_child (string name) const
52 {
53         list<shared_ptr<cxml::Node> > n = node_children (name);
54         if (n.size() > 1) {
55                 throw cxml::Error ("duplicate XML tag " + name);
56         } else if (n.empty ()) {
57                 throw cxml::Error ("missing XML tag " + name + " in " + _node->get_name());
58         }
59
60         return n.front ();
61 }
62
63 shared_ptr<cxml::Node>
64 cxml::Node::optional_node_child (string name) const
65 {
66         list<shared_ptr<cxml::Node> > n = node_children (name);
67         if (n.size() > 1) {
68                 throw cxml::Error ("duplicate XML tag " + name);
69         } else if (n.empty ()) {
70                 return shared_ptr<cxml::Node> ();
71         }
72
73         return n.front ();
74 }
75
76 list<shared_ptr<cxml::Node> >
77 cxml::Node::node_children (string name) const
78 {
79         /* XXX: using find / get_path should work here, but I can't follow
80            how get_path works.
81         */
82
83         xmlpp::Node::NodeList c = _node->get_children ();
84
85         list<shared_ptr<cxml::Node> > n;
86         for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
87                 if ((*i)->get_name() == name) {
88                         n.push_back (shared_ptr<Node> (new Node (*i)));
89                 }
90         }
91
92         _taken.push_back (name);
93         return n;
94 }
95
96 string
97 cxml::Node::string_child (string c) const
98 {
99         return node_child(c)->content ();
100 }
101
102 optional<string>
103 cxml::Node::optional_string_child (string c) const
104 {
105         list<shared_ptr<Node> > nodes = node_children (c);
106         if (nodes.size() > 1) {
107                 throw cxml::Error ("duplicate XML tag " + c);
108         }
109
110         if (nodes.empty ()) {
111                 return optional<string> ();
112         }
113
114         return nodes.front()->content();
115 }
116
117 bool
118 cxml::Node::bool_child (string c) const
119 {
120         string const s = string_child (c);
121         return (s == "1" || s == "yes" || s == "True");
122 }
123
124 optional<bool>
125 cxml::Node::optional_bool_child (string c) const
126 {
127         optional<string> s = optional_string_child (c);
128         if (!s) {
129                 return optional<bool> ();
130         }
131
132         return (s.get() == "1" || s.get() == "yes" || s.get() == "True");
133 }
134
135 void
136 cxml::Node::ignore_child (string name) const
137 {
138         _taken.push_back (name);
139 }
140
141 string
142 cxml::Node::string_attribute (string name) const
143 {
144         xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
145         if (!e) {
146                 throw cxml::Error ("missing attribute " + name);
147         }
148
149         xmlpp::Attribute* a = e->get_attribute (name);
150         if (!a) {
151                 throw cxml::Error ("missing attribute " + name);
152         }
153
154         return a->get_value ();
155 }
156
157 optional<string>
158 cxml::Node::optional_string_attribute (string name) const
159 {
160         xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
161         if (!e) {
162                 return optional<string> ();
163         }
164
165         xmlpp::Attribute* a = e->get_attribute (name);
166         if (!a) {
167                 return optional<string> ();
168         }
169
170         return string (a->get_value ());
171 }
172
173 bool
174 cxml::Node::bool_attribute (string name) const
175 {
176         string const s = string_attribute (name);
177         return (s == "1" || s == "yes");
178 }
179
180 optional<bool>
181 cxml::Node::optional_bool_attribute (string name) const
182 {
183         optional<string> s = optional_string_attribute (name);
184         if (!s) {
185                 return optional<bool> ();
186         }
187
188         return (s.get() == "1" || s.get() == "yes");
189 }
190
191 void
192 cxml::Node::done () const
193 {
194         xmlpp::Node::NodeList c = _node->get_children ();
195         for (xmlpp::Node::NodeList::iterator i = c.begin(); i != c.end(); ++i) {
196                 if (dynamic_cast<xmlpp::Element *> (*i) && find (_taken.begin(), _taken.end(), (*i)->get_name()) == _taken.end ()) {
197                         throw cxml::Error ("unexpected XML node " + (*i)->get_name());
198                 }
199         }
200 }
201
202 string
203 cxml::Node::content () const
204 {
205         string content;
206
207         xmlpp::Node::NodeList c = _node->get_children ();
208         for (xmlpp::Node::NodeList::const_iterator i = c.begin(); i != c.end(); ++i) {
209                 xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (*i);
210                 if (v) {
211                         content += v->get_content ();
212                 }
213         }
214
215         return content;
216 }
217
218 string
219 cxml::Node::namespace_uri () const
220 {
221         return _node->get_namespace_uri ();
222 }
223
224 string
225 cxml::Node::namespace_prefix () const
226 {
227         return _node->get_namespace_prefix ();
228 }
229
230 cxml::Document::Document (string root_name)
231         : _root_name (root_name)
232 {
233         _parser = new xmlpp::DomParser;
234 }
235
236 cxml::Document::Document (string root_name, boost::filesystem::path file)
237         : _root_name (root_name)
238 {
239         _parser = new xmlpp::DomParser ();
240         read_file (file);
241 }
242
243 cxml::Document::Document ()
244 {
245         _parser = new xmlpp::DomParser ();
246 }
247
248 cxml::Document::~Document ()
249 {
250         delete _parser;
251 }
252
253 void
254 cxml::Document::read_file (boost::filesystem::path file)
255 {
256         if (!boost::filesystem::exists (file)) {
257                 throw cxml::Error ("XML file " + file.string() + " does not exist");
258         }
259
260         _parser->parse_file (file.string ());
261         take_root_node ();
262 }
263
264 void
265 cxml::Document::read_string (string s)
266 {
267         _parser->parse_memory (s);
268         take_root_node ();
269 }
270
271 void
272 cxml::Document::take_root_node ()
273 {
274         if (!_parser) {
275                 throw cxml::Error ("could not parse XML");
276         }
277
278         _node = _parser->get_document()->get_root_node ();
279         if (!_root_name.empty() && _node->get_name() != _root_name) {
280                 throw cxml::Error ("unrecognised root node " + _node->get_name() + " (expecting " + _root_name + ")");
281         } else if (_root_name.empty ()) {
282                 _root_name = _node->get_name ();
283         }
284 }