]>
git.saurik.com Git - wxWidgets.git/blob - src/xrc/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 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "xml.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
23 #include "wx/wfstream.h"
24 #include "wx/datstrm.h"
25 #include "wx/zstream.h"
28 #include "wx/strconv.h"
30 #include "wx/xrc/xml.h"
32 #include "xmlparse.h" // from Expat
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 wxXmlNode::wxXmlNode(wxXmlNode
*parent
,wxXmlNodeType type
,
39 const wxString
& name
, const wxString
& content
,
40 wxXmlProperty
*props
, wxXmlNode
*next
)
41 : m_type(type
), m_name(name
), m_content(content
),
42 m_properties(props
), m_parent(parent
),
43 m_children(NULL
), m_next(next
)
47 if (m_parent
->m_children
)
49 m_next
= m_parent
->m_children
;
50 m_parent
->m_children
= this;
53 m_parent
->m_children
= this;
57 wxXmlNode::wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
58 const wxString
& content
)
59 : m_type(type
), m_name(name
), m_content(content
),
60 m_properties(NULL
), m_parent(NULL
),
61 m_children(NULL
), m_next(NULL
)
64 wxXmlNode::wxXmlNode(const wxXmlNode
& node
)
71 wxXmlNode::~wxXmlNode()
74 for (c
= m_children
; c
; c
= c2
)
80 wxXmlProperty
*p
, *p2
;
81 for (p
= m_properties
; p
; p
= p2
)
88 wxXmlNode
& wxXmlNode::operator=(const wxXmlNode
& node
)
90 wxDELETE(m_properties
);
96 void wxXmlNode::DoCopy(const wxXmlNode
& node
)
100 m_content
= node
.m_content
;
103 wxXmlNode
*n
= node
.m_children
;
106 AddChild(new wxXmlNode(*n
));
111 wxXmlProperty
*p
= node
.m_properties
;
114 AddProperty(p
->GetName(), p
->GetValue());
119 bool wxXmlNode::HasProp(const wxString
& propName
) const
121 wxXmlProperty
*prop
= GetProperties();
125 if (prop
->GetName() == propName
) return TRUE
;
126 prop
= prop
->GetNext();
132 bool wxXmlNode::GetPropVal(const wxString
& propName
, wxString
*value
) const
134 wxXmlProperty
*prop
= GetProperties();
138 if (prop
->GetName() == propName
)
140 *value
= prop
->GetValue();
143 prop
= prop
->GetNext();
149 wxString
wxXmlNode::GetPropVal(const wxString
& propName
, const wxString
& defaultVal
) const
152 if (GetPropVal(propName
, &tmp
))
158 void wxXmlNode::AddChild(wxXmlNode
*child
)
160 if (m_children
== NULL
)
164 wxXmlNode
*ch
= m_children
;
165 while (ch
->m_next
) ch
= ch
->m_next
;
168 child
->m_next
= NULL
;
169 child
->m_parent
= this;
172 void wxXmlNode::InsertChild(wxXmlNode
*child
, wxXmlNode
*before_node
)
174 wxASSERT_MSG(before_node
->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
176 if (m_children
== before_node
)
180 wxXmlNode
*ch
= m_children
;
181 while (ch
->m_next
!= before_node
) ch
= ch
->m_next
;
185 child
->m_parent
= this;
186 child
->m_next
= before_node
;
189 bool wxXmlNode::RemoveChild(wxXmlNode
*child
)
191 if (m_children
== NULL
)
193 else if (m_children
== child
)
195 m_children
= child
->m_next
;
196 child
->m_parent
= NULL
;
197 child
->m_next
= NULL
;
202 wxXmlNode
*ch
= m_children
;
205 if (ch
->m_next
== child
)
207 ch
->m_next
= child
->m_next
;
208 child
->m_parent
= NULL
;
209 child
->m_next
= NULL
;
218 void wxXmlNode::AddProperty(const wxString
& name
, const wxString
& value
)
220 AddProperty(new wxXmlProperty(name
, value
, NULL
));
223 void wxXmlNode::AddProperty(wxXmlProperty
*prop
)
225 if (m_properties
== NULL
)
229 wxXmlProperty
*p
= m_properties
;
230 while (p
->GetNext()) p
= p
->GetNext();
235 bool wxXmlNode::DeleteProperty(const wxString
& name
)
239 if (m_properties
== NULL
)
242 else if (m_properties
->GetName() == name
)
245 m_properties
= prop
->GetNext();
253 wxXmlProperty
*p
= m_properties
;
256 if (p
->GetNext()->GetName() == name
)
259 p
->SetNext(prop
->GetNext());
272 //-----------------------------------------------------------------------------
274 //-----------------------------------------------------------------------------
276 wxXmlDocument::wxXmlDocument()
277 : m_version(wxT("1.0")), m_fileEncoding(wxT("utf-8")), m_root(NULL
)
280 m_encoding
= wxT("UTF-8");
284 wxXmlDocument::wxXmlDocument(const wxString
& filename
, const wxString
& encoding
)
285 : wxObject(), m_root(NULL
)
287 if ( !Load(filename
, encoding
) )
293 wxXmlDocument::wxXmlDocument(wxInputStream
& stream
, const wxString
& encoding
)
294 : wxObject(), m_root(NULL
)
296 if ( !Load(stream
, encoding
) )
302 wxXmlDocument::wxXmlDocument(const wxXmlDocument
& doc
)
307 wxXmlDocument
& wxXmlDocument::operator=(const wxXmlDocument
& doc
)
314 void wxXmlDocument::DoCopy(const wxXmlDocument
& doc
)
316 m_version
= doc
.m_version
;
318 m_encoding
= doc
.m_encoding
;
320 m_fileEncoding
= doc
.m_fileEncoding
;
321 m_root
= new wxXmlNode(*doc
.m_root
);
324 bool wxXmlDocument::Load(const wxString
& filename
, const wxString
& encoding
)
326 wxFileInputStream
stream(filename
);
327 return Load(stream
, encoding
);
330 bool wxXmlDocument::Save(const wxString
& filename
) const
332 wxFileOutputStream
stream(filename
);
338 //-----------------------------------------------------------------------------
339 // wxXmlDocument loading routines
340 //-----------------------------------------------------------------------------
344 - process all elements, including CDATA
347 // converts Expat-produced string in UTF-8 into wxString.
348 inline static wxString
CharToString(wxMBConv
*conv
,
349 const char *s
, size_t len
= wxSTRING_MAXLEN
)
353 return wxString(s
, wxConvUTF8
, len
);
357 size_t nLen
= (len
!= wxSTRING_MAXLEN
) ? len
:
358 nLen
= wxConvUTF8
.MB2WC((wchar_t*) NULL
, s
, 0);
360 wchar_t *buf
= new wchar_t[nLen
+1];
361 wxConvUTF8
.MB2WC(buf
, s
, nLen
);
363 wxString
str(buf
, *conv
, len
);
368 return wxString(s
, len
);
372 struct wxXmlParsingContext
377 wxXmlNode
*lastAsText
;
382 static void StartElementHnd(void *userData
, const char *name
, const char **atts
)
384 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
385 wxXmlNode
*node
= new wxXmlNode(wxXML_ELEMENT_NODE
, CharToString(ctx
->conv
, name
));
386 const char **a
= atts
;
389 node
->AddProperty(CharToString(ctx
->conv
, a
[0]), CharToString(ctx
->conv
, a
[1]));
392 if (ctx
->root
== NULL
)
395 ctx
->node
->AddChild(node
);
397 ctx
->lastAsText
= NULL
;
400 static void EndElementHnd(void *userData
, const char* WXUNUSED(name
))
402 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
404 ctx
->node
= ctx
->node
->GetParent();
405 ctx
->lastAsText
= NULL
;
408 static void TextHnd(void *userData
, const char *s
, int len
)
410 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
411 char *buf
= new char[len
+ 1];
414 memcpy(buf
, s
, (size_t)len
);
418 ctx
->lastAsText
->SetContent(ctx
->lastAsText
->GetContent() +
419 CharToString(ctx
->conv
, buf
));
423 bool whiteOnly
= TRUE
;
424 for (char *c
= buf
; *c
!= '\0'; c
++)
425 if (*c
!= ' ' && *c
!= '\t' && *c
!= '\n' && *c
!= '\r')
432 ctx
->lastAsText
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"),
433 CharToString(ctx
->conv
, buf
));
434 ctx
->node
->AddChild(ctx
->lastAsText
);
441 static void CommentHnd(void *userData
, const char *data
)
443 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
447 // VS: ctx->node == NULL happens if there is a comment before
448 // the root element (e.g. wxDesigner's output). We ignore such
449 // comments, no big deal...
450 ctx
->node
->AddChild(new wxXmlNode(wxXML_COMMENT_NODE
,
451 wxT("comment"), CharToString(ctx
->conv
, data
)));
453 ctx
->lastAsText
= NULL
;
456 static void DefaultHnd(void *userData
, const char *s
, int len
)
459 if (len
> 6 && memcmp(s
, "<?xml ", 6) == 0)
461 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
463 wxString buf
= CharToString(ctx
->conv
, s
, (size_t)len
);
465 pos
= buf
.Find(wxT("encoding="));
466 if (pos
!= wxNOT_FOUND
)
467 ctx
->encoding
= buf
.Mid(pos
+ 10).BeforeFirst(buf
[(size_t)pos
+9]);
468 pos
= buf
.Find(wxT("version="));
469 if (pos
!= wxNOT_FOUND
)
470 ctx
->version
= buf
.Mid(pos
+ 9).BeforeFirst(buf
[(size_t)pos
+8]);
474 static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData
),
475 const XML_Char
*name
, XML_Encoding
*info
)
477 // We must build conversion table for expat. The easiest way to do so
478 // is to let wxCSConv convert as string containing all characters to
479 // wide character representation:
480 wxCSConv
conv(wxString(name
, wxConvLibc
));
487 for (i
= 0; i
< 255; i
++)
489 mbBuf
[0] = (char)(i
+1);
490 if (conv
.MB2WC(wcBuf
, mbBuf
, 2) == (size_t)-1)
492 // invalid/undefined byte in the encoding:
495 info
->map
[i
+1] = (int)wcBuf
[0];
499 info
->convert
= NULL
;
500 info
->release
= NULL
;
505 bool wxXmlDocument::Load(wxInputStream
& stream
, const wxString
& encoding
)
510 m_encoding
= encoding
;
513 const size_t BUFSIZE
= 1024;
515 wxXmlParsingContext ctx
;
517 XML_Parser parser
= XML_ParserCreate(NULL
);
519 ctx
.root
= ctx
.node
= NULL
;
520 ctx
.encoding
= wxT("UTF-8"); // default in absence of encoding=""
523 if ( encoding
!= wxT("UTF-8") && encoding
!= wxT("utf-8") )
524 ctx
.conv
= new wxCSConv(encoding
);
527 XML_SetUserData(parser
, (void*)&ctx
);
528 XML_SetElementHandler(parser
, StartElementHnd
, EndElementHnd
);
529 XML_SetCharacterDataHandler(parser
, TextHnd
);
530 XML_SetCommentHandler(parser
, CommentHnd
);
531 XML_SetDefaultHandler(parser
, DefaultHnd
);
532 XML_SetUnknownEncodingHandler(parser
, UnknownEncodingHnd
, NULL
);
536 size_t len
= stream
.Read(buf
, BUFSIZE
).LastRead();
537 done
= (len
< BUFSIZE
);
538 if (!XML_Parse(parser
, buf
, len
, done
))
540 wxLogError(_("XML parsing error: '%s' at line %d"),
541 XML_ErrorString(XML_GetErrorCode(parser
)),
542 XML_GetCurrentLineNumber(parser
));
547 SetVersion(ctx
.version
);
548 SetFileEncoding(ctx
.encoding
);
551 XML_ParserFree(parser
);
563 //-----------------------------------------------------------------------------
564 // wxXmlDocument saving routines
565 //-----------------------------------------------------------------------------
567 // write string to output:
568 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
569 wxMBConv
*convMem
, wxMBConv
*convFile
)
571 if (str
.IsEmpty()) return;
573 const wxWX2MBbuf
buf(str
.mb_str(convFile
? *convFile
: wxConvUTF8
));
574 stream
.Write((const char*)buf
, strlen((const char*)buf
));
576 if ( convFile
== NULL
)
577 stream
.Write(str
.mb_str(), str
.Len());
580 wxString
str2(str
.wc_str(*convMem
), *convFile
);
581 stream
.Write(str2
.mb_str(), str2
.Len());
586 // Same as above, but create entities first.
587 // Translates '<' to "<", '>' to ">" and '&' to "&"
588 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
589 wxMBConv
*convMem
, wxMBConv
*convFile
)
597 for (i
= 0; i
< len
; i
++)
600 if (c
== wxT('<') || c
== wxT('>') ||
601 (c
== wxT('&') && str
.Mid(i
+1, 4) != wxT("amp;")))
603 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
607 OutputString(stream
, wxT("<"), NULL
, NULL
);
610 OutputString(stream
, wxT(">"), NULL
, NULL
);
613 OutputString(stream
, wxT("&"), NULL
, NULL
);
620 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
623 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
)
625 wxString str
= wxT("\n");
626 for (int i
= 0; i
< indent
; i
++)
627 str
<< wxT(' ') << wxT(' ');
628 OutputString(stream
, str
, NULL
, NULL
);
631 static void OutputNode(wxOutputStream
& stream
, wxXmlNode
*node
, int indent
,
632 wxMBConv
*convMem
, wxMBConv
*convFile
)
637 switch (node
->GetType())
639 case wxXML_TEXT_NODE
:
640 OutputStringEnt(stream
, node
->GetContent(), convMem
, convFile
);
643 case wxXML_ELEMENT_NODE
:
644 OutputString(stream
, wxT("<"), NULL
, NULL
);
645 OutputString(stream
, node
->GetName(), NULL
, NULL
);
647 prop
= node
->GetProperties();
650 OutputString(stream
, wxT(" ") + prop
->GetName() +
651 wxT("=\"") + prop
->GetValue() + wxT("\""),
653 // FIXME - what if prop contains '"'?
654 prop
= prop
->GetNext();
657 if (node
->GetChildren())
659 OutputString(stream
, wxT(">"), NULL
, NULL
);
661 n
= node
->GetChildren();
664 if (n
&& n
->GetType() != wxXML_TEXT_NODE
)
665 OutputIndentation(stream
, indent
+ 1);
666 OutputNode(stream
, n
, indent
+ 1, convMem
, convFile
);
670 if (prev
&& prev
->GetType() != wxXML_TEXT_NODE
)
671 OutputIndentation(stream
, indent
);
672 OutputString(stream
, wxT("</"), NULL
, NULL
);
673 OutputString(stream
, node
->GetName(), NULL
, NULL
);
674 OutputString(stream
, wxT(">"), NULL
, NULL
);
677 OutputString(stream
, wxT("/>"), NULL
, NULL
);
680 case wxXML_COMMENT_NODE
:
681 OutputString(stream
, wxT("<!--"), NULL
, NULL
);
682 OutputString(stream
, node
->GetContent(), convMem
, convFile
);
683 OutputString(stream
, wxT("-->"), NULL
, NULL
);
687 wxFAIL_MSG(wxT("unsupported node type"));
691 bool wxXmlDocument::Save(wxOutputStream
& stream
) const
698 wxMBConv
*convMem
= NULL
, *convFile
= NULL
;
700 convFile
= new wxCSConv(GetFileEncoding());
702 if ( GetFileEncoding() != GetEncoding() )
704 convFile
= new wxCSConv(GetFileEncoding());
705 convMem
= new wxCSConv(GetEncoding());
709 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
710 GetVersion().c_str(), GetFileEncoding().c_str());
711 OutputString(stream
, s
, NULL
, NULL
);
713 OutputNode(stream
, GetRoot(), 0, convMem
, convFile
);
714 OutputString(stream
, wxT("\n"), NULL
, NULL
);