]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xml.cpp
handle 646 charset under Solaris as US-ASCII
[wxWidgets.git] / src / xrc / 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/xrc/xml.h"
31 #include "wx/xrc/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()
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
93 wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node)
94 {
95 wxDELETE(m_properties);
96 wxDELETE(m_children);
97 DoCopy(node);
98 return *this;
99 }
100
101
102
103 void 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
127 bool 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
142 bool 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
161 wxString 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
172 void 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
188 void 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
207 bool 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
238 void wxXmlNode::AddProperty(const wxString& name, const wxString& value)
239 {
240 AddProperty(new wxXmlProperty(name, value, NULL));
241 }
242
243 void 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
257 bool wxXmlNode::DeleteProperty(const wxString& name)
258 {
259 wxXmlProperty *prop;
260
261 if (m_properties == NULL)
262 return FALSE;
263
264 else if (m_properties->GetName() == name)
265 {
266 prop = m_properties;
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 {
280 prop = p->GetNext();
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
299 wxList *wxXmlDocument::sm_handlers = NULL;
300
301
302
303 wxXmlDocument::wxXmlDocument(const wxString& filename, wxXmlIOType io_type,
304 const wxString& encoding)
305 : wxObject(), m_root(NULL)
306 {
307 if (!Load(filename, io_type, encoding))
308 {
309 wxDELETE(m_root);
310 }
311 }
312
313
314
315 wxXmlDocument::wxXmlDocument(wxInputStream& stream, wxXmlIOType io_type,
316 const wxString& encoding)
317 : wxObject(), m_root(NULL)
318 {
319 if (!Load(stream, io_type, encoding))
320 {
321 wxDELETE(m_root);
322 }
323 }
324
325
326
327 wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc)
328 {
329 DoCopy(doc);
330 }
331
332
333
334 wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc)
335 {
336 wxDELETE(m_root);
337 DoCopy(doc);
338 return *this;
339 }
340
341
342
343 void wxXmlDocument::DoCopy(const wxXmlDocument& doc)
344 {
345 m_version = doc.m_version;
346 #if !wxUSE_UNICODE
347 m_encoding = doc.m_encoding;
348 #endif
349 m_fileEncoding = doc.m_fileEncoding;
350 m_root = new wxXmlNode(*doc.m_root);
351 }
352
353
354
355 bool wxXmlDocument::Load(const wxString& filename, wxXmlIOType io_type,
356 const wxString& encoding)
357 {
358 wxFileInputStream stream(filename);
359 return Load(stream, io_type, encoding);
360 }
361
362
363
364 bool wxXmlDocument::Load(wxInputStream& stream, wxXmlIOType io_type,
365 const wxString& encoding)
366 {
367 #if wxUSE_UNICODE
368 (void)encoding;
369 #else
370 m_encoding = encoding;
371 #endif
372
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 {
381 return h->Load(stream, *this, encoding);
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
391 bool wxXmlDocument::Save(const wxString& filename, wxXmlIOType io_type) const
392 {
393 wxFileOutputStream stream(filename);
394 return Save(stream, io_type);
395 }
396
397
398
399 bool 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
420 void 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
431 void wxXmlDocument::CleanUpHandlers()
432 {
433 wxDELETE(sm_handlers);
434 }
435
436
437 void 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
450 class 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
459 IMPLEMENT_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.
466 void wxXmlInitXmlModule()
467 {
468 wxModule* module = new wxXmlModule;
469 module->Init();
470 wxModule::RegisterModule(module);
471 }
472