diff --git a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageDeserializer.cpp b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageDeserializer.cpp index 1f5da4113f..c4e308ca04 100644 --- a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageDeserializer.cpp +++ b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageDeserializer.cpp @@ -1,116 +1,131 @@ /*=================================================================== 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. ===================================================================*/ //Poco headers #include "Poco/Zip/Decompress.h" #include "Poco/Path.h" #include "Poco/File.h" #include "mitkNavigationToolStorageDeserializer.h" #include #include #include "mitkNavigationToolReader.h" //POCO #include + +#include "mitkIGTException.h" +#include "mitkIGTIOException.h" + + mitk::NavigationToolStorageDeserializer::NavigationToolStorageDeserializer(mitk::DataStorage::Pointer dataStorage) { m_DataStorage = dataStorage; //create temp directory for this reader m_tempDirectory = mitk::StandardFileLocations::GetInstance()->GetOptionDirectory() + Poco::Path::separator() + "tempNavigationToolDeserializer"; Poco::File myFile(m_tempDirectory); myFile.createDirectory(); } mitk::NavigationToolStorageDeserializer::~NavigationToolStorageDeserializer() { //remove temp directory Poco::File myFile(m_tempDirectory); try { if (myFile.exists()) myFile.remove(); } catch(...) { MITK_ERROR << "Can't remove temp directory " << m_tempDirectory << "!"; } } mitk::NavigationToolStorage::Pointer mitk::NavigationToolStorageDeserializer::Deserialize(std::string filename) { bool success = false; //decomress zip file into temporary directory success = decomressFiles(filename,m_tempDirectory); //currently returns an empty storage in case of an error. TODO when exception handling is availiable in MITK: Throw an exception? - if (!success) {return mitk::NavigationToolStorage::New();} + if (!success) + { + //Exception if file cannot be decompressed + mitkThrowException(mitk::IGTException)<<"File has not been decopreseed"; + return mitk::NavigationToolStorage::New(); + } //now read all files and convert them to navigation tools mitk::NavigationToolStorage::Pointer returnValue = mitk::NavigationToolStorage::New(m_DataStorage); bool cont = true; int i; for (i=0; cont==true; i++) { std::string fileName = m_tempDirectory + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool"; mitk::NavigationToolReader::Pointer myReader = mitk::NavigationToolReader::New(); mitk::NavigationTool::Pointer readTool = myReader->DoRead(fileName); if (readTool.IsNull()) cont = false; else returnValue->AddTool(readTool); - //delete file + //delete file std::remove(fileName.c_str()); } if(i==1) - { + { + //throw an exception here in case of not finding the tool + mitkThrowException(mitk::IGTException)<<"Error: did not find any tool. \n Is this a tool storage file?"; m_ErrorMessage = "Error: did not find any tool. \n Is this a tool storage file?"; - MITK_ERROR << "Error: did not find any tool. Is this a tool storage file?"; } return returnValue; } std::string mitk::NavigationToolStorageDeserializer::convertIntToString(int i) - { - std::string s; - std::stringstream out; - out << i; - s = out.str(); - return s; - } +{ +std::string s; +std::stringstream out; +out << i; +s = out.str(); +return s; +} bool mitk::NavigationToolStorageDeserializer::decomressFiles(std::string filename,std::string path) - { +{ std::ifstream file( filename.c_str(), std::ios::binary ); if (!file.good()) - { + { + //throw an exception + mitkThrowException(mitk::IGTException)<<"Cannot open"+filename+" for reading"; m_ErrorMessage = "Cannot open '" + filename + "' for reading"; - return false; + return false; } try - { - Poco::Zip::Decompress unzipper( file, Poco::Path( path ) ); + { + Poco::Zip::Decompress unzipper( file, Poco::Path( path ) ); unzipper.decompressAllFiles(); file.close(); } + catch(Poco::IllegalStateException e) //temporary solution: replace this by defined exception handling later! - { - m_ErrorMessage = "Error: wrong file format! \n (please only load tool storage files)"; - MITK_ERROR << "Error: wrong file format! (please only load tool storage files)"; + { + //std::String message<<"Error: wrong file format! (please only load tool storage files)"; + m_ErrorMessage = "Error: wrong file format! \n (please only load tool storage files)"; + MITK_ERROR << "Error: wrong file format! (please only load tool storage files)"; return false; } return true; } diff --git a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageDeserializer.h b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageDeserializer.h index 0dfb1e4a07..f768d8d8f3 100644 --- a/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageDeserializer.h +++ b/Modules/IGT/IGTToolManagement/mitkNavigationToolStorageDeserializer.h @@ -1,68 +1,71 @@ /*=================================================================== 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 NAVIGATIONTOOLSTORAGEDESERIALIZER_H_INCLUDED #define NAVIGATIONTOOLSTORAGEDESERIALIZER_H_INCLUDED //itk headers #include //mitk headers #include #include #include "mitkNavigationToolStorage.h" #include namespace mitk { /**Documentation * \brief This class offers methods to load an object of the class NavigationToolStorage * from the harddisc. * * \ingroup IGT */ class MitkIGT_EXPORT NavigationToolStorageDeserializer : public itk::Object { public: mitkClassMacro(NavigationToolStorageDeserializer,itk::Object); mitkNewMacro1Param(Self,mitk::DataStorage::Pointer); /** * @brief Loads a collection of navigation tools represented by a mitk::NavigationToolStorage * from a file. * @return Returns the storage which was loaded or an empty storage if there was an error in the loading process. - * + * @throws Throws an Exception if File has not been decopreseed + * @throws Throws an Exception if no tool is found */ mitk::NavigationToolStorage::Pointer Deserialize(std::string filename); itkGetMacro(ErrorMessage,std::string); protected: NavigationToolStorageDeserializer(mitk::DataStorage::Pointer dataStorage); ~NavigationToolStorageDeserializer(); std::string m_ErrorMessage; mitk::DataStorage::Pointer m_DataStorage; std::string m_tempDirectory; std::string convertIntToString(int i); + //@throws Throws an Exception if particular file cannot be opened for reading + bool decomressFiles(std::string file,std::string path); }; } // namespace mitk #endif //NAVIGATIONTOOLSTORAGEDESERIALIZER