Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkNavigationToolStorageDeserializer.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 //Poco headers
14 #include "Poco/Zip/Decompress.h"
15 #include "Poco/Path.h"
16 #include "Poco/File.h"
17 
19 #include <mitkSceneIO.h>
20 #include <mitkIOUtil.h>
22 
23 //POCO
24 #include <Poco/Exception.h>
25 
26 
27 #include "mitkIGTException.h"
28 #include "mitkIGTIOException.h"
29 
30 
32  {
33  m_DataStorage = dataStorage;
34  //create temp directory for this reader
35  m_tempDirectory = mitk::IOUtil::CreateTemporaryDirectory("NavigationToolStorageDeserializerTmp_XXXXXX",mitk::IOUtil::GetTempPath());
36  }
37 
39  {
40  //remove temp directory
42  try
43  {
44  if (myFile.exists()) myFile.remove();
45  }
46  catch(...)
47  {
48  MITK_ERROR << "Can't remove temp directory " << m_tempDirectory << "!";
49  }
50  }
51 
52 mitk::NavigationToolStorage::Pointer mitk::NavigationToolStorageDeserializer::Deserialize(std::string filename)
53  {
54  //decompress zip file into temporary directory
56 
57  //now read all files and convert them to navigation tools
58  mitk::NavigationToolStorage::Pointer returnValue = mitk::NavigationToolStorage::New(m_DataStorage);
59  bool cont = true;
60  int i;
61  for (i=0; cont==true; i++)
62  {
63  std::string fileName = m_tempDirectory + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool";
64  mitk::NavigationToolReader::Pointer myReader = mitk::NavigationToolReader::New();
65  mitk::NavigationTool::Pointer readTool = myReader->DoRead(fileName);
66  if (readTool.IsNull()) cont = false;
67  else returnValue->AddTool(readTool);
68  //delete file
69  std::remove(fileName.c_str());
70  }
71  if(i==1)
72  {
73  //throw an exception here in case of not finding any tool
74  m_ErrorMessage = "Error: did not find any tool. \n Is this a tool storage file?";
75  mitkThrowException(mitk::IGTException)<<"Error: did not find any tool. \n Is this a tool storage file?";
76  }
77  return returnValue;
78  }
79 
81 {
82 std::string s;
83 std::stringstream out;
84 out << i;
85 s = out.str();
86 return s;
87 }
88 
89 void mitk::NavigationToolStorageDeserializer::decompressFiles(std::string filename,std::string path)
90 {
91  std::ifstream file( filename.c_str(), std::ios::binary );
92  if (!file.good())
93  {
94  m_ErrorMessage = "Cannot open '" + filename + "' for reading";
95  mitkThrowException(mitk::IGTException)<<"Cannot open"+filename+" for reading";
96  }
97 
98  try
99  {
100  Poco::Zip::Decompress unzipper( file, Poco::Path( path ) );
101  unzipper.decompressAllFiles();
102  file.close();
103  }
104 
105  catch(const Poco::IllegalStateException&) //temporary solution: replace this by defined exception handling later!
106  {
107  m_ErrorMessage = "Error: wrong file format! \n (please only load tool storage files)";
109  }
110 
111  }
NavigationToolStorageDeserializer(mitk::DataStorage::Pointer dataStorage)
static std::string GetTempPath()
Definition: mitkIOUtil.cpp:378
#define MITK_ERROR
Definition: mitkLogMacros.h:20
An object of this class represents an exception of the MITK-IGT module.
mitk::NavigationToolStorage::Pointer Deserialize(std::string filename)
Loads a collection of navigation tools represented by a mitk::NavigationToolStorage from a file...
static Pointer New()
Constructs a NavigationToolStorage without reference to a DataStorage. The Data Nodes of tools have t...
static std::string CreateTemporaryDirectory(const std::string &templateName="XXXXXX", std::string path=std::string())
Definition: mitkIOUtil.cpp:458
#define mitkThrowException(classname)
void decompressFiles(std::string file, std::string path)