]> git.saurik.com Git - wxWidgets.git/blame - src/xml/xml.cpp
Return non-const pointer from wxDataViewRendererBase::GetView().
[wxWidgets.git] / src / xml / xml.cpp
CommitLineData
27b0c286 1/////////////////////////////////////////////////////////////////////////////
40989e46 2// Name: src/xml/xml.cpp
27b0c286
VS
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
65571936 8// Licence: wxWindows licence
27b0c286
VS
9/////////////////////////////////////////////////////////////////////////////
10
27b0c286
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
27b0c286
VS
18#if wxUSE_XML
19
40989e46
WS
20#include "wx/xml/xml.h"
21
88a7a4e1
WS
22#ifndef WX_PRECOMP
23 #include "wx/intl.h"
e4db172a 24 #include "wx/log.h"
670f9935 25 #include "wx/app.h"
88a7a4e1
WS
26#endif
27
27b0c286
VS
28#include "wx/wfstream.h"
29#include "wx/datstrm.h"
30#include "wx/zstream.h"
27b0c286 31#include "wx/strconv.h"
664e1314 32#include "wx/scopedptr.h"
ccec9093 33#include "wx/versioninfo.h"
27b0c286
VS
34
35#include "expat.h" // from Expat
36
34fdf762 37// DLL options compatibility check:
34fdf762
VS
38WX_CHECK_BUILD_OPTIONS("wxXML")
39
4c43dd90
JS
40
41IMPLEMENT_CLASS(wxXmlDocument, wxObject)
42
43
538f3830 44// a private utility used by wxXML
352d9b89 45static bool wxIsWhiteOnly(const wxString& buf);
538f3830 46
4c43dd90 47
27b0c286
VS
48//-----------------------------------------------------------------------------
49// wxXmlNode
50//-----------------------------------------------------------------------------
51
52wxXmlNode::wxXmlNode(wxXmlNode *parent,wxXmlNodeType type,
53 const wxString& name, const wxString& content,
6e26d6b7 54 wxXmlAttribute *attrs, wxXmlNode *next, int lineNo)
27b0c286 55 : m_type(type), m_name(name), m_content(content),
288b6107 56 m_attrs(attrs), m_parent(parent),
6e26d6b7 57 m_children(NULL), m_next(next),
30f6914b
JS
58 m_lineNo(lineNo),
59 m_noConversion(false)
27b0c286 60{
527d2d2d
VS
61 wxASSERT_MSG ( type != wxXML_ELEMENT_NODE || content.empty(), "element nodes can't have content" );
62
27b0c286
VS
63 if (m_parent)
64 {
65 if (m_parent->m_children)
66 {
67 m_next = m_parent->m_children;
68 m_parent->m_children = this;
69 }
70 else
71 m_parent->m_children = this;
72 }
73}
74
75wxXmlNode::wxXmlNode(wxXmlNodeType type, const wxString& name,
6e26d6b7
VS
76 const wxString& content,
77 int lineNo)
27b0c286 78 : m_type(type), m_name(name), m_content(content),
288b6107 79 m_attrs(NULL), m_parent(NULL),
6e26d6b7 80 m_children(NULL), m_next(NULL),
30f6914b 81 m_lineNo(lineNo), m_noConversion(false)
527d2d2d
VS
82{
83 wxASSERT_MSG ( type != wxXML_ELEMENT_NODE || content.empty(), "element nodes can't have content" );
84}
27b0c286
VS
85
86wxXmlNode::wxXmlNode(const wxXmlNode& node)
87{
88 m_next = NULL;
89 m_parent = NULL;
90 DoCopy(node);
91}
92
93wxXmlNode::~wxXmlNode()
94{
95 wxXmlNode *c, *c2;
96 for (c = m_children; c; c = c2)
97 {
98 c2 = c->m_next;
99 delete c;
100 }
101
288b6107
VS
102 wxXmlAttribute *p, *p2;
103 for (p = m_attrs; p; p = p2)
27b0c286
VS
104 {
105 p2 = p->GetNext();
106 delete p;
107 }
108}
109
110wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node)
111{
288b6107 112 wxDELETE(m_attrs);
27b0c286
VS
113 wxDELETE(m_children);
114 DoCopy(node);
115 return *this;
116}
117
118void wxXmlNode::DoCopy(const wxXmlNode& node)
119{
120 m_type = node.m_type;
121 m_name = node.m_name;
122 m_content = node.m_content;
6e26d6b7 123 m_lineNo = node.m_lineNo;
30f6914b 124 m_noConversion = node.m_noConversion;
27b0c286
VS
125 m_children = NULL;
126
127 wxXmlNode *n = node.m_children;
128 while (n)
129 {
130 AddChild(new wxXmlNode(*n));
131 n = n->GetNext();
132 }
133
288b6107
VS
134 m_attrs = NULL;
135 wxXmlAttribute *p = node.m_attrs;
27b0c286
VS
136 while (p)
137 {
288b6107 138 AddAttribute(p->GetName(), p->GetValue());
27b0c286
VS
139 p = p->GetNext();
140 }
141}
142
288b6107 143bool wxXmlNode::HasAttribute(const wxString& attrName) const
27b0c286 144{
288b6107 145 wxXmlAttribute *attr = GetAttributes();
27b0c286 146
288b6107 147 while (attr)
27b0c286 148 {
288b6107
VS
149 if (attr->GetName() == attrName) return true;
150 attr = attr->GetNext();
27b0c286
VS
151 }
152
759f7272 153 return false;
27b0c286
VS
154}
155
288b6107 156bool wxXmlNode::GetAttribute(const wxString& attrName, wxString *value) const
27b0c286 157{
cc24bf91
VS
158 wxCHECK_MSG( value, false, "value argument must not be NULL" );
159
288b6107 160 wxXmlAttribute *attr = GetAttributes();
27b0c286 161
288b6107 162 while (attr)
27b0c286 163 {
288b6107 164 if (attr->GetName() == attrName)
27b0c286 165 {
288b6107 166 *value = attr->GetValue();
759f7272 167 return true;
27b0c286 168 }
288b6107 169 attr = attr->GetNext();
27b0c286
VS
170 }
171
759f7272 172 return false;
27b0c286
VS
173}
174
288b6107 175wxString wxXmlNode::GetAttribute(const wxString& attrName, const wxString& defaultVal) const
27b0c286
VS
176{
177 wxString tmp;
288b6107 178 if (GetAttribute(attrName, &tmp))
27b0c286 179 return tmp;
0e2710a6
DS
180
181 return defaultVal;
27b0c286
VS
182}
183
184void wxXmlNode::AddChild(wxXmlNode *child)
185{
186 if (m_children == NULL)
187 m_children = child;
188 else
189 {
190 wxXmlNode *ch = m_children;
191 while (ch->m_next) ch = ch->m_next;
192 ch->m_next = child;
193 }
194 child->m_next = NULL;
195 child->m_parent = this;
196}
197
5e05df3c
VS
198// inserts a new node in front of 'followingNode'
199bool wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *followingNode)
27b0c286 200{
5e05df3c
VS
201 wxCHECK_MSG( child, false, "cannot insert a NULL node!" );
202 wxCHECK_MSG( child->m_parent == NULL, false, "node already has a parent" );
203 wxCHECK_MSG( child->m_next == NULL, false, "node already has m_next" );
204 wxCHECK_MSG( followingNode == NULL || followingNode->GetParent() == this,
205 false,
206 "wxXmlNode::InsertChild - followingNode has incorrect parent" );
207
208 // this is for backward compatibility, NULL was allowed here thanks to
209 // the confusion about followingNode's meaning
210 if ( followingNode == NULL )
211 followingNode = m_children;
212
213 if ( m_children == followingNode )
fa6a8373 214 {
fa6a8373
RR
215 child->m_next = m_children;
216 m_children = child;
fa6a8373 217 }
27b0c286
VS
218 else
219 {
220 wxXmlNode *ch = m_children;
5e05df3c
VS
221 while ( ch && ch->m_next != followingNode )
222 ch = ch->m_next;
223 if ( !ch )
224 {
225 wxFAIL_MSG( "followingNode has this node as parent, but couldn't be found among children" );
226 return false;
227 }
228
229 child->m_next = followingNode;
27b0c286
VS
230 ch->m_next = child;
231 }
232
233 child->m_parent = this;
fa6a8373 234 return true;
27b0c286
VS
235}
236
43a302f2
VS
237// inserts a new node right after 'precedingNode'
238bool wxXmlNode::InsertChildAfter(wxXmlNode *child, wxXmlNode *precedingNode)
239{
240 wxCHECK_MSG( child, false, "cannot insert a NULL node!" );
241 wxCHECK_MSG( child->m_parent == NULL, false, "node already has a parent" );
242 wxCHECK_MSG( child->m_next == NULL, false, "node already has m_next" );
243 wxCHECK_MSG( precedingNode == NULL || precedingNode->m_parent == this, false,
244 "precedingNode has wrong parent" );
245
246 if ( precedingNode )
247 {
248 child->m_next = precedingNode->m_next;
249 precedingNode->m_next = child;
250 }
251 else // precedingNode == NULL
252 {
253 wxCHECK_MSG( m_children == NULL, false,
254 "NULL precedingNode only makes sense when there are no children" );
255
256 child->m_next = m_children;
257 m_children = child;
258 }
259
260 child->m_parent = this;
261 return true;
262}
263
27b0c286
VS
264bool wxXmlNode::RemoveChild(wxXmlNode *child)
265{
266 if (m_children == NULL)
759f7272 267 return false;
27b0c286
VS
268 else if (m_children == child)
269 {
270 m_children = child->m_next;
271 child->m_parent = NULL;
272 child->m_next = NULL;
759f7272 273 return true;
27b0c286
VS
274 }
275 else
276 {
277 wxXmlNode *ch = m_children;
278 while (ch->m_next)
279 {
280 if (ch->m_next == child)
281 {
282 ch->m_next = child->m_next;
283 child->m_parent = NULL;
284 child->m_next = NULL;
759f7272 285 return true;
27b0c286
VS
286 }
287 ch = ch->m_next;
288 }
759f7272 289 return false;
27b0c286
VS
290 }
291}
292
e13ce4a3
VZ
293void wxXmlNode::AddAttribute(const wxString& name, const wxString& value)
294{
295 AddProperty(name, value);
296}
297
298void wxXmlNode::AddAttribute(wxXmlAttribute *attr)
299{
300 AddProperty(attr);
301}
302
303bool wxXmlNode::DeleteAttribute(const wxString& name)
304{
305 return DeleteProperty(name);
306}
307
27b0c286
VS
308void wxXmlNode::AddProperty(const wxString& name, const wxString& value)
309{
288b6107 310 AddProperty(new wxXmlAttribute(name, value, NULL));
27b0c286
VS
311}
312
288b6107 313void wxXmlNode::AddProperty(wxXmlAttribute *attr)
27b0c286 314{
288b6107
VS
315 if (m_attrs == NULL)
316 m_attrs = attr;
27b0c286
VS
317 else
318 {
288b6107 319 wxXmlAttribute *p = m_attrs;
27b0c286 320 while (p->GetNext()) p = p->GetNext();
288b6107 321 p->SetNext(attr);
27b0c286
VS
322 }
323}
324
325bool wxXmlNode::DeleteProperty(const wxString& name)
326{
288b6107 327 wxXmlAttribute *attr;
27b0c286 328
288b6107 329 if (m_attrs == NULL)
759f7272 330 return false;
27b0c286 331
288b6107 332 else if (m_attrs->GetName() == name)
27b0c286 333 {
288b6107
VS
334 attr = m_attrs;
335 m_attrs = attr->GetNext();
336 attr->SetNext(NULL);
337 delete attr;
759f7272 338 return true;
27b0c286
VS
339 }
340
341 else
342 {
288b6107 343 wxXmlAttribute *p = m_attrs;
27b0c286
VS
344 while (p->GetNext())
345 {
346 if (p->GetNext()->GetName() == name)
347 {
288b6107
VS
348 attr = p->GetNext();
349 p->SetNext(attr->GetNext());
350 attr->SetNext(NULL);
351 delete attr;
759f7272 352 return true;
27b0c286
VS
353 }
354 p = p->GetNext();
355 }
759f7272 356 return false;
27b0c286
VS
357 }
358}
359
4c43dd90
JS
360wxString wxXmlNode::GetNodeContent() const
361{
362 wxXmlNode *n = GetChildren();
363
364 while (n)
365 {
366 if (n->GetType() == wxXML_TEXT_NODE ||
367 n->GetType() == wxXML_CDATA_SECTION_NODE)
368 return n->GetContent();
369 n = n->GetNext();
370 }
371 return wxEmptyString;
372}
373
538f3830
VS
374int wxXmlNode::GetDepth(wxXmlNode *grandparent) const
375{
376 const wxXmlNode *n = this;
377 int ret = -1;
378
379 do
380 {
381 ret++;
382 n = n->GetParent();
383 if (n == grandparent)
384 return ret;
385
386 } while (n);
387
388 return wxNOT_FOUND;
389}
390
391bool wxXmlNode::IsWhitespaceOnly() const
392{
393 return wxIsWhiteOnly(m_content);
394}
395
27b0c286
VS
396
397
398//-----------------------------------------------------------------------------
399// wxXmlDocument
400//-----------------------------------------------------------------------------
401
402wxXmlDocument::wxXmlDocument()
cee8636e 403 : m_version(wxS("1.0")), m_fileEncoding(wxS("UTF-8")), m_docNode(NULL)
27b0c286
VS
404{
405#if !wxUSE_UNICODE
e767076e 406 m_encoding = wxS("UTF-8");
27b0c286
VS
407#endif
408}
409
410wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding)
cee8636e 411 :wxObject(), m_docNode(NULL)
27b0c286
VS
412{
413 if ( !Load(filename, encoding) )
414 {
cee8636e 415 wxDELETE(m_docNode);
27b0c286
VS
416 }
417}
418
419wxXmlDocument::wxXmlDocument(wxInputStream& stream, const wxString& encoding)
cee8636e 420 :wxObject(), m_docNode(NULL)
27b0c286
VS
421{
422 if ( !Load(stream, encoding) )
423 {
cee8636e 424 wxDELETE(m_docNode);
27b0c286
VS
425 }
426}
427
428wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc)
d0468e8c 429 :wxObject()
27b0c286
VS
430{
431 DoCopy(doc);
432}
433
434wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc)
435{
cee8636e 436 wxDELETE(m_docNode);
27b0c286
VS
437 DoCopy(doc);
438 return *this;
439}
440
441void wxXmlDocument::DoCopy(const wxXmlDocument& doc)
442{
443 m_version = doc.m_version;
444#if !wxUSE_UNICODE
445 m_encoding = doc.m_encoding;
446#endif
447 m_fileEncoding = doc.m_fileEncoding;
e8da6b7c 448
cee8636e
VZ
449 if (doc.m_docNode)
450 m_docNode = new wxXmlNode(*doc.m_docNode);
e8da6b7c 451 else
cee8636e 452 m_docNode = NULL;
27b0c286
VS
453}
454
538f3830 455bool wxXmlDocument::Load(const wxString& filename, const wxString& encoding, int flags)
27b0c286
VS
456{
457 wxFileInputStream stream(filename);
a1b806b9 458 if (!stream.IsOk())
97757cee 459 return false;
538f3830 460 return Load(stream, encoding, flags);
27b0c286
VS
461}
462
538f3830 463bool wxXmlDocument::Save(const wxString& filename, int indentstep) const
27b0c286
VS
464{
465 wxFileOutputStream stream(filename);
a1b806b9 466 if (!stream.IsOk())
97757cee 467 return false;
538f3830 468 return Save(stream, indentstep);
27b0c286
VS
469}
470
cee8636e
VZ
471wxXmlNode *wxXmlDocument::GetRoot() const
472{
473 wxXmlNode *node = m_docNode;
474 if (node)
475 {
476 node = m_docNode->GetChildren();
477 while (node != NULL && node->GetType() != wxXML_ELEMENT_NODE)
478 node = node->GetNext();
479 }
480 return node;
481}
482
483wxXmlNode *wxXmlDocument::DetachRoot()
484{
485 wxXmlNode *node = m_docNode;
486 if (node)
487 {
488 node = m_docNode->GetChildren();
489 wxXmlNode *prev = NULL;
490 while (node != NULL && node->GetType() != wxXML_ELEMENT_NODE)
491 {
492 prev = node;
493 node = node->GetNext();
494 }
495 if (node)
496 {
497 if (node == m_docNode->GetChildren())
498 m_docNode->SetChildren(node->GetNext());
499
500 if (prev)
501 prev->SetNext(node->GetNext());
502
503 node->SetParent(NULL);
504 node->SetNext(NULL);
505 }
506 }
507 return node;
508}
27b0c286 509
cee8636e
VZ
510void wxXmlDocument::SetRoot(wxXmlNode *root)
511{
6f24b817
VZ
512 if (root)
513 {
514 wxASSERT_MSG( root->GetType() == wxXML_ELEMENT_NODE,
515 "Can only set an element type node as root" );
516 }
517
cee8636e
VZ
518 wxXmlNode *node = m_docNode;
519 if (node)
520 {
521 node = m_docNode->GetChildren();
522 wxXmlNode *prev = NULL;
523 while (node != NULL && node->GetType() != wxXML_ELEMENT_NODE)
524 {
525 prev = node;
526 node = node->GetNext();
527 }
6f24b817 528 if (node && root)
cee8636e
VZ
529 {
530 root->SetNext( node->GetNext() );
531 wxDELETE(node);
532 }
533 if (prev)
534 prev->SetNext(root);
535 else
536 m_docNode->SetChildren(root);
537 }
538 else
539 {
540 m_docNode = new wxXmlNode(wxXML_DOCUMENT_NODE, wxEmptyString);
6f24b817 541 m_docNode->SetChildren(root);
cee8636e 542 }
6f24b817
VZ
543 if (root)
544 root->SetParent(m_docNode);
cee8636e
VZ
545}
546
547void wxXmlDocument::AppendToProlog(wxXmlNode *node)
548{
549 if (!m_docNode)
550 m_docNode = new wxXmlNode(wxXML_DOCUMENT_NODE, wxEmptyString);
551 if (IsOk())
552 m_docNode->InsertChild( node, GetRoot() );
553 else
554 m_docNode->AddChild( node );
555}
27b0c286
VS
556
557//-----------------------------------------------------------------------------
558// wxXmlDocument loading routines
559//-----------------------------------------------------------------------------
560
ddae497f
VZ
561// converts Expat-produced string in UTF-8 into wxString using the specified
562// conv or keep in UTF-8 if conv is NULL
563static wxString CharToString(wxMBConv *conv,
b04edcaf 564 const char *s, size_t len = wxString::npos)
27b0c286 565{
b04edcaf 566#if !wxUSE_UNICODE
27b0c286
VS
567 if ( conv )
568 {
ddae497f
VZ
569 // there can be no embedded NULs in this string so we don't need the
570 // output length, it will be NUL-terminated
571 const wxWCharBuffer wbuf(
6df6e35a 572 wxConvUTF8.cMB2WC(s, len == wxString::npos ? wxNO_LEN : len, NULL));
ddae497f 573
da452b9e 574 return wxString(wbuf, *conv);
27b0c286 575 }
b04edcaf
VS
576 // else: the string is wanted in UTF-8
577#endif // !wxUSE_UNICODE
578
579 wxUnusedVar(conv);
cc209a51 580 return wxString::FromUTF8Unchecked(s, len);
27b0c286
VS
581}
582
538f3830 583// returns true if the given string contains only whitespaces
352d9b89 584bool wxIsWhiteOnly(const wxString& buf)
538f3830 585{
352d9b89
VS
586 for ( wxString::const_iterator i = buf.begin(); i != buf.end(); ++i )
587 {
588 wxChar c = *i;
e767076e 589 if ( c != wxS(' ') && c != wxS('\t') && c != wxS('\n') && c != wxS('\r'))
538f3830 590 return false;
352d9b89 591 }
538f3830
VS
592 return true;
593}
594
595
27b0c286
VS
596struct wxXmlParsingContext
597{
43a302f2
VS
598 wxXmlParsingContext()
599 : conv(NULL),
43a302f2
VS
600 node(NULL),
601 lastChild(NULL),
602 lastAsText(NULL),
603 removeWhiteOnlyNodes(false)
604 {}
605
6e26d6b7 606 XML_Parser parser;
27b0c286 607 wxMBConv *conv;
43a302f2
VS
608 wxXmlNode *node; // the node being parsed
609 wxXmlNode *lastChild; // the last child of "node"
610 wxXmlNode *lastAsText; // the last _text_ child of "node"
27b0c286
VS
611 wxString encoding;
612 wxString version;
538f3830 613 bool removeWhiteOnlyNodes;
27b0c286
VS
614};
615
43a302f2
VS
616// checks that ctx->lastChild is in consistent state
617#define ASSERT_LAST_CHILD_OK(ctx) \
618 wxASSERT( ctx->lastChild == NULL || \
619 ctx->lastChild->GetNext() == NULL ); \
620 wxASSERT( ctx->lastChild == NULL || \
621 ctx->lastChild->GetParent() == ctx->node )
622
865bb325 623extern "C" {
27b0c286
VS
624static void StartElementHnd(void *userData, const char *name, const char **atts)
625{
626 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
6e26d6b7
VS
627 wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE,
628 CharToString(ctx->conv, name),
629 wxEmptyString,
630 XML_GetCurrentLineNumber(ctx->parser));
27b0c286 631 const char **a = atts;
6e26d6b7 632
43a302f2 633 // add node attributes
27b0c286
VS
634 while (*a)
635 {
288b6107 636 node->AddAttribute(CharToString(ctx->conv, a[0]), CharToString(ctx->conv, a[1]));
27b0c286
VS
637 a += 2;
638 }
43a302f2 639
cee8636e
VZ
640 ASSERT_LAST_CHILD_OK(ctx);
641 ctx->node->InsertChildAfter(node, ctx->lastChild);
27b0c286 642 ctx->lastAsText = NULL;
43a302f2
VS
643 ctx->lastChild = NULL; // our new node "node" has no children yet
644
645 ctx->node = node;
27b0c286
VS
646}
647
648static void EndElementHnd(void *userData, const char* WXUNUSED(name))
649{
650 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
651
43a302f2
VS
652 // we're exiting the last children of ctx->node->GetParent() and going
653 // back one level up, so current value of ctx->node points to the last
654 // child of ctx->node->GetParent()
655 ctx->lastChild = ctx->node;
656
27b0c286
VS
657 ctx->node = ctx->node->GetParent();
658 ctx->lastAsText = NULL;
659}
660
661static void TextHnd(void *userData, const char *s, int len)
662{
663 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
538f3830 664 wxString str = CharToString(ctx->conv, s, len);
27b0c286
VS
665
666 if (ctx->lastAsText)
667 {
d6cb1287 668 ctx->lastAsText->SetContent(ctx->lastAsText->GetContent() + str);
27b0c286
VS
669 }
670 else
671 {
538f3830
VS
672 bool whiteOnly = false;
673 if (ctx->removeWhiteOnlyNodes)
674 whiteOnly = wxIsWhiteOnly(str);
675
27b0c286
VS
676 if (!whiteOnly)
677 {
43a302f2 678 wxXmlNode *textnode =
e767076e 679 new wxXmlNode(wxXML_TEXT_NODE, wxS("text"), str,
6e26d6b7 680 XML_GetCurrentLineNumber(ctx->parser));
43a302f2
VS
681
682 ASSERT_LAST_CHILD_OK(ctx);
683 ctx->node->InsertChildAfter(textnode, ctx->lastChild);
684 ctx->lastChild= ctx->lastAsText = textnode;
27b0c286
VS
685 }
686 }
27b0c286
VS
687}
688
a01cbf0a
RR
689static void StartCdataHnd(void *userData)
690{
691 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
692
43a302f2 693 wxXmlNode *textnode =
e767076e 694 new wxXmlNode(wxXML_CDATA_SECTION_NODE, wxS("cdata"), wxS(""),
6e26d6b7 695 XML_GetCurrentLineNumber(ctx->parser));
43a302f2
VS
696
697 ASSERT_LAST_CHILD_OK(ctx);
698 ctx->node->InsertChildAfter(textnode, ctx->lastChild);
699 ctx->lastChild= ctx->lastAsText = textnode;
a01cbf0a 700}
a01cbf0a 701
a6cf6bcf
VZ
702static void EndCdataHnd(void *userData)
703{
704 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
705
706 // we need to reset this pointer so that subsequent text nodes don't append
707 // their contents to this one but create new wxXML_TEXT_NODE objects (or
708 // not create anything at all if only white space follows the CDATA section
709 // and wxXMLDOC_KEEP_WHITESPACE_NODES is not used as is commonly the case)
710 ctx->lastAsText = NULL;
711}
712
27b0c286
VS
713static void CommentHnd(void *userData, const char *data)
714{
715 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
716
cee8636e
VZ
717 wxXmlNode *commentnode =
718 new wxXmlNode(wxXML_COMMENT_NODE,
719 wxS("comment"), CharToString(ctx->conv, data),
720 XML_GetCurrentLineNumber(ctx->parser));
43a302f2 721
cee8636e
VZ
722 ASSERT_LAST_CHILD_OK(ctx);
723 ctx->node->InsertChildAfter(commentnode, ctx->lastChild);
724 ctx->lastChild = commentnode;
725 ctx->lastAsText = NULL;
726}
727
728static void PIHnd(void *userData, const char *target, const char *data)
729{
730 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
731
732 wxXmlNode *pinode =
733 new wxXmlNode(wxXML_PI_NODE, CharToString(ctx->conv, target),
734 CharToString(ctx->conv, data),
735 XML_GetCurrentLineNumber(ctx->parser));
736
737 ASSERT_LAST_CHILD_OK(ctx);
738 ctx->node->InsertChildAfter(pinode, ctx->lastChild);
739 ctx->lastChild = pinode;
27b0c286
VS
740 ctx->lastAsText = NULL;
741}
742
743static void DefaultHnd(void *userData, const char *s, int len)
744{
745 // XML header:
746 if (len > 6 && memcmp(s, "<?xml ", 6) == 0)
747 {
748 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
749
750 wxString buf = CharToString(ctx->conv, s, (size_t)len);
751 int pos;
e767076e 752 pos = buf.Find(wxS("encoding="));
27b0c286
VS
753 if (pos != wxNOT_FOUND)
754 ctx->encoding = buf.Mid(pos + 10).BeforeFirst(buf[(size_t)pos+9]);
e767076e 755 pos = buf.Find(wxS("version="));
27b0c286
VS
756 if (pos != wxNOT_FOUND)
757 ctx->version = buf.Mid(pos + 9).BeforeFirst(buf[(size_t)pos+8]);
758 }
759}
760
761static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData),
762 const XML_Char *name, XML_Encoding *info)
763{
764 // We must build conversion table for expat. The easiest way to do so
765 // is to let wxCSConv convert as string containing all characters to
766 // wide character representation:
86501081 767 wxCSConv conv(name);
27b0c286
VS
768 char mbBuf[2];
769 wchar_t wcBuf[10];
770 size_t i;
771
772 mbBuf[1] = 0;
773 info->map[0] = 0;
774 for (i = 0; i < 255; i++)
775 {
776 mbBuf[0] = (char)(i+1);
777 if (conv.MB2WC(wcBuf, mbBuf, 2) == (size_t)-1)
778 {
779 // invalid/undefined byte in the encoding:
780 info->map[i+1] = -1;
781 }
782 info->map[i+1] = (int)wcBuf[0];
783 }
42841dfc 784
27b0c286
VS
785 info->data = NULL;
786 info->convert = NULL;
787 info->release = NULL;
788
789 return 1;
790}
26296ac9
VS
791
792} // extern "C"
27b0c286 793
538f3830 794bool wxXmlDocument::Load(wxInputStream& stream, const wxString& encoding, int flags)
27b0c286
VS
795{
796#if wxUSE_UNICODE
797 (void)encoding;
798#else
799 m_encoding = encoding;
800#endif
801
802 const size_t BUFSIZE = 1024;
803 char buf[BUFSIZE];
804 wxXmlParsingContext ctx;
805 bool done;
806 XML_Parser parser = XML_ParserCreate(NULL);
cee8636e 807 wxXmlNode *root = new wxXmlNode(wxXML_DOCUMENT_NODE, wxEmptyString);
27b0c286 808
e767076e 809 ctx.encoding = wxS("UTF-8"); // default in absence of encoding=""
27b0c286
VS
810 ctx.conv = NULL;
811#if !wxUSE_UNICODE
e767076e 812 if ( encoding.CmpNoCase(wxS("UTF-8")) != 0 )
27b0c286
VS
813 ctx.conv = new wxCSConv(encoding);
814#endif
538f3830 815 ctx.removeWhiteOnlyNodes = (flags & wxXMLDOC_KEEP_WHITESPACE_NODES) == 0;
6e26d6b7 816 ctx.parser = parser;
cee8636e 817 ctx.node = root;
27b0c286
VS
818
819 XML_SetUserData(parser, (void*)&ctx);
820 XML_SetElementHandler(parser, StartElementHnd, EndElementHnd);
821 XML_SetCharacterDataHandler(parser, TextHnd);
a6cf6bcf 822 XML_SetCdataSectionHandler(parser, StartCdataHnd, EndCdataHnd);;
27b0c286 823 XML_SetCommentHandler(parser, CommentHnd);
cee8636e 824 XML_SetProcessingInstructionHandler(parser, PIHnd);
27b0c286
VS
825 XML_SetDefaultHandler(parser, DefaultHnd);
826 XML_SetUnknownEncodingHandler(parser, UnknownEncodingHnd, NULL);
827
828 bool ok = true;
829 do
830 {
831 size_t len = stream.Read(buf, BUFSIZE).LastRead();
832 done = (len < BUFSIZE);
833 if (!XML_Parse(parser, buf, len, done))
834 {
6a8fb6bd
VS
835 wxString error(XML_ErrorString(XML_GetErrorCode(parser)),
836 *wxConvCurrent);
27b0c286 837 wxLogError(_("XML parsing error: '%s' at line %d"),
6a8fb6bd 838 error.c_str(),
936759a4 839 (int)XML_GetCurrentLineNumber(parser));
27b0c286
VS
840 ok = false;
841 break;
842 }
843 } while (!done);
844
845 if (ok)
846 {
759f7272 847 if (!ctx.version.empty())
6a8fb6bd 848 SetVersion(ctx.version);
759f7272 849 if (!ctx.encoding.empty())
6a8fb6bd 850 SetFileEncoding(ctx.encoding);
cee8636e 851 SetDocumentNode(root);
27b0c286 852 }
6a8fb6bd
VS
853 else
854 {
cee8636e 855 delete root;
6a8fb6bd 856 }
27b0c286
VS
857
858 XML_ParserFree(parser);
859#if !wxUSE_UNICODE
860 if ( ctx.conv )
861 delete ctx.conv;
862#endif
863
864 return ok;
865
866}
867
868
869
870//-----------------------------------------------------------------------------
871// wxXmlDocument saving routines
872//-----------------------------------------------------------------------------
873
e767076e
VZ
874// helpers for XML generation
875namespace
876{
877
27b0c286 878// write string to output:
e767076e
VZ
879bool OutputString(wxOutputStream& stream,
880 const wxString& str,
881 wxMBConv *convMem,
882 wxMBConv *convFile)
27b0c286 883{
8dfd4fad 884 if (str.empty())
e767076e 885 return true;
8dfd4fad 886
27b0c286 887#if wxUSE_UNICODE
8dfd4fad 888 wxUnusedVar(convMem);
a6ed2786
VZ
889 if ( !convFile )
890 convFile = &wxConvUTF8;
f13d956e 891
a6ed2786
VZ
892 const wxScopedCharBuffer buf(str.mb_str(*convFile));
893 if ( !buf.length() )
894 {
895 // conversion failed, can't write this string in an XML file in this
896 // (presumably non-UTF-8) encoding
e767076e 897 return false;
a6ed2786 898 }
e767076e 899
a6ed2786 900 stream.Write(buf, buf.length());
8dfd4fad
VZ
901#else // !wxUSE_UNICODE
902 if ( convFile && convMem )
27b0c286
VS
903 {
904 wxString str2(str.wc_str(*convMem), *convFile);
e767076e 905 stream.Write(str2.mb_str(), str2.length());
27b0c286 906 }
8dfd4fad
VZ
907 else // no conversions to do
908 {
e767076e 909 stream.Write(str.mb_str(), str.length());
8dfd4fad
VZ
910 }
911#endif // wxUSE_UNICODE/!wxUSE_UNICODE
e767076e
VZ
912
913 return stream.IsOk();
27b0c286
VS
914}
915
926649a9 916enum EscapingMode
8dfd4fad 917{
926649a9
VS
918 Escape_Text,
919 Escape_Attribute
8dfd4fad
VZ
920};
921
27b0c286 922// Same as above, but create entities first.
926649a9
VS
923// Translates '<' to "&lt;", '>' to "&gt;" and so on, according to the spec:
924// http://www.w3.org/TR/2000/WD-xml-c14n-20000119.html#charescaping
925bool OutputEscapedString(wxOutputStream& stream,
926 const wxString& str,
927 wxMBConv *convMem,
928 wxMBConv *convFile,
929 EscapingMode mode)
930{
931 wxString escaped;
932 escaped.reserve(str.length());
e767076e 933
926649a9
VS
934 for ( wxString::const_iterator i = str.begin(); i != str.end(); ++i )
935 {
936 const wxChar c = *i;
e767076e 937
926649a9
VS
938 switch ( c )
939 {
940 case wxS('<'):
941 escaped.append(wxS("&lt;"));
942 break;
943 case wxS('>'):
944 escaped.append(wxS("&gt;"));
945 break;
946 case wxS('&'):
947 escaped.append(wxS("&amp;"));
948 break;
949 case wxS('\r'):
950 escaped.append(wxS("&#xD;"));
951 break;
952 default:
953 if ( mode == Escape_Attribute )
954 {
955 switch ( c )
956 {
957 case wxS('"'):
958 escaped.append(wxS("&quot;"));
959 break;
960 case wxS('\t'):
961 escaped.append(wxS("&#x9;"));
962 break;
963 case wxS('\n'):
964 escaped.append(wxS("&#xA;"));
965 break;
966 default:
967 escaped.append(c);
968 }
969
970 }
971 else
972 {
973 escaped.append(c);
974 }
27b0c286
VS
975 }
976 }
e767076e 977
926649a9 978 return OutputString(stream, escaped, convMem, convFile);
27b0c286
VS
979}
980
e767076e
VZ
981bool OutputIndentation(wxOutputStream& stream,
982 int indent,
983 wxMBConv *convMem,
984 wxMBConv *convFile)
27b0c286 985{
e767076e 986 wxString str(wxS("\n"));
57cc93eb 987 str += wxString(indent, wxS(' '));
e767076e 988 return OutputString(stream, str, convMem, convFile);
27b0c286
VS
989}
990
e767076e
VZ
991bool OutputNode(wxOutputStream& stream,
992 wxXmlNode *node,
993 int indent,
994 wxMBConv *convMem,
995 wxMBConv *convFile,
996 int indentstep)
27b0c286 997{
e767076e 998 bool rc;
27b0c286
VS
999 switch (node->GetType())
1000 {
eac789ff 1001 case wxXML_CDATA_SECTION_NODE:
e767076e
VZ
1002 rc = OutputString(stream, wxS("<![CDATA["), convMem, convFile) &&
1003 OutputString(stream, node->GetContent(), convMem, convFile) &&
1004 OutputString(stream, wxS("]]>"), convMem, convFile);
eac789ff 1005 break;
a01cbf0a 1006
27b0c286 1007 case wxXML_TEXT_NODE:
30f6914b
JS
1008 if (node->GetNoConversion())
1009 {
1010 stream.Write(node->GetContent().c_str(), node->GetContent().Length());
1011 rc = true;
1012 }
1013 else
1014 rc = OutputEscapedString(stream, node->GetContent(),
926649a9
VS
1015 convMem, convFile,
1016 Escape_Text);
27b0c286
VS
1017 break;
1018
1019 case wxXML_ELEMENT_NODE:
e767076e
VZ
1020 rc = OutputString(stream, wxS("<"), convMem, convFile) &&
1021 OutputString(stream, node->GetName(), convMem, convFile);
27b0c286 1022
e767076e 1023 if ( rc )
27b0c286 1024 {
e767076e
VZ
1025 for ( wxXmlAttribute *attr = node->GetAttributes();
1026 attr && rc;
1027 attr = attr->GetNext() )
1028 {
1029 rc = OutputString(stream,
1030 wxS(" ") + attr->GetName() + wxS("=\""),
1031 convMem, convFile) &&
926649a9
VS
1032 OutputEscapedString(stream, attr->GetValue(),
1033 convMem, convFile,
1034 Escape_Attribute) &&
e767076e
VZ
1035 OutputString(stream, wxS("\""), convMem, convFile);
1036 }
27b0c286
VS
1037 }
1038
e767076e 1039 if ( node->GetChildren() )
27b0c286 1040 {
e767076e
VZ
1041 rc = OutputString(stream, wxS(">"), convMem, convFile);
1042
1043 wxXmlNode *prev = NULL;
1044 for ( wxXmlNode *n = node->GetChildren();
1045 n && rc;
1046 n = n->GetNext() )
27b0c286 1047 {
e767076e
VZ
1048 if ( indentstep >= 0 && n->GetType() != wxXML_TEXT_NODE )
1049 {
1050 rc = OutputIndentation(stream, indent + indentstep,
1051 convMem, convFile);
1052 }
1053
1054 if ( rc )
1055 rc = OutputNode(stream, n, indent + indentstep,
1056 convMem, convFile, indentstep);
1057
27b0c286 1058 prev = n;
27b0c286 1059 }
e767076e
VZ
1060
1061 if ( rc && indentstep >= 0 &&
1062 prev && prev->GetType() != wxXML_TEXT_NODE )
1063 {
1064 rc = OutputIndentation(stream, indent, convMem, convFile);
1065 }
1066
1067 if ( rc )
1068 {
1069 rc = OutputString(stream, wxS("</"), convMem, convFile) &&
1070 OutputString(stream, node->GetName(),
1071 convMem, convFile) &&
1072 OutputString(stream, wxS(">"), convMem, convFile);
1073 }
1074 }
1075 else // no children, output "<foo/>"
1076 {
1077 rc = OutputString(stream, wxS("/>"), convMem, convFile);
27b0c286 1078 }
27b0c286
VS
1079 break;
1080
1081 case wxXML_COMMENT_NODE:
e767076e
VZ
1082 rc = OutputString(stream, wxS("<!--"), convMem, convFile) &&
1083 OutputString(stream, node->GetContent(), convMem, convFile) &&
1084 OutputString(stream, wxS("-->"), convMem, convFile);
27b0c286
VS
1085 break;
1086
cee8636e
VZ
1087 case wxXML_PI_NODE:
1088 rc = OutputString(stream, wxT("<?"), convMem, convFile) &&
1089 OutputString(stream, node->GetName(), convMem, convFile) &&
1090 OutputString(stream, wxT(" "), convMem, convFile) &&
1091 OutputString(stream, node->GetContent(), convMem, convFile) &&
1092 OutputString(stream, wxT("?>"), convMem, convFile);
1093 break;
1094
27b0c286 1095 default:
e767076e
VZ
1096 wxFAIL_MSG("unsupported node type");
1097 rc = false;
27b0c286 1098 }
e767076e
VZ
1099
1100 return rc;
27b0c286
VS
1101}
1102
e767076e
VZ
1103} // anonymous namespace
1104
538f3830 1105bool wxXmlDocument::Save(wxOutputStream& stream, int indentstep) const
27b0c286
VS
1106{
1107 if ( !IsOk() )
759f7272 1108 return false;
27b0c286 1109
e767076e 1110 wxScopedPtr<wxMBConv> convMem, convFile;
759f7272 1111
27b0c286 1112#if wxUSE_UNICODE
e767076e 1113 convFile.reset(new wxCSConv(GetFileEncoding()));
27b0c286 1114#else
8605f9c5 1115 if ( GetFileEncoding().CmpNoCase(GetEncoding()) != 0 )
27b0c286 1116 {
e767076e
VZ
1117 convFile.reset(new wxCSConv(GetFileEncoding()));
1118 convMem.reset(new wxCSConv(GetEncoding()));
8605f9c5 1119 }
e767076e 1120 //else: file and in-memory encodings are the same, no conversion needed
27b0c286
VS
1121#endif
1122
cee8636e
VZ
1123 wxString dec = wxString::Format(
1124 wxS("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
1125 GetVersion(), GetFileEncoding()
1126 );
1127 bool rc = OutputString(stream, dec, convMem.get(), convFile.get());
1128
1129 wxXmlNode *node = GetDocumentNode();
1130 if ( node )
1131 node = node->GetChildren();
1132
1133 while( rc && node )
1134 {
1135 rc = OutputNode(stream, node, 0, convMem.get(),
1136 convFile.get(), indentstep) &&
1137 OutputString(stream, wxS("\n"), convMem.get(), convFile.get());
1138 node = node->GetNext();
1139 }
1140 return rc;
27b0c286
VS
1141}
1142
ccec9093
VZ
1143/*static*/ wxVersionInfo wxXmlDocument::GetLibraryVersionInfo()
1144{
1145 return wxVersionInfo("expat",
1146 XML_MAJOR_VERSION,
1147 XML_MINOR_VERSION,
1148 XML_MICRO_VERSION);
1149}
1150
27b0c286 1151#endif // wxUSE_XML