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