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.
36 #include <libxml/parser.h>
37 #include <libxml/tree.h>
38 #include <boost/shared_ptr.hpp>
40 #include "pbd/libpbd_visibility.h"
46 typedef std::list<XMLNode *> XMLNodeList;
47 typedef std::list<boost::shared_ptr<XMLNode> > XMLSharedNodeList;
48 typedef XMLNodeList::iterator XMLNodeIterator;
49 typedef XMLNodeList::const_iterator XMLNodeConstIterator;
50 typedef std::list<XMLProperty*> XMLPropertyList;
51 typedef XMLPropertyList::iterator XMLPropertyIterator;
52 typedef XMLPropertyList::const_iterator XMLPropertyConstIterator;
53 typedef std::map<std::string, XMLProperty*> XMLPropertyMap;
55 class LIBPBD_API XMLTree {
58 XMLTree(const std::string& fn, bool validate = false);
59 XMLTree(const XMLTree*);
62 XMLNode* root() const { return _root; }
63 XMLNode* set_root(XMLNode* n) { return _root = n; }
65 const std::string& filename() const { return _filename; }
66 const std::string& set_filename(const std::string& fn) { return _filename = fn; }
68 int compression() const { return _compression; }
69 int set_compression(int);
71 bool read() { return read_internal(false); }
72 bool read(const std::string& fn) { set_filename(fn); return read_internal(false); }
73 bool read_and_validate() { return read_internal(true); }
74 bool read_and_validate(const std::string& fn) { set_filename(fn); return read_internal(true); }
75 bool read_buffer(const std::string&);
78 bool write(const std::string& fn) { set_filename(fn); return write(); }
80 void debug (FILE*) const;
82 const std::string& write_buffer() const;
84 boost::shared_ptr<XMLSharedNodeList> find(const std::string xpath, XMLNode* = 0) const;
87 bool read_internal(bool validate);
89 std::string _filename;
95 class LIBPBD_API XMLNode {
97 XMLNode(const std::string& name);
98 XMLNode(const std::string& name, const std::string& content);
99 XMLNode(const XMLNode& other);
102 XMLNode& operator= (const XMLNode& other);
104 const std::string& name() const { return _name; }
106 bool is_content() const { return _is_content; }
107 const std::string& content() const { return _content; }
108 const std::string& set_content(const std::string&);
109 XMLNode* add_content(const std::string& s = std::string());
111 const XMLNodeList& children(const std::string& str = std::string()) const;
112 XMLNode* child(const char*) const;
113 XMLNode* add_child(const char *);
114 XMLNode* add_child_copy(const XMLNode&);
115 void add_child_nocopy(XMLNode&);
117 std::string attribute_value();
119 const XMLPropertyList& properties() const { return _proplist; }
120 XMLProperty* property(const char*);
121 XMLProperty* property(const std::string&);
122 const XMLProperty* property(const char* n) const { return const_cast<XMLNode*>(this)->property(n); }
123 const XMLProperty* property(const std::string& n) const { return const_cast<XMLNode*>(this)->property(n); }
125 XMLProperty* add_property(const char* name, const std::string& value);
126 XMLProperty* add_property(const char* name, const char* value = "");
127 XMLProperty* add_property(const char* name, const long value);
129 void remove_property(const std::string&);
130 void remove_property_recursively(const std::string&);
132 /** Remove all nodes with the name passed to remove_nodes */
133 void remove_nodes(const std::string&);
134 /** Remove and delete all nodes with the name passed to remove_nodes */
135 void remove_nodes_and_delete(const std::string&);
136 /** Remove and delete all nodes with property prop matching val */
137 void remove_nodes_and_delete(const std::string& propname, const std::string& val);
139 void dump (std::ostream &, std::string p = "") const;
144 std::string _content;
145 XMLNodeList _children;
146 XMLPropertyList _proplist;
147 XMLPropertyMap _propmap;
148 mutable XMLNodeList _selected_children;
153 class LIBPBD_API XMLProperty {
155 XMLProperty(const std::string& n, const std::string& v = std::string());
158 const std::string& name() const { return _name; }
159 const std::string& value() const { return _value; }
160 const std::string& set_value(const std::string& v) { return _value = v; }
167 class LIBPBD_API XMLException: public std::exception {
169 explicit XMLException(const std::string msg) : _message(msg) {}
170 virtual ~XMLException() throw() {}
172 virtual const char* what() const throw() { return _message.c_str(); }
175 std::string _message;