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