VTK
dox/IO/vtkXMLParser.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkXMLParser.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00028 #ifndef __vtkXMLParser_h
00029 #define __vtkXMLParser_h
00030 
00031 #include "vtkObject.h"
00032 
00033 extern "C"
00034 {
00035   void vtkXMLParserStartElement(void*, const char*, const char**);
00036   void vtkXMLParserEndElement(void*, const char*);
00037   void vtkXMLParserCharacterDataHandler(void*, const char*, int);
00038 }
00039 
00040 class VTK_IO_EXPORT vtkXMLParser : public vtkObject
00041 {
00042 public:
00043   vtkTypeMacro(vtkXMLParser,vtkObject);
00044   void PrintSelf(ostream& os, vtkIndent indent);
00045 
00046   static vtkXMLParser* New();
00047 
00048   //BTX
00050 
00051   vtkSetMacro(Stream, istream*);
00052   vtkGetMacro(Stream, istream*);
00053   //ETX
00055 
00057 
00060   long TellG();
00061   void SeekG(long position);
00063 
00065   virtual int Parse();
00066 
00068 
00070   virtual int Parse(const char* inputString);
00071   virtual int Parse(const char* inputString, unsigned int length);
00073 
00075 
00080   virtual int InitializeParser();
00081   virtual int ParseChunk(const char* inputString, unsigned int length);
00082   virtual int CleanupParser();
00084 
00086 
00087   vtkSetStringMacro(FileName);
00088   vtkGetStringMacro(FileName);
00090 
00092 
00095   vtkSetMacro(IgnoreCharacterData, int);
00096   vtkGetMacro(IgnoreCharacterData, int);
00098 
00100 
00104   vtkSetStringMacro(Encoding);
00105   vtkGetStringMacro(Encoding);
00107 
00108 protected:
00109   vtkXMLParser();
00110   ~vtkXMLParser();
00111 
00112   // Input stream.  Set by user.
00113   istream* Stream;
00114 
00115   // File name to parse
00116   char* FileName;
00117 
00118   // Encoding
00119   char* Encoding;
00120 
00121   // This variable is true if there was a parse error while parsing in
00122   // chunks.
00123   int ParseError;
00124 
00125   // Character message to parse
00126   const char* InputString;
00127   int InputStringLength;
00128 
00129   // Expat parser structure.  Exists only during call to Parse().
00130   void* Parser;
00131 
00132   // Create/Allocate the internal parser (can be overriden by subclasses).
00133   virtual int CreateParser();
00134 
00135   // Called by Parse() to read the stream and call ParseBuffer.  Can
00136   // be replaced by subclasses to change how input is read.
00137   virtual int ParseXML();
00138 
00139   // Called before each block of input is read from the stream to
00140   // check if parsing is complete.  Can be replaced by subclasses to
00141   // change the terminating condition for parsing.  Parsing always
00142   // stops when the end of file is reached in the stream.
00143   virtual int ParsingComplete();
00144 
00145   // Called when a new element is opened in the XML source.  Should be
00146   // replaced by subclasses to handle each element.
00147   //  name = Name of new element.
00148   //  atts = Null-terminated array of attribute name/value pairs.
00149   //         Even indices are attribute names, and odd indices are values.
00150   virtual void StartElement(const char* name, const char** atts);
00151 
00152   // Called at the end of an element in the XML source opened when
00153   // StartElement was called.
00154   virtual void EndElement(const char* name);
00155 
00156   // Called when there is character data to handle.
00157   virtual void CharacterDataHandler(const char* data, int length);
00158 
00159   // Called by begin handlers to report any stray attribute values.
00160   virtual void ReportStrayAttribute(const char* element, const char* attr,
00161                                     const char* value);
00162 
00163   // Called by begin handlers to report any missing attribute values.
00164   virtual void ReportMissingAttribute(const char* element, const char* attr);
00165 
00166   // Called by begin handlers to report bad attribute values.
00167   virtual void ReportBadAttribute(const char* element, const char* attr,
00168                                   const char* value);
00169 
00170   // Called by StartElement to report unknown element type.
00171   virtual void ReportUnknownElement(const char* element);
00172 
00173   // Called by Parse to report an XML syntax error.
00174   virtual void ReportXmlParseError();
00175 
00176   // Get the current byte index from the beginning of the XML stream.
00177   unsigned long GetXMLByteIndex();
00178 
00179   // Send the given buffer to the XML parser.
00180   virtual int ParseBuffer(const char* buffer, unsigned int count);
00181 
00182   // Send the given c-style string to the XML parser.
00183   int ParseBuffer(const char* buffer);
00184 
00185   // Utility for convenience of subclasses.  Wraps isspace C library
00186   // routine.
00187   static int IsSpace(char c);
00188 
00189   //BTX
00190   friend void vtkXMLParserStartElement(void*, const char*, const char**);
00191   friend void vtkXMLParserEndElement(void*, const char*);
00192   friend void vtkXMLParserCharacterDataHandler(void*, const char*, int);
00193   //ETX
00194 
00195   int IgnoreCharacterData;
00196 
00197 private:
00198   vtkXMLParser(const vtkXMLParser&);  // Not implemented.
00199   void operator=(const vtkXMLParser&);  // Not implemented.
00200 };
00201 
00202 //----------------------------------------------------------------------------
00203 inline
00204 void vtkXMLParserCharacterDataHandler(
00205         void* parser,
00206         const char* data,
00207         int length)
00208 {
00209   // Character data handler that is registered with the XML_Parser.
00210   // This just casts the user data to a vtkXMLParser and calls
00211   // CharacterDataHandler.
00212   static_cast<vtkXMLParser*>(parser)->CharacterDataHandler(data, length);
00213 }
00214 
00215 #endif