]>
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 | ||
ab7ce33c | 14 | #if defined(__GNUG__) && !defined(__APPLE__) |
56d2f750 VS |
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 | ||
30dc3455 VS |
23 | #ifdef WXMAKINGDLL_XRC |
24 | #define WXDLLIMPEXP_XRC WXEXPORT | |
25 | #elif defined(WXUSINGDLL) | |
26 | #define WXDLLIMPEXP_XRC WXIMPORT | |
27 | #else // not making nor using DLL | |
28 | #define WXDLLIMPEXP_XRC | |
ea89ec17 | 29 | #endif |
56d2f750 | 30 | |
30dc3455 VS |
31 | class WXDLLIMPEXP_XRC wxXmlNode; |
32 | class WXDLLIMPEXP_XRC wxXmlProperty; | |
33 | class WXDLLIMPEXP_XRC wxXmlDocument; | |
34 | class WXDLLIMPEXP_XRC wxXmlIOHandler; | |
1ad83f6b VS |
35 | class WXDLLEXPORT wxInputStream; |
36 | class WXDLLEXPORT wxOutputStream; | |
56d2f750 VS |
37 | |
38 | ||
39 | // Represents XML node type. | |
40 | enum wxXmlNodeType | |
41 | { | |
42 | // note: values are synchronized with xmlElementType from libxml | |
43 | wxXML_ELEMENT_NODE = 1, | |
44 | wxXML_ATTRIBUTE_NODE = 2, | |
45 | wxXML_TEXT_NODE = 3, | |
46 | wxXML_CDATA_SECTION_NODE = 4, | |
47 | wxXML_ENTITY_REF_NODE = 5, | |
48 | wxXML_ENTITY_NODE = 6, | |
49 | wxXML_PI_NODE = 7, | |
50 | wxXML_COMMENT_NODE = 8, | |
51 | wxXML_DOCUMENT_NODE = 9, | |
52 | wxXML_DOCUMENT_TYPE_NODE = 10, | |
53 | wxXML_DOCUMENT_FRAG_NODE = 11, | |
54 | wxXML_NOTATION_NODE = 12, | |
55 | wxXML_HTML_DOCUMENT_NODE = 13 | |
56 | }; | |
57 | ||
58 | ||
56d2f750 VS |
59 | // Represents node property(ies). |
60 | // Example: in <img src="hello.gif" id="3"/> "src" is property with value | |
61 | // "hello.gif" and "id" is prop. with value "3". | |
62 | ||
30dc3455 | 63 | class WXDLLIMPEXP_XRC wxXmlProperty |
56d2f750 | 64 | { |
eb8671f2 VS |
65 | public: |
66 | wxXmlProperty() : m_next(NULL) {} | |
ea89ec17 | 67 | wxXmlProperty(const wxString& name, const wxString& value, |
eb8671f2 VS |
68 | wxXmlProperty *next) |
69 | : m_name(name), m_value(value), m_next(next) {} | |
eb8671f2 VS |
70 | |
71 | wxString GetName() const { return m_name; } | |
72 | wxString GetValue() const { return m_value; } | |
73 | wxXmlProperty *GetNext() const { return m_next; } | |
74 | ||
75 | void SetName(const wxString& name) { m_name = name; } | |
76 | void SetValue(const wxString& value) { m_value = value; } | |
77 | void SetNext(wxXmlProperty *next) { m_next = next; } | |
78 | ||
79 | private: | |
80 | wxString m_name; | |
81 | wxString m_value; | |
82 | wxXmlProperty *m_next; | |
56d2f750 VS |
83 | }; |
84 | ||
85 | ||
86 | ||
87 | // Represents node in XML document. Node has name and may have content | |
88 | // and properties. Most common node types are wxXML_TEXT_NODE (name and props | |
89 | // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is | |
90 | // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE | |
91 | // with content="hi"). | |
eb8671f2 | 92 | // |
cc30b233 VS |
93 | // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load |
94 | // (default is UTF-8). | |
56d2f750 | 95 | |
30dc3455 | 96 | class WXDLLIMPEXP_XRC wxXmlNode |
56d2f750 | 97 | { |
eb8671f2 | 98 | public: |
ea89ec17 | 99 | wxXmlNode() : m_properties(NULL), m_parent(NULL), |
eb8671f2 | 100 | m_children(NULL), m_next(NULL) {} |
ea89ec17 | 101 | wxXmlNode(wxXmlNode *parent,wxXmlNodeType type, |
eb8671f2 VS |
102 | const wxString& name, const wxString& content, |
103 | wxXmlProperty *props, wxXmlNode *next); | |
2f0bac12 | 104 | ~wxXmlNode(); |
eb8671f2 VS |
105 | |
106 | // copy ctor & operator=. Note that this does NOT copy syblings | |
107 | // and parent pointer, i.e. m_parent and m_next will be NULL | |
108 | // after using copy ctor and are never unmodified by operator=. | |
109 | // On the other hand, it DOES copy children and properties. | |
110 | wxXmlNode(const wxXmlNode& node); | |
111 | wxXmlNode& operator=(const wxXmlNode& node); | |
112 | ||
113 | // user-friendly creation: | |
ea89ec17 | 114 | wxXmlNode(wxXmlNodeType type, const wxString& name, |
eb8671f2 VS |
115 | const wxString& content = wxEmptyString); |
116 | void AddChild(wxXmlNode *child); | |
117 | void InsertChild(wxXmlNode *child, wxXmlNode *before_node); | |
118 | bool RemoveChild(wxXmlNode *child); | |
119 | void AddProperty(const wxString& name, const wxString& value); | |
120 | bool DeleteProperty(const wxString& name); | |
121 | ||
122 | // access methods: | |
123 | wxXmlNodeType GetType() const { return m_type; } | |
124 | wxString GetName() const { return m_name; } | |
125 | wxString GetContent() const { return m_content; } | |
126 | ||
127 | wxXmlNode *GetParent() const { return m_parent; } | |
128 | wxXmlNode *GetNext() const { return m_next; } | |
129 | wxXmlNode *GetChildren() const { return m_children; } | |
130 | ||
131 | wxXmlProperty *GetProperties() const { return m_properties; } | |
132 | bool GetPropVal(const wxString& propName, wxString *value) const; | |
ea89ec17 | 133 | wxString GetPropVal(const wxString& propName, |
eb8671f2 VS |
134 | 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); | |
56d2f750 VS |
156 | }; |
157 | ||
158 | ||
159 | ||
160 | ||
161 | ||
162 | ||
163 | ||
cc30b233 | 164 | // This class holds XML data/document as parsed by XML parser. |
56d2f750 | 165 | |
30dc3455 | 166 | class WXDLLIMPEXP_XRC wxXmlDocument : public wxObject |
56d2f750 | 167 | { |
eb8671f2 | 168 | public: |
5dac8a3b | 169 | wxXmlDocument(); |
ea89ec17 | 170 | wxXmlDocument(const wxString& filename, |
cc30b233 | 171 | const wxString& encoding = wxT("UTF-8")); |
ea89ec17 | 172 | wxXmlDocument(wxInputStream& stream, |
cc30b233 | 173 | const wxString& encoding = wxT("UTF-8")); |
eb8671f2 VS |
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 | |
ea89ec17 | 180 | // otherwise. |
cc30b233 | 181 | bool Load(const wxString& filename, |
cc30b233 VS |
182 | const wxString& encoding = wxT("UTF-8")); |
183 | bool Load(wxInputStream& stream, | |
cc30b233 | 184 | const wxString& encoding = wxT("UTF-8")); |
5dac8a3b | 185 | |
eb8671f2 | 186 | // Saves document as .xml file. |
4d876ee3 VS |
187 | bool Save(const wxString& filename) const; |
188 | bool Save(wxOutputStream& stream) const; | |
eb8671f2 VS |
189 | |
190 | bool IsOk() const { return m_root != NULL; } | |
191 | ||
192 | // Returns root node of the document. | |
193 | wxXmlNode *GetRoot() const { return m_root; } | |
194 | ||
195 | // Returns version of document (may be empty). | |
196 | wxString GetVersion() const { return m_version; } | |
197 | // Returns encoding of document (may be empty). | |
5dac8a3b | 198 | // Note: this is the encoding original file was saved in, *not* the |
cc30b233 VS |
199 | // encoding of in-memory representation! |
200 | wxString GetFileEncoding() const { return m_fileEncoding; } | |
eb8671f2 VS |
201 | |
202 | // Write-access methods: | |
203 | void SetRoot(wxXmlNode *node) { delete m_root ; m_root = node; } | |
204 | void SetVersion(const wxString& version) { m_version = version; } | |
cc30b233 VS |
205 | void SetFileEncoding(const wxString& encoding) { m_fileEncoding = encoding; } |
206 | ||
207 | #if !wxUSE_UNICODE | |
208 | // Returns encoding of in-memory representation of the document | |
209 | // (same as passed to Load or ctor, defaults to UTF-8). | |
210 | // NB: this is meaningless in Unicode build where data are stored as wchar_t* | |
211 | wxString GetEncoding() const { return m_encoding; } | |
5dac8a3b | 212 | void SetEncoding(const wxString& enc) { m_encoding = enc; } |
cc30b233 | 213 | #endif |
eb8671f2 | 214 | |
eb8671f2 | 215 | private: |
cc30b233 VS |
216 | wxString m_version; |
217 | wxString m_fileEncoding; | |
218 | #if !wxUSE_UNICODE | |
219 | wxString m_encoding; | |
220 | #endif | |
eb8671f2 VS |
221 | wxXmlNode *m_root; |
222 | ||
223 | void DoCopy(const wxXmlDocument& doc); | |
56d2f750 VS |
224 | }; |
225 | ||
56d2f750 | 226 | #endif // _WX_XML_H_ |