1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/xml/xml.cpp
3 // Purpose: wxXmlDocument - XML parser & data holder class
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2000 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
19 #include "wx/xml/xml.h"
27 #include "wx/wfstream.h"
28 #include "wx/datstrm.h"
29 #include "wx/zstream.h"
30 #include "wx/strconv.h"
31 #include "wx/scopedptr.h"
32 #include "wx/versioninfo.h"
34 #include "expat.h" // from Expat
36 // DLL options compatibility check:
37 WX_CHECK_BUILD_OPTIONS("wxXML")
40 IMPLEMENT_CLASS(wxXmlDocument
, wxObject
)
43 // a private utility used by wxXML
44 static bool wxIsWhiteOnly(const wxString
& buf
);
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 wxXmlNode::wxXmlNode(wxXmlNode
*parent
,wxXmlNodeType type
,
52 const wxString
& name
, const wxString
& content
,
53 wxXmlAttribute
*attrs
, wxXmlNode
*next
, int lineNo
)
54 : m_type(type
), m_name(name
), m_content(content
),
55 m_attrs(attrs
), m_parent(parent
),
56 m_children(NULL
), m_next(next
),
60 wxASSERT_MSG ( type
!= wxXML_ELEMENT_NODE
|| content
.empty(), "element nodes can't have content" );
64 if (m_parent
->m_children
)
66 m_next
= m_parent
->m_children
;
67 m_parent
->m_children
= this;
70 m_parent
->m_children
= this;
74 wxXmlNode::wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
75 const wxString
& content
,
77 : m_type(type
), m_name(name
), m_content(content
),
78 m_attrs(NULL
), m_parent(NULL
),
79 m_children(NULL
), m_next(NULL
),
80 m_lineNo(lineNo
), m_noConversion(false)
82 wxASSERT_MSG ( type
!= wxXML_ELEMENT_NODE
|| content
.empty(), "element nodes can't have content" );
85 wxXmlNode::wxXmlNode(const wxXmlNode
& node
)
92 wxXmlNode::~wxXmlNode()
97 wxXmlNode
& wxXmlNode::operator=(const wxXmlNode
& node
)
108 void wxXmlNode::DoFree()
111 for (c
= m_children
; c
; c
= c2
)
117 wxXmlAttribute
*p
, *p2
;
118 for (p
= m_attrs
; p
; p
= p2
)
125 void wxXmlNode::DoCopy(const wxXmlNode
& node
)
127 m_type
= node
.m_type
;
128 m_name
= node
.m_name
;
129 m_content
= node
.m_content
;
130 m_lineNo
= node
.m_lineNo
;
131 m_noConversion
= node
.m_noConversion
;
134 wxXmlNode
*n
= node
.m_children
;
137 AddChild(new wxXmlNode(*n
));
142 wxXmlAttribute
*p
= node
.m_attrs
;
145 AddAttribute(p
->GetName(), p
->GetValue());
150 bool wxXmlNode::HasAttribute(const wxString
& attrName
) const
152 wxXmlAttribute
*attr
= GetAttributes();
156 if (attr
->GetName() == attrName
) return true;
157 attr
= attr
->GetNext();
163 bool wxXmlNode::GetAttribute(const wxString
& attrName
, wxString
*value
) const
165 wxCHECK_MSG( value
, false, "value argument must not be NULL" );
167 wxXmlAttribute
*attr
= GetAttributes();
171 if (attr
->GetName() == attrName
)
173 *value
= attr
->GetValue();
176 attr
= attr
->GetNext();
182 wxString
wxXmlNode::GetAttribute(const wxString
& attrName
, const wxString
& defaultVal
) const
185 if (GetAttribute(attrName
, &tmp
))
191 void wxXmlNode::AddChild(wxXmlNode
*child
)
193 if (m_children
== NULL
)
197 wxXmlNode
*ch
= m_children
;
198 while (ch
->m_next
) ch
= ch
->m_next
;
201 child
->m_next
= NULL
;
202 child
->m_parent
= this;
205 // inserts a new node in front of 'followingNode'
206 bool wxXmlNode::InsertChild(wxXmlNode
*child
, wxXmlNode
*followingNode
)
208 wxCHECK_MSG( child
, false, "cannot insert a NULL node!" );
209 wxCHECK_MSG( child
->m_parent
== NULL
, false, "node already has a parent" );
210 wxCHECK_MSG( child
->m_next
== NULL
, false, "node already has m_next" );
211 wxCHECK_MSG( followingNode
== NULL
|| followingNode
->GetParent() == this,
213 "wxXmlNode::InsertChild - followingNode has incorrect parent" );
215 // this is for backward compatibility, NULL was allowed here thanks to
216 // the confusion about followingNode's meaning
217 if ( followingNode
== NULL
)
218 followingNode
= m_children
;
220 if ( m_children
== followingNode
)
222 child
->m_next
= m_children
;
227 wxXmlNode
*ch
= m_children
;
228 while ( ch
&& ch
->m_next
!= followingNode
)
232 wxFAIL_MSG( "followingNode has this node as parent, but couldn't be found among children" );
236 child
->m_next
= followingNode
;
240 child
->m_parent
= this;
244 // inserts a new node right after 'precedingNode'
245 bool wxXmlNode::InsertChildAfter(wxXmlNode
*child
, wxXmlNode
*precedingNode
)
247 wxCHECK_MSG( child
, false, "cannot insert a NULL node!" );
248 wxCHECK_MSG( child
->m_parent
== NULL
, false, "node already has a parent" );
249 wxCHECK_MSG( child
->m_next
== NULL
, false, "node already has m_next" );
250 wxCHECK_MSG( precedingNode
== NULL
|| precedingNode
->m_parent
== this, false,
251 "precedingNode has wrong parent" );
255 child
->m_next
= precedingNode
->m_next
;
256 precedingNode
->m_next
= child
;
258 else // precedingNode == NULL
260 wxCHECK_MSG( m_children
== NULL
, false,
261 "NULL precedingNode only makes sense when there are no children" );
263 child
->m_next
= m_children
;
267 child
->m_parent
= this;
271 bool wxXmlNode::RemoveChild(wxXmlNode
*child
)
273 if (m_children
== NULL
)
275 else if (m_children
== child
)
277 m_children
= child
->m_next
;
278 child
->m_parent
= NULL
;
279 child
->m_next
= NULL
;
284 wxXmlNode
*ch
= m_children
;
287 if (ch
->m_next
== child
)
289 ch
->m_next
= child
->m_next
;
290 child
->m_parent
= NULL
;
291 child
->m_next
= NULL
;
300 void wxXmlNode::AddAttribute(const wxString
& name
, const wxString
& value
)
302 AddProperty(name
, value
);
305 void wxXmlNode::AddAttribute(wxXmlAttribute
*attr
)
310 bool wxXmlNode::DeleteAttribute(const wxString
& name
)
312 return DeleteProperty(name
);
315 void wxXmlNode::AddProperty(const wxString
& name
, const wxString
& value
)
317 AddProperty(new wxXmlAttribute(name
, value
, NULL
));
320 void wxXmlNode::AddProperty(wxXmlAttribute
*attr
)
326 wxXmlAttribute
*p
= m_attrs
;
327 while (p
->GetNext()) p
= p
->GetNext();
332 bool wxXmlNode::DeleteProperty(const wxString
& name
)
334 wxXmlAttribute
*attr
;
339 else if (m_attrs
->GetName() == name
)
342 m_attrs
= attr
->GetNext();
350 wxXmlAttribute
*p
= m_attrs
;
353 if (p
->GetNext()->GetName() == name
)
356 p
->SetNext(attr
->GetNext());
367 wxString
wxXmlNode::GetNodeContent() const
369 wxXmlNode
*n
= GetChildren();
373 if (n
->GetType() == wxXML_TEXT_NODE
||
374 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
375 return n
->GetContent();
378 return wxEmptyString
;
381 int wxXmlNode::GetDepth(wxXmlNode
*grandparent
) const
383 const wxXmlNode
*n
= this;
390 if (n
== grandparent
)
398 bool wxXmlNode::IsWhitespaceOnly() const
400 return wxIsWhiteOnly(m_content
);
405 //-----------------------------------------------------------------------------
407 //-----------------------------------------------------------------------------
409 wxXmlDocument::wxXmlDocument()
410 : m_version(wxS("1.0")), m_fileEncoding(wxS("UTF-8")), m_docNode(NULL
)
413 m_encoding
= wxS("UTF-8");
417 wxXmlDocument::wxXmlDocument(const wxString
& filename
, const wxString
& encoding
)
418 :wxObject(), m_docNode(NULL
)
420 if ( !Load(filename
, encoding
) )
426 wxXmlDocument::wxXmlDocument(wxInputStream
& stream
, const wxString
& encoding
)
427 :wxObject(), m_docNode(NULL
)
429 if ( !Load(stream
, encoding
) )
435 wxXmlDocument::wxXmlDocument(const wxXmlDocument
& doc
)
441 wxXmlDocument
& wxXmlDocument::operator=(const wxXmlDocument
& doc
)
448 void wxXmlDocument::DoCopy(const wxXmlDocument
& doc
)
450 m_version
= doc
.m_version
;
452 m_encoding
= doc
.m_encoding
;
454 m_fileEncoding
= doc
.m_fileEncoding
;
457 m_docNode
= new wxXmlNode(*doc
.m_docNode
);
462 bool wxXmlDocument::Load(const wxString
& filename
, const wxString
& encoding
, int flags
)
464 wxFileInputStream
stream(filename
);
467 return Load(stream
, encoding
, flags
);
470 bool wxXmlDocument::Save(const wxString
& filename
, int indentstep
) const
472 wxFileOutputStream
stream(filename
);
475 return Save(stream
, indentstep
);
478 wxXmlNode
*wxXmlDocument::GetRoot() const
480 wxXmlNode
*node
= m_docNode
;
483 node
= m_docNode
->GetChildren();
484 while (node
!= NULL
&& node
->GetType() != wxXML_ELEMENT_NODE
)
485 node
= node
->GetNext();
490 wxXmlNode
*wxXmlDocument::DetachRoot()
492 wxXmlNode
*node
= m_docNode
;
495 node
= m_docNode
->GetChildren();
496 wxXmlNode
*prev
= NULL
;
497 while (node
!= NULL
&& node
->GetType() != wxXML_ELEMENT_NODE
)
500 node
= node
->GetNext();
504 if (node
== m_docNode
->GetChildren())
505 m_docNode
->SetChildren(node
->GetNext());
508 prev
->SetNext(node
->GetNext());
510 node
->SetParent(NULL
);
517 void wxXmlDocument::SetRoot(wxXmlNode
*root
)
521 wxASSERT_MSG( root
->GetType() == wxXML_ELEMENT_NODE
,
522 "Can only set an element type node as root" );
525 wxXmlNode
*node
= m_docNode
;
528 node
= m_docNode
->GetChildren();
529 wxXmlNode
*prev
= NULL
;
530 while (node
!= NULL
&& node
->GetType() != wxXML_ELEMENT_NODE
)
533 node
= node
->GetNext();
537 root
->SetNext( node
->GetNext() );
543 m_docNode
->SetChildren(root
);
547 m_docNode
= new wxXmlNode(wxXML_DOCUMENT_NODE
, wxEmptyString
);
548 m_docNode
->SetChildren(root
);
551 root
->SetParent(m_docNode
);
554 void wxXmlDocument::AppendToProlog(wxXmlNode
*node
)
557 m_docNode
= new wxXmlNode(wxXML_DOCUMENT_NODE
, wxEmptyString
);
559 m_docNode
->InsertChild( node
, GetRoot() );
561 m_docNode
->AddChild( node
);
564 //-----------------------------------------------------------------------------
565 // wxXmlDocument loading routines
566 //-----------------------------------------------------------------------------
568 // converts Expat-produced string in UTF-8 into wxString using the specified
569 // conv or keep in UTF-8 if conv is NULL
570 static wxString
CharToString(wxMBConv
*conv
,
571 const char *s
, size_t len
= wxString::npos
)
576 // there can be no embedded NULs in this string so we don't need the
577 // output length, it will be NUL-terminated
578 const wxWCharBuffer
wbuf(
579 wxConvUTF8
.cMB2WC(s
, len
== wxString::npos
? wxNO_LEN
: len
, NULL
));
581 return wxString(wbuf
, *conv
);
583 // else: the string is wanted in UTF-8
584 #endif // !wxUSE_UNICODE
587 return wxString::FromUTF8Unchecked(s
, len
);
590 // returns true if the given string contains only whitespaces
591 bool wxIsWhiteOnly(const wxString
& buf
)
593 for ( wxString::const_iterator i
= buf
.begin(); i
!= buf
.end(); ++i
)
596 if ( c
!= wxS(' ') && c
!= wxS('\t') && c
!= wxS('\n') && c
!= wxS('\r'))
603 struct wxXmlParsingContext
605 wxXmlParsingContext()
610 removeWhiteOnlyNodes(false)
615 wxXmlNode
*node
; // the node being parsed
616 wxXmlNode
*lastChild
; // the last child of "node"
617 wxXmlNode
*lastAsText
; // the last _text_ child of "node"
620 bool removeWhiteOnlyNodes
;
623 // checks that ctx->lastChild is in consistent state
624 #define ASSERT_LAST_CHILD_OK(ctx) \
625 wxASSERT( ctx->lastChild == NULL || \
626 ctx->lastChild->GetNext() == NULL ); \
627 wxASSERT( ctx->lastChild == NULL || \
628 ctx->lastChild->GetParent() == ctx->node )
631 static void StartElementHnd(void *userData
, const char *name
, const char **atts
)
633 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
634 wxXmlNode
*node
= new wxXmlNode(wxXML_ELEMENT_NODE
,
635 CharToString(ctx
->conv
, name
),
637 XML_GetCurrentLineNumber(ctx
->parser
));
638 const char **a
= atts
;
640 // add node attributes
643 node
->AddAttribute(CharToString(ctx
->conv
, a
[0]), CharToString(ctx
->conv
, a
[1]));
647 ASSERT_LAST_CHILD_OK(ctx
);
648 ctx
->node
->InsertChildAfter(node
, ctx
->lastChild
);
649 ctx
->lastAsText
= NULL
;
650 ctx
->lastChild
= NULL
; // our new node "node" has no children yet
655 static void EndElementHnd(void *userData
, const char* WXUNUSED(name
))
657 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
659 // we're exiting the last children of ctx->node->GetParent() and going
660 // back one level up, so current value of ctx->node points to the last
661 // child of ctx->node->GetParent()
662 ctx
->lastChild
= ctx
->node
;
664 ctx
->node
= ctx
->node
->GetParent();
665 ctx
->lastAsText
= NULL
;
668 static void TextHnd(void *userData
, const char *s
, int len
)
670 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
671 wxString str
= CharToString(ctx
->conv
, s
, len
);
675 ctx
->lastAsText
->SetContent(ctx
->lastAsText
->GetContent() + str
);
679 bool whiteOnly
= false;
680 if (ctx
->removeWhiteOnlyNodes
)
681 whiteOnly
= wxIsWhiteOnly(str
);
685 wxXmlNode
*textnode
=
686 new wxXmlNode(wxXML_TEXT_NODE
, wxS("text"), str
,
687 XML_GetCurrentLineNumber(ctx
->parser
));
689 ASSERT_LAST_CHILD_OK(ctx
);
690 ctx
->node
->InsertChildAfter(textnode
, ctx
->lastChild
);
691 ctx
->lastChild
= ctx
->lastAsText
= textnode
;
696 static void StartCdataHnd(void *userData
)
698 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
700 wxXmlNode
*textnode
=
701 new wxXmlNode(wxXML_CDATA_SECTION_NODE
, wxS("cdata"), wxS(""),
702 XML_GetCurrentLineNumber(ctx
->parser
));
704 ASSERT_LAST_CHILD_OK(ctx
);
705 ctx
->node
->InsertChildAfter(textnode
, ctx
->lastChild
);
706 ctx
->lastChild
= ctx
->lastAsText
= textnode
;
709 static void EndCdataHnd(void *userData
)
711 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
713 // we need to reset this pointer so that subsequent text nodes don't append
714 // their contents to this one but create new wxXML_TEXT_NODE objects (or
715 // not create anything at all if only white space follows the CDATA section
716 // and wxXMLDOC_KEEP_WHITESPACE_NODES is not used as is commonly the case)
717 ctx
->lastAsText
= NULL
;
720 static void CommentHnd(void *userData
, const char *data
)
722 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
724 wxXmlNode
*commentnode
=
725 new wxXmlNode(wxXML_COMMENT_NODE
,
726 wxS("comment"), CharToString(ctx
->conv
, data
),
727 XML_GetCurrentLineNumber(ctx
->parser
));
729 ASSERT_LAST_CHILD_OK(ctx
);
730 ctx
->node
->InsertChildAfter(commentnode
, ctx
->lastChild
);
731 ctx
->lastChild
= commentnode
;
732 ctx
->lastAsText
= NULL
;
735 static void PIHnd(void *userData
, const char *target
, const char *data
)
737 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
740 new wxXmlNode(wxXML_PI_NODE
, CharToString(ctx
->conv
, target
),
741 CharToString(ctx
->conv
, data
),
742 XML_GetCurrentLineNumber(ctx
->parser
));
744 ASSERT_LAST_CHILD_OK(ctx
);
745 ctx
->node
->InsertChildAfter(pinode
, ctx
->lastChild
);
746 ctx
->lastChild
= pinode
;
747 ctx
->lastAsText
= NULL
;
750 static void DefaultHnd(void *userData
, const char *s
, int len
)
753 if (len
> 6 && memcmp(s
, "<?xml ", 6) == 0)
755 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
757 wxString buf
= CharToString(ctx
->conv
, s
, (size_t)len
);
759 pos
= buf
.Find(wxS("encoding="));
760 if (pos
!= wxNOT_FOUND
)
761 ctx
->encoding
= buf
.Mid(pos
+ 10).BeforeFirst(buf
[(size_t)pos
+9]);
762 pos
= buf
.Find(wxS("version="));
763 if (pos
!= wxNOT_FOUND
)
764 ctx
->version
= buf
.Mid(pos
+ 9).BeforeFirst(buf
[(size_t)pos
+8]);
768 static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData
),
769 const XML_Char
*name
, XML_Encoding
*info
)
771 // We must build conversion table for expat. The easiest way to do so
772 // is to let wxCSConv convert as string containing all characters to
773 // wide character representation:
781 for (i
= 0; i
< 255; i
++)
783 mbBuf
[0] = (char)(i
+1);
784 if (conv
.MB2WC(wcBuf
, mbBuf
, 2) == (size_t)-1)
786 // invalid/undefined byte in the encoding:
789 info
->map
[i
+1] = (int)wcBuf
[0];
793 info
->convert
= NULL
;
794 info
->release
= NULL
;
801 bool wxXmlDocument::Load(wxInputStream
& stream
, const wxString
& encoding
, int flags
)
806 m_encoding
= encoding
;
809 const size_t BUFSIZE
= 1024;
811 wxXmlParsingContext ctx
;
813 XML_Parser parser
= XML_ParserCreate(NULL
);
814 wxXmlNode
*root
= new wxXmlNode(wxXML_DOCUMENT_NODE
, wxEmptyString
);
816 ctx
.encoding
= wxS("UTF-8"); // default in absence of encoding=""
819 if ( encoding
.CmpNoCase(wxS("UTF-8")) != 0 )
820 ctx
.conv
= new wxCSConv(encoding
);
822 ctx
.removeWhiteOnlyNodes
= (flags
& wxXMLDOC_KEEP_WHITESPACE_NODES
) == 0;
826 XML_SetUserData(parser
, (void*)&ctx
);
827 XML_SetElementHandler(parser
, StartElementHnd
, EndElementHnd
);
828 XML_SetCharacterDataHandler(parser
, TextHnd
);
829 XML_SetCdataSectionHandler(parser
, StartCdataHnd
, EndCdataHnd
);;
830 XML_SetCommentHandler(parser
, CommentHnd
);
831 XML_SetProcessingInstructionHandler(parser
, PIHnd
);
832 XML_SetDefaultHandler(parser
, DefaultHnd
);
833 XML_SetUnknownEncodingHandler(parser
, UnknownEncodingHnd
, NULL
);
838 size_t len
= stream
.Read(buf
, BUFSIZE
).LastRead();
839 done
= (len
< BUFSIZE
);
840 if (!XML_Parse(parser
, buf
, len
, done
))
842 wxString
error(XML_ErrorString(XML_GetErrorCode(parser
)),
844 wxLogError(_("XML parsing error: '%s' at line %d"),
846 (int)XML_GetCurrentLineNumber(parser
));
854 if (!ctx
.version
.empty())
855 SetVersion(ctx
.version
);
856 if (!ctx
.encoding
.empty())
857 SetFileEncoding(ctx
.encoding
);
858 SetDocumentNode(root
);
865 XML_ParserFree(parser
);
877 //-----------------------------------------------------------------------------
878 // wxXmlDocument saving routines
879 //-----------------------------------------------------------------------------
881 // helpers for XML generation
885 // write string to output:
886 bool OutputString(wxOutputStream
& stream
,
895 wxUnusedVar(convMem
);
897 convFile
= &wxConvUTF8
;
899 const wxScopedCharBuffer
buf(str
.mb_str(*convFile
));
902 // conversion failed, can't write this string in an XML file in this
903 // (presumably non-UTF-8) encoding
907 stream
.Write(buf
, buf
.length());
908 #else // !wxUSE_UNICODE
909 if ( convFile
&& convMem
)
911 wxString
str2(str
.wc_str(*convMem
), *convFile
);
912 stream
.Write(str2
.mb_str(), str2
.length());
914 else // no conversions to do
916 stream
.Write(str
.mb_str(), str
.length());
918 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
920 return stream
.IsOk();
929 // Same as above, but create entities first.
930 // Translates '<' to "<", '>' to ">" and so on, according to the spec:
931 // http://www.w3.org/TR/2000/WD-xml-c14n-20000119.html#charescaping
932 bool OutputEscapedString(wxOutputStream
& stream
,
939 escaped
.reserve(str
.length());
941 for ( wxString::const_iterator i
= str
.begin(); i
!= str
.end(); ++i
)
948 escaped
.append(wxS("<"));
951 escaped
.append(wxS(">"));
954 escaped
.append(wxS("&"));
957 escaped
.append(wxS("
"));
960 if ( mode
== Escape_Attribute
)
965 escaped
.append(wxS("""));
968 escaped
.append(wxS("	"));
971 escaped
.append(wxS("
"));
985 return OutputString(stream
, escaped
, convMem
, convFile
);
988 bool OutputIndentation(wxOutputStream
& stream
,
993 wxString
str(wxS("\n"));
994 str
+= wxString(indent
, wxS(' '));
995 return OutputString(stream
, str
, convMem
, convFile
);
998 bool OutputNode(wxOutputStream
& stream
,
1006 switch (node
->GetType())
1008 case wxXML_CDATA_SECTION_NODE
:
1009 rc
= OutputString(stream
, wxS("<![CDATA["), convMem
, convFile
) &&
1010 OutputString(stream
, node
->GetContent(), convMem
, convFile
) &&
1011 OutputString(stream
, wxS("]]>"), convMem
, convFile
);
1014 case wxXML_TEXT_NODE
:
1015 if (node
->GetNoConversion())
1017 stream
.Write(node
->GetContent().c_str(), node
->GetContent().Length());
1021 rc
= OutputEscapedString(stream
, node
->GetContent(),
1026 case wxXML_ELEMENT_NODE
:
1027 rc
= OutputString(stream
, wxS("<"), convMem
, convFile
) &&
1028 OutputString(stream
, node
->GetName(), convMem
, convFile
);
1032 for ( wxXmlAttribute
*attr
= node
->GetAttributes();
1034 attr
= attr
->GetNext() )
1036 rc
= OutputString(stream
,
1037 wxS(" ") + attr
->GetName() + wxS("=\""),
1038 convMem
, convFile
) &&
1039 OutputEscapedString(stream
, attr
->GetValue(),
1041 Escape_Attribute
) &&
1042 OutputString(stream
, wxS("\""), convMem
, convFile
);
1046 if ( node
->GetChildren() )
1048 rc
= OutputString(stream
, wxS(">"), convMem
, convFile
);
1050 wxXmlNode
*prev
= NULL
;
1051 for ( wxXmlNode
*n
= node
->GetChildren();
1055 if ( indentstep
>= 0 && n
->GetType() != wxXML_TEXT_NODE
)
1057 rc
= OutputIndentation(stream
, indent
+ indentstep
,
1062 rc
= OutputNode(stream
, n
, indent
+ indentstep
,
1063 convMem
, convFile
, indentstep
);
1068 if ( rc
&& indentstep
>= 0 &&
1069 prev
&& prev
->GetType() != wxXML_TEXT_NODE
)
1071 rc
= OutputIndentation(stream
, indent
, convMem
, convFile
);
1076 rc
= OutputString(stream
, wxS("</"), convMem
, convFile
) &&
1077 OutputString(stream
, node
->GetName(),
1078 convMem
, convFile
) &&
1079 OutputString(stream
, wxS(">"), convMem
, convFile
);
1082 else // no children, output "<foo/>"
1084 rc
= OutputString(stream
, wxS("/>"), convMem
, convFile
);
1088 case wxXML_COMMENT_NODE
:
1089 rc
= OutputString(stream
, wxS("<!--"), convMem
, convFile
) &&
1090 OutputString(stream
, node
->GetContent(), convMem
, convFile
) &&
1091 OutputString(stream
, wxS("-->"), convMem
, convFile
);
1095 rc
= OutputString(stream
, wxT("<?"), convMem
, convFile
) &&
1096 OutputString(stream
, node
->GetName(), convMem
, convFile
) &&
1097 OutputString(stream
, wxT(" "), convMem
, convFile
) &&
1098 OutputString(stream
, node
->GetContent(), convMem
, convFile
) &&
1099 OutputString(stream
, wxT("?>"), convMem
, convFile
);
1103 wxFAIL_MSG("unsupported node type");
1110 } // anonymous namespace
1112 bool wxXmlDocument::Save(wxOutputStream
& stream
, int indentstep
) const
1117 wxScopedPtr
<wxMBConv
> convMem
, convFile
;
1120 convFile
.reset(new wxCSConv(GetFileEncoding()));
1122 if ( GetFileEncoding().CmpNoCase(GetEncoding()) != 0 )
1124 convFile
.reset(new wxCSConv(GetFileEncoding()));
1125 convMem
.reset(new wxCSConv(GetEncoding()));
1127 //else: file and in-memory encodings are the same, no conversion needed
1130 wxString dec
= wxString::Format(
1131 wxS("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
1132 GetVersion(), GetFileEncoding()
1134 bool rc
= OutputString(stream
, dec
, convMem
.get(), convFile
.get());
1136 wxXmlNode
*node
= GetDocumentNode();
1138 node
= node
->GetChildren();
1142 rc
= OutputNode(stream
, node
, 0, convMem
.get(),
1143 convFile
.get(), indentstep
) &&
1144 OutputString(stream
, wxS("\n"), convMem
.get(), convFile
.get());
1145 node
= node
->GetNext();
1150 /*static*/ wxVersionInfo
wxXmlDocument::GetLibraryVersionInfo()
1152 return wxVersionInfo("expat",