]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/xml/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/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());
280 wxXmlDocument::wxXmlDocument(const wxString
& filename
, wxXmlIOType io_type
)
281 : wxObject(), m_Root(NULL
)
283 if (!Load(filename
, io_type
))
292 wxXmlDocument::wxXmlDocument(wxInputStream
& stream
, wxXmlIOType io_type
)
293 : wxObject(), m_Root(NULL
)
295 if (!Load(stream
, io_type
))
304 wxXmlDocument::wxXmlDocument(const wxXmlDocument
& doc
)
311 wxXmlDocument
& wxXmlDocument::operator=(const wxXmlDocument
& doc
)
320 void wxXmlDocument::DoCopy(const wxXmlDocument
& doc
)
322 m_Version
= doc
.m_Version
;
323 m_Encoding
= doc
.m_Encoding
;
324 m_Root
= new wxXmlNode(*doc
.m_Root
);
329 bool wxXmlDocument::Load(const wxString
& filename
, wxXmlIOType io_type
)
331 wxFileInputStream
stream(filename
);
332 return Load(stream
, io_type
);
337 bool wxXmlDocument::Load(wxInputStream
& stream
, wxXmlIOType io_type
)
339 wxNode
*n
= sm_Handlers
->GetFirst();
342 wxXmlIOHandler
*h
= (wxXmlIOHandler
*) n
->GetData();
344 if ((io_type
== wxXML_IO_AUTO
|| io_type
== h
->GetType()) &&
347 return h
->Load(stream
, *this);
351 wxLogError(_("Cannot find XML I/O handler capable of loading this format."));
357 bool wxXmlDocument::Save(const wxString
& filename
, wxXmlIOType io_type
) const
359 wxFileOutputStream
stream(filename
);
360 return Save(stream
, io_type
);
365 bool wxXmlDocument::Save(wxOutputStream
& stream
, wxXmlIOType io_type
) const
367 wxNode
*n
= sm_Handlers
->GetFirst();
370 wxXmlIOHandler
*h
= (wxXmlIOHandler
*) n
->GetData();
371 if (io_type
== h
->GetType() && h
->CanSave())
373 return h
->Save(stream
, *this);
377 wxLogError(_("Cannot find XML I/O handler capable of saving in this format."));
386 wxList
*wxXmlDocument::sm_Handlers
= NULL
;
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
);
409 AddHandler(new wxXmlIOHandlerBinZ
);
410 AddHandler(new wxXmlIOHandlerLibxml
);
414 #include "wx/module.h"
416 class wxXmlModule
: public wxModule
418 DECLARE_DYNAMIC_CLASS(wxXmlModule
)
421 bool OnInit() { wxXmlDocument::InitStandardHandlers(); return TRUE
; };
422 void OnExit() { wxXmlDocument::CleanUpHandlers(); };
425 IMPLEMENT_DYNAMIC_CLASS(wxXmlModule
, wxModule
)