]>
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"
13 #pragma implementation "xmlio.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/wfstream.h"
25 #include "wx/datstrm.h"
26 #include "wx/zstream.h"
29 #include "wx/strconv.h"
31 #include "wx/xrc/xml.h"
33 #include "xmlparse.h" // from Expat
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(const wxString
& filename
, const wxString
& encoding
)
278 : wxObject(), m_root(NULL
)
280 if ( !Load(filename
, encoding
) )
286 wxXmlDocument::wxXmlDocument(wxInputStream
& stream
, const wxString
& encoding
)
287 : wxObject(), m_root(NULL
)
289 if ( !Load(stream
, encoding
) )
295 wxXmlDocument::wxXmlDocument(const wxXmlDocument
& doc
)
300 wxXmlDocument
& wxXmlDocument::operator=(const wxXmlDocument
& doc
)
307 void wxXmlDocument::DoCopy(const wxXmlDocument
& doc
)
309 m_version
= doc
.m_version
;
311 m_encoding
= doc
.m_encoding
;
313 m_fileEncoding
= doc
.m_fileEncoding
;
314 m_root
= new wxXmlNode(*doc
.m_root
);
317 bool wxXmlDocument::Load(const wxString
& filename
, const wxString
& encoding
)
319 wxFileInputStream
stream(filename
);
320 return Load(stream
, encoding
);
323 bool wxXmlDocument::Save(const wxString
& filename
) const
325 wxFileOutputStream
stream(filename
);
331 //-----------------------------------------------------------------------------
332 // wxXmlDocument loading routines
333 //-----------------------------------------------------------------------------
337 - process all elements, including CDATA
340 // converts Expat-produced string in UTF-8 into wxString.
341 inline static wxString
CharToString(wxMBConv
*conv
,
342 const char *s
, size_t len
= wxSTRING_MAXLEN
)
346 return wxString(s
, wxConvUTF8
, len
);
350 size_t nLen
= (len
!= wxSTRING_MAXLEN
) ? len
:
351 nLen
= wxConvUTF8
.MB2WC((wchar_t*) NULL
, s
, 0);
353 wchar_t *buf
= new wchar_t[nLen
+1];
354 wxConvUTF8
.MB2WC(buf
, s
, nLen
);
356 wxString
s(buf
, *conv
, len
);
361 return wxString(s
, len
);
365 struct wxXmlParsingContext
370 wxXmlNode
*lastAsText
;
375 static void StartElementHnd(void *userData
, const char *name
, const char **atts
)
377 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
378 wxXmlNode
*node
= new wxXmlNode(wxXML_ELEMENT_NODE
, CharToString(ctx
->conv
, name
));
379 const char **a
= atts
;
382 node
->AddProperty(CharToString(ctx
->conv
, a
[0]), CharToString(ctx
->conv
, a
[1]));
385 if (ctx
->root
== NULL
)
388 ctx
->node
->AddChild(node
);
390 ctx
->lastAsText
= NULL
;
393 static void EndElementHnd(void *userData
, const char* WXUNUSED(name
))
395 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
397 ctx
->node
= ctx
->node
->GetParent();
398 ctx
->lastAsText
= NULL
;
401 static void TextHnd(void *userData
, const char *s
, int len
)
403 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
404 char *buf
= new char[len
+ 1];
407 memcpy(buf
, s
, (size_t)len
);
411 ctx
->lastAsText
->SetContent(ctx
->lastAsText
->GetContent() +
412 CharToString(ctx
->conv
, buf
));
416 bool whiteOnly
= TRUE
;
417 for (char *c
= buf
; *c
!= '\0'; c
++)
418 if (*c
!= ' ' && *c
!= '\t' && *c
!= '\n' && *c
!= '\r')
425 ctx
->lastAsText
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"),
426 CharToString(ctx
->conv
, buf
));
427 ctx
->node
->AddChild(ctx
->lastAsText
);
434 static void CommentHnd(void *userData
, const char *data
)
436 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
440 // VS: ctx->node == NULL happens if there is a comment before
441 // the root element (e.g. wxDesigner's output). We ignore such
442 // comments, no big deal...
443 ctx
->node
->AddChild(new wxXmlNode(wxXML_COMMENT_NODE
,
444 wxT("comment"), CharToString(ctx
->conv
, data
)));
446 ctx
->lastAsText
= NULL
;
449 static void DefaultHnd(void *userData
, const char *s
, int len
)
452 if (len
> 6 && memcmp(s
, "<?xml ", 6) == 0)
454 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
456 wxString buf
= CharToString(ctx
->conv
, s
, (size_t)len
);
458 pos
= buf
.Find(wxT("encoding="));
459 if (pos
!= wxNOT_FOUND
)
460 ctx
->encoding
= buf
.Mid(pos
+ 10).BeforeFirst(buf
[(size_t)pos
+9]);
461 pos
= buf
.Find(wxT("version="));
462 if (pos
!= wxNOT_FOUND
)
463 ctx
->version
= buf
.Mid(pos
+ 9).BeforeFirst(buf
[(size_t)pos
+8]);
467 static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData
),
468 const XML_Char
*name
, XML_Encoding
*info
)
470 // We must build conversion table for expat. The easiest way to do so
471 // is to let wxCSConv convert as string containing all characters to
472 // wide character representation:
473 wxCSConv
conv(wxString(name
, wxConvLibc
));
478 for (i
= 0; i
< 255; i
++)
479 mbBuf
[i
] = (char) (i
+1);
481 conv
.MB2WC(wcBuf
, mbBuf
, 255);
485 for (i
= 0; i
< 255; i
++)
486 info
->map
[i
+1] = (int)wcBuf
[i
];
489 info
->convert
= NULL
;
490 info
->release
= NULL
;
495 bool wxXmlDocument::Load(wxInputStream
& stream
, const wxString
& encoding
)
500 m_encoding
= encoding
;
503 const size_t BUFSIZE
= 1024;
505 wxXmlParsingContext ctx
;
507 XML_Parser parser
= XML_ParserCreate(NULL
);
509 ctx
.root
= ctx
.node
= NULL
;
510 ctx
.encoding
= wxT("UTF-8"); // default in absence of encoding=""
513 if ( encoding
!= wxT("UTF-8") && encoding
!= wxT("utf-8") )
514 ctx
.conv
= new wxCSConv(encoding
);
517 XML_SetUserData(parser
, (void*)&ctx
);
518 XML_SetElementHandler(parser
, StartElementHnd
, EndElementHnd
);
519 XML_SetCharacterDataHandler(parser
, TextHnd
);
520 XML_SetCommentHandler(parser
, CommentHnd
);
521 XML_SetDefaultHandler(parser
, DefaultHnd
);
522 XML_SetUnknownEncodingHandler(parser
, UnknownEncodingHnd
, NULL
);
526 size_t len
= stream
.Read(buf
, BUFSIZE
).LastRead();
527 done
= (len
< BUFSIZE
);
528 if (!XML_Parse(parser
, buf
, len
, done
))
530 wxLogError(_("XML parsing error: '%s' at line %d"),
531 XML_ErrorString(XML_GetErrorCode(parser
)),
532 XML_GetCurrentLineNumber(parser
));
537 SetVersion(ctx
.version
);
538 SetFileEncoding(ctx
.encoding
);
541 XML_ParserFree(parser
);
553 //-----------------------------------------------------------------------------
554 // wxXmlDocument saving routines
555 //-----------------------------------------------------------------------------
557 // write string to output:
558 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
559 wxMBConv
*convMem
, wxMBConv
*convFile
)
561 if (str
.IsEmpty()) return;
563 const wxWX2MBbuf
buf(str
.mb_str(convFile
? *convFile
: wxConvUTF8
));
564 stream
.Write((const char*)buf
, strlen((const char*)buf
));
566 if ( convFile
== NULL
)
567 stream
.Write(str
.mb_str(), str
.Len());
570 wxString
str2(str
.wc_str(*convMem
), *convFile
);
571 stream
.Write(str2
.mb_str(), str2
.Len());
576 // Same as above, but create entities first.
577 // Translates '<' to "<", '>' to ">" and '&' to "&"
578 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
579 wxMBConv
*convMem
, wxMBConv
*convFile
)
587 for (i
= 0; i
< len
; i
++)
590 if (c
== wxT('<') || c
== wxT('>') ||
591 (c
== wxT('&') && str
.Mid(i
+1, 4) != wxT("amp;")))
593 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
597 OutputString(stream
, wxT("<"), NULL
, NULL
);
600 OutputString(stream
, wxT(">"), NULL
, NULL
);
603 OutputString(stream
, wxT("&"), NULL
, NULL
);
610 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
613 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
)
615 wxString str
= wxT("\n");
616 for (int i
= 0; i
< indent
; i
++)
617 str
<< wxT(' ') << wxT(' ');
618 OutputString(stream
, str
, NULL
, NULL
);
621 static void OutputNode(wxOutputStream
& stream
, wxXmlNode
*node
, int indent
,
622 wxMBConv
*convMem
, wxMBConv
*convFile
)
627 switch (node
->GetType())
629 case wxXML_TEXT_NODE
:
630 OutputStringEnt(stream
, node
->GetContent(), convMem
, convFile
);
633 case wxXML_ELEMENT_NODE
:
634 OutputString(stream
, wxT("<"), NULL
, NULL
);
635 OutputString(stream
, node
->GetName(), NULL
, NULL
);
637 prop
= node
->GetProperties();
640 OutputString(stream
, wxT(" ") + prop
->GetName() +
641 wxT("=\"") + prop
->GetValue() + wxT("\""),
643 // FIXME - what if prop contains '"'?
644 prop
= prop
->GetNext();
647 if (node
->GetChildren())
649 OutputString(stream
, wxT(">"), NULL
, NULL
);
651 n
= node
->GetChildren();
654 if (n
&& n
->GetType() != wxXML_TEXT_NODE
)
655 OutputIndentation(stream
, indent
+ 1);
656 OutputNode(stream
, n
, indent
+ 1, convMem
, convFile
);
660 if (prev
&& prev
->GetType() != wxXML_TEXT_NODE
)
661 OutputIndentation(stream
, indent
);
662 OutputString(stream
, wxT("</"), NULL
, NULL
);
663 OutputString(stream
, node
->GetName(), NULL
, NULL
);
664 OutputString(stream
, wxT(">"), NULL
, NULL
);
667 OutputString(stream
, wxT("/>"), NULL
, NULL
);
670 case wxXML_COMMENT_NODE
:
671 OutputString(stream
, wxT("<!--"), NULL
, NULL
);
672 OutputString(stream
, node
->GetContent(), convMem
, convFile
);
673 OutputString(stream
, wxT("-->"), NULL
, NULL
);
677 wxFAIL_MSG(wxT("unsupported node type"));
681 bool wxXmlDocument::Save(wxOutputStream
& stream
) const
688 wxMBConv
*convMem
= NULL
, *convFile
= NULL
;
690 convFile
= new wxCSConv(GetFileEncoding());
692 if ( GetFileEncoding() != GetEncoding() )
694 convFile
= new wxCSConv(GetFileEncoding());
695 convMem
= new wxCSConv(GetEncoding());
699 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
700 GetVersion().c_str(), GetFileEncoding().c_str());
701 OutputString(stream
, s
, NULL
, NULL
);
703 OutputNode(stream
, GetRoot(), 0, convMem
, convFile
);
704 OutputString(stream
, wxT("\n"), NULL
, NULL
);