VTK
dox/IO/XMLParser/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 "vtkIOXMLParserModule.h" // For export macro
00032 #include "vtkObject.h"
00033 
00034 extern "C"
00035 {
00036   void vtkXMLParserStartElement(void*, const char*, const char**);
00037   void vtkXMLParserEndElement(void*, const char*);
00038   void vtkXMLParserCharacterDataHandler(void*, const char*, int);
00039 }
00040 
00041 class VTKIOXMLPARSER_EXPORT vtkXMLParser : public vtkObject
00042 {
00043 public:
00044   vtkTypeMacro(vtkXMLParser,vtkObject);
00045   void PrintSelf(ostream& os, vtkIndent indent);
00046 
00047   static vtkXMLParser* New();
00048 
00049   //BTX
00051 
00052   vtkSetMacro(Stream, istream*);
00053   vtkGetMacro(Stream, istream*);
00055 
00057 
00060   vtkTypeInt64 TellG();
00061   void SeekG(vtkTypeInt64 position);
00062   //ETX
00064 
00066   virtual int Parse();
00067 
00069 
00071   virtual int Parse(const char* inputString);
00072   virtual int Parse(const char* inputString, unsigned int length);
00074 
00076 
00081   virtual int InitializeParser();
00082   virtual int ParseChunk(const char* inputString, unsigned int length);
00083   virtual int CleanupParser();
00085 
00087 
00088   vtkSetStringMacro(FileName);
00089   vtkGetStringMacro(FileName);
00091 
00093 
00096   vtkSetMacro(IgnoreCharacterData, int);
00097   vtkGetMacro(IgnoreCharacterData, int);
00099 
00101 
00105   vtkSetStringMacro(Encoding);
00106   vtkGetStringMacro(Encoding);
00108 
00109 protected:
00110   vtkXMLParser();
00111   ~vtkXMLParser();
00112 
00113   // Input stream.  Set by user.
00114   istream* Stream;
00115 
00116   // File name to parse
00117   char* FileName;
00118 
00119   // Encoding
00120   char* Encoding;
00121 
00122   // This variable is true if there was a parse error while parsing in
00123   // chunks.
00124   int ParseError;
00125 
00126   // Character message to parse
00127   const char* InputString;
00128   int InputStringLength;
00129 
00130   // Expat parser structure.  Exists only during call to Parse().
00131   void* Parser;
00132 
00133   // Create/Allocate the internal parser (can be overriden by subclasses).
00134   virtual int CreateParser();
00135 
00136   // Called by Parse() to read the stream and call ParseBuffer.  Can
00137   // be replaced by subclasses to change how input is read.
00138   virtual int ParseXML();
00139 
00140   // Called before each block of input is read from the stream to
00141   // check if parsing is complete.  Can be replaced by subclasses to
00142   // change the terminating condition for parsing.  Parsing always
00143   // stops when the end of file is reached in the stream.
00144   virtual int ParsingComplete();
00145 
00146   // Called when a new element is opened in the XML source.  Should be
00147   // replaced by subclasses to handle each element.
00148   //  name = Name of new element.
00149   //  atts = Null-terminated array of attribute name/value pairs.
00150   //         Even indices are attribute names, and odd indices are values.
00151   virtual void StartElement(const char* name, const char** atts);
00152 
00153   // Called at the end of an element in the XML source opened when
00154   // StartElement was called.
00155   virtual void EndElement(const char* name);
00156 
00157   // Called when there is character data to handle.
00158   virtual void CharacterDataHandler(const char* data, int length);
00159 
00160   // Called by begin handlers to report any stray attribute values.
00161   virtual void ReportStrayAttribute(const char* element, const char* attr,
00162                                     const char* value);
00163 
00164   // Called by begin handlers to report any missing attribute values.
00165   virtual void ReportMissingAttribute(const char* element, const char* attr);
00166 
00167   // Called by begin handlers to report bad attribute values.
00168   virtual void ReportBadAttribute(const char* element, const char* attr,
00169                                   const char* value);
00170 
00171   // Called by StartElement to report unknown element type.
00172   virtual void ReportUnknownElement(const char* element);
00173 
00174   // Called by Parse to report an XML syntax error.
00175   virtual void ReportXmlParseError();
00176 
00177   // Get the current byte index from the beginning of the XML stream.
00178   vtkTypeInt64 GetXMLByteIndex();
00179 
00180   // Send the given buffer to the XML parser.
00181   virtual int ParseBuffer(const char* buffer, unsigned int count);
00182 
00183   // Send the given c-style string to the XML parser.
00184   int ParseBuffer(const char* buffer);
00185 
00186   // Utility for convenience of subclasses.  Wraps isspace C library
00187   // routine.
00188   static int IsSpace(char c);
00189 
00190   //BTX
00191   friend void vtkXMLParserStartElement(void*, const char*, const char**);
00192   friend void vtkXMLParserEndElement(void*, const char*);
00193   friend void vtkXMLParserCharacterDataHandler(void*, const char*, int);
00194   //ETX
00195 
00196   int IgnoreCharacterData;
00197 
00198 private:
00199   vtkXMLParser(const vtkXMLParser&);  // Not implemented.
00200   void operator=(const vtkXMLParser&);  // Not implemented.
00201 };
00202 
00203 //----------------------------------------------------------------------------
00204 inline
00205 void vtkXMLParserCharacterDataHandler(
00206         void* parser,
00207         const char* data,
00208         int length)
00209 {
00210   // Character data handler that is registered with the XML_Parser.
00211   // This just casts the user data to a vtkXMLParser and calls
00212   // CharacterDataHandler.
00213   static_cast<vtkXMLParser*>(parser)->CharacterDataHandler(data, length);
00214 }
00215 
00216 #endif