ITK  5.4.0
Insight Toolkit
itkXMLFile.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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  * https://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 {
35 class ITKIOXML_EXPORT XMLReaderBase : public LightProcessObject
36 {
37 public:
38  ITK_DISALLOW_COPY_AND_MOVE(XMLReaderBase);
39 
41 
43  itkSetStringMacro(Filename);
44 
46  itkGetStringMacro(Filename);
47 
49  virtual int
50  CanReadFile(const char * name) = 0;
51 
53  virtual void
54  GenerateOutputInformation();
55 
59  virtual void
60  StartElement(const char * name, const char ** atts) = 0;
61 
65  virtual void
66  EndElement(const char * name) = 0;
67 
71  virtual void
72  CharacterDataHandler(const char * inData, int inLength) = 0;
73 
74 protected:
75  XMLReaderBase() = default;
76  ~XMLReaderBase() override = default;
77  void
78  PrintSelf(std::ostream & os, Indent indent) const override;
79 
84  void
85  parse();
86 
87  std::string m_Filename{};
88 };
89 
99 template <typename T>
100 class ITK_TEMPLATE_EXPORT XMLReader : public XMLReaderBase
101 {
102 public:
103  ITK_DISALLOW_COPY_AND_MOVE(XMLReader);
104 
105  using Self = XMLReader;
106 
110  void
112  {
113  m_OutputObject = obj;
114  }
115 
118  T *
120  {
121  return m_OutputObject;
122  }
123 
124 protected:
126  : m_OutputObject(nullptr)
127  {}
128 
129  ~XMLReader() override = default;
130 
131  T * m_OutputObject{};
132 };
133 
144 template <typename T>
145 class ITK_TEMPLATE_EXPORT XMLWriterBase : public LightProcessObject
146 {
147 public:
148  ITK_DISALLOW_COPY_AND_MOVE(XMLWriterBase);
149 
151 
155  XMLWriterBase() { m_InputObject = nullptr; }
156 
158  itkSetStringMacro(Filename);
159 
161  itkGetStringMacro(Filename);
162 
164  virtual int
165  CanWriteFile(const char * name) = 0;
166 
168  void
169  SetObject(T * toWrite)
170  {
171  m_InputObject = toWrite;
172  }
173 
175  virtual int
176  WriteFile() = 0;
177 
178 #if !defined(ITK_WRAPPING_PARSER)
179 
180  void
181  WriteStartElement(const char * const tag, std::ofstream & file)
182  {
183  file << '<' << tag << '>';
184  }
185 
187  void
188  WriteEndElement(const char * const tag, std::ofstream & file)
189  {
190  file << '<' << '/' << tag << '>';
191  }
192 
194  void
195  WriteCharacterData(const char * const data, std::ofstream & file)
196  {
197  file << data;
198  }
199 
201  void
202  WriteStartElement(std::string & tag, std::ofstream & file)
203  {
204  WriteStartElement(tag.c_str(), file);
205  }
206 
208  void
209  WriteEndElement(std::string & tag, std::ofstream & file)
210  {
211  WriteEndElement(tag.c_str(), file);
212  }
213 
215  void
216  WriteCharacterData(std::string & data, std::ofstream & file)
217  {
218  WriteCharacterData(data.c_str(), file);
219  }
220 #endif
221 
222 protected:
223  T * m_InputObject{}; // object to write out to an XML file
224  std::string m_Filename{}; // name of file to write.
225 };
226 } // namespace itk
227 #endif
itk::XMLReader::XMLReader
XMLReader()
Definition: itkXMLFile.h:125
itk::LightProcessObject
LightProcessObject is the base class for all process objects (source, filters, mappers) in the Insigh...
Definition: itkLightProcessObject.h:72
itkLightProcessObject.h
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::XMLReaderBase
Definition: itkXMLFile.h:35
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::XMLWriterBase
Definition: itkXMLFile.h:145
itk::XMLWriterBase::WriteStartElement
void WriteStartElement(const char *const tag, std::ofstream &file)
Definition: itkXMLFile.h:181
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::XMLReader::GetOutputObject
T * GetOutputObject()
Definition: itkXMLFile.h:119
itk::XMLWriterBase::XMLWriterBase
XMLWriterBase()
Definition: itkXMLFile.h:155
itk::XMLWriterBase::SetObject
void SetObject(T *toWrite)
Definition: itkXMLFile.h:169
itk::XMLReader::SetOutputObject
void SetOutputObject(T *obj)
Definition: itkXMLFile.h:111
itk::XMLReader
template base class for an XMLReader Its purpose really is just to define the simple interface for ex...
Definition: itkXMLFile.h:100