diff --git a/Modules/SceneSerializationBase/mitkPropertyListSerializer.cpp b/Modules/SceneSerializationBase/mitkPropertyListSerializer.cpp index 278dfddf81..0f99d98081 100644 --- a/Modules/SceneSerializationBase/mitkPropertyListSerializer.cpp +++ b/Modules/SceneSerializationBase/mitkPropertyListSerializer.cpp @@ -1,167 +1,167 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date$ Version: $Revision: 1.12 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #include "mitkPropertyListSerializer.h" #include "mitkBasePropertySerializer.h" #include #include "mitkStandardFileLocations.h" #include mitk::PropertyListSerializer::PropertyListSerializer() : m_FilenameHint("unnamed") , m_WorkingDirectory("") { } mitk::PropertyListSerializer::~PropertyListSerializer() { } std::string mitk::PropertyListSerializer::Serialize() { m_FailedProperties = PropertyList::New(); - if ( m_PropertyList.IsNull() && m_PropertyList->IsEmpty() ) + 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(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 ) { 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 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(property) ); } if (allSerializers.size() > 1) { MITK_WARN << "Multiple serializers found for " << property->GetNameOfClass() << "Using arbitrarily the first one."; } for ( std::list::iterator iter = allSerializers.begin(); iter != allSerializers.end(); ++iter ) { if (BasePropertySerializer* serializer = dynamic_cast( 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(property) ); } } catch (std::exception& e) { MITK_ERROR << "Serializer " << serializer->GetNameOfClass() << " failed: " << e.what(); m_FailedProperties->ReplaceProperty( key, const_cast(property) ); // \TODO: log only if all potential serializers fail? } break; } } return keyelement; } mitk::PropertyList* mitk::PropertyListSerializer::GetFailedProperties() { if (m_FailedProperties.IsNotNull() && !m_FailedProperties->IsEmpty()) { return m_FailedProperties; } else { return NULL; } }