]> git.saurik.com Git - wxWidgets.git/blob - include/wx/xrc/xml.h
changed wxXML to XRC, wx/xml/*.h->wx/xrc/*.h
[wxWidgets.git] / include / wx / xrc / 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 #ifdef WXXMLISDLL
24 #define WXXMLDLLEXPORT WXDLLEXPORT
25 #else
26 #define WXXMLDLLEXPORT
27 #endif
28
29 class WXXMLDLLEXPORT wxXmlNode;
30 class WXXMLDLLEXPORT wxXmlProperty;
31 class WXXMLDLLEXPORT wxXmlDocument;
32 class WXXMLDLLEXPORT wxXmlIOHandler;
33 class WXDLLEXPORT wxInputStream;
34 class WXDLLEXPORT wxOutputStream;
35
36
37 // Represents XML node type.
38 enum wxXmlNodeType
39 {
40 // note: values are synchronized with xmlElementType from libxml
41 wxXML_ELEMENT_NODE = 1,
42 wxXML_ATTRIBUTE_NODE = 2,
43 wxXML_TEXT_NODE = 3,
44 wxXML_CDATA_SECTION_NODE = 4,
45 wxXML_ENTITY_REF_NODE = 5,
46 wxXML_ENTITY_NODE = 6,
47 wxXML_PI_NODE = 7,
48 wxXML_COMMENT_NODE = 8,
49 wxXML_DOCUMENT_NODE = 9,
50 wxXML_DOCUMENT_TYPE_NODE = 10,
51 wxXML_DOCUMENT_FRAG_NODE = 11,
52 wxXML_NOTATION_NODE = 12,
53 wxXML_HTML_DOCUMENT_NODE = 13
54 };
55
56
57 // Types of XML files:
58
59 enum wxXmlIOType
60 {
61 wxXML_IO_AUTO = 0, // detect it automatically
62 wxXML_IO_EXPAT, // use Expat to load from text/xml document
63 wxXML_IO_TEXT_OUTPUT, // generic saver into text/xml
64 wxXML_IO_BIN, // save in binary uncompressed proprietary format
65 wxXML_IO_BINZ // svae in binary zlib-compressed proprietary format
66 };
67
68
69 // Represents node property(ies).
70 // Example: in <img src="hello.gif" id="3"/> "src" is property with value
71 // "hello.gif" and "id" is prop. with value "3".
72
73 class WXXMLDLLEXPORT wxXmlProperty
74 {
75 public:
76 wxXmlProperty() : m_next(NULL) {}
77 wxXmlProperty(const wxString& name, const wxString& value,
78 wxXmlProperty *next)
79 : m_name(name), m_value(value), m_next(next) {}
80 ~wxXmlProperty() { delete m_next; }
81
82 wxString GetName() const { return m_name; }
83 wxString GetValue() const { return m_value; }
84 wxXmlProperty *GetNext() const { return m_next; }
85
86 void SetName(const wxString& name) { m_name = name; }
87 void SetValue(const wxString& value) { m_value = value; }
88 void SetNext(wxXmlProperty *next) { m_next = next; }
89
90 private:
91 wxString m_name;
92 wxString m_value;
93 wxXmlProperty *m_next;
94 };
95
96
97
98 // Represents node in XML document. Node has name and may have content
99 // and properties. Most common node types are wxXML_TEXT_NODE (name and props
100 // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is
101 // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE
102 // with content="hi").
103 //
104 // If wxUSE_UNICODE is 0, all strings are encoded in UTF-8 encoding (same as
105 // ASCII for characters 0-127). You can use wxMBConvUTF8 to convert then to
106 // desired encoding:
107 //
108 // wxCSConv myConv("iso8859-2");
109 // wxString s(cMB2WC(node->GetContent().c_str()), myConv);
110
111 class WXXMLDLLEXPORT wxXmlNode
112 {
113 public:
114 wxXmlNode() : m_properties(NULL), m_parent(NULL),
115 m_children(NULL), m_next(NULL) {}
116 wxXmlNode(wxXmlNode *parent,wxXmlNodeType type,
117 const wxString& name, const wxString& content,
118 wxXmlProperty *props, wxXmlNode *next);
119 ~wxXmlNode() { delete m_properties; delete m_next; delete m_children; }
120
121 // copy ctor & operator=. Note that this does NOT copy syblings
122 // and parent pointer, i.e. m_parent and m_next will be NULL
123 // after using copy ctor and are never unmodified by operator=.
124 // On the other hand, it DOES copy children and properties.
125 wxXmlNode(const wxXmlNode& node);
126 wxXmlNode& operator=(const wxXmlNode& node);
127
128 // user-friendly creation:
129 wxXmlNode(wxXmlNodeType type, const wxString& name,
130 const wxString& content = wxEmptyString);
131 void AddChild(wxXmlNode *child);
132 void InsertChild(wxXmlNode *child, wxXmlNode *before_node);
133 bool RemoveChild(wxXmlNode *child);
134 void AddProperty(const wxString& name, const wxString& value);
135 bool DeleteProperty(const wxString& name);
136
137 // access methods:
138 wxXmlNodeType GetType() const { return m_type; }
139 wxString GetName() const { return m_name; }
140 wxString GetContent() const { return m_content; }
141
142 wxXmlNode *GetParent() const { return m_parent; }
143 wxXmlNode *GetNext() const { return m_next; }
144 wxXmlNode *GetChildren() const { return m_children; }
145
146 wxXmlProperty *GetProperties() const { return m_properties; }
147 bool GetPropVal(const wxString& propName, wxString *value) const;
148 wxString GetPropVal(const wxString& propName,
149 const wxString& defaultVal) const;
150 bool HasProp(const wxString& propName) const;
151
152 void SetType(wxXmlNodeType type) { m_type = type; }
153 void SetName(const wxString& name) { m_name = name; }
154 void SetContent(const wxString& con) { m_content = con; }
155
156 void SetParent(wxXmlNode *parent) { m_parent = parent; }
157 void SetNext(wxXmlNode *next) { m_next = next; }
158 void SetChildren(wxXmlNode *child) { m_children = child; }
159
160 void SetProperties(wxXmlProperty *prop) { m_properties = prop; }
161 void AddProperty(wxXmlProperty *prop);
162
163 private:
164 wxXmlNodeType m_type;
165 wxString m_name;
166 wxString m_content;
167 wxXmlProperty *m_properties;
168 wxXmlNode *m_parent, *m_children, *m_next;
169
170 void DoCopy(const wxXmlNode& node);
171 };
172
173
174
175
176
177
178
179 // This class holds XML data/document as parsed by libxml. Note that
180 // internal representation is independant on libxml and you can use
181 // it without libxml (see Load/SaveBinary).
182
183 class WXXMLDLLEXPORT wxXmlDocument : public wxObject
184 {
185 public:
186 wxXmlDocument() : wxObject(), m_version(wxT("1.0")), m_root(NULL) {}
187 wxXmlDocument(const wxString& filename,
188 wxXmlIOType io_type = wxXML_IO_AUTO);
189 wxXmlDocument(wxInputStream& stream,
190 wxXmlIOType io_type = wxXML_IO_AUTO);
191 ~wxXmlDocument() { delete m_root; }
192
193 wxXmlDocument(const wxXmlDocument& doc);
194 wxXmlDocument& operator=(const wxXmlDocument& doc);
195
196 // Parses .xml file and loads data. Returns TRUE on success, FALSE
197 // otherwise.
198 // NOTE: Any call to this method will result into linking against libxml
199 // and app's binary size will grow by ca. 250kB
200 bool Load(const wxString& filename, wxXmlIOType io_type = wxXML_IO_AUTO);
201 bool Load(wxInputStream& stream, wxXmlIOType io_type = wxXML_IO_AUTO);
202
203 // Saves document as .xml file.
204 bool Save(const wxString& filename,
205 wxXmlIOType io_type = wxXML_IO_TEXT_OUTPUT) const;
206 bool Save(wxOutputStream& stream,
207 wxXmlIOType io_type = wxXML_IO_TEXT_OUTPUT) const;
208
209 bool IsOk() const { return m_root != NULL; }
210
211 // Returns root node of the document.
212 wxXmlNode *GetRoot() const { return m_root; }
213
214 // Returns version of document (may be empty).
215 wxString GetVersion() const { return m_version; }
216 // Returns encoding of document (may be empty).
217 // Note: this is the encoding original fail was saved in, *not* the
218 // encoding of in-memory representation! Data in wxXmlNode are always
219 // stored in wchar_t (in Unicode build) or UTF-8 encoded
220 // (if wxUSE_UNICODE is 0).
221 wxString GetEncoding() const { return m_encoding; }
222
223 // Write-access methods:
224 void SetRoot(wxXmlNode *node) { delete m_root ; m_root = node; }
225 void SetVersion(const wxString& version) { m_version = version; }
226 void SetEncoding(const wxString& encoding) { m_encoding = encoding; }
227
228 static void AddHandler(wxXmlIOHandler *handler);
229 static void CleanUpHandlers();
230 static void InitStandardHandlers();
231
232 protected:
233 static wxList *sm_handlers;
234
235 private:
236 wxString m_version, m_encoding;
237 wxXmlNode *m_root;
238
239 void DoCopy(const wxXmlDocument& doc);
240 };
241
242
243
244 // wxXmlIOHandler takes care of loading and/or saving XML data.
245 // see xmlio.h for available handlers
246
247 class WXXMLDLLEXPORT wxXmlIOHandler : public wxObject
248 {
249 public:
250 wxXmlIOHandler() {}
251
252 virtual wxXmlIOType GetType() = 0;
253 virtual bool CanLoad(wxInputStream& stream) = 0;
254 virtual bool CanSave() = 0;
255
256 virtual bool Load(wxInputStream& stream, wxXmlDocument& doc) = 0;
257 virtual bool Save(wxOutputStream& stream, const wxXmlDocument& doc) = 0;
258 };
259
260
261
262 void wxXmlInitXmlModule();
263
264 #endif // _WX_XML_H_