Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 //Poco headers
18 #include "Poco/Zip/Decompress.h"
19 #include "Poco/Path.h"
20 #include "Poco/File.h"
21 
23 #include <mitkSceneIO.h>
24 #include <mitkIOUtil.h>
26 
27 //POCO
28 #include <Poco/Exception.h>
29 
30 
31 #include "mitkIGTException.h"
32 #include "mitkIGTIOException.h"
33 
34 
36  {
37  m_DataStorage = dataStorage;
38  //create temp directory for this reader
39  m_tempDirectory = mitk::IOUtil::CreateTemporaryDirectory("NavigationToolStorageDeserializerTmp_XXXXXX",mitk::IOUtil::GetTempPath());
40  }
41 
43  {
44  //remove temp directory
45  Poco::File myFile(m_tempDirectory);
46  try
47  {
48  if (myFile.exists()) myFile.remove();
49  }
50  catch(...)
51  {
52  MITK_ERROR << "Can't remove temp directory " << m_tempDirectory << "!";
53  }
54  }
55 
57  {
58  //decomress zip file into temporary directory
59  decomressFiles(filename,m_tempDirectory);
60 
61  //now read all files and convert them to navigation tools
63  bool cont = true;
64  int i;
65  for (i=0; cont==true; i++)
66  {
67  std::string fileName = m_tempDirectory + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool";
69  mitk::NavigationTool::Pointer readTool = myReader->DoRead(fileName);
70  if (readTool.IsNull()) cont = false;
71  else returnValue->AddTool(readTool);
72  //delete file
73  std::remove(fileName.c_str());
74  }
75  if(i==1)
76  {
77  //throw an exception here in case of not finding any tool
78  m_ErrorMessage = "Error: did not find any tool. \n Is this a tool storage file?";
79  mitkThrowException(mitk::IGTException)<<"Error: did not find any tool. \n Is this a tool storage file?";
80  }
81  return returnValue;
82  }
83 
85 {
86 std::string s;
87 std::stringstream out;
88 out << i;
89 s = out.str();
90 return s;
91 }
92 
94 {
95  std::ifstream file( filename.c_str(), std::ios::binary );
96  if (!file.good())
97  {
98  m_ErrorMessage = "Cannot open '" + filename + "' for reading";
99  mitkThrowException(mitk::IGTException)<<"Cannot open"+filename+" for reading";
100  }
101 
102  try
103  {
104  Poco::Zip::Decompress unzipper( file, Poco::Path( path ) );
105  unzipper.decompressAllFiles();
106  file.close();
107  }
108 
109  catch(Poco::IllegalStateException e) //temporary solution: replace this by defined exception handling later!
110  {
111  m_ErrorMessage = "Error: wrong file format! \n (please only load tool storage files)";
112  MITK_ERROR << m_ErrorMessage;
113  mitkThrowException(mitk::IGTException) << m_ErrorMessage;
114  }
115 
116  }
NavigationToolStorageDeserializer(mitk::DataStorage::Pointer dataStorage)
itk::SmartPointer< Self > Pointer
static std::string GetTempPath()
Definition: mitkIOUtil.cpp:372
#define MITK_ERROR
Definition: mitkLogMacros.h:24
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...
mitk::DataStorage::Pointer m_DataStorage
void decomressFiles(std::string file, std::string path)
static const std::string filename
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:452
#define mitkThrowException(classname)