VTK  9.3.1
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 
88  vtkSetFilePathMacro(FileName);
89  vtkGetFilePathMacro(FileName);
91 
93 
98  vtkSetMacro(IgnoreCharacterData, vtkTypeBool);
99  vtkGetMacro(IgnoreCharacterData, vtkTypeBool);
101 
103 
109  vtkSetStringMacro(Encoding);
110  vtkGetStringMacro(Encoding);
112 
117  static bool hasLargeOffsets();
118 
119 protected:
120  vtkXMLParser();
121  ~vtkXMLParser() override;
122 
123  // Input stream. Set by user.
124  istream* Stream;
125 
126  // File name to parse
127  char* FileName;
128 
129  // Encoding
130  char* Encoding;
131 
132  // This variable is true if there was a parse error while parsing in
133  // chunks.
135 
136  // Character message to parse
137  const char* InputString;
139 
140  // Expat parser structure. Exists only during call to Parse().
141  void* Parser;
142 
143  // Create/Allocate the internal parser (can be overridden by subclasses).
144  virtual int CreateParser();
145 
146  // Called by Parse() to read the stream and call ParseBuffer. Can
147  // be replaced by subclasses to change how input is read.
148  virtual int ParseXML();
149 
150  // Called before each block of input is read from the stream to
151  // check if parsing is complete. Can be replaced by subclasses to
152  // change the terminating condition for parsing. Parsing always
153  // stops when the end of file is reached in the stream.
154  virtual int ParsingComplete();
155 
156  // Called when a new element is opened in the XML source. Should be
157  // replaced by subclasses to handle each element.
158  // name = Name of new element.
159  // atts = Null-terminated array of attribute name/value pairs.
160  // Even indices are attribute names, and odd indices are values.
161  virtual void StartElement(const char* name, const char** atts);
162 
163  // Called at the end of an element in the XML source opened when
164  // StartElement was called.
165  virtual void EndElement(const char* name);
166 
167  // Called when there is character data to handle.
168  virtual void CharacterDataHandler(const char* data, int length);
169 
170  // Called by begin handlers to report any stray attribute values.
171  virtual void ReportStrayAttribute(const char* element, const char* attr, const char* value);
172 
173  // Called by begin handlers to report any missing attribute values.
174  virtual void ReportMissingAttribute(const char* element, const char* attr);
175 
176  // Called by begin handlers to report bad attribute values.
177  virtual void ReportBadAttribute(const char* element, const char* attr, const char* value);
178 
179  // Called by StartElement to report unknown element type.
180  virtual void ReportUnknownElement(const char* element);
181 
182  // Called by Parse to report an XML syntax error.
183  virtual void ReportXmlParseError();
184 
185  // Get the current byte index from the beginning of the XML stream.
186  vtkTypeInt64 GetXMLByteIndex();
187 
188  // Send the given buffer to the XML parser.
189  virtual int ParseBuffer(const char* buffer, unsigned int count);
190 
191  // Send the given c-style string to the XML parser.
192  int ParseBuffer(const char* buffer);
193 
194  // Utility for convenience of subclasses. Wraps isspace C library
195  // routine.
196  static int IsSpace(char c);
197 
198  friend void vtkXMLParserStartElement(void*, const char*, const char**);
199  friend void vtkXMLParserEndElement(void*, const char*);
200  friend void vtkXMLParserCharacterDataHandler(void*, const char*, int);
201 
203 
204 private:
205  vtkXMLParser(const vtkXMLParser&) = delete;
206  void operator=(const vtkXMLParser&) = delete;
207 };
208 
209 //----------------------------------------------------------------------------
210 inline void vtkXMLParserCharacterDataHandler(void* parser, const char* data, int length)
211 {
212  // Character data handler that is registered with the XML_Parser.
213  // This just casts the user data to a vtkXMLParser and calls
214  // CharacterDataHandler.
215  static_cast<vtkXMLParser*>(parser)->CharacterDataHandler(data, length);
216 }
217 
218 VTK_ABI_NAMESPACE_END
219 #endif
int InputStringLength
Definition: vtkXMLParser.h:138
abstract base class for most VTK objects
Definition: vtkObject.h:51
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:28
vtkTypeBool IgnoreCharacterData
Definition: vtkXMLParser.h:202
char * FileName
Definition: vtkXMLParser.h:127
void vtkXMLParserCharacterDataHandler(void *, const char *, int)
Definition: vtkXMLParser.h:210
const char * InputString
Definition: vtkXMLParser.h:137
void vtkXMLParserEndElement(void *, const char *)
int vtkTypeBool
Definition: vtkABI.h:64
a simple class to control print indentation
Definition: vtkIndent.h:28
void vtkXMLParserStartElement(void *, const char *, const char **)
istream * Stream
Definition: vtkXMLParser.h:124
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
char * Encoding
Definition: vtkXMLParser.h:130