2 Copyright (C) 2012 Paul Davis
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.
24 * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and
25 * are covered by the GNU Lesser General Public License, which should be
26 * included with libxml++ as the file COPYING.
27 * Modified for Ardour and released under the same terms.
35 #include <libxml/parser.h>
36 #include <libxml/tree.h>
37 #include <boost/shared_ptr.hpp>
39 #include <glibmm/ustring.h>
41 #include "pbd/string_convert.h"
42 #include "pbd/libpbd_visibility.h"
47 class LIBPBD_API XMLProperty {
49 XMLProperty(const std::string& n, const std::string& v = std::string());
52 const std::string& name() const { return _name; }
53 const std::string& value() const { return _value; }
54 const std::string& set_value(const std::string& v) { return _value = v; }
61 typedef std::vector<XMLNode *> XMLNodeList;
62 typedef std::vector<boost::shared_ptr<XMLNode> > XMLSharedNodeList;
63 typedef XMLNodeList::iterator XMLNodeIterator;
64 typedef XMLNodeList::const_iterator XMLNodeConstIterator;
65 typedef std::vector<XMLProperty*> XMLPropertyList;
66 typedef XMLPropertyList::iterator XMLPropertyIterator;
67 typedef XMLPropertyList::const_iterator XMLPropertyConstIterator;
69 class LIBPBD_API XMLTree {
72 XMLTree(const std::string& fn, bool validate = false);
73 XMLTree(const XMLTree*);
76 XMLNode* root() const { return _root; }
77 XMLNode* set_root(XMLNode* n) { return _root = n; }
79 const std::string& filename() const { return _filename; }
80 const std::string& set_filename(const std::string& fn) { return _filename = fn; }
82 int compression() const { return _compression; }
83 int set_compression(int);
85 bool read() { return read_internal(false); }
86 bool read(const std::string& fn) { set_filename(fn); return read_internal(false); }
87 bool read_and_validate() { return read_internal(true); }
88 bool read_and_validate(const std::string& fn) { set_filename(fn); return read_internal(true); }
89 bool read_buffer(const std::string&, bool to_tree_doc = false);
92 bool write(const std::string& fn) { set_filename(fn); return write(); }
94 void debug (FILE*) const;
96 const std::string& write_buffer() const;
98 boost::shared_ptr<XMLSharedNodeList> find(const std::string xpath, XMLNode* = 0) const;
101 bool read_internal(bool validate);
103 std::string _filename;
109 class LIBPBD_API XMLNode {
111 XMLNode(const std::string& name);
112 XMLNode(const std::string& name, const std::string& content);
113 XMLNode(const XMLNode& other);
116 XMLNode& operator= (const XMLNode& other);
118 bool operator== (const XMLNode& other) const;
119 bool operator!= (const XMLNode& other) const;
121 const std::string& name() const { return _name; }
123 bool is_content() const { return _is_content; }
124 const std::string& content() const { return _content; }
125 const std::string& set_content(const std::string&);
126 XMLNode* add_content(const std::string& s = std::string());
128 const XMLNodeList& children(const std::string& str = std::string()) const;
129 XMLNode* child(const char*) const;
130 XMLNode* add_child(const char *);
131 XMLNode* add_child_copy(const XMLNode&);
132 void add_child_nocopy(XMLNode&);
134 std::string attribute_value();
136 const XMLPropertyList& properties() const { return _proplist; }
137 XMLProperty const * property(const char*) const;
138 XMLProperty const * property(const std::string&) const;
139 XMLProperty * property(const char*);
140 XMLProperty * property(const std::string&);
142 bool has_property_with_value (const std::string&, const std::string&) const;
144 XMLProperty* add_property(const char* name, const std::string& value);
145 XMLProperty* add_property(const char* name, const char* value = "");
146 XMLProperty* add_property(const char* name, const long value);
148 bool set_property (const char* name, const std::string& value);
150 bool set_property (const char* name, const char* cstr) {
151 return set_property (name, std::string(cstr));
154 bool set_property (const char* name, const Glib::ustring& ustr)
156 return set_property (name, ustr.raw ());
160 bool set_property (const char* name, const T& value)
163 if (!PBD::to_string<T> (value, str)) {
166 return set_property(name, str);
169 bool get_property (const char* name, std::string& value) const;
172 bool get_property (const char* name, T& value) const
174 XMLProperty const* const prop = property (name);
179 return PBD::string_to<T> (prop->value (), value);
182 void remove_property(const std::string&);
183 void remove_property_recursively(const std::string&);
185 /** Remove all nodes with the name passed to remove_nodes */
186 void remove_nodes(const std::string&);
187 /** Remove and delete all nodes with the name passed to remove_nodes */
188 void remove_nodes_and_delete(const std::string&);
189 /** Remove and delete all nodes with property prop matching val */
190 void remove_nodes_and_delete(const std::string& propname, const std::string& val);
192 void dump (std::ostream &, std::string p = "") const;
197 std::string _content;
198 XMLNodeList _children;
199 XMLPropertyList _proplist;
200 mutable XMLNodeList _selected_children;
205 class LIBPBD_API XMLException: public std::exception {
207 explicit XMLException(const std::string msg) : _message(msg) {}
208 virtual ~XMLException() throw() {}
210 virtual const char* what() const throw() { return _message.c_str(); }
213 std::string _message;