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