65ab0f2c03ac4d2eb105f893482678df9d70c8a8
[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 class XMLTree;
22 class XMLNode;
23 class XMLProperty;
24
25 typedef std::list<XMLNode *>                   XMLNodeList;
26 typedef std::list<boost::shared_ptr<XMLNode> > XMLSharedNodeList;
27 typedef XMLNodeList::iterator                  XMLNodeIterator;
28 typedef XMLNodeList::const_iterator            XMLNodeConstIterator;
29 typedef std::list<XMLProperty*>                XMLPropertyList;
30 typedef XMLPropertyList::iterator              XMLPropertyIterator;
31 typedef XMLPropertyList::const_iterator        XMLPropertyConstIterator;
32 typedef std::map<std::string, XMLProperty*>    XMLPropertyMap;
33
34 class XMLTree {
35 public:
36         XMLTree();
37         XMLTree(const std::string& fn, bool validate = false);
38         XMLTree(const XMLTree*);
39         ~XMLTree();
40
41         XMLNode* root() const         { return _root; }
42         XMLNode* set_root(XMLNode* n) { return _root = n; }
43
44         const std::string& filename() const               { return _filename; }
45         const std::string& set_filename(const std::string& fn) { return _filename = fn; }
46
47         int compression() const { return _compression; }
48         int set_compression(int);
49
50         bool read() { return read_internal(false); }
51         bool read(const std::string& fn) { set_filename(fn); return read_internal(false); }
52         bool read_and_validate() { return read_internal(true); }
53         bool read_and_validate(const std::string& fn) { set_filename(fn); return read_internal(true); }
54         bool read_buffer(const std::string&);
55
56         bool write() const;
57         bool write(const std::string& fn) { set_filename(fn); return write(); }
58
59         void debug (FILE*) const;
60
61         const std::string& write_buffer() const;
62
63 private:
64         bool read_internal(bool validate);
65         
66         std::string _filename;
67         XMLNode*    _root;
68         int         _compression;
69 };
70
71 class XMLNode {
72 public:
73         XMLNode(const std::string& name);
74         XMLNode(const std::string& name, const std::string& content);
75         XMLNode(const XMLNode& other);
76         ~XMLNode();
77
78         const std::string& name() const { return _name; }
79
80         bool          is_content() const { return _is_content; }
81         const std::string& content()    const { return _content; }
82         const std::string& set_content(const std::string&);
83         XMLNode*      add_content(const std::string& s = std::string());
84
85         const XMLNodeList& children(const std::string& str = std::string()) const;
86         XMLNode* child(const char*) const;
87         XMLNode* add_child(const char *);
88         XMLNode* add_child_copy(const XMLNode&);
89         void     add_child_nocopy(XMLNode&);
90
91         boost::shared_ptr<XMLSharedNodeList> find(const std::string xpath) const;
92         std::string attribute_value();
93
94         const XMLPropertyList& properties() const { return _proplist; }
95         XMLProperty*       property(const char*);
96         XMLProperty*       property(const std::string&);
97         const XMLProperty* property(const char* n)   const { return ((XMLNode*)this)->property(n); }
98         const XMLProperty* property(const std::string& n) const { return ((XMLNode*)this)->property(n); }
99         
100         XMLProperty* add_property(const char* name, const std::string& value);
101         XMLProperty* add_property(const char* name, const char* value = "");
102         XMLProperty* add_property(const char* name, const long value);
103
104         void remove_property(const std::string&);
105
106         /** Remove all nodes with the name passed to remove_nodes */
107         void remove_nodes(const std::string&);
108         /** Remove and delete all nodes with the name passed to remove_nodes */
109         void remove_nodes_and_delete(const std::string&);
110         /** Remove and delete all nodes with property prop matching val */
111         void remove_nodes_and_delete(const std::string& propname, const std::string& val);
112
113 private:
114         std::string         _name;
115         bool                _is_content;
116         std::string         _content;
117         XMLNodeList         _children;
118         XMLPropertyList     _proplist;
119         XMLPropertyMap      _propmap;
120         mutable XMLNodeList _selected_children;
121 };
122
123 class XMLProperty {
124 public:
125         XMLProperty(const std::string& n, const std::string& v = std::string());
126         ~XMLProperty();
127
128         const std::string& name() const { return _name; }
129         const std::string& value() const { return _value; }
130         const std::string& set_value(const std::string& v) { return _value = v; }
131
132 private:
133         std::string _name;
134         std::string _value;
135 };
136
137 class XMLException: public std::exception {
138 public:
139         explicit XMLException(const std::string msg) : _message(msg) {}
140         virtual ~XMLException() throw() {}
141
142         virtual const char* what() const throw() { return _message.c_str(); }
143
144 private:
145         std::string _message;
146 };
147
148 #endif /* __XML_H */
149