]> git.saurik.com Git - wxWidgets.git/blame - src/xml/xml.cpp
typo fix
[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;
409};
410
865bb325 411extern "C" {
27b0c286
VS
412static void StartElementHnd(void *userData, const char *name, const char **atts)
413{
414 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
415 wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, CharToString(ctx->conv, name));
416 const char **a = atts;
417 while (*a)
418 {
419 node->AddProperty(CharToString(ctx->conv, a[0]), CharToString(ctx->conv, a[1]));
420 a += 2;
421 }
422 if (ctx->root == NULL)
423 ctx->root = node;
424 else
425 ctx->node->AddChild(node);
426 ctx->node = node;
427 ctx->lastAsText = NULL;
428}
865bb325 429}
27b0c286 430
865bb325 431extern "C" {
27b0c286
VS
432static void EndElementHnd(void *userData, const char* WXUNUSED(name))
433{
434 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
435
436 ctx->node = ctx->node->GetParent();
437 ctx->lastAsText = NULL;
438}
865bb325 439}
27b0c286 440
865bb325 441extern "C" {
27b0c286
VS
442static void TextHnd(void *userData, const char *s, int len)
443{
444 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
445 char *buf = new char[len + 1];
446
447 buf[len] = '\0';
448 memcpy(buf, s, (size_t)len);
449
450 if (ctx->lastAsText)
451 {
452 ctx->lastAsText->SetContent(ctx->lastAsText->GetContent() +
453 CharToString(ctx->conv, buf));
454 }
455 else
456 {
759f7272 457 bool whiteOnly = true;
27b0c286
VS
458 for (char *c = buf; *c != '\0'; c++)
459 if (*c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
460 {
759f7272 461 whiteOnly = false;
27b0c286
VS
462 break;
463 }
464 if (!whiteOnly)
465 {
466 ctx->lastAsText = new wxXmlNode(wxXML_TEXT_NODE, wxT("text"),
467 CharToString(ctx->conv, buf));
468 ctx->node->AddChild(ctx->lastAsText);
469 }
470 }
471
472 delete[] buf;
473}
865bb325 474}
27b0c286 475
865bb325 476extern "C" {
27b0c286
VS
477static void CommentHnd(void *userData, const char *data)
478{
479 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
480
481 if (ctx->node)
482 {
483 // VS: ctx->node == NULL happens if there is a comment before
484 // the root element (e.g. wxDesigner's output). We ignore such
485 // comments, no big deal...
486 ctx->node->AddChild(new wxXmlNode(wxXML_COMMENT_NODE,
487 wxT("comment"), CharToString(ctx->conv, data)));
488 }
489 ctx->lastAsText = NULL;
490}
865bb325 491}
27b0c286 492
865bb325 493extern "C" {
27b0c286
VS
494static void DefaultHnd(void *userData, const char *s, int len)
495{
496 // XML header:
497 if (len > 6 && memcmp(s, "<?xml ", 6) == 0)
498 {
499 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
500
501 wxString buf = CharToString(ctx->conv, s, (size_t)len);
502 int pos;
503 pos = buf.Find(wxT("encoding="));
504 if (pos != wxNOT_FOUND)
505 ctx->encoding = buf.Mid(pos + 10).BeforeFirst(buf[(size_t)pos+9]);
506 pos = buf.Find(wxT("version="));
507 if (pos != wxNOT_FOUND)
508 ctx->version = buf.Mid(pos + 9).BeforeFirst(buf[(size_t)pos+8]);
509 }
510}
865bb325 511}
27b0c286 512
865bb325 513extern "C" {
27b0c286
VS
514static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData),
515 const XML_Char *name, XML_Encoding *info)
516{
517 // We must build conversion table for expat. The easiest way to do so
518 // is to let wxCSConv convert as string containing all characters to
519 // wide character representation:
42841dfc
WS
520 wxString str(name, wxConvLibc);
521 wxCSConv conv(str);
27b0c286
VS
522 char mbBuf[2];
523 wchar_t wcBuf[10];
524 size_t i;
525
526 mbBuf[1] = 0;
527 info->map[0] = 0;
528 for (i = 0; i < 255; i++)
529 {
530 mbBuf[0] = (char)(i+1);
531 if (conv.MB2WC(wcBuf, mbBuf, 2) == (size_t)-1)
532 {
533 // invalid/undefined byte in the encoding:
534 info->map[i+1] = -1;
535 }
536 info->map[i+1] = (int)wcBuf[0];
537 }
42841dfc 538
27b0c286
VS
539 info->data = NULL;
540 info->convert = NULL;
541 info->release = NULL;
542
543 return 1;
544}
865bb325 545}
27b0c286
VS
546
547bool wxXmlDocument::Load(wxInputStream& stream, const wxString& encoding)
548{
549#if wxUSE_UNICODE
550 (void)encoding;
551#else
552 m_encoding = encoding;
553#endif
554
555 const size_t BUFSIZE = 1024;
556 char buf[BUFSIZE];
557 wxXmlParsingContext ctx;
558 bool done;
559 XML_Parser parser = XML_ParserCreate(NULL);
560
561 ctx.root = ctx.node = NULL;
562 ctx.encoding = wxT("UTF-8"); // default in absence of encoding=""
563 ctx.conv = NULL;
564#if !wxUSE_UNICODE
565 if ( encoding != wxT("UTF-8") && encoding != wxT("utf-8") )
566 ctx.conv = new wxCSConv(encoding);
567#endif
568
569 XML_SetUserData(parser, (void*)&ctx);
570 XML_SetElementHandler(parser, StartElementHnd, EndElementHnd);
571 XML_SetCharacterDataHandler(parser, TextHnd);
572 XML_SetCommentHandler(parser, CommentHnd);
573 XML_SetDefaultHandler(parser, DefaultHnd);
574 XML_SetUnknownEncodingHandler(parser, UnknownEncodingHnd, NULL);
575
576 bool ok = true;
577 do
578 {
579 size_t len = stream.Read(buf, BUFSIZE).LastRead();
580 done = (len < BUFSIZE);
581 if (!XML_Parse(parser, buf, len, done))
582 {
6a8fb6bd
VS
583 wxString error(XML_ErrorString(XML_GetErrorCode(parser)),
584 *wxConvCurrent);
27b0c286 585 wxLogError(_("XML parsing error: '%s' at line %d"),
6a8fb6bd 586 error.c_str(),
27b0c286
VS
587 XML_GetCurrentLineNumber(parser));
588 ok = false;
589 break;
590 }
591 } while (!done);
592
593 if (ok)
594 {
759f7272 595 if (!ctx.version.empty())
6a8fb6bd 596 SetVersion(ctx.version);
759f7272 597 if (!ctx.encoding.empty())
6a8fb6bd 598 SetFileEncoding(ctx.encoding);
27b0c286
VS
599 SetRoot(ctx.root);
600 }
6a8fb6bd
VS
601 else
602 {
603 delete ctx.root;
604 }
27b0c286
VS
605
606 XML_ParserFree(parser);
607#if !wxUSE_UNICODE
608 if ( ctx.conv )
609 delete ctx.conv;
610#endif
611
612 return ok;
613
614}
615
616
617
618//-----------------------------------------------------------------------------
619// wxXmlDocument saving routines
620//-----------------------------------------------------------------------------
621
622// write string to output:
623inline static void OutputString(wxOutputStream& stream, const wxString& str,
8dfd4fad
VZ
624 wxMBConv *convMem = NULL,
625 wxMBConv *convFile = NULL)
27b0c286 626{
8dfd4fad
VZ
627 if (str.empty())
628 return;
629
27b0c286 630#if wxUSE_UNICODE
8dfd4fad
VZ
631 wxUnusedVar(convMem);
632
6a8fb6bd 633 const wxWX2MBbuf buf(str.mb_str(*(convFile ? convFile : &wxConvUTF8)));
27b0c286 634 stream.Write((const char*)buf, strlen((const char*)buf));
8dfd4fad
VZ
635#else // !wxUSE_UNICODE
636 if ( convFile && convMem )
27b0c286
VS
637 {
638 wxString str2(str.wc_str(*convMem), *convFile);
639 stream.Write(str2.mb_str(), str2.Len());
640 }
8dfd4fad
VZ
641 else // no conversions to do
642 {
643 stream.Write(str.mb_str(), str.Len());
644 }
645#endif // wxUSE_UNICODE/!wxUSE_UNICODE
27b0c286
VS
646}
647
8dfd4fad
VZ
648// flags for OutputStringEnt()
649enum
650{
651 XML_ESCAPE_QUOTES = 1
652};
653
27b0c286
VS
654// Same as above, but create entities first.
655// Translates '<' to "&lt;", '>' to "&gt;" and '&' to "&amp;"
656static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
8dfd4fad
VZ
657 wxMBConv *convMem = NULL,
658 wxMBConv *convFile = NULL,
659 int flags = 0)
27b0c286
VS
660{
661 wxString buf;
662 size_t i, last, len;
663 wxChar c;
664
665 len = str.Len();
666 last = 0;
667 for (i = 0; i < len; i++)
668 {
669 c = str.GetChar(i);
670 if (c == wxT('<') || c == wxT('>') ||
ebf0700d 671 (c == wxT('&') && str.Mid(i+1, 4) != wxT("amp;")) ||
8dfd4fad 672 ((flags & XML_ESCAPE_QUOTES) && c == wxT('"')))
27b0c286
VS
673 {
674 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
675 switch (c)
676 {
677 case wxT('<'):
8dfd4fad 678 OutputString(stream, wxT("&lt;"));
27b0c286
VS
679 break;
680 case wxT('>'):
8dfd4fad 681 OutputString(stream, wxT("&gt;"));
27b0c286
VS
682 break;
683 case wxT('&'):
8dfd4fad 684 OutputString(stream, wxT("&amp;"));
27b0c286 685 break;
ebf0700d 686 case wxT('"'):
8dfd4fad
VZ
687 OutputString(stream, wxT("&quot;"));
688 break;
689 default:
ebf0700d 690 break;
27b0c286
VS
691 }
692 last = i + 1;
693 }
694 }
695 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
696}
697
698inline static void OutputIndentation(wxOutputStream& stream, int indent)
699{
700 wxString str = wxT("\n");
701 for (int i = 0; i < indent; i++)
702 str << wxT(' ') << wxT(' ');
8dfd4fad 703 OutputString(stream, str);
27b0c286
VS
704}
705
706static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent,
707 wxMBConv *convMem, wxMBConv *convFile)
708{
709 wxXmlNode *n, *prev;
710 wxXmlProperty *prop;
711
712 switch (node->GetType())
713 {
714 case wxXML_TEXT_NODE:
715 OutputStringEnt(stream, node->GetContent(), convMem, convFile);
716 break;
717
718 case wxXML_ELEMENT_NODE:
8dfd4fad
VZ
719 OutputString(stream, wxT("<"));
720 OutputString(stream, node->GetName());
27b0c286
VS
721
722 prop = node->GetProperties();
723 while (prop)
724 {
8dfd4fad 725 OutputString(stream, wxT(" ") + prop->GetName() + wxT("=\""));
24c80a28 726 OutputStringEnt(stream, prop->GetValue(), convMem, convFile,
8dfd4fad
VZ
727 XML_ESCAPE_QUOTES);
728 OutputString(stream, wxT("\""));
27b0c286
VS
729 prop = prop->GetNext();
730 }
731
732 if (node->GetChildren())
733 {
8dfd4fad 734 OutputString(stream, wxT(">"));
27b0c286
VS
735 prev = NULL;
736 n = node->GetChildren();
737 while (n)
738 {
739 if (n && n->GetType() != wxXML_TEXT_NODE)
740 OutputIndentation(stream, indent + 1);
741 OutputNode(stream, n, indent + 1, convMem, convFile);
742 prev = n;
743 n = n->GetNext();
744 }
745 if (prev && prev->GetType() != wxXML_TEXT_NODE)
746 OutputIndentation(stream, indent);
8dfd4fad
VZ
747 OutputString(stream, wxT("</"));
748 OutputString(stream, node->GetName());
749 OutputString(stream, wxT(">"));
27b0c286
VS
750 }
751 else
8dfd4fad 752 OutputString(stream, wxT("/>"));
27b0c286
VS
753 break;
754
755 case wxXML_COMMENT_NODE:
8dfd4fad 756 OutputString(stream, wxT("<!--"));
27b0c286 757 OutputString(stream, node->GetContent(), convMem, convFile);
8dfd4fad 758 OutputString(stream, wxT("-->"));
27b0c286
VS
759 break;
760
761 default:
762 wxFAIL_MSG(wxT("unsupported node type"));
763 }
764}
765
766bool wxXmlDocument::Save(wxOutputStream& stream) const
767{
768 if ( !IsOk() )
759f7272 769 return false;
27b0c286
VS
770
771 wxString s;
772
759f7272
WS
773 wxMBConv *convMem = NULL;
774
27b0c286 775#if wxUSE_UNICODE
759f7272 776 wxMBConv *convFile = new wxCSConv(GetFileEncoding());
27b0c286 777#else
759f7272 778 wxMBConv *convFile = NULL;
27b0c286
VS
779 if ( GetFileEncoding() != GetEncoding() )
780 {
781 convFile = new wxCSConv(GetFileEncoding());
782 convMem = new wxCSConv(GetEncoding());
783 }
784#endif
785
786 s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
787 GetVersion().c_str(), GetFileEncoding().c_str());
8dfd4fad 788 OutputString(stream, s);
27b0c286
VS
789
790 OutputNode(stream, GetRoot(), 0, convMem, convFile);
8dfd4fad 791 OutputString(stream, wxT("\n"));
27b0c286
VS
792
793 if ( convFile )
794 delete convFile;
795 if ( convMem )
796 delete convMem;
797
759f7272 798 return true;
27b0c286
VS
799}
800
801#endif // wxUSE_XML