]>
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"
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 const wxString
& encoding
)
305 : wxObject(), m_root(NULL
)
307 if (!Load(filename
, io_type
, encoding
))
315 wxXmlDocument::wxXmlDocument(wxInputStream
& stream
, wxXmlIOType io_type
,
316 const wxString
& encoding
)
317 : wxObject(), m_root(NULL
)
319 if (!Load(stream
, io_type
, encoding
))
327 wxXmlDocument::wxXmlDocument(const wxXmlDocument
& doc
)
334 wxXmlDocument
& wxXmlDocument::operator=(const wxXmlDocument
& doc
)
343 void wxXmlDocument::DoCopy(const wxXmlDocument
& doc
)
345 m_version
= doc
.m_version
;
347 m_encoding
= doc
.m_encoding
;
349 m_fileEncoding
= doc
.m_fileEncoding
;
350 m_root
= new wxXmlNode(*doc
.m_root
);
355 bool wxXmlDocument::Load(const wxString
& filename
, wxXmlIOType io_type
,
356 const wxString
& encoding
)
358 wxFileInputStream
stream(filename
);
359 return Load(stream
, io_type
, encoding
);
364 bool wxXmlDocument::Load(wxInputStream
& stream
, wxXmlIOType io_type
,
365 const wxString
& encoding
)
370 m_encoding
= encoding
;
373 wxNode
*n
= sm_handlers
->GetFirst();
376 wxXmlIOHandler
*h
= (wxXmlIOHandler
*) n
->GetData();
378 if ((io_type
== wxXML_IO_AUTO
|| io_type
== h
->GetType()) &&
381 return h
->Load(stream
, *this, encoding
);
385 wxLogError(_("Cannot find XML I/O handler capable of loading this format."));
391 bool wxXmlDocument::Save(const wxString
& filename
, wxXmlIOType io_type
) const
393 wxFileOutputStream
stream(filename
);
394 return Save(stream
, io_type
);
399 bool wxXmlDocument::Save(wxOutputStream
& stream
, wxXmlIOType io_type
) const
401 wxNode
*n
= sm_handlers
->GetFirst();
404 wxXmlIOHandler
*h
= (wxXmlIOHandler
*) n
->GetData();
405 if (io_type
== h
->GetType() && h
->CanSave())
407 return h
->Save(stream
, *this);
411 wxLogError(_("Cannot find XML I/O handler capable of saving in this format."));
420 void wxXmlDocument::AddHandler(wxXmlIOHandler
*handler
)
422 if (sm_handlers
== NULL
)
424 sm_handlers
= new wxList
;
425 sm_handlers
->DeleteContents(TRUE
);
427 sm_handlers
->Append(handler
);
431 void wxXmlDocument::CleanUpHandlers()
433 wxDELETE(sm_handlers
);
437 void wxXmlDocument::InitStandardHandlers()
439 AddHandler(new wxXmlIOHandlerBin
);
441 AddHandler(new wxXmlIOHandlerBinZ
);
443 AddHandler(new wxXmlIOHandlerExpat
);
444 AddHandler(new wxXmlIOHandlerWriter
);
448 #include "wx/module.h"
450 class wxXmlModule
: public wxModule
452 DECLARE_DYNAMIC_CLASS(wxXmlModule
)
455 bool OnInit() { wxXmlDocument::InitStandardHandlers(); return TRUE
; };
456 void OnExit() { wxXmlDocument::CleanUpHandlers(); };
459 IMPLEMENT_DYNAMIC_CLASS(wxXmlModule
, wxModule
)
464 // When wxXml is loaded dynamically after the application is already running
465 // then the built-in module system won't pick this one up. Add it manually.
466 void wxXmlInitXmlModule()
468 wxModule
* module = new wxXmlModule
;
470 wxModule::RegisterModule(module);