VTK
vtkXMLParser.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkXMLParser.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
29 #ifndef vtkXMLParser_h
30 #define vtkXMLParser_h
31 
32 #include "vtkIOXMLParserModule.h" // For export macro
33 #include "vtkObject.h"
34 
35 extern "C"
36 {
37  void vtkXMLParserStartElement(void*, const char*, const char**);
38  void vtkXMLParserEndElement(void*, const char*);
39  void vtkXMLParserCharacterDataHandler(void*, const char*, int);
40 }
41 
42 class VTKIOXMLPARSER_EXPORT vtkXMLParser : public vtkObject
43 {
44 public:
45  vtkTypeMacro(vtkXMLParser,vtkObject);
46  void PrintSelf(ostream& os, vtkIndent indent) override;
47 
48  static vtkXMLParser* New();
49 
51 
54  vtkSetMacro(Stream, istream*);
55  vtkGetMacro(Stream, istream*);
57 
59 
64  vtkTypeInt64 TellG();
65  void SeekG(vtkTypeInt64 position);
67 
71  virtual int Parse();
72 
74 
78  virtual int Parse(const char* inputString);
79  virtual int Parse(const char* inputString, unsigned int length);
81 
83 
93  virtual int InitializeParser();
94  virtual int ParseChunk(const char* inputString, unsigned int length);
95  virtual int CleanupParser();
97 
99 
102  vtkSetStringMacro(FileName);
103  vtkGetStringMacro(FileName);
105 
107 
112  vtkSetMacro(IgnoreCharacterData, int);
113  vtkGetMacro(IgnoreCharacterData, int);
115 
117 
123  vtkSetStringMacro(Encoding);
124  vtkGetStringMacro(Encoding);
126 
127 protected:
128  vtkXMLParser();
129  ~vtkXMLParser() override;
130 
131  // Input stream. Set by user.
132  istream* Stream;
133 
134  // File name to parse
135  char* FileName;
136 
137  // Encoding
138  char* Encoding;
139 
140  // This variable is true if there was a parse error while parsing in
141  // chunks.
143 
144  // Character message to parse
145  const char* InputString;
147 
148  // Expat parser structure. Exists only during call to Parse().
149  void* Parser;
150 
151  // Create/Allocate the internal parser (can be overridden by subclasses).
152  virtual int CreateParser();
153 
154  // Called by Parse() to read the stream and call ParseBuffer. Can
155  // be replaced by subclasses to change how input is read.
156  virtual int ParseXML();
157 
158  // Called before each block of input is read from the stream to
159  // check if parsing is complete. Can be replaced by subclasses to
160  // change the terminating condition for parsing. Parsing always
161  // stops when the end of file is reached in the stream.
162  virtual int ParsingComplete();
163 
164  // Called when a new element is opened in the XML source. Should be
165  // replaced by subclasses to handle each element.
166  // name = Name of new element.
167  // atts = Null-terminated array of attribute name/value pairs.
168  // Even indices are attribute names, and odd indices are values.
169  virtual void StartElement(const char* name, const char** atts);
170 
171  // Called at the end of an element in the XML source opened when
172  // StartElement was called.
173  virtual void EndElement(const char* name);
174 
175  // Called when there is character data to handle.
176  virtual void CharacterDataHandler(const char* data, int length);
177 
178  // Called by begin handlers to report any stray attribute values.
179  virtual void ReportStrayAttribute(const char* element, const char* attr,
180  const char* value);
181 
182  // Called by begin handlers to report any missing attribute values.
183  virtual void ReportMissingAttribute(const char* element, const char* attr);
184 
185  // Called by begin handlers to report bad attribute values.
186  virtual void ReportBadAttribute(const char* element, const char* attr,
187  const char* value);
188 
189  // Called by StartElement to report unknown element type.
190  virtual void ReportUnknownElement(const char* element);
191 
192  // Called by Parse to report an XML syntax error.
193  virtual void ReportXmlParseError();
194 
195  // Get the current byte index from the beginning of the XML stream.
196  vtkTypeInt64 GetXMLByteIndex();
197 
198  // Send the given buffer to the XML parser.
199  virtual int ParseBuffer(const char* buffer, unsigned int count);
200 
201  // Send the given c-style string to the XML parser.
202  int ParseBuffer(const char* buffer);
203 
204  // Utility for convenience of subclasses. Wraps isspace C library
205  // routine.
206  static int IsSpace(char c);
207 
208  friend void vtkXMLParserStartElement(void*, const char*, const char**);
209  friend void vtkXMLParserEndElement(void*, const char*);
210  friend void vtkXMLParserCharacterDataHandler(void*, const char*, int);
211 
213 
214 private:
215  vtkXMLParser(const vtkXMLParser&) = delete;
216  void operator=(const vtkXMLParser&) = delete;
217 };
218 
219 //----------------------------------------------------------------------------
220 inline
222  void* parser,
223  const char* data,
224  int length)
225 {
226  // Character data handler that is registered with the XML_Parser.
227  // This just casts the user data to a vtkXMLParser and calls
228  // CharacterDataHandler.
229  static_cast<vtkXMLParser*>(parser)->CharacterDataHandler(data, length);
230 }
231 
232 #endif
int InputStringLength
Definition: vtkXMLParser.h:146
abstract base class for most VTK objects
Definition: vtkObject.h:59
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
Parse XML to handle element tags and attributes.
Definition: vtkXMLParser.h:42
char * FileName
Definition: vtkXMLParser.h:135
void vtkXMLParserCharacterDataHandler(void *, const char *, int)
Definition: vtkXMLParser.h:221
const char * InputString
Definition: vtkXMLParser.h:145
void vtkXMLParserEndElement(void *, const char *)
a simple class to control print indentation
Definition: vtkIndent.h:39
int IgnoreCharacterData
Definition: vtkXMLParser.h:212
void vtkXMLParserStartElement(void *, const char *, const char **)
istream * Stream
Definition: vtkXMLParser.h:132
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
char * Encoding
Definition: vtkXMLParser.h:138