1 2 3 4 5 6 7 8 9 10 11 12 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 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 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 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
|
/*=========================================================================
Program: ParaView
Module: vtkSMReaderFactory.cxx
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSMReaderFactory.h"
#include "vtkCallbackCommand.h"
#include "vtkClientServerStream.h"
#include "vtkObjectFactory.h"
#include "vtkProcessModule.h"
#include "vtkPVProxyDefinitionIterator.h"
#include "vtkPVXMLElement.h"
#include "vtkPVXMLParser.h"
#include "vtkSmartPointer.h"
#include "vtkSMPropertyHelper.h"
#include "vtkSMProxyDefinitionManager.h"
#include "vtkSMProxy.h"
#include "vtkSMProxyManager.h"
#include "vtkSMSession.h"
#include "vtkSMSessionProxyManager.h"
#include "vtkSMSourceProxy.h"
#include "vtkStringList.h"
#include <list>
#include <set>
#include <string>
#include <vector>
#include <sstream>
#include <vtksys/SystemTools.hxx>
#include <vtksys/RegularExpression.hxx>
#include <assert.h>
static void string_replace(std::string& string, char c, std::string str)
{
size_t cc= string.find(c);
while (cc < std::string::npos)
{
string = string.replace(cc, 1, str);
cc = string.find(c, cc + str.size());
}
}
class vtkSMReaderFactory::vtkInternals
{
public:
static std::set<std::pair<std::string,std::string> > ReaderWhitelist;
struct vtkValue
{
vtkWeakPointer<vtkSMSession> Session;
std::string Group;
std::string Name;
std::vector<std::string> Extensions;
std::vector<vtksys::RegularExpression> FilenameRegExs;
std::vector<std::string> FilenamePatterns;
std::string Description;
vtkSMSessionProxyManager* GetProxyManager(vtkSMSession* session)
{
return vtkSMProxyManager::GetProxyManager()->GetSessionProxyManager(session);
}
vtkSMProxy* GetPrototypeProxy(vtkSMSession* session, const char* groupName, const char* proxyName)
{
return session->GetSessionProxyManager()->GetPrototypeProxy(groupName, proxyName);
}
void FillInformation(vtkSMSession* session)
{
vtkSMProxy* prototype = this->GetPrototypeProxy(session, this->Group.c_str(),
this->Name.c_str());
if (!prototype || !prototype->GetHints())
{
return;
}
vtkPVXMLElement* rfHint =
prototype->GetHints()->FindNestedElementByName("ReaderFactory");
if (!rfHint)
{
return;
}
this->Extensions.clear();
const char* exts = rfHint->GetAttribute("extensions");
if (exts)
{
vtksys::SystemTools::Split(exts, this->Extensions,' ');
}
const char* filename_patterns = rfHint->GetAttribute("filename_patterns");
if (filename_patterns)
{
vtksys::SystemTools::Split(filename_patterns, this->FilenamePatterns,' ');
std::vector<std::string>::iterator iter;
// convert the wild-card based patterns to regular expressions.
for (iter = this->FilenamePatterns.begin(); iter !=
this->FilenamePatterns.end(); iter++)
{
std::string regex = *iter;
::string_replace(regex, '.', "\\.");
::string_replace(regex, '?', ".");
::string_replace(regex, '*', ".?");
this->FilenameRegExs.push_back(vtksys::RegularExpression(regex.c_str()));
}
}
this->Description = rfHint->GetAttribute("file_description");
}
// Returns true is a prototype proxy can be created on the given connection.
// For now, the connection is totally ignored since ServerManager doesn't
// support that.
bool CanCreatePrototype(vtkSMSession* session)
{
return (this->GetPrototypeProxy(session,
this->Group.c_str(), this->Name.c_str())
!= NULL);
}
// Returns true if the reader can read the file. More correctly, it returns
// false is the reader reports that it cannot read the file.
bool CanReadFile(const char* filename,
const std::vector<std::string>& extensions, vtkSMSession* session,
bool skip_filename_test=false);
// Tests if 'any' of the strings in extensions is contained in
// this->Extensions.
bool ExtensionTest(const std::vector<std::string>& extensions);
// Tests if the FilenameRegEx matches the filename.
bool FilenameRegExTest(const char* filename);
};
void BuildExtensions(
const char* filename, std::vector<std::string>& extensions)
{
// basically we are filling up extensions with all possible extension
// combintations eg. myfilename.tar.gz.vtk.000 results in
// 000, vtk.000, gz.vtk.000, tar.gz.vtk.000,
// vtk, gz.vtk, tar.gz.vtk
// gz, tar.gz
// tar, tar.gz
// gz
// in that order.
std::string extension =
vtksys::SystemTools::GetFilenameExtension(filename);
if (extension.size() > 0)
{
extension.erase(extension.begin()); // remove the first "."
}
std::vector<std::string> parts;
vtksys::SystemTools::Split(extension.c_str(), parts, '.');
int num_parts = static_cast<int>(parts.size());
for (int cc=num_parts-1; cc >= 0; cc--)
{
for (int kk=cc; kk >=0; kk--)
{
std::string cur_string;
for (int ii=kk; ii <=cc; ii++)
{
if (parts[ii].size() == 0)
{
continue;//skip empty parts.
}
if (ii != kk)
{
cur_string += ".";
}
cur_string += parts[ii];
}
extensions.push_back(cur_string);
}
}
}
// we use a map here instead of a set because I'm avoiding const
// correctness of the methods of vtkValue. The key is a
// combination of the prototype name and group.
typedef std::map<std::string, vtkValue> PrototypesType;
PrototypesType Prototypes;
std::string SupportedFileTypes;
// The set of groups that are searched for readers. By default "sources" is
// included.
std::set<std::string> Groups;
};
std::set<std::pair<std::string,std::string> > vtkSMReaderFactory::vtkInternals::ReaderWhitelist;
//----------------------------------------------------------------------------
bool vtkSMReaderFactory::vtkInternals::vtkValue::ExtensionTest(
const std::vector<std::string>& extensions)
{
if (this->Extensions.size() == 0)
{
return false;
}
std::vector<std::string>::const_iterator iter1;
for (iter1 = extensions.begin(); iter1 != extensions.end(); ++iter1)
{
std::vector<std::string>::const_iterator iter2;
for (iter2 = this->Extensions.begin(); iter2 != this->Extensions.end();
++iter2)
{
if (*iter1 == *iter2)
{
return true;
}
}
}
return false;
}
//----------------------------------------------------------------------------
bool vtkSMReaderFactory::vtkInternals::vtkValue::FilenameRegExTest(
const char* filename)
{
if (this->FilenameRegExs.size() == 0)
{
return false;
}
std::vector<vtksys::RegularExpression>::iterator iter;
for (iter = this->FilenameRegExs.begin();
iter != this->FilenameRegExs.end(); ++iter)
{
if (iter->find(filename))
{
return true;
}
}
return false;
}
//----------------------------------------------------------------------------
bool vtkSMReaderFactory::vtkInternals::vtkValue::CanReadFile(
const char* filename,
const std::vector<std::string>& extensions,
vtkSMSession* session,
bool skip_filename_test/*=false*/)
{
vtkSMSessionProxyManager* pxm = this->GetProxyManager(session);
vtkSMProxy* prototype = this->GetPrototypeProxy( session,
this->Group.c_str(),
this->Name.c_str() );
if (!prototype)
{
return false;
}
if (!skip_filename_test)
{
this->FillInformation(session);
if (!this->ExtensionTest(extensions) &&
!this->FilenameRegExTest(filename))
{
return false;
}
}
vtkSMProxy* proxy = pxm->NewProxy(this->Group.c_str(), this->Name.c_str());
proxy->SetLocation(vtkProcessModule::DATA_SERVER_ROOT);
// we deliberate don't call UpdateVTKObjects() here since CanReadFile() can
// avoid callind UpdateVTKObjects() all together if not needed.
// proxy->UpdateVTKObjects();
bool canRead = vtkSMReaderFactory::CanReadFile(filename, proxy);
proxy->Delete();
return canRead;
}
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkSMReaderFactory);
//----------------------------------------------------------------------------
vtkSMReaderFactory::vtkSMReaderFactory()
{
this->Internals = new vtkInternals();
this->Internals->Groups.insert("sources");
this->Readers = vtkStringList::New();
this->ReaderName = 0;
this->ReaderGroup = 0;
}
//----------------------------------------------------------------------------
vtkSMReaderFactory::~vtkSMReaderFactory()
{
delete this->Internals;
this->SetReaderName(0);
this->SetReaderGroup(0);
this->Readers->Delete();
this->Readers = 0;
}
//----------------------------------------------------------------------------
void vtkSMReaderFactory::Initialize()
{
this->Internals->Prototypes.clear();
this->Internals->Groups.clear();
this->Internals->Groups.insert("sources");
}
//----------------------------------------------------------------------------
unsigned int vtkSMReaderFactory::GetNumberOfRegisteredPrototypes()
{
return static_cast<unsigned int>(this->Internals->Prototypes.size());
}
void vtkSMReaderFactory::UpdateAvailableReaders()
{
vtkSMProxyManager* proxyManager = vtkSMProxyManager::GetProxyManager();
// when we change the server we may not have a session yet. that's ok
// since we'll come back here after the proxy definitions are loaded
// from that session.
if(vtkSMSession* session = proxyManager->GetActiveSession())
{
vtkSMSessionProxyManager* sessionProxyManager = session->GetSessionProxyManager();
vtkSMProxyDefinitionManager* pdm = sessionProxyManager->GetProxyDefinitionManager();
for(std::set<std::string>::iterator group=this->Internals->Groups.begin();
group!=this->Internals->Groups.end();group++)
{
vtkPVProxyDefinitionIterator* iter =
pdm->NewSingleGroupIterator(group->c_str());
for (iter->GoToFirstItem(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
{
vtkPVXMLElement* hints = sessionProxyManager->GetProxyHints(
iter->GetGroupName(), iter->GetProxyName());
if (hints && hints->FindNestedElementByName("ReaderFactory"))
{
// By default this does no filtering on the readers available. However, if the
// application has specified that it is only interested in a subset of the readers
// then only that subset will be available.
std::pair<std::string,std::string> reader(iter->GetGroupName(), iter->GetProxyName());
if (vtkInternals::ReaderWhitelist.empty() ||
vtkInternals::ReaderWhitelist.find(reader) != vtkInternals::ReaderWhitelist.end())
{
this->RegisterPrototype(iter->GetGroupName(), iter->GetProxyName());
}
}
}
iter->Delete();
}
}
}
//----------------------------------------------------------------------------
void vtkSMReaderFactory::AddGroup(const char* groupName)
{
if(groupName)
{
this->Internals->Groups.insert(groupName);
}
}
//----------------------------------------------------------------------------
void vtkSMReaderFactory::RemoveGroup(const char* groupName)
{
if(groupName)
{
this->Internals->Groups.erase(groupName);
}
}
//----------------------------------------------------------------------------
void vtkSMReaderFactory::GetGroups(vtkStringList* groups)
{
if(groups)
{
groups->RemoveAllItems();
for(std::set<std::string>::iterator group=this->Internals->Groups.begin();
group!=this->Internals->Groups.end();group++)
{
groups->AddString(group->c_str());
}
}
}
//----------------------------------------------------------------------------
void vtkSMReaderFactory::RegisterPrototype(const char* xmlgroup, const char* xmlname)
{
vtkInternals::vtkValue value;
value.Group = xmlgroup;
value.Name = xmlname;
std::string key = value.Name+value.Group;
this->Internals->Prototypes[key] = value;
}
//----------------------------------------------------------------------------
vtkStringList* vtkSMReaderFactory::GetReaders(vtkSMSession* session)
{
return this->GetPossibleReaders(NULL, session);
}
//----------------------------------------------------------------------------
vtkStringList* vtkSMReaderFactory::GetReaders(const char* filename,
vtkSMSession* session)
{
this->Readers->RemoveAllItems();
if (!filename || filename[0] == 0)
{
return this->Readers;
}
std::vector<std::string> extensions;
this->Internals->BuildExtensions(filename, extensions);
vtkInternals::PrototypesType::iterator iter;
for (iter = this->Internals->Prototypes.begin();
iter != this->Internals->Prototypes.end(); ++iter)
{
if (iter->second.CanCreatePrototype(session) &&
iter->second.CanReadFile(filename, extensions, session))
{
iter->second.FillInformation(session);
this->Readers->AddString(iter->second.Group.c_str());
this->Readers->AddString(iter->second.Name.c_str());
this->Readers->AddString(iter->second.Description.c_str());
}
}
return this->Readers;
}
//----------------------------------------------------------------------------
vtkStringList* vtkSMReaderFactory::GetPossibleReaders(const char* filename,
vtkSMSession* session)
{
this->Readers->RemoveAllItems();
bool empty_filename = (!filename || filename[0] == 0);
std::vector<std::string> extensions;
// purposefully set the extensions to empty, since we don't want the extension
// test to be used for this case.
vtkInternals::PrototypesType::iterator iter;
for (iter = this->Internals->Prototypes.begin();
iter != this->Internals->Prototypes.end(); ++iter)
{
if (iter->second.CanCreatePrototype(session) &&
(empty_filename || iter->second.CanReadFile(filename, extensions, session, true)))
{
iter->second.FillInformation(session);
this->Readers->AddString(iter->second.Group.c_str());
this->Readers->AddString(iter->second.Name.c_str());
this->Readers->AddString(iter->second.Description.c_str());
}
}
return this->Readers;
}
//----------------------------------------------------------------------------
bool vtkSMReaderFactory::CanReadFile(const char* filename, vtkSMSession* session)
{
this->SetReaderGroup(0);
this->SetReaderName(0);
if (!filename || filename[0] == 0)
{
return false;
}
std::vector<std::string> extensions;
this->Internals->BuildExtensions(filename, extensions);
vtkInternals::PrototypesType::iterator iter;
for (iter = this->Internals->Prototypes.begin();
iter != this->Internals->Prototypes.end(); ++iter)
{
if (iter->second.CanCreatePrototype(session) &&
iter->second.CanReadFile(filename, extensions, session))
{
this->SetReaderGroup(iter->second.Group.c_str());
this->SetReaderName(iter->second.Name.c_str());
return true;
}
}
return false;
}
//----------------------------------------------------------------------------
static std::string vtkJoin(
const std::vector<std::string> exts, const char* prefix,
const char* suffix)
{
std::ostringstream stream;
std::vector<std::string>::const_iterator iter;
for (iter = exts.begin(); iter != exts.end(); ++iter)
{
stream << prefix << *iter << suffix;
}
return stream.str();
}
//----------------------------------------------------------------------------
const char* vtkSMReaderFactory::GetSupportedFileTypes(vtkSMSession* session)
{
std::ostringstream all_types;
all_types << "Supported Files (";
std::set<std::string> sorted_types;
vtkInternals::PrototypesType::iterator iter;
for (iter = this->Internals->Prototypes.begin();
iter != this->Internals->Prototypes.end(); ++iter)
{
if (iter->second.CanCreatePrototype(session))
{
iter->second.FillInformation(session);
std::string ext_list;
if (iter->second.Extensions.size() > 0)
{
ext_list = ::vtkJoin(iter->second.Extensions, "*.", " ");
}
if (iter->second.FilenameRegExs.size() > 0)
{
std::string ext_join = ::vtkJoin(
iter->second.FilenamePatterns, "", " ");
if (ext_list.size() > 0)
{
ext_list += " ";
ext_list += ext_join;
}
else
{
ext_list = ext_join;
}
}
if (ext_list.size() > 0)
{
std::ostringstream stream;
stream << iter->second.Description << "(" << ext_list << ")";
sorted_types.insert(stream.str());
all_types << ext_list << " ";
}
}
}
all_types << ")";
std::set<std::string>::iterator iter2;
for (iter2 = sorted_types.begin(); iter2 != sorted_types.end(); ++iter2)
{
all_types << ";;" << (*iter2);
}
this->Internals->SupportedFileTypes = all_types.str();
return this->Internals->SupportedFileTypes.c_str();
}
//----------------------------------------------------------------------------
bool vtkSMReaderFactory::TestFileReadability(const char* filename, vtkSMSession* session)
{
assert("Session should be valid" && session);
vtkSMSessionProxyManager* pxm =
vtkSMProxyManager::GetProxyManager()->GetSessionProxyManager(session);
vtkSmartPointer<vtkSMProxy> proxy;
proxy.TakeReference(pxm->NewProxy("file_listing", "ServerFileListing"));
if (!proxy)
{
vtkGenericWarningMacro("Failed to create ServerFileListing proxy.");
return false;
}
proxy->SetLocation(vtkProcessModule::DATA_SERVER_ROOT);
vtkSMPropertyHelper(proxy, "ActiveFileName").Set(filename);
proxy->UpdateVTKObjects();
proxy->UpdatePropertyInformation();
if (vtkSMPropertyHelper(proxy, "ActiveFileIsReadable").GetAsInt() != 0)
{
return true;
}
return false;
}
//----------------------------------------------------------------------------
bool vtkSMReaderFactory::CanReadFile(const char* filename, vtkSMProxy* proxy)
{
// Assume that it can read the file if CanReadFile does not exist.
int canRead = 1;
vtkSMSession* session = proxy->GetSession();
vtkSMSourceProxy* source = vtkSMSourceProxy::SafeDownCast(proxy);
// first check if the source requires MPI to be initialized and
// that it is initialized on the server.
if (source && source->GetMPIRequired() &&
session->IsMPIInitialized(source->GetLocation()) == false)
{
return false;
}
// Check if the source requires multiple processes and if we have
// multiple processes on the server.
if (source && session->GetNumberOfProcesses(source->GetLocation()) > 1)
{
if (source->GetProcessSupport() == vtkSMSourceProxy::SINGLE_PROCESS)
{
return false;
}
}
else
{
if (source->GetProcessSupport() == vtkSMSourceProxy::MULTIPLE_PROCESSES)
{
return false;
}
}
// ensure that VTK objects are created.
proxy->UpdateVTKObjects();
vtkClientServerStream stream;
stream << vtkClientServerStream::Invoke
<< VTKOBJECT(proxy)
<< "CanReadFile" << filename
<< vtkClientServerStream::End;
session->ExecuteStream(proxy->GetLocation(), stream, /*ignore_error*/true);
session->GetLastResult(proxy->GetLocation()).GetArgument(0, 0, &canRead);
return (canRead != 0);
}
//----------------------------------------------------------------------------
bool vtkSMReaderFactory::CanReadFile(const char* filename,
const char* readerxmlgroup, const char* readerxmlname, vtkSMSession* session)
{
assert("Session should be valid" && session);
vtkSMSessionProxyManager* pxm =
vtkSMProxyManager::GetProxyManager()->GetSessionProxyManager(session);
vtkSMProxy* proxy = pxm->NewProxy( readerxmlgroup, readerxmlname );
if (!proxy)
{
return false;
}
proxy->SetLocation(vtkProcessModule::DATA_SERVER_ROOT);
bool canRead = vtkSMReaderFactory::CanReadFile(filename, proxy);
proxy->Delete();
return canRead;
}
//----------------------------------------------------------------------------
void vtkSMReaderFactory::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
void vtkSMReaderFactory::AddReaderToWhitelist(const char* readerxmlgroup,
const char* readerxmlname)
{
if (readerxmlgroup != NULL && readerxmlname != NULL)
{
vtkSMReaderFactory::vtkInternals::ReaderWhitelist.insert(
std::pair<std::string,std::string>(readerxmlgroup,readerxmlname));
}
}
|