MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkNavigationToolStorageDeserializer.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found 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
41 Poco::File myFile(m_tempDirectory);
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
52mitk::NavigationToolStorage::Pointer mitk::NavigationToolStorageDeserializer::Deserialize(std::string filename)
53 {
54 //decompress zip file into temporary directory
55 decompressFiles(filename,m_tempDirectory);
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{
82std::string s;
83std::stringstream out;
84out << i;
85s = out.str();
86return s;
87}
88
89void 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)";
108 mitkThrowException(mitk::IGTException) << m_ErrorMessage;
109 }
110
111 }
An object of this class represents an exception of the MITK-IGT module.
NavigationToolStorageDeserializer(mitk::DataStorage::Pointer dataStorage)
mitk::NavigationToolStorage::Pointer Deserialize(std::string filename)
Loads a collection of navigation tools represented by a mitk::NavigationToolStorage from a file.
void decompressFiles(std::string file, std::string path)