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