16 #include <itksys/SystemTools.hxx> 23 TiXmlElement *elem)
const 31 std::string type =
"";
32 readOp = elem->QueryStringAttribute(
"type", &type) == TIXML_SUCCESS;
34 readOp = elem->QueryStringAttribute(
"name", &name) == TIXML_SUCCESS;
37 <<
" attribute not found in a property";
41 if (type ==
"BoolProperty")
44 readOp = elem->QueryIntAttribute(
"value", &val) == TIXML_SUCCESS;
50 else if (type ==
"StringProperty")
53 readOp = elem->QueryStringAttribute(
"value", &val) == TIXML_SUCCESS;
59 else if (type ==
"IntProperty")
62 readOp = elem->QueryIntAttribute(
"value", &val) == TIXML_SUCCESS;
68 else if (type ==
"DoubleProperty")
71 readOp = elem->QueryDoubleAttribute(
"value", &val) == TIXML_SUCCESS;
77 else if (type ==
"FloatProperty")
80 readOp = elem->QueryFloatAttribute(
"value", &val) == TIXML_SUCCESS;
90 <<
" attribute unknown. Only BoolProperty, StringProperty, IntProperty, DoubleProperty or " 91 "FloatProperty allowed.";
96 <<
" attribute not found in a property";
100 <<
" attribute not found in a property";
106 TiXmlElement *elem)
const 120 if ((boolProp = dynamic_cast<const BoolProperty *>(prop)))
123 elem->SetAttribute(
"value", boolProp->
GetValue() ? 1 : 0);
124 elem->SetAttribute(
"type",
"BoolProperty");
126 else if ((stringProp = dynamic_cast<const StringProperty *>(prop)))
129 elem->SetAttribute(
"value", stringProp->
GetValue());
130 elem->SetAttribute(
"type",
"StringProperty");
132 else if ((intProp = dynamic_cast<const IntProperty *>(prop)))
135 elem->SetAttribute(
"value", intProp->
GetValue());
136 elem->SetAttribute(
"type",
"IntProperty");
138 else if ((doubleProp = dynamic_cast<const DoubleProperty *>(prop)))
141 elem->SetDoubleAttribute(
"value", doubleProp->
GetValue());
142 elem->SetAttribute(
"type",
"DoubleProperty");
144 else if ((floatProp = dynamic_cast<const FloatProperty *>(prop)))
147 elem->SetDoubleAttribute(
"value", static_cast<float>(floatProp->
GetValue()));
148 elem->SetAttribute(
"type",
"FloatProperty");
152 MITK_WARN(
"PropertyListImportFromXmlFile") <<
"Base property " << name <<
" is unknown";
158 const std::string &fileName,
const std::map<std::string, mitk::PropertyList::Pointer> &_PropertyLists)
const 162 auto decl =
new TiXmlDeclaration(
"1.0",
"",
"");
163 doc.LinkEndChild(decl);
165 auto propertyListsElem =
new TiXmlElement(
"PropertyLists");
167 bool allPropsConverted =
true;
168 auto it = _PropertyLists.begin();
169 while (it != _PropertyLists.end())
171 const std::string &
id = (*it).first;
173 auto propertyListElem =
new TiXmlElement(
"PropertyList");
176 const std::map<std::string, BaseProperty::Pointer> *propMap = propList->
GetMap();
177 auto propMapIt = propMap->begin();
178 while (propMapIt != propMap->end())
180 const std::string &propName = (*propMapIt).first;
182 auto propertyElem =
new TiXmlElement(
"Property");
185 allPropsConverted =
false;
187 propertyListElem->LinkEndChild(propertyElem);
191 propertyListsElem->LinkEndChild(propertyListElem);
195 doc.LinkEndChild(propertyListsElem);
197 return (allPropsConverted && doc.SaveFile(fileName.c_str()));
200 const std::string &fileName, std::map<std::string, mitk::PropertyList::Pointer> &_PropertyLists)
const 203 TiXmlDocument doc(fileName);
206 TiXmlHandle docHandle(&doc);
207 TiXmlElement *elem = docHandle.FirstChildElement(
"PropertyLists").FirstChildElement(
"PropertyList").ToElement();
211 MITK_WARN(
"PropertyListFromXml") <<
"Cannot find a PropertyList element (inside a PropertyLists element)";
218 std::string propListId;
225 TiXmlElement *propElem = elem->FirstChildElement(
"Property");
234 propList->SetProperty(name, prop);
235 propElem = propElem->NextSiblingElement(
"Property");
240 _PropertyLists[propListId] = propList;
241 elem = elem->NextSiblingElement(
"PropertyList");
virtual const char * GetValue() const
PropertyListsXmlFileReaderAndWriter()
~PropertyListsXmlFileReaderAndWriter() override
DataCollection - Class to facilitate loading/accessing structured data.
bool PropertyToXmlElem(const std::string &name, const mitk::BaseProperty *prop, TiXmlElement *elem) const
Key-value list holding instances of BaseProperty.
static const char * GetPropertyListIdElementName()
virtual T GetValue() const
bool ReadLists(const std::string &fileName, std::map< std::string, mitk::PropertyList::Pointer > &_PropertyLists) const
Abstract base class for properties.
bool WriteLists(const std::string &fileName, const std::map< std::string, mitk::PropertyList::Pointer > &_PropertyLists) const
bool PropertyFromXmlElem(std::string &name, mitk::BaseProperty::Pointer &prop, TiXmlElement *elem) const
const PropertyMap * GetMap() const