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