diff --git a/Core/Code/IO/mitkAbstractFileReader.cpp b/Core/Code/IO/mitkAbstractFileReader.cpp index 3201b3bbb5..e31382e807 100644 --- a/Core/Code/IO/mitkAbstractFileReader.cpp +++ b/Core/Code/IO/mitkAbstractFileReader.cpp @@ -1,163 +1,166 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include #include #include mitk::AbstractFileReader::AbstractFileReader() : m_Priority (0) { } mitk::AbstractFileReader::AbstractFileReader(const std::string& extension, const std::string& description) : m_Extension (extension), m_Description (description), m_Priority (0) { } ////////////////// Filenames etc. ////////////////// std::string mitk::AbstractFileReader::GetFileName() const { return m_FileName; } void mitk::AbstractFileReader::SetFileName(const std::string& aFileName) { m_FileName = aFileName; } // FILE PATTERN mechanic is currently deactivated until decision has been reached on how to handle patterns // //std::string mitk::AbstractFileReader::GetFilePrefix() const //{ // return m_FilePrefix; //} // //void mitk::AbstractFileReader::SetFilePrefix(const std::string& aFilePrefix) //{ // m_FilePrefix = aFilePrefix; //} // //std::string mitk::AbstractFileReader::GetFilePattern() const //{ // return m_FilePattern; //} // //void mitk::AbstractFileReader::SetFilePattern(const std::string& aFilePattern) //{ // m_FilePattern = aFilePattern; //} ////////////////////// Reading ///////////////////////// std::list< itk::SmartPointer > mitk::AbstractFileReader::Read(const std::string& path, mitk::DataStorage* /*ds*/) { if (! itksys::SystemTools::FileExists(path.c_str())) mitkThrow() << "File '" + path + "' not found."; std::ifstream stream; stream.open(path.c_str()); return this->Read(stream); } //////////// µS Registration & Properties ////////////// -void mitk::AbstractFileReader::RegisterMicroservice(us::ModuleContext* context) +us::ServiceRegistration mitk::AbstractFileReader::RegisterService(us::ModuleContext* context) { + if (m_Registration) return m_Registration; + us::ServiceProperties props = this->ConstructServiceProperties(); m_Registration = context->RegisterService(this, props); + return m_Registration; } -void mitk::AbstractFileReader::UnregisterMicroservice(us::ModuleContext* /*context*/) +void mitk::AbstractFileReader::UnregisterService() { if (! m_Registration ) { MITK_WARN << "Someone tried to unregister a FileReader, but it was either not registered or the registration has expired."; return; } m_Registration.Unregister(); } us::ServiceProperties mitk::AbstractFileReader::ConstructServiceProperties() { if ( m_Extension.empty() ) MITK_WARN << "Registered a Reader with no extension defined (m_Extension is empty). Reader will not be found by calls from ReaderManager.)"; if ( m_Description.empty() ) MITK_WARN << "Registered a Reader with no description defined (m_Description is empty). Reader will have no human readable extension information in FileDialogs.)"; std::transform(m_Extension.begin(), m_Extension.end(), m_Extension.begin(), ::tolower); us::ServiceProperties result; result[mitk::IFileReader::PROP_EXTENSION] = m_Extension; result[mitk::IFileReader::PROP_DESCRIPTION] = m_Description; result[us::ServiceConstants::SERVICE_RANKING()] = m_Priority; for (std::list::const_iterator it = m_Options.begin(); it != m_Options.end(); ++it) { result[*it] = std::string("true"); } return result; } //////////////////////// Options /////////////////////// std::list< std::string > mitk::AbstractFileReader::GetSupportedOptions() const { return m_Options; } ////////////////// MISC ////////////////// bool mitk::AbstractFileReader::CanRead(const std::string& path) const { // Default implementation only checks if extension is correct std::string pathEnd = path.substr( path.length() - m_Extension.length(), m_Extension.length() ); return (m_Extension == pathEnd); } float mitk::AbstractFileReader::GetProgress() const { // Default implementation always returns 1 (finished) return 1; } ////////////////// µS related Getters ////////////////// int mitk::AbstractFileReader::GetPriority() const { return m_Priority; } std::string mitk::AbstractFileReader::GetExtension() const { return m_Extension; } std::string mitk::AbstractFileReader::GetDescription() const { return m_Description; } diff --git a/Core/Code/IO/mitkAbstractFileReader.h b/Core/Code/IO/mitkAbstractFileReader.h index f6e25f2225..830eecaf49 100644 --- a/Core/Code/IO/mitkAbstractFileReader.h +++ b/Core/Code/IO/mitkAbstractFileReader.h @@ -1,135 +1,135 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef AbstractFileReader_H_HEADER_INCLUDED_C1E7E521 #define AbstractFileReader_H_HEADER_INCLUDED_C1E7E521 // Macro #include // MITK #include #include // Microservices #include #include +#include namespace mitk { //##Documentation //## @brief Interface class of readers that read from files //## @ingroup Process class MITK_CORE_EXPORT AbstractFileReader : public mitk::IFileReader { public: //##Documentation //## @brief Get the specified the file to load. //## //## Either the FileName or FilePrefix plus FilePattern are used to read. virtual std::string GetFileName() const; //##Documentation //## @brief Specify the file to load. //## //## Either the FileName or FilePrefix plus FilePattern are used to read. virtual void SetFileName(const std::string& aFileName); //##Documentation //## @brief Get the specified file prefix for the file(s) to load. //## //## You should specify either a FileName or FilePrefix. Use FilePrefix if //## the data is stored in multiple files. // File Pattern mechanic is currently deactivated until decision has been reached on how to handle patterns //virtual std::string GetFilePrefix() const; //##Documentation //## @brief Specify file prefix for the file(s) to load. //## //## You should specify either a FileName or FilePrefix. Use FilePrefix if //## the data is stored in multiple files. // File Pattern mechanic is currently deactivated until decision has been reached on how to handle patterns // virtual void SetFilePrefix(const std::string& aFilePrefix); //##Documentation //## @brief Get the specified file pattern for the file(s) to load. The //## sprintf format used to build filename from FilePrefix and number. //## //## You should specify either a FileName or FilePrefix. Use FilePrefix if //## the data is stored in multiple files. // File Pattern mechanic is currently deactivated until decision has been reached on how to handle patterns //virtual std::string GetFilePattern() const; /** @brief Specified file pattern for the file(s) to load. The sprintf format used to build filename from FilePrefix and number. You should specify either a FileName or FilePrefix. Use FilePrefix if the data is stored in multiple files. */ // File Pattern mechanic is currently deactivated until decision has been reached on how to handle patterns //virtual void SetFilePattern(const std::string& aFilePattern); virtual std::list< itk::SmartPointer > Read(const std::string& path, mitk::DataStorage *ds = 0 ); virtual std::list< itk::SmartPointer > Read(const std::istream& stream, mitk::DataStorage *ds = 0 ) = 0; virtual int GetPriority() const; virtual std::string GetExtension() const; virtual std::string GetDescription() const; virtual std::list< std::string > GetSupportedOptions() const; virtual bool CanRead(const std::string& path) const; virtual float GetProgress() const; + us::ServiceRegistration RegisterService(us::ModuleContext* context = us::GetModuleContext()); + + void UnregisterService(); protected: AbstractFileReader(); AbstractFileReader(const std::string& extension, const std::string& description); // Filenames etc.. std::string m_FileName; std::string m_FilePrefix; std::string m_FilePattern; // Minimal Service Properties: ALWAYS SET THESE IN CONSTRUCTOR OF DERIVED CLASSES! std::string m_Extension; std::string m_Description; int m_Priority; std::list< std::string > m_Options; // Options supported by this reader. Can be left emtpy if no special options are required // Registration us::ServiceRegistration m_Registration; - virtual void RegisterMicroservice(us::ModuleContext* context); - - virtual void UnregisterMicroservice(us::ModuleContext* context); - virtual us::ServiceProperties ConstructServiceProperties(); }; } // namespace mitk // This is the microservice declaration. Do not meddle! US_DECLARE_SERVICE_INTERFACE(mitk::AbstractFileReader, "org.mitk.services.FileReader") #endif /* AbstractFileReader_H_HEADER_INCLUDED_C1E7E521 */ diff --git a/Core/Code/IO/mitkAbstractFileWriter.cpp b/Core/Code/IO/mitkAbstractFileWriter.cpp index 99e89c731e..3b801d2df6 100644 --- a/Core/Code/IO/mitkAbstractFileWriter.cpp +++ b/Core/Code/IO/mitkAbstractFileWriter.cpp @@ -1,174 +1,177 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include #include #include #include mitk::AbstractFileWriter::AbstractFileWriter() : m_Priority (0) { } mitk::AbstractFileWriter::AbstractFileWriter(const std::string& basedataType, const std::string& extension, const std::string& description) : m_Extension (extension), m_BasedataType(basedataType), m_Description (description), m_Priority (0) { } ////////////////// Filenames etc. ////////////////// std::string mitk::AbstractFileWriter::GetFileName() const { return m_FileName; } void mitk::AbstractFileWriter::SetFileName(const std::string& aFileName) { m_FileName = aFileName; } //std::string mitk::AbstractFileWriter::GetFilePrefix() const //{ // return m_FilePrefix; //} // //void mitk::AbstractFileWriter::SetFilePrefix(const std::string& aFilePrefix) //{ // m_FilePrefix = aFilePrefix; //} // //std::string mitk::AbstractFileWriter::GetFilePattern() const //{ // return m_FilePattern; //} // //void mitk::AbstractFileWriter::SetFilePattern(const std::string& aFilePattern) //{ // m_FilePattern = aFilePattern; //} ////////////////////// Writing ///////////////////////// void mitk::AbstractFileWriter::Write(const BaseData *data) { this->Write(data, GetFileName()); } void mitk::AbstractFileWriter::Write(const BaseData* data, const std::string& path) { if (! itksys::SystemTools::FileExists(path.c_str())) mitkThrow() << "File '" + path + "' not found."; std::ofstream stream; stream.open(path.c_str()); this->Write(data, stream); } //////////// µS Registration & Properties ////////////// -void mitk::AbstractFileWriter::RegisterMicroservice(us::ModuleContext* context) +us::ServiceRegistration mitk::AbstractFileWriter::RegisterService(us::ModuleContext* context) { + if (m_Registration) return m_Registration; + us::ServiceProperties props = this->ConstructServiceProperties(); m_Registration = context->RegisterService(this, props); + return m_Registration; } -void mitk::AbstractFileWriter::UnregisterMicroservice(us::ModuleContext* /*context*/) +void mitk::AbstractFileWriter::UnregisterService() { if (! m_Registration ) { MITK_WARN << "Someone tried to unregister a FileWriter, but it was either not registered or the registration has expired."; return; } m_Registration.Unregister(); } us::ServiceProperties mitk::AbstractFileWriter::ConstructServiceProperties() { if ( m_Extension.empty() ) MITK_WARN << "Registered a Writer with no extension defined (m_Extension is empty). Writer will not be found by calls from WriterManager.)"; if ( m_BasedataType.empty() ) MITK_WARN << "Registered a Writer with no BasedataType defined (m_BasedataType is empty). Writer will not be found by calls from WriterManager.)"; if ( m_Description.empty() ) MITK_WARN << "Registered a Writer with no description defined (m_Description is empty). Writer will have no human readable extension information in FileDialogs.)"; us::ServiceProperties result; result[mitk::IFileWriter::PROP_EXTENSION] = m_Extension; result[mitk::IFileWriter::PROP_DESCRIPTION] = m_Description; result[mitk::IFileWriter::PROP_BASEDATA_TYPE] = m_BasedataType; result[us::ServiceConstants::SERVICE_RANKING()] = m_Priority; for (std::list::const_iterator it = m_Options.begin(); it != m_Options.end(); ++it) { result[*it] = std::string("true"); } return result; } //////////////////////// Options /////////////////////// std::list< std::string > mitk::AbstractFileWriter::GetSupportedOptions() const { return m_Options; } ////////////////// MISC ////////////////// bool mitk::AbstractFileWriter::CanWrite(const BaseData* data, const std::string& path) const { // Default implementation only checks if extension and basedatatype are correct std::string pathEnd = path.substr( path.length() - m_Extension.length(), m_Extension.length() ); if ( !(m_Extension == pathEnd)) return false; std::string externalDataType = data->GetNameOfClass(); return (externalDataType == m_BasedataType); } float mitk::AbstractFileWriter::GetProgress() const { // Default implementation always returns 1 (finished) return 1; } ////////////////// µS related Getters ////////////////// int mitk::AbstractFileWriter::GetPriority() const { return m_Priority; } std::string mitk::AbstractFileWriter::GetExtension() const { return m_Extension; } std::string mitk::AbstractFileWriter::GetDescription() const { return m_Description; } std::string mitk::AbstractFileWriter::GetSupportedBasedataType() const { return m_BasedataType; } diff --git a/Core/Code/IO/mitkAbstractFileWriter.h b/Core/Code/IO/mitkAbstractFileWriter.h index a174d441c8..7741ab81a6 100644 --- a/Core/Code/IO/mitkAbstractFileWriter.h +++ b/Core/Code/IO/mitkAbstractFileWriter.h @@ -1,174 +1,174 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef AbstractFileWriter_H_HEADER_INCLUDED_C1E7E521 #define AbstractFileWriter_H_HEADER_INCLUDED_C1E7E521 // Macro #include // MITK #include // Microservices #include #include +#include namespace mitk { /** * @brief This abstract class gives a meaningful default implementations to most methods of mitkIFileWriter.h. * * In general, all FileWriters should derive from this class, this way it is made sure that the new implementation is * exposed to the Microservice-Framework and that is automatically available troughout MITK. * When deriving, inherit from AbstractFileWriter and at least from itkLightObject (Or any other object that derives * from itkLightObject and is more suited to your cause). The default implementation only requires both the write * methods to be implemented. Be sure to set all important members in the constructor. These are: *
    *
  • m_Extension: To define which file extension can be written. *
  • m_Description: To give a human readable name for this writer to be displayed in FileDialogs. *
  • m_BasedataType: To define which type of Basedata this fileWriter can handle. *
  • (Optional) m_Priority: To make this writer rank higher when choosing writers automatically *
  • (Optional) m_SupportedOptions: To define which options this writer can handle. Options can modify writing behaviour (e.g. set a compression) *
* You can also use the protected constructor for this. * * @ingroup Process */ class MITK_CORE_EXPORT AbstractFileWriter : public mitk::IFileWriter { public: /** * \brief Get the complete file name and path to the file that will be written. */ virtual std::string GetFileName() const; /** * \brief Set the complete file name and path to the file that will be writen. */ virtual void SetFileName(const std::string& aFileName); /** * \brief Write the data in data to the the location returned by GetFileName(). */ virtual void Write(const BaseData *data); /** * \brief Write the data in data to the the location specified in path */ virtual void Write(const BaseData* data, const std::string& path); /** * \brief Write the data in data to the the stream specified in stream */ virtual void Write(const BaseData* data, std::ostream& stream ) = 0; /** * \brief Returns the priority which defined how 'good' the FileWriter can handle it's file format. * * Default is zero and should only be chosen differently for a reason. * The priority is intended to be used by the MicroserviceFramework to determine * which reader to use if several equivalent readers have been found. * It may be used to replace a default reader from MITK in your own project. * E.g. if you want to use your own reader for *.nrrd files instead of the default, * implement it and give it a higher priority than zero. * * This method has a default implementation and need not be reimplemented, simply set m_Priority in the constructor. */ virtual int GetPriority() const; /** * \brief returns the file extension that this FileWriter is able to handle. * * Please enter only the characters after the fullstop, e.g "nrrd" is correct * while "*.nrrd" and ".nrrd" are incorrect. * * This method has a default implementation and need not be reimplemented, simply set m_Extension in the constructor. */ virtual std::string GetExtension() const; /** * \brief returns the name of the itk Basedata that this FileWriter is able to handle. * * The correct value is the one given as the first parameter in the ItkNewMacro of that Basedata derivate. * You can also retrieve it by calling GetNameOfClass() on an instance of said data. * * This method has a default implementation and need not be reimplemented, simply set m_BaseDataType in the constructor. */ virtual std::string GetSupportedBasedataType() const; /** * \brief Returns a human readable description of the file format. * * This will be used in FileDialogs for example. * This method has a default implementation and need not be reimplemented, simply set m_BaseDataType in the constructor. */ virtual std::string GetDescription() const; /** * \brief returns a list of the supported Options * * Options are strings that are treated as flags when passed to the read method. */ virtual std::list< std::string > GetSupportedOptions() const; /** * \brief Return true if this writer can confirm that it can read this file and false otherwise. * * The default implementation of AbstractFileWriter checks if the supplied filename is of the same extension as m_extension. * Overwrite this method if you require more specific behaviour */ virtual bool CanWrite(const BaseData* data, const std::string& path) const; /** * \brief Returns a value between 0 and 1 depending on the progress of the writing process. * This method need not necessarily be implemented, always returning zero is accepted. */ virtual float GetProgress() const; + us::ServiceRegistration RegisterService(us::ModuleContext* context = us::GetModuleContext()); + + void UnregisterService(); protected: AbstractFileWriter(); AbstractFileWriter(const std::string& basedataType, const std::string& extension, const std::string& description); // Filenames etc.. std::string m_FileName; //std::string m_FilePrefix; //std::string m_FilePattern; // Minimal Service Properties: ALWAYS SET THESE IN CONSTRUCTOR OF DERIVED CLASSES! std::string m_Extension; std::string m_BasedataType; std::string m_Description; int m_Priority; std::list< std::string > m_Options; // Options supported by this Writer. Can be left emtpy if no special options are required // Registration us::ServiceRegistration m_Registration; - virtual void RegisterMicroservice(us::ModuleContext* context); - - virtual void UnregisterMicroservice(us::ModuleContext* context); - virtual us::ServiceProperties ConstructServiceProperties(); }; } // namespace mitk #endif /* AbstractFileWriter_H_HEADER_INCLUDED_C1E7E521 */ diff --git a/Core/Code/IO/mitkLegacyFileReaderService.cpp b/Core/Code/IO/mitkLegacyFileReaderService.cpp index 64bb852aab..01a8489b62 100644 --- a/Core/Code/IO/mitkLegacyFileReaderService.cpp +++ b/Core/Code/IO/mitkLegacyFileReaderService.cpp @@ -1,50 +1,50 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include mitk::LegacyFileReaderService::LegacyFileReaderService(const std::string& extension, const std::string& description) : AbstractFileReader(extension, description) { - if (extension.empty()) mitkThrow() << "LegacyFileReaderWrapper cannot be initialized without FileExtension." ; - RegisterMicroservice(us::GetModuleContext()); + if (extension.empty()) mitkThrow() << "LegacyFileReaderWrapper cannot be initialized without FileExtension."; + this->RegisterService(); } mitk::LegacyFileReaderService::~LegacyFileReaderService() { } ////////////////////// Reading ///////////////////////// std::list< mitk::BaseData::Pointer > mitk::LegacyFileReaderService::Read(const std::string& path, mitk::DataStorage* /*ds*/) { std::list< mitk::BaseData::Pointer > result; result.push_front(mitk::IOUtil::LoadDataNode(path)->GetData()); return result; } std::list< mitk::BaseData::Pointer > mitk::LegacyFileReaderService::Read(const std::istream& /*stream*/, mitk::DataStorage* /*ds*/) { mitkThrow () << "Streaming is not supported in Legacy Wrappers."; std::list< mitk::BaseData::Pointer > result; return result; } diff --git a/Core/Code/IO/mitkLegacyFileWriterService.cpp b/Core/Code/IO/mitkLegacyFileWriterService.cpp index 48b04080d0..9e04539e4f 100644 --- a/Core/Code/IO/mitkLegacyFileWriterService.cpp +++ b/Core/Code/IO/mitkLegacyFileWriterService.cpp @@ -1,64 +1,64 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include mitk::LegacyFileWriterService::LegacyFileWriterService(mitk::FileWriter::Pointer legacyWriter, const std::string& basedataType, const std::string& extension, const std::string& description) : AbstractFileWriter(basedataType, extension, description) , m_LegacyWriter(legacyWriter) { if (extension.empty()) mitkThrow() << "LegacyFileWriterWrapper cannot be initialized without FileExtension." ; - RegisterMicroservice(us::GetModuleContext()); + RegisterService(); } ////////////////////// Writing ///////////////////////// void mitk::LegacyFileWriterService::Write(const BaseData* data, const std::string& path) { if (m_LegacyWriter.IsNull()) mitkThrow() << "LegacyFileWriterService was incorrectly initialized: Has no LegacyFileWriter."; m_LegacyWriter->SetFileName(path.c_str()); mitk::DataNode::Pointer node = mitk::DataNode::New(); node->SetData(const_cast(data)); m_LegacyWriter->SetInput(node); m_LegacyWriter->Write(); } void mitk::LegacyFileWriterService::Write(const BaseData* /*data*/, std::ostream& /*stream*/ ) { mitkThrow () << "Streaming is not supported in Legacy Wrappers."; //return 0; } bool mitk::LegacyFileWriterService::CanWrite(const BaseData* data, const std::string& path) const { // Default implementation only checks if extension and basedatatype are correct std::string pathEnd = path.substr( path.length() - m_Extension.length(), m_Extension.length() ); if ( !(m_Extension == pathEnd)) return false; std::string externalDataType = data->GetNameOfClass(); return (externalDataType == m_BasedataType); } diff --git a/Core/Code/Testing/mitkFileReaderManagerTest.cpp b/Core/Code/Testing/mitkFileReaderManagerTest.cpp index dd5c3c43d5..472a14f018 100644 --- a/Core/Code/Testing/mitkFileReaderManagerTest.cpp +++ b/Core/Code/Testing/mitkFileReaderManagerTest.cpp @@ -1,163 +1,164 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkTestingMacros.h" #include "mitkAbstractFileReader.h" #include "mitkIFileReader.h" #include "mitkFileReaderManager.h" #include #include #include #include #include #include #include class DummyReader : public mitk::AbstractFileReader { public: DummyReader(const std::string& extension, int priority) : mitk::AbstractFileReader(extension, "This is a dummy description") { m_Priority = priority; + this->RegisterService(); } using mitk::AbstractFileReader::Read; virtual std::list< itk::SmartPointer > Read(const std::istream& /*stream*/, mitk::DataStorage* /*ds*/ = 0) { std::list result; return result; } virtual void SetOptions(const std::list< std::string >& options ) { m_Options = options; m_Registration.SetProperties(ConstructServiceProperties()); } }; // End of internal dummy reader /** * TODO */ int mitkFileReaderManagerTest(int /*argc*/ , char* /*argv*/[]) { // always start with this! MITK_TEST_BEGIN("FileReaderManager"); // mitk::FileReaderManager::Pointer frm = mitk::FileReaderManager::New(); // MITK_TEST_CONDITION_REQUIRED(argc == 2,"Testing FileReaderManager instantiation"); DummyReader testDR("test",1); DummyReader otherDR("other",1); MITK_TEST_CONDITION_REQUIRED(testDR.CanRead("/this/is/a/folder/file.test"),"Positive test of default CanRead() implementation"); MITK_TEST_CONDITION_REQUIRED(!testDR.CanRead("/this/is/a/folder/file.tes"),"Negative test of default CanRead() implementation"); mitk::IFileReader* returned = mitk::FileReaderManager::GetReader("test"); MITK_TEST_CONDITION_REQUIRED(&static_cast(testDR) == returned,"Testing correct retrieval of FileReader 1/2"); returned = mitk::FileReaderManager::GetReader("other"); MITK_TEST_CONDITION_REQUIRED(&static_cast(otherDR) == returned,"Testing correct retrieval of FileReader 2/2"); DummyReader mediocreTestDR("test", 20); DummyReader prettyFlyTestDR("test", 50); DummyReader awesomeTestDR("test", 100); returned = mitk::FileReaderManager::GetReader("test"); MITK_TEST_CONDITION_REQUIRED(&static_cast(awesomeTestDR) == returned, "Testing correct priorized retrieval of FileReader: Best reader"); // Now to give those readers some options, then we will try again std::list options; options.push_front("isANiceGuy"); mediocreTestDR.SetOptions(options); options.clear(); options.push_front("canFly"); prettyFlyTestDR.SetOptions(options); options.push_front("isAwesome"); awesomeTestDR.SetOptions(options); //note: awesomeReader canFly and isAwesome // Reset Options, use to define what we want the reader to do options.clear(); options.push_front("canFly"); returned = mitk::FileReaderManager::GetReader("test", options); MITK_TEST_CONDITION_REQUIRED(&static_cast(awesomeTestDR) == returned, "Testing correct retrieval of FileReader with Options: Best reader with options"); options.push_front("isAwesome"); returned = mitk::FileReaderManager::GetReader("test", options); MITK_TEST_CONDITION_REQUIRED(&static_cast(awesomeTestDR) == returned, "Testing correct retrieval of FileReader with multiple Options: Best reader with options"); options.clear(); options.push_front("isANiceGuy"); returned = mitk::FileReaderManager::GetReader("test", options); MITK_TEST_CONDITION_REQUIRED(&static_cast(mediocreTestDR) == returned, "Testing correct retrieval of specific FileReader with Options: Low priority reader with specific option"); options.push_front("canFly"); returned = mitk::FileReaderManager::GetReader("test", options); MITK_TEST_CONDITION_REQUIRED(returned == 0, "Testing correct return of 0 value when no matching reader was found"); // Onward to test the retrieval of multiple readers std::vector< mitk::IFileReader* > returnedList; returnedList = mitk::FileReaderManager::GetReaders("test", options); MITK_TEST_CONDITION_REQUIRED(returnedList.size() == 0, "Testing correct return of zero readers when no matching reader was found, asking for all compatibles"); options.clear(); options.push_back("canFly"); returnedList = mitk::FileReaderManager::GetReaders("test", options); MITK_TEST_CONDITION_REQUIRED(returnedList.size() == 2, "Testing correct return of two readers when two matching reader was found, asking for all compatibles"); MITK_TEST_CONDITION_REQUIRED(returnedList.front() == &static_cast(awesomeTestDR), "Testing correct priorization of returned Readers with options 1/2"); MITK_TEST_CONDITION_REQUIRED(returnedList.back() == &static_cast(prettyFlyTestDR), "Testing correct priorization of returned Readers with options 2/2"); options.clear(); options.push_back("isAwesome"); returnedList = mitk::FileReaderManager::GetReaders("test", options); MITK_TEST_CONDITION_REQUIRED(returnedList.size() == 1, "Testing correct return of one readers when one matching reader was found, asking for all compatibles"); MITK_TEST_CONDITION_REQUIRED(returnedList.front() == &static_cast(awesomeTestDR), "Testing correctness of result from former query"); // And now to verify a working read chain for a mps file: //mitk::PointSetReader::Pointer psr = mitk::PointSetReader::New(); //std::list basedata; //basedata = mitk::FileReaderManager::Read("F://Build//MITK-Data//pointSet.mps"); //MITK_TEST_CONDITION_REQUIRED(basedata.size() > 0, "Testing correct read of PointSet"); // Need to instanciate the CoreObjectFactory, so legacy Readers are available mitk::CoreObjectFactory::GetInstance(); // Testing templated call to ReaderManager mitk::PointSet::Pointer pointset = mitk::FileReaderManager::Read< mitk::PointSet >("F://Build//MITK-Data//pointSet.mps"); MITK_TEST_CONDITION_REQUIRED(pointset.IsNotNull(), "Testing templated call of Read()"); // And now for something completely different... (Debug) // mitk::LegacyFileReaderService::Pointer lfr = mitk::LegacyFileReaderService::New(".nrrd", "Nearly Raw Raster Data"); //returned = mitk::FileReaderManager::GetReader(".nrrd"); //MITK_TEST_CONDITION_REQUIRED(lfr == returned, "Testing correct retrieval of specific FileReader with Options: Low priority reader with specific option"); std::list image = mitk::FileReaderManager::Read("F://Build//MITK-Data//Pic2DplusT.nrrd"); MITK_TEST_CONDITION_REQUIRED(image.size() > 0, "Testing whether image was returned or not"); mitk::Image::Pointer image2 = dynamic_cast (image.front().GetPointer()); MITK_TEST_CONDITION_REQUIRED(image2.IsNotNull(), "Testing if BaseData is an image"); // always end with this! MITK_TEST_END(); }