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