]>
git.saurik.com Git - wxWidgets.git/blob - src/xml/xml.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/xml/xml.cpp
3 // Purpose: wxXmlDocument - XML parser & data holder class
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/xml/xml.h"
28 #include "wx/wfstream.h"
29 #include "wx/datstrm.h"
30 #include "wx/zstream.h"
31 #include "wx/strconv.h"
33 #include "expat.h" // from Expat
35 // DLL options compatibility check:
36 WX_CHECK_BUILD_OPTIONS("wxXML")
39 IMPLEMENT_CLASS(wxXmlDocument
, wxObject
)
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 wxXmlNode::wxXmlNode(wxXmlNode
*parent
,wxXmlNodeType type
,
48 const wxString
& name
, const wxString
& content
,
49 wxXmlProperty
*props
, wxXmlNode
*next
)
50 : m_type(type
), m_name(name
), m_content(content
),
51 m_properties(props
), m_parent(parent
),
52 m_children(NULL
), m_next(next
)
56 if (m_parent
->m_children
)
58 m_next
= m_parent
->m_children
;
59 m_parent
->m_children
= this;
62 m_parent
->m_children
= this;
66 wxXmlNode::wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
67 const wxString
& content
)
68 : m_type(type
), m_name(name
), m_content(content
),
69 m_properties(NULL
), m_parent(NULL
),
70 m_children(NULL
), m_next(NULL
)
73 wxXmlNode::wxXmlNode(const wxXmlNode
& node
)
80 wxXmlNode::~wxXmlNode()
83 for (c
= m_children
; c
; c
= c2
)
89 wxXmlProperty
*p
, *p2
;
90 for (p
= m_properties
; p
; p
= p2
)
97 wxXmlNode
& wxXmlNode::operator=(const wxXmlNode
& node
)
99 wxDELETE(m_properties
);
100 wxDELETE(m_children
);
105 void wxXmlNode::DoCopy(const wxXmlNode
& node
)
107 m_type
= node
.m_type
;
108 m_name
= node
.m_name
;
109 m_content
= node
.m_content
;
112 wxXmlNode
*n
= node
.m_children
;
115 AddChild(new wxXmlNode(*n
));
120 wxXmlProperty
*p
= node
.m_properties
;
123 AddProperty(p
->GetName(), p
->GetValue());
128 bool wxXmlNode::HasProp(const wxString
& propName
) const
130 wxXmlProperty
*prop
= GetProperties();
134 if (prop
->GetName() == propName
) return true;
135 prop
= prop
->GetNext();
141 bool wxXmlNode::GetPropVal(const wxString
& propName
, wxString
*value
) const
143 wxXmlProperty
*prop
= GetProperties();
147 if (prop
->GetName() == propName
)
149 *value
= prop
->GetValue();
152 prop
= prop
->GetNext();
158 wxString
wxXmlNode::GetPropVal(const wxString
& propName
, const wxString
& defaultVal
) const
161 if (GetPropVal(propName
, &tmp
))
167 void wxXmlNode::AddChild(wxXmlNode
*child
)
169 if (m_children
== NULL
)
173 wxXmlNode
*ch
= m_children
;
174 while (ch
->m_next
) ch
= ch
->m_next
;
177 child
->m_next
= NULL
;
178 child
->m_parent
= this;
181 void wxXmlNode::InsertChild(wxXmlNode
*child
, wxXmlNode
*before_node
)
183 wxASSERT_MSG(before_node
->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
185 if (m_children
== before_node
)
189 wxXmlNode
*ch
= m_children
;
190 while (ch
->m_next
!= before_node
) ch
= ch
->m_next
;
194 child
->m_parent
= this;
195 child
->m_next
= before_node
;
198 bool wxXmlNode::RemoveChild(wxXmlNode
*child
)
200 if (m_children
== NULL
)
202 else if (m_children
== child
)
204 m_children
= child
->m_next
;
205 child
->m_parent
= NULL
;
206 child
->m_next
= NULL
;
211 wxXmlNode
*ch
= m_children
;
214 if (ch
->m_next
== child
)
216 ch
->m_next
= child
->m_next
;
217 child
->m_parent
= NULL
;
218 child
->m_next
= NULL
;
227 void wxXmlNode::AddProperty(const wxString
& name
, const wxString
& value
)
229 AddProperty(new wxXmlProperty(name
, value
, NULL
));
232 void wxXmlNode::AddProperty(wxXmlProperty
*prop
)
234 if (m_properties
== NULL
)
238 wxXmlProperty
*p
= m_properties
;
239 while (p
->GetNext()) p
= p
->GetNext();
244 bool wxXmlNode::DeleteProperty(const wxString
& name
)
248 if (m_properties
== NULL
)
251 else if (m_properties
->GetName() == name
)
254 m_properties
= prop
->GetNext();
262 wxXmlProperty
*p
= m_properties
;
265 if (p
->GetNext()->GetName() == name
)
268 p
->SetNext(prop
->GetNext());
279 wxString
wxXmlNode::GetNodeContent() const
281 wxXmlNode
*n
= GetChildren();
285 if (n
->GetType() == wxXML_TEXT_NODE
||
286 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
287 return n
->GetContent();
290 return wxEmptyString
;
295 //-----------------------------------------------------------------------------
297 //-----------------------------------------------------------------------------
299 wxXmlDocument::wxXmlDocument()
300 : m_version(wxT("1.0")), m_fileEncoding(wxT("utf-8")), m_root(NULL
)
303 m_encoding
= wxT("UTF-8");
307 wxXmlDocument::wxXmlDocument(const wxString
& filename
, const wxString
& encoding
)
308 :wxObject(), m_root(NULL
)
310 if ( !Load(filename
, encoding
) )
316 wxXmlDocument::wxXmlDocument(wxInputStream
& stream
, const wxString
& encoding
)
317 :wxObject(), m_root(NULL
)
319 if ( !Load(stream
, encoding
) )
325 wxXmlDocument::wxXmlDocument(const wxXmlDocument
& doc
)
331 wxXmlDocument
& wxXmlDocument::operator=(const wxXmlDocument
& doc
)
338 void wxXmlDocument::DoCopy(const wxXmlDocument
& doc
)
340 m_version
= doc
.m_version
;
342 m_encoding
= doc
.m_encoding
;
344 m_fileEncoding
= doc
.m_fileEncoding
;
345 m_root
= new wxXmlNode(*doc
.m_root
);
348 bool wxXmlDocument::Load(const wxString
& filename
, const wxString
& encoding
)
350 wxFileInputStream
stream(filename
);
353 return Load(stream
, encoding
);
356 bool wxXmlDocument::Save(const wxString
& filename
) const
358 wxFileOutputStream
stream(filename
);
366 //-----------------------------------------------------------------------------
367 // wxXmlDocument loading routines
368 //-----------------------------------------------------------------------------
372 - process all elements, including CDATA
375 // converts Expat-produced string in UTF-8 into wxString using the specified
376 // conv or keep in UTF-8 if conv is NULL
377 static wxString
CharToString(wxMBConv
*conv
,
378 const char *s
, size_t len
= wxSTRING_MAXLEN
)
383 return wxString(s
, wxConvUTF8
, len
);
384 #else // !wxUSE_UNICODE
387 // there can be no embedded NULs in this string so we don't need the
388 // output length, it will be NUL-terminated
389 const wxWCharBuffer
wbuf(
390 wxConvUTF8
.cMB2WC(s
, len
== wxSTRING_MAXLEN
? wxNO_LEN
: len
, NULL
));
392 return wxString(wbuf
, *conv
);
394 else // already in UTF-8, no conversion needed
396 return wxString(s
, len
!= wxSTRING_MAXLEN
? len
: strlen(s
));
398 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
401 struct wxXmlParsingContext
406 wxXmlNode
*lastAsText
;
413 static void StartElementHnd(void *userData
, const char *name
, const char **atts
)
415 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
416 wxXmlNode
*node
= new wxXmlNode(wxXML_ELEMENT_NODE
, CharToString(ctx
->conv
, name
));
417 const char **a
= atts
;
420 node
->AddProperty(CharToString(ctx
->conv
, a
[0]), CharToString(ctx
->conv
, a
[1]));
423 if (ctx
->root
== NULL
)
426 ctx
->node
->AddChild(node
);
428 ctx
->lastAsText
= NULL
;
433 static void EndElementHnd(void *userData
, const char* WXUNUSED(name
))
435 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
437 ctx
->node
= ctx
->node
->GetParent();
438 ctx
->lastAsText
= NULL
;
443 static void TextHnd(void *userData
, const char *s
, int len
)
445 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
446 char *buf
= new char[len
+ 1];
449 memcpy(buf
, s
, (size_t)len
);
453 if ( ctx
->bLastCdata
)
455 ctx
->lastAsText
->SetContent(ctx
->lastAsText
->GetContent() +
456 CharToString(NULL
, buf
));
460 ctx
->lastAsText
->SetContent(ctx
->lastAsText
->GetContent() +
461 CharToString(ctx
->conv
, buf
));
466 bool whiteOnly
= true;
467 for (char *c
= buf
; *c
!= '\0'; c
++)
468 if (*c
!= ' ' && *c
!= '\t' && *c
!= '\n' && *c
!= '\r')
475 ctx
->lastAsText
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"),
476 CharToString(ctx
->conv
, buf
));
477 ctx
->node
->AddChild(ctx
->lastAsText
);
486 static void StartCdataHnd(void *userData
)
488 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
490 ctx
->bLastCdata
= true;
492 ctx
->lastAsText
= new wxXmlNode(wxXML_CDATA_SECTION_NODE
, wxT("cdata"),wxT(""));
493 ctx
->node
->AddChild(ctx
->lastAsText
);
498 static void EndCdataHnd(void *userData
)
500 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
502 ctx
->bLastCdata
= false;
507 static void CommentHnd(void *userData
, const char *data
)
509 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
513 // VS: ctx->node == NULL happens if there is a comment before
514 // the root element (e.g. wxDesigner's output). We ignore such
515 // comments, no big deal...
516 ctx
->node
->AddChild(new wxXmlNode(wxXML_COMMENT_NODE
,
517 wxT("comment"), CharToString(ctx
->conv
, data
)));
519 ctx
->lastAsText
= NULL
;
524 static void DefaultHnd(void *userData
, const char *s
, int len
)
527 if (len
> 6 && memcmp(s
, "<?xml ", 6) == 0)
529 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
531 wxString buf
= CharToString(ctx
->conv
, s
, (size_t)len
);
533 pos
= buf
.Find(wxT("encoding="));
534 if (pos
!= wxNOT_FOUND
)
535 ctx
->encoding
= buf
.Mid(pos
+ 10).BeforeFirst(buf
[(size_t)pos
+9]);
536 pos
= buf
.Find(wxT("version="));
537 if (pos
!= wxNOT_FOUND
)
538 ctx
->version
= buf
.Mid(pos
+ 9).BeforeFirst(buf
[(size_t)pos
+8]);
544 static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData
),
545 const XML_Char
*name
, XML_Encoding
*info
)
547 // We must build conversion table for expat. The easiest way to do so
548 // is to let wxCSConv convert as string containing all characters to
549 // wide character representation:
550 wxString
str(name
, wxConvLibc
);
558 for (i
= 0; i
< 255; i
++)
560 mbBuf
[0] = (char)(i
+1);
561 if (conv
.MB2WC(wcBuf
, mbBuf
, 2) == (size_t)-1)
563 // invalid/undefined byte in the encoding:
566 info
->map
[i
+1] = (int)wcBuf
[0];
570 info
->convert
= NULL
;
571 info
->release
= NULL
;
577 bool wxXmlDocument::Load(wxInputStream
& stream
, const wxString
& encoding
)
582 m_encoding
= encoding
;
585 const size_t BUFSIZE
= 1024;
587 wxXmlParsingContext ctx
;
589 XML_Parser parser
= XML_ParserCreate(NULL
);
591 ctx
.root
= ctx
.node
= NULL
;
592 ctx
.encoding
= wxT("UTF-8"); // default in absence of encoding=""
595 if ( encoding
!= wxT("UTF-8") && encoding
!= wxT("utf-8") )
596 ctx
.conv
= new wxCSConv(encoding
);
598 ctx
.bLastCdata
= false;
600 XML_SetUserData(parser
, (void*)&ctx
);
601 XML_SetElementHandler(parser
, StartElementHnd
, EndElementHnd
);
602 XML_SetCharacterDataHandler(parser
, TextHnd
);
603 XML_SetCdataSectionHandler(parser
, StartCdataHnd
, EndCdataHnd
);
604 XML_SetCommentHandler(parser
, CommentHnd
);
605 XML_SetDefaultHandler(parser
, DefaultHnd
);
606 XML_SetUnknownEncodingHandler(parser
, UnknownEncodingHnd
, NULL
);
611 size_t len
= stream
.Read(buf
, BUFSIZE
).LastRead();
612 done
= (len
< BUFSIZE
);
613 if (!XML_Parse(parser
, buf
, len
, done
))
615 wxString
error(XML_ErrorString(XML_GetErrorCode(parser
)),
617 wxLogError(_("XML parsing error: '%s' at line %d"),
619 XML_GetCurrentLineNumber(parser
));
627 if (!ctx
.version
.empty())
628 SetVersion(ctx
.version
);
629 if (!ctx
.encoding
.empty())
630 SetFileEncoding(ctx
.encoding
);
638 XML_ParserFree(parser
);
650 //-----------------------------------------------------------------------------
651 // wxXmlDocument saving routines
652 //-----------------------------------------------------------------------------
654 // write string to output:
655 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
656 wxMBConv
*convMem
= NULL
,
657 wxMBConv
*convFile
= NULL
)
663 wxUnusedVar(convMem
);
665 const wxWX2MBbuf
buf(str
.mb_str(*(convFile
? convFile
: &wxConvUTF8
)));
666 stream
.Write((const char*)buf
, strlen((const char*)buf
));
667 #else // !wxUSE_UNICODE
668 if ( convFile
&& convMem
)
670 wxString
str2(str
.wc_str(*convMem
), *convFile
);
671 stream
.Write(str2
.mb_str(), str2
.Len());
673 else // no conversions to do
675 stream
.Write(str
.mb_str(), str
.Len());
677 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
680 // flags for OutputStringEnt()
683 XML_ESCAPE_QUOTES
= 1
686 // Same as above, but create entities first.
687 // Translates '<' to "<", '>' to ">" and '&' to "&"
688 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
689 wxMBConv
*convMem
= NULL
,
690 wxMBConv
*convFile
= NULL
,
699 for (i
= 0; i
< len
; i
++)
702 if (c
== wxT('<') || c
== wxT('>') ||
703 (c
== wxT('&') && str
.Mid(i
+1, 4) != wxT("amp;")) ||
704 ((flags
& XML_ESCAPE_QUOTES
) && c
== wxT('"')))
706 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
710 OutputString(stream
, wxT("<"));
713 OutputString(stream
, wxT(">"));
716 OutputString(stream
, wxT("&"));
719 OutputString(stream
, wxT("""));
727 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
730 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
)
732 wxString str
= wxT("\n");
733 for (int i
= 0; i
< indent
; i
++)
734 str
<< wxT(' ') << wxT(' ');
735 OutputString(stream
, str
);
738 static void OutputNode(wxOutputStream
& stream
, wxXmlNode
*node
, int indent
,
739 wxMBConv
*convMem
, wxMBConv
*convFile
)
744 switch (node
->GetType())
746 case wxXML_CDATA_SECTION_NODE
:
747 OutputString( stream
, wxT("<![CDATA["));
748 OutputString( stream
, node
->GetContent() );
749 OutputString( stream
, wxT("]]>") );
752 case wxXML_TEXT_NODE
:
753 OutputStringEnt(stream
, node
->GetContent(), convMem
, convFile
);
756 case wxXML_ELEMENT_NODE
:
757 OutputString(stream
, wxT("<"));
758 OutputString(stream
, node
->GetName());
760 prop
= node
->GetProperties();
763 OutputString(stream
, wxT(" ") + prop
->GetName() + wxT("=\""));
764 OutputStringEnt(stream
, prop
->GetValue(), convMem
, convFile
,
766 OutputString(stream
, wxT("\""));
767 prop
= prop
->GetNext();
770 if (node
->GetChildren())
772 OutputString(stream
, wxT(">"));
774 n
= node
->GetChildren();
777 if (n
&& n
->GetType() != wxXML_TEXT_NODE
)
778 OutputIndentation(stream
, indent
+ 1);
779 OutputNode(stream
, n
, indent
+ 1, convMem
, convFile
);
783 if (prev
&& prev
->GetType() != wxXML_TEXT_NODE
)
784 OutputIndentation(stream
, indent
);
785 OutputString(stream
, wxT("</"));
786 OutputString(stream
, node
->GetName());
787 OutputString(stream
, wxT(">"));
790 OutputString(stream
, wxT("/>"));
793 case wxXML_COMMENT_NODE
:
794 OutputString(stream
, wxT("<!--"));
795 OutputString(stream
, node
->GetContent(), convMem
, convFile
);
796 OutputString(stream
, wxT("-->"));
800 wxFAIL_MSG(wxT("unsupported node type"));
804 bool wxXmlDocument::Save(wxOutputStream
& stream
) const
811 wxMBConv
*convMem
= NULL
;
814 wxMBConv
*convFile
= new wxCSConv(GetFileEncoding());
816 wxMBConv
*convFile
= NULL
;
817 if ( GetFileEncoding() != GetEncoding() )
819 convFile
= new wxCSConv(GetFileEncoding());
820 convMem
= new wxCSConv(GetEncoding());
824 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
825 GetVersion().c_str(), GetFileEncoding().c_str());
826 OutputString(stream
, s
);
828 OutputNode(stream
, GetRoot(), 0, convMem
, convFile
);
829 OutputString(stream
, wxT("\n"));