]> git.saurik.com Git - wxWidgets.git/blob - src/xml/xml.cpp
Don't rely on RVO in wxON_BLOCK_EXIT_SET().
[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 #include "wx/scopedptr.h"
33 #include "wx/versioninfo.h"
34
35 #include "expat.h" // from Expat
36
37 // DLL options compatibility check:
38 WX_CHECK_BUILD_OPTIONS("wxXML")
39
40
41 IMPLEMENT_CLASS(wxXmlDocument, wxObject)
42
43
44 // a private utility used by wxXML
45 static bool wxIsWhiteOnly(const wxString& buf);
46
47
48 //-----------------------------------------------------------------------------
49 // wxXmlNode
50 //-----------------------------------------------------------------------------
51
52 wxXmlNode::wxXmlNode(wxXmlNode *parent,wxXmlNodeType type,
53 const wxString& name, const wxString& content,
54 wxXmlAttribute *attrs, wxXmlNode *next, int lineNo)
55 : m_type(type), m_name(name), m_content(content),
56 m_attrs(attrs), m_parent(parent),
57 m_children(NULL), m_next(next),
58 m_lineNo(lineNo),
59 m_noConversion(false)
60 {
61 if (m_parent)
62 {
63 if (m_parent->m_children)
64 {
65 m_next = m_parent->m_children;
66 m_parent->m_children = this;
67 }
68 else
69 m_parent->m_children = this;
70 }
71 }
72
73 wxXmlNode::wxXmlNode(wxXmlNodeType type, const wxString& name,
74 const wxString& content,
75 int lineNo)
76 : m_type(type), m_name(name), m_content(content),
77 m_attrs(NULL), m_parent(NULL),
78 m_children(NULL), m_next(NULL),
79 m_lineNo(lineNo), m_noConversion(false)
80 {}
81
82 wxXmlNode::wxXmlNode(const wxXmlNode& node)
83 {
84 m_next = NULL;
85 m_parent = NULL;
86 DoCopy(node);
87 }
88
89 wxXmlNode::~wxXmlNode()
90 {
91 wxXmlNode *c, *c2;
92 for (c = m_children; c; c = c2)
93 {
94 c2 = c->m_next;
95 delete c;
96 }
97
98 wxXmlAttribute *p, *p2;
99 for (p = m_attrs; p; p = p2)
100 {
101 p2 = p->GetNext();
102 delete p;
103 }
104 }
105
106 wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node)
107 {
108 wxDELETE(m_attrs);
109 wxDELETE(m_children);
110 DoCopy(node);
111 return *this;
112 }
113
114 void wxXmlNode::DoCopy(const wxXmlNode& node)
115 {
116 m_type = node.m_type;
117 m_name = node.m_name;
118 m_content = node.m_content;
119 m_lineNo = node.m_lineNo;
120 m_noConversion = node.m_noConversion;
121 m_children = NULL;
122
123 wxXmlNode *n = node.m_children;
124 while (n)
125 {
126 AddChild(new wxXmlNode(*n));
127 n = n->GetNext();
128 }
129
130 m_attrs = NULL;
131 wxXmlAttribute *p = node.m_attrs;
132 while (p)
133 {
134 AddAttribute(p->GetName(), p->GetValue());
135 p = p->GetNext();
136 }
137 }
138
139 bool wxXmlNode::HasAttribute(const wxString& attrName) const
140 {
141 wxXmlAttribute *attr = GetAttributes();
142
143 while (attr)
144 {
145 if (attr->GetName() == attrName) return true;
146 attr = attr->GetNext();
147 }
148
149 return false;
150 }
151
152 bool wxXmlNode::GetAttribute(const wxString& attrName, wxString *value) const
153 {
154 wxCHECK_MSG( value, false, "value argument must not be NULL" );
155
156 wxXmlAttribute *attr = GetAttributes();
157
158 while (attr)
159 {
160 if (attr->GetName() == attrName)
161 {
162 *value = attr->GetValue();
163 return true;
164 }
165 attr = attr->GetNext();
166 }
167
168 return false;
169 }
170
171 wxString wxXmlNode::GetAttribute(const wxString& attrName, const wxString& defaultVal) const
172 {
173 wxString tmp;
174 if (GetAttribute(attrName, &tmp))
175 return tmp;
176
177 return defaultVal;
178 }
179
180 void wxXmlNode::AddChild(wxXmlNode *child)
181 {
182 if (m_children == NULL)
183 m_children = child;
184 else
185 {
186 wxXmlNode *ch = m_children;
187 while (ch->m_next) ch = ch->m_next;
188 ch->m_next = child;
189 }
190 child->m_next = NULL;
191 child->m_parent = this;
192 }
193
194 // inserts a new node in front of 'followingNode'
195 bool wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *followingNode)
196 {
197 wxCHECK_MSG( child, false, "cannot insert a NULL node!" );
198 wxCHECK_MSG( child->m_parent == NULL, false, "node already has a parent" );
199 wxCHECK_MSG( child->m_next == NULL, false, "node already has m_next" );
200 wxCHECK_MSG( followingNode == NULL || followingNode->GetParent() == this,
201 false,
202 "wxXmlNode::InsertChild - followingNode has incorrect parent" );
203
204 // this is for backward compatibility, NULL was allowed here thanks to
205 // the confusion about followingNode's meaning
206 if ( followingNode == NULL )
207 followingNode = m_children;
208
209 if ( m_children == followingNode )
210 {
211 child->m_next = m_children;
212 m_children = child;
213 }
214 else
215 {
216 wxXmlNode *ch = m_children;
217 while ( ch && ch->m_next != followingNode )
218 ch = ch->m_next;
219 if ( !ch )
220 {
221 wxFAIL_MSG( "followingNode has this node as parent, but couldn't be found among children" );
222 return false;
223 }
224
225 child->m_next = followingNode;
226 ch->m_next = child;
227 }
228
229 child->m_parent = this;
230 return true;
231 }
232
233 // inserts a new node right after 'precedingNode'
234 bool wxXmlNode::InsertChildAfter(wxXmlNode *child, wxXmlNode *precedingNode)
235 {
236 wxCHECK_MSG( child, false, "cannot insert a NULL node!" );
237 wxCHECK_MSG( child->m_parent == NULL, false, "node already has a parent" );
238 wxCHECK_MSG( child->m_next == NULL, false, "node already has m_next" );
239 wxCHECK_MSG( precedingNode == NULL || precedingNode->m_parent == this, false,
240 "precedingNode has wrong parent" );
241
242 if ( precedingNode )
243 {
244 child->m_next = precedingNode->m_next;
245 precedingNode->m_next = child;
246 }
247 else // precedingNode == NULL
248 {
249 wxCHECK_MSG( m_children == NULL, false,
250 "NULL precedingNode only makes sense when there are no children" );
251
252 child->m_next = m_children;
253 m_children = child;
254 }
255
256 child->m_parent = this;
257 return true;
258 }
259
260 bool wxXmlNode::RemoveChild(wxXmlNode *child)
261 {
262 if (m_children == NULL)
263 return false;
264 else if (m_children == child)
265 {
266 m_children = child->m_next;
267 child->m_parent = NULL;
268 child->m_next = NULL;
269 return true;
270 }
271 else
272 {
273 wxXmlNode *ch = m_children;
274 while (ch->m_next)
275 {
276 if (ch->m_next == child)
277 {
278 ch->m_next = child->m_next;
279 child->m_parent = NULL;
280 child->m_next = NULL;
281 return true;
282 }
283 ch = ch->m_next;
284 }
285 return false;
286 }
287 }
288
289 void wxXmlNode::AddAttribute(const wxString& name, const wxString& value)
290 {
291 AddProperty(name, value);
292 }
293
294 void wxXmlNode::AddAttribute(wxXmlAttribute *attr)
295 {
296 AddProperty(attr);
297 }
298
299 bool wxXmlNode::DeleteAttribute(const wxString& name)
300 {
301 return DeleteProperty(name);
302 }
303
304 void wxXmlNode::AddProperty(const wxString& name, const wxString& value)
305 {
306 AddProperty(new wxXmlAttribute(name, value, NULL));
307 }
308
309 void wxXmlNode::AddProperty(wxXmlAttribute *attr)
310 {
311 if (m_attrs == NULL)
312 m_attrs = attr;
313 else
314 {
315 wxXmlAttribute *p = m_attrs;
316 while (p->GetNext()) p = p->GetNext();
317 p->SetNext(attr);
318 }
319 }
320
321 bool wxXmlNode::DeleteProperty(const wxString& name)
322 {
323 wxXmlAttribute *attr;
324
325 if (m_attrs == NULL)
326 return false;
327
328 else if (m_attrs->GetName() == name)
329 {
330 attr = m_attrs;
331 m_attrs = attr->GetNext();
332 attr->SetNext(NULL);
333 delete attr;
334 return true;
335 }
336
337 else
338 {
339 wxXmlAttribute *p = m_attrs;
340 while (p->GetNext())
341 {
342 if (p->GetNext()->GetName() == name)
343 {
344 attr = p->GetNext();
345 p->SetNext(attr->GetNext());
346 attr->SetNext(NULL);
347 delete attr;
348 return true;
349 }
350 p = p->GetNext();
351 }
352 return false;
353 }
354 }
355
356 wxString wxXmlNode::GetNodeContent() const
357 {
358 wxXmlNode *n = GetChildren();
359
360 while (n)
361 {
362 if (n->GetType() == wxXML_TEXT_NODE ||
363 n->GetType() == wxXML_CDATA_SECTION_NODE)
364 return n->GetContent();
365 n = n->GetNext();
366 }
367 return wxEmptyString;
368 }
369
370 int wxXmlNode::GetDepth(wxXmlNode *grandparent) const
371 {
372 const wxXmlNode *n = this;
373 int ret = -1;
374
375 do
376 {
377 ret++;
378 n = n->GetParent();
379 if (n == grandparent)
380 return ret;
381
382 } while (n);
383
384 return wxNOT_FOUND;
385 }
386
387 bool wxXmlNode::IsWhitespaceOnly() const
388 {
389 return wxIsWhiteOnly(m_content);
390 }
391
392
393
394 //-----------------------------------------------------------------------------
395 // wxXmlDocument
396 //-----------------------------------------------------------------------------
397
398 wxXmlDocument::wxXmlDocument()
399 : m_version(wxS("1.0")), m_fileEncoding(wxS("UTF-8")), m_docNode(NULL)
400 {
401 #if !wxUSE_UNICODE
402 m_encoding = wxS("UTF-8");
403 #endif
404 }
405
406 wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding)
407 :wxObject(), m_docNode(NULL)
408 {
409 if ( !Load(filename, encoding) )
410 {
411 wxDELETE(m_docNode);
412 }
413 }
414
415 wxXmlDocument::wxXmlDocument(wxInputStream& stream, const wxString& encoding)
416 :wxObject(), m_docNode(NULL)
417 {
418 if ( !Load(stream, encoding) )
419 {
420 wxDELETE(m_docNode);
421 }
422 }
423
424 wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc)
425 :wxObject()
426 {
427 DoCopy(doc);
428 }
429
430 wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc)
431 {
432 wxDELETE(m_docNode);
433 DoCopy(doc);
434 return *this;
435 }
436
437 void wxXmlDocument::DoCopy(const wxXmlDocument& doc)
438 {
439 m_version = doc.m_version;
440 #if !wxUSE_UNICODE
441 m_encoding = doc.m_encoding;
442 #endif
443 m_fileEncoding = doc.m_fileEncoding;
444
445 if (doc.m_docNode)
446 m_docNode = new wxXmlNode(*doc.m_docNode);
447 else
448 m_docNode = NULL;
449 }
450
451 bool wxXmlDocument::Load(const wxString& filename, const wxString& encoding, int flags)
452 {
453 wxFileInputStream stream(filename);
454 if (!stream.Ok())
455 return false;
456 return Load(stream, encoding, flags);
457 }
458
459 bool wxXmlDocument::Save(const wxString& filename, int indentstep) const
460 {
461 wxFileOutputStream stream(filename);
462 if (!stream.Ok())
463 return false;
464 return Save(stream, indentstep);
465 }
466
467 wxXmlNode *wxXmlDocument::GetRoot() const
468 {
469 wxXmlNode *node = m_docNode;
470 if (node)
471 {
472 node = m_docNode->GetChildren();
473 while (node != NULL && node->GetType() != wxXML_ELEMENT_NODE)
474 node = node->GetNext();
475 }
476 return node;
477 }
478
479 wxXmlNode *wxXmlDocument::DetachRoot()
480 {
481 wxXmlNode *node = m_docNode;
482 if (node)
483 {
484 node = m_docNode->GetChildren();
485 wxXmlNode *prev = NULL;
486 while (node != NULL && node->GetType() != wxXML_ELEMENT_NODE)
487 {
488 prev = node;
489 node = node->GetNext();
490 }
491 if (node)
492 {
493 if (node == m_docNode->GetChildren())
494 m_docNode->SetChildren(node->GetNext());
495
496 if (prev)
497 prev->SetNext(node->GetNext());
498
499 node->SetParent(NULL);
500 node->SetNext(NULL);
501 }
502 }
503 return node;
504 }
505
506 void wxXmlDocument::SetRoot(wxXmlNode *root)
507 {
508 if (root)
509 {
510 wxASSERT_MSG( root->GetType() == wxXML_ELEMENT_NODE,
511 "Can only set an element type node as root" );
512 }
513
514 wxXmlNode *node = m_docNode;
515 if (node)
516 {
517 node = m_docNode->GetChildren();
518 wxXmlNode *prev = NULL;
519 while (node != NULL && node->GetType() != wxXML_ELEMENT_NODE)
520 {
521 prev = node;
522 node = node->GetNext();
523 }
524 if (node && root)
525 {
526 root->SetNext( node->GetNext() );
527 wxDELETE(node);
528 }
529 if (prev)
530 prev->SetNext(root);
531 else
532 m_docNode->SetChildren(root);
533 }
534 else
535 {
536 m_docNode = new wxXmlNode(wxXML_DOCUMENT_NODE, wxEmptyString);
537 m_docNode->SetChildren(root);
538 }
539 if (root)
540 root->SetParent(m_docNode);
541 }
542
543 void wxXmlDocument::AppendToProlog(wxXmlNode *node)
544 {
545 if (!m_docNode)
546 m_docNode = new wxXmlNode(wxXML_DOCUMENT_NODE, wxEmptyString);
547 if (IsOk())
548 m_docNode->InsertChild( node, GetRoot() );
549 else
550 m_docNode->AddChild( node );
551 }
552
553 //-----------------------------------------------------------------------------
554 // wxXmlDocument loading routines
555 //-----------------------------------------------------------------------------
556
557 // converts Expat-produced string in UTF-8 into wxString using the specified
558 // conv or keep in UTF-8 if conv is NULL
559 static wxString CharToString(wxMBConv *conv,
560 const char *s, size_t len = wxString::npos)
561 {
562 #if !wxUSE_UNICODE
563 if ( conv )
564 {
565 // there can be no embedded NULs in this string so we don't need the
566 // output length, it will be NUL-terminated
567 const wxWCharBuffer wbuf(
568 wxConvUTF8.cMB2WC(s, len == wxString::npos ? wxNO_LEN : len, NULL));
569
570 return wxString(wbuf, *conv);
571 }
572 // else: the string is wanted in UTF-8
573 #endif // !wxUSE_UNICODE
574
575 wxUnusedVar(conv);
576 return wxString::FromUTF8Unchecked(s, len);
577 }
578
579 // returns true if the given string contains only whitespaces
580 bool wxIsWhiteOnly(const wxString& buf)
581 {
582 for ( wxString::const_iterator i = buf.begin(); i != buf.end(); ++i )
583 {
584 wxChar c = *i;
585 if ( c != wxS(' ') && c != wxS('\t') && c != wxS('\n') && c != wxS('\r'))
586 return false;
587 }
588 return true;
589 }
590
591
592 struct wxXmlParsingContext
593 {
594 wxXmlParsingContext()
595 : conv(NULL),
596 node(NULL),
597 lastChild(NULL),
598 lastAsText(NULL),
599 removeWhiteOnlyNodes(false)
600 {}
601
602 XML_Parser parser;
603 wxMBConv *conv;
604 wxXmlNode *node; // the node being parsed
605 wxXmlNode *lastChild; // the last child of "node"
606 wxXmlNode *lastAsText; // the last _text_ child of "node"
607 wxString encoding;
608 wxString version;
609 bool removeWhiteOnlyNodes;
610 };
611
612 // checks that ctx->lastChild is in consistent state
613 #define ASSERT_LAST_CHILD_OK(ctx) \
614 wxASSERT( ctx->lastChild == NULL || \
615 ctx->lastChild->GetNext() == NULL ); \
616 wxASSERT( ctx->lastChild == NULL || \
617 ctx->lastChild->GetParent() == ctx->node )
618
619 extern "C" {
620 static void StartElementHnd(void *userData, const char *name, const char **atts)
621 {
622 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
623 wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE,
624 CharToString(ctx->conv, name),
625 wxEmptyString,
626 XML_GetCurrentLineNumber(ctx->parser));
627 const char **a = atts;
628
629 // add node attributes
630 while (*a)
631 {
632 node->AddAttribute(CharToString(ctx->conv, a[0]), CharToString(ctx->conv, a[1]));
633 a += 2;
634 }
635
636 ASSERT_LAST_CHILD_OK(ctx);
637 ctx->node->InsertChildAfter(node, ctx->lastChild);
638 ctx->lastAsText = NULL;
639 ctx->lastChild = NULL; // our new node "node" has no children yet
640
641 ctx->node = node;
642 }
643
644 static void EndElementHnd(void *userData, const char* WXUNUSED(name))
645 {
646 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
647
648 // we're exiting the last children of ctx->node->GetParent() and going
649 // back one level up, so current value of ctx->node points to the last
650 // child of ctx->node->GetParent()
651 ctx->lastChild = ctx->node;
652
653 ctx->node = ctx->node->GetParent();
654 ctx->lastAsText = NULL;
655 }
656
657 static void TextHnd(void *userData, const char *s, int len)
658 {
659 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
660 wxString str = CharToString(ctx->conv, s, len);
661
662 if (ctx->lastAsText)
663 {
664 ctx->lastAsText->SetContent(ctx->lastAsText->GetContent() + str);
665 }
666 else
667 {
668 bool whiteOnly = false;
669 if (ctx->removeWhiteOnlyNodes)
670 whiteOnly = wxIsWhiteOnly(str);
671
672 if (!whiteOnly)
673 {
674 wxXmlNode *textnode =
675 new wxXmlNode(wxXML_TEXT_NODE, wxS("text"), str,
676 XML_GetCurrentLineNumber(ctx->parser));
677
678 ASSERT_LAST_CHILD_OK(ctx);
679 ctx->node->InsertChildAfter(textnode, ctx->lastChild);
680 ctx->lastChild= ctx->lastAsText = textnode;
681 }
682 }
683 }
684
685 static void StartCdataHnd(void *userData)
686 {
687 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
688
689 wxXmlNode *textnode =
690 new wxXmlNode(wxXML_CDATA_SECTION_NODE, wxS("cdata"), wxS(""),
691 XML_GetCurrentLineNumber(ctx->parser));
692
693 ASSERT_LAST_CHILD_OK(ctx);
694 ctx->node->InsertChildAfter(textnode, ctx->lastChild);
695 ctx->lastChild= ctx->lastAsText = textnode;
696 }
697
698 static void EndCdataHnd(void *userData)
699 {
700 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
701
702 // we need to reset this pointer so that subsequent text nodes don't append
703 // their contents to this one but create new wxXML_TEXT_NODE objects (or
704 // not create anything at all if only white space follows the CDATA section
705 // and wxXMLDOC_KEEP_WHITESPACE_NODES is not used as is commonly the case)
706 ctx->lastAsText = NULL;
707 }
708
709 static void CommentHnd(void *userData, const char *data)
710 {
711 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
712
713 wxXmlNode *commentnode =
714 new wxXmlNode(wxXML_COMMENT_NODE,
715 wxS("comment"), CharToString(ctx->conv, data),
716 XML_GetCurrentLineNumber(ctx->parser));
717
718 ASSERT_LAST_CHILD_OK(ctx);
719 ctx->node->InsertChildAfter(commentnode, ctx->lastChild);
720 ctx->lastChild = commentnode;
721 ctx->lastAsText = NULL;
722 }
723
724 static void PIHnd(void *userData, const char *target, const char *data)
725 {
726 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
727
728 wxXmlNode *pinode =
729 new wxXmlNode(wxXML_PI_NODE, CharToString(ctx->conv, target),
730 CharToString(ctx->conv, data),
731 XML_GetCurrentLineNumber(ctx->parser));
732
733 ASSERT_LAST_CHILD_OK(ctx);
734 ctx->node->InsertChildAfter(pinode, ctx->lastChild);
735 ctx->lastChild = pinode;
736 ctx->lastAsText = NULL;
737 }
738
739 static void DefaultHnd(void *userData, const char *s, int len)
740 {
741 // XML header:
742 if (len > 6 && memcmp(s, "<?xml ", 6) == 0)
743 {
744 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
745
746 wxString buf = CharToString(ctx->conv, s, (size_t)len);
747 int pos;
748 pos = buf.Find(wxS("encoding="));
749 if (pos != wxNOT_FOUND)
750 ctx->encoding = buf.Mid(pos + 10).BeforeFirst(buf[(size_t)pos+9]);
751 pos = buf.Find(wxS("version="));
752 if (pos != wxNOT_FOUND)
753 ctx->version = buf.Mid(pos + 9).BeforeFirst(buf[(size_t)pos+8]);
754 }
755 }
756
757 static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData),
758 const XML_Char *name, XML_Encoding *info)
759 {
760 // We must build conversion table for expat. The easiest way to do so
761 // is to let wxCSConv convert as string containing all characters to
762 // wide character representation:
763 wxCSConv conv(name);
764 char mbBuf[2];
765 wchar_t wcBuf[10];
766 size_t i;
767
768 mbBuf[1] = 0;
769 info->map[0] = 0;
770 for (i = 0; i < 255; i++)
771 {
772 mbBuf[0] = (char)(i+1);
773 if (conv.MB2WC(wcBuf, mbBuf, 2) == (size_t)-1)
774 {
775 // invalid/undefined byte in the encoding:
776 info->map[i+1] = -1;
777 }
778 info->map[i+1] = (int)wcBuf[0];
779 }
780
781 info->data = NULL;
782 info->convert = NULL;
783 info->release = NULL;
784
785 return 1;
786 }
787
788 } // extern "C"
789
790 bool wxXmlDocument::Load(wxInputStream& stream, const wxString& encoding, int flags)
791 {
792 #if wxUSE_UNICODE
793 (void)encoding;
794 #else
795 m_encoding = encoding;
796 #endif
797
798 const size_t BUFSIZE = 1024;
799 char buf[BUFSIZE];
800 wxXmlParsingContext ctx;
801 bool done;
802 XML_Parser parser = XML_ParserCreate(NULL);
803 wxXmlNode *root = new wxXmlNode(wxXML_DOCUMENT_NODE, wxEmptyString);
804
805 ctx.encoding = wxS("UTF-8"); // default in absence of encoding=""
806 ctx.conv = NULL;
807 #if !wxUSE_UNICODE
808 if ( encoding.CmpNoCase(wxS("UTF-8")) != 0 )
809 ctx.conv = new wxCSConv(encoding);
810 #endif
811 ctx.removeWhiteOnlyNodes = (flags & wxXMLDOC_KEEP_WHITESPACE_NODES) == 0;
812 ctx.parser = parser;
813 ctx.node = root;
814
815 XML_SetUserData(parser, (void*)&ctx);
816 XML_SetElementHandler(parser, StartElementHnd, EndElementHnd);
817 XML_SetCharacterDataHandler(parser, TextHnd);
818 XML_SetCdataSectionHandler(parser, StartCdataHnd, EndCdataHnd);;
819 XML_SetCommentHandler(parser, CommentHnd);
820 XML_SetProcessingInstructionHandler(parser, PIHnd);
821 XML_SetDefaultHandler(parser, DefaultHnd);
822 XML_SetUnknownEncodingHandler(parser, UnknownEncodingHnd, NULL);
823
824 bool ok = true;
825 do
826 {
827 size_t len = stream.Read(buf, BUFSIZE).LastRead();
828 done = (len < BUFSIZE);
829 if (!XML_Parse(parser, buf, len, done))
830 {
831 wxString error(XML_ErrorString(XML_GetErrorCode(parser)),
832 *wxConvCurrent);
833 wxLogError(_("XML parsing error: '%s' at line %d"),
834 error.c_str(),
835 (int)XML_GetCurrentLineNumber(parser));
836 ok = false;
837 break;
838 }
839 } while (!done);
840
841 if (ok)
842 {
843 if (!ctx.version.empty())
844 SetVersion(ctx.version);
845 if (!ctx.encoding.empty())
846 SetFileEncoding(ctx.encoding);
847 SetDocumentNode(root);
848 }
849 else
850 {
851 delete root;
852 }
853
854 XML_ParserFree(parser);
855 #if !wxUSE_UNICODE
856 if ( ctx.conv )
857 delete ctx.conv;
858 #endif
859
860 return ok;
861
862 }
863
864
865
866 //-----------------------------------------------------------------------------
867 // wxXmlDocument saving routines
868 //-----------------------------------------------------------------------------
869
870 // helpers for XML generation
871 namespace
872 {
873
874 // write string to output:
875 bool OutputString(wxOutputStream& stream,
876 const wxString& str,
877 wxMBConv *convMem,
878 wxMBConv *convFile)
879 {
880 if (str.empty())
881 return true;
882
883 #if wxUSE_UNICODE
884 wxUnusedVar(convMem);
885 if ( !convFile )
886 convFile = &wxConvUTF8;
887
888 const wxScopedCharBuffer buf(str.mb_str(*convFile));
889 if ( !buf.length() )
890 {
891 // conversion failed, can't write this string in an XML file in this
892 // (presumably non-UTF-8) encoding
893 return false;
894 }
895
896 stream.Write(buf, buf.length());
897 #else // !wxUSE_UNICODE
898 if ( convFile && convMem )
899 {
900 wxString str2(str.wc_str(*convMem), *convFile);
901 stream.Write(str2.mb_str(), str2.length());
902 }
903 else // no conversions to do
904 {
905 stream.Write(str.mb_str(), str.length());
906 }
907 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
908
909 return stream.IsOk();
910 }
911
912 enum EscapingMode
913 {
914 Escape_Text,
915 Escape_Attribute
916 };
917
918 // Same as above, but create entities first.
919 // Translates '<' to "&lt;", '>' to "&gt;" and so on, according to the spec:
920 // http://www.w3.org/TR/2000/WD-xml-c14n-20000119.html#charescaping
921 bool OutputEscapedString(wxOutputStream& stream,
922 const wxString& str,
923 wxMBConv *convMem,
924 wxMBConv *convFile,
925 EscapingMode mode)
926 {
927 wxString escaped;
928 escaped.reserve(str.length());
929
930 for ( wxString::const_iterator i = str.begin(); i != str.end(); ++i )
931 {
932 const wxChar c = *i;
933
934 switch ( c )
935 {
936 case wxS('<'):
937 escaped.append(wxS("&lt;"));
938 break;
939 case wxS('>'):
940 escaped.append(wxS("&gt;"));
941 break;
942 case wxS('&'):
943 escaped.append(wxS("&amp;"));
944 break;
945 case wxS('\r'):
946 escaped.append(wxS("&#xD;"));
947 break;
948 default:
949 if ( mode == Escape_Attribute )
950 {
951 switch ( c )
952 {
953 case wxS('"'):
954 escaped.append(wxS("&quot;"));
955 break;
956 case wxS('\t'):
957 escaped.append(wxS("&#x9;"));
958 break;
959 case wxS('\n'):
960 escaped.append(wxS("&#xA;"));
961 break;
962 default:
963 escaped.append(c);
964 }
965
966 }
967 else
968 {
969 escaped.append(c);
970 }
971 }
972 }
973
974 return OutputString(stream, escaped, convMem, convFile);
975 }
976
977 bool OutputIndentation(wxOutputStream& stream,
978 int indent,
979 wxMBConv *convMem,
980 wxMBConv *convFile)
981 {
982 wxString str(wxS("\n"));
983 str += wxString(indent, wxS(' '));
984 return OutputString(stream, str, convMem, convFile);
985 }
986
987 bool OutputNode(wxOutputStream& stream,
988 wxXmlNode *node,
989 int indent,
990 wxMBConv *convMem,
991 wxMBConv *convFile,
992 int indentstep)
993 {
994 bool rc;
995 switch (node->GetType())
996 {
997 case wxXML_CDATA_SECTION_NODE:
998 rc = OutputString(stream, wxS("<![CDATA["), convMem, convFile) &&
999 OutputString(stream, node->GetContent(), convMem, convFile) &&
1000 OutputString(stream, wxS("]]>"), convMem, convFile);
1001 break;
1002
1003 case wxXML_TEXT_NODE:
1004 if (node->GetNoConversion())
1005 {
1006 stream.Write(node->GetContent().c_str(), node->GetContent().Length());
1007 rc = true;
1008 }
1009 else
1010 rc = OutputEscapedString(stream, node->GetContent(),
1011 convMem, convFile,
1012 Escape_Text);
1013 break;
1014
1015 case wxXML_ELEMENT_NODE:
1016 rc = OutputString(stream, wxS("<"), convMem, convFile) &&
1017 OutputString(stream, node->GetName(), convMem, convFile);
1018
1019 if ( rc )
1020 {
1021 for ( wxXmlAttribute *attr = node->GetAttributes();
1022 attr && rc;
1023 attr = attr->GetNext() )
1024 {
1025 rc = OutputString(stream,
1026 wxS(" ") + attr->GetName() + wxS("=\""),
1027 convMem, convFile) &&
1028 OutputEscapedString(stream, attr->GetValue(),
1029 convMem, convFile,
1030 Escape_Attribute) &&
1031 OutputString(stream, wxS("\""), convMem, convFile);
1032 }
1033 }
1034
1035 if ( node->GetChildren() )
1036 {
1037 rc = OutputString(stream, wxS(">"), convMem, convFile);
1038
1039 wxXmlNode *prev = NULL;
1040 for ( wxXmlNode *n = node->GetChildren();
1041 n && rc;
1042 n = n->GetNext() )
1043 {
1044 if ( indentstep >= 0 && n->GetType() != wxXML_TEXT_NODE )
1045 {
1046 rc = OutputIndentation(stream, indent + indentstep,
1047 convMem, convFile);
1048 }
1049
1050 if ( rc )
1051 rc = OutputNode(stream, n, indent + indentstep,
1052 convMem, convFile, indentstep);
1053
1054 prev = n;
1055 }
1056
1057 if ( rc && indentstep >= 0 &&
1058 prev && prev->GetType() != wxXML_TEXT_NODE )
1059 {
1060 rc = OutputIndentation(stream, indent, convMem, convFile);
1061 }
1062
1063 if ( rc )
1064 {
1065 rc = OutputString(stream, wxS("</"), convMem, convFile) &&
1066 OutputString(stream, node->GetName(),
1067 convMem, convFile) &&
1068 OutputString(stream, wxS(">"), convMem, convFile);
1069 }
1070 }
1071 else // no children, output "<foo/>"
1072 {
1073 rc = OutputString(stream, wxS("/>"), convMem, convFile);
1074 }
1075 break;
1076
1077 case wxXML_COMMENT_NODE:
1078 rc = OutputString(stream, wxS("<!--"), convMem, convFile) &&
1079 OutputString(stream, node->GetContent(), convMem, convFile) &&
1080 OutputString(stream, wxS("-->"), convMem, convFile);
1081 break;
1082
1083 case wxXML_PI_NODE:
1084 rc = OutputString(stream, wxT("<?"), convMem, convFile) &&
1085 OutputString(stream, node->GetName(), convMem, convFile) &&
1086 OutputString(stream, wxT(" "), convMem, convFile) &&
1087 OutputString(stream, node->GetContent(), convMem, convFile) &&
1088 OutputString(stream, wxT("?>"), convMem, convFile);
1089 break;
1090
1091 default:
1092 wxFAIL_MSG("unsupported node type");
1093 rc = false;
1094 }
1095
1096 return rc;
1097 }
1098
1099 } // anonymous namespace
1100
1101 bool wxXmlDocument::Save(wxOutputStream& stream, int indentstep) const
1102 {
1103 if ( !IsOk() )
1104 return false;
1105
1106 wxScopedPtr<wxMBConv> convMem, convFile;
1107
1108 #if wxUSE_UNICODE
1109 convFile.reset(new wxCSConv(GetFileEncoding()));
1110 #else
1111 if ( GetFileEncoding().CmpNoCase(GetEncoding()) != 0 )
1112 {
1113 convFile.reset(new wxCSConv(GetFileEncoding()));
1114 convMem.reset(new wxCSConv(GetEncoding()));
1115 }
1116 //else: file and in-memory encodings are the same, no conversion needed
1117 #endif
1118
1119 wxString dec = wxString::Format(
1120 wxS("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
1121 GetVersion(), GetFileEncoding()
1122 );
1123 bool rc = OutputString(stream, dec, convMem.get(), convFile.get());
1124
1125 wxXmlNode *node = GetDocumentNode();
1126 if ( node )
1127 node = node->GetChildren();
1128
1129 while( rc && node )
1130 {
1131 rc = OutputNode(stream, node, 0, convMem.get(),
1132 convFile.get(), indentstep) &&
1133 OutputString(stream, wxS("\n"), convMem.get(), convFile.get());
1134 node = node->GetNext();
1135 }
1136 return rc;
1137 }
1138
1139 /*static*/ wxVersionInfo wxXmlDocument::GetLibraryVersionInfo()
1140 {
1141 return wxVersionInfo("expat",
1142 XML_MAJOR_VERSION,
1143 XML_MINOR_VERSION,
1144 XML_MICRO_VERSION);
1145 }
1146
1147 #endif // wxUSE_XML