Medical Imaging Interaction Toolkit  2018.4.99-b7f3afaa
Medical Imaging Interaction Toolkit
mitkPropertyPersistenceInfo.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 
13 #include <cassert>
14 #include <regex>
15 
16 #include <mitkIOMimeTypes.h>
18 #include <mitkStringProperty.h>
19 
20 namespace mitk
21 {
22  struct PropertyPersistenceInfo::Impl
23  {
24  Impl();
25  ~Impl();
26 
27  std::string Name;
28  std::string Key;
29  bool IsRegEx;
30  std::string NameTemplate;
31  std::string KeyTemplate;
34  MimeTypeNameType MimeTypeName;
35  };
36 
37  PropertyPersistenceInfo::Impl::Impl()
38  : Name(""),
39  Key(""),
40  IsRegEx(false),
41  DeSerFnc(PropertyPersistenceDeserialization::deserializeToStringProperty),
42  SerFnc(PropertyPersistenceSerialization::serializeByGetValueAsString),
44  {
45  }
46 
47  PropertyPersistenceInfo::Impl::~Impl() {}
48 }
49 
50 mitk::PropertyPersistenceInfo::PropertyPersistenceInfo(const std::string &name) : m_Impl(new Impl())
51 {
52  m_Impl->Name = name;
53  m_Impl->Key = name;
54 }
55 
56 mitk::PropertyPersistenceInfo::PropertyPersistenceInfo(const std::string &name, const std::string &mimeTypeName)
57  : m_Impl(new Impl())
58 {
59  m_Impl->Name = name;
60  m_Impl->Key = name;
61  m_Impl->MimeTypeName = mimeTypeName;
62 }
63 
65 {
66  delete m_Impl;
67 }
68 
70 {
71  return m_Impl->Name;
72 }
73 
75 {
76  return m_Impl->Key;
77 }
78 
79 void mitk::PropertyPersistenceInfo::SetName(const std::string &name)
80 {
81  m_Impl->Name = name;
82  m_Impl->Key = name;
83  m_Impl->IsRegEx = false;
84  m_Impl->NameTemplate.clear();
85  m_Impl->KeyTemplate.clear();
86 }
87 
88 void mitk::PropertyPersistenceInfo::SetNameAndKey(const std::string &name, const std::string &key)
89 {
90  m_Impl->Name = name;
91  m_Impl->Key = key;
92  m_Impl->IsRegEx = false;
93  m_Impl->NameTemplate.clear();
94  m_Impl->KeyTemplate.clear();
95 }
96 
97 void mitk::PropertyPersistenceInfo::UseRegEx(const std::string &nameRegEx, const std::string &nameTemplate)
98 {
99  std::regex checker(nameRegEx); // no exception => valid we can change the info
100  m_Impl->Name = nameRegEx;
101  m_Impl->Key = nameRegEx;
102  m_Impl->IsRegEx = true;
103  m_Impl->NameTemplate = nameTemplate;
104  m_Impl->KeyTemplate = nameTemplate;
105 }
106 
107 void mitk::PropertyPersistenceInfo::UseRegEx(const std::string &nameRegEx,
108  const std::string &nameTemplate,
109  const std::string &keyRegEx,
110  const std::string keyTemplate)
111 {
112  std::regex nameChecker(nameRegEx); // no exception => valid we can change the info
113  std::regex keyChecker(keyRegEx); // no exception => valid we can change the info
114  m_Impl->Name = nameRegEx;
115  m_Impl->Key = keyRegEx;
116  m_Impl->IsRegEx = true;
117  m_Impl->NameTemplate = nameTemplate;
118  m_Impl->KeyTemplate = keyTemplate;
119 }
120 
122 {
123  return m_Impl->IsRegEx;
124 };
125 
127 {
128  return m_Impl->KeyTemplate;
129 }
130 
132 {
133  return m_Impl->NameTemplate;
134 }
135 
137 {
138  return m_Impl->MimeTypeName;
139 };
140 
142 {
143  m_Impl->MimeTypeName = mimeTypeName;
144 };
145 
148 {
149  return m_Impl->DeSerFnc;
150 };
151 
154 {
155  m_Impl->DeSerFnc = fnc;
156 };
157 
159  const
160 {
161  return m_Impl->SerFnc;
162 };
163 
166 {
167  m_Impl->SerFnc = fnc;
168 };
169 
170 std::string GenerateFromTemplate(const std::string &sourceStr,
171  const std::string &templateStr,
172  const std::string &regexStr)
173 {
174  std::smatch sm;
175  std::regex ex(regexStr);
176  std::regex_match(sourceStr, sm, ex);
177 
178  std::string result = templateStr;
179 
180  int groupID = 0;
181  for (const auto &match : sm)
182  {
183  if (groupID)
184  {
185  std::ostringstream stream;
186  stream << "(\\$" << groupID << ")";
187  std::regex rex(stream.str());
188  result = std::regex_replace(result, rex, match.str());
189  }
190  ++groupID;
191  }
192 
193  return result;
194 };
195 
197  const std::string &propertyName) const
198 {
200  *(resultInfo->m_Impl) = *(this->m_Impl);
201 
202  if (this->IsRegEx())
203  {
204  std::string newKey = GenerateFromTemplate(propertyName, this->GetKeyTemplate(), this->GetName());
205  resultInfo->SetNameAndKey(propertyName, newKey);
206  }
207 
208  return resultInfo;
209 };
210 
212 {
214  *(resultInfo->m_Impl) = *(this->m_Impl);
215 
216  if (this->IsRegEx())
217  {
218  std::string newName = GenerateFromTemplate(key, this->GetNameTemplate(), this->GetKey());
219  resultInfo->SetNameAndKey(newName, key);
220  }
221 
222  return resultInfo;
223 };
224 
226 {
227  static std::string name = IOMimeTypes::DEFAULT_BASE_NAME() + ".any_type";
228  return name;
229 };
230 
231 void mitk::PropertyPersistenceInfo::PrintSelf(std::ostream &os, itk::Indent indent) const
232 {
233  this->Superclass::PrintSelf(os, indent);
234 
235  os << indent << "Name: " << this->m_Impl->Name << std::endl;
236  os << indent << "Key: " << this->m_Impl->Key << std::endl;
237  os << indent << "IsRegEx: " << this->m_Impl->IsRegEx << std::endl;
238  os << indent << "NameTemplate: " << this->m_Impl->NameTemplate << std::endl;
239  os << indent << "KeyTemplate: " << this->m_Impl->KeyTemplate << std::endl;
240  os << indent << "MimeTypeName: " << this->m_Impl->MimeTypeName << std::endl;
241 };
242 
243 std::ostream &mitk::operator<<(std::ostream &os, const PropertyPersistenceInfo &info)
244 {
245  info.Print(os);
246  return os;
247 }
248 
250 {
251  std::string result = "";
252  if (prop)
253  {
254  result = prop->GetValueAsString();
255  }
256  return result;
257 }
258 
260  const std::string &value)
261 {
263  return result.GetPointer();
264 }
const DeserializationFunctionType GetDeserializationFunction() const
void SetSerializationFunction(const SerializationFunctionType &fnc)
MITKCORE_EXPORT::std::string serializeByGetValueAsString(const mitk::BaseProperty *prop)
DataCollection - Class to facilitate loading/accessing structured data.
const SerializationFunctionType GetSerializationFunction() const
MITKCORE_EXPORT mitk::BaseProperty::Pointer deserializeToStringProperty(const std::string &value)
static void info(const char *fmt,...)
Definition: svm.cpp:86
PropertyPersistenceInfo::Pointer UnRegExByKey(const std::string &key) const
void PrintSelf(std::ostream &os, itk::Indent indent) const override
virtual std::string GetValueAsString() const
Abstract base class for properties.
Property persistence info. This class is used to specify the way the persistance of a property of Bas...
static std::string DEFAULT_BASE_NAME()
std::function< mitk::BaseProperty::Pointer(const std::string &)> DeserializationFunctionType
const MimeTypeNameType & GetMimeTypeName() const
void SetDeserializationFunction(const DeserializationFunctionType &fnc)
std::string GenerateFromTemplate(const std::string &sourceStr, const std::string &templateStr, const std::string &regexStr)
void SetName(const std::string &name)
const std::string & GetKeyTemplate() const
void SetMimeTypeName(const MimeTypeNameType &mimeTypeName)
PropertyPersistenceInfo(const std::string &name="")
Constructor.
const std::string & GetNameTemplate() const
void UseRegEx(const std::string &nameRegEx, const std::string &nameTemplate)
void SetNameAndKey(const std::string &name, const std::string &key)
static Pointer New()
std::function< std::string(const mitk::BaseProperty *)> SerializationFunctionType
PropertyPersistenceInfo::Pointer UnRegExByName(const std::string &propertyName) const
MITKCORE_EXPORT std::ostream & operator<<(std::ostream &o, DataNode::Pointer &dtn)