]> git.saurik.com Git - wxWidgets.git/blame - src/xml/xml.cpp
Prevents crashes caused by negative line count being passed to wrapping functions...
[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
VS
31#include "wx/strconv.h"
32
33#include "expat.h" // from Expat
34
34fdf762 35// DLL options compatibility check:
34fdf762
VS
36WX_CHECK_BUILD_OPTIONS("wxXML")
37
4c43dd90
JS
38
39IMPLEMENT_CLASS(wxXmlDocument, wxObject)
40
41
42
27b0c286
VS
43//-----------------------------------------------------------------------------
44// wxXmlNode
45//-----------------------------------------------------------------------------
46
47wxXmlNode::wxXmlNode(wxXmlNode *parent,wxXmlNodeType type,
48 const wxString& name, const wxString& content,
49 wxXmlProperty *props, wxXmlNode *next)
50 : m_type(type), m_name(name), m_content(content),
51 m_properties(props), m_parent(parent),
52 m_children(NULL), m_next(next)
53{
54 if (m_parent)
55 {
56 if (m_parent->m_children)
57 {
58 m_next = m_parent->m_children;
59 m_parent->m_children = this;
60 }
61 else
62 m_parent->m_children = this;
63 }
64}
65
66wxXmlNode::wxXmlNode(wxXmlNodeType type, const wxString& name,
67 const wxString& content)
68 : m_type(type), m_name(name), m_content(content),
69 m_properties(NULL), m_parent(NULL),
70 m_children(NULL), m_next(NULL)
71{}
72
73wxXmlNode::wxXmlNode(const wxXmlNode& node)
74{
75 m_next = NULL;
76 m_parent = NULL;
77 DoCopy(node);
78}
79
80wxXmlNode::~wxXmlNode()
81{
82 wxXmlNode *c, *c2;
83 for (c = m_children; c; c = c2)
84 {
85 c2 = c->m_next;
86 delete c;
87 }
88
89 wxXmlProperty *p, *p2;
90 for (p = m_properties; p; p = p2)
91 {
92 p2 = p->GetNext();
93 delete p;
94 }
95}
96
97wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node)
98{
99 wxDELETE(m_properties);
100 wxDELETE(m_children);
101 DoCopy(node);
102 return *this;
103}
104
105void wxXmlNode::DoCopy(const wxXmlNode& node)
106{
107 m_type = node.m_type;
108 m_name = node.m_name;
109 m_content = node.m_content;
110 m_children = NULL;
111
112 wxXmlNode *n = node.m_children;
113 while (n)
114 {
115 AddChild(new wxXmlNode(*n));
116 n = n->GetNext();
117 }
118
119 m_properties = NULL;
120 wxXmlProperty *p = node.m_properties;
121 while (p)
122 {
123 AddProperty(p->GetName(), p->GetValue());
124 p = p->GetNext();
125 }
126}
127
128bool wxXmlNode::HasProp(const wxString& propName) const
129{
130 wxXmlProperty *prop = GetProperties();
131
132 while (prop)
133 {
759f7272 134 if (prop->GetName() == propName) return true;
27b0c286
VS
135 prop = prop->GetNext();
136 }
137
759f7272 138 return false;
27b0c286
VS
139}
140
141bool wxXmlNode::GetPropVal(const wxString& propName, wxString *value) const
142{
143 wxXmlProperty *prop = GetProperties();
144
145 while (prop)
146 {
147 if (prop->GetName() == propName)
148 {
149 *value = prop->GetValue();
759f7272 150 return true;
27b0c286
VS
151 }
152 prop = prop->GetNext();
153 }
154
759f7272 155 return false;
27b0c286
VS
156}
157
158wxString wxXmlNode::GetPropVal(const wxString& propName, const wxString& defaultVal) const
159{
160 wxString tmp;
161 if (GetPropVal(propName, &tmp))
162 return tmp;
0e2710a6
DS
163
164 return defaultVal;
27b0c286
VS
165}
166
167void wxXmlNode::AddChild(wxXmlNode *child)
168{
169 if (m_children == NULL)
170 m_children = child;
171 else
172 {
173 wxXmlNode *ch = m_children;
174 while (ch->m_next) ch = ch->m_next;
175 ch->m_next = child;
176 }
177 child->m_next = NULL;
178 child->m_parent = this;
179}
180
181void wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *before_node)
182{
183 wxASSERT_MSG(before_node->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
184
185 if (m_children == before_node)
186 m_children = child;
187 else
188 {
189 wxXmlNode *ch = m_children;
190 while (ch->m_next != before_node) ch = ch->m_next;
191 ch->m_next = child;
192 }
193
194 child->m_parent = this;
195 child->m_next = before_node;
196}
197
198bool wxXmlNode::RemoveChild(wxXmlNode *child)
199{
200 if (m_children == NULL)
759f7272 201 return false;
27b0c286
VS
202 else if (m_children == child)
203 {
204 m_children = child->m_next;
205 child->m_parent = NULL;
206 child->m_next = NULL;
759f7272 207 return true;
27b0c286
VS
208 }
209 else
210 {
211 wxXmlNode *ch = m_children;
212 while (ch->m_next)
213 {
214 if (ch->m_next == child)
215 {
216 ch->m_next = child->m_next;
217 child->m_parent = NULL;
218 child->m_next = NULL;
759f7272 219 return true;
27b0c286
VS
220 }
221 ch = ch->m_next;
222 }
759f7272 223 return false;
27b0c286
VS
224 }
225}
226
227void wxXmlNode::AddProperty(const wxString& name, const wxString& value)
228{
229 AddProperty(new wxXmlProperty(name, value, NULL));
230}
231
232void wxXmlNode::AddProperty(wxXmlProperty *prop)
233{
234 if (m_properties == NULL)
235 m_properties = prop;
236 else
237 {
238 wxXmlProperty *p = m_properties;
239 while (p->GetNext()) p = p->GetNext();
240 p->SetNext(prop);
241 }
242}
243
244bool wxXmlNode::DeleteProperty(const wxString& name)
245{
246 wxXmlProperty *prop;
247
248 if (m_properties == NULL)
759f7272 249 return false;
27b0c286
VS
250
251 else if (m_properties->GetName() == name)
252 {
253 prop = m_properties;
254 m_properties = prop->GetNext();
255 prop->SetNext(NULL);
256 delete prop;
759f7272 257 return true;
27b0c286
VS
258 }
259
260 else
261 {
262 wxXmlProperty *p = m_properties;
263 while (p->GetNext())
264 {
265 if (p->GetNext()->GetName() == name)
266 {
267 prop = p->GetNext();
268 p->SetNext(prop->GetNext());
269 prop->SetNext(NULL);
270 delete prop;
759f7272 271 return true;
27b0c286
VS
272 }
273 p = p->GetNext();
274 }
759f7272 275 return false;
27b0c286
VS
276 }
277}
278
4c43dd90
JS
279wxString wxXmlNode::GetNodeContent() const
280{
281 wxXmlNode *n = GetChildren();
282
283 while (n)
284 {
285 if (n->GetType() == wxXML_TEXT_NODE ||
286 n->GetType() == wxXML_CDATA_SECTION_NODE)
287 return n->GetContent();
288 n = n->GetNext();
289 }
290 return wxEmptyString;
291}
292
27b0c286
VS
293
294
295//-----------------------------------------------------------------------------
296// wxXmlDocument
297//-----------------------------------------------------------------------------
298
299wxXmlDocument::wxXmlDocument()
300 : m_version(wxT("1.0")), m_fileEncoding(wxT("utf-8")), m_root(NULL)
301{
302#if !wxUSE_UNICODE
303 m_encoding = wxT("UTF-8");
304#endif
305}
306
307wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding)
d0468e8c 308 :wxObject(), m_root(NULL)
27b0c286
VS
309{
310 if ( !Load(filename, encoding) )
311 {
312 wxDELETE(m_root);
313 }
314}
315
316wxXmlDocument::wxXmlDocument(wxInputStream& stream, const wxString& encoding)
d0468e8c 317 :wxObject(), m_root(NULL)
27b0c286
VS
318{
319 if ( !Load(stream, encoding) )
320 {
321 wxDELETE(m_root);
322 }
323}
324
325wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc)
d0468e8c 326 :wxObject()
27b0c286
VS
327{
328 DoCopy(doc);
329}
330
331wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc)
332{
333 wxDELETE(m_root);
334 DoCopy(doc);
335 return *this;
336}
337
338void wxXmlDocument::DoCopy(const wxXmlDocument& doc)
339{
340 m_version = doc.m_version;
341#if !wxUSE_UNICODE
342 m_encoding = doc.m_encoding;
343#endif
344 m_fileEncoding = doc.m_fileEncoding;
345 m_root = new wxXmlNode(*doc.m_root);
346}
347
348bool wxXmlDocument::Load(const wxString& filename, const wxString& encoding)
349{
350 wxFileInputStream stream(filename);
97757cee
VZ
351 if (!stream.Ok())
352 return false;
27b0c286
VS
353 return Load(stream, encoding);
354}
355
356bool wxXmlDocument::Save(const wxString& filename) const
357{
358 wxFileOutputStream stream(filename);
97757cee
VZ
359 if (!stream.Ok())
360 return false;
27b0c286
VS
361 return Save(stream);
362}
363
364
365
366//-----------------------------------------------------------------------------
367// wxXmlDocument loading routines
368//-----------------------------------------------------------------------------
369
370/*
371 FIXME:
372 - process all elements, including CDATA
373 */
374
ddae497f
VZ
375// converts Expat-produced string in UTF-8 into wxString using the specified
376// conv or keep in UTF-8 if conv is NULL
377static wxString CharToString(wxMBConv *conv,
27b0c286
VS
378 const char *s, size_t len = wxSTRING_MAXLEN)
379{
380#if wxUSE_UNICODE
ddae497f
VZ
381 wxUnusedVar(conv);
382
27b0c286 383 return wxString(s, wxConvUTF8, len);
ddae497f 384#else // !wxUSE_UNICODE
27b0c286
VS
385 if ( conv )
386 {
ddae497f
VZ
387 // there can be no embedded NULs in this string so we don't need the
388 // output length, it will be NUL-terminated
389 const wxWCharBuffer wbuf(
390 wxConvUTF8.cMB2WC(s, len == wxSTRING_MAXLEN ? wxNO_LEN : len, NULL));
391
da452b9e 392 return wxString(wbuf, *conv);
27b0c286 393 }
ddae497f
VZ
394 else // already in UTF-8, no conversion needed
395 {
088ab984 396 return wxString(s, len != wxSTRING_MAXLEN ? len : strlen(s));
ddae497f
VZ
397 }
398#endif // wxUSE_UNICODE/!wxUSE_UNICODE
27b0c286
VS
399}
400
401struct wxXmlParsingContext
402{
403 wxMBConv *conv;
404 wxXmlNode *root;
405 wxXmlNode *node;
406 wxXmlNode *lastAsText;
407 wxString encoding;
408 wxString version;
a01cbf0a 409 bool bLastCdata;
27b0c286
VS
410};
411
865bb325 412extern "C" {
27b0c286
VS
413static void StartElementHnd(void *userData, const char *name, const char **atts)
414{
415 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
416 wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, CharToString(ctx->conv, name));
417 const char **a = atts;
418 while (*a)
419 {
420 node->AddProperty(CharToString(ctx->conv, a[0]), CharToString(ctx->conv, a[1]));
421 a += 2;
422 }
423 if (ctx->root == NULL)
424 ctx->root = node;
425 else
426 ctx->node->AddChild(node);
427 ctx->node = node;
428 ctx->lastAsText = NULL;
429}
865bb325 430}
27b0c286 431
865bb325 432extern "C" {
27b0c286
VS
433static void EndElementHnd(void *userData, const char* WXUNUSED(name))
434{
435 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
436
437 ctx->node = ctx->node->GetParent();
438 ctx->lastAsText = NULL;
439}
865bb325 440}
27b0c286 441
865bb325 442extern "C" {
27b0c286
VS
443static void TextHnd(void *userData, const char *s, int len)
444{
445 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
446 char *buf = new char[len + 1];
447
448 buf[len] = '\0';
449 memcpy(buf, s, (size_t)len);
450
451 if (ctx->lastAsText)
452 {
a01cbf0a
RR
453 if ( ctx->bLastCdata )
454 {
455 ctx->lastAsText->SetContent(ctx->lastAsText->GetContent() +
456 CharToString(NULL, buf));
457 }
458 else
459 {
460 ctx->lastAsText->SetContent(ctx->lastAsText->GetContent() +
27b0c286 461 CharToString(ctx->conv, buf));
a01cbf0a 462 }
27b0c286
VS
463 }
464 else
465 {
759f7272 466 bool whiteOnly = true;
27b0c286
VS
467 for (char *c = buf; *c != '\0'; c++)
468 if (*c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
469 {
759f7272 470 whiteOnly = false;
27b0c286
VS
471 break;
472 }
473 if (!whiteOnly)
474 {
475 ctx->lastAsText = new wxXmlNode(wxXML_TEXT_NODE, wxT("text"),
476 CharToString(ctx->conv, buf));
477 ctx->node->AddChild(ctx->lastAsText);
478 }
479 }
480
481 delete[] buf;
482}
865bb325 483}
27b0c286 484
a01cbf0a
RR
485extern "C" {
486static void StartCdataHnd(void *userData)
487{
488 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
489
490 ctx->bLastCdata = true;
491
492 ctx->lastAsText = new wxXmlNode(wxXML_CDATA_SECTION_NODE, wxT("cdata"),wxT(""));
493 ctx->node->AddChild(ctx->lastAsText);
494}
495}
496
497extern "C" {
498static void EndCdataHnd(void *userData)
499{
500 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
501
502 ctx->bLastCdata = false;
503}
504}
505
865bb325 506extern "C" {
27b0c286
VS
507static void CommentHnd(void *userData, const char *data)
508{
509 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
510
511 if (ctx->node)
512 {
513 // VS: ctx->node == NULL happens if there is a comment before
514 // the root element (e.g. wxDesigner's output). We ignore such
515 // comments, no big deal...
516 ctx->node->AddChild(new wxXmlNode(wxXML_COMMENT_NODE,
517 wxT("comment"), CharToString(ctx->conv, data)));
518 }
519 ctx->lastAsText = NULL;
520}
865bb325 521}
27b0c286 522
865bb325 523extern "C" {
27b0c286
VS
524static void DefaultHnd(void *userData, const char *s, int len)
525{
526 // XML header:
527 if (len > 6 && memcmp(s, "<?xml ", 6) == 0)
528 {
529 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
530
531 wxString buf = CharToString(ctx->conv, s, (size_t)len);
532 int pos;
533 pos = buf.Find(wxT("encoding="));
534 if (pos != wxNOT_FOUND)
535 ctx->encoding = buf.Mid(pos + 10).BeforeFirst(buf[(size_t)pos+9]);
536 pos = buf.Find(wxT("version="));
537 if (pos != wxNOT_FOUND)
538 ctx->version = buf.Mid(pos + 9).BeforeFirst(buf[(size_t)pos+8]);
539 }
540}
865bb325 541}
27b0c286 542
865bb325 543extern "C" {
27b0c286
VS
544static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData),
545 const XML_Char *name, XML_Encoding *info)
546{
547 // We must build conversion table for expat. The easiest way to do so
548 // is to let wxCSConv convert as string containing all characters to
549 // wide character representation:
42841dfc
WS
550 wxString str(name, wxConvLibc);
551 wxCSConv conv(str);
27b0c286
VS
552 char mbBuf[2];
553 wchar_t wcBuf[10];
554 size_t i;
555
556 mbBuf[1] = 0;
557 info->map[0] = 0;
558 for (i = 0; i < 255; i++)
559 {
560 mbBuf[0] = (char)(i+1);
561 if (conv.MB2WC(wcBuf, mbBuf, 2) == (size_t)-1)
562 {
563 // invalid/undefined byte in the encoding:
564 info->map[i+1] = -1;
565 }
566 info->map[i+1] = (int)wcBuf[0];
567 }
42841dfc 568
27b0c286
VS
569 info->data = NULL;
570 info->convert = NULL;
571 info->release = NULL;
572
573 return 1;
574}
865bb325 575}
27b0c286
VS
576
577bool wxXmlDocument::Load(wxInputStream& stream, const wxString& encoding)
578{
579#if wxUSE_UNICODE
580 (void)encoding;
581#else
582 m_encoding = encoding;
583#endif
584
585 const size_t BUFSIZE = 1024;
586 char buf[BUFSIZE];
587 wxXmlParsingContext ctx;
588 bool done;
589 XML_Parser parser = XML_ParserCreate(NULL);
590
591 ctx.root = ctx.node = NULL;
592 ctx.encoding = wxT("UTF-8"); // default in absence of encoding=""
593 ctx.conv = NULL;
594#if !wxUSE_UNICODE
595 if ( encoding != wxT("UTF-8") && encoding != wxT("utf-8") )
596 ctx.conv = new wxCSConv(encoding);
597#endif
a01cbf0a 598 ctx.bLastCdata = false;
27b0c286
VS
599
600 XML_SetUserData(parser, (void*)&ctx);
601 XML_SetElementHandler(parser, StartElementHnd, EndElementHnd);
602 XML_SetCharacterDataHandler(parser, TextHnd);
a01cbf0a 603 XML_SetCdataSectionHandler(parser, StartCdataHnd, EndCdataHnd );
27b0c286
VS
604 XML_SetCommentHandler(parser, CommentHnd);
605 XML_SetDefaultHandler(parser, DefaultHnd);
606 XML_SetUnknownEncodingHandler(parser, UnknownEncodingHnd, NULL);
607
608 bool ok = true;
609 do
610 {
611 size_t len = stream.Read(buf, BUFSIZE).LastRead();
612 done = (len < BUFSIZE);
613 if (!XML_Parse(parser, buf, len, done))
614 {
6a8fb6bd
VS
615 wxString error(XML_ErrorString(XML_GetErrorCode(parser)),
616 *wxConvCurrent);
27b0c286 617 wxLogError(_("XML parsing error: '%s' at line %d"),
6a8fb6bd 618 error.c_str(),
27b0c286
VS
619 XML_GetCurrentLineNumber(parser));
620 ok = false;
621 break;
622 }
623 } while (!done);
624
625 if (ok)
626 {
759f7272 627 if (!ctx.version.empty())
6a8fb6bd 628 SetVersion(ctx.version);
759f7272 629 if (!ctx.encoding.empty())
6a8fb6bd 630 SetFileEncoding(ctx.encoding);
27b0c286
VS
631 SetRoot(ctx.root);
632 }
6a8fb6bd
VS
633 else
634 {
635 delete ctx.root;
636 }
27b0c286
VS
637
638 XML_ParserFree(parser);
639#if !wxUSE_UNICODE
640 if ( ctx.conv )
641 delete ctx.conv;
642#endif
643
644 return ok;
645
646}
647
648
649
650//-----------------------------------------------------------------------------
651// wxXmlDocument saving routines
652//-----------------------------------------------------------------------------
653
654// write string to output:
655inline static void OutputString(wxOutputStream& stream, const wxString& str,
8dfd4fad
VZ
656 wxMBConv *convMem = NULL,
657 wxMBConv *convFile = NULL)
27b0c286 658{
8dfd4fad
VZ
659 if (str.empty())
660 return;
661
27b0c286 662#if wxUSE_UNICODE
8dfd4fad
VZ
663 wxUnusedVar(convMem);
664
6a8fb6bd 665 const wxWX2MBbuf buf(str.mb_str(*(convFile ? convFile : &wxConvUTF8)));
27b0c286 666 stream.Write((const char*)buf, strlen((const char*)buf));
8dfd4fad
VZ
667#else // !wxUSE_UNICODE
668 if ( convFile && convMem )
27b0c286
VS
669 {
670 wxString str2(str.wc_str(*convMem), *convFile);
671 stream.Write(str2.mb_str(), str2.Len());
672 }
8dfd4fad
VZ
673 else // no conversions to do
674 {
675 stream.Write(str.mb_str(), str.Len());
676 }
677#endif // wxUSE_UNICODE/!wxUSE_UNICODE
27b0c286
VS
678}
679
8dfd4fad
VZ
680// flags for OutputStringEnt()
681enum
682{
683 XML_ESCAPE_QUOTES = 1
684};
685
27b0c286
VS
686// Same as above, but create entities first.
687// Translates '<' to "&lt;", '>' to "&gt;" and '&' to "&amp;"
688static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
8dfd4fad
VZ
689 wxMBConv *convMem = NULL,
690 wxMBConv *convFile = NULL,
691 int flags = 0)
27b0c286
VS
692{
693 wxString buf;
694 size_t i, last, len;
695 wxChar c;
696
697 len = str.Len();
698 last = 0;
699 for (i = 0; i < len; i++)
700 {
701 c = str.GetChar(i);
702 if (c == wxT('<') || c == wxT('>') ||
ebf0700d 703 (c == wxT('&') && str.Mid(i+1, 4) != wxT("amp;")) ||
8dfd4fad 704 ((flags & XML_ESCAPE_QUOTES) && c == wxT('"')))
27b0c286
VS
705 {
706 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
707 switch (c)
708 {
709 case wxT('<'):
8dfd4fad 710 OutputString(stream, wxT("&lt;"));
27b0c286
VS
711 break;
712 case wxT('>'):
8dfd4fad 713 OutputString(stream, wxT("&gt;"));
27b0c286
VS
714 break;
715 case wxT('&'):
8dfd4fad 716 OutputString(stream, wxT("&amp;"));
27b0c286 717 break;
ebf0700d 718 case wxT('"'):
8dfd4fad
VZ
719 OutputString(stream, wxT("&quot;"));
720 break;
721 default:
ebf0700d 722 break;
27b0c286
VS
723 }
724 last = i + 1;
725 }
726 }
727 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
728}
729
730inline static void OutputIndentation(wxOutputStream& stream, int indent)
731{
732 wxString str = wxT("\n");
733 for (int i = 0; i < indent; i++)
734 str << wxT(' ') << wxT(' ');
8dfd4fad 735 OutputString(stream, str);
27b0c286
VS
736}
737
738static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent,
739 wxMBConv *convMem, wxMBConv *convFile)
740{
741 wxXmlNode *n, *prev;
742 wxXmlProperty *prop;
743
744 switch (node->GetType())
745 {
a01cbf0a
RR
746 case wxXML_CDATA_SECTION_NODE:
747 OutputString( stream, wxT("<![CDATA["));
748 OutputString( stream, node->GetContent() );
749 OutputString( stream, wxT("]]>") );
750 break;
751
27b0c286
VS
752 case wxXML_TEXT_NODE:
753 OutputStringEnt(stream, node->GetContent(), convMem, convFile);
754 break;
755
756 case wxXML_ELEMENT_NODE:
8dfd4fad
VZ
757 OutputString(stream, wxT("<"));
758 OutputString(stream, node->GetName());
27b0c286
VS
759
760 prop = node->GetProperties();
761 while (prop)
762 {
8dfd4fad 763 OutputString(stream, wxT(" ") + prop->GetName() + wxT("=\""));
24c80a28 764 OutputStringEnt(stream, prop->GetValue(), convMem, convFile,
8dfd4fad
VZ
765 XML_ESCAPE_QUOTES);
766 OutputString(stream, wxT("\""));
27b0c286
VS
767 prop = prop->GetNext();
768 }
769
770 if (node->GetChildren())
771 {
8dfd4fad 772 OutputString(stream, wxT(">"));
27b0c286
VS
773 prev = NULL;
774 n = node->GetChildren();
775 while (n)
776 {
777 if (n && n->GetType() != wxXML_TEXT_NODE)
778 OutputIndentation(stream, indent + 1);
779 OutputNode(stream, n, indent + 1, convMem, convFile);
780 prev = n;
781 n = n->GetNext();
782 }
783 if (prev && prev->GetType() != wxXML_TEXT_NODE)
784 OutputIndentation(stream, indent);
8dfd4fad
VZ
785 OutputString(stream, wxT("</"));
786 OutputString(stream, node->GetName());
787 OutputString(stream, wxT(">"));
27b0c286
VS
788 }
789 else
8dfd4fad 790 OutputString(stream, wxT("/>"));
27b0c286
VS
791 break;
792
793 case wxXML_COMMENT_NODE:
8dfd4fad 794 OutputString(stream, wxT("<!--"));
27b0c286 795 OutputString(stream, node->GetContent(), convMem, convFile);
8dfd4fad 796 OutputString(stream, wxT("-->"));
27b0c286
VS
797 break;
798
799 default:
800 wxFAIL_MSG(wxT("unsupported node type"));
801 }
802}
803
804bool wxXmlDocument::Save(wxOutputStream& stream) const
805{
806 if ( !IsOk() )
759f7272 807 return false;
27b0c286
VS
808
809 wxString s;
810
759f7272
WS
811 wxMBConv *convMem = NULL;
812
27b0c286 813#if wxUSE_UNICODE
759f7272 814 wxMBConv *convFile = new wxCSConv(GetFileEncoding());
27b0c286 815#else
759f7272 816 wxMBConv *convFile = NULL;
27b0c286
VS
817 if ( GetFileEncoding() != GetEncoding() )
818 {
819 convFile = new wxCSConv(GetFileEncoding());
820 convMem = new wxCSConv(GetEncoding());
821 }
822#endif
823
824 s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
825 GetVersion().c_str(), GetFileEncoding().c_str());
8dfd4fad 826 OutputString(stream, s);
27b0c286
VS
827
828 OutputNode(stream, GetRoot(), 0, convMem, convFile);
8dfd4fad 829 OutputString(stream, wxT("\n"));
27b0c286
VS
830
831 if ( convFile )
832 delete convFile;
833 if ( convMem )
834 delete convMem;
835
759f7272 836 return true;
27b0c286
VS
837}
838
839#endif // wxUSE_XML