]>
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
);
351 return Load(stream
, encoding
);
354 bool wxXmlDocument::Save(const wxString
& filename
) const
356 wxFileOutputStream
stream(filename
);
362 //-----------------------------------------------------------------------------
363 // wxXmlDocument loading routines
364 //-----------------------------------------------------------------------------
368 - process all elements, including CDATA
371 // converts Expat-produced string in UTF-8 into wxString using the specified
372 // conv or keep in UTF-8 if conv is NULL
373 static wxString
CharToString(wxMBConv
*conv
,
374 const char *s
, size_t len
= wxSTRING_MAXLEN
)
379 return wxString(s
, wxConvUTF8
, len
);
380 #else // !wxUSE_UNICODE
383 // there can be no embedded NULs in this string so we don't need the
384 // output length, it will be NUL-terminated
385 const wxWCharBuffer
wbuf(
386 wxConvUTF8
.cMB2WC(s
, len
== wxSTRING_MAXLEN
? wxNO_LEN
: len
, NULL
));
388 return wxString(wbuf
, conv
);
390 else // already in UTF-8, no conversion needed
392 return wxString(s
, len
!= wxSTRING_MAXLEN
? len
: strlen(s
));
394 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
397 struct wxXmlParsingContext
402 wxXmlNode
*lastAsText
;
408 static void StartElementHnd(void *userData
, const char *name
, const char **atts
)
410 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
411 wxXmlNode
*node
= new wxXmlNode(wxXML_ELEMENT_NODE
, CharToString(ctx
->conv
, name
));
412 const char **a
= atts
;
415 node
->AddProperty(CharToString(ctx
->conv
, a
[0]), CharToString(ctx
->conv
, a
[1]));
418 if (ctx
->root
== NULL
)
421 ctx
->node
->AddChild(node
);
423 ctx
->lastAsText
= NULL
;
428 static void EndElementHnd(void *userData
, const char* WXUNUSED(name
))
430 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
432 ctx
->node
= ctx
->node
->GetParent();
433 ctx
->lastAsText
= NULL
;
438 static void TextHnd(void *userData
, const char *s
, int len
)
440 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
441 char *buf
= new char[len
+ 1];
444 memcpy(buf
, s
, (size_t)len
);
448 ctx
->lastAsText
->SetContent(ctx
->lastAsText
->GetContent() +
449 CharToString(ctx
->conv
, buf
));
453 bool whiteOnly
= true;
454 for (char *c
= buf
; *c
!= '\0'; c
++)
455 if (*c
!= ' ' && *c
!= '\t' && *c
!= '\n' && *c
!= '\r')
462 ctx
->lastAsText
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"),
463 CharToString(ctx
->conv
, buf
));
464 ctx
->node
->AddChild(ctx
->lastAsText
);
473 static void CommentHnd(void *userData
, const char *data
)
475 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
479 // VS: ctx->node == NULL happens if there is a comment before
480 // the root element (e.g. wxDesigner's output). We ignore such
481 // comments, no big deal...
482 ctx
->node
->AddChild(new wxXmlNode(wxXML_COMMENT_NODE
,
483 wxT("comment"), CharToString(ctx
->conv
, data
)));
485 ctx
->lastAsText
= NULL
;
490 static void DefaultHnd(void *userData
, const char *s
, int len
)
493 if (len
> 6 && memcmp(s
, "<?xml ", 6) == 0)
495 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
497 wxString buf
= CharToString(ctx
->conv
, s
, (size_t)len
);
499 pos
= buf
.Find(wxT("encoding="));
500 if (pos
!= wxNOT_FOUND
)
501 ctx
->encoding
= buf
.Mid(pos
+ 10).BeforeFirst(buf
[(size_t)pos
+9]);
502 pos
= buf
.Find(wxT("version="));
503 if (pos
!= wxNOT_FOUND
)
504 ctx
->version
= buf
.Mid(pos
+ 9).BeforeFirst(buf
[(size_t)pos
+8]);
510 static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData
),
511 const XML_Char
*name
, XML_Encoding
*info
)
513 // We must build conversion table for expat. The easiest way to do so
514 // is to let wxCSConv convert as string containing all characters to
515 // wide character representation:
516 wxString
str(name
, wxConvLibc
);
524 for (i
= 0; i
< 255; i
++)
526 mbBuf
[0] = (char)(i
+1);
527 if (conv
.MB2WC(wcBuf
, mbBuf
, 2) == (size_t)-1)
529 // invalid/undefined byte in the encoding:
532 info
->map
[i
+1] = (int)wcBuf
[0];
536 info
->convert
= NULL
;
537 info
->release
= NULL
;
543 bool wxXmlDocument::Load(wxInputStream
& stream
, const wxString
& encoding
)
548 m_encoding
= encoding
;
551 const size_t BUFSIZE
= 1024;
553 wxXmlParsingContext ctx
;
555 XML_Parser parser
= XML_ParserCreate(NULL
);
557 ctx
.root
= ctx
.node
= NULL
;
558 ctx
.encoding
= wxT("UTF-8"); // default in absence of encoding=""
561 if ( encoding
!= wxT("UTF-8") && encoding
!= wxT("utf-8") )
562 ctx
.conv
= new wxCSConv(encoding
);
565 XML_SetUserData(parser
, (void*)&ctx
);
566 XML_SetElementHandler(parser
, StartElementHnd
, EndElementHnd
);
567 XML_SetCharacterDataHandler(parser
, TextHnd
);
568 XML_SetCommentHandler(parser
, CommentHnd
);
569 XML_SetDefaultHandler(parser
, DefaultHnd
);
570 XML_SetUnknownEncodingHandler(parser
, UnknownEncodingHnd
, NULL
);
575 size_t len
= stream
.Read(buf
, BUFSIZE
).LastRead();
576 done
= (len
< BUFSIZE
);
577 if (!XML_Parse(parser
, buf
, len
, done
))
579 wxString
error(XML_ErrorString(XML_GetErrorCode(parser
)),
581 wxLogError(_("XML parsing error: '%s' at line %d"),
583 XML_GetCurrentLineNumber(parser
));
591 if (!ctx
.version
.empty())
592 SetVersion(ctx
.version
);
593 if (!ctx
.encoding
.empty())
594 SetFileEncoding(ctx
.encoding
);
602 XML_ParserFree(parser
);
614 //-----------------------------------------------------------------------------
615 // wxXmlDocument saving routines
616 //-----------------------------------------------------------------------------
618 // write string to output:
619 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
620 wxMBConv
*convMem
= NULL
,
621 wxMBConv
*convFile
= NULL
)
627 wxUnusedVar(convMem
);
629 const wxWX2MBbuf
buf(str
.mb_str(*(convFile
? convFile
: &wxConvUTF8
)));
630 stream
.Write((const char*)buf
, strlen((const char*)buf
));
631 #else // !wxUSE_UNICODE
632 if ( convFile
&& convMem
)
634 wxString
str2(str
.wc_str(*convMem
), *convFile
);
635 stream
.Write(str2
.mb_str(), str2
.Len());
637 else // no conversions to do
639 stream
.Write(str
.mb_str(), str
.Len());
641 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
644 // flags for OutputStringEnt()
647 XML_ESCAPE_QUOTES
= 1
650 // Same as above, but create entities first.
651 // Translates '<' to "<", '>' to ">" and '&' to "&"
652 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
653 wxMBConv
*convMem
= NULL
,
654 wxMBConv
*convFile
= NULL
,
663 for (i
= 0; i
< len
; i
++)
666 if (c
== wxT('<') || c
== wxT('>') ||
667 (c
== wxT('&') && str
.Mid(i
+1, 4) != wxT("amp;")) ||
668 ((flags
& XML_ESCAPE_QUOTES
) && c
== wxT('"')))
670 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
674 OutputString(stream
, wxT("<"));
677 OutputString(stream
, wxT(">"));
680 OutputString(stream
, wxT("&"));
683 OutputString(stream
, wxT("""));
691 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
694 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
)
696 wxString str
= wxT("\n");
697 for (int i
= 0; i
< indent
; i
++)
698 str
<< wxT(' ') << wxT(' ');
699 OutputString(stream
, str
);
702 static void OutputNode(wxOutputStream
& stream
, wxXmlNode
*node
, int indent
,
703 wxMBConv
*convMem
, wxMBConv
*convFile
)
708 switch (node
->GetType())
710 case wxXML_TEXT_NODE
:
711 OutputStringEnt(stream
, node
->GetContent(), convMem
, convFile
);
714 case wxXML_ELEMENT_NODE
:
715 OutputString(stream
, wxT("<"));
716 OutputString(stream
, node
->GetName());
718 prop
= node
->GetProperties();
721 OutputString(stream
, wxT(" ") + prop
->GetName() + wxT("=\""));
722 OutputStringEnt(stream
, prop
->GetValue(), convMem
, convFile
,
724 OutputString(stream
, wxT("\""));
725 prop
= prop
->GetNext();
728 if (node
->GetChildren())
730 OutputString(stream
, wxT(">"));
732 n
= node
->GetChildren();
735 if (n
&& n
->GetType() != wxXML_TEXT_NODE
)
736 OutputIndentation(stream
, indent
+ 1);
737 OutputNode(stream
, n
, indent
+ 1, convMem
, convFile
);
741 if (prev
&& prev
->GetType() != wxXML_TEXT_NODE
)
742 OutputIndentation(stream
, indent
);
743 OutputString(stream
, wxT("</"));
744 OutputString(stream
, node
->GetName());
745 OutputString(stream
, wxT(">"));
748 OutputString(stream
, wxT("/>"));
751 case wxXML_COMMENT_NODE
:
752 OutputString(stream
, wxT("<!--"));
753 OutputString(stream
, node
->GetContent(), convMem
, convFile
);
754 OutputString(stream
, wxT("-->"));
758 wxFAIL_MSG(wxT("unsupported node type"));
762 bool wxXmlDocument::Save(wxOutputStream
& stream
) const
769 wxMBConv
*convMem
= NULL
;
772 wxMBConv
*convFile
= new wxCSConv(GetFileEncoding());
774 wxMBConv
*convFile
= NULL
;
775 if ( GetFileEncoding() != GetEncoding() )
777 convFile
= new wxCSConv(GetFileEncoding());
778 convMem
= new wxCSConv(GetEncoding());
782 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
783 GetVersion().c_str(), GetFileEncoding().c_str());
784 OutputString(stream
, s
);
786 OutputNode(stream
, GetRoot(), 0, convMem
, convFile
);
787 OutputString(stream
, wxT("\n"));