]>
git.saurik.com Git - wxWidgets.git/blob - contrib/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"
30 #include "wx/xrc/xml.h"
31 #include "wx/xrc/xmlio.h"
35 wxXmlNode::wxXmlNode(wxXmlNode
*parent
,wxXmlNodeType type
,
36 const wxString
& name
, const wxString
& content
,
37 wxXmlProperty
*props
, wxXmlNode
*next
)
38 : m_type(type
), m_name(name
), m_content(content
),
39 m_properties(props
), m_parent(parent
),
40 m_children(NULL
), m_next(next
)
44 if (m_parent
->m_children
)
46 m_next
= m_parent
->m_children
;
47 m_parent
->m_children
= this;
50 m_parent
->m_children
= this;
56 wxXmlNode::wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
57 const wxString
& content
)
58 : m_type(type
), m_name(name
), m_content(content
),
59 m_properties(NULL
), m_parent(NULL
),
60 m_children(NULL
), m_next(NULL
)
65 wxXmlNode::wxXmlNode(const wxXmlNode
& node
)
74 wxXmlNode::~wxXmlNode()
77 for (c
= m_children
; c
; c
= c2
)
83 wxXmlProperty
*p
, *p2
;
84 for (p
= m_properties
; p
; p
= p2
)
93 wxXmlNode
& wxXmlNode::operator=(const wxXmlNode
& node
)
95 wxDELETE(m_properties
);
103 void wxXmlNode::DoCopy(const wxXmlNode
& node
)
105 m_type
= node
.m_type
;
106 m_name
= node
.m_name
;
107 m_content
= node
.m_content
;
110 wxXmlNode
*n
= node
.m_children
;
113 AddChild(new wxXmlNode(*n
));
118 wxXmlProperty
*p
= node
.m_properties
;
121 AddProperty(p
->GetName(), p
->GetValue());
127 bool wxXmlNode::HasProp(const wxString
& propName
) const
129 wxXmlProperty
*prop
= GetProperties();
133 if (prop
->GetName() == propName
) return TRUE
;
134 prop
= prop
->GetNext();
142 bool wxXmlNode::GetPropVal(const wxString
& propName
, wxString
*value
) const
144 wxXmlProperty
*prop
= GetProperties();
148 if (prop
->GetName() == propName
)
150 *value
= prop
->GetValue();
153 prop
= prop
->GetNext();
161 wxString
wxXmlNode::GetPropVal(const wxString
& propName
, const wxString
& defaultVal
) const
164 if (GetPropVal(propName
, &tmp
))
172 void wxXmlNode::AddChild(wxXmlNode
*child
)
174 if (m_children
== NULL
)
178 wxXmlNode
*ch
= m_children
;
179 while (ch
->m_next
) ch
= ch
->m_next
;
182 child
->m_next
= NULL
;
183 child
->m_parent
= this;
188 void wxXmlNode::InsertChild(wxXmlNode
*child
, wxXmlNode
*before_node
)
190 wxASSERT_MSG(before_node
->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
192 if (m_children
== before_node
)
196 wxXmlNode
*ch
= m_children
;
197 while (ch
->m_next
!= before_node
) ch
= ch
->m_next
;
201 child
->m_parent
= this;
202 child
->m_next
= before_node
;
207 bool wxXmlNode::RemoveChild(wxXmlNode
*child
)
209 if (m_children
== NULL
)
211 else if (m_children
== child
)
213 m_children
= child
->m_next
;
214 child
->m_parent
= NULL
;
215 child
->m_next
= NULL
;
220 wxXmlNode
*ch
= m_children
;
223 if (ch
->m_next
== child
)
225 ch
->m_next
= child
->m_next
;
226 child
->m_parent
= NULL
;
227 child
->m_next
= NULL
;
238 void wxXmlNode::AddProperty(const wxString
& name
, const wxString
& value
)
240 AddProperty(new wxXmlProperty(name
, value
, NULL
));
243 void wxXmlNode::AddProperty(wxXmlProperty
*prop
)
245 if (m_properties
== NULL
)
249 wxXmlProperty
*p
= m_properties
;
250 while (p
->GetNext()) p
= p
->GetNext();
257 bool wxXmlNode::DeleteProperty(const wxString
& name
)
261 if (m_properties
== NULL
)
264 else if (m_properties
->GetName() == name
)
267 m_properties
= prop
->GetNext();
275 wxXmlProperty
*p
= m_properties
;
278 if (p
->GetNext()->GetName() == name
)
281 p
->SetNext(prop
->GetNext());
299 wxList
*wxXmlDocument::sm_handlers
= NULL
;
303 wxXmlDocument::wxXmlDocument(const wxString
& filename
, wxXmlIOType io_type
)
304 : wxObject(), m_root(NULL
)
306 if (!Load(filename
, io_type
))
314 wxXmlDocument::wxXmlDocument(wxInputStream
& stream
, wxXmlIOType io_type
)
315 : wxObject(), m_root(NULL
)
317 if (!Load(stream
, io_type
))
325 wxXmlDocument::wxXmlDocument(const wxXmlDocument
& doc
)
332 wxXmlDocument
& wxXmlDocument::operator=(const wxXmlDocument
& doc
)
341 void wxXmlDocument::DoCopy(const wxXmlDocument
& doc
)
343 m_version
= doc
.m_version
;
344 m_encoding
= doc
.m_encoding
;
345 m_root
= new wxXmlNode(*doc
.m_root
);
350 bool wxXmlDocument::Load(const wxString
& filename
, wxXmlIOType io_type
)
352 wxFileInputStream
stream(filename
);
353 return Load(stream
, io_type
);
358 bool wxXmlDocument::Load(wxInputStream
& stream
, wxXmlIOType io_type
)
360 wxNode
*n
= sm_handlers
->GetFirst();
363 wxXmlIOHandler
*h
= (wxXmlIOHandler
*) n
->GetData();
365 if ((io_type
== wxXML_IO_AUTO
|| io_type
== h
->GetType()) &&
368 return h
->Load(stream
, *this);
372 wxLogError(_("Cannot find XML I/O handler capable of loading this format."));
378 bool wxXmlDocument::Save(const wxString
& filename
, wxXmlIOType io_type
) const
380 wxFileOutputStream
stream(filename
);
381 return Save(stream
, io_type
);
386 bool wxXmlDocument::Save(wxOutputStream
& stream
, wxXmlIOType io_type
) const
388 wxNode
*n
= sm_handlers
->GetFirst();
391 wxXmlIOHandler
*h
= (wxXmlIOHandler
*) n
->GetData();
392 if (io_type
== h
->GetType() && h
->CanSave())
394 return h
->Save(stream
, *this);
398 wxLogError(_("Cannot find XML I/O handler capable of saving in this format."));
407 void wxXmlDocument::AddHandler(wxXmlIOHandler
*handler
)
409 if (sm_handlers
== NULL
)
411 sm_handlers
= new wxList
;
412 sm_handlers
->DeleteContents(TRUE
);
414 sm_handlers
->Append(handler
);
418 void wxXmlDocument::CleanUpHandlers()
420 wxDELETE(sm_handlers
);
424 void wxXmlDocument::InitStandardHandlers()
426 AddHandler(new wxXmlIOHandlerBin
);
428 AddHandler(new wxXmlIOHandlerBinZ
);
430 AddHandler(new wxXmlIOHandlerExpat
);
431 AddHandler(new wxXmlIOHandlerWriter
);
435 #include "wx/module.h"
437 class wxXmlModule
: public wxModule
439 DECLARE_DYNAMIC_CLASS(wxXmlModule
)
442 bool OnInit() { wxXmlDocument::InitStandardHandlers(); return TRUE
; };
443 void OnExit() { wxXmlDocument::CleanUpHandlers(); };
446 IMPLEMENT_DYNAMIC_CLASS(wxXmlModule
, wxModule
)
451 // When wxXml is loaded dynamically after the application is already running
452 // then the built-in module system won't pick this one up. Add it manually.
453 void wxXmlInitXmlModule()
455 wxModule
* module = new wxXmlModule
;
457 wxModule::RegisterModule(module);