Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkStringLookupTablePropertySerializer.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef mitkStringLookupTablePropertySerializer_h_included
18 #define mitkStringLookupTablePropertySerializer_h_included
19 
21 
22 #include "mitkProperties.h"
23 
24 namespace mitk
25 {
26  class StringLookupTablePropertySerializer : public BasePropertySerializer
27  {
28  public:
29  mitkClassMacro(StringLookupTablePropertySerializer, BasePropertySerializer);
30  itkFactorylessNewMacro(Self) itkCloneMacro(Self)
31 
32  virtual TiXmlElement *Serialize() override
33  {
34  const StringLookupTableProperty *prop = dynamic_cast<const StringLookupTableProperty *>(m_Property.GetPointer());
35  if (prop == nullptr)
36  return nullptr;
37  StringLookupTable lut = prop->GetValue();
38  // if (lut.IsNull())
39  // return NULL; // really?
40  const StringLookupTable::LookupTableType &map = lut.GetLookupTable();
41 
42  auto element = new TiXmlElement("StringLookupTable");
43  for (auto it = map.begin(); it != map.end(); ++it)
44  {
45  auto tableEntry = new TiXmlElement("LUTValue");
46  tableEntry->SetAttribute("id", it->first);
47  tableEntry->SetAttribute("value", it->second);
48  element->LinkEndChild(tableEntry);
49  }
50  return element;
51  }
52 
53  virtual BaseProperty::Pointer Deserialize(TiXmlElement *element) override
54  {
55  if (!element)
56  return nullptr;
57 
58  StringLookupTable lut;
59  for (TiXmlElement *child = element->FirstChildElement("LUTValue"); child != nullptr;
60  child = child->NextSiblingElement("LUTValue"))
61  {
62  int temp;
63  if (child->QueryIntAttribute("id", &temp) == TIXML_WRONG_TYPE)
64  return nullptr; // TODO: can we do a better error handling?
66 
67  if (child->Attribute("value") == nullptr)
68  return nullptr; // TODO: can we do a better error handling?
69  StringLookupTable::ValueType val = child->Attribute("value");
70  lut.SetTableValue(id, val);
71  }
72  return StringLookupTableProperty::New(lut).GetPointer();
73  }
74 
75  protected:
76  StringLookupTablePropertySerializer() {}
77  virtual ~StringLookupTablePropertySerializer() {}
78  };
79 } // namespace
80 // important to put this into the GLOBAL namespace (because it starts with 'namespace mitk')
81 MITK_REGISTER_SERIALIZER(StringLookupTablePropertySerializer);
82 #endif
std::map< IdentifierType, ValueType > LookupTableType
DataCollection - Class to facilitate loading/accessing structured data.
itk::SmartPointer< Self > Pointer
MITK_REGISTER_SERIALIZER(StringLookupTablePropertySerializer)
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:44