30 #include <itksys/SystemTools.hxx>
36 struct AbstractFileWriter::LocalFile::Impl
38 Impl(
const std::string &location, std::ostream *os) : m_Location(location), m_Stream(os) {}
39 std::string m_Location;
40 std::string m_TmpFileName;
41 std::ostream *m_Stream;
45 : d(new Impl(writer->GetOutputLocation(), writer->GetOutputStream()))
51 if (d->m_Stream && !d->m_TmpFileName.empty())
53 std::ifstream ifs(d->m_TmpFileName.c_str(), std::ios_base::binary);
54 *d->m_Stream << ifs.rdbuf();
57 std::remove(d->m_TmpFileName.c_str());
63 if (d->m_Stream == NULL)
67 else if (d->m_TmpFileName.empty())
69 std::string ext = itksys::SystemTools::GetFilenameExtension(d->m_Location);
72 return d->m_TmpFileName;
76 :
std::ostream(NULL), m_Stream(NULL)
81 this->init(stream->rdbuf());
86 this->init(m_Stream->rdbuf());
95 Impl(
const Impl &other)
97 m_BaseDataType(other.m_BaseDataType),
100 m_PrototypeFactory(NULL)
104 std::string m_BaseDataType;
105 const BaseData *m_BaseData;
106 std::string m_Location;
107 std::ostream *m_Stream;
117 d->m_Location = location;
124 d->m_Location = location;
133 delete d->m_PrototypeFactory;
142 d->m_BaseDataType = baseDataType;
147 const std::string &description)
150 d->m_BaseDataType = baseDataType;
151 d->SetMimeType(mimeType);
152 d->SetDescription(description);
159 if (d->m_BaseData == NULL)
162 std::vector<std::string> classHierarchy = d->m_BaseData->GetClassHierarchy();
163 if (std::find(classHierarchy.begin(), classHierarchy.end(), d->m_BaseDataType) == classHierarchy.end())
175 if (d->m_PrototypeFactory)
183 d->RegisterMimeType(context);
187 MITK_WARN <<
"Not registering writer due to empty MIME type.";
206 delete us::ExtractInterface<IFileWriter>(service);
210 d->m_PrototypeFactory =
new PrototypeFactory(
this);
212 d->m_Reg = context->RegisterService<
IFileWriter>(d->m_PrototypeFactory, props);
220 d->m_Reg.Unregister();
222 catch (
const std::exception &)
247 return d->RegisterMimeType(context);
256 d->SetDefaultOptions(defaultOptions);
269 d->RemoveProgressCallback(callback);
284 if (fileName.empty())
286 mitkThrow() <<
"No output location or stream specified";
AbstractFileWriter(const AbstractFileWriter &other)
virtual void SetInput(const BaseData *data) override
Set the input data for writing.
US_Core_EXPORT const std::string & SERVICE_RANKING()
Base of all data objects.
virtual 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.
virtual us::ServiceProperties GetServiceProperties() const
DataCollection - Class to facilitate loading/accessing structured data.
void ValidateOutputLocation() const
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)
const CustomMimeType * GetMimeType() const
virtual 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...
virtual std::string GetBaseDataType() const
void SetRanking(int ranking)
Set the service ranking for this file writer.
Options GetDefaultOptions() const
MimeType GetRegisteredMimeType() const
virtual void SetOutputStream(const std::string &location, std::ostream *os) override
Set an output stream for writing.
virtual const BaseData * GetInput() const override
Get the input data set via SetInput().
void SetBaseDataType(const std::string &baseDataType)
Sets the name of the mitk::Basedata that this writer is able to handle.
std::string GetName() const
Returns the unique name for the MimeType.
virtual 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.
virtual std::ostream * GetOutputStream() const override
Get the output stream.
virtual 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)
virtual void SetOptions(const Options &options) override
virtual std::string GetOutputLocation() const override
Get the current output location.
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
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.
virtual us::Any GetOption(const std::string &name) const override
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
virtual void AddProgressCallback(const ProgressCallback &callback) override
std::string GetMimeTypePrefix() const
virtual void RemoveProgressCallback(const ProgressCallback &callback) override
std::string GetDescription() const