]> git.saurik.com Git - wxWidgets.git/blob - include/wx/xrc/xml.h
warning 4284 doesn't have to be suppressed any more for VC++
[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 #if defined(__GNUG__) && !defined(__APPLE__)
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 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
29 #endif
30
31 class WXDLLIMPEXP_XRC wxXmlNode;
32 class WXDLLIMPEXP_XRC wxXmlProperty;
33 class WXDLLIMPEXP_XRC wxXmlDocument;
34 class WXDLLIMPEXP_XRC wxXmlIOHandler;
35 class WXDLLEXPORT wxInputStream;
36 class WXDLLEXPORT wxOutputStream;
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
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
63 class WXDLLIMPEXP_XRC wxXmlProperty
64 {
65 public:
66 wxXmlProperty() : m_next(NULL) {}
67 wxXmlProperty(const wxString& name, const wxString& value,
68 wxXmlProperty *next)
69 : m_name(name), m_value(value), m_next(next) {}
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;
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").
92 //
93 // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load
94 // (default is UTF-8).
95
96 class WXDLLIMPEXP_XRC wxXmlNode
97 {
98 public:
99 wxXmlNode() : m_properties(NULL), m_parent(NULL),
100 m_children(NULL), m_next(NULL) {}
101 wxXmlNode(wxXmlNode *parent,wxXmlNodeType type,
102 const wxString& name, const wxString& content,
103 wxXmlProperty *props, wxXmlNode *next);
104 ~wxXmlNode();
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:
114 wxXmlNode(wxXmlNodeType type, const wxString& name,
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;
133 wxString GetPropVal(const wxString& propName,
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);
156 };
157
158
159
160
161
162
163
164 // This class holds XML data/document as parsed by XML parser.
165
166 class WXDLLIMPEXP_XRC wxXmlDocument : public wxObject
167 {
168 public:
169 wxXmlDocument();
170 wxXmlDocument(const wxString& filename,
171 const wxString& encoding = wxT("UTF-8"));
172 wxXmlDocument(wxInputStream& stream,
173 const wxString& encoding = wxT("UTF-8"));
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
180 // otherwise.
181 bool Load(const wxString& filename,
182 const wxString& encoding = wxT("UTF-8"));
183 bool Load(wxInputStream& stream,
184 const wxString& encoding = wxT("UTF-8"));
185
186 // Saves document as .xml file.
187 bool Save(const wxString& filename) const;
188 bool Save(wxOutputStream& stream) const;
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).
198 // Note: this is the encoding original file was saved in, *not* the
199 // encoding of in-memory representation!
200 wxString GetFileEncoding() const { return m_fileEncoding; }
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; }
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; }
212 void SetEncoding(const wxString& enc) { m_encoding = enc; }
213 #endif
214
215 private:
216 wxString m_version;
217 wxString m_fileEncoding;
218 #if !wxUSE_UNICODE
219 wxString m_encoding;
220 #endif
221 wxXmlNode *m_root;
222
223 void DoCopy(const wxXmlDocument& doc);
224 };
225
226 #endif // _WX_XML_H_