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