]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/xml/xml.cpp
fixed wxUSE_MAC not being defined under Unix => -D_REENTRANT was missing resulting...
[wxWidgets.git] / contrib / src / xml / xml.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: xml.cpp
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 #ifdef __GNUG__
12 #pragma implementation "xml.h"
13 #pragma implementation "xmlio.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23
24 #include "wx/wfstream.h"
25 #include "wx/datstrm.h"
26 #include "wx/zstream.h"
27 #include "wx/log.h"
28 #include "wx/intl.h"
29
30 #include "wx/xml/xml.h"
31 #include "wx/xml/xmlio.h"
32
33
34
35 wxXmlNode::wxXmlNode(wxXmlNode *parent,wxXmlNodeType type,
36 const wxString& name, const wxString& content,
37 wxXmlProperty *props, wxXmlNode *next)
38 : m_Type(type), m_Name(name), m_Content(content),
39 m_Properties(props), m_Parent(parent),
40 m_Children(NULL), m_Next(next)
41 {
42 if (m_Parent)
43 {
44 if (m_Parent->m_Children)
45 {
46 m_Next = m_Parent->m_Children;
47 m_Parent->m_Children = this;
48 }
49 else
50 m_Parent->m_Children = this;
51 }
52 }
53
54
55
56 wxXmlNode::wxXmlNode(wxXmlNodeType type, const wxString& name,
57 const wxString& content)
58 : m_Type(type), m_Name(name), m_Content(content),
59 m_Properties(NULL), m_Parent(NULL),
60 m_Children(NULL), m_Next(NULL)
61 {}
62
63
64
65 wxXmlNode::wxXmlNode(const wxXmlNode& node)
66 {
67 m_Next = NULL;
68 m_Parent = NULL;
69 DoCopy(node);
70 }
71
72
73
74 wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node)
75 {
76 delete m_Properties;
77 delete m_Children;
78 DoCopy(node);
79 return *this;
80 }
81
82
83
84 void wxXmlNode::DoCopy(const wxXmlNode& node)
85 {
86 m_Type = node.m_Type;
87 m_Name = node.m_Name;
88 m_Content = node.m_Content;
89 m_Children = NULL;
90
91 wxXmlNode *n = node.m_Children;
92 while (n)
93 {
94 AddChild(new wxXmlNode(*n));
95 n = n->GetNext();
96 }
97
98 m_Properties = NULL;
99 wxXmlProperty *p = node.m_Properties;
100 while (p)
101 {
102 AddProperty(p->GetName(), p->GetValue());
103 p = p->GetNext();
104 }
105 }
106
107
108 bool wxXmlNode::HasProp(const wxString& propName) const
109 {
110 wxXmlProperty *prop = GetProperties();
111
112 while (prop)
113 {
114 if (prop->GetName() == propName) return TRUE;
115 prop = prop->GetNext();
116 }
117
118 return FALSE;
119 }
120
121
122
123 bool wxXmlNode::GetPropVal(const wxString& propName, wxString *value) const
124 {
125 wxXmlProperty *prop = GetProperties();
126
127 while (prop)
128 {
129 if (prop->GetName() == propName)
130 {
131 *value = prop->GetValue();
132 return TRUE;
133 }
134 prop = prop->GetNext();
135 }
136
137 return FALSE;
138 }
139
140
141
142 wxString wxXmlNode::GetPropVal(const wxString& propName, const wxString& defaultVal) const
143 {
144 wxString tmp;
145 if (GetPropVal(propName, &tmp))
146 return tmp;
147 else
148 return defaultVal;
149 }
150
151
152
153 void wxXmlNode::AddChild(wxXmlNode *child)
154 {
155 if (m_Children == NULL)
156 m_Children = child;
157 else
158 {
159 wxXmlNode *ch = m_Children;
160 while (ch->m_Next) ch = ch->m_Next;
161 ch->m_Next = child;
162 }
163 child->m_Next = NULL;
164 child->m_Parent = this;
165 }
166
167
168
169 void wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *before_node)
170 {
171 wxASSERT_MSG(before_node->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
172
173 if (m_Children == before_node)
174 m_Children = child;
175 else
176 {
177 wxXmlNode *ch = m_Children;
178 while (ch->m_Next != before_node) ch = ch->m_Next;
179 ch->m_Next = child;
180 }
181
182 child->m_Parent = this;
183 child->m_Next = before_node;
184 }
185
186
187
188 bool wxXmlNode::RemoveChild(wxXmlNode *child)
189 {
190 if (m_Children == NULL)
191 return FALSE;
192 else if (m_Children == child)
193 {
194 m_Children = child->m_Next;
195 child->m_Parent = NULL;
196 child->m_Next = NULL;
197 return TRUE;
198 }
199 else
200 {
201 wxXmlNode *ch = m_Children;
202 while (ch->m_Next)
203 {
204 if (ch->m_Next == child)
205 {
206 ch->m_Next = child->m_Next;
207 child->m_Parent = NULL;
208 child->m_Next = NULL;
209 return TRUE;
210 }
211 ch = ch->m_Next;
212 }
213 return FALSE;
214 }
215 }
216
217
218
219 void wxXmlNode::AddProperty(const wxString& name, const wxString& value)
220 {
221 AddProperty(new wxXmlProperty(name, value, NULL));
222 }
223
224 void wxXmlNode::AddProperty(wxXmlProperty *prop)
225 {
226 if (m_Properties == NULL)
227 m_Properties = prop;
228 else
229 {
230 wxXmlProperty *p = m_Properties;
231 while (p->GetNext()) p = p->GetNext();
232 p->SetNext(prop);
233 }
234 }
235
236
237
238 bool wxXmlNode::DeleteProperty(const wxString& name)
239 {
240 if (m_Properties == NULL)
241 return FALSE;
242
243 else if (m_Properties->GetName() == name)
244 {
245 wxXmlProperty *prop = m_Properties;
246 m_Properties = prop->GetNext();
247 prop->SetNext(NULL);
248 delete prop;
249 return TRUE;
250 }
251
252 else
253 {
254 wxXmlProperty *p = m_Properties;
255 while (p->GetNext())
256 {
257 if (p->GetNext()->GetName() == name)
258 {
259 wxXmlProperty *prop = p->GetNext();
260 p->SetNext(prop->GetNext());
261 prop->SetNext(NULL);
262 delete prop;
263 return TRUE;
264 }
265 p = p->GetNext();
266 }
267 return FALSE;
268 }
269 }
270
271
272
273
274
275
276
277
278 wxList *wxXmlDocument::sm_Handlers = NULL;
279
280
281
282 wxXmlDocument::wxXmlDocument(const wxString& filename, wxXmlIOType io_type)
283 : wxObject(), m_Root(NULL)
284 {
285 if (!Load(filename, io_type))
286 {
287 delete m_Root;
288 m_Root = NULL;
289 }
290 }
291
292
293
294 wxXmlDocument::wxXmlDocument(wxInputStream& stream, wxXmlIOType io_type)
295 : wxObject(), m_Root(NULL)
296 {
297 if (!Load(stream, io_type))
298 {
299 delete m_Root;
300 m_Root = NULL;
301 }
302 }
303
304
305
306 wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc)
307 {
308 DoCopy(doc);
309 }
310
311
312
313 wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc)
314 {
315 delete m_Root;
316 DoCopy(doc);
317 return *this;
318 }
319
320
321
322 void wxXmlDocument::DoCopy(const wxXmlDocument& doc)
323 {
324 m_Version = doc.m_Version;
325 m_Encoding = doc.m_Encoding;
326 m_Root = new wxXmlNode(*doc.m_Root);
327 }
328
329
330
331 bool wxXmlDocument::Load(const wxString& filename, wxXmlIOType io_type)
332 {
333 wxFileInputStream stream(filename);
334 return Load(stream, io_type);
335 }
336
337
338
339 bool wxXmlDocument::Load(wxInputStream& stream, wxXmlIOType io_type)
340 {
341 wxNode *n = sm_Handlers->GetFirst();
342 while (n)
343 {
344 wxXmlIOHandler *h = (wxXmlIOHandler*) n->GetData();
345
346 if ((io_type == wxXML_IO_AUTO || io_type == h->GetType()) &&
347 h->CanLoad(stream))
348 {
349 return h->Load(stream, *this);
350 }
351 n = n->GetNext();
352 }
353 wxLogError(_("Cannot find XML I/O handler capable of loading this format."));
354 return FALSE;
355 }
356
357
358
359 bool wxXmlDocument::Save(const wxString& filename, wxXmlIOType io_type) const
360 {
361 wxFileOutputStream stream(filename);
362 return Save(stream, io_type);
363 }
364
365
366
367 bool wxXmlDocument::Save(wxOutputStream& stream, wxXmlIOType io_type) const
368 {
369 wxNode *n = sm_Handlers->GetFirst();
370 while (n)
371 {
372 wxXmlIOHandler *h = (wxXmlIOHandler*) n->GetData();
373 if (io_type == h->GetType() && h->CanSave())
374 {
375 return h->Save(stream, *this);
376 }
377 n = n->GetNext();
378 }
379 wxLogError(_("Cannot find XML I/O handler capable of saving in this format."));
380 return FALSE;
381 }
382
383
384
385
386
387
388 void wxXmlDocument::AddHandler(wxXmlIOHandler *handler)
389 {
390 if (sm_Handlers == NULL)
391 {
392 sm_Handlers = new wxList;
393 sm_Handlers->DeleteContents(TRUE);
394 }
395 sm_Handlers->Append(handler);
396 }
397
398
399 void wxXmlDocument::CleanUpHandlers()
400 {
401 delete sm_Handlers;
402 sm_Handlers = NULL;
403 }
404
405
406 void wxXmlDocument::InitStandardHandlers()
407 {
408 AddHandler(new wxXmlIOHandlerBin);
409 AddHandler(new wxXmlIOHandlerBinZ);
410 AddHandler(new wxXmlIOHandlerLibxml);
411 }
412
413
414 #include "wx/module.h"
415
416 class wxXmlModule: public wxModule
417 {
418 DECLARE_DYNAMIC_CLASS(wxXmlModule)
419 public:
420 wxXmlModule() {}
421 bool OnInit() { wxXmlDocument::InitStandardHandlers(); return TRUE; };
422 void OnExit() { wxXmlDocument::CleanUpHandlers(); };
423 };
424
425 IMPLEMENT_DYNAMIC_CLASS(wxXmlModule, wxModule)