]>
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
)
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
)
259 if (m_properties
== NULL
)
262 else if (m_properties
->GetName() == name
)
264 wxXmlProperty
*prop
= m_properties
;
265 m_properties
= prop
->GetNext();
273 wxXmlProperty
*p
= m_properties
;
276 if (p
->GetNext()->GetName() == name
)
278 wxXmlProperty
*prop
= p
->GetNext();
279 p
->SetNext(prop
->GetNext());
297 wxList
*wxXmlDocument::sm_handlers
= NULL
;
301 wxXmlDocument::wxXmlDocument(const wxString
& filename
, wxXmlIOType io_type
)
302 : wxObject(), m_root(NULL
)
304 if (!Load(filename
, io_type
))
313 wxXmlDocument::wxXmlDocument(wxInputStream
& stream
, wxXmlIOType io_type
)
314 : wxObject(), m_root(NULL
)
316 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()
425 void wxXmlDocument::InitStandardHandlers()
427 AddHandler(new wxXmlIOHandlerBin
);
429 AddHandler(new wxXmlIOHandlerBinZ
);
431 AddHandler(new wxXmlIOHandlerExpat
);
432 AddHandler(new wxXmlIOHandlerWriter
);
436 #include "wx/module.h"
438 class wxXmlModule
: public wxModule
440 DECLARE_DYNAMIC_CLASS(wxXmlModule
)
443 bool OnInit() { wxXmlDocument::InitStandardHandlers(); return TRUE
; };
444 void OnExit() { wxXmlDocument::CleanUpHandlers(); };
447 IMPLEMENT_DYNAMIC_CLASS(wxXmlModule
, wxModule
)
452 // When wxXml is loaded dynamically after the application is already running
453 // then the built-in module system won't pick this one up. Add it manually.
454 void wxXmlInitXmlModule()
456 wxModule
* module = new wxXmlModule
;
458 wxModule::RegisterModule(module);