VTK  9.3.0
vtkXMLParser.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
17 #ifndef vtkXMLParser_h
18 #define vtkXMLParser_h
19 
20 #include "vtkIOXMLParserModule.h" // For export macro
21 #include "vtkObject.h"
22 
23 VTK_ABI_NAMESPACE_BEGIN
24 void vtkXMLParserStartElement(void*, const char*, const char**);
25 void vtkXMLParserEndElement(void*, const char*);
26 void vtkXMLParserCharacterDataHandler(void*, const char*, int);
27 
28 class VTKIOXMLPARSER_EXPORT vtkXMLParser : public vtkObject
29 {
30 public:
31  vtkTypeMacro(vtkXMLParser, vtkObject);
32  void PrintSelf(ostream& os, vtkIndent indent) override;
33 
34  static vtkXMLParser* New();
35 
37 
40  vtkSetMacro(Stream, istream*);
41  vtkGetMacro(Stream, istream*);
43 
45 
50  vtkTypeInt64 TellG();
51  void SeekG(vtkTypeInt64 position);
53 
57  virtual int Parse();
58 
60 
64  virtual int Parse(const char* inputString);
65  virtual int Parse(const char* inputString, unsigned int length);
67 
69 
79  virtual int InitializeParser();
80  virtual int ParseChunk(const char* inputString, unsigned int length);
81  virtual int CleanupParser();
83 
85 
91 
93 
98  vtkSetMacro(IgnoreCharacterData, vtkTypeBool);
99  vtkGetMacro(IgnoreCharacterData, vtkTypeBool);
101 
103 
109  vtkSetStringMacro(Encoding);
110  vtkGetStringMacro(Encoding);
112 
113 protected:
115  ~vtkXMLParser() override;
116 
117  // Input stream. Set by user.
118  istream* Stream;
119 
120  // File name to parse
121  char* FileName;
122 
123  // Encoding
124  char* Encoding;
125 
126  // This variable is true if there was a parse error while parsing in
127  // chunks.
129 
130  // Character message to parse
131  const char* InputString;
133 
134  // Expat parser structure. Exists only during call to Parse().
135  void* Parser;
136 
137  // Create/Allocate the internal parser (can be overridden by subclasses).
138  virtual int CreateParser();
139 
140  // Called by Parse() to read the stream and call ParseBuffer. Can
141  // be replaced by subclasses to change how input is read.
142  virtual int ParseXML();
143 
144  // Called before each block of input is read from the stream to
145  // check if parsing is complete. Can be replaced by subclasses to
146  // change the terminating condition for parsing. Parsing always
147  // stops when the end of file is reached in the stream.
148  virtual int ParsingComplete();
149 
150  // Called when a new element is opened in the XML source. Should be
151  // replaced by subclasses to handle each element.
152  // name = Name of new element.
153  // atts = Null-terminated array of attribute name/value pairs.
154  // Even indices are attribute names, and odd indices are values.
155  virtual void StartElement(const char* name, const char** atts);
156 
157  // Called at the end of an element in the XML source opened when
158  // StartElement was called.
159  virtual void EndElement(const char* name);
160 
161  // Called when there is character data to handle.
162  virtual void CharacterDataHandler(const char* data, int length);
163 
164  // Called by begin handlers to report any stray attribute values.
165  virtual void ReportStrayAttribute(const char* element, const char* attr, const char* value);
166 
167  // Called by begin handlers to report any missing attribute values.
168  virtual void ReportMissingAttribute(const char* element, const char* attr);
169 
170  // Called by begin handlers to report bad attribute values.
171  virtual void ReportBadAttribute(const char* element, const char* attr, const char* value);
172 
173  // Called by StartElement to report unknown element type.
174  virtual void ReportUnknownElement(const char* element);
175 
176  // Called by Parse to report an XML syntax error.
177  virtual void ReportXmlParseError();
178 
179  // Get the current byte index from the beginning of the XML stream.
180  vtkTypeInt64 GetXMLByteIndex();
181 
182  // Send the given buffer to the XML parser.
183  virtual int ParseBuffer(const char* buffer, unsigned int count);
184 
185  // Send the given c-style string to the XML parser.
186  int ParseBuffer(const char* buffer);
187 
188  // Utility for convenience of subclasses. Wraps isspace C library
189  // routine.
190  static int IsSpace(char c);
191 
192  friend void vtkXMLParserStartElement(void*, const char*, const char**);
193  friend void vtkXMLParserEndElement(void*, const char*);
194  friend void vtkXMLParserCharacterDataHandler(void*, const char*, int);
195 
197 
198 private:
199  vtkXMLParser(const vtkXMLParser&) = delete;
200  void operator=(const vtkXMLParser&) = delete;
201 };
202 
203 //----------------------------------------------------------------------------
204 inline void vtkXMLParserCharacterDataHandler(void* parser, const char* data, int length)
205 {
206  // Character data handler that is registered with the XML_Parser.
207  // This just casts the user data to a vtkXMLParser and calls
208  // CharacterDataHandler.
209  static_cast<vtkXMLParser*>(parser)->CharacterDataHandler(data, length);
210 }
211 
212 VTK_ABI_NAMESPACE_END
213 #endif
a simple class to control print indentation
Definition: vtkIndent.h:38
abstract base class for most VTK objects
Definition: vtkObject.h:61
Parse XML to handle element tags and attributes.
Definition: vtkXMLParser.h:29
virtual void EndElement(const char *name)
vtkTypeBool IgnoreCharacterData
Definition: vtkXMLParser.h:196
char * Encoding
Definition: vtkXMLParser.h:124
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:121
virtual void ReportXmlParseError()
istream * Stream
Definition: vtkXMLParser.h:118
virtual int Parse()
Parse the XML input.
friend void vtkXMLParserStartElement(void *, const char *, const char **)
virtual int ParseChunk(const char *inputString, unsigned int length)
When parsing fragments of XML, or when streaming XML, use the following three methods:
vtkSetFilePathMacro(FileName)
Set and get file name.
virtual int CleanupParser()
When parsing fragments of XML, or when streaming XML, use the following three methods:
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)
Parse the XML message.
vtkTypeInt64 TellG()
Used by subclasses and their supporting classes.
virtual void ReportUnknownElement(const char *element)
virtual int CreateParser()
void SeekG(vtkTypeInt64 position)
Used by subclasses and their supporting classes.
virtual int ParseBuffer(const char *buffer, unsigned int count)
vtkTypeInt64 GetXMLByteIndex()
~vtkXMLParser() override
int InputStringLength
Definition: vtkXMLParser.h:132
virtual void ReportMissingAttribute(const char *element, const char *attr)
const char * InputString
Definition: vtkXMLParser.h:131
virtual int ParsingComplete()
virtual void ReportStrayAttribute(const char *element, const char *attr, const char *value)
vtkGetFilePathMacro(FileName)
Set and get file name.
virtual void ReportBadAttribute(const char *element, const char *attr, const char *value)
@ length
Definition: vtkX3D.h:393
@ value
Definition: vtkX3D.h:220
@ name
Definition: vtkX3D.h:219
@ position
Definition: vtkX3D.h:261
@ data
Definition: vtkX3D.h:315
int vtkTypeBool
Definition: vtkABI.h:64
void vtkXMLParserCharacterDataHandler(void *, const char *, int)
Definition: vtkXMLParser.h:204
void vtkXMLParserStartElement(void *, const char *, const char **)
void vtkXMLParserEndElement(void *, const char *)