vtkXMLReader.cxx 29.4 KB
Newer Older
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkXMLReader.cxx

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

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkXMLReader.h"

#include "vtkCallbackCommand.h"
#include "vtkDataArray.h"
#include "vtkDataArraySelection.h"
#include "vtkDataCompressor.h"
#include "vtkDataSet.h"
#include "vtkDataSetAttributes.h"
#include "vtkInstantiator.h"
#include "vtkObjectFactory.h"
#include "vtkXMLDataElement.h"
#include "vtkXMLDataParser.h"
#include "vtkXMLFileReadTester.h"
28
#include "vtkZLibDataCompressor.h"
29 30 31
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkStreamingDemandDrivenPipeline.h"
32 33

#include <sys/stat.h>
34
#include <assert.h>
35

36
vtkCxxRevisionMacro(vtkXMLReader, "1.27");
37 38 39 40 41

//----------------------------------------------------------------------------
vtkXMLReader::vtkXMLReader()
{
  this->FileName = 0;
42
  this->Stream = 0;
43
  this->FileStream = 0;
44
  this->XMLParser = 0;
45
  this->FieldDataElement = 0;
46 47
  this->PointDataArraySelection = vtkDataArraySelection::New();
  this->CellDataArraySelection = vtkDataArraySelection::New();
48 49
  this->InformationError = 0;
  this->DataError = 0;
50
  this->ReadError = 0;
51
  this->CurrentOutput = -1;
52 53
  this->ProgressRange[0] = 0;
  this->ProgressRange[1] = 1;
54 55 56 57 58 59 60 61 62
  
  // Setup the selection callback to modify this object when an array
  // selection is changed.
  this->SelectionObserver = vtkCallbackCommand::New();
  this->SelectionObserver->SetCallback(&vtkXMLReader::SelectionModifiedCallback);
  this->SelectionObserver->SetClientData(this);
  this->PointDataArraySelection->AddObserver(vtkCommand::ModifiedEvent,
                                             this->SelectionObserver);
  this->CellDataArraySelection->AddObserver(vtkCommand::ModifiedEvent,
63 64 65
                                            this->SelectionObserver); 
  this->SetNumberOfInputPorts(0);
  this->SetNumberOfOutputPorts(1);
66 67 68 69 70 71 72 73 74

  // Time support:
  this->TimeStep = 0; // By default the file does not have timestep
  this->TimeStepRange[0] = 0;
  this->TimeStepRange[1] = 0;
  this->NumberOfTimeSteps = 0;
  this->TimeSteps = 0;
  this->CurrentTimeStep = 0;
  this->FileWasReadOnce = 0;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
}

//----------------------------------------------------------------------------
vtkXMLReader::~vtkXMLReader()
{
  this->SetFileName(0);
  if(this->XMLParser)
    {
    this->DestroyXMLParser();
    }
  this->CellDataArraySelection->RemoveObserver(this->SelectionObserver);
  this->PointDataArraySelection->RemoveObserver(this->SelectionObserver);
  this->SelectionObserver->Delete();
  this->CellDataArraySelection->Delete();
  this->PointDataArraySelection->Delete();
90
  delete[] this->TimeSteps;
91 92 93 94 95 96 97 98 99
}

//----------------------------------------------------------------------------
void vtkXMLReader::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "FileName: "
     << (this->FileName? this->FileName:"(none)") << "\n";
  os << indent << "CellDataArraySelection: " << this->CellDataArraySelection 
100
     << "\n";
101
  os << indent << "PointDataArraySelection: " << this->PointDataArraySelection 
102 103 104 105 106 107 108 109 110
     << "\n";
  if(this->Stream)
    {
    os << indent << "Stream: " << this->Stream << "\n";
    }
  else
    {
    os << indent << "Stream: (none)\n";
    }
111 112 113 114 115
}

//----------------------------------------------------------------------------
vtkDataSet* vtkXMLReader::GetOutputAsDataSet()
{
116 117 118 119 120 121
  return this->GetOutputAsDataSet(0);
}

//----------------------------------------------------------------------------
vtkDataSet* vtkXMLReader::GetOutputAsDataSet(int index)
{
122
  return vtkDataSet::SafeDownCast( this->GetOutputDataObject(index) );
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
}

//----------------------------------------------------------------------------
int vtkXMLReader::CanReadFileVersion(int, int)
{
  return 1;
}

//----------------------------------------------------------------------------
int vtkXMLReader::OpenVTKFile()
{
  if(this->FileStream)
    {
    vtkErrorMacro("File already open.");
    return 1;
    }
139 140
  
  if(!this->Stream && !this->FileName)
Andy Cedilnik's avatar
Andy Cedilnik committed
141 142 143 144
    {
    vtkErrorMacro("File name not specified");
    return 0;
    }
145
  
146 147 148 149 150 151 152 153
  if(this->Stream)
    {
    // Use user-provided stream.
    return 1;
    }
  
  // Need to open a file.  First make sure it exists.  This prevents
  // an empty file from being created on older compilers.
154 155 156 157 158 159 160 161
  struct stat fs;
  if(stat(this->FileName, &fs) != 0)
    {
    vtkErrorMacro("Error opening file " << this->FileName);
    return 0;
    }
  
#ifdef _WIN32
162
  this->FileStream = new ifstream(this->FileName, ios::binary | ios::in);
163
#else
164
  this->FileStream = new ifstream(this->FileName, ios::in);
165 166
#endif
  
167
  if(!this->FileStream || !(*this->FileStream))
168 169
    {
    vtkErrorMacro("Error opening file " << this->FileName);
170 171 172 173 174
    if(this->FileStream)
      {
      delete this->FileStream;
      this->FileStream = 0;
      }
175 176
    return 0;
    }
177 178 179 180
  
  // Use the file stream.
  this->Stream = this->FileStream;
  
181 182 183 184 185 186
  return 1;
}

//----------------------------------------------------------------------------
void vtkXMLReader::CloseVTKFile()
{
187
  if(!this->Stream)
188 189 190 191
    {
    vtkErrorMacro("File not open.");
    return;
    }
192 193 194 195 196 197 198 199
  if(this->Stream == this->FileStream)
    {
    // We opened the file.  Close it.
    this->FileStream->close();
    delete this->FileStream;
    this->FileStream = 0;
    this->Stream = 0;
    }
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
}

//----------------------------------------------------------------------------
void vtkXMLReader::CreateXMLParser()
{
  if(this->XMLParser)
    {
    vtkErrorMacro("CreateXMLParser() called with existing XMLParser.");
    this->DestroyXMLParser();
    }
  this->XMLParser = vtkXMLDataParser::New();
}

//----------------------------------------------------------------------------
void vtkXMLReader::DestroyXMLParser()
{
  if(!this->XMLParser)
    {
    vtkErrorMacro("DestroyXMLParser() called with no current XMLParser.");
    return;
    }
  this->XMLParser->Delete();
  this->XMLParser = 0;
}

//----------------------------------------------------------------------------
void vtkXMLReader::SetupCompressor(const char* type)
{
  // Instantiate a compressor of the given type.
  if(!type)
    {
    vtkErrorMacro("Compressor has no type.");
    return;
    }
  vtkObject* object = vtkInstantiator::CreateInstance(type);
  vtkDataCompressor* compressor = vtkDataCompressor::SafeDownCast(object);
236 237 238 239 240 241 242 243
  
  // In static builds, the vtkZLibDataCompressor may not have been
  // registered with the vtkInstantiator.  Check for it here.
  if(!compressor && (strcmp(type, "vtkZLibDataCompressor") == 0))
    {
    compressor = vtkZLibDataCompressor::New();
    }
  
244 245 246
  if(!compressor)
    {
    vtkErrorMacro("Error creating " << type);
247 248 249 250
    if(object)
      {
      object->Delete();
      }
251 252 253 254 255 256
    return;
    }
  this->XMLParser->SetCompressor(compressor);
  compressor->Delete();
}

257 258


259
//----------------------------------------------------------------------------
260
int vtkXMLReader::ReadXMLInformation()
261
{
262 263
  // only Parse if something has changed
  if(this->GetMTime() > this->ReadMTime)
264
    {
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    // Destroy any old information that was parsed.
    if(this->XMLParser)
      {
      this->DestroyXMLParser();
      }

    // Open the input file.  If it fails, the error was already
    // reported by OpenVTKFile.
    if(!this->OpenVTKFile())
      {
      this->SetupEmptyOutput();
      return 0;
      }

    // Create the vtkXMLParser instance used to parse the file.
    this->CreateXMLParser();

    // Configure the parser for this file.
    this->XMLParser->SetStream(this->Stream);

    // Parse the input file.
    if(this->XMLParser->Parse())
      {
      // Let the subclasses read the information they want.
      if(!this->ReadVTKFile(this->XMLParser->GetRootElement()))
        {
        // There was an error reading the file.
        this->ReadError = 1;
        }
      else
        {
        this->ReadError = 0;
        }
      }
    else
      {
      vtkErrorMacro("Error parsing input file.  ReadXMLInformation aborting.");
      // The output should be empty to prevent the rest of the pipeline
      // from executing.
      this->ReadError = 1;
      this->SetupEmptyOutput();
      }

    // Close the file to prevent resource leaks.
    this->CloseVTKFile();

    this->ReadMTime.Modified();
312
    }
313 314 315 316 317
  return !this->ReadError;
}

//----------------------------------------------------------------------------
int vtkXMLReader::RequestInformation(vtkInformation *request,
318 319
                                     vtkInformationVector **vtkNotUsed(inputVector),
                                     vtkInformationVector *outputVector)
320 321
{
  if (this->ReadXMLInformation())
322
    {
323
    this->InformationError = 0;
324
    // Let the subclasses read the information they want.
325 326 327
    int outputPort = request->Get( vtkDemandDrivenPipeline::FROM_OUTPUT_PORT() );
    outputPort = outputPort >= 0 ? outputPort : 0;
    this->SetupOutputInformation( outputVector->GetInformationObject(outputPort) );
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

    // this->NumberOfTimeSteps has been set during the this->ReadXMLInformation()
    int numTimesteps = this->GetNumberOfTimeSteps();
    this->TimeStepRange[0] = 0;
    this->TimeStepRange[1] = numTimesteps-1;
    if (numTimesteps != 0)
      {
      double* timeSteps = new double[numTimesteps];
      for (int i=0; i<numTimesteps; i++)
        {
        timeSteps[i] = i;
        }
      vtkInformation* outInfo = outputVector->GetInformationObject(0);
      outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(), 
                   timeSteps, 
                   numTimesteps);
      }
345 346 347
    }
  else
    {
348
    this->InformationError = 1;
349
    }
350

351
  return !this->InformationError;
352 353 354
}

//----------------------------------------------------------------------------
355
int vtkXMLReader::RequestData(vtkInformation *request, 
356
                              vtkInformationVector **vtkNotUsed(inputVector),
357
                              vtkInformationVector *outputVector)
358
{
359 360
  // Set which output we are updating.  If the given object is not one
  // of our outputs, just initialize it to empty and return.
361
  this->CurrentOutput = request->Get( vtkDemandDrivenPipeline::FROM_OUTPUT_PORT() );
362 363
  if(this->CurrentOutput < 0)
    {
364 365
    this->GetExecutive()->GetOutputData(0)->Initialize();
    return 0;
366
    }
367

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  this->CurrentTimeStep = this->TimeStep;

  vtkInformation* outInfo = outputVector->GetInformationObject(0);
  // get the ouptut
  vtkDataObject *output = outInfo->Get(vtkDataObject::DATA_OBJECT());

  if (outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_INDEX()))
    {
    this->CurrentTimeStep = outInfo->Get(
      vtkStreamingDemandDrivenPipeline::UPDATE_TIME_INDEX());

    // save the time and time index in the data object's information
    output->GetInformation()->Set(
      vtkDataObject::DATA_TIME(), 
      outInfo->Get(vtkStreamingDemandDrivenPipeline::TIME_STEPS())[this->CurrentTimeStep]);
    output->GetInformation()->Set(vtkDataObject::DATA_TIME_INDEX(), this->CurrentTimeStep);
    }

  if ( this->CurrentTimeStep < this->TimeStepRange[0] )
    {
    this->CurrentTimeStep = this->TimeStepRange[0];
    }
  else if ( this->CurrentTimeStep > this->TimeStepRange[1] )
    {
    this->CurrentTimeStep = this->TimeStepRange[1];
    }

395 396 397 398
  // Re-open the input file.  If it fails, the error was already
  // reported by OpenVTKFile.
  if(!this->OpenVTKFile())
    {
399
    this->SetupEmptyOutput();
400
    return 0;
401 402 403 404 405 406 407 408
    }
  if(!this->XMLParser)
    {
    vtkErrorMacro("ExecuteData called with no current XMLParser.");
    }
  
  // Give the vtkXMLParser instance its file back so that data section
  // reads will work.
409
  this->XMLParser->SetStream(this->Stream);
410
  
411 412 413 414 415 416 417
  // We are just starting to read.  Do not call UpdateProgressDiscrete
  // because we want a 0 progress callback the first time.
  this->UpdateProgress(0);
  
  // Initialize progress range to entire 0..1 range.
  float wholeProgressRange[2] = {0,1};
  this->SetProgressRange(wholeProgressRange, 0, 1);
418
  
419 420
  if(!this->InformationError)
    {
421 422
    // We are just starting to execute.  No errors have yet occurred.
    this->XMLParser->SetAbort(0);
423
    this->DataError = 0;
424

425
    // Let the subclasses read the data they want.
426 427
    this->ReadXMLData();
    
428 429
    // If we aborted or there was an error, provide empty output.
    if(this->DataError || this->AbortExecute)
430
      {
431
      this->GetOutputAsDataSet(this->CurrentOutput)->Initialize();
432 433 434 435 436
      }
    }
  else
    {
    // There was an error reading the file.  Provide empty output.
437
    this->GetOutputAsDataSet(this->CurrentOutput)->Initialize();
438
    }
439
  
440 441 442
  // We have finished reading.
  this->UpdateProgressDiscrete(1);
  
443 444
  // Close the file to prevent resource leaks.
  this->CloseVTKFile();
445 446
  this->FileWasReadOnce = 1;

447
  return 1;
448 449 450 451 452 453
}


//----------------------------------------------------------------------------
void vtkXMLReader::ReadXMLData()
{
454
  // Initialize the output's data.
455 456 457 458
  if( !this->FileWasReadOnce )
    {
    this->SetupOutputData();
    }
459 460 461 462 463 464 465
}

//----------------------------------------------------------------------------
int vtkXMLReader::ReadVTKFile(vtkXMLDataElement* eVTKFile)
{
  // Setup the compressor if there is one.
  const char* compressor = eVTKFile->GetAttribute("compressor");
466 467 468 469
  if(compressor)
    {
    this->SetupCompressor(compressor);
    }
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
  
  // Get the primary element.
  const char* name = this->GetDataSetName();
  int i;
  vtkXMLDataElement* ePrimary = 0;
  for(i=0; i < eVTKFile->GetNumberOfNestedElements(); ++i)
    {
    vtkXMLDataElement* eNested = eVTKFile->GetNestedElement(i);
    if(strcmp(eNested->GetName(), name) == 0)
      {
      ePrimary = eNested;
      break;
      }
    }
  if(!ePrimary)
    {
    vtkErrorMacro("Cannot find " << name << " element in file.");
    return 0;
    }
  
  // Read the primary element.
  return this->ReadPrimaryElement(ePrimary);
}

//----------------------------------------------------------------------------
495
int vtkXMLReader::ReadPrimaryElement(vtkXMLDataElement* ePrimary)
496
{
497 498 499 500 501 502 503 504 505
  // We don't need any information from the primary element here.
  //
  //
  // Let check the "TimeValues" here
  double timevalues[512];
  int numTimeSteps = ePrimary->GetVectorAttribute("TimeValues", timevalues);
  this->SetNumberOfTimeSteps( numTimeSteps );
  assert( numTimeSteps < 512 );

506 507
  // See if there is a FieldData element
  int numNested = ePrimary->GetNumberOfNestedElements();
508
  for(int i=0; i < numNested; ++i)
509 510 511 512 513 514 515 516 517 518
    {
    vtkXMLDataElement* eNested = ePrimary->GetNestedElement(i);
    if(strcmp(eNested->GetName(), "FieldData") == 0) 
      {
      this->FieldDataElement = eNested;
      return 1;
      }
    }
  
  this->FieldDataElement = 0;
519

520 521 522
  return 1;
}

523

524 525 526
//----------------------------------------------------------------------------
void vtkXMLReader::SetupOutputData()
{
527 528 529 530 531 532
  // Initialize the output.
  int i;
  for(i=0; i < this->GetNumberOfOutputPorts(); ++i)
    {
    this->GetExecutive()->GetOutputData(i)->Initialize();
    }
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
}

//----------------------------------------------------------------------------
vtkDataArray* vtkXMLReader::CreateDataArray(vtkXMLDataElement* da)
{
  int dataType = 0;
  if(!da->GetWordTypeAttribute("type", dataType))
    {
    return 0;
    }  
  
  vtkDataArray* array = vtkDataArray::CreateDataArray(dataType);
  
  array->SetName(da->GetAttribute("Name"));
  
  int components;
  if(da->GetScalarAttribute("NumberOfComponents", components))
    {
    array->SetNumberOfComponents(components);
    }
  
  return array;
}

//----------------------------------------------------------------------------
int vtkXMLReader::CanReadFile(const char* name)
{
  // First make sure the file exists.  This prevents an empty file
  // from being created on older compilers.
  struct stat fs;
563 564 565 566
  if(stat(name, &fs) != 0) 
    { 
    return 0; 
    }
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
  
  // Test if the file with the given name is a VTKFile with the given
  // type.
  vtkXMLFileReadTester* tester = vtkXMLFileReadTester::New();
  tester->SetFileName(name);
  
  int result = 0;
  if(tester->TestReadFile() && tester->GetFileDataType())
    {
    if(strcmp(tester->GetFileDataType(), this->GetDataSetName()) == 0)
      {
      const char* version = tester->GetFileVersion();
      if(version)
        {
        if(this->CanReadFileVersionString(version))
          {
583
          result = 3;
584 585 586 587
          }
        }
      else
        {
588
        result = 3;
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
        }
      }
    }
  
  tester->Delete();
  return result;
}

//----------------------------------------------------------------------------
int vtkXMLReader::CanReadFileVersionString(const char* version)
{
  // Extract the major and minor version numbers.
  size_t length = strlen(version);
  int major = 0;
  int minor = 0;
  const char* begin = version;
  const char* end = version + length;
  const char* s;
  
  for(s=begin; (s != end) && (*s != '.'); ++s);
  
  if(s > begin)
    {
    strstream str;
    str.write(begin, s-begin);
    str << ends;
    str >> major;
    if(!str)
      {
      major = 0;
      }
    }
  if(++s < end)
    {
    strstream str;
    str.write(s, end-s);
    str << ends;
    str >> minor;
    if(!str)
      {
      minor = 0;
      }
    }
  
  return this->CanReadFileVersion(major, minor);
}

//----------------------------------------------------------------------------
int vtkXMLReader::IntersectExtents(int* extent1, int* extent2, int* result)
{
  if((extent1[0] > extent2[1]) || (extent1[2] > extent2[3]) ||
     (extent1[4] > extent2[5]) || (extent1[1] < extent2[0]) ||
     (extent1[3] < extent2[2]) || (extent1[5] < extent2[4]))
    {
    // No intersection of extents.
    return 0;
    }
  
  // Get the intersection of the extents.
  result[0] = this->Max(extent1[0], extent2[0]);
  result[1] = this->Min(extent1[1], extent2[1]);
  result[2] = this->Max(extent1[2], extent2[2]);
  result[3] = this->Min(extent1[3], extent2[3]);
  result[4] = this->Max(extent1[4], extent2[4]);
  result[5] = this->Min(extent1[5], extent2[5]);
  
  return 1;
}

//----------------------------------------------------------------------------
int vtkXMLReader::Min(int a, int b)
{
  return (a < b)? a : b;
}

//----------------------------------------------------------------------------
int vtkXMLReader::Max(int a, int b)
{
  return (a > b)? a : b;
}

//----------------------------------------------------------------------------
void vtkXMLReader::ComputeDimensions(int* extent, int* dimensions, int isPoint)
{
  dimensions[0] = extent[1] - extent[0] + isPoint;
  dimensions[1] = extent[3] - extent[2] + isPoint;
  dimensions[2] = extent[5] - extent[4] + isPoint;
}

//----------------------------------------------------------------------------
679 680
void vtkXMLReader::ComputeIncrements(int* extent, vtkIdType* increments,
                                     int isPoint)
681 682 683 684 685 686 687 688 689
{
  int dimensions[3];
  this->ComputeDimensions(extent, dimensions, isPoint);
  increments[0] = 1;
  increments[1] = increments[0]*dimensions[0];
  increments[2] = increments[1]*dimensions[1];
}

//----------------------------------------------------------------------------
690 691
vtkIdType vtkXMLReader::GetStartTuple(int* extent, vtkIdType* increments,
                                      int i, int j, int k)
692
{
693
  vtkIdType offset = (i - extent[0]) * increments[0];
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
  offset += (j - extent[2]) * increments[1];
  offset += (k - extent[4]) * increments[2];
  return offset;
}

//----------------------------------------------------------------------------
void vtkXMLReader::ReadAttributeIndices(vtkXMLDataElement* eDSA,
                                        vtkDataSetAttributes* dsa)
{
  // Setup attribute indices.
  int i;
  for(i=0;i < vtkDataSetAttributes::NUM_ATTRIBUTES;++i)
    {
    const char* attrName = vtkDataSetAttributes::GetAttributeTypeAsString(i);
    if(eDSA && eDSA->GetAttribute(attrName))
      {
      dsa->SetActiveAttribute(eDSA->GetAttribute(attrName), i);
      }
    }
}

//----------------------------------------------------------------------------
char** vtkXMLReader::CreateStringArray(int numStrings)
{
  char** strings = new char*[numStrings];
  int i;
  for(i=0; i < numStrings; ++i)
    {
    strings[i] = 0;
    }
  return strings;
}

//----------------------------------------------------------------------------
void vtkXMLReader::DestroyStringArray(int numStrings, char** strings)
{
  int i;
  for(i=0; i < numStrings; ++i)
    {
    if(strings[i])
      {
      delete [] strings[i];
      }
    }
738
  delete [] strings;
739 740 741 742
}

//----------------------------------------------------------------------------
void vtkXMLReader::SetDataArraySelections(vtkXMLDataElement* eDSA,
743
                                          vtkDataArraySelection* sel)
744 745 746 747 748 749 750 751 752 753 754 755 756
{
  if(!eDSA)
    {
    sel->SetArrays(0, 0);
    return;
    }
  int numArrays = eDSA->GetNumberOfNestedElements();
  if(!numArrays)
    {
    sel->SetArrays(0, 0);
    return;
    }
  
757
  for(int i=0;i < numArrays;++i)
758 759
    {
    vtkXMLDataElement* eNested = eDSA->GetNestedElement(i);
760 761 762
    const char* name = eNested->GetAttribute("Name");
    if(name)
      {
763
      sel->AddArray( name );
764 765 766 767 768
      }
    else
      {
      ostrstream ostr;
      ostr << "Array " << i << ends;
769
      sel->AddArray( ostr.str() );
770
      }
771 772 773
    }
}

774

775
//----------------------------------------------------------------------------
776
int vtkXMLReader::SetFieldDataInfo(vtkXMLDataElement *eDSA, 
777 778 779
                                   int association, int numTuples,
                                   vtkInformationVector *(&infoVector))
{
780 781 782 783 784
  if (!eDSA)
    {
    return 1;
    }

785
  int i, j, components, dataType, activeFlag;
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
  const char *name;
  char *(attributeName[vtkDataSetAttributes::NUM_ATTRIBUTES]);
  vtkInformation *info = NULL;

  for(i = 0; i < vtkDataSetAttributes::NUM_ATTRIBUTES; i++)
    {
    const char* attrName = vtkDataSetAttributes::GetAttributeTypeAsString(i);
    name = eDSA->GetAttribute(attrName);
    if (name)
      {
      attributeName[i] = new char[strlen(name)+1];
      strcpy(attributeName[i], name);
      }
    else
      {
      attributeName[i] = NULL;
      }
    }

  if (!infoVector)
    {
    infoVector = vtkInformationVector::New();
    }

  // Cycle through each data array
  for(i = 0; i < eDSA->GetNumberOfNestedElements(); i++)
    {
    vtkXMLDataElement* eNested = eDSA->GetNestedElement(i);

    info = vtkInformation::New();
816
    activeFlag = 0;
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
    info->Set(vtkDataObject::FIELD_ASSOCIATION(), association);
    info->Set(vtkDataObject::FIELD_NUMBER_OF_TUPLES(), numTuples);

    name = eNested->GetAttribute( "Name" );
    if (!name)
      {
      this->InformationError = 1;
      break;
      }
    info->Set(vtkDataObject::FIELD_NAME(), name);

    // Search for matching attribute name
    for (j = 0; j < vtkDataSetAttributes::NUM_ATTRIBUTES; j++)
      {
      if (attributeName[j] && !strcmp(name, attributeName[j]))
        {
833 834
        // set appropriate bit to indicate an active attribute type
        activeFlag |= 1 << j;
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
        break;
        }
      }

    if (!eNested->GetWordTypeAttribute("type", dataType))
      {
      this->InformationError = 1;
      break;
      }
    info->Set(vtkDataObject::FIELD_ARRAY_TYPE(), dataType);

    if (eNested->GetScalarAttribute("NumberOfComponents", components))
      {
      info->Set(vtkDataObject::FIELD_NUMBER_OF_COMPONENTS(), components);
      }
    else
      {
      info->Set(vtkDataObject::FIELD_NUMBER_OF_COMPONENTS(), 1);
      }

855
    info->Set(vtkDataObject::FIELD_ACTIVE_ATTRIBUTE(), activeFlag);
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
    infoVector->Append( info );
    info->Delete();
    }

  for(i = 0; i < vtkDataSetAttributes::NUM_ATTRIBUTES; i++)
    {
    if (attributeName[i])
      {
      delete [] attributeName[i];
      }
    }

  if (this->InformationError)
    {
    info->Delete();
    infoVector->Delete();
872
    infoVector = NULL;
873 874 875 876 877 878 879
    return 0;
    }

  return 1;
  }


880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
//----------------------------------------------------------------------------
int vtkXMLReader::PointDataArrayIsEnabled(vtkXMLDataElement* ePDA)
{
  const char* name = ePDA->GetAttribute("Name");
  return (name && this->PointDataArraySelection->ArrayIsEnabled(name));
}

//----------------------------------------------------------------------------
int vtkXMLReader::CellDataArrayIsEnabled(vtkXMLDataElement* eCDA)
{
  const char* name = eCDA->GetAttribute("Name");
  return (name && this->CellDataArraySelection->ArrayIsEnabled(name));
}

//----------------------------------------------------------------------------
void vtkXMLReader::SelectionModifiedCallback(vtkObject*, unsigned long,
                                             void* clientdata, void*)
{
  static_cast<vtkXMLReader*>(clientdata)->Modified();
}

//----------------------------------------------------------------------------
int vtkXMLReader::GetNumberOfPointArrays()
{
  return this->PointDataArraySelection->GetNumberOfArrays();
}

//----------------------------------------------------------------------------
const char* vtkXMLReader::GetPointArrayName(int index)
{
  return this->PointDataArraySelection->GetArrayName(index);
}

//----------------------------------------------------------------------------
int vtkXMLReader::GetPointArrayStatus(const char* name)
{
  return this->PointDataArraySelection->ArrayIsEnabled(name);
}

//----------------------------------------------------------------------------
void vtkXMLReader::SetPointArrayStatus(const char* name, int status)
{
  if(status)
    {
    this->PointDataArraySelection->EnableArray(name);
    }
  else
    {
    this->PointDataArraySelection->DisableArray(name);
    }
}

//----------------------------------------------------------------------------
int vtkXMLReader::GetNumberOfCellArrays()
{
  return this->CellDataArraySelection->GetNumberOfArrays();
}

//----------------------------------------------------------------------------
const char* vtkXMLReader::GetCellArrayName(int index)
{
  return this->CellDataArraySelection->GetArrayName(index);
}

//----------------------------------------------------------------------------
int vtkXMLReader::GetCellArrayStatus(const char* name)
{
  return this->CellDataArraySelection->ArrayIsEnabled(name);
}

//----------------------------------------------------------------------------
void vtkXMLReader::SetCellArrayStatus(const char* name, int status)
{
  if(status)
    {
    this->CellDataArraySelection->EnableArray(name);
    }
  else
    {
    this->CellDataArraySelection->DisableArray(name);
    }
}
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979

//----------------------------------------------------------------------------
void vtkXMLReader::GetProgressRange(float* range)
{
  range[0] = this->ProgressRange[0];
  range[1] = this->ProgressRange[1];
}

//----------------------------------------------------------------------------
void vtkXMLReader::SetProgressRange(float* range, int curStep, int numSteps)
{
  float stepSize = (range[1] - range[0])/numSteps;
  this->ProgressRange[0] = range[0] + stepSize*curStep;
  this->ProgressRange[1] = range[0] + stepSize*(curStep+1);
  this->UpdateProgressDiscrete(this->ProgressRange[0]);
}

//----------------------------------------------------------------------------
980 981
void vtkXMLReader::SetProgressRange(float* range, int curStep,
                                    float* fractions)
982 983
{
  float width = range[1] - range[0];
984 985
  this->ProgressRange[0] = range[0] + fractions[curStep]*width;
  this->ProgressRange[1] = range[0] + fractions[curStep+1]*width;
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
  this->UpdateProgressDiscrete(this->ProgressRange[0]);
}

//----------------------------------------------------------------------------
void vtkXMLReader::UpdateProgressDiscrete(float progress)
{
  if(!this->AbortExecute)
    {
    // Round progress to nearest 100th.
    float rounded = float(int((progress*100)+0.5))/100;
    if(this->GetProgress() != rounded)
      {
      this->UpdateProgress(rounded);
      }
    }
}
1002 1003


1004
//----------------------------------------------------------------------------
1005
int vtkXMLReader::ProcessRequest(vtkInformation* request,
1006 1007
                                 vtkInformationVector** inputVector,
                                 vtkInformationVector* outputVector)
1008 1009
{
  // generate the data
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
  if(request->Has(vtkDemandDrivenPipeline::REQUEST_DATA_NOT_GENERATED()))
    {
    //abort();
    //return this->RequestData(request, inputVector, outputVector);
  vtkInformation* outInfo = outputVector->GetInformationObject(0);
  if ( this->CurrentOutput == 0)
    {
    outInfo->Set(vtkDemandDrivenPipeline::DATA_NOT_GENERATED(), 1);
    }
    }
  // generate the data
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
  if(request->Has(vtkDemandDrivenPipeline::REQUEST_DATA()))
    {
    return this->RequestData(request, inputVector, outputVector);
    }

  // create the output
  if(request->Has(vtkDemandDrivenPipeline::REQUEST_DATA_OBJECT()))
    {
    return this->RequestDataObject(request, inputVector, outputVector);
    }

  // execute information
  if(request->Has(vtkDemandDrivenPipeline::REQUEST_INFORMATION()))
    {
    return this->RequestInformation(request, inputVector, outputVector);
    }

  return this->Superclass::ProcessRequest(request, inputVector, outputVector);
}
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067

//----------------------------------------------------------------------------
void vtkXMLReader::SetNumberOfTimeSteps(int num)
{
  if( this->NumberOfTimeSteps != num )
    {
    this->NumberOfTimeSteps = num;
    delete[] this->TimeSteps;
    // Reallocate a buffer large enough
    this->TimeSteps = new int[num];
    assert(memset(this->TimeSteps, -1, num));
    this->Modified();
    }
}

//----------------------------------------------------------------------------
int vtkXMLReader::IsTimeStepInArray(int timestep, int* timesteps, int length)
{
  for(int i=0; i<length; i++)
    {
    if (timesteps[i] == timestep)
      {
      return 1;
      }
    }
  return 0;
}