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 =========================================================================*/
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:
125  vtkXMLParser();
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,
177  const char* value);
178 
179  // Called by begin handlers to report any missing attribute values.
180  virtual void ReportMissingAttribute(const char* element, const char* attr);
181 
182  // Called by begin handlers to report bad attribute values.
183  virtual void ReportBadAttribute(const char* element, const char* attr,
184  const char* value);
185 
186  // Called by StartElement to report unknown element type.
187  virtual void ReportUnknownElement(const char* element);
188 
189  // Called by Parse to report an XML syntax error.
190  virtual void ReportXmlParseError();
191 
192  // Get the current byte index from the beginning of the XML stream.
193  vtkTypeInt64 GetXMLByteIndex();
194 
195  // Send the given buffer to the XML parser.
196  virtual int ParseBuffer(const char* buffer, unsigned int count);
197 
198  // Send the given c-style string to the XML parser.
199  int ParseBuffer(const char* buffer);
200 
201  // Utility for convenience of subclasses. Wraps isspace C library
202  // routine.
203  static int IsSpace(char c);
204 
205  friend void vtkXMLParserStartElement(void*, const char*, const char**);
206  friend void vtkXMLParserEndElement(void*, const char*);
207  friend void vtkXMLParserCharacterDataHandler(void*, const char*, int);
208 
210 
211 private:
212  vtkXMLParser(const vtkXMLParser&) = delete;
213  void operator=(const vtkXMLParser&) = delete;
214 };
215 
216 //----------------------------------------------------------------------------
217 inline
219  void* parser,
220  const char* data,
221  int length)
222 {
223  // Character data handler that is registered with the XML_Parser.
224  // This just casts the user data to a vtkXMLParser and calls
225  // CharacterDataHandler.
226  static_cast<vtkXMLParser*>(parser)->CharacterDataHandler(data, length);
227 }
228 
229 #endif
int InputStringLength
Definition: vtkXMLParser.h:143
abstract base class for most VTK objects
Definition: vtkObject.h:53
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:39
char * FileName
Definition: vtkXMLParser.h:132
void vtkXMLParserCharacterDataHandler(void *, const char *, int)
Definition: vtkXMLParser.h:218
const char * InputString
Definition: vtkXMLParser.h:142
void vtkXMLParserEndElement(void *, const char *)
a simple class to control print indentation
Definition: vtkIndent.h:33
int IgnoreCharacterData
Definition: vtkXMLParser.h:209
void vtkXMLParserStartElement(void *, const char *, const char **)
istream * Stream
Definition: vtkXMLParser.h:129
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
char * Encoding
Definition: vtkXMLParser.h:135