Serializes a mitk::PropertyList. More...
#include <mitkPropertyListSerializer.h>
Public Types | |
typedef PropertyListSerializer | Self |
typedef itk::Object | Superclass |
typedef itk::SmartPointer< Self > | Pointer |
typedef itk::SmartPointer < const Self > | ConstPointer |
Public Member Functions | |
virtual const char * | GetClassName () const |
virtual void | SetFilenameHint (const char *_arg) |
virtual const char * | GetFilenameHint () const |
virtual void | SetWorkingDirectory (const char *_arg) |
virtual const char * | GetWorkingDirectory () const |
virtual void | SetPropertyList (PropertyList *_arg) |
virtual std::string | Serialize () |
Serializes given PropertyList object. | |
PropertyList * | GetFailedProperties () |
Static Public Member Functions | |
static Pointer | New () |
Protected Member Functions | |
PropertyListSerializer () | |
virtual | ~PropertyListSerializer () |
TiXmlElement * | SerializeOneProperty (const std::string &key, const BaseProperty *property) |
Protected Attributes | |
std::string | m_FilenameHint |
std::string | m_WorkingDirectory |
PropertyList::Pointer | m_PropertyList |
PropertyList::Pointer | m_FailedProperties |
Serializes a mitk::PropertyList.
Definition at line 35 of file mitkPropertyListSerializer.h.
typedef itk::SmartPointer<const Self> mitk::PropertyListSerializer::ConstPointer |
Definition at line 39 of file mitkPropertyListSerializer.h.
typedef itk::SmartPointer<Self> mitk::PropertyListSerializer::Pointer |
Definition at line 39 of file mitkPropertyListSerializer.h.
Definition at line 39 of file mitkPropertyListSerializer.h.
typedef itk::Object mitk::PropertyListSerializer::Superclass |
Definition at line 39 of file mitkPropertyListSerializer.h.
mitk::PropertyListSerializer::PropertyListSerializer | ( | ) | [protected] |
Definition at line 27 of file mitkPropertyListSerializer.cpp.
: m_FilenameHint("unnamed") , m_WorkingDirectory("") { }
mitk::PropertyListSerializer::~PropertyListSerializer | ( | ) | [protected, virtual] |
Definition at line 33 of file mitkPropertyListSerializer.cpp.
{ }
virtual const char* mitk::PropertyListSerializer::GetClassName | ( | ) | const [virtual] |
mitk::PropertyList * mitk::PropertyListSerializer::GetFailedProperties | ( | ) |
Definition at line 157 of file mitkPropertyListSerializer.cpp.
{ if (m_FailedProperties.IsNotNull() && !m_FailedProperties->IsEmpty()) { return m_FailedProperties; } else { return NULL; } }
virtual const char* mitk::PropertyListSerializer::GetFilenameHint | ( | ) | const [virtual] |
virtual const char* mitk::PropertyListSerializer::GetWorkingDirectory | ( | ) | const [virtual] |
static Pointer mitk::PropertyListSerializer::New | ( | ) | [static] |
Referenced by mitkPropertySerializationTest(), and mitk::SceneIO::SavePropertyList().
std::string mitk::PropertyListSerializer::Serialize | ( | ) | [virtual] |
Serializes given PropertyList object.
Definition at line 37 of file mitkPropertyListSerializer.cpp.
References MITK_ERROR, mitk::PropertyList::New(), and TiXmlElement::SetAttribute().
{ m_FailedProperties = PropertyList::New(); if ( m_PropertyList.IsNull() && m_PropertyList->IsEmpty() ) { MITK_ERROR << "Not serializing NULL or empty PropertyList"; return ""; } // tmpname static unsigned long count = 1; unsigned long n = count++; std::ostringstream name; for (int i = 0; i < 6; ++i) { name << char('a' + (n % 26)); n /= 26; } std::string filename; filename.append(name.str()); std::string fullname(m_WorkingDirectory); fullname += "/"; fullname += filename; fullname = itksys::SystemTools::ConvertToOutputPath(fullname.c_str()); TiXmlDocument document; TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" ); // TODO what to write here? encoding? etc.... document.LinkEndChild( decl ); TiXmlElement* version = new TiXmlElement("Version"); version->SetAttribute("Writer", __FILE__ ); version->SetAttribute("Revision", "$Revision: 17055 $" ); version->SetAttribute("FileVersion", 1 ); document.LinkEndChild(version); // add XML contents const PropertyList::PropertyMap* propmap = m_PropertyList->GetMap(); for ( PropertyList::PropertyMap::const_iterator iter = propmap->begin(); iter != propmap->end(); ++iter ) { std::string key = iter->first; const BaseProperty* property = iter->second.first; TiXmlElement* element = SerializeOneProperty( key, property ); if (element) { document.LinkEndChild( element ); // TODO test serializer for error } else { m_FailedProperties->ReplaceProperty( key, const_cast<BaseProperty*>(property) ); } } // save XML file if ( !document.SaveFile( fullname ) ) { MITK_ERROR << "Could not write PropertyList to " << fullname << "\nTinyXML reports '" << document.ErrorDesc() << "'"; return ""; } return filename; }
TiXmlElement * mitk::PropertyListSerializer::SerializeOneProperty | ( | const std::string & | key, |
const BaseProperty * | property | ||
) | [protected] |
Definition at line 104 of file mitkPropertyListSerializer.cpp.
References TiXmlNode::LinkEndChild(), MITK_ERROR, MITK_WARN, and TiXmlElement::SetAttribute().
{ TiXmlElement* keyelement = new TiXmlElement("property"); keyelement->SetAttribute("key", key); keyelement->SetAttribute("type", property->GetNameOfClass()); // construct name of serializer class std::string serializername(property->GetNameOfClass()); serializername += "Serializer"; std::list<itk::LightObject::Pointer> allSerializers = itk::ObjectFactoryBase::CreateAllInstance(serializername.c_str()); if (allSerializers.size() < 1) { MITK_ERROR << "No serializer found for " << property->GetNameOfClass() << ". Skipping object"; m_FailedProperties->ReplaceProperty( key, const_cast<BaseProperty*>(property) ); } if (allSerializers.size() > 1) { MITK_WARN << "Multiple serializers found for " << property->GetNameOfClass() << "Using arbitrarily the first one."; } for ( std::list<itk::LightObject::Pointer>::iterator iter = allSerializers.begin(); iter != allSerializers.end(); ++iter ) { if (BasePropertySerializer* serializer = dynamic_cast<BasePropertySerializer*>( iter->GetPointer() ) ) { serializer->SetProperty(property); try { TiXmlElement* valueelement = serializer->Serialize(); if (valueelement) { keyelement->LinkEndChild( valueelement ); // \TODO: put 'return keyelement;' here? } else { m_FailedProperties->ReplaceProperty( key, const_cast<BaseProperty*>(property) ); } } catch (std::exception& e) { MITK_ERROR << "Serializer " << serializer->GetNameOfClass() << " failed: " << e.what(); m_FailedProperties->ReplaceProperty( key, const_cast<BaseProperty*>(property) ); // \TODO: log only if all potential serializers fail? } break; } } return keyelement; }
virtual void mitk::PropertyListSerializer::SetFilenameHint | ( | const char * | _arg ) | [virtual] |
virtual void mitk::PropertyListSerializer::SetPropertyList | ( | PropertyList * | _arg ) | [virtual] |
virtual void mitk::PropertyListSerializer::SetWorkingDirectory | ( | const char * | _arg ) | [virtual] |
Definition at line 69 of file mitkPropertyListSerializer.h.
std::string mitk::PropertyListSerializer::m_FilenameHint [protected] |
Definition at line 65 of file mitkPropertyListSerializer.h.
Definition at line 67 of file mitkPropertyListSerializer.h.
std::string mitk::PropertyListSerializer::m_WorkingDirectory [protected] |
Definition at line 66 of file mitkPropertyListSerializer.h.