20 #include <itksys/SystemTools.hxx>
27 TiXmlElement *elem)
const
35 std::string type =
"";
36 readOp = elem->QueryStringAttribute(
"type", &type) == TIXML_SUCCESS;
38 readOp = elem->QueryStringAttribute(
"name", &name) == TIXML_SUCCESS;
41 <<
" attribute not found in a property";
45 if (type ==
"BoolProperty")
48 readOp = elem->QueryIntAttribute(
"value", &val) == TIXML_SUCCESS;
54 else if (type ==
"StringProperty")
57 readOp = elem->QueryStringAttribute(
"value", &val) == TIXML_SUCCESS;
63 else if (type ==
"IntProperty")
66 readOp = elem->QueryIntAttribute(
"value", &val) == TIXML_SUCCESS;
72 else if (type ==
"DoubleProperty")
75 readOp = elem->QueryDoubleAttribute(
"value", &val) == TIXML_SUCCESS;
81 else if (type ==
"FloatProperty")
84 readOp = elem->QueryFloatAttribute(
"value", &val) == TIXML_SUCCESS;
94 <<
" attribute unknown. Only BoolProperty, StringProperty, IntProperty, DoubleProperty or "
95 "FloatProperty allowed.";
100 <<
" attribute not found in a property";
104 <<
" attribute not found in a property";
110 TiXmlElement *elem)
const
124 if ((boolProp = dynamic_cast<const BoolProperty *>(prop)))
127 elem->SetAttribute(
"value", boolProp->
GetValue() ? 1 : 0);
128 elem->SetAttribute(
"type",
"BoolProperty");
130 else if ((stringProp = dynamic_cast<const StringProperty *>(prop)))
133 elem->SetAttribute(
"value", stringProp->
GetValue());
134 elem->SetAttribute(
"type",
"StringProperty");
136 else if ((intProp = dynamic_cast<const IntProperty *>(prop)))
139 elem->SetAttribute(
"value", intProp->
GetValue());
140 elem->SetAttribute(
"type",
"IntProperty");
142 else if ((doubleProp = dynamic_cast<const DoubleProperty *>(prop)))
145 elem->SetDoubleAttribute(
"value", doubleProp->
GetValue());
146 elem->SetAttribute(
"type",
"DoubleProperty");
148 else if ((floatProp = dynamic_cast<const FloatProperty *>(prop)))
151 elem->SetDoubleAttribute(
"value", static_cast<float>(floatProp->
GetValue()));
152 elem->SetAttribute(
"type",
"FloatProperty");
156 MITK_WARN(
"PropertyListImportFromXmlFile") <<
"Base property " << name <<
" is unknown";
162 const std::string &fileName,
const std::map<std::string, mitk::PropertyList::Pointer> &_PropertyLists)
const
166 auto decl =
new TiXmlDeclaration(
"1.0",
"",
"");
167 doc.LinkEndChild(decl);
169 auto propertyListsElem =
new TiXmlElement(
"PropertyLists");
171 bool allPropsConverted =
true;
172 auto it = _PropertyLists.begin();
173 while (it != _PropertyLists.end())
175 const std::string &
id = (*it).first;
177 auto propertyListElem =
new TiXmlElement(
"PropertyList");
180 const std::map<std::string, BaseProperty::Pointer> *propMap = propList->
GetMap();
181 auto propMapIt = propMap->begin();
182 while (propMapIt != propMap->end())
184 const std::string &propName = (*propMapIt).first;
186 auto propertyElem =
new TiXmlElement(
"Property");
189 allPropsConverted =
false;
191 propertyListElem->LinkEndChild(propertyElem);
195 propertyListsElem->LinkEndChild(propertyListElem);
199 doc.LinkEndChild(propertyListsElem);
201 return (allPropsConverted && doc.SaveFile(fileName.c_str()));
204 const std::string &fileName, std::map<std::string, mitk::PropertyList::Pointer> &_PropertyLists)
const
207 TiXmlDocument doc(fileName);
210 TiXmlHandle docHandle(&doc);
211 TiXmlElement *elem = docHandle.FirstChildElement(
"PropertyLists").FirstChildElement(
"PropertyList").ToElement();
215 MITK_WARN(
"PropertyListFromXml") <<
"Cannot find a PropertyList element (inside a PropertyLists element)";
222 std::string propListId;
229 TiXmlElement *propElem = elem->FirstChildElement(
"Property");
238 propList->SetProperty(name, prop);
239 propElem = propElem->NextSiblingElement(
"Property");
244 _PropertyLists[propListId] = propList;
245 elem = elem->NextSiblingElement(
"PropertyList");
virtual ~PropertyListsXmlFileReaderAndWriter()
bool ReadLists(const std::string &fileName, std::map< std::string, mitk::PropertyList::Pointer > &_PropertyLists) const
PropertyListsXmlFileReaderAndWriter()
DataCollection - Class to facilitate loading/accessing structured data.
Key-value list holding instances of BaseProperty.
static const char * GetPropertyListIdElementName()
virtual const char * GetValue() const
Abstract base class for properties.
bool PropertyFromXmlElem(std::string &name, mitk::BaseProperty::Pointer &prop, TiXmlElement *elem) const
bool PropertyToXmlElem(const std::string &name, const mitk::BaseProperty *prop, TiXmlElement *elem) const
bool WriteLists(const std::string &fileName, const std::map< std::string, mitk::PropertyList::Pointer > &_PropertyLists) const
const PropertyMap * GetMap() const
virtual T GetValue() const