ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkXMLFile.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkXMLFile_h
19 #define itkXMLFile_h
20 #include "itkLightProcessObject.h"
21 #include "ITKIOXMLExport.h"
22 #include <fstream>
23 
24 namespace itk
25 {
34 class ITKIOXML_EXPORT XMLReaderBase:public LightProcessObject
35 {
36 public:
37  ITK_DISALLOW_COPY_AND_ASSIGN(XMLReaderBase);
38 
40 
42  itkSetStringMacro(Filename);
43 
45  itkGetStringMacro(Filename);
46 
48  virtual int CanReadFile(const char *name) = 0;
49 
51  virtual void GenerateOutputInformation();
52 
56  virtual void StartElement(const char *name, const char **atts) = 0;
57 
61  virtual void EndElement(const char *name) = 0;
62 
66  virtual void CharacterDataHandler(const char *inData, int inLength) = 0;
67 
68 protected:
69  XMLReaderBase() = default;
70  ~XMLReaderBase() override = default;
71  void PrintSelf(std::ostream & os, Indent indent) const override;
72 
77  void parse();
78 
79  std::string m_Filename;
80 };
81 
90 template< typename T >
91 class ITK_TEMPLATE_EXPORT XMLReader: public XMLReaderBase
92 {
93 public:
94  ITK_DISALLOW_COPY_AND_ASSIGN(XMLReader);
95 
96  using Self = XMLReader;
97 
101  void SetOutputObject(T *obj) { m_OutputObject = obj; }
102 
105  T * GetOutputObject() { return m_OutputObject; }
106 
107 protected:
109  m_OutputObject(nullptr)
110  {}
111 
112  ~XMLReader() override = default;
113 
115 };
116 
126 template< typename T >
127 class ITK_TEMPLATE_EXPORT XMLWriterBase:public LightProcessObject
128 {
129 public:
130  ITK_DISALLOW_COPY_AND_ASSIGN(XMLWriterBase);
131 
133 
138  {
139  m_InputObject = nullptr;
140  }
141 
143  itkSetStringMacro(Filename);
144 
146  itkGetStringMacro(Filename);
147 
149  virtual int CanWriteFile(const char *name) = 0;
150 
152  void SetObject(T *toWrite) { m_InputObject = toWrite; }
153 
155  virtual int WriteFile() = 0;
156 
157 #if !defined( ITK_WRAPPING_PARSER )
158 
159  void WriteStartElement(const char *const tag, std::ofstream & file)
160  {
161  file << '<' << tag << '>';
162  }
163 
165  void WriteEndElement(const char *const tag, std::ofstream & file)
166  {
167  file << '<' << '/' << tag << '>';
168  }
169 
171  void WriteCharacterData(const char *const data, std::ofstream & file)
172  {
173  file << data;
174  }
175 
177  void WriteStartElement(std::string & tag, std::ofstream & file)
178  {
179  WriteStartElement(tag.c_str(), file);
180  }
181 
183  void WriteEndElement(std::string & tag, std::ofstream & file)
184  {
185  WriteEndElement(tag.c_str(), file);
186  }
187 
189  void WriteCharacterData(std::string & data, std::ofstream & file)
190  {
191  WriteCharacterData(data.c_str(), file);
192  }
193 #endif
194 
195 protected:
196  T *m_InputObject; // object to write out to an XML file
197  std::string m_Filename; // name of file to write.
198 };
199 }
200 #endif
Light weight base class for most itk classes.
std::string m_Filename
Definition: itkXMLFile.h:79
T * GetOutputObject()
Definition: itkXMLFile.h:105
void SetOutputObject(T *obj)
Definition: itkXMLFile.h:101
void SetObject(T *toWrite)
Definition: itkXMLFile.h:152
template base class for an XMLReader It&#39;s purpose really is just to define the simple interface for e...
Definition: itkXMLFile.h:91
LightProcessObject is the base class for all process objects (source, filters, mappers) in the Insigh...
Control indentation during Print() invocation.
Definition: itkIndent.h:49
void WriteStartElement(const char *const tag, std::ofstream &file)
Definition: itkXMLFile.h:159