]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/xml/xml.cpp
02d5019dde354406638a9017c8d11d6f656a4f47
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/xml/xml.h"
31 #include "wx/xml/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::operator=(const wxXmlNode
& node
)
84 void wxXmlNode::DoCopy(const wxXmlNode
& node
)
88 m_content
= node
.m_content
;
91 wxXmlNode
*n
= node
.m_children
;
94 AddChild(new wxXmlNode(*n
));
99 wxXmlProperty
*p
= node
.m_properties
;
102 AddProperty(p
->GetName(), p
->GetValue());
108 bool wxXmlNode::HasProp(const wxString
& propName
) const
110 wxXmlProperty
*prop
= GetProperties();
114 if (prop
->GetName() == propName
) return TRUE
;
115 prop
= prop
->GetNext();
123 bool wxXmlNode::GetPropVal(const wxString
& propName
, wxString
*value
) const
125 wxXmlProperty
*prop
= GetProperties();
129 if (prop
->GetName() == propName
)
131 *value
= prop
->GetValue();
134 prop
= prop
->GetNext();
142 wxString
wxXmlNode::GetPropVal(const wxString
& propName
, const wxString
& defaultVal
) const
145 if (GetPropVal(propName
, &tmp
))
153 void wxXmlNode::AddChild(wxXmlNode
*child
)
155 if (m_children
== NULL
)
159 wxXmlNode
*ch
= m_children
;
160 while (ch
->m_next
) ch
= ch
->m_next
;
163 child
->m_next
= NULL
;
164 child
->m_parent
= this;
169 void wxXmlNode::InsertChild(wxXmlNode
*child
, wxXmlNode
*before_node
)
171 wxASSERT_MSG(before_node
->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
173 if (m_children
== before_node
)
177 wxXmlNode
*ch
= m_children
;
178 while (ch
->m_next
!= before_node
) ch
= ch
->m_next
;
182 child
->m_parent
= this;
183 child
->m_next
= before_node
;
188 bool wxXmlNode::RemoveChild(wxXmlNode
*child
)
190 if (m_children
== NULL
)
192 else if (m_children
== child
)
194 m_children
= child
->m_next
;
195 child
->m_parent
= NULL
;
196 child
->m_next
= NULL
;
201 wxXmlNode
*ch
= m_children
;
204 if (ch
->m_next
== child
)
206 ch
->m_next
= child
->m_next
;
207 child
->m_parent
= NULL
;
208 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();
238 bool wxXmlNode::DeleteProperty(const wxString
& name
)
240 if (m_properties
== NULL
)
243 else if (m_properties
->GetName() == name
)
245 wxXmlProperty
*prop
= m_properties
;
246 m_properties
= prop
->GetNext();
254 wxXmlProperty
*p
= m_properties
;
257 if (p
->GetNext()->GetName() == name
)
259 wxXmlProperty
*prop
= p
->GetNext();
260 p
->SetNext(prop
->GetNext());
278 wxList
*wxXmlDocument::sm_handlers
= NULL
;
282 wxXmlDocument::wxXmlDocument(const wxString
& filename
, wxXmlIOType io_type
)
283 : wxObject(), m_root(NULL
)
285 if (!Load(filename
, io_type
))
294 wxXmlDocument::wxXmlDocument(wxInputStream
& stream
, wxXmlIOType io_type
)
295 : wxObject(), m_root(NULL
)
297 if (!Load(stream
, io_type
))
306 wxXmlDocument::wxXmlDocument(const wxXmlDocument
& doc
)
313 wxXmlDocument
& wxXmlDocument::operator=(const wxXmlDocument
& doc
)
322 void wxXmlDocument::DoCopy(const wxXmlDocument
& doc
)
324 m_version
= doc
.m_version
;
325 m_encoding
= doc
.m_encoding
;
326 m_root
= new wxXmlNode(*doc
.m_root
);
331 bool wxXmlDocument::Load(const wxString
& filename
, wxXmlIOType io_type
)
333 wxFileInputStream
stream(filename
);
334 return Load(stream
, io_type
);
339 bool wxXmlDocument::Load(wxInputStream
& stream
, wxXmlIOType io_type
)
341 wxNode
*n
= sm_handlers
->GetFirst();
344 wxXmlIOHandler
*h
= (wxXmlIOHandler
*) n
->GetData();
346 if ((io_type
== wxXML_IO_AUTO
|| io_type
== h
->GetType()) &&
349 return h
->Load(stream
, *this);
353 wxLogError(_("Cannot find XML I/O handler capable of loading this format."));
359 bool wxXmlDocument::Save(const wxString
& filename
, wxXmlIOType io_type
) const
361 wxFileOutputStream
stream(filename
);
362 return Save(stream
, io_type
);
367 bool wxXmlDocument::Save(wxOutputStream
& stream
, wxXmlIOType io_type
) const
369 wxNode
*n
= sm_handlers
->GetFirst();
372 wxXmlIOHandler
*h
= (wxXmlIOHandler
*) n
->GetData();
373 if (io_type
== h
->GetType() && h
->CanSave())
375 return h
->Save(stream
, *this);
379 wxLogError(_("Cannot find XML I/O handler capable of saving in this format."));
388 void wxXmlDocument::AddHandler(wxXmlIOHandler
*handler
)
390 if (sm_handlers
== NULL
)
392 sm_handlers
= new wxList
;
393 sm_handlers
->DeleteContents(TRUE
);
395 sm_handlers
->Append(handler
);
399 void wxXmlDocument::CleanUpHandlers()
406 void wxXmlDocument::InitStandardHandlers()
408 AddHandler(new wxXmlIOHandlerBin
);
410 AddHandler(new wxXmlIOHandlerBinZ
);
412 AddHandler(new wxXmlIOHandlerExpat
);
413 AddHandler(new wxXmlIOHandlerWriter
);
417 #include "wx/module.h"
419 class wxXmlModule
: public wxModule
421 DECLARE_DYNAMIC_CLASS(wxXmlModule
)
424 bool OnInit() { wxXmlDocument::InitStandardHandlers(); return TRUE
; };
425 void OnExit() { wxXmlDocument::CleanUpHandlers(); };
428 IMPLEMENT_DYNAMIC_CLASS(wxXmlModule
, wxModule
)
433 // When wxXml is loaded dynamically after the application is already running
434 // then the built-in module system won't pick this one up. Add it manually.
435 void wxXmlInitXmlModule()
437 wxModule
* module = new wxXmlModule
;
439 wxModule::RegisterModule(module);