Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkFileReaderRegistry.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 "mitkFileReaderRegistry.h"
18 
19 #include "mitkCoreServices.h"
20 #include "mitkIMimeTypeProvider.h"
21 
22 // Microservices
23 #include <usGetModuleContext.h>
24 #include <usLDAPProp.h>
25 #include <usModuleContext.h>
26 #include <usServiceProperties.h>
27 
28 #include "itksys/SystemTools.hxx"
29 
31 {
32 }
33 
35 {
36  for (auto &elem : m_ServiceObjects)
37  {
38  elem.second.UngetService(elem.first);
39  }
40 }
41 
42 mitk::MimeType mitk::FileReaderRegistry::GetMimeTypeForFile(const std::string &path, us::ModuleContext *context)
43 {
44  if (path.empty())
45  {
46  mitkThrow() << "FileReaderRegistry::GetMimeTypeForFile was called with empty path. Returning empty MimeType, "
47  "please report this error to the developers.";
48  }
49 
51  std::vector<MimeType> mimeTypes = mimeTypeProvider->GetMimeTypesForFile(path);
52  if (mimeTypes.empty())
53  {
54  return MimeType();
55  }
56  else
57  {
58  return mimeTypes.front();
59  }
60 }
61 
62 std::vector<mitk::FileReaderRegistry::ReaderReference> mitk::FileReaderRegistry::GetReferences(
63  const MimeType &mimeType, us::ModuleContext *context)
64 {
65  if (context == NULL)
66  context = us::GetModuleContext();
67 
68  std::string filter = us::LDAPProp(us::ServiceConstants::OBJECTCLASS()) == us_service_interface_iid<IFileReader>() &&
70  return context->GetServiceReferences<IFileReader>(filter);
71 }
72 
74  us::ModuleContext *context)
75 {
76  if (context == NULL)
77  context = us::GetModuleContext();
78 
79  us::ServiceObjects<mitk::IFileReader> serviceObjects = context->GetServiceObjects(ref);
80  mitk::IFileReader *reader = serviceObjects.GetService();
81  m_ServiceObjects.insert(std::make_pair(reader, serviceObjects));
82  return reader;
83 }
84 
85 std::vector<mitk::IFileReader *> mitk::FileReaderRegistry::GetReaders(const MimeType &mimeType,
86  us::ModuleContext *context)
87 {
88  if (context == NULL)
89  context = us::GetModuleContext();
90 
91  std::vector<mitk::IFileReader *> result;
92 
93  if (!mimeType.IsValid())
94  return result;
95 
96  std::vector<us::ServiceReference<IFileReader>> refs = GetReferences(mimeType, context);
97  std::sort(refs.begin(), refs.end());
98 
99  result.reserve(refs.size());
100 
101  // Translate List of ServiceRefs to List of Pointers
102  for (std::vector<us::ServiceReference<IFileReader>>::const_reverse_iterator iter = refs.rbegin(), end = refs.rend();
103  iter != end;
104  ++iter)
105  {
106  us::ServiceObjects<mitk::IFileReader> serviceObjects = context->GetServiceObjects(*iter);
107  mitk::IFileReader *reader = serviceObjects.GetService();
108  m_ServiceObjects.insert(std::make_pair(reader, serviceObjects));
109  result.push_back(reader);
110  }
111 
112  return result;
113 }
114 
116 {
117  std::map<mitk::IFileReader *, us::ServiceObjects<mitk::IFileReader>>::iterator readerIter =
118  m_ServiceObjects.find(reader);
119  if (readerIter != m_ServiceObjects.end())
120  {
121  readerIter->second.UngetService(reader);
122  m_ServiceObjects.erase(readerIter);
123  }
124 }
125 
126 void mitk::FileReaderRegistry::UngetReaders(const std::vector<mitk::IFileReader *> &readers)
127 {
128  for (const auto &reader : readers)
129  {
130  this->UngetReader(reader);
131  }
132 }
S * GetService() const
static IMimeTypeProvider * GetMimeTypeProvider(us::ModuleContext *context=us::GetModuleContext())
Get an IMimeTypeProvider instance.
mitk::IFileReader * GetReader(const ReaderReference &ref, us::ModuleContext *context=us::GetModuleContext())
std::vector< mitk::IFileReader * > GetReaders(const MimeType &mimeType, us::ModuleContext *context=us::GetModuleContext())
static MimeType GetMimeTypeForFile(const std::string &path, us::ModuleContext *context=us::GetModuleContext())
Get the highest ranked mime-type for the given file name.
bool IsValid() const
static std::string PROP_MIMETYPE()
Service property name for the mime-type associated with this file writer.
Definition: mitkIFileIO.cpp:28
std::string GetName() const
#define mitkThrow()
US_Core_EXPORT const std::string & OBJECTCLASS()
The MimeType class represens a registered mime-type. It is an immutable wrapper for mitk::CustomMimeT...
Definition: mitkMimeType.h:45
void UngetReaders(const std::vector< mitk::IFileReader * > &readers)
A RAII helper class for core service objects.
static std::vector< ReaderReference > GetReferences(const MimeType &mimeType, us::ModuleContext *context=us::GetModuleContext())
The common interface for all MITK file readers.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
virtual std::vector< MimeType > GetMimeTypesForFile(const std::string &filePath) const =0
void UngetReader(mitk::IFileReader *reader)