ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkXMLFile.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkXMLFile_h
00019 #define __itkXMLFile_h
00020 #include "itkLightProcessObject.h"
00021 #include "expat.h"
00022 #include <fstream>
00023 
00024 namespace itk
00025 {
00034 class ITK_EXPORT XMLReaderBase:public LightProcessObject
00035 {
00036 public:
00037   typedef XMLReaderBase Self;
00038 
00040   itkSetStringMacro(Filename);
00041 
00043   itkGetStringMacro(Filename);
00044 
00046   virtual int CanReadFile(const char *name) = 0;
00047 
00049   virtual void GenerateOutputInformation();
00050 
00054   virtual void StartElement(const char *name, const char **atts) = 0;
00055 
00059   virtual void EndElement(const char *name) = 0;
00060 
00064   virtual void CharacterDataHandler(const char *inData, int inLength) = 0;
00065 
00066 protected:
00067   XMLReaderBase() {}
00068   virtual ~XMLReaderBase() {}
00069   void PrintSelf(std::ostream & os, Indent indent) const;
00070 
00075   void parse(void);
00076 
00077   std::string m_Filename;
00078 private:
00079   XMLReaderBase(const Self &);  //purposely not implemented
00080   void operator=(const Self &); //purposely not implemented
00081 };
00082 
00091 template< class T >
00092 class ITK_EXPORT XMLReader: public XMLReaderBase
00093 {
00094 public:
00095   typedef XMLReader Self;
00096 
00100   void SetOutputObject(T *obj) { m_OutputObject = obj; }
00101 
00104   T * GetOutputObject(void) { return m_OutputObject; }
00105 protected:
00106   XMLReader() {}
00107   virtual ~XMLReader() {}
00109 
00110   T *m_OutputObject;
00111 private:
00112   XMLReader(const Self &);      //purposely not implemented
00113   void operator=(const Self &); //purposely not implemented
00114 };
00115 
00125 template< class T >
00126 class ITK_EXPORT XMLWriterBase:public LightProcessObject
00127 {
00128 public:
00129   typedef XMLWriterBase Self;
00130 
00134   XMLWriterBase()
00135   {
00136     m_InputObject = 0;
00137   }
00138 
00140   itkSetStringMacro(Filename);
00141 
00143   itkGetStringMacro(Filename);
00144 
00146   virtual int CanWriteFile(const char *name) = 0;
00147 
00149   void SetObject(T *toWrite) { m_InputObject = toWrite; }
00150 
00152   virtual int WriteFile() = 0;
00153 
00155   void WriteStartElement(const char *const tag, std::ofstream & file)
00156   {
00157     file << '<' << tag << '>';
00158   }
00159 
00161   void WriteEndElement(const char *const tag, std::ofstream & file)
00162   {
00163     file << '<' << '/'  << tag << '>';
00164   }
00165 
00167   void WriteCharacterData(const char *const data, std::ofstream & file)
00168   {
00169     file << data;
00170   }
00171 
00173   void WriteStartElement(std::string & tag, std::ofstream & file)
00174   {
00175     WriteStartElement(tag.c_str(), file);
00176   }
00177 
00179   void WriteEndElement(std::string & tag, std::ofstream & file)
00180   {
00181     WriteEndElement(tag.c_str(), file);
00182   }
00183 
00185   void WriteCharacterData(std::string & data, std::ofstream & file)
00186   {
00187     WriteCharacterData(data.c_str(), file);
00188   }
00189 
00190 protected:
00191   T          *m_InputObject;    // object to write out to an XML file
00192   std::string m_Filename;       // name of file to write.
00193 private:
00194   XMLWriterBase(const Self &);  //purposely not implemented
00195   void operator=(const Self &); //purposely not implemented
00196 };
00197 }
00198 #endif
00199