Medical Imaging Interaction Toolkit  2018.4.99-b585543d
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 (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 "mitkFileReaderRegistry.h"
14 
15 #include "mitkCoreServices.h"
16 #include "mitkIMimeTypeProvider.h"
17 
18 // Microservices
19 #include <usGetModuleContext.h>
20 #include <usLDAPProp.h>
21 #include <usModuleContext.h>
22 #include <usServiceProperties.h>
23 
24 #include "itksys/SystemTools.hxx"
25 
27 {
28 }
29 
31 {
32  for (auto &elem : m_ServiceObjects)
33  {
34  elem.second.UngetService(elem.first);
35  }
36 }
37 
38 mitk::MimeType mitk::FileReaderRegistry::GetMimeTypeForFile(const std::string &path, us::ModuleContext *context)
39 {
40  if (path.empty())
41  {
42  mitkThrow() << "FileReaderRegistry::GetMimeTypeForFile was called with empty path. Returning empty MimeType, "
43  "please report this error to the developers.";
44  }
45 
47  std::vector<MimeType> mimeTypes = mimeTypeProvider->GetMimeTypesForFile(path);
48  if (mimeTypes.empty())
49  {
50  return MimeType();
51  }
52  else
53  {
54  return mimeTypes.front();
55  }
56 }
57 
58 std::vector<mitk::FileReaderRegistry::ReaderReference> mitk::FileReaderRegistry::GetReferences(
59  const MimeType &mimeType, us::ModuleContext *context)
60 {
61  if (context == nullptr)
62  context = us::GetModuleContext();
63 
64  std::string filter = us::LDAPProp(us::ServiceConstants::OBJECTCLASS()) == us_service_interface_iid<IFileReader>() &&
66  return context->GetServiceReferences<IFileReader>(filter);
67 }
68 
70  us::ModuleContext *context)
71 {
72  if (context == nullptr)
73  context = us::GetModuleContext();
74 
75  us::ServiceObjects<mitk::IFileReader> serviceObjects = context->GetServiceObjects(ref);
76  mitk::IFileReader *reader = serviceObjects.GetService();
77  m_ServiceObjects.insert(std::make_pair(reader, serviceObjects));
78  return reader;
79 }
80 
81 std::vector<mitk::IFileReader *> mitk::FileReaderRegistry::GetReaders(const MimeType &mimeType,
82  us::ModuleContext *context)
83 {
84  if (context == nullptr)
85  context = us::GetModuleContext();
86 
87  std::vector<mitk::IFileReader *> result;
88 
89  if (!mimeType.IsValid())
90  return result;
91 
92  std::vector<us::ServiceReference<IFileReader>> refs = GetReferences(mimeType, context);
93  std::sort(refs.begin(), refs.end());
94 
95  result.reserve(refs.size());
96 
97  // Translate List of ServiceRefs to List of Pointers
98  for (std::vector<us::ServiceReference<IFileReader>>::const_reverse_iterator iter = refs.rbegin(), end = refs.rend();
99  iter != end;
100  ++iter)
101  {
102  us::ServiceObjects<mitk::IFileReader> serviceObjects = context->GetServiceObjects(*iter);
103  mitk::IFileReader *reader = serviceObjects.GetService();
104  m_ServiceObjects.insert(std::make_pair(reader, serviceObjects));
105  result.push_back(reader);
106  }
107 
108  return result;
109 }
110 
112 {
113  auto readerIter =
114  m_ServiceObjects.find(reader);
115  if (readerIter != m_ServiceObjects.end())
116  {
117  readerIter->second.UngetService(reader);
118  m_ServiceObjects.erase(readerIter);
119  }
120 }
121 
122 void mitk::FileReaderRegistry::UngetReaders(const std::vector<mitk::IFileReader *> &readers)
123 {
124  for (const auto &reader : readers)
125  {
126  this->UngetReader(reader);
127  }
128 }
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.
std::string GetName() const
static std::string PROP_MIMETYPE()
Service property name for the mime-type associated with this file writer.
Definition: mitkIFileIO.cpp:24
bool IsValid() const
void UngetReaders(const std::vector< mitk::IFileReader *> &readers)
#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:36
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)