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