]>
git.saurik.com Git - wxWidgets.git/blob - src/xml/xml.cpp
   1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxXmlDocument - XML parser & data holder class 
   4 // Author:      Vaclav Slavik 
   7 // Copyright:   (c) 2000 Vaclav Slavik 
   8 // Licence:     wxWindows licence 
   9 ///////////////////////////////////////////////////////////////////////////// 
  11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) 
  12 #pragma implementation "xml.h" 
  15 // For compilers that support precompilation, includes "wx.h". 
  16 #include "wx/wxprec.h" 
  22 #include "wx/xml/xml.h" 
  26 #include "wx/wfstream.h" 
  27 #include "wx/datstrm.h" 
  28 #include "wx/zstream.h" 
  31 #include "wx/strconv.h" 
  33 #include "expat.h" // from Expat 
  35 // DLL options compatibility check: 
  37 WX_CHECK_BUILD_OPTIONS("wxXML") 
  39 //----------------------------------------------------------------------------- 
  41 //----------------------------------------------------------------------------- 
  43 wxXmlNode::wxXmlNode(wxXmlNode 
*parent
,wxXmlNodeType type
, 
  44                      const wxString
& name
, const wxString
& content
, 
  45                      wxXmlProperty 
*props
, wxXmlNode 
*next
) 
  46     : m_type(type
), m_name(name
), m_content(content
), 
  47       m_properties(props
), m_parent(parent
), 
  48       m_children(NULL
), m_next(next
) 
  52         if (m_parent
->m_children
) 
  54             m_next 
= m_parent
->m_children
; 
  55             m_parent
->m_children 
= this; 
  58             m_parent
->m_children 
= this; 
  62 wxXmlNode::wxXmlNode(wxXmlNodeType type
, const wxString
& name
, 
  63                      const wxString
& content
) 
  64     : m_type(type
), m_name(name
), m_content(content
), 
  65       m_properties(NULL
), m_parent(NULL
), 
  66       m_children(NULL
), m_next(NULL
) 
  69 wxXmlNode::wxXmlNode(const wxXmlNode
& node
) 
  76 wxXmlNode::~wxXmlNode() 
  79     for (c 
= m_children
; c
; c 
= c2
) 
  85     wxXmlProperty 
*p
, *p2
; 
  86     for (p 
= m_properties
; p
; p 
= p2
) 
  93 wxXmlNode
& wxXmlNode::operator=(const wxXmlNode
& node
) 
  95     wxDELETE(m_properties
); 
 101 void wxXmlNode::DoCopy(const wxXmlNode
& node
) 
 103     m_type 
= node
.m_type
; 
 104     m_name 
= node
.m_name
; 
 105     m_content 
= node
.m_content
; 
 108     wxXmlNode 
*n 
= node
.m_children
; 
 111         AddChild(new wxXmlNode(*n
)); 
 116     wxXmlProperty 
*p 
= node
.m_properties
; 
 119        AddProperty(p
->GetName(), p
->GetValue()); 
 124 bool wxXmlNode::HasProp(const wxString
& propName
) const 
 126     wxXmlProperty 
*prop 
= GetProperties(); 
 130         if (prop
->GetName() == propName
) return TRUE
; 
 131         prop 
= prop
->GetNext(); 
 137 bool wxXmlNode::GetPropVal(const wxString
& propName
, wxString 
*value
) const 
 139     wxXmlProperty 
*prop 
= GetProperties(); 
 143         if (prop
->GetName() == propName
) 
 145             *value 
= prop
->GetValue(); 
 148         prop 
= prop
->GetNext(); 
 154 wxString 
wxXmlNode::GetPropVal(const wxString
& propName
, const wxString
& defaultVal
) const 
 157     if (GetPropVal(propName
, &tmp
)) 
 163 void wxXmlNode::AddChild(wxXmlNode 
*child
) 
 165     if (m_children 
== NULL
) 
 169         wxXmlNode 
*ch 
= m_children
; 
 170         while (ch
->m_next
) ch 
= ch
->m_next
; 
 173     child
->m_next 
= NULL
; 
 174     child
->m_parent 
= this; 
 177 void wxXmlNode::InsertChild(wxXmlNode 
*child
, wxXmlNode 
*before_node
) 
 179     wxASSERT_MSG(before_node
->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent")); 
 181     if (m_children 
== before_node
) 
 185         wxXmlNode 
*ch 
= m_children
; 
 186         while (ch
->m_next 
!= before_node
) ch 
= ch
->m_next
; 
 190     child
->m_parent 
= this; 
 191     child
->m_next 
= before_node
; 
 194 bool wxXmlNode::RemoveChild(wxXmlNode 
*child
) 
 196     if (m_children 
== NULL
) 
 198     else if (m_children 
== child
) 
 200         m_children 
= child
->m_next
; 
 201         child
->m_parent 
= NULL
; 
 202         child
->m_next 
= NULL
; 
 207         wxXmlNode 
*ch 
= m_children
; 
 210             if (ch
->m_next 
== child
) 
 212                 ch
->m_next 
= child
->m_next
; 
 213                 child
->m_parent 
= NULL
; 
 214                 child
->m_next 
= NULL
; 
 223 void wxXmlNode::AddProperty(const wxString
& name
, const wxString
& value
) 
 225     AddProperty(new wxXmlProperty(name
, value
, NULL
)); 
 228 void wxXmlNode::AddProperty(wxXmlProperty 
*prop
) 
 230     if (m_properties 
== NULL
) 
 234         wxXmlProperty 
*p 
= m_properties
; 
 235         while (p
->GetNext()) p 
= p
->GetNext(); 
 240 bool wxXmlNode::DeleteProperty(const wxString
& name
) 
 244     if (m_properties 
== NULL
) 
 247     else if (m_properties
->GetName() == name
) 
 250         m_properties 
= prop
->GetNext(); 
 258         wxXmlProperty 
*p 
= m_properties
; 
 261             if (p
->GetNext()->GetName() == name
) 
 264                 p
->SetNext(prop
->GetNext()); 
 277 //----------------------------------------------------------------------------- 
 279 //----------------------------------------------------------------------------- 
 281 wxXmlDocument::wxXmlDocument() 
 282     : m_version(wxT("1.0")), m_fileEncoding(wxT("utf-8")), m_root(NULL
) 
 285     m_encoding 
= wxT("UTF-8"); 
 289 wxXmlDocument::wxXmlDocument(const wxString
& filename
, const wxString
& encoding
) 
 290                           : wxObject(), m_root(NULL
) 
 292     if ( !Load(filename
, encoding
) ) 
 298 wxXmlDocument::wxXmlDocument(wxInputStream
& stream
, const wxString
& encoding
) 
 299                           : wxObject(), m_root(NULL
) 
 301     if ( !Load(stream
, encoding
) ) 
 307 wxXmlDocument::wxXmlDocument(const wxXmlDocument
& doc
) 
 312 wxXmlDocument
& wxXmlDocument::operator=(const wxXmlDocument
& doc
) 
 319 void wxXmlDocument::DoCopy(const wxXmlDocument
& doc
) 
 321     m_version 
= doc
.m_version
; 
 323     m_encoding 
= doc
.m_encoding
; 
 325     m_fileEncoding 
= doc
.m_fileEncoding
; 
 326     m_root 
= new wxXmlNode(*doc
.m_root
); 
 329 bool wxXmlDocument::Load(const wxString
& filename
, const wxString
& encoding
) 
 331     wxFileInputStream 
stream(filename
); 
 332     return Load(stream
, encoding
); 
 335 bool wxXmlDocument::Save(const wxString
& filename
) const 
 337     wxFileOutputStream 
stream(filename
); 
 343 //----------------------------------------------------------------------------- 
 344 //  wxXmlDocument loading routines 
 345 //----------------------------------------------------------------------------- 
 349        - process all elements, including CDATA 
 352 // converts Expat-produced string in UTF-8 into wxString. 
 353 inline static wxString 
CharToString(wxMBConv 
*conv
, 
 354                                     const char *s
, size_t len 
= wxSTRING_MAXLEN
) 
 358     return wxString(s
, wxConvUTF8
, len
); 
 362         size_t nLen 
= (len 
!= wxSTRING_MAXLEN
) ? len 
: 
 363                           wxConvUTF8
.MB2WC((wchar_t*) NULL
, s
, 0); 
 365         wchar_t *buf 
= new wchar_t[nLen
+1]; 
 366         wxConvUTF8
.MB2WC(buf
, s
, nLen
); 
 368         wxString 
str(buf
, *conv
, len
); 
 373         return wxString(s
, len 
!= wxSTRING_MAXLEN 
? len 
: strlen(s
)); 
 377 struct wxXmlParsingContext
 
 382     wxXmlNode 
*lastAsText
; 
 387 static void StartElementHnd(void *userData
, const char *name
, const char **atts
) 
 389     wxXmlParsingContext 
*ctx 
= (wxXmlParsingContext
*)userData
; 
 390     wxXmlNode 
*node 
= new wxXmlNode(wxXML_ELEMENT_NODE
, CharToString(ctx
->conv
, name
)); 
 391     const char **a 
= atts
; 
 394         node
->AddProperty(CharToString(ctx
->conv
, a
[0]), CharToString(ctx
->conv
, a
[1])); 
 397     if (ctx
->root 
== NULL
) 
 400         ctx
->node
->AddChild(node
); 
 402     ctx
->lastAsText 
= NULL
; 
 405 static void EndElementHnd(void *userData
, const char* WXUNUSED(name
)) 
 407     wxXmlParsingContext 
*ctx 
= (wxXmlParsingContext
*)userData
; 
 409     ctx
->node 
= ctx
->node
->GetParent(); 
 410     ctx
->lastAsText 
= NULL
; 
 413 static void TextHnd(void *userData
, const char *s
, int len
) 
 415     wxXmlParsingContext 
*ctx 
= (wxXmlParsingContext
*)userData
; 
 416     char *buf 
= new char[len 
+ 1]; 
 419     memcpy(buf
, s
, (size_t)len
); 
 423         ctx
->lastAsText
->SetContent(ctx
->lastAsText
->GetContent() + 
 424                                     CharToString(ctx
->conv
, buf
)); 
 428         bool whiteOnly 
= TRUE
; 
 429         for (char *c 
= buf
; *c 
!= '\0'; c
++) 
 430             if (*c 
!= ' ' && *c 
!= '\t' && *c 
!= '\n' && *c 
!= '\r') 
 437             ctx
->lastAsText 
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"), 
 438                                             CharToString(ctx
->conv
, buf
)); 
 439             ctx
->node
->AddChild(ctx
->lastAsText
); 
 446 static void CommentHnd(void *userData
, const char *data
) 
 448     wxXmlParsingContext 
*ctx 
= (wxXmlParsingContext
*)userData
; 
 452         // VS: ctx->node == NULL happens if there is a comment before 
 453         //     the root element (e.g. wxDesigner's output). We ignore such 
 454         //     comments, no big deal... 
 455         ctx
->node
->AddChild(new wxXmlNode(wxXML_COMMENT_NODE
, 
 456                             wxT("comment"), CharToString(ctx
->conv
, data
))); 
 458     ctx
->lastAsText 
= NULL
; 
 461 static void DefaultHnd(void *userData
, const char *s
, int len
) 
 464     if (len 
> 6 && memcmp(s
, "<?xml ", 6) == 0) 
 466         wxXmlParsingContext 
*ctx 
= (wxXmlParsingContext
*)userData
; 
 468         wxString buf 
= CharToString(ctx
->conv
, s
, (size_t)len
); 
 470         pos 
= buf
.Find(wxT("encoding=")); 
 471         if (pos 
!= wxNOT_FOUND
) 
 472             ctx
->encoding 
= buf
.Mid(pos 
+ 10).BeforeFirst(buf
[(size_t)pos
+9]); 
 473         pos 
= buf
.Find(wxT("version=")); 
 474         if (pos 
!= wxNOT_FOUND
) 
 475             ctx
->version 
= buf
.Mid(pos 
+ 9).BeforeFirst(buf
[(size_t)pos
+8]); 
 479 static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData
), 
 480                               const XML_Char 
*name
, XML_Encoding 
*info
) 
 482     // We must build conversion table for expat. The easiest way to do so 
 483     // is to let wxCSConv convert as string containing all characters to 
 484     // wide character representation: 
 485     wxString 
str(name
, wxConvLibc
); 
 493     for (i 
= 0; i 
< 255; i
++) 
 495         mbBuf
[0] = (char)(i
+1); 
 496         if (conv
.MB2WC(wcBuf
, mbBuf
, 2) == (size_t)-1) 
 498             // invalid/undefined byte in the encoding: 
 501         info
->map
[i
+1] = (int)wcBuf
[0]; 
 505     info
->convert 
= NULL
; 
 506     info
->release 
= NULL
; 
 511 bool wxXmlDocument::Load(wxInputStream
& stream
, const wxString
& encoding
) 
 516     m_encoding 
= encoding
; 
 519     const size_t BUFSIZE 
= 1024; 
 521     wxXmlParsingContext ctx
; 
 523     XML_Parser parser 
= XML_ParserCreate(NULL
); 
 525     ctx
.root 
= ctx
.node 
= NULL
; 
 526     ctx
.encoding 
= wxT("UTF-8"); // default in absence of encoding="" 
 529     if ( encoding 
!= wxT("UTF-8") && encoding 
!= wxT("utf-8") ) 
 530         ctx
.conv 
= new wxCSConv(encoding
); 
 533     XML_SetUserData(parser
, (void*)&ctx
); 
 534     XML_SetElementHandler(parser
, StartElementHnd
, EndElementHnd
); 
 535     XML_SetCharacterDataHandler(parser
, TextHnd
); 
 536     XML_SetCommentHandler(parser
, CommentHnd
); 
 537     XML_SetDefaultHandler(parser
, DefaultHnd
); 
 538     XML_SetUnknownEncodingHandler(parser
, UnknownEncodingHnd
, NULL
); 
 543         size_t len 
= stream
.Read(buf
, BUFSIZE
).LastRead(); 
 544         done 
= (len 
< BUFSIZE
); 
 545         if (!XML_Parse(parser
, buf
, len
, done
)) 
 547             wxString 
error(XML_ErrorString(XML_GetErrorCode(parser
)), 
 549             wxLogError(_("XML parsing error: '%s' at line %d"), 
 551                        XML_GetCurrentLineNumber(parser
)); 
 559         if (!ctx
.version
.IsEmpty()) 
 560             SetVersion(ctx
.version
); 
 561         if (!ctx
.encoding
.IsEmpty()) 
 562             SetFileEncoding(ctx
.encoding
); 
 570     XML_ParserFree(parser
); 
 582 //----------------------------------------------------------------------------- 
 583 //  wxXmlDocument saving routines 
 584 //----------------------------------------------------------------------------- 
 586 // write string to output: 
 587 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
, 
 589     wxMBConv 
* WXUNUSED(convMem
), 
 595     if (str
.IsEmpty()) return; 
 597     const wxWX2MBbuf 
buf(str
.mb_str(*(convFile 
? convFile 
: &wxConvUTF8
))); 
 598     stream
.Write((const char*)buf
, strlen((const char*)buf
)); 
 600     if ( convFile 
== NULL 
) 
 601         stream
.Write(str
.mb_str(), str
.Len()); 
 604         wxString 
str2(str
.wc_str(*convMem
), *convFile
); 
 605         stream
.Write(str2
.mb_str(), str2
.Len()); 
 610 // Same as above, but create entities first. 
 611 // Translates '<' to "<", '>' to ">" and '&' to "&" 
 612 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
, 
 613                             wxMBConv 
*convMem
, wxMBConv 
*convFile
, 
 614                             bool escapeQuotes 
= false) 
 622     for (i 
= 0; i 
< len
; i
++) 
 625         if (c 
== wxT('<') || c 
== wxT('>') || 
 626             (c 
== wxT('&') && str
.Mid(i
+1, 4) != wxT("amp;")) || 
 627             (escapeQuotes 
&& c 
== wxT('"'))) 
 629             OutputString(stream
, str
.Mid(last
, i 
- last
), convMem
, convFile
); 
 633                     OutputString(stream
, wxT("<"), NULL
, NULL
); 
 636                     OutputString(stream
, wxT(">"), NULL
, NULL
); 
 639                     OutputString(stream
, wxT("&"), NULL
, NULL
); 
 642                     OutputString(stream
, wxT("""), NULL
, NULL
); 
 649     OutputString(stream
, str
.Mid(last
, i 
- last
), convMem
, convFile
); 
 652 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
) 
 654     wxString str 
= wxT("\n"); 
 655     for (int i 
= 0; i 
< indent
; i
++) 
 656         str 
<< wxT(' ') << wxT(' '); 
 657     OutputString(stream
, str
, NULL
, NULL
); 
 660 static void OutputNode(wxOutputStream
& stream
, wxXmlNode 
*node
, int indent
, 
 661                        wxMBConv 
*convMem
, wxMBConv 
*convFile
) 
 666     switch (node
->GetType()) 
 668         case wxXML_TEXT_NODE
: 
 669             OutputStringEnt(stream
, node
->GetContent(), convMem
, convFile
); 
 672         case wxXML_ELEMENT_NODE
: 
 673             OutputString(stream
, wxT("<"), NULL
, NULL
); 
 674             OutputString(stream
, node
->GetName(), NULL
, NULL
); 
 676             prop 
= node
->GetProperties(); 
 679                 OutputString(stream
, wxT(" ") + prop
->GetName() +  wxT("=\""), 
 681                 OutputStringEnt(stream
, prop
->GetValue(), NULL
, NULL
, 
 682                                 true/*escapeQuotes*/); 
 683                 OutputString(stream
, wxT("\""), NULL
, NULL
); 
 684                 prop 
= prop
->GetNext(); 
 687             if (node
->GetChildren()) 
 689                 OutputString(stream
, wxT(">"), NULL
, NULL
); 
 691                 n 
= node
->GetChildren(); 
 694                     if (n 
&& n
->GetType() != wxXML_TEXT_NODE
) 
 695                         OutputIndentation(stream
, indent 
+ 1); 
 696                     OutputNode(stream
, n
, indent 
+ 1, convMem
, convFile
); 
 700                 if (prev 
&& prev
->GetType() != wxXML_TEXT_NODE
) 
 701                     OutputIndentation(stream
, indent
); 
 702                 OutputString(stream
, wxT("</"), NULL
, NULL
); 
 703                 OutputString(stream
, node
->GetName(), NULL
, NULL
); 
 704                 OutputString(stream
, wxT(">"), NULL
, NULL
); 
 707                 OutputString(stream
, wxT("/>"), NULL
, NULL
); 
 710         case wxXML_COMMENT_NODE
: 
 711             OutputString(stream
, wxT("<!--"), NULL
, NULL
); 
 712             OutputString(stream
, node
->GetContent(), convMem
, convFile
); 
 713             OutputString(stream
, wxT("-->"), NULL
, NULL
); 
 717             wxFAIL_MSG(wxT("unsupported node type")); 
 721 bool wxXmlDocument::Save(wxOutputStream
& stream
) const 
 728     wxMBConv 
*convMem 
= NULL
, *convFile 
= NULL
; 
 730     convFile 
= new wxCSConv(GetFileEncoding()); 
 732     if ( GetFileEncoding() != GetEncoding() ) 
 734         convFile 
= new wxCSConv(GetFileEncoding()); 
 735         convMem 
= new wxCSConv(GetEncoding()); 
 739     s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"), 
 740              GetVersion().c_str(), GetFileEncoding().c_str()); 
 741     OutputString(stream
, s
, NULL
, NULL
); 
 743     OutputNode(stream
, GetRoot(), 0, convMem
, convFile
); 
 744     OutputString(stream
, wxT("\n"), NULL
, NULL
);