Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPropertyListDeserializerV1.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
15 #include "mitkSerializerMacros.h"
16 #include <tinyxml.h>
17 
18 MITK_REGISTER_SERIALIZER(PropertyListDeserializerV1)
19 
21 {
22 }
23 
25 {
26 }
27 
29 {
30  bool error(false);
31 
33 
34  TiXmlDocument document(m_Filename);
35  if (!document.LoadFile())
36  {
37  MITK_ERROR << "Could not open/read/parse " << m_Filename << "\nTinyXML reports: " << document.ErrorDesc()
38  << std::endl;
39  return false;
40  }
41 
42  for (TiXmlElement *propertyElement = document.FirstChildElement("property"); propertyElement != nullptr;
43  propertyElement = propertyElement->NextSiblingElement("property"))
44  {
45  const char *keya = propertyElement->Attribute("key");
46  std::string key(keya ? keya : "");
47 
48  const char *typea = propertyElement->Attribute("type");
49  std::string type(typea ? typea : "");
50 
51  // hand propertyElement to specific reader
52  std::stringstream propertyDeserializerClassName;
53  propertyDeserializerClassName << type << "Serializer";
54 
55  std::list<itk::LightObject::Pointer> readers =
56  itk::ObjectFactoryBase::CreateAllInstance(propertyDeserializerClassName.str().c_str());
57  if (readers.size() < 1)
58  {
59  MITK_ERROR << "No property reader found for " << type;
60  error = true;
61  }
62  if (readers.size() > 1)
63  {
64  MITK_WARN << "Multiple property readers found for " << type << ". Using arbitrary first one.";
65  }
66 
67  for (auto iter = readers.begin(); iter != readers.end(); ++iter)
68  {
69  if (auto *reader = dynamic_cast<BasePropertySerializer *>(iter->GetPointer()))
70  {
71  BaseProperty::Pointer property = reader->Deserialize(propertyElement->FirstChildElement());
72  if (property.IsNotNull())
73  {
74  m_PropertyList->ReplaceProperty(key, property);
75  }
76  else
77  {
78  MITK_ERROR << "There were errors while loading property '" << key << "' of type " << type
79  << ". Your data may be corrupted";
80  error = true;
81  }
82  break;
83  }
84  }
85  }
86  return !error;
87 }
#define MITK_REGISTER_SERIALIZER(classname)
static Pointer New()
#define MITK_ERROR
Definition: mitkLogMacros.h:20
Deserializes a mitk::PropertyList.
DataCollection - Class to facilitate loading/accessing structured data.
#define MITK_WARN
Definition: mitkLogMacros.h:19
bool Deserialize() override
Reads a propertylist from file. Get result via GetOutput()