]>
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"
22 #include "wx/wfstream.h"
23 #include "wx/datstrm.h"
24 #include "wx/zstream.h"
27 #include "wx/strconv.h"
29 #include "expat.h" // from Expat
31 // DLL options compatibility check:
33 WX_CHECK_BUILD_OPTIONS("wxXML")
36 IMPLEMENT_CLASS(wxXmlDocument
, wxObject
)
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 wxXmlNode::wxXmlNode(wxXmlNode
*parent
,wxXmlNodeType type
,
45 const wxString
& name
, const wxString
& content
,
46 wxXmlProperty
*props
, wxXmlNode
*next
)
47 : m_type(type
), m_name(name
), m_content(content
),
48 m_properties(props
), m_parent(parent
),
49 m_children(NULL
), m_next(next
)
53 if (m_parent
->m_children
)
55 m_next
= m_parent
->m_children
;
56 m_parent
->m_children
= this;
59 m_parent
->m_children
= this;
63 wxXmlNode::wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
64 const wxString
& content
)
65 : m_type(type
), m_name(name
), m_content(content
),
66 m_properties(NULL
), m_parent(NULL
),
67 m_children(NULL
), m_next(NULL
)
70 wxXmlNode::wxXmlNode(const wxXmlNode
& node
)
77 wxXmlNode::~wxXmlNode()
80 for (c
= m_children
; c
; c
= c2
)
86 wxXmlProperty
*p
, *p2
;
87 for (p
= m_properties
; p
; p
= p2
)
94 wxXmlNode
& wxXmlNode::operator=(const wxXmlNode
& node
)
96 wxDELETE(m_properties
);
102 void wxXmlNode::DoCopy(const wxXmlNode
& node
)
104 m_type
= node
.m_type
;
105 m_name
= node
.m_name
;
106 m_content
= node
.m_content
;
109 wxXmlNode
*n
= node
.m_children
;
112 AddChild(new wxXmlNode(*n
));
117 wxXmlProperty
*p
= node
.m_properties
;
120 AddProperty(p
->GetName(), p
->GetValue());
125 bool wxXmlNode::HasProp(const wxString
& propName
) const
127 wxXmlProperty
*prop
= GetProperties();
131 if (prop
->GetName() == propName
) return true;
132 prop
= prop
->GetNext();
138 bool wxXmlNode::GetPropVal(const wxString
& propName
, wxString
*value
) const
140 wxXmlProperty
*prop
= GetProperties();
144 if (prop
->GetName() == propName
)
146 *value
= prop
->GetValue();
149 prop
= prop
->GetNext();
155 wxString
wxXmlNode::GetPropVal(const wxString
& propName
, const wxString
& defaultVal
) const
158 if (GetPropVal(propName
, &tmp
))
164 void wxXmlNode::AddChild(wxXmlNode
*child
)
166 if (m_children
== NULL
)
170 wxXmlNode
*ch
= m_children
;
171 while (ch
->m_next
) ch
= ch
->m_next
;
174 child
->m_next
= NULL
;
175 child
->m_parent
= this;
178 void wxXmlNode::InsertChild(wxXmlNode
*child
, wxXmlNode
*before_node
)
180 wxASSERT_MSG(before_node
->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
182 if (m_children
== before_node
)
186 wxXmlNode
*ch
= m_children
;
187 while (ch
->m_next
!= before_node
) ch
= ch
->m_next
;
191 child
->m_parent
= this;
192 child
->m_next
= before_node
;
195 bool wxXmlNode::RemoveChild(wxXmlNode
*child
)
197 if (m_children
== NULL
)
199 else if (m_children
== child
)
201 m_children
= child
->m_next
;
202 child
->m_parent
= NULL
;
203 child
->m_next
= NULL
;
208 wxXmlNode
*ch
= m_children
;
211 if (ch
->m_next
== child
)
213 ch
->m_next
= child
->m_next
;
214 child
->m_parent
= NULL
;
215 child
->m_next
= NULL
;
224 void wxXmlNode::AddProperty(const wxString
& name
, const wxString
& value
)
226 AddProperty(new wxXmlProperty(name
, value
, NULL
));
229 void wxXmlNode::AddProperty(wxXmlProperty
*prop
)
231 if (m_properties
== NULL
)
235 wxXmlProperty
*p
= m_properties
;
236 while (p
->GetNext()) p
= p
->GetNext();
241 bool wxXmlNode::DeleteProperty(const wxString
& name
)
245 if (m_properties
== NULL
)
248 else if (m_properties
->GetName() == name
)
251 m_properties
= prop
->GetNext();
259 wxXmlProperty
*p
= m_properties
;
262 if (p
->GetNext()->GetName() == name
)
265 p
->SetNext(prop
->GetNext());
276 wxString
wxXmlNode::GetNodeContent() const
278 wxXmlNode
*n
= GetChildren();
282 if (n
->GetType() == wxXML_TEXT_NODE
||
283 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
284 return n
->GetContent();
287 return wxEmptyString
;
292 //-----------------------------------------------------------------------------
294 //-----------------------------------------------------------------------------
296 wxXmlDocument::wxXmlDocument()
297 : m_version(wxT("1.0")), m_fileEncoding(wxT("utf-8")), m_root(NULL
)
300 m_encoding
= wxT("UTF-8");
304 wxXmlDocument::wxXmlDocument(const wxString
& filename
, const wxString
& encoding
)
305 :wxObject(), m_root(NULL
)
307 if ( !Load(filename
, encoding
) )
313 wxXmlDocument::wxXmlDocument(wxInputStream
& stream
, const wxString
& encoding
)
314 :wxObject(), m_root(NULL
)
316 if ( !Load(stream
, encoding
) )
322 wxXmlDocument::wxXmlDocument(const wxXmlDocument
& doc
)
328 wxXmlDocument
& wxXmlDocument::operator=(const wxXmlDocument
& doc
)
335 void wxXmlDocument::DoCopy(const wxXmlDocument
& doc
)
337 m_version
= doc
.m_version
;
339 m_encoding
= doc
.m_encoding
;
341 m_fileEncoding
= doc
.m_fileEncoding
;
342 m_root
= new wxXmlNode(*doc
.m_root
);
345 bool wxXmlDocument::Load(const wxString
& filename
, const wxString
& encoding
)
347 wxFileInputStream
stream(filename
);
348 return Load(stream
, encoding
);
351 bool wxXmlDocument::Save(const wxString
& filename
) const
353 wxFileOutputStream
stream(filename
);
359 //-----------------------------------------------------------------------------
360 // wxXmlDocument loading routines
361 //-----------------------------------------------------------------------------
365 - process all elements, including CDATA
368 // converts Expat-produced string in UTF-8 into wxString using the specified
369 // conv or keep in UTF-8 if conv is NULL
370 static wxString
CharToString(wxMBConv
*conv
,
371 const char *s
, size_t len
= wxSTRING_MAXLEN
)
376 return wxString(s
, wxConvUTF8
, len
);
377 #else // !wxUSE_UNICODE
380 // there can be no embedded NULs in this string so we don't need the
381 // output length, it will be NUL-terminated
382 const wxWCharBuffer
wbuf(
383 wxConvUTF8
.cMB2WC(s
, len
== wxSTRING_MAXLEN
? wxNO_LEN
: len
, NULL
));
385 return wxString(wbuf
, conv
);
387 else // already in UTF-8, no conversion needed
389 return wxString(s
, len
!= wxSTRING_MAXLEN
? len
: strlen(s
));
391 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
394 struct wxXmlParsingContext
399 wxXmlNode
*lastAsText
;
405 static void StartElementHnd(void *userData
, const char *name
, const char **atts
)
407 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
408 wxXmlNode
*node
= new wxXmlNode(wxXML_ELEMENT_NODE
, CharToString(ctx
->conv
, name
));
409 const char **a
= atts
;
412 node
->AddProperty(CharToString(ctx
->conv
, a
[0]), CharToString(ctx
->conv
, a
[1]));
415 if (ctx
->root
== NULL
)
418 ctx
->node
->AddChild(node
);
420 ctx
->lastAsText
= NULL
;
425 static void EndElementHnd(void *userData
, const char* WXUNUSED(name
))
427 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
429 ctx
->node
= ctx
->node
->GetParent();
430 ctx
->lastAsText
= NULL
;
435 static void TextHnd(void *userData
, const char *s
, int len
)
437 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
438 char *buf
= new char[len
+ 1];
441 memcpy(buf
, s
, (size_t)len
);
445 ctx
->lastAsText
->SetContent(ctx
->lastAsText
->GetContent() +
446 CharToString(ctx
->conv
, buf
));
450 bool whiteOnly
= true;
451 for (char *c
= buf
; *c
!= '\0'; c
++)
452 if (*c
!= ' ' && *c
!= '\t' && *c
!= '\n' && *c
!= '\r')
459 ctx
->lastAsText
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"),
460 CharToString(ctx
->conv
, buf
));
461 ctx
->node
->AddChild(ctx
->lastAsText
);
470 static void CommentHnd(void *userData
, const char *data
)
472 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
476 // VS: ctx->node == NULL happens if there is a comment before
477 // the root element (e.g. wxDesigner's output). We ignore such
478 // comments, no big deal...
479 ctx
->node
->AddChild(new wxXmlNode(wxXML_COMMENT_NODE
,
480 wxT("comment"), CharToString(ctx
->conv
, data
)));
482 ctx
->lastAsText
= NULL
;
487 static void DefaultHnd(void *userData
, const char *s
, int len
)
490 if (len
> 6 && memcmp(s
, "<?xml ", 6) == 0)
492 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
494 wxString buf
= CharToString(ctx
->conv
, s
, (size_t)len
);
496 pos
= buf
.Find(wxT("encoding="));
497 if (pos
!= wxNOT_FOUND
)
498 ctx
->encoding
= buf
.Mid(pos
+ 10).BeforeFirst(buf
[(size_t)pos
+9]);
499 pos
= buf
.Find(wxT("version="));
500 if (pos
!= wxNOT_FOUND
)
501 ctx
->version
= buf
.Mid(pos
+ 9).BeforeFirst(buf
[(size_t)pos
+8]);
507 static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData
),
508 const XML_Char
*name
, XML_Encoding
*info
)
510 // We must build conversion table for expat. The easiest way to do so
511 // is to let wxCSConv convert as string containing all characters to
512 // wide character representation:
513 wxString
str(name
, wxConvLibc
);
521 for (i
= 0; i
< 255; i
++)
523 mbBuf
[0] = (char)(i
+1);
524 if (conv
.MB2WC(wcBuf
, mbBuf
, 2) == (size_t)-1)
526 // invalid/undefined byte in the encoding:
529 info
->map
[i
+1] = (int)wcBuf
[0];
533 info
->convert
= NULL
;
534 info
->release
= NULL
;
540 bool wxXmlDocument::Load(wxInputStream
& stream
, const wxString
& encoding
)
545 m_encoding
= encoding
;
548 const size_t BUFSIZE
= 1024;
550 wxXmlParsingContext ctx
;
552 XML_Parser parser
= XML_ParserCreate(NULL
);
554 ctx
.root
= ctx
.node
= NULL
;
555 ctx
.encoding
= wxT("UTF-8"); // default in absence of encoding=""
558 if ( encoding
!= wxT("UTF-8") && encoding
!= wxT("utf-8") )
559 ctx
.conv
= new wxCSConv(encoding
);
562 XML_SetUserData(parser
, (void*)&ctx
);
563 XML_SetElementHandler(parser
, StartElementHnd
, EndElementHnd
);
564 XML_SetCharacterDataHandler(parser
, TextHnd
);
565 XML_SetCommentHandler(parser
, CommentHnd
);
566 XML_SetDefaultHandler(parser
, DefaultHnd
);
567 XML_SetUnknownEncodingHandler(parser
, UnknownEncodingHnd
, NULL
);
572 size_t len
= stream
.Read(buf
, BUFSIZE
).LastRead();
573 done
= (len
< BUFSIZE
);
574 if (!XML_Parse(parser
, buf
, len
, done
))
576 wxString
error(XML_ErrorString(XML_GetErrorCode(parser
)),
578 wxLogError(_("XML parsing error: '%s' at line %d"),
580 XML_GetCurrentLineNumber(parser
));
588 if (!ctx
.version
.empty())
589 SetVersion(ctx
.version
);
590 if (!ctx
.encoding
.empty())
591 SetFileEncoding(ctx
.encoding
);
599 XML_ParserFree(parser
);
611 //-----------------------------------------------------------------------------
612 // wxXmlDocument saving routines
613 //-----------------------------------------------------------------------------
615 // write string to output:
616 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
617 wxMBConv
*convMem
= NULL
,
618 wxMBConv
*convFile
= NULL
)
624 wxUnusedVar(convMem
);
626 const wxWX2MBbuf
buf(str
.mb_str(*(convFile
? convFile
: &wxConvUTF8
)));
627 stream
.Write((const char*)buf
, strlen((const char*)buf
));
628 #else // !wxUSE_UNICODE
629 if ( convFile
&& convMem
)
631 wxString
str2(str
.wc_str(*convMem
), *convFile
);
632 stream
.Write(str2
.mb_str(), str2
.Len());
634 else // no conversions to do
636 stream
.Write(str
.mb_str(), str
.Len());
638 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
641 // flags for OutputStringEnt()
644 XML_ESCAPE_QUOTES
= 1
647 // Same as above, but create entities first.
648 // Translates '<' to "<", '>' to ">" and '&' to "&"
649 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
650 wxMBConv
*convMem
= NULL
,
651 wxMBConv
*convFile
= NULL
,
660 for (i
= 0; i
< len
; i
++)
663 if (c
== wxT('<') || c
== wxT('>') ||
664 (c
== wxT('&') && str
.Mid(i
+1, 4) != wxT("amp;")) ||
665 ((flags
& XML_ESCAPE_QUOTES
) && c
== wxT('"')))
667 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
671 OutputString(stream
, wxT("<"));
674 OutputString(stream
, wxT(">"));
677 OutputString(stream
, wxT("&"));
680 OutputString(stream
, wxT("""));
688 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
691 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
)
693 wxString str
= wxT("\n");
694 for (int i
= 0; i
< indent
; i
++)
695 str
<< wxT(' ') << wxT(' ');
696 OutputString(stream
, str
);
699 static void OutputNode(wxOutputStream
& stream
, wxXmlNode
*node
, int indent
,
700 wxMBConv
*convMem
, wxMBConv
*convFile
)
705 switch (node
->GetType())
707 case wxXML_TEXT_NODE
:
708 OutputStringEnt(stream
, node
->GetContent(), convMem
, convFile
);
711 case wxXML_ELEMENT_NODE
:
712 OutputString(stream
, wxT("<"));
713 OutputString(stream
, node
->GetName());
715 prop
= node
->GetProperties();
718 OutputString(stream
, wxT(" ") + prop
->GetName() + wxT("=\""));
719 OutputStringEnt(stream
, prop
->GetValue(), convMem
, convFile
,
721 OutputString(stream
, wxT("\""));
722 prop
= prop
->GetNext();
725 if (node
->GetChildren())
727 OutputString(stream
, wxT(">"));
729 n
= node
->GetChildren();
732 if (n
&& n
->GetType() != wxXML_TEXT_NODE
)
733 OutputIndentation(stream
, indent
+ 1);
734 OutputNode(stream
, n
, indent
+ 1, convMem
, convFile
);
738 if (prev
&& prev
->GetType() != wxXML_TEXT_NODE
)
739 OutputIndentation(stream
, indent
);
740 OutputString(stream
, wxT("</"));
741 OutputString(stream
, node
->GetName());
742 OutputString(stream
, wxT(">"));
745 OutputString(stream
, wxT("/>"));
748 case wxXML_COMMENT_NODE
:
749 OutputString(stream
, wxT("<!--"));
750 OutputString(stream
, node
->GetContent(), convMem
, convFile
);
751 OutputString(stream
, wxT("-->"));
755 wxFAIL_MSG(wxT("unsupported node type"));
759 bool wxXmlDocument::Save(wxOutputStream
& stream
) const
766 wxMBConv
*convMem
= NULL
;
769 wxMBConv
*convFile
= new wxCSConv(GetFileEncoding());
771 wxMBConv
*convFile
= NULL
;
772 if ( GetFileEncoding() != GetEncoding() )
774 convFile
= new wxCSConv(GetFileEncoding());
775 convMem
= new wxCSConv(GetEncoding());
779 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
780 GetVersion().c_str(), GetFileEncoding().c_str());
781 OutputString(stream
, s
);
783 OutputNode(stream
, GetRoot(), 0, convMem
, convFile
);
784 OutputString(stream
, wxT("\n"));