Medical Imaging Interaction Toolkit  2018.4.99-67d34b5d
Medical Imaging Interaction Toolkit
mitkGeometryDataWriterService.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 #include "mitkIOMimeTypes.h"
15 
17 
18 #include <tinyxml.h>
19 
20 #include <mitkLocaleSwitch.h>
21 
24  GeometryData::GetStaticNameOfClass(), IOMimeTypes::GEOMETRY_DATA_MIMETYPE(), "MITK Geometry Data Writer")
25 {
27 }
28 
30  : AbstractFileWriter(other)
31 {
32 }
33 
35 {
36 }
37 
39 {
40  /* using the stream interface produces files without line breaks or indentation..
41  But before changing to file interface, need to understand the new I/O classes */
42  OutputStream out(this);
43 
44  if (!out.good())
45  {
46  mitkThrow() << "Stream not good.";
47  }
48 
49  // Switch the current locale to "C"
50  LocaleSwitch localeSwitch("C");
51 
52  // Open XML file using TinyXML,
53  // loop over all inputs,
54  // call appropriate serializing functions
55 
56  TiXmlDocument doc;
57 
58  auto *decl = new TiXmlDeclaration(
59  "1.0", "UTF-8", ""); // TODO what to write here? encoding? standalone would mean that we provide a DTD somewhere...
60  doc.LinkEndChild(decl);
61 
62  auto *rootNode = new TiXmlElement("GeometryData");
63  doc.LinkEndChild(rootNode);
64 
65  // note version info
66  auto *version = new TiXmlElement("Version");
67  version->SetAttribute("Writer", __FILE__);
68  version->SetAttribute("FileVersion", 1);
69  rootNode->LinkEndChild(version);
70 
71  const auto *data = static_cast<const GeometryData *>(this->GetInput());
72 
73  const ProportionalTimeGeometry *timeGeometry(nullptr);
74  if ((timeGeometry = dynamic_cast<const ProportionalTimeGeometry *>(data->GetTimeGeometry())))
75  {
76  TiXmlElement *timeGeometryElement = ProportionalTimeGeometryToXML::ToXML(timeGeometry);
77  rootNode->LinkEndChild(timeGeometryElement);
78  }
79  else
80  {
81  MITK_WARN << "Serializing GeometryData that does not have a valid ProportionalTimeGeometry! Not implemented!";
82  }
83 
84  // Write out document
85  out << doc;
86 }
87 
88 mitk::GeometryDataWriterService *mitk::GeometryDataWriterService::Clone() const
89 {
90  return new GeometryDataWriterService(*this);
91 }
AbstractFileWriter(const AbstractFileWriter &other)
The IOMimeTypes class.
static TiXmlElement * ToXML(const ProportionalTimeGeometry *geometry)
Serialize given geometry to XML.
#define MITK_WARN
Definition: mitkLogMacros.h:19
Convenience class to temporarily change the current locale.
#define mitkThrow()
const BaseData * GetInput() const override
Get the input data set via SetInput().
Data class only having a BaseGeometry but not containing any specific data.
void Write() override
Write the base data to the specified location or output stream.
us::ServiceRegistration< IFileWriter > RegisterService(us::ModuleContext *context=us::GetModuleContext())
Base class for writing mitk::BaseData objects to files or streams.