9427c7070c8f5f278f2083970c96050c06433c72
[ardour.git] / libs / pbd / pbd / xml++.h
1 /* xml++.h
2  * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and
3  * are covered by the GNU Lesser General Public License, which should be
4  * included with libxml++ as the file COPYING.
5  * Modified for Ardour and released under the same terms.
6  */
7
8 #include <string>
9 #include <list>
10 #include <map>
11 #include <cstdio>
12 #include <cstdarg>
13
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
16 #include <boost/shared_ptr.hpp>
17
18 #ifndef __XML_H
19 #define __XML_H
20
21 using std::string;
22 using std::map;
23 using std::list;
24
25 class XMLTree;
26 class XMLNode;
27 class XMLProperty;
28
29 typedef list<XMLNode *>                   XMLNodeList;
30 typedef list<boost::shared_ptr<XMLNode> > XMLSharedNodeList;
31 typedef XMLNodeList::iterator             XMLNodeIterator;
32 typedef XMLNodeList::const_iterator       XMLNodeConstIterator;
33 typedef list<XMLProperty*>                XMLPropertyList;
34 typedef XMLPropertyList::iterator         XMLPropertyIterator;
35 typedef XMLPropertyList::const_iterator   XMLPropertyConstIterator;
36 typedef map<string, XMLProperty*>         XMLPropertyMap;
37
38 class XMLTree {
39 public:
40         XMLTree();
41         XMLTree(const string& fn, bool validate = false);
42         XMLTree(const XMLTree*);
43         ~XMLTree();
44
45         XMLNode* root() const         { return _root; }
46         XMLNode* set_root(XMLNode* n) { return _root = n; }
47
48         const string& filename() const               { return _filename; }
49         const string& set_filename(const string& fn) { return _filename = fn; }
50
51         int compression() const { return _compression; }
52         int set_compression(int);
53
54         bool read() { return read_internal(false); }
55         bool read(const string& fn) { set_filename(fn); return read_internal(false); }
56         bool read_and_validate() { return read_internal(true); }
57         bool read_and_validate(const string& fn) { set_filename(fn); return read_internal(true); }
58         bool read_buffer(const string&);
59
60         bool write() const;
61         bool write(const string& fn) { set_filename(fn); return write(); }
62
63         void debug (FILE*) const;
64
65         const string& write_buffer() const;
66
67 private:
68         bool read_internal(bool validate);
69         
70         string   _filename;
71         XMLNode* _root;
72         int      _compression;
73 };
74
75 class XMLNode {
76 public:
77         XMLNode(const string& name);
78         XMLNode(const string& name, const string& content);
79         XMLNode(const XMLNode& other);
80         ~XMLNode();
81
82         const string& name() const { return _name; }
83
84         bool          is_content() const { return _is_content; }
85         const string& content()    const { return _content; }
86         const string& set_content(const string&);
87         XMLNode*      add_content(const string& s = string());
88
89         const XMLNodeList& children(const string& str = string()) const;
90         XMLNode* child(const char*) const;
91         XMLNode* add_child(const char *);
92         XMLNode* add_child_copy(const XMLNode&);
93         void     add_child_nocopy(XMLNode&);
94
95         boost::shared_ptr<XMLSharedNodeList> find(const std::string xpath) const;
96         std::string attribute_value();
97
98         const XMLPropertyList& properties() const { return _proplist; }
99         XMLProperty*       property(const char*);
100         XMLProperty*       property(const string&);
101         const XMLProperty* property(const char* n)   const { return ((XMLNode*)this)->property(n); }
102         const XMLProperty* property(const string& n) const { return ((XMLNode*)this)->property(n); }
103         
104         XMLProperty* add_property(const char* name, const string& value);
105         XMLProperty* add_property(const char* name, const char* value = "");
106         XMLProperty* add_property(const char* name, const long value);
107
108         void remove_property(const string&);
109
110         /** Remove all nodes with the name passed to remove_nodes */
111         void remove_nodes(const string&);
112         /** Remove and delete all nodes with the name passed to remove_nodes */
113         void remove_nodes_and_delete(const string&);
114         /** Remove and delete all nodes with property prop matching val */
115         void remove_nodes_and_delete(const string& propname, const string& val);
116
117 private:
118         string              _name;
119         bool                _is_content;
120         string              _content;
121         XMLNodeList         _children;
122         XMLPropertyList     _proplist;
123         XMLPropertyMap      _propmap;
124         mutable XMLNodeList _selected_children;
125 };
126
127 class XMLProperty {
128 public:
129         XMLProperty(const string& n, const string& v = string());
130         ~XMLProperty();
131
132         const string& name() const { return _name; }
133         const string& value() const { return _value; }
134         const string& set_value(const string& v) { return _value = v; }
135
136 private:
137         string _name;
138         string _value;
139 };
140
141 class XMLException: public std::exception {
142 public:
143         explicit XMLException(const string msg) : _message(msg) {}
144         virtual ~XMLException() throw() {}
145
146         virtual const char* what() const throw() { return _message.c_str(); }
147
148 private:
149         string _message;
150 };
151
152 #endif /* __XML_H */
153