]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xml.cpp
fixed compilation
[wxWidgets.git] / src / xrc / xml.cpp
CommitLineData
78d14f80
VS
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/xrc/xml.h"
31#include "wx/xrc/xmlio.h"
32
33
34
35wxXmlNode::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
56wxXmlNode::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
65wxXmlNode::wxXmlNode(const wxXmlNode& node)
66{
67 m_next = NULL;
68 m_parent = NULL;
69 DoCopy(node);
70}
71
72
73
93cd80d6
VS
74wxXmlNode::~wxXmlNode()
75{
76 wxXmlNode *c, *c2;
77 for (c = m_children; c; c = c2)
78 {
79 c2 = c->m_next;
80 delete c;
81 }
82
83 wxXmlProperty *p, *p2;
84 for (p = m_properties; p; p = p2)
85 {
86 p2 = p->GetNext();
87 delete p;
88 }
89}
90
91
92
78d14f80
VS
93wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node)
94{
3fb676f8
GT
95 wxDELETE(m_properties);
96 wxDELETE(m_children);
78d14f80
VS
97 DoCopy(node);
98 return *this;
99}
100
101
102
103void wxXmlNode::DoCopy(const wxXmlNode& node)
104{
105 m_type = node.m_type;
106 m_name = node.m_name;
107 m_content = node.m_content;
108 m_children = NULL;
109
110 wxXmlNode *n = node.m_children;
111 while (n)
112 {
113 AddChild(new wxXmlNode(*n));
114 n = n->GetNext();
115 }
116
117 m_properties = NULL;
118 wxXmlProperty *p = node.m_properties;
119 while (p)
120 {
121 AddProperty(p->GetName(), p->GetValue());
122 p = p->GetNext();
123 }
124}
125
126
127bool wxXmlNode::HasProp(const wxString& propName) const
128{
129 wxXmlProperty *prop = GetProperties();
130
131 while (prop)
132 {
133 if (prop->GetName() == propName) return TRUE;
134 prop = prop->GetNext();
135 }
136
137 return FALSE;
138}
139
140
141
142bool wxXmlNode::GetPropVal(const wxString& propName, wxString *value) const
143{
144 wxXmlProperty *prop = GetProperties();
145
146 while (prop)
147 {
148 if (prop->GetName() == propName)
149 {
150 *value = prop->GetValue();
151 return TRUE;
152 }
153 prop = prop->GetNext();
154 }
155
156 return FALSE;
157}
158
159
160
161wxString wxXmlNode::GetPropVal(const wxString& propName, const wxString& defaultVal) const
162{
163 wxString tmp;
164 if (GetPropVal(propName, &tmp))
165 return tmp;
166 else
167 return defaultVal;
168}
169
170
171
172void wxXmlNode::AddChild(wxXmlNode *child)
173{
174 if (m_children == NULL)
175 m_children = child;
176 else
177 {
178 wxXmlNode *ch = m_children;
179 while (ch->m_next) ch = ch->m_next;
180 ch->m_next = child;
181 }
182 child->m_next = NULL;
183 child->m_parent = this;
184}
185
186
187
188void wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *before_node)
189{
190 wxASSERT_MSG(before_node->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
191
192 if (m_children == before_node)
193 m_children = child;
194 else
195 {
196 wxXmlNode *ch = m_children;
197 while (ch->m_next != before_node) ch = ch->m_next;
198 ch->m_next = child;
199 }
200
201 child->m_parent = this;
202 child->m_next = before_node;
203}
204
205
206
207bool wxXmlNode::RemoveChild(wxXmlNode *child)
208{
209 if (m_children == NULL)
210 return FALSE;
211 else if (m_children == child)
212 {
213 m_children = child->m_next;
214 child->m_parent = NULL;
215 child->m_next = NULL;
216 return TRUE;
217 }
218 else
219 {
220 wxXmlNode *ch = m_children;
221 while (ch->m_next)
222 {
223 if (ch->m_next == child)
224 {
225 ch->m_next = child->m_next;
226 child->m_parent = NULL;
227 child->m_next = NULL;
228 return TRUE;
229 }
230 ch = ch->m_next;
231 }
232 return FALSE;
233 }
234}
235
236
237
238void wxXmlNode::AddProperty(const wxString& name, const wxString& value)
239{
240 AddProperty(new wxXmlProperty(name, value, NULL));
241}
242
243void wxXmlNode::AddProperty(wxXmlProperty *prop)
244{
245 if (m_properties == NULL)
246 m_properties = prop;
247 else
248 {
249 wxXmlProperty *p = m_properties;
250 while (p->GetNext()) p = p->GetNext();
251 p->SetNext(prop);
252 }
253}
254
255
256
257bool wxXmlNode::DeleteProperty(const wxString& name)
258{
46b8b00b
GT
259 wxXmlProperty *prop;
260
78d14f80
VS
261 if (m_properties == NULL)
262 return FALSE;
263
264 else if (m_properties->GetName() == name)
265 {
46b8b00b 266 prop = m_properties;
78d14f80
VS
267 m_properties = prop->GetNext();
268 prop->SetNext(NULL);
269 delete prop;
270 return TRUE;
271 }
272
273 else
274 {
275 wxXmlProperty *p = m_properties;
276 while (p->GetNext())
277 {
278 if (p->GetNext()->GetName() == name)
279 {
46b8b00b 280 prop = p->GetNext();
78d14f80
VS
281 p->SetNext(prop->GetNext());
282 prop->SetNext(NULL);
283 delete prop;
284 return TRUE;
285 }
286 p = p->GetNext();
287 }
288 return FALSE;
289 }
290}
291
292
293
294
295
296
297
298
299wxList *wxXmlDocument::sm_handlers = NULL;
300
301
302
303wxXmlDocument::wxXmlDocument(const wxString& filename, wxXmlIOType io_type)
304 : wxObject(), m_root(NULL)
305{
306 if (!Load(filename, io_type))
307 {
3fb676f8 308 wxDELETE(m_root);
78d14f80
VS
309 }
310}
311
312
313
314wxXmlDocument::wxXmlDocument(wxInputStream& stream, wxXmlIOType io_type)
315 : wxObject(), m_root(NULL)
316{
317 if (!Load(stream, io_type))
318 {
3fb676f8 319 wxDELETE(m_root);
78d14f80
VS
320 }
321}
322
323
324
325wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc)
326{
327 DoCopy(doc);
328}
329
330
331
332wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc)
333{
3fb676f8 334 wxDELETE(m_root);
78d14f80
VS
335 DoCopy(doc);
336 return *this;
337}
338
339
340
341void wxXmlDocument::DoCopy(const wxXmlDocument& doc)
342{
343 m_version = doc.m_version;
344 m_encoding = doc.m_encoding;
345 m_root = new wxXmlNode(*doc.m_root);
346}
347
348
349
350bool wxXmlDocument::Load(const wxString& filename, wxXmlIOType io_type)
351{
352 wxFileInputStream stream(filename);
353 return Load(stream, io_type);
354}
355
356
357
358bool wxXmlDocument::Load(wxInputStream& stream, wxXmlIOType io_type)
359{
360 wxNode *n = sm_handlers->GetFirst();
361 while (n)
362 {
363 wxXmlIOHandler *h = (wxXmlIOHandler*) n->GetData();
364
365 if ((io_type == wxXML_IO_AUTO || io_type == h->GetType()) &&
366 h->CanLoad(stream))
367 {
368 return h->Load(stream, *this);
369 }
370 n = n->GetNext();
371 }
372 wxLogError(_("Cannot find XML I/O handler capable of loading this format."));
373 return FALSE;
374}
375
376
377
378bool wxXmlDocument::Save(const wxString& filename, wxXmlIOType io_type) const
379{
380 wxFileOutputStream stream(filename);
381 return Save(stream, io_type);
382}
383
384
385
386bool wxXmlDocument::Save(wxOutputStream& stream, wxXmlIOType io_type) const
387{
388 wxNode *n = sm_handlers->GetFirst();
389 while (n)
390 {
391 wxXmlIOHandler *h = (wxXmlIOHandler*) n->GetData();
392 if (io_type == h->GetType() && h->CanSave())
393 {
394 return h->Save(stream, *this);
395 }
396 n = n->GetNext();
397 }
398 wxLogError(_("Cannot find XML I/O handler capable of saving in this format."));
399 return FALSE;
400}
401
402
403
404
405
406
407void wxXmlDocument::AddHandler(wxXmlIOHandler *handler)
408{
409 if (sm_handlers == NULL)
410 {
411 sm_handlers = new wxList;
412 sm_handlers->DeleteContents(TRUE);
413 }
414 sm_handlers->Append(handler);
415}
416
417
418void wxXmlDocument::CleanUpHandlers()
419{
3fb676f8 420 wxDELETE(sm_handlers);
78d14f80
VS
421}
422
423
424void wxXmlDocument::InitStandardHandlers()
425{
426 AddHandler(new wxXmlIOHandlerBin);
427#if wxUSE_ZLIB
428 AddHandler(new wxXmlIOHandlerBinZ);
429#endif
430 AddHandler(new wxXmlIOHandlerExpat);
431 AddHandler(new wxXmlIOHandlerWriter);
432}
433
434
435#include "wx/module.h"
436
437class wxXmlModule: public wxModule
438{
439 DECLARE_DYNAMIC_CLASS(wxXmlModule)
440 public:
441 wxXmlModule() {}
442 bool OnInit() { wxXmlDocument::InitStandardHandlers(); return TRUE; };
443 void OnExit() { wxXmlDocument::CleanUpHandlers(); };
444};
445
446IMPLEMENT_DYNAMIC_CLASS(wxXmlModule, wxModule)
447
448
449
450
451// When wxXml is loaded dynamically after the application is already running
452// then the built-in module system won't pick this one up. Add it manually.
453void wxXmlInitXmlModule()
454{
455 wxModule* module = new wxXmlModule;
456 module->Init();
457 wxModule::RegisterModule(module);
458}
459