]>
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
;
412 static void StartElementHnd(void *userData
, const char *name
, const char **atts
)
414 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
415 wxXmlNode
*node
= new wxXmlNode(wxXML_ELEMENT_NODE
, CharToString(ctx
->conv
, name
));
416 const char **a
= atts
;
419 node
->AddProperty(CharToString(ctx
->conv
, a
[0]), CharToString(ctx
->conv
, a
[1]));
422 if (ctx
->root
== NULL
)
425 ctx
->node
->AddChild(node
);
427 ctx
->lastAsText
= NULL
;
432 static void EndElementHnd(void *userData
, const char* WXUNUSED(name
))
434 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
436 ctx
->node
= ctx
->node
->GetParent();
437 ctx
->lastAsText
= NULL
;
442 static void TextHnd(void *userData
, const char *s
, int len
)
444 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
445 char *buf
= new char[len
+ 1];
448 memcpy(buf
, s
, (size_t)len
);
452 ctx
->lastAsText
->SetContent(ctx
->lastAsText
->GetContent() +
453 CharToString(ctx
->conv
, buf
));
457 bool whiteOnly
= true;
458 for (char *c
= buf
; *c
!= '\0'; c
++)
459 if (*c
!= ' ' && *c
!= '\t' && *c
!= '\n' && *c
!= '\r')
466 ctx
->lastAsText
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"),
467 CharToString(ctx
->conv
, buf
));
468 ctx
->node
->AddChild(ctx
->lastAsText
);
477 static void CommentHnd(void *userData
, const char *data
)
479 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
483 // VS: ctx->node == NULL happens if there is a comment before
484 // the root element (e.g. wxDesigner's output). We ignore such
485 // comments, no big deal...
486 ctx
->node
->AddChild(new wxXmlNode(wxXML_COMMENT_NODE
,
487 wxT("comment"), CharToString(ctx
->conv
, data
)));
489 ctx
->lastAsText
= NULL
;
494 static void DefaultHnd(void *userData
, const char *s
, int len
)
497 if (len
> 6 && memcmp(s
, "<?xml ", 6) == 0)
499 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
501 wxString buf
= CharToString(ctx
->conv
, s
, (size_t)len
);
503 pos
= buf
.Find(wxT("encoding="));
504 if (pos
!= wxNOT_FOUND
)
505 ctx
->encoding
= buf
.Mid(pos
+ 10).BeforeFirst(buf
[(size_t)pos
+9]);
506 pos
= buf
.Find(wxT("version="));
507 if (pos
!= wxNOT_FOUND
)
508 ctx
->version
= buf
.Mid(pos
+ 9).BeforeFirst(buf
[(size_t)pos
+8]);
514 static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData
),
515 const XML_Char
*name
, XML_Encoding
*info
)
517 // We must build conversion table for expat. The easiest way to do so
518 // is to let wxCSConv convert as string containing all characters to
519 // wide character representation:
520 wxString
str(name
, wxConvLibc
);
528 for (i
= 0; i
< 255; i
++)
530 mbBuf
[0] = (char)(i
+1);
531 if (conv
.MB2WC(wcBuf
, mbBuf
, 2) == (size_t)-1)
533 // invalid/undefined byte in the encoding:
536 info
->map
[i
+1] = (int)wcBuf
[0];
540 info
->convert
= NULL
;
541 info
->release
= NULL
;
547 bool wxXmlDocument::Load(wxInputStream
& stream
, const wxString
& encoding
)
552 m_encoding
= encoding
;
555 const size_t BUFSIZE
= 1024;
557 wxXmlParsingContext ctx
;
559 XML_Parser parser
= XML_ParserCreate(NULL
);
561 ctx
.root
= ctx
.node
= NULL
;
562 ctx
.encoding
= wxT("UTF-8"); // default in absence of encoding=""
565 if ( encoding
!= wxT("UTF-8") && encoding
!= wxT("utf-8") )
566 ctx
.conv
= new wxCSConv(encoding
);
569 XML_SetUserData(parser
, (void*)&ctx
);
570 XML_SetElementHandler(parser
, StartElementHnd
, EndElementHnd
);
571 XML_SetCharacterDataHandler(parser
, TextHnd
);
572 XML_SetCommentHandler(parser
, CommentHnd
);
573 XML_SetDefaultHandler(parser
, DefaultHnd
);
574 XML_SetUnknownEncodingHandler(parser
, UnknownEncodingHnd
, NULL
);
579 size_t len
= stream
.Read(buf
, BUFSIZE
).LastRead();
580 done
= (len
< BUFSIZE
);
581 if (!XML_Parse(parser
, buf
, len
, done
))
583 wxString
error(XML_ErrorString(XML_GetErrorCode(parser
)),
585 wxLogError(_("XML parsing error: '%s' at line %d"),
587 XML_GetCurrentLineNumber(parser
));
595 if (!ctx
.version
.empty())
596 SetVersion(ctx
.version
);
597 if (!ctx
.encoding
.empty())
598 SetFileEncoding(ctx
.encoding
);
606 XML_ParserFree(parser
);
618 //-----------------------------------------------------------------------------
619 // wxXmlDocument saving routines
620 //-----------------------------------------------------------------------------
622 // write string to output:
623 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
624 wxMBConv
*convMem
= NULL
,
625 wxMBConv
*convFile
= NULL
)
631 wxUnusedVar(convMem
);
633 const wxWX2MBbuf
buf(str
.mb_str(*(convFile
? convFile
: &wxConvUTF8
)));
634 stream
.Write((const char*)buf
, strlen((const char*)buf
));
635 #else // !wxUSE_UNICODE
636 if ( convFile
&& convMem
)
638 wxString
str2(str
.wc_str(*convMem
), *convFile
);
639 stream
.Write(str2
.mb_str(), str2
.Len());
641 else // no conversions to do
643 stream
.Write(str
.mb_str(), str
.Len());
645 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
648 // flags for OutputStringEnt()
651 XML_ESCAPE_QUOTES
= 1
654 // Same as above, but create entities first.
655 // Translates '<' to "<", '>' to ">" and '&' to "&"
656 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
657 wxMBConv
*convMem
= NULL
,
658 wxMBConv
*convFile
= NULL
,
667 for (i
= 0; i
< len
; i
++)
670 if (c
== wxT('<') || c
== wxT('>') ||
671 (c
== wxT('&') && str
.Mid(i
+1, 4) != wxT("amp;")) ||
672 ((flags
& XML_ESCAPE_QUOTES
) && c
== wxT('"')))
674 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
678 OutputString(stream
, wxT("<"));
681 OutputString(stream
, wxT(">"));
684 OutputString(stream
, wxT("&"));
687 OutputString(stream
, wxT("""));
695 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
698 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
)
700 wxString str
= wxT("\n");
701 for (int i
= 0; i
< indent
; i
++)
702 str
<< wxT(' ') << wxT(' ');
703 OutputString(stream
, str
);
706 static void OutputNode(wxOutputStream
& stream
, wxXmlNode
*node
, int indent
,
707 wxMBConv
*convMem
, wxMBConv
*convFile
)
712 switch (node
->GetType())
714 case wxXML_TEXT_NODE
:
715 OutputStringEnt(stream
, node
->GetContent(), convMem
, convFile
);
718 case wxXML_ELEMENT_NODE
:
719 OutputString(stream
, wxT("<"));
720 OutputString(stream
, node
->GetName());
722 prop
= node
->GetProperties();
725 OutputString(stream
, wxT(" ") + prop
->GetName() + wxT("=\""));
726 OutputStringEnt(stream
, prop
->GetValue(), convMem
, convFile
,
728 OutputString(stream
, wxT("\""));
729 prop
= prop
->GetNext();
732 if (node
->GetChildren())
734 OutputString(stream
, wxT(">"));
736 n
= node
->GetChildren();
739 if (n
&& n
->GetType() != wxXML_TEXT_NODE
)
740 OutputIndentation(stream
, indent
+ 1);
741 OutputNode(stream
, n
, indent
+ 1, convMem
, convFile
);
745 if (prev
&& prev
->GetType() != wxXML_TEXT_NODE
)
746 OutputIndentation(stream
, indent
);
747 OutputString(stream
, wxT("</"));
748 OutputString(stream
, node
->GetName());
749 OutputString(stream
, wxT(">"));
752 OutputString(stream
, wxT("/>"));
755 case wxXML_COMMENT_NODE
:
756 OutputString(stream
, wxT("<!--"));
757 OutputString(stream
, node
->GetContent(), convMem
, convFile
);
758 OutputString(stream
, wxT("-->"));
762 wxFAIL_MSG(wxT("unsupported node type"));
766 bool wxXmlDocument::Save(wxOutputStream
& stream
) const
773 wxMBConv
*convMem
= NULL
;
776 wxMBConv
*convFile
= new wxCSConv(GetFileEncoding());
778 wxMBConv
*convFile
= NULL
;
779 if ( GetFileEncoding() != GetEncoding() )
781 convFile
= new wxCSConv(GetFileEncoding());
782 convMem
= new wxCSConv(GetEncoding());
786 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
787 GetVersion().c_str(), GetFileEncoding().c_str());
788 OutputString(stream
, s
);
790 OutputNode(stream
, GetRoot(), 0, convMem
, convFile
);
791 OutputString(stream
, wxT("\n"));