]>
Commit | Line | Data |
---|---|---|
27b0c286 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 | |
65571936 | 8 | // Licence: wxWindows licence |
27b0c286 VS |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | ||
12 | /* ************************************************************************* * | |
13 | * CAUTION! * | |
14 | * * | |
15 | * The API defined in this header *WILL* change in the future and backward * | |
16 | * compatibility will *not* be preserved. If you use these classes in your * | |
77ffb593 | 17 | * application, it probably won't compile with future wxWidgets releases. * |
27b0c286 VS |
18 | * Use on your own risk. * |
19 | * * | |
20 | * ************************************************************************* */ | |
21 | ||
22 | #ifndef _WX_XML_H_ | |
23 | #define _WX_XML_H_ | |
24 | ||
27b0c286 VS |
25 | #include "wx/defs.h" |
26 | ||
27 | #if wxUSE_XML | |
28 | ||
29 | #include "wx/string.h" | |
30 | #include "wx/object.h" | |
31 | #include "wx/list.h" | |
32 | ||
33 | #ifdef WXMAKINGDLL_XML | |
34 | #define WXDLLIMPEXP_XML WXEXPORT | |
35 | #elif defined(WXUSINGDLL) | |
36 | #define WXDLLIMPEXP_XML WXIMPORT | |
37 | #else // not making nor using DLL | |
38 | #define WXDLLIMPEXP_XML | |
39 | #endif | |
40 | ||
41 | class WXDLLIMPEXP_XML wxXmlNode; | |
42 | class WXDLLIMPEXP_XML wxXmlProperty; | |
43 | class WXDLLIMPEXP_XML wxXmlDocument; | |
44 | class WXDLLIMPEXP_XML wxXmlIOHandler; | |
45 | class WXDLLIMPEXP_BASE wxInputStream; | |
46 | class WXDLLIMPEXP_BASE wxOutputStream; | |
47 | ||
48 | ||
49 | // Represents XML node type. | |
50 | enum wxXmlNodeType | |
51 | { | |
52 | // note: values are synchronized with xmlElementType from libxml | |
53 | wxXML_ELEMENT_NODE = 1, | |
54 | wxXML_ATTRIBUTE_NODE = 2, | |
55 | wxXML_TEXT_NODE = 3, | |
56 | wxXML_CDATA_SECTION_NODE = 4, | |
57 | wxXML_ENTITY_REF_NODE = 5, | |
58 | wxXML_ENTITY_NODE = 6, | |
59 | wxXML_PI_NODE = 7, | |
60 | wxXML_COMMENT_NODE = 8, | |
61 | wxXML_DOCUMENT_NODE = 9, | |
62 | wxXML_DOCUMENT_TYPE_NODE = 10, | |
63 | wxXML_DOCUMENT_FRAG_NODE = 11, | |
64 | wxXML_NOTATION_NODE = 12, | |
65 | wxXML_HTML_DOCUMENT_NODE = 13 | |
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 WXDLLIMPEXP_XML 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 | ||
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 | ||
89 | private: | |
90 | wxString m_name; | |
91 | wxString m_value; | |
92 | wxXmlProperty *m_next; | |
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"). | |
102 | // | |
103 | // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load | |
104 | // (default is UTF-8). | |
105 | ||
106 | class WXDLLIMPEXP_XML wxXmlNode | |
107 | { | |
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(); | |
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); | |
166 | }; | |
167 | ||
168 | ||
169 | ||
170 | ||
171 | ||
172 | ||
173 | ||
174 | // This class holds XML data/document as parsed by XML parser. | |
175 | ||
176 | class WXDLLIMPEXP_XML wxXmlDocument : public wxObject | |
177 | { | |
178 | public: | |
179 | wxXmlDocument(); | |
180 | wxXmlDocument(const wxString& filename, | |
181 | const wxString& encoding = wxT("UTF-8")); | |
182 | wxXmlDocument(wxInputStream& stream, | |
183 | const wxString& encoding = wxT("UTF-8")); | |
184 | ~wxXmlDocument() { delete m_root; } | |
185 | ||
186 | wxXmlDocument(const wxXmlDocument& doc); | |
187 | wxXmlDocument& operator=(const wxXmlDocument& doc); | |
188 | ||
189 | // Parses .xml file and loads data. Returns TRUE on success, FALSE | |
190 | // otherwise. | |
191 | bool Load(const wxString& filename, | |
192 | const wxString& encoding = wxT("UTF-8")); | |
193 | bool Load(wxInputStream& stream, | |
194 | const wxString& encoding = wxT("UTF-8")); | |
195 | ||
196 | // Saves document as .xml file. | |
197 | bool Save(const wxString& filename) const; | |
198 | bool Save(wxOutputStream& stream) const; | |
199 | ||
200 | bool IsOk() const { return m_root != NULL; } | |
201 | ||
202 | // Returns root node of the document. | |
203 | wxXmlNode *GetRoot() const { return m_root; } | |
204 | ||
205 | // Returns version of document (may be empty). | |
206 | wxString GetVersion() const { return m_version; } | |
207 | // Returns encoding of document (may be empty). | |
208 | // Note: this is the encoding original file was saved in, *not* the | |
209 | // encoding of in-memory representation! | |
210 | wxString GetFileEncoding() const { return m_fileEncoding; } | |
211 | ||
212 | // Write-access methods: | |
213 | void SetRoot(wxXmlNode *node) { delete m_root ; m_root = node; } | |
214 | void SetVersion(const wxString& version) { m_version = version; } | |
215 | void SetFileEncoding(const wxString& encoding) { m_fileEncoding = encoding; } | |
216 | ||
217 | #if !wxUSE_UNICODE | |
218 | // Returns encoding of in-memory representation of the document | |
219 | // (same as passed to Load or ctor, defaults to UTF-8). | |
220 | // NB: this is meaningless in Unicode build where data are stored as wchar_t* | |
221 | wxString GetEncoding() const { return m_encoding; } | |
222 | void SetEncoding(const wxString& enc) { m_encoding = enc; } | |
223 | #endif | |
224 | ||
225 | private: | |
226 | wxString m_version; | |
227 | wxString m_fileEncoding; | |
228 | #if !wxUSE_UNICODE | |
229 | wxString m_encoding; | |
230 | #endif | |
231 | wxXmlNode *m_root; | |
232 | ||
233 | void DoCopy(const wxXmlDocument& doc); | |
234 | }; | |
235 | ||
236 | #endif // wxUSE_XML | |
237 | ||
238 | #endif // _WX_XML_H_ |