28 #include <itksys/SystemTools.hxx>
38 Impl() : m_FileWriter(NULL), m_ConfidenceLevel(IFileWriter::Unsupported), m_BaseDataIndex(0), m_Id(-1) {}
40 IFileWriter *m_FileWriter;
42 std::size_t m_BaseDataIndex;
49 Impl() : m_BestId(-1), m_SelectedId(m_BestId) {}
50 Impl(
const Impl &other) :
us::
SharedData(other), m_BestId(-1), m_SelectedId(m_BestId) {}
51 FileWriterRegistry m_WriterRegistry;
52 std::map<long, FileWriterSelector::Item> m_Items;
53 std::set<MimeType> m_MimeTypes;
64 std::vector<FileWriterRegistry::WriterReference> refs;
66 std::string destMimeType = mimeType;
67 if (destMimeType.empty() && !path.empty())
71 if (!mimeTypes.empty())
73 for (
unsigned int index = 0; index < mimeTypes.size(); index++)
75 std::vector<FileWriterRegistry::WriterReference> tempRefs =
76 m_Data->m_WriterRegistry.GetReferences(baseData, mimeTypes.at(index).GetName());
77 for (
unsigned int innerIndex = 0; innerIndex < tempRefs.size(); innerIndex++)
79 refs.push_back(tempRefs.at(innerIndex));
83 else if (!itksys::SystemTools::GetFilenameExtension(path).empty())
91 refs = m_Data->m_WriterRegistry.GetReferences(baseData, destMimeType);
96 refs = m_Data->m_WriterRegistry.GetReferences(baseData, destMimeType);
103 for (std::vector<FileWriterRegistry::WriterReference>::const_iterator iter = refs.begin(), iterEnd = refs.end();
108 if (!mimeTypeName.empty())
116 IFileWriter *writer = m_Data->m_WriterRegistry.GetWriter(*iter);
129 std::vector<std::string>::iterator idxIter =
130 std::find(classHierarchy.begin(), classHierarchy.end(), baseDataType);
132 if (idxIter != classHierarchy.end())
134 baseDataIndex = std::distance(classHierarchy.begin(), idxIter);
138 item.d->m_FileWriterRef = *iter;
139 item.d->m_FileWriter = writer;
140 item.d->m_ConfidenceLevel = confidenceLevel;
141 item.d->m_BaseDataIndex = baseDataIndex;
142 item.d->m_MimeType = mimeType;
144 m_Data->m_Items.insert(std::make_pair(item.d->m_Id, item));
145 m_Data->m_MimeTypes.insert(mimeType);
155 catch (
const std::exception &e)
158 MITK_WARN <<
"IFileWriter::GetConfidenceLevel exception: " << e.what();
167 m_Data->m_SelectedId = m_Data->m_BestId;
174 m_Data = other.m_Data;
181 std::vector<Item> result;
182 for (std::map<long, Item>::const_iterator iter = m_Data->m_Items.begin(), iterEnd = m_Data->m_Items.end();
186 if (mimeType.empty() || iter->second.GetMimeType().GetName() == mimeType)
188 result.push_back(iter->second);
191 std::sort(result.begin(), result.end());
202 std::map<long, Item>::const_iterator iter = m_Data->m_Items.find(
id);
203 if (iter != m_Data->m_Items.end())
216 std::vector<Item> items =
Get(mimeType);
219 return Select(items.back());
227 if (m_Data->m_Items.find(
id) == m_Data->m_Items.end())
231 m_Data->m_SelectedId = id;
239 std::vector<MimeType> result;
240 result.reserve(m_Data->m_MimeTypes.size());
241 result.assign(m_Data->m_MimeTypes.begin(), m_Data->m_MimeTypes.end());
259 return std::string();
269 return std::string();
278 if (d->m_ConfidenceLevel == other.d->m_ConfidenceLevel)
282 if (d->m_BaseDataIndex == other.d->m_BaseDataIndex)
285 return d->m_FileWriterRef < other.d->m_FileWriterRef;
287 return other.d->m_BaseDataIndex < d->m_BaseDataIndex;
289 return d->m_ConfidenceLevel < other.d->m_ConfidenceLevel;
292 FileWriterSelector::Item::Item() : d(new Impl()) {}
bool operator<(const Item &other) const
virtual ConfidenceLevel GetConfidenceLevel() const =0
The confidence level of the reader or writer implementation.
us::ServiceReference< IFileWriter > GetReference() const
static IMimeTypeProvider * GetMimeTypeProvider(us::ModuleContext *context=us::GetModuleContext())
Get an IMimeTypeProvider instance.
virtual const char * what() const override
long GetSelectedId() const
Base of all data objects.
long GetServiceId() const
Item & operator=(const Item &other)
virtual MimeType GetMimeTypeForName(const std::string &name) const =0
static std::string PROP_DESCRIPTION()
Service property name for a description.
bool Select(const std::string &mimeType)
DataCollection - Class to facilitate loading/accessing structured data.
ValueType * any_cast(Any *operand)
std::vector< Item > Get() const
Get a sorted list of file writer info objects.
static std::string PROP_MIMETYPE()
Service property name for the mime-type associated with this file writer.
virtual std::vector< std::string > GetClassHierarchy() const
void Swap(ExplicitlySharedDataPointer &other)
std::string GetDescription() const
void swap(CustomMimeType &l, CustomMimeType &r)
std::vector< MimeType > GetMimeTypes() const
virtual void SetInput(const BaseData *data)=0
Set the input data for writing.
std::string ToString() const
The MimeType class represens a registered mime-type. It is an immutable wrapper for mitk::CustomMimeT...
FileWriterSelector & operator=(const FileWriterSelector &other)
static std::string PROP_BASEDATA_TYPE()
Service property name for the supported mitk::BaseData sub-class.
FileWriterSelector(const FileWriterSelector &other)
long GetDefaultId() const
std::string GetBaseDataType() const
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
A RAII helper class for core service objects.
IFileWriter::ConfidenceLevel GetConfidenceLevel() const
US_Core_EXPORT const std::string & SERVICE_ID()
The common interface of all MITK file writers.
MimeType GetMimeType() const
IFileWriter * GetWriter() const
void Swap(FileWriterSelector &fws)
virtual std::vector< MimeType > GetMimeTypesForFile(const std::string &filePath) const =0