ITK  4.2.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 private:
79  XMLReaderBase(const Self &); //purposely not implemented
80  void operator=(const Self &); //purposely not implemented
81 };
82 
91 template< class T >
92 class ITK_EXPORT 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 protected:
107  virtual ~XMLReader() {}
109 
111 private:
112  XMLReader(const Self &); //purposely not implemented
113  void operator=(const Self &); //purposely not implemented
114 };
115 
125 template< class T >
126 class ITK_EXPORT XMLWriterBase:public LightProcessObject
127 {
128 public:
130 
135  {
136  m_InputObject = 0;
137  }
138 
140  itkSetStringMacro(Filename);
141 
143  itkGetStringMacro(Filename);
144 
146  virtual int CanWriteFile(const char *name) = 0;
147 
149  void SetObject(T *toWrite) { m_InputObject = toWrite; }
150 
152  virtual int WriteFile() = 0;
153 
155  void WriteStartElement(const char *const tag, std::ofstream & file)
156  {
157  file << '<' << tag << '>';
158  }
159 
161  void WriteEndElement(const char *const tag, std::ofstream & file)
162  {
163  file << '<' << '/' << tag << '>';
164  }
165 
167  void WriteCharacterData(const char *const data, std::ofstream & file)
168  {
169  file << data;
170  }
171 
173  void WriteStartElement(std::string & tag, std::ofstream & file)
174  {
175  WriteStartElement(tag.c_str(), file);
176  }
177 
179  void WriteEndElement(std::string & tag, std::ofstream & file)
180  {
181  WriteEndElement(tag.c_str(), file);
182  }
183 
185  void WriteCharacterData(std::string & data, std::ofstream & file)
186  {
187  WriteCharacterData(data.c_str(), file);
188  }
189 
190 protected:
191  T *m_InputObject; // object to write out to an XML file
192  std::string m_Filename; // name of file to write.
193 private:
194  XMLWriterBase(const Self &); //purposely not implemented
195  void operator=(const Self &); //purposely not implemented
196 };
197 }
198 #endif
199