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