VTK  9.3.20240424
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
23VTK_ABI_NAMESPACE_BEGIN
24void vtkXMLParserStartElement(void*, const char*, const char**);
25void vtkXMLParserEndElement(void*, const char*);
26void vtkXMLParserCharacterDataHandler(void*, const char*, int);
27
28class VTKIOXMLPARSER_EXPORT vtkXMLParser : public vtkObject
29{
30public:
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
117 static bool hasLargeOffsets();
118
119protected:
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
204private:
205 vtkXMLParser(const vtkXMLParser&) = delete;
206 void operator=(const vtkXMLParser&) = delete;
207};
208
209//----------------------------------------------------------------------------
210inline 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
218VTK_ABI_NAMESPACE_END
219#endif
a simple class to control print indentation
Definition vtkIndent.h:108
abstract base class for most VTK objects
Definition vtkObject.h:162
Parse XML to handle element tags and attributes.
virtual void EndElement(const char *name)
vtkTypeBool IgnoreCharacterData
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)
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)
virtual void ReportXmlParseError()
istream * Stream
virtual int Parse()
Parse the XML input.
static vtkXMLParser * New()
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
virtual void ReportMissingAttribute(const char *element, const char *attr)
const char * InputString
static bool hasLargeOffsets()
The Expat library can only handle binary files > 2Gb if either size_of(long) == 8 or the "large size"...
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)
int vtkTypeBool
Definition vtkABI.h:64
void vtkXMLParserCharacterDataHandler(void *, const char *, int)
void vtkXMLParserStartElement(void *, const char *, const char **)
void vtkXMLParserEndElement(void *, const char *)