]>
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 // For compilers that support precompilation, includes "wx.h". 
  12 #include "wx/wxprec.h" 
  18 #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") 
  35 //----------------------------------------------------------------------------- 
  37 //----------------------------------------------------------------------------- 
  39 wxXmlNode::wxXmlNode(wxXmlNode 
*parent
,wxXmlNodeType type
, 
  40                      const wxString
& name
, const wxString
& content
, 
  41                      wxXmlProperty 
*props
, wxXmlNode 
*next
) 
  42     : m_type(type
), m_name(name
), m_content(content
), 
  43       m_properties(props
), m_parent(parent
), 
  44       m_children(NULL
), m_next(next
) 
  48         if (m_parent
->m_children
) 
  50             m_next 
= m_parent
->m_children
; 
  51             m_parent
->m_children 
= this; 
  54             m_parent
->m_children 
= this; 
  58 wxXmlNode::wxXmlNode(wxXmlNodeType type
, const wxString
& name
, 
  59                      const wxString
& content
) 
  60     : m_type(type
), m_name(name
), m_content(content
), 
  61       m_properties(NULL
), m_parent(NULL
), 
  62       m_children(NULL
), m_next(NULL
) 
  65 wxXmlNode::wxXmlNode(const wxXmlNode
& node
) 
  72 wxXmlNode::~wxXmlNode() 
  75     for (c 
= m_children
; c
; c 
= c2
) 
  81     wxXmlProperty 
*p
, *p2
; 
  82     for (p 
= m_properties
; p
; p 
= p2
) 
  89 wxXmlNode
& wxXmlNode::operator=(const wxXmlNode
& node
) 
  91     wxDELETE(m_properties
); 
  97 void wxXmlNode::DoCopy(const wxXmlNode
& node
) 
 100     m_name 
= node
.m_name
; 
 101     m_content 
= node
.m_content
; 
 104     wxXmlNode 
*n 
= node
.m_children
; 
 107         AddChild(new wxXmlNode(*n
)); 
 112     wxXmlProperty 
*p 
= node
.m_properties
; 
 115        AddProperty(p
->GetName(), p
->GetValue()); 
 120 bool wxXmlNode::HasProp(const wxString
& propName
) const 
 122     wxXmlProperty 
*prop 
= GetProperties(); 
 126         if (prop
->GetName() == propName
) return true; 
 127         prop 
= prop
->GetNext(); 
 133 bool wxXmlNode::GetPropVal(const wxString
& propName
, wxString 
*value
) const 
 135     wxXmlProperty 
*prop 
= GetProperties(); 
 139         if (prop
->GetName() == propName
) 
 141             *value 
= prop
->GetValue(); 
 144         prop 
= prop
->GetNext(); 
 150 wxString 
wxXmlNode::GetPropVal(const wxString
& propName
, const wxString
& defaultVal
) const 
 153     if (GetPropVal(propName
, &tmp
)) 
 159 void wxXmlNode::AddChild(wxXmlNode 
*child
) 
 161     if (m_children 
== NULL
) 
 165         wxXmlNode 
*ch 
= m_children
; 
 166         while (ch
->m_next
) ch 
= ch
->m_next
; 
 169     child
->m_next 
= NULL
; 
 170     child
->m_parent 
= this; 
 173 void wxXmlNode::InsertChild(wxXmlNode 
*child
, wxXmlNode 
*before_node
) 
 175     wxASSERT_MSG(before_node
->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent")); 
 177     if (m_children 
== before_node
) 
 181         wxXmlNode 
*ch 
= m_children
; 
 182         while (ch
->m_next 
!= before_node
) ch 
= ch
->m_next
; 
 186     child
->m_parent 
= this; 
 187     child
->m_next 
= before_node
; 
 190 bool wxXmlNode::RemoveChild(wxXmlNode 
*child
) 
 192     if (m_children 
== NULL
) 
 194     else if (m_children 
== child
) 
 196         m_children 
= child
->m_next
; 
 197         child
->m_parent 
= NULL
; 
 198         child
->m_next 
= NULL
; 
 203         wxXmlNode 
*ch 
= m_children
; 
 206             if (ch
->m_next 
== child
) 
 208                 ch
->m_next 
= child
->m_next
; 
 209                 child
->m_parent 
= NULL
; 
 210                 child
->m_next 
= NULL
; 
 219 void wxXmlNode::AddProperty(const wxString
& name
, const wxString
& value
) 
 221     AddProperty(new wxXmlProperty(name
, value
, NULL
)); 
 224 void wxXmlNode::AddProperty(wxXmlProperty 
*prop
) 
 226     if (m_properties 
== NULL
) 
 230         wxXmlProperty 
*p 
= m_properties
; 
 231         while (p
->GetNext()) p 
= p
->GetNext(); 
 236 bool wxXmlNode::DeleteProperty(const wxString
& name
) 
 240     if (m_properties 
== NULL
) 
 243     else if (m_properties
->GetName() == name
) 
 246         m_properties 
= prop
->GetNext(); 
 254         wxXmlProperty 
*p 
= m_properties
; 
 257             if (p
->GetNext()->GetName() == name
) 
 260                 p
->SetNext(prop
->GetNext()); 
 273 //----------------------------------------------------------------------------- 
 275 //----------------------------------------------------------------------------- 
 277 wxXmlDocument::wxXmlDocument() 
 278     : m_version(wxT("1.0")), m_fileEncoding(wxT("utf-8")), m_root(NULL
) 
 281     m_encoding 
= wxT("UTF-8"); 
 285 wxXmlDocument::wxXmlDocument(const wxString
& filename
, const wxString
& encoding
) 
 286               :wxObject(), m_root(NULL
) 
 288     if ( !Load(filename
, encoding
) ) 
 294 wxXmlDocument::wxXmlDocument(wxInputStream
& stream
, const wxString
& encoding
) 
 295               :wxObject(), m_root(NULL
) 
 297     if ( !Load(stream
, encoding
) ) 
 303 wxXmlDocument::wxXmlDocument(const wxXmlDocument
& doc
) 
 309 wxXmlDocument
& wxXmlDocument::operator=(const wxXmlDocument
& doc
) 
 316 void wxXmlDocument::DoCopy(const wxXmlDocument
& doc
) 
 318     m_version 
= doc
.m_version
; 
 320     m_encoding 
= doc
.m_encoding
; 
 322     m_fileEncoding 
= doc
.m_fileEncoding
; 
 323     m_root 
= new wxXmlNode(*doc
.m_root
); 
 326 bool wxXmlDocument::Load(const wxString
& filename
, const wxString
& encoding
) 
 328     wxFileInputStream 
stream(filename
); 
 329     return Load(stream
, encoding
); 
 332 bool wxXmlDocument::Save(const wxString
& filename
) const 
 334     wxFileOutputStream 
stream(filename
); 
 340 //----------------------------------------------------------------------------- 
 341 //  wxXmlDocument loading routines 
 342 //----------------------------------------------------------------------------- 
 346        - process all elements, including CDATA 
 349 // converts Expat-produced string in UTF-8 into wxString. 
 350 inline static wxString 
CharToString(wxMBConv 
*conv
, 
 351                                     const char *s
, size_t len 
= wxSTRING_MAXLEN
) 
 355     return wxString(s
, wxConvUTF8
, len
); 
 359         size_t nLen 
= (len 
!= wxSTRING_MAXLEN
) ? len 
: 
 360                           wxConvUTF8
.MB2WC((wchar_t*) NULL
, s
, 0); 
 362         wchar_t *buf 
= new wchar_t[nLen
+1]; 
 363         wxConvUTF8
.MB2WC(buf
, s
, nLen
); 
 365         wxString 
str(buf
, *conv
, len
); 
 370         return wxString(s
, len 
!= wxSTRING_MAXLEN 
? len 
: strlen(s
)); 
 374 struct wxXmlParsingContext
 
 379     wxXmlNode 
*lastAsText
; 
 385 static void StartElementHnd(void *userData
, const char *name
, const char **atts
) 
 387     wxXmlParsingContext 
*ctx 
= (wxXmlParsingContext
*)userData
; 
 388     wxXmlNode 
*node 
= new wxXmlNode(wxXML_ELEMENT_NODE
, CharToString(ctx
->conv
, name
)); 
 389     const char **a 
= atts
; 
 392         node
->AddProperty(CharToString(ctx
->conv
, a
[0]), CharToString(ctx
->conv
, a
[1])); 
 395     if (ctx
->root 
== NULL
) 
 398         ctx
->node
->AddChild(node
); 
 400     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
; 
 415 static void TextHnd(void *userData
, const char *s
, int len
) 
 417     wxXmlParsingContext 
*ctx 
= (wxXmlParsingContext
*)userData
; 
 418     char *buf 
= new char[len 
+ 1]; 
 421     memcpy(buf
, s
, (size_t)len
); 
 425         ctx
->lastAsText
->SetContent(ctx
->lastAsText
->GetContent() + 
 426                                     CharToString(ctx
->conv
, buf
)); 
 430         bool whiteOnly 
= true; 
 431         for (char *c 
= buf
; *c 
!= '\0'; c
++) 
 432             if (*c 
!= ' ' && *c 
!= '\t' && *c 
!= '\n' && *c 
!= '\r') 
 439             ctx
->lastAsText 
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"), 
 440                                             CharToString(ctx
->conv
, buf
)); 
 441             ctx
->node
->AddChild(ctx
->lastAsText
); 
 450 static void CommentHnd(void *userData
, const char *data
) 
 452     wxXmlParsingContext 
*ctx 
= (wxXmlParsingContext
*)userData
; 
 456         // VS: ctx->node == NULL happens if there is a comment before 
 457         //     the root element (e.g. wxDesigner's output). We ignore such 
 458         //     comments, no big deal... 
 459         ctx
->node
->AddChild(new wxXmlNode(wxXML_COMMENT_NODE
, 
 460                             wxT("comment"), CharToString(ctx
->conv
, data
))); 
 462     ctx
->lastAsText 
= NULL
; 
 467 static void DefaultHnd(void *userData
, const char *s
, int len
) 
 470     if (len 
> 6 && memcmp(s
, "<?xml ", 6) == 0) 
 472         wxXmlParsingContext 
*ctx 
= (wxXmlParsingContext
*)userData
; 
 474         wxString buf 
= CharToString(ctx
->conv
, s
, (size_t)len
); 
 476         pos 
= buf
.Find(wxT("encoding=")); 
 477         if (pos 
!= wxNOT_FOUND
) 
 478             ctx
->encoding 
= buf
.Mid(pos 
+ 10).BeforeFirst(buf
[(size_t)pos
+9]); 
 479         pos 
= buf
.Find(wxT("version=")); 
 480         if (pos 
!= wxNOT_FOUND
) 
 481             ctx
->version 
= buf
.Mid(pos 
+ 9).BeforeFirst(buf
[(size_t)pos
+8]); 
 487 static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData
), 
 488                               const XML_Char 
*name
, XML_Encoding 
*info
) 
 490     // We must build conversion table for expat. The easiest way to do so 
 491     // is to let wxCSConv convert as string containing all characters to 
 492     // wide character representation: 
 493     wxString 
str(name
, wxConvLibc
); 
 501     for (i 
= 0; i 
< 255; i
++) 
 503         mbBuf
[0] = (char)(i
+1); 
 504         if (conv
.MB2WC(wcBuf
, mbBuf
, 2) == (size_t)-1) 
 506             // invalid/undefined byte in the encoding: 
 509         info
->map
[i
+1] = (int)wcBuf
[0]; 
 513     info
->convert 
= NULL
; 
 514     info
->release 
= NULL
; 
 520 bool wxXmlDocument::Load(wxInputStream
& stream
, const wxString
& encoding
) 
 525     m_encoding 
= encoding
; 
 528     const size_t BUFSIZE 
= 1024; 
 530     wxXmlParsingContext ctx
; 
 532     XML_Parser parser 
= XML_ParserCreate(NULL
); 
 534     ctx
.root 
= ctx
.node 
= NULL
; 
 535     ctx
.encoding 
= wxT("UTF-8"); // default in absence of encoding="" 
 538     if ( encoding 
!= wxT("UTF-8") && encoding 
!= wxT("utf-8") ) 
 539         ctx
.conv 
= new wxCSConv(encoding
); 
 542     XML_SetUserData(parser
, (void*)&ctx
); 
 543     XML_SetElementHandler(parser
, StartElementHnd
, EndElementHnd
); 
 544     XML_SetCharacterDataHandler(parser
, TextHnd
); 
 545     XML_SetCommentHandler(parser
, CommentHnd
); 
 546     XML_SetDefaultHandler(parser
, DefaultHnd
); 
 547     XML_SetUnknownEncodingHandler(parser
, UnknownEncodingHnd
, NULL
); 
 552         size_t len 
= stream
.Read(buf
, BUFSIZE
).LastRead(); 
 553         done 
= (len 
< BUFSIZE
); 
 554         if (!XML_Parse(parser
, buf
, len
, done
)) 
 556             wxString 
error(XML_ErrorString(XML_GetErrorCode(parser
)), 
 558             wxLogError(_("XML parsing error: '%s' at line %d"), 
 560                        XML_GetCurrentLineNumber(parser
)); 
 568         if (!ctx
.version
.empty()) 
 569             SetVersion(ctx
.version
); 
 570         if (!ctx
.encoding
.empty()) 
 571             SetFileEncoding(ctx
.encoding
); 
 579     XML_ParserFree(parser
); 
 591 //----------------------------------------------------------------------------- 
 592 //  wxXmlDocument saving routines 
 593 //----------------------------------------------------------------------------- 
 595 // write string to output: 
 596 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
, 
 598     wxMBConv 
* WXUNUSED(convMem
), 
 604     if (str
.empty()) return; 
 606     const wxWX2MBbuf 
buf(str
.mb_str(*(convFile 
? convFile 
: &wxConvUTF8
))); 
 607     stream
.Write((const char*)buf
, strlen((const char*)buf
)); 
 609     if ( convFile 
== NULL 
) 
 610         stream
.Write(str
.mb_str(), str
.Len()); 
 613         wxString 
str2(str
.wc_str(*convMem
), *convFile
); 
 614         stream
.Write(str2
.mb_str(), str2
.Len()); 
 619 // Same as above, but create entities first. 
 620 // Translates '<' to "<", '>' to ">" and '&' to "&" 
 621 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
, 
 622                             wxMBConv 
*convMem
, wxMBConv 
*convFile
, 
 623                             bool escapeQuotes 
= false) 
 631     for (i 
= 0; i 
< len
; i
++) 
 634         if (c 
== wxT('<') || c 
== wxT('>') || 
 635             (c 
== wxT('&') && str
.Mid(i
+1, 4) != wxT("amp;")) || 
 636             (escapeQuotes 
&& c 
== wxT('"'))) 
 638             OutputString(stream
, str
.Mid(last
, i 
- last
), convMem
, convFile
); 
 642                     OutputString(stream
, wxT("<"), NULL
, NULL
); 
 645                     OutputString(stream
, wxT(">"), NULL
, NULL
); 
 648                     OutputString(stream
, wxT("&"), NULL
, NULL
); 
 651                     OutputString(stream
, wxT("""), NULL
, NULL
); 
 658     OutputString(stream
, str
.Mid(last
, i 
- last
), convMem
, convFile
); 
 661 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
) 
 663     wxString str 
= wxT("\n"); 
 664     for (int i 
= 0; i 
< indent
; i
++) 
 665         str 
<< wxT(' ') << wxT(' '); 
 666     OutputString(stream
, str
, NULL
, NULL
); 
 669 static void OutputNode(wxOutputStream
& stream
, wxXmlNode 
*node
, int indent
, 
 670                        wxMBConv 
*convMem
, wxMBConv 
*convFile
) 
 675     switch (node
->GetType()) 
 677         case wxXML_TEXT_NODE
: 
 678             OutputStringEnt(stream
, node
->GetContent(), convMem
, convFile
); 
 681         case wxXML_ELEMENT_NODE
: 
 682             OutputString(stream
, wxT("<"), NULL
, NULL
); 
 683             OutputString(stream
, node
->GetName(), NULL
, NULL
); 
 685             prop 
= node
->GetProperties(); 
 688                 OutputString(stream
, wxT(" ") + prop
->GetName() +  wxT("=\""), 
 690                 OutputStringEnt(stream
, prop
->GetValue(), NULL
, NULL
, 
 691                                 true/*escapeQuotes*/); 
 692                 OutputString(stream
, wxT("\""), NULL
, NULL
); 
 693                 prop 
= prop
->GetNext(); 
 696             if (node
->GetChildren()) 
 698                 OutputString(stream
, wxT(">"), NULL
, NULL
); 
 700                 n 
= node
->GetChildren(); 
 703                     if (n 
&& n
->GetType() != wxXML_TEXT_NODE
) 
 704                         OutputIndentation(stream
, indent 
+ 1); 
 705                     OutputNode(stream
, n
, indent 
+ 1, convMem
, convFile
); 
 709                 if (prev 
&& prev
->GetType() != wxXML_TEXT_NODE
) 
 710                     OutputIndentation(stream
, indent
); 
 711                 OutputString(stream
, wxT("</"), NULL
, NULL
); 
 712                 OutputString(stream
, node
->GetName(), NULL
, NULL
); 
 713                 OutputString(stream
, wxT(">"), NULL
, NULL
); 
 716                 OutputString(stream
, wxT("/>"), NULL
, NULL
); 
 719         case wxXML_COMMENT_NODE
: 
 720             OutputString(stream
, wxT("<!--"), NULL
, NULL
); 
 721             OutputString(stream
, node
->GetContent(), convMem
, convFile
); 
 722             OutputString(stream
, wxT("-->"), NULL
, NULL
); 
 726             wxFAIL_MSG(wxT("unsupported node type")); 
 730 bool wxXmlDocument::Save(wxOutputStream
& stream
) const 
 737     wxMBConv 
*convMem 
= NULL
; 
 740     wxMBConv 
*convFile 
= new wxCSConv(GetFileEncoding()); 
 742     wxMBConv 
*convFile 
= NULL
; 
 743     if ( GetFileEncoding() != GetEncoding() ) 
 745         convFile 
= new wxCSConv(GetFileEncoding()); 
 746         convMem 
= new wxCSConv(GetEncoding()); 
 750     s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"), 
 751              GetVersion().c_str(), GetFileEncoding().c_str()); 
 752     OutputString(stream
, s
, NULL
, NULL
); 
 754     OutputNode(stream
, GetRoot(), 0, convMem
, convFile
); 
 755     OutputString(stream
, wxT("\n"), NULL
, NULL
);