]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xml.cpp
Added missing '!' to HasCapture() in window.cpp
[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
480505bc
VS
303wxXmlDocument::wxXmlDocument(const wxString& filename, wxXmlIOType io_type,
304 const wxString& encoding)
78d14f80
VS
305 : wxObject(), m_root(NULL)
306{
480505bc 307 if (!Load(filename, io_type, encoding))
78d14f80 308 {
3fb676f8 309 wxDELETE(m_root);
78d14f80
VS
310 }
311}
312
313
314
480505bc
VS
315wxXmlDocument::wxXmlDocument(wxInputStream& stream, wxXmlIOType io_type,
316 const wxString& encoding)
78d14f80
VS
317 : wxObject(), m_root(NULL)
318{
480505bc 319 if (!Load(stream, io_type, encoding))
78d14f80 320 {
3fb676f8 321 wxDELETE(m_root);
78d14f80
VS
322 }
323}
324
325
326
327wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc)
328{
329 DoCopy(doc);
330}
331
332
333
334wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc)
335{
3fb676f8 336 wxDELETE(m_root);
78d14f80
VS
337 DoCopy(doc);
338 return *this;
339}
340
341
342
343void wxXmlDocument::DoCopy(const wxXmlDocument& doc)
344{
345 m_version = doc.m_version;
480505bc 346#if !wxUSE_UNICODE
78d14f80 347 m_encoding = doc.m_encoding;
480505bc
VS
348#endif
349 m_fileEncoding = doc.m_fileEncoding;
78d14f80
VS
350 m_root = new wxXmlNode(*doc.m_root);
351}
352
353
354
480505bc
VS
355bool wxXmlDocument::Load(const wxString& filename, wxXmlIOType io_type,
356 const wxString& encoding)
78d14f80
VS
357{
358 wxFileInputStream stream(filename);
480505bc 359 return Load(stream, io_type, encoding);
78d14f80
VS
360}
361
362
363
480505bc
VS
364bool wxXmlDocument::Load(wxInputStream& stream, wxXmlIOType io_type,
365 const wxString& encoding)
78d14f80 366{
480505bc
VS
367#if wxUSE_UNICODE
368 (void)encoding;
369#else
370 m_encoding = encoding;
371#endif
372
78d14f80
VS
373 wxNode *n = sm_handlers->GetFirst();
374 while (n)
375 {
376 wxXmlIOHandler *h = (wxXmlIOHandler*) n->GetData();
377
378 if ((io_type == wxXML_IO_AUTO || io_type == h->GetType()) &&
379 h->CanLoad(stream))
380 {
480505bc 381 return h->Load(stream, *this, encoding);
78d14f80
VS
382 }
383 n = n->GetNext();
384 }
385 wxLogError(_("Cannot find XML I/O handler capable of loading this format."));
386 return FALSE;
387}
388
389
390
391bool wxXmlDocument::Save(const wxString& filename, wxXmlIOType io_type) const
392{
393 wxFileOutputStream stream(filename);
394 return Save(stream, io_type);
395}
396
397
398
399bool wxXmlDocument::Save(wxOutputStream& stream, wxXmlIOType io_type) const
400{
401 wxNode *n = sm_handlers->GetFirst();
402 while (n)
403 {
404 wxXmlIOHandler *h = (wxXmlIOHandler*) n->GetData();
405 if (io_type == h->GetType() && h->CanSave())
406 {
407 return h->Save(stream, *this);
408 }
409 n = n->GetNext();
410 }
411 wxLogError(_("Cannot find XML I/O handler capable of saving in this format."));
412 return FALSE;
413}
414
415
416
417
418
419
420void wxXmlDocument::AddHandler(wxXmlIOHandler *handler)
421{
422 if (sm_handlers == NULL)
423 {
424 sm_handlers = new wxList;
425 sm_handlers->DeleteContents(TRUE);
426 }
427 sm_handlers->Append(handler);
428}
429
430
431void wxXmlDocument::CleanUpHandlers()
432{
3fb676f8 433 wxDELETE(sm_handlers);
78d14f80
VS
434}
435
436
437void wxXmlDocument::InitStandardHandlers()
438{
439 AddHandler(new wxXmlIOHandlerBin);
440#if wxUSE_ZLIB
441 AddHandler(new wxXmlIOHandlerBinZ);
442#endif
443 AddHandler(new wxXmlIOHandlerExpat);
444 AddHandler(new wxXmlIOHandlerWriter);
445}
446
447
448#include "wx/module.h"
449
450class wxXmlModule: public wxModule
451{
452 DECLARE_DYNAMIC_CLASS(wxXmlModule)
453 public:
454 wxXmlModule() {}
455 bool OnInit() { wxXmlDocument::InitStandardHandlers(); return TRUE; };
456 void OnExit() { wxXmlDocument::CleanUpHandlers(); };
457};
458
459IMPLEMENT_DYNAMIC_CLASS(wxXmlModule, wxModule)
460
461
462
463
464// When wxXml is loaded dynamically after the application is already running
465// then the built-in module system won't pick this one up. Add it manually.
466void wxXmlInitXmlModule()
467{
468 wxModule* module = new wxXmlModule;
469 module->Init();
470 wxModule::RegisterModule(module);
471}
472