Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkExampleDataStructureWriterService.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 
18 
19 // itk includes
20 #include "itksys/SystemTools.hxx"
21 
22 // mitk includes
23 #include "mitkExampleIOMimeTypes.h"
24 
27  CustomMimeType(mitk::ExampleIOMimeTypes::EXAMPLE_MIMETYPE()),
28  "Default writer for the example data structure")
29 {
31 }
32 
35  : AbstractFileWriter(other)
36 {
37 }
38 
40 {
41 }
42 
44 {
45  return new ExampleDataStructureWriterService(*this);
46 }
47 
49 {
50  MITK_INFO << "Writing ExampleDataStructure";
51  InputType::ConstPointer input = dynamic_cast<const InputType *>(this->GetInput());
52  if (input.IsNull())
53  {
54  MITK_ERROR << "Sorry, input to ExampleDataStructureWriterService is NULL!";
55  return;
56  }
57  if (this->GetOutputLocation().empty())
58  {
59  MITK_ERROR << "Sorry, filename has not been set!";
60  return;
61  }
62 
63  std::string ext = itksys::SystemTools::GetFilenameLastExtension(this->GetOutputLocation());
64  ext = itksys::SystemTools::LowerCase(ext);
65 
66  // default extension is .txt
67  if (ext == "")
68  {
69  ext = ".txt";
70  this->SetOutputLocation(this->GetOutputLocation() + ext);
71  }
72 
73  try
74  {
75  std::ofstream file(this->GetOutputLocation());
76 
77  if (file.is_open())
78  {
79  file << input->GetData();
80  }
81  else
82  {
83  mitkThrow() << "Could not open file " << this->GetOutputLocation() << " for writing.";
84  }
85 
86  MITK_INFO << "Example Data Structure has been written";
87  }
88 
89  catch (mitk::Exception e)
90  {
91  MITK_ERROR << e.GetDescription();
92  }
93  catch (...)
94  {
95  MITK_ERROR << "Unknown error occurred while trying to write file.";
96  }
97 }
virtual void Write() override
Write the base data to the specified location or output stream.
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
DataCollection - Class to facilitate loading/accessing structured data.
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
#define mitkThrow()
static const char * GetStaticNameOfClass()
virtual mitk::ExampleDataStructureWriterService * Clone() const override
us::ServiceRegistration< IFileWriter > RegisterService(us::ModuleContext *context=us::GetModuleContext())
Base class for writing mitk::BaseData objects to files or streams.