ITK  4.3.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 "expat.h"
22 #include <fstream>
23 
24 namespace itk
25 {
34 class ITK_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() {}
69  void PrintSelf(std::ostream & os, Indent indent) const;
70 
75  void parse(void);
76 
77  std::string m_Filename;
78 
79 private:
80  XMLReaderBase(const Self &); //purposely not implemented
81  void operator=(const Self &); //purposely not implemented
82 };
83 
92 template< class T >
93 class ITK_EXPORT XMLReader: public XMLReaderBase
94 {
95 public:
96  typedef XMLReader Self;
97 
101  void SetOutputObject(T *obj) { m_OutputObject = obj; }
102 
105  T * GetOutputObject(void) { return m_OutputObject; }
106 
107 protected:
109  virtual ~XMLReader() {}
110 
112 
113 private:
114  XMLReader(const Self &); //purposely not implemented
115  void operator=(const Self &); //purposely not implemented
116 };
117 
127 template< class T >
128 class ITK_EXPORT XMLWriterBase:public LightProcessObject
129 {
130 public:
132 
137  {
138  m_InputObject = 0;
139  }
140 
142  itkSetStringMacro(Filename);
143 
145  itkGetStringMacro(Filename);
146 
148  virtual int CanWriteFile(const char *name) = 0;
149 
151  void SetObject(T *toWrite) { m_InputObject = toWrite; }
152 
154  virtual int WriteFile() = 0;
155 
157  void WriteStartElement(const char *const tag, std::ofstream & file)
158  {
159  file << '<' << tag << '>';
160  }
161 
163  void WriteEndElement(const char *const tag, std::ofstream & file)
164  {
165  file << '<' << '/' << tag << '>';
166  }
167 
169  void WriteCharacterData(const char *const data, std::ofstream & file)
170  {
171  file << data;
172  }
173 
175  void WriteStartElement(std::string & tag, std::ofstream & file)
176  {
177  WriteStartElement(tag.c_str(), file);
178  }
179 
181  void WriteEndElement(std::string & tag, std::ofstream & file)
182  {
183  WriteEndElement(tag.c_str(), file);
184  }
185 
187  void WriteCharacterData(std::string & data, std::ofstream & file)
188  {
189  WriteCharacterData(data.c_str(), file);
190  }
191 
192 protected:
193  T *m_InputObject; // object to write out to an XML file
194  std::string m_Filename; // name of file to write.
195 
196 private:
197  XMLWriterBase(const Self &); //purposely not implemented
198  void operator=(const Self &); //purposely not implemented
199 };
200 }
201 #endif
202