]> git.saurik.com Git - wxWidgets.git/blob - contrib/include/wx/xml/xml.h
*** empty log message ***
[wxWidgets.git] / contrib / include / wx / xml / xml.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: xml.h
3 // Purpose: wxXmlDocument - XML parser & data holder class
4 // Author: Vaclav Slavik
5 // Created: 2000/03/05
6 // RCS-ID: $Id$
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_XML_H_
12 #define _WX_XML_H_
13
14 #ifdef __GNUG__
15 #pragma interface "xml.h"
16 #endif
17
18 #include "wx/defs.h"
19 #include "wx/string.h"
20 #include "wx/object.h"
21 #include "wx/list.h"
22
23
24 class WXDLLEXPORT wxXmlNode;
25 class WXDLLEXPORT wxXmlProperty;
26 class WXDLLEXPORT wxXmlDocument;
27 class WXDLLEXPORT wxXmlIOHandler;
28 class WXDLLEXPORT wxInputStream;
29 class WXDLLEXPORT wxOutputStream;
30
31
32 // Represents XML node type.
33 enum wxXmlNodeType
34 {
35 // note: values are synchronized with xmlElementType from libxml
36 wxXML_ELEMENT_NODE = 1,
37 wxXML_ATTRIBUTE_NODE = 2,
38 wxXML_TEXT_NODE = 3,
39 wxXML_CDATA_SECTION_NODE = 4,
40 wxXML_ENTITY_REF_NODE = 5,
41 wxXML_ENTITY_NODE = 6,
42 wxXML_PI_NODE = 7,
43 wxXML_COMMENT_NODE = 8,
44 wxXML_DOCUMENT_NODE = 9,
45 wxXML_DOCUMENT_TYPE_NODE = 10,
46 wxXML_DOCUMENT_FRAG_NODE = 11,
47 wxXML_NOTATION_NODE = 12,
48 wxXML_HTML_DOCUMENT_NODE = 13
49 };
50
51
52 // Types of XML files:
53
54 enum wxXmlIOType
55 {
56 wxXML_IO_AUTO = 0, // detect it automatically
57 wxXML_IO_LIBXML, // use libxml2 to parse/save XML document
58 wxXML_IO_BIN, // save in binary uncompressed proprietary format
59 wxXML_IO_BINZ // svae in binary zlib-compressed proprietary format
60 };
61
62
63 // Represents node property(ies).
64 // Example: in <img src="hello.gif" id="3"/> "src" is property with value
65 // "hello.gif" and "id" is prop. with value "3".
66
67 class WXDLLEXPORT wxXmlProperty
68 {
69 public:
70 wxXmlProperty() : m_Next(NULL) {}
71 wxXmlProperty(const wxString& name, const wxString& value, wxXmlProperty *next)
72 : m_Name(name), m_Value(value), m_Next(next) {}
73 ~wxXmlProperty() { delete m_Next; }
74
75 wxString GetName() const { return m_Name; }
76 wxString GetValue() const { return m_Value; }
77 wxXmlProperty *GetNext() const { return m_Next; }
78
79 void SetName(const wxString& name) { m_Name = name; }
80 void SetValue(const wxString& value) { m_Value = value; }
81 void SetNext(wxXmlProperty *next) { m_Next = next; }
82
83 private:
84 wxString m_Name;
85 wxString m_Value;
86 wxXmlProperty *m_Next;
87 };
88
89
90
91 // Represents node in XML document. Node has name and may have content
92 // and properties. Most common node types are wxXML_TEXT_NODE (name and props
93 // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is
94 // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE
95 // with content="hi").
96
97 class WXDLLEXPORT wxXmlNode
98 {
99 public:
100 wxXmlNode() : m_Properties(NULL), m_Parent(NULL),
101 m_Children(NULL), m_Next(NULL) {}
102 wxXmlNode(wxXmlNode *parent,wxXmlNodeType type,
103 const wxString& name, const wxString& content,
104 wxXmlProperty *props, wxXmlNode *next);
105 ~wxXmlNode() { delete m_Properties; delete m_Next; delete m_Children; }
106
107 // copy ctor & operator=. Note that this does NOT copy syblings
108 // and parent pointer, i.e. m_Parent and m_Next will be NULL
109 // after using copy ctor and are never unmodified by operator=.
110 // On the other hand, it DOES copy children and properties.
111 wxXmlNode(const wxXmlNode& node);
112 wxXmlNode& operator=(const wxXmlNode& node);
113
114 // user-friendly creation:
115 wxXmlNode(wxXmlNodeType type, const wxString& name,
116 const wxString& content = wxEmptyString);
117 void AddChild(wxXmlNode *child);
118 void InsertChild(wxXmlNode *child, wxXmlNode *before_node);
119 bool RemoveChild(wxXmlNode *child);
120 void AddProperty(const wxString& name, const wxString& value);
121 bool DeleteProperty(const wxString& name);
122
123 // access methods:
124 wxXmlNodeType GetType() const { return m_Type; }
125 wxString GetName() const { return m_Name; }
126 wxString GetContent() const { return m_Content; }
127
128 wxXmlNode *GetParent() const { return m_Parent; }
129 wxXmlNode *GetNext() const { return m_Next; }
130 wxXmlNode *GetChildren() const { return m_Children; }
131
132 wxXmlProperty *GetProperties() const { return m_Properties; }
133 bool GetPropVal(const wxString& propName, wxString *value) const;
134 wxString GetPropVal(const wxString& propName, const wxString& defaultVal) const;
135 bool HasProp(const wxString& propName) const;
136
137 void SetType(wxXmlNodeType type) { m_Type = type; }
138 void SetName(const wxString& name) { m_Name = name; }
139 void SetContent(const wxString& con) { m_Content = con; }
140
141 void SetParent(wxXmlNode *parent) { m_Parent = parent; }
142 void SetNext(wxXmlNode *next) { m_Next = next; }
143 void SetChildren(wxXmlNode *child) { m_Children = child; }
144
145 void SetProperties(wxXmlProperty *prop) { m_Properties = prop; }
146 void AddProperty(wxXmlProperty *prop);
147
148 private:
149 wxXmlNodeType m_Type;
150 wxString m_Name;
151 wxString m_Content;
152 wxXmlProperty *m_Properties;
153 wxXmlNode *m_Parent, *m_Children, *m_Next;
154
155 void DoCopy(const wxXmlNode& node);
156 };
157
158
159
160
161
162
163
164 // This class holds XML data/document as parsed by libxml. Note that
165 // internal representation is independant on libxml and you can use
166 // it without libxml (see Load/SaveBinary).
167
168 class WXDLLEXPORT wxXmlDocument : public wxObject
169 {
170 public:
171 wxXmlDocument() : wxObject(), m_Version(wxT("1.0")), m_Root(NULL) {}
172 wxXmlDocument(const wxString& filename, wxXmlIOType io_type = wxXML_IO_AUTO);
173 wxXmlDocument(wxInputStream& stream, wxXmlIOType io_type = wxXML_IO_AUTO);
174 ~wxXmlDocument() { delete m_Root; }
175
176 wxXmlDocument(const wxXmlDocument& doc);
177 wxXmlDocument& operator=(const wxXmlDocument& doc);
178
179 // Parses .xml file and loads data. Returns TRUE on success, FALSE
180 // otherwise.
181 // NOTE: Any call to this method will result into linking against libxml
182 // and app's binary size will grow by ca. 250kB
183 bool Load(const wxString& filename, wxXmlIOType io_type = wxXML_IO_AUTO);
184 bool Load(wxInputStream& stream, wxXmlIOType io_type = wxXML_IO_AUTO);
185
186 // Saves document as .xml file.
187 bool Save(const wxString& filename, wxXmlIOType io_type) const;
188 bool Save(wxOutputStream& stream, wxXmlIOType io_type) const;
189
190 // Returns root node of the document.
191 wxXmlNode *GetRoot() const { return m_Root; }
192
193 // Returns version of document (may be empty).
194 wxString GetVersion() const { return m_Version; }
195 // Returns encoding of document (may be empty).
196 wxString GetEncoding() const { return m_Encoding; }
197
198 // Write-access methods:
199 void SetRoot(wxXmlNode *node) { delete m_Root ; m_Root = node; }
200 void SetVersion(const wxString& version) { m_Version = version; }
201 void SetEncoding(const wxString& encoding) { m_Encoding = encoding; }
202
203 static void AddHandler(wxXmlIOHandler *handler);
204 static void CleanUpHandlers();
205 static void InitStandardHandlers();
206
207 protected:
208 static wxList *sm_Handlers;
209
210 private:
211 wxString m_Version, m_Encoding;
212 wxXmlNode *m_Root;
213
214 void DoCopy(const wxXmlDocument& doc);
215 };
216
217
218
219 // wxXmlIOHandler takes care of loading and/or saving XML data.
220 // see xmlio.h for available handlers
221
222 class WXDLLEXPORT wxXmlIOHandler : public wxObject
223 {
224 public:
225 wxXmlIOHandler() {}
226
227 virtual wxXmlIOType GetType() = 0;
228 virtual bool CanLoad(wxInputStream& stream) = 0;
229 virtual bool CanSave() = 0;
230
231 virtual bool Load(wxInputStream& stream, wxXmlDocument& doc) = 0;
232 virtual bool Save(wxOutputStream& stream, const wxXmlDocument& doc) = 0;
233 };
234
235
236
237
238
239 #endif // _WX_XML_H_