ITK  4.13.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:
38 
40  itkSetStringMacro(Filename);
41 
43  itkGetStringMacro(Filename);
44 
46  virtual int CanReadFile(const char *name) = 0;
47 
49  virtual void GenerateOutputInformation();
50 
54  virtual void StartElement(const char *name, const char **atts) = 0;
55 
59  virtual void EndElement(const char *name) = 0;
60 
64  virtual void CharacterDataHandler(const char *inData, int inLength) = 0;
65 
66 protected:
68  virtual ~XMLReaderBase() ITK_OVERRIDE {}
69  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
70 
75  void parse();
76 
77  std::string m_Filename;
78 
79 private:
80  ITK_DISALLOW_COPY_AND_ASSIGN(XMLReaderBase);
81 };
82 
91 template< typename T >
92 class XMLReader: public XMLReaderBase
93 {
94 public:
95  typedef XMLReader Self;
96 
100  void SetOutputObject(T *obj) { m_OutputObject = obj; }
101 
104  T * GetOutputObject(void) { return m_OutputObject; }
105 
106 protected:
108  m_OutputObject(ITK_NULLPTR)
109  {}
110 
111  virtual ~XMLReader() {}
112 
114 
115 private:
116  ITK_DISALLOW_COPY_AND_ASSIGN(XMLReader);
117 };
118 
128 template< typename T >
130 {
131 public:
133 
138  {
139  m_InputObject = ITK_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 
158  void WriteStartElement(const char *const tag, std::ofstream & file)
159  {
160  file << '<' << tag << '>';
161  }
162 
164  void WriteEndElement(const char *const tag, std::ofstream & file)
165  {
166  file << '<' << '/' << tag << '>';
167  }
168 
170  void WriteCharacterData(const char *const data, std::ofstream & file)
171  {
172  file << data;
173  }
174 
176  void WriteStartElement(std::string & tag, std::ofstream & file)
177  {
178  WriteStartElement(tag.c_str(), file);
179  }
180 
182  void WriteEndElement(std::string & tag, std::ofstream & file)
183  {
184  WriteEndElement(tag.c_str(), file);
185  }
186 
188  void WriteCharacterData(std::string & data, std::ofstream & file)
189  {
190  WriteCharacterData(data.c_str(), file);
191  }
192 
193 protected:
194  T *m_InputObject; // object to write out to an XML file
195  std::string m_Filename; // name of file to write.
196 
197 private:
198  ITK_DISALLOW_COPY_AND_ASSIGN(XMLWriterBase);
199 };
200 }
201 #endif
T * GetOutputObject(void)
Definition: itkXMLFile.h:104
virtual int WriteFile()=0
std::string m_Filename
Definition: itkXMLFile.h:77
XMLReaderBase Self
Definition: itkXMLFile.h:37
void SetOutputObject(T *obj)
Definition: itkXMLFile.h:100
virtual int CanWriteFile(const char *name)=0
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:92
LightProcessObject is the base class for all process objects (source, filters, mappers) in the Insigh...
XMLWriterBase Self
Definition: itkXMLFile.h:132
Control indentation during Print() invocation.
Definition: itkIndent.h:49
void WriteStartElement(const char *const tag, std::ofstream &file)
Definition: itkXMLFile.h:158
virtual ~XMLReaderBase() override
Definition: itkXMLFile.h:68
XMLReader Self
Definition: itkXMLFile.h:95
virtual ~XMLReader()
Definition: itkXMLFile.h:111