vtkXMLDataParser.h 8.03 KB
Newer Older
1 2
/*=========================================================================

Brad King's avatar
Brad King committed
3
  Program:   Visualization Toolkit
4 5
  Module:    vtkXMLDataParser.h

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
Brad King's avatar
Brad King committed
7 8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
Brad King's avatar
Brad King committed
12
     PURPOSE.  See the above copyright notice for more information.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

=========================================================================*/
// .NAME vtkXMLDataParser - Used by vtkXMLReader to parse VTK XML files.
// .SECTION Description
// vtkXMLDataParser provides a subclass of vtkXMLParser that
// constructs a representation of an XML data format's file using
// vtkXMLDataElement to represent each XML element.  This
// representation is then used by vtkXMLReader and its subclasses to
// traverse the structure of the file and extract data.

// .SECTION See Also
// vtkXMLDataElement

#ifndef __vtkXMLDataParser_h
#define __vtkXMLDataParser_h

#include "vtkXMLParser.h"

class vtkXMLDataElement;
class vtkInputStream;
class vtkDataCompressor;

class VTK_IO_EXPORT vtkXMLDataParser : public vtkXMLParser
{
public:
  vtkTypeRevisionMacro(vtkXMLDataParser,vtkXMLParser);
  void PrintSelf(ostream& os, vtkIndent indent);
  static vtkXMLDataParser* New();
41

42 43 44 45 46
  //BTX
  // Description:
  // Enumerate big and little endian byte order settings.
  enum { BigEndian, LittleEndian };
  //ETX
47

48 49 50
  // Description:
  // Get the root element from the XML document.
  vtkXMLDataElement* GetRootElement();
51

52 53 54 55 56 57 58 59 60 61
  // Description:
  // Read inline data from inside the given element.  Returns the
  // number of words read.
  unsigned long ReadInlineData(vtkXMLDataElement* element, int isAscii,
                               void* buffer, int startWord, int numWords,
                               int wordType);
  unsigned long ReadInlineData(vtkXMLDataElement* element, int isAscii,
                               char* buffer, int startWord, int numWords)
    { return this->ReadInlineData(element, isAscii, buffer, startWord,
                                  numWords, VTK_CHAR); }
62

63 64 65 66 67 68 69 70 71
  // Description:
  // Read from an appended data section starting at the given appended
  // data offset.  Returns the number of words read.
  unsigned long ReadAppendedData(unsigned long offset, void* buffer,
                                 int startWord, int numWords, int wordType);
  unsigned long ReadAppendedData(unsigned long offset, char* buffer,
                                 int startWord, int numWords)
    { return this->ReadAppendedData(offset, buffer, startWord, numWords,
                                    VTK_CHAR); }
72

73 74 75 76 77
  // Description:
  // Read from an ascii data section starting at the current position in
  // the stream.  Returns the number of words read.
  unsigned long ReadAsciiData(void* buffer, int startWord, int numWords,
                              int wordType);
78

79 80 81 82 83
  // Description:
  // Read from a data section starting at the current position in the
  // stream.  Returns the number of words read.
  unsigned long ReadBinaryData(void* buffer, int startWord, int maxWords,
                               int wordType);
84

85 86 87 88 89
  // Description:
  // Get/Set the compressor used to decompress binary and appended data
  // after reading from the file.
  virtual void SetCompressor(vtkDataCompressor*);
  vtkGetObjectMacro(Compressor, vtkDataCompressor);
90

91 92 93
  // Description:
  // Get the size of a word of the given type.
  unsigned long GetWordTypeSize(int wordType);
94 95 96 97 98 99

  // Description:
  // Parse the XML input and check that the file is safe to read.
  // Returns 1 for okay, 0 for error.
  virtual int Parse();

100 101 102 103 104
  // Description:
  // Get/Set flag to abort reading of data.  This may be set by a
  // progress event observer.
  vtkGetMacro(Abort, int);
  vtkSetMacro(Abort, int);
105

106 107 108 109
  // Description:
  // Get/Set progress of reading data.  This may be checked by a
  // progress event observer.
  vtkGetMacro(Progress, float);
110 111
  vtkSetMacro(Progress, float);

112 113 114 115 116 117 118 119 120
  // Description:
  // Get/Set the character encoding that will be used to set the attributes's
  // encoding type of each vtkXMLDataElement created by this parser (i.e.,
  // the data element attributes will use that encoding internally).
  // If set to VTK_ENCODING_NONE (default), the attribute encoding type will
  // not be changed and will default to the vtkXMLDataElement default encoding
  // type (see vtkXMLDataElement::AttributeEncoding).
  vtkSetClampMacro(AttributesEncoding,int,VTK_ENCODING_NONE,VTK_ENCODING_UNKNOWN);
  vtkGetMacro(AttributesEncoding, int);
121

122 123 124
protected:
  vtkXMLDataParser();
  ~vtkXMLDataParser();
125

126 127
  // This parser does not support parsing from a string.
  virtual int Parse(const char*);
Andy Cedilnik's avatar
Andy Cedilnik committed
128
  virtual int Parse(const char*, unsigned int);
129

130
  // Implement parsing methods.
131 132 133
  void StartElement(const char* name, const char** atts);
  void EndElement(const char*);
  int ParsingComplete();
134
  int CheckPrimaryAttributes();
135 136 137
  void FindAppendedDataPosition();
  unsigned long FindInlineDataPosition(unsigned long start);
  int ParseBuffer(const char* buffer, unsigned int count);
138

139 140 141
  void AddElement(vtkXMLDataElement* element);
  void PushOpenElement(vtkXMLDataElement* element);
  vtkXMLDataElement* PopOpenElement();
142
  void FreeAllElements();
143
  void PerformByteSwap(void* data, int numWords, int wordSize);
144

145 146 147 148 149
  // Data reading methods.
  void ReadCompressionHeader();
  unsigned int FindBlockSize(unsigned int block);
  int ReadBlock(unsigned int block, unsigned char* buffer);
  unsigned char* ReadBlock(unsigned int block);
150 151 152 153 154 155 156 157
  unsigned long ReadUncompressedData(unsigned char* data,
                                     unsigned long startWord,
                                     unsigned long numWords,
                                     int wordSize);
  unsigned long ReadCompressedData(unsigned char* data,
                                   unsigned long startWord,
                                   unsigned long numWords,
                                   int wordSize);
158

159 160 161
  // Ascii data reading methods.
  int ParseAsciiData(int wordType);
  void FreeAsciiBuffer();
162

163 164
  // Progress update methods.
  void UpdateProgress(float progress);
165

166 167
  // The root XML element.
  vtkXMLDataElement* RootElement;
168

169 170 171 172
  // The stack of elements currently being parsed.
  vtkXMLDataElement** OpenElements;
  unsigned int NumberOfOpenElements;
  unsigned int OpenElementsSize;
173

174 175
  // The position of the appended data section, if found.
  unsigned long AppendedDataPosition;
176

177 178
  // How much of the string "<AppendedData" has been matched in input.
  int AppendedDataMatched;
179

180 181
  // The byte order of the binary input.
  int ByteOrder;
182

183 184 185
  // The input stream used to read data.  Set by ReadAppendedData and
  // ReadInlineData methods.
  vtkInputStream* DataStream;
186

187 188 189
  // The input stream used to read inline data.  May transparently
  // decode the data.
  vtkInputStream* InlineDataStream;
190

191 192
  // The stream to use for appended data.
  vtkInputStream* AppendedDataStream;
193

194 195 196 197 198 199 200 201 202 203 204 205 206
  //BTX
  // We need a 32 bit unsigned integer type for platform-independent
  // binary headers.  Note that this is duplicated in vtkXMLWriter.h.
#if VTK_SIZEOF_SHORT == 4
  typedef unsigned short HeaderType;
#elif VTK_SIZEOF_INT == 4
  typedef unsigned int HeaderType;
#elif VTK_SIZEOF_LONG == 4
  typedef unsigned long HeaderType;
#else
# error "No native data type can represent an unsigned 32-bit integer."
#endif
  //ETX
207

208 209 210 211 212
  // Decompression data.
  vtkDataCompressor* Compressor;
  unsigned int NumberOfBlocks;
  unsigned int BlockUncompressedSize;
  unsigned int PartialLastBlockUncompressedSize;
213
  HeaderType* BlockCompressedSizes;
214
  unsigned long* BlockStartOffsets;
215

216 217 218 219 220
  // Ascii data parsing.
  unsigned char* AsciiDataBuffer;
  int AsciiDataBufferLength;
  int AsciiDataWordType;
  unsigned long AsciiDataPosition;
221

222 223
  // Progress during reading of data.
  float Progress;
224

225 226
  // Abort flag checked during reading of data.
  int Abort;
227 228 229

  int AttributesEncoding;

230 231 232 233 234 235
private:
  vtkXMLDataParser(const vtkXMLDataParser&);  // Not implemented.
  void operator=(const vtkXMLDataParser&);  // Not implemented.
};

#endif