vtkXMLDataParser.h 8.63 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

=========================================================================*/
// .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

26 27
#ifndef vtkXMLDataParser_h
#define vtkXMLDataParser_h
28

29
#include "vtkIOXMLParserModule.h" // For export macro
30
#include "vtkXMLParser.h"
Burlen Loring's avatar
BUG:  
Burlen Loring committed
31
#include "vtkXMLDataElement.h"//For inline definition.
32 33 34 35

class vtkInputStream;
class vtkDataCompressor;

36
class VTKIOXMLPARSER_EXPORT vtkXMLDataParser : public vtkXMLParser
37 38
{
public:
39
  vtkTypeMacro(vtkXMLDataParser,vtkXMLParser);
40 41
  void PrintSelf(ostream& os, vtkIndent indent);
  static vtkXMLDataParser* New();
42

43 44 45 46
  // Description:
  // Get the root element from the XML document.
  vtkXMLDataElement* GetRootElement();

47 48 49
  // Description:
  // Enumerate big and little endian byte order settings.
  enum { BigEndian, LittleEndian };
50

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

63 64 65
  // Description:
  // Read from an appended data section starting at the given appended
  // data offset.  Returns the number of words read.
66 67 68 69 70 71 72
  size_t ReadAppendedData(vtkTypeInt64 offset, void* buffer,
                          vtkTypeUInt64 startWord,
                          size_t numWords, int wordType);
  size_t ReadAppendedData(vtkTypeInt64 offset, char* buffer,
                          vtkTypeUInt64 startWord,
                          size_t numWords)
  { return this->ReadAppendedData(offset, buffer, startWord, numWords,
73
                                    VTK_CHAR); }
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.
78 79
  size_t ReadAsciiData(void* buffer, vtkTypeUInt64 startWord,
                       size_t numWords, int wordType);
80

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

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

93 94
  // Description:
  // Get the size of a word of the given type.
95
  size_t GetWordTypeSize(int wordType);
96 97 98 99 100 101

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

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

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

114 115 116 117 118 119 120 121 122
  // 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);
123

124 125 126
  // Description:
  // If you need the text inside XMLElements, turn IgnoreCharacterData off.
  // This method will then be called when the file is parsed, and the text
127
  // will be stored in each XMLDataElement. VTK XML Readers store the
128
  // information elsewhere, so the default is to ignore it.
129 130
  virtual void CharacterDataHandler(const char* data, int length);

131 132 133 134 135 136 137 138
  // Description:
  // Returns the byte index of where appended data starts (if the
  // file is using appended data). Valid after the XML is parsed.
  vtkTypeInt64 GetAppendedDataPosition()
  {
    return this->AppendedDataPosition;
  }

139 140 141
protected:
  vtkXMLDataParser();
  ~vtkXMLDataParser();
142

143 144
  // This parser does not support parsing from a string.
  virtual int Parse(const char*);
Andy Cedilnik's avatar
Andy Cedilnik committed
145
  virtual int Parse(const char*, unsigned int);
146

147
  // Implement parsing methods.
148 149 150
  virtual void StartElement(const char* name, const char** atts);
  virtual void EndElement(const char*);

151
  int ParsingComplete();
152
  int CheckPrimaryAttributes();
153 154
  void FindAppendedDataPosition();
  int ParseBuffer(const char* buffer, unsigned int count);
155

156 157 158
  void AddElement(vtkXMLDataElement* element);
  void PushOpenElement(vtkXMLDataElement* element);
  vtkXMLDataElement* PopOpenElement();
159
  void FreeAllElements();
160
  void PerformByteSwap(void* data, size_t numWords, size_t wordSize);
161

162
  // Data reading methods.
163
  int ReadCompressionHeader();
164 165 166 167 168 169 170 171 172 173 174
  size_t FindBlockSize(vtkTypeUInt64 block);
  int ReadBlock(vtkTypeUInt64 block, unsigned char* buffer);
  unsigned char* ReadBlock(vtkTypeUInt64 block);
  size_t ReadUncompressedData(unsigned char* data,
                              vtkTypeUInt64 startWord,
                              size_t numWords,
                              size_t wordSize);
  size_t ReadCompressedData(unsigned char* data,
                            vtkTypeUInt64 startWord,
                            size_t numWords,
                            size_t wordSize);
175

176 177 178
  // Go to the start of the inline data
  void SeekInlineDataPosition(vtkXMLDataElement *element);

179 180 181
  // Ascii data reading methods.
  int ParseAsciiData(int wordType);
  void FreeAsciiBuffer();
182

183 184
  // Progress update methods.
  void UpdateProgress(float progress);
185

186 187
  // The root XML element.
  vtkXMLDataElement* RootElement;
188

189 190 191 192
  // The stack of elements currently being parsed.
  vtkXMLDataElement** OpenElements;
  unsigned int NumberOfOpenElements;
  unsigned int OpenElementsSize;
193

194
  // The position of the appended data section, if found.
195
  vtkTypeInt64 AppendedDataPosition;
196

197 198
  // How much of the string "<AppendedData" has been matched in input.
  int AppendedDataMatched;
199

200 201
  // The byte order of the binary input.
  int ByteOrder;
202

203 204 205
  // The word type of binary input headers.
  int HeaderType;

206 207 208
  // The input stream used to read data.  Set by ReadAppendedData and
  // ReadInlineData methods.
  vtkInputStream* DataStream;
209

210 211 212
  // The input stream used to read inline data.  May transparently
  // decode the data.
  vtkInputStream* InlineDataStream;
213

214 215
  // The stream to use for appended data.
  vtkInputStream* AppendedDataStream;
216

217 218
  // Decompression data.
  vtkDataCompressor* Compressor;
219
  size_t NumberOfBlocks;
220 221
  size_t BlockUncompressedSize;
  size_t PartialLastBlockUncompressedSize;
222
  size_t* BlockCompressedSizes;
223
  vtkTypeInt64* BlockStartOffsets;
224

225 226
  // Ascii data parsing.
  unsigned char* AsciiDataBuffer;
227
  size_t AsciiDataBufferLength;
228
  int AsciiDataWordType;
229
  vtkTypeInt64 AsciiDataPosition;
230

231 232
  // Progress during reading of data.
  float Progress;
233

234 235
  // Abort flag checked during reading of data.
  int Abort;
236 237 238

  int AttributesEncoding;

239
private:
240
  vtkXMLDataParser(const vtkXMLDataParser&) VTK_DELETE_FUNCTION;
241
  void operator=(const vtkXMLDataParser&) VTK_DELETE_FUNCTION;
242 243
};

Burlen Loring's avatar
BUG:  
Burlen Loring committed
244 245 246 247 248 249 250 251 252
//----------------------------------------------------------------------------
inline
void vtkXMLDataParser::CharacterDataHandler(const char* data, int length )
{
  const unsigned int eid=this->NumberOfOpenElements-1;
  this->OpenElements[eid]->AddCharacterData(data, length);
}


253
#endif