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