26 #include <itksys/SystemTools.hxx> 32 struct AbstractFileWriter::LocalFile::Impl
34 Impl(
const std::string &location, std::ostream *os) : m_Location(location), m_Stream(os) {}
35 std::string m_Location;
36 std::string m_TmpFileName;
37 std::ostream *m_Stream;
47 if (d->m_Stream && !d->m_TmpFileName.empty())
49 std::ifstream ifs(d->m_TmpFileName.c_str(), std::ios_base::binary);
50 *d->m_Stream << ifs.rdbuf();
53 std::remove(d->m_TmpFileName.c_str());
59 if (d->m_Stream ==
nullptr)
63 else if (d->m_TmpFileName.empty())
65 std::string ext = itksys::SystemTools::GetFilenameExtension(d->m_Location);
68 return d->m_TmpFileName;
72 :
std::ostream(nullptr), m_Stream(nullptr)
77 this->init(stream->rdbuf());
82 this->init(m_Stream->rdbuf());
90 Impl() :
FileReaderWriterBase(), m_BaseData(
nullptr), m_Stream(
nullptr), m_PrototypeFactory(
nullptr) {}
91 Impl(
const Impl &other)
93 m_BaseDataType(other.m_BaseDataType),
96 m_PrototypeFactory(
nullptr)
100 std::string m_BaseDataType;
102 std::string m_Location;
103 std::ostream *m_Stream;
113 d->m_Location = location;
114 d->m_Stream =
nullptr;
120 d->m_Location = location;
129 delete d->m_PrototypeFactory;
138 d->m_BaseDataType = baseDataType;
143 const std::string &description)
146 d->m_BaseDataType = baseDataType;
147 d->SetMimeType(mimeType);
148 d->SetDescription(description);
155 if (d->m_BaseData ==
nullptr)
158 std::vector<std::string> classHierarchy = d->m_BaseData->GetClassHierarchy();
159 if (std::find(classHierarchy.begin(), classHierarchy.end(), d->m_BaseDataType) == classHierarchy.end())
171 if (d->m_PrototypeFactory)
174 if (context ==
nullptr)
179 d->RegisterMimeType(context);
183 MITK_WARN <<
"Not registering writer due to empty MIME type.";
202 delete us::ExtractInterface<IFileWriter>(service);
206 d->m_PrototypeFactory =
new PrototypeFactory(
this);
208 d->m_Reg = context->RegisterService<
IFileWriter>(d->m_PrototypeFactory, props);
216 d->m_Reg.Unregister();
218 catch (
const std::exception &)
243 return d->RegisterMimeType(context);
252 d->SetDefaultOptions(defaultOptions);
265 d->RemoveProgressCallback(callback);
280 if (fileName.empty())
282 mitkThrow() <<
"No output location or stream specified";
AbstractFileWriter(const AbstractFileWriter &other)
MimeType GetRegisteredMimeType() const
void SetInput(const BaseData *data) override
Set the input data for writing.
virtual us::ServiceProperties GetServiceProperties() const
US_Core_EXPORT const std::string & SERVICE_RANKING()
Base of all data objects.
const CustomMimeType * GetMimeType() const
virtual std::string GetBaseDataType() const
Options GetOptions() const override
returns a list of the supported options
std::map< std::string, void * > InterfaceMap
std::string GetFileName()
static std::string PROP_DESCRIPTION()
Service property name for a description.
std::string GetName() const
Returns the unique name for the MimeType.
~AbstractFileWriter() override
DataCollection - Class to facilitate loading/accessing structured data.
virtual std::string GetOutputLocation() const =0
Get the current output location.
std::map< std::string, us::Any > Options
Options for reading or writing data.
virtual us::ServiceRegistration< CustomMimeType > RegisterMimeType(us::ModuleContext *context)
static std::string PROP_MIMETYPE()
Service property name for the mime-type associated with this file writer.
void SetDefaultOptions(const Options &defaultOptions)
ConfidenceLevel GetConfidenceLevel() const override
The confidence level of the reader or writer implementation.
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
void SetRanking(int ranking)
Set the service ranking for this file writer.
void SetOutputStream(const std::string &location, std::ostream *os) override
Set an output stream for writing.
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
std::string GetDescription() const
const BaseData * GetInput() const override
Get the input data set via SetInput().
Options GetDefaultOptions() const
void SetBaseDataType(const std::string &baseDataType)
Sets the name of the mitk::Basedata that this writer is able to handle.
void ValidateOutputLocation() const
std::string GetMimeTypePrefix() const
void SetOutputLocation(const std::string &location) override
Set the output location.
The MimeType class represens a registered mime-type. It is an immutable wrapper for mitk::CustomMimeT...
US_UNORDERED_MAP_TYPE< std::string, Any > ServiceProperties
virtual std::ostream * GetOutputStream() const =0
Get the output stream.
std::ostream * GetOutputStream() const override
Get the output stream.
void SetOption(const std::string &name, const us::Any &value) override
static std::string PROP_BASEDATA_TYPE()
Service property name for the supported mitk::BaseData sub-class.
static std::string CreateTemporaryFile(std::ofstream &tmpStream, const std::string &templateName="XXXXXX", std::string path=std::string())
us::ServiceRegistration< IFileWriter > RegisterService(us::ModuleContext *context=us::GetModuleContext())
void SetMimeType(const CustomMimeType &mimeType)
static std::string GetName(std::string fileName, std::string suffix)
OutputStream(IFileWriter *writer, std::ios_base::openmode mode=std::ios_base::trunc|std::ios_base::out)
void SetOptions(const Options &options) override
std::string GetOutputLocation() const override
Get the current output location.
void SetMimeTypePrefix(const std::string &prefix)
The common interface of all MITK file writers.
Base class for writing mitk::BaseData objects to files or streams.
void SetDescription(const std::string &description)
Sets a human readable description of this writer.
us::Any GetOption(const std::string &name) const override
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void AddProgressCallback(const ProgressCallback &callback) override
void RemoveProgressCallback(const ProgressCallback &callback) override