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