23 #include <itksys/SystemTools.hxx>
37 if (m_PropertyList.IsNull() || m_PropertyList->IsEmpty())
39 MITK_ERROR <<
"Not serializing NULL or empty PropertyList";
44 static unsigned long count = 1;
45 unsigned long n = count++;
46 std::ostringstream name;
47 for (
int i = 0; i < 6; ++i)
49 name << char(
'a' + (n % 26));
53 filename.append(name.str());
55 std::string fullname(m_WorkingDirectory);
58 fullname = itksys::SystemTools::ConvertToOutputPath(fullname.c_str());
61 std::string::size_type length = fullname.length();
63 if (length >= 2 && fullname[0] ==
'"' && fullname[length - 1] ==
'"')
64 fullname = fullname.substr(1, length - 2);
66 TiXmlDocument document;
67 auto decl =
new TiXmlDeclaration(
"1.0",
"",
"");
68 document.LinkEndChild(decl);
70 auto version =
new TiXmlElement(
"Version");
71 version->SetAttribute(
"Writer", __FILE__);
72 version->SetAttribute(
"Revision",
"$Revision: 17055 $");
73 version->SetAttribute(
"FileVersion", 1);
74 document.LinkEndChild(version);
78 for (
auto iter = propmap->begin(); iter != propmap->end(); ++iter)
80 std::string key = iter->first;
82 TiXmlElement *element = SerializeOneProperty(key, property);
85 document.LinkEndChild(element);
90 m_FailedProperties->ReplaceProperty(key, const_cast<BaseProperty *>(property));
95 if (!document.SaveFile(fullname))
97 MITK_ERROR <<
"Could not write PropertyList to " << fullname <<
"\nTinyXML reports '" << document.ErrorDesc()
107 auto keyelement =
new TiXmlElement(
"property");
108 keyelement->SetAttribute(
"key", key);
109 keyelement->SetAttribute(
"type", property->GetNameOfClass());
112 std::string serializername(property->GetNameOfClass());
113 serializername +=
"Serializer";
115 std::list<itk::LightObject::Pointer> allSerializers =
116 itk::ObjectFactoryBase::CreateAllInstance(serializername.c_str());
117 if (allSerializers.size() < 1)
119 MITK_ERROR <<
"No serializer found for " <<
property->GetNameOfClass() <<
". Skipping object";
121 if (allSerializers.size() > 1)
123 MITK_WARN <<
"Multiple serializers found for " <<
property->GetNameOfClass() <<
"Using arbitrarily the first one.";
126 for (
auto iter = allSerializers.begin(); iter != allSerializers.end(); ++iter)
130 serializer->SetProperty(property);
133 TiXmlElement *valueelement = serializer->Serialize();
136 keyelement->LinkEndChild(valueelement);
139 catch (std::exception &e)
141 MITK_ERROR <<
"Serializer " << serializer->GetNameOfClass() <<
" failed: " << e.what();
148 MITK_ERROR <<
"Found a serializer called '" << (*iter)->GetNameOfClass()
149 <<
"' that does not implement the BasePropertySerializer interface.";
154 if (keyelement->NoChildren())
156 m_FailedProperties->ReplaceProperty(key, const_cast<BaseProperty *>(property));
167 if (m_FailedProperties.IsNotNull() && !m_FailedProperties->IsEmpty())
169 return m_FailedProperties;
virtual std::string Serialize()
Serializes given PropertyList object.
virtual ~PropertyListSerializer()
Key-value list holding instances of BaseProperty.
PropertyList * GetFailedProperties()
Abstract base class for properties.
std::map< std::string, BaseProperty::Pointer > PropertyMap
static const std::string filename
TiXmlElement * SerializeOneProperty(const std::string &key, const BaseProperty *property)
Base class for objects that serialize BaseProperty types.