VTK  9.0.1
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 =========================================================================*/
26 #ifndef vtkXMLParser_h
27 #define vtkXMLParser_h
28 
29 #include "vtkIOXMLParserModule.h" // For export macro
30 #include "vtkObject.h"
31 
32 extern "C"
33 {
34  void vtkXMLParserStartElement(void*, const char*, const char**);
35  void vtkXMLParserEndElement(void*, const char*);
36  void vtkXMLParserCharacterDataHandler(void*, const char*, int);
37 }
38 
39 class VTKIOXMLPARSER_EXPORT vtkXMLParser : public vtkObject
40 {
41 public:
42  vtkTypeMacro(vtkXMLParser, vtkObject);
43  void PrintSelf(ostream& os, vtkIndent indent) override;
44 
45  static vtkXMLParser* New();
46 
48 
51  vtkSetMacro(Stream, istream*);
52  vtkGetMacro(Stream, istream*);
54 
56 
61  vtkTypeInt64 TellG();
62  void SeekG(vtkTypeInt64 position);
64 
68  virtual int Parse();
69 
71 
75  virtual int Parse(const char* inputString);
76  virtual int Parse(const char* inputString, unsigned int length);
78 
80 
90  virtual int InitializeParser();
91  virtual int ParseChunk(const char* inputString, unsigned int length);
92  virtual int CleanupParser();
94 
96 
99  vtkSetStringMacro(FileName);
100  vtkGetStringMacro(FileName);
102 
104 
109  vtkSetMacro(IgnoreCharacterData, int);
110  vtkGetMacro(IgnoreCharacterData, int);
112 
114 
120  vtkSetStringMacro(Encoding);
121  vtkGetStringMacro(Encoding);
123 
124 protected:
126  ~vtkXMLParser() override;
127 
128  // Input stream. Set by user.
129  istream* Stream;
130 
131  // File name to parse
132  char* FileName;
133 
134  // Encoding
135  char* Encoding;
136 
137  // This variable is true if there was a parse error while parsing in
138  // chunks.
140 
141  // Character message to parse
142  const char* InputString;
144 
145  // Expat parser structure. Exists only during call to Parse().
146  void* Parser;
147 
148  // Create/Allocate the internal parser (can be overridden by subclasses).
149  virtual int CreateParser();
150 
151  // Called by Parse() to read the stream and call ParseBuffer. Can
152  // be replaced by subclasses to change how input is read.
153  virtual int ParseXML();
154 
155  // Called before each block of input is read from the stream to
156  // check if parsing is complete. Can be replaced by subclasses to
157  // change the terminating condition for parsing. Parsing always
158  // stops when the end of file is reached in the stream.
159  virtual int ParsingComplete();
160 
161  // Called when a new element is opened in the XML source. Should be
162  // replaced by subclasses to handle each element.
163  // name = Name of new element.
164  // atts = Null-terminated array of attribute name/value pairs.
165  // Even indices are attribute names, and odd indices are values.
166  virtual void StartElement(const char* name, const char** atts);
167 
168  // Called at the end of an element in the XML source opened when
169  // StartElement was called.
170  virtual void EndElement(const char* name);
171 
172  // Called when there is character data to handle.
173  virtual void CharacterDataHandler(const char* data, int length);
174 
175  // Called by begin handlers to report any stray attribute values.
176  virtual void ReportStrayAttribute(const char* element, const char* attr, const char* value);
177 
178  // Called by begin handlers to report any missing attribute values.
179  virtual void ReportMissingAttribute(const char* element, const char* attr);
180 
181  // Called by begin handlers to report bad attribute values.
182  virtual void ReportBadAttribute(const char* element, const char* attr, const char* value);
183 
184  // Called by StartElement to report unknown element type.
185  virtual void ReportUnknownElement(const char* element);
186 
187  // Called by Parse to report an XML syntax error.
188  virtual void ReportXmlParseError();
189 
190  // Get the current byte index from the beginning of the XML stream.
191  vtkTypeInt64 GetXMLByteIndex();
192 
193  // Send the given buffer to the XML parser.
194  virtual int ParseBuffer(const char* buffer, unsigned int count);
195 
196  // Send the given c-style string to the XML parser.
197  int ParseBuffer(const char* buffer);
198 
199  // Utility for convenience of subclasses. Wraps isspace C library
200  // routine.
201  static int IsSpace(char c);
202 
203  friend void vtkXMLParserStartElement(void*, const char*, const char**);
204  friend void vtkXMLParserEndElement(void*, const char*);
205  friend void vtkXMLParserCharacterDataHandler(void*, const char*, int);
206 
208 
209 private:
210  vtkXMLParser(const vtkXMLParser&) = delete;
211  void operator=(const vtkXMLParser&) = delete;
212 };
213 
214 //----------------------------------------------------------------------------
215 inline void vtkXMLParserCharacterDataHandler(void* parser, const char* data, int length)
216 {
217  // Character data handler that is registered with the XML_Parser.
218  // This just casts the user data to a vtkXMLParser and calls
219  // CharacterDataHandler.
220  static_cast<vtkXMLParser*>(parser)->CharacterDataHandler(data, length);
221 }
222 
223 #endif
a simple class to control print indentation
Definition: vtkIndent.h:34
abstract base class for most VTK objects
Definition: vtkObject.h:54
Parse XML to handle element tags and attributes.
Definition: vtkXMLParser.h:40
virtual void EndElement(const char *name)
char * Encoding
Definition: vtkXMLParser.h:135
virtual void CharacterDataHandler(const char *data, int length)
virtual int ParseXML()
static int IsSpace(char c)
virtual int Parse(const char *inputString)
Parse the XML message.
int ParseBuffer(const char *buffer)
static vtkXMLParser * New()
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
virtual void StartElement(const char *name, const char **atts)
char * FileName
Definition: vtkXMLParser.h:132
virtual void ReportXmlParseError()
istream * Stream
Definition: vtkXMLParser.h:129
virtual int Parse()
Parse the XML input.
friend void vtkXMLParserStartElement(void *, const char *, const char **)
virtual int ParseChunk(const char *inputString, unsigned int length)
virtual int CleanupParser()
friend void vtkXMLParserEndElement(void *, const char *)
virtual int InitializeParser()
When parsing fragments of XML, or when streaming XML, use the following three methods:
virtual int Parse(const char *inputString, unsigned int length)
vtkTypeInt64 TellG()
Used by subclasses and their supporting classes.
virtual void ReportUnknownElement(const char *element)
virtual int CreateParser()
int IgnoreCharacterData
Definition: vtkXMLParser.h:207
void SeekG(vtkTypeInt64 position)
virtual int ParseBuffer(const char *buffer, unsigned int count)
vtkTypeInt64 GetXMLByteIndex()
~vtkXMLParser() override
int InputStringLength
Definition: vtkXMLParser.h:143
virtual void ReportMissingAttribute(const char *element, const char *attr)
const char * InputString
Definition: vtkXMLParser.h:142
virtual int ParsingComplete()
virtual void ReportStrayAttribute(const char *element, const char *attr, const char *value)
virtual void ReportBadAttribute(const char *element, const char *attr, const char *value)
@ length
Definition: vtkX3D.h:399
@ value
Definition: vtkX3D.h:226
@ name
Definition: vtkX3D.h:225
@ position
Definition: vtkX3D.h:267
@ data
Definition: vtkX3D.h:321
void vtkXMLParserCharacterDataHandler(void *, const char *, int)
Definition: vtkXMLParser.h:215
void vtkXMLParserStartElement(void *, const char *, const char **)
void vtkXMLParserEndElement(void *, const char *)