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