Medical Imaging Interaction Toolkit  2018.4.99-87d68d9f
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 (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 
14 
15 // itk includes
16 #include "itksys/SystemTools.hxx"
17 
18 // mitk includes
19 #include "mitkExampleIOMimeTypes.h"
20 
22  : AbstractFileWriter(mitk::ExampleDataStructure::GetStaticNameOfClass(),
23  CustomMimeType(mitk::ExampleIOMimeTypes::EXAMPLE_MIMETYPE()),
24  "Default writer for the example data structure")
25 {
27 }
28 
31  : AbstractFileWriter(other)
32 {
33 }
34 
36 {
37 }
38 
40 {
41  return new ExampleDataStructureWriterService(*this);
42 }
43 
45 {
46  MITK_INFO << "Writing ExampleDataStructure";
47  InputType::ConstPointer input = dynamic_cast<const InputType *>(this->GetInput());
48  if (input.IsNull())
49  {
50  MITK_ERROR << "Sorry, input to ExampleDataStructureWriterService is nullptr!";
51  return;
52  }
53  if (this->GetOutputLocation().empty())
54  {
55  MITK_ERROR << "Sorry, filename has not been set!";
56  return;
57  }
58 
59  std::string ext = itksys::SystemTools::GetFilenameLastExtension(this->GetOutputLocation());
60  ext = itksys::SystemTools::LowerCase(ext);
61 
62  // default extension is .txt
63  if (ext == "")
64  {
65  ext = ".txt";
66  this->SetOutputLocation(this->GetOutputLocation() + ext);
67  }
68 
69  try
70  {
71  std::ofstream file(this->GetOutputLocation());
72 
73  if (file.is_open())
74  {
75  file << input->GetData();
76  }
77  else
78  {
79  mitkThrow() << "Could not open file " << this->GetOutputLocation() << " for writing.";
80  }
81 
82  MITK_INFO << "Example Data Structure has been written";
83  }
84 
85  catch (const mitk::Exception& e)
86  {
87  MITK_ERROR << e.GetDescription();
88  }
89  catch (...)
90  {
91  MITK_ERROR << "Unknown error occurred while trying to write file.";
92  }
93 }
void Write() override
Write the base data to the specified location or output stream.
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_ERROR
Definition: mitkLogMacros.h:20
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&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
#define mitkThrow()
const BaseData * GetInput() const override
Get the input data set via SetInput().
void SetOutputLocation(const std::string &location) override
Set the output location.
mitk::ExampleDataStructureWriterService * Clone() const override
us::ServiceRegistration< IFileWriter > RegisterService(us::ModuleContext *context=us::GetModuleContext())
std::string GetOutputLocation() const override
Get the current output location.
Base class for writing mitk::BaseData objects to files or streams.