xtixml.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 542 行 · 第 1/2 页
CPP
542 行
/////////////////////////////////////////////////////////////////////////////
// Name: src/common/xtistrm.cpp
// Purpose: streaming runtime metadata information
// Author: Stefan Csomor
// Modified by:
// Created: 27/07/03
// RCS-ID: $Id: xtixml.cpp,v 1.11 2004/09/24 14:32:35 ABX Exp $
// Copyright: (c) 2003 Stefan Csomor
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "xtistrm.h"
#endif
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/hash.h"
#include "wx/object.h"
#endif
#include "wx/xml/xml.h"
#include "wx/tokenzr.h"
#include "wx/txtstrm.h"
#include "wx/event.h"
#if wxUSE_EXTENDED_RTTI
#include "wx/xtistrm.h"
#include "wx/xtixml.h"
#include "wx/beforestd.h"
#include <map>
#include <vector>
#include <string>
#include "wx/afterstd.h"
using namespace std ;
//
// XML Streaming
//
// convenience functions
void wxXmlAddContentToNode( wxXmlNode* node , const wxString& data )
{
node->AddChild(new wxXmlNode(wxXML_TEXT_NODE, wxT("value"), data ) );
}
wxString wxXmlGetContentFromNode( wxXmlNode *node )
{
if ( node->GetChildren() )
return node->GetChildren()->GetContent() ;
else
return wxEmptyString ;
}
struct wxXmlWriter::wxXmlWriterInternal
{
wxXmlNode *m_root ;
wxXmlNode *m_current ;
vector< wxXmlNode * > m_objectStack ;
void Push( wxXmlNode *newCurrent )
{
m_objectStack.push_back( m_current ) ;
m_current = newCurrent ;
}
void Pop()
{
m_current = m_objectStack.back() ;
m_objectStack.pop_back() ;
}
} ;
wxXmlWriter::wxXmlWriter( wxXmlNode * rootnode )
{
m_data = new wxXmlWriterInternal() ;
m_data->m_root = rootnode ;
m_data->m_current = rootnode ;
}
wxXmlWriter::~wxXmlWriter()
{
delete m_data ;
}
void wxXmlWriter::DoBeginWriteTopLevelEntry( const wxString &name )
{
wxXmlNode *pnode;
pnode = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("entry"));
pnode->AddProperty(wxString(wxT("name")), name);
m_data->m_current->AddChild(pnode) ;
m_data->Push( pnode ) ;
}
void wxXmlWriter::DoEndWriteTopLevelEntry( const wxString &WXUNUSED(name) )
{
m_data->Pop() ;
}
void wxXmlWriter::DoBeginWriteObject(const wxObject *WXUNUSED(object), const wxClassInfo *classInfo, int objectID , wxxVariantArray &metadata )
{
wxXmlNode *pnode;
pnode = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("object"));
pnode->AddProperty(wxT("class"), wxString(classInfo->GetClassName()));
pnode->AddProperty(wxT("id"), wxString::Format( wxT("%d") , objectID ) );
for ( size_t i = 0 ; i < metadata.GetCount() ; ++i )
{
pnode->AddProperty( metadata[i].GetName() , metadata[i].GetAsString() ) ;
}
m_data->m_current->AddChild(pnode) ;
m_data->Push( pnode ) ;
}
// end of writing the root object
void wxXmlWriter::DoEndWriteObject(const wxObject *WXUNUSED(object), const wxClassInfo *WXUNUSED(classInfo), int WXUNUSED(objectID) )
{
m_data->Pop() ;
}
// writes a property in the stream format
void wxXmlWriter::DoWriteSimpleType( wxxVariant &value )
{
wxXmlAddContentToNode( m_data->m_current ,value.GetAsString() ) ;
}
void wxXmlWriter::DoBeginWriteElement()
{
wxXmlNode *pnode;
pnode = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("element") );
m_data->m_current->AddChild(pnode) ;
m_data->Push( pnode ) ;
}
void wxXmlWriter::DoEndWriteElement()
{
m_data->Pop() ;
}
void wxXmlWriter::DoBeginWriteProperty(const wxPropertyInfo *pi )
{
wxXmlNode *pnode;
pnode = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("prop") );
pnode->AddProperty(wxT("name"), pi->GetName() );
m_data->m_current->AddChild(pnode) ;
m_data->Push( pnode ) ;
}
void wxXmlWriter::DoEndWriteProperty(const wxPropertyInfo *WXUNUSED(propInfo) )
{
m_data->Pop() ;
}
// insert an object reference to an already written object
void wxXmlWriter::DoWriteRepeatedObject( int objectID )
{
wxXmlNode *pnode;
pnode = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("object"));
pnode->AddProperty(wxString(wxT("href")), wxString::Format( wxT("%d") , objectID ) );
m_data->m_current->AddChild(pnode) ;
}
// insert a null reference
void wxXmlWriter::DoWriteNullObject()
{
wxXmlNode *pnode;
pnode = new wxXmlNode(wxXML_ELEMENT_NODE, wxT("object"));
m_data->m_current->AddChild(pnode) ;
}
// writes a delegate in the stream format
void wxXmlWriter::DoWriteDelegate( const wxObject *WXUNUSED(object), const wxClassInfo* WXUNUSED(classInfo) , const wxPropertyInfo *WXUNUSED(pi) ,
const wxObject *eventSink, int sinkObjectID , const wxClassInfo* WXUNUSED(eventSinkClassInfo) , const wxHandlerInfo* handlerInfo )
{
if ( eventSink != NULL && handlerInfo != NULL )
{
wxXmlAddContentToNode( m_data->m_current ,wxString::Format(wxT("%d.%s"), sinkObjectID , handlerInfo->GetName().c_str()) ) ;
}
}
// ----------------------------------------------------------------------------
// reading objects in
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// reading xml in
// ----------------------------------------------------------------------------
/*
Reading components has not to be extended for components
as properties are always sought by typeinfo over all levels
and create params are always toplevel class only
*/
int wxXmlReader::ReadComponent(wxXmlNode *node, wxDepersister *callbacks)
{
wxASSERT_MSG( callbacks , wxT("Does not support reading without a Depersistor") ) ;
wxString className;
wxClassInfo *classInfo;
wxxVariant *createParams ;
int *createParamOids ;
const wxClassInfo** createClassInfos ;
wxXmlNode *children;
int objectID;
wxString ObjectIdString ;
children = node->GetChildren();
if (!children)
{
// check for a null object or href
if (node->GetPropVal(wxT("href") , &ObjectIdString ) )
{
objectID = atoi( ObjectIdString.ToAscii() ) ;
if ( HasObjectClassInfo( objectID ) )
{
return objectID ;
}
else
{
wxLogError( _("Forward hrefs are not supported") ) ;
return wxInvalidObjectID ;
}
}
if ( !node->GetPropVal(wxT("id") , &ObjectIdString ) )
{
return wxNullObjectID;
}
}
if (!node->GetPropVal(wxT("class"), &className))
{
// No class name. Eek. FIXME: error handling
return wxInvalidObjectID;
}
classInfo = wxClassInfo::FindClass(className);
if ( classInfo == NULL )
{
wxLogError( wxString::Format(_("unknown class %s"),className ) ) ;
return wxInvalidObjectID ;
}
if ( children != NULL && children->GetType() == wxXML_TEXT_NODE )
{
wxLogError(_("objects cannot have XML Text Nodes") ) ;
return wxInvalidObjectID;
}
if (!node->GetPropVal(wxT("id"), &ObjectIdString))
{
wxLogError(_("Objects must have an id attribute") ) ;
// No object id. Eek. FIXME: error handling
return wxInvalidObjectID;
}
objectID = atoi( ObjectIdString.ToAscii() ) ;
// is this object already has been streamed in, return it here
if ( HasObjectClassInfo( objectID ) )
{
wxLogError ( wxString::Format(_("Doubly used id : %d"), objectID ) ) ;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?