19 #include <itksys/SystemTools.hxx> 35 MITK_ERROR <<
"Not serializing nullptr or empty PropertyList";
40 static unsigned long count = 1;
41 unsigned long n = count++;
42 std::ostringstream name;
43 for (
int i = 0; i < 6; ++i)
45 name << char(
'a' + (n % 26));
49 filename.append(name.str());
54 fullname = itksys::SystemTools::ConvertToOutputPath(fullname.c_str());
57 std::string::size_type length = fullname.length();
59 if (length >= 2 && fullname[0] ==
'"' && fullname[length - 1] ==
'"')
60 fullname = fullname.substr(1, length - 2);
62 TiXmlDocument document;
63 auto decl =
new TiXmlDeclaration(
"1.0",
"",
"");
64 document.LinkEndChild(decl);
66 auto version =
new TiXmlElement(
"Version");
67 version->SetAttribute(
"Writer", __FILE__);
68 version->SetAttribute(
"Revision",
"$Revision: 17055 $");
69 version->SetAttribute(
"FileVersion", 1);
70 document.LinkEndChild(version);
74 for (
auto iter = propmap->begin(); iter != propmap->end(); ++iter)
76 std::string key = iter->first;
81 document.LinkEndChild(element);
91 if (!document.SaveFile(fullname))
93 MITK_ERROR <<
"Could not write PropertyList to " << fullname <<
"\nTinyXML reports '" << document.ErrorDesc()
103 auto keyelement =
new TiXmlElement(
"property");
104 keyelement->SetAttribute(
"key", key);
105 keyelement->SetAttribute(
"type", property->GetNameOfClass());
108 std::string serializername(property->GetNameOfClass());
109 serializername +=
"Serializer";
111 std::list<itk::LightObject::Pointer> allSerializers =
112 itk::ObjectFactoryBase::CreateAllInstance(serializername.c_str());
113 if (allSerializers.size() < 1)
115 MITK_ERROR <<
"No serializer found for " <<
property->GetNameOfClass() <<
". Skipping object";
117 if (allSerializers.size() > 1)
119 MITK_WARN <<
"Multiple serializers found for " <<
property->GetNameOfClass() <<
"Using arbitrarily the first one.";
122 for (
auto iter = allSerializers.begin(); iter != allSerializers.end(); ++iter)
124 if (
auto *serializer = dynamic_cast<BasePropertySerializer *>(iter->GetPointer()))
126 serializer->SetProperty(property);
129 TiXmlElement *valueelement = serializer->Serialize();
132 keyelement->LinkEndChild(valueelement);
135 catch (std::exception &e)
137 MITK_ERROR <<
"Serializer " << serializer->GetNameOfClass() <<
" failed: " << e.what();
144 MITK_ERROR <<
"Found a serializer called '" << (*iter)->GetNameOfClass()
145 <<
"' that does not implement the BasePropertySerializer interface.";
150 if (keyelement->NoChildren())
PropertyList::Pointer m_FailedProperties
virtual std::string Serialize()
Serializes given PropertyList object.
Key-value list holding instances of BaseProperty.
PropertyList * GetFailedProperties()
Abstract base class for properties.
std::map< std::string, BaseProperty::Pointer > PropertyMap
std::string m_WorkingDirectory
TiXmlElement * SerializeOneProperty(const std::string &key, const BaseProperty *property)
PropertyList::Pointer m_PropertyList
~PropertyListSerializer() override