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 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 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 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 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 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 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 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
|
// =================================================================================================
// Copyright Adobe
// Copyright 2011 Adobe
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it.
// =================================================================================================
#include "public/include/XMP_Environment.h" // ! XMP_Environment.h must be the first included header.
#include "public/include/XMP_Const.h"
#include "source/XIO.hpp"
#include "XMPFiles/source/HandlerRegistry.h"
#if EnablePluginManager
#include "XMPFiles/source/PluginHandler/XMPAtoms.h"
#endif
#if EnablePhotoHandlers
#include "XMPFiles/source/FileHandlers/JPEG_Handler.hpp"
#include "XMPFiles/source/FileHandlers/PSD_Handler.hpp"
#include "XMPFiles/source/FileHandlers/TIFF_Handler.hpp"
#include "XMPFiles/source/FileHandlers/GIF_Handler.hpp"
#endif
#if EnableDynamicMediaHandlers
#include "XMPFiles/source/FileHandlers/AIFF_Handler.hpp"
#include "XMPFiles/source/FileHandlers/ASF_Handler.hpp"
#include "XMPFiles/source/FileHandlers/FLV_Handler.hpp"
#include "XMPFiles/source/FileHandlers/MP3_Handler.hpp"
#include "XMPFiles/source/FileHandlers/MPEG2_Handler.hpp"
#include "XMPFiles/source/FileHandlers/MPEG4_Handler.hpp"
#include "XMPFiles/source/FileHandlers/P2_Handler.hpp"
#include "XMPFiles/source/FileHandlers/WAVE_Handler.hpp"
#include "XMPFiles/source/FileHandlers/RIFF_Handler.hpp"
#include "XMPFiles/source/FileHandlers/SonyHDV_Handler.hpp"
#include "XMPFiles/source/FileHandlers/SWF_Handler.hpp"
#include "XMPFiles/source/FileHandlers/XDCAMEX_Handler.hpp"
#include "XMPFiles/source/FileHandlers/XDCAMFAM_Handler.hpp"
#include "XMPFiles/source/FileHandlers/XDCAMSAM_Handler.hpp"
#include "XMPFiles/source/FileHandlers/WEBP_Handler.hpp"
#endif
#if EnableMiscHandlers
#include "XMPFiles/source/FileHandlers/InDesign_Handler.hpp"
#include "XMPFiles/source/FileHandlers/PNG_Handler.hpp"
#include "XMPFiles/source/FileHandlers/PostScript_Handler.hpp"
#include "XMPFiles/source/FileHandlers/UCF_Handler.hpp"
#include "XMPFiles/source/FileHandlers/SVG_Handler.hpp"
#endif
//#if EnablePacketScanning
//#include "XMPFiles/source/FileHandlers/Scanner_Handler.hpp"
//#endif
using namespace Common;
#if EnablePluginManager
using namespace XMP_PLUGIN;
#endif
// =================================================================================================
#if EnableDynamicMediaHandlers
static const char * kP2ContentChildren[] = { "CLIP", "VIDEO", "AUDIO", "ICON", "VOICE", "PROXY", 0 };
static inline bool CheckP2ContentChild ( const std::string & folderName )
{
for ( int i = 0; kP2ContentChildren[i] != 0; ++i ) {
if ( folderName == kP2ContentChildren[i] ) return true;
}
return false;
}
#endif
// =================================================================================================
//
// Static init
//
HandlerRegistry* HandlerRegistry::sInstance = 0;
/*static*/ HandlerRegistry& HandlerRegistry::getInstance()
{
if ( sInstance == 0 ) sInstance = new HandlerRegistry();
return *sInstance;
}
/*static*/ void HandlerRegistry::terminate()
{
delete sInstance;
sInstance = 0;
}
HandlerRegistry::HandlerRegistry()
{
mFolderHandlers = new XMPFileHandlerTable;
mNormalHandlers = new XMPFileHandlerTable;
mOwningHandlers = new XMPFileHandlerTable;
mReplacedHandlers = new XMPFileHandlerTable;
}
HandlerRegistry::~HandlerRegistry()
{
delete mFolderHandlers;
delete mNormalHandlers;
delete mOwningHandlers;
delete mReplacedHandlers;
}
// =================================================================================================
void HandlerRegistry::initialize()
{
bool allOK = true; // All of the linked-in handler registrations must work, do one test at the end.
// -----------------------------------------
// Register the directory-oriented handlers.
#if EnableDynamicMediaHandlers
allOK &= this->registerFolderHandler ( kXMP_P2File, kP2_HandlerFlags, P2_CheckFormat, P2_MetaHandlerCTor );
allOK &= this->registerFolderHandler ( kXMP_SonyHDVFile, kSonyHDV_HandlerFlags, SonyHDV_CheckFormat, SonyHDV_MetaHandlerCTor );
allOK &= this->registerFolderHandler ( kXMP_XDCAM_FAMFile, kXDCAMFAM_HandlerFlags, XDCAMFAM_CheckFormat, XDCAMFAM_MetaHandlerCTor );
allOK &= this->registerFolderHandler ( kXMP_XDCAM_SAMFile, kXDCAMSAM_HandlerFlags, XDCAMSAM_CheckFormat, XDCAMSAM_MetaHandlerCTor );
allOK &= this->registerFolderHandler ( kXMP_XDCAM_EXFile, kXDCAMEX_HandlerFlags, XDCAMEX_CheckFormat, XDCAMEX_MetaHandlerCTor );
#endif
// ------------------------------------------------------------------------------------------
// Register the file-oriented handlers that don't want to open and close the file themselves.
#if EnablePhotoHandlers
allOK &= this->registerNormalHandler ( kXMP_JPEGFile, kJPEG_HandlerFlags, JPEG_CheckFormat, JPEG_MetaHandlerCTor );
allOK &= this->registerNormalHandler ( kXMP_PhotoshopFile, kPSD_HandlerFlags, PSD_CheckFormat, PSD_MetaHandlerCTor );
allOK &= this->registerNormalHandler ( kXMP_TIFFFile, kTIFF_HandlerFlags, TIFF_CheckFormat, TIFF_MetaHandlerCTor );
allOK &= this->registerNormalHandler( kXMP_GIFFile, kGIF_HandlerFlags, GIF_CheckFormat, GIF_MetaHandlerCTor );
#endif
#if EnableDynamicMediaHandlers
allOK &= this->registerNormalHandler ( kXMP_WMAVFile, kASF_HandlerFlags, ASF_CheckFormat, ASF_MetaHandlerCTor );
allOK &= this->registerNormalHandler ( kXMP_MP3File, kMP3_HandlerFlags, MP3_CheckFormat, MP3_MetaHandlerCTor );
allOK &= this->registerNormalHandler ( kXMP_WAVFile, kWAVE_HandlerFlags, WAVE_CheckFormat, WAVE_MetaHandlerCTor );
allOK &= this->registerNormalHandler ( kXMP_AVIFile, kRIFF_HandlerFlags, RIFF_CheckFormat, RIFF_MetaHandlerCTor );
allOK &= this->registerNormalHandler ( kXMP_WEBPFile, kWEBP_HandlerFlags, WEBP_CheckFormat, WEBP_MetaHandlerCTor );
allOK &= this->registerNormalHandler ( kXMP_SWFFile, kSWF_HandlerFlags, SWF_CheckFormat, SWF_MetaHandlerCTor );
allOK &= this->registerNormalHandler ( kXMP_MPEG4File, kMPEG4_HandlerFlags, MPEG4_CheckFormat, MPEG4_MetaHandlerCTor );
allOK &= this->registerNormalHandler ( kXMP_MOVFile, kMPEG4_HandlerFlags, MPEG4_CheckFormat, MPEG4_MetaHandlerCTor ); // ! Yes, MPEG-4 includes MOV.
allOK &= this->registerNormalHandler ( kXMP_FLVFile, kFLV_HandlerFlags, FLV_CheckFormat, FLV_MetaHandlerCTor );
allOK &= this->registerNormalHandler ( kXMP_AIFFFile, kAIFF_HandlerFlags, AIFF_CheckFormat, AIFF_MetaHandlerCTor );
#endif
#if EnableMiscHandlers
allOK &= this->registerNormalHandler ( kXMP_InDesignFile, kInDesign_HandlerFlags, InDesign_CheckFormat, InDesign_MetaHandlerCTor );
allOK &= this->registerNormalHandler ( kXMP_PNGFile, kPNG_HandlerFlags, PNG_CheckFormat, PNG_MetaHandlerCTor );
allOK &= this->registerNormalHandler ( kXMP_UCFFile, kUCF_HandlerFlags, UCF_CheckFormat, UCF_MetaHandlerCTor );
// ! EPS and PostScript have the same handler, EPS is a proper subset of PostScript.
allOK &= this->registerNormalHandler ( kXMP_EPSFile, kPostScript_HandlerFlags, PostScript_CheckFormat, PostScript_MetaHandlerCTor );
allOK &= this->registerNormalHandler ( kXMP_PostScriptFile, kPostScript_HandlerFlags, PostScript_CheckFormat, PostScript_MetaHandlerCTor );
allOK &= this->registerNormalHandler ( kXMP_SVGFile, kSVG_HandlerFlags, SVG_CheckFormat, SVG_MetaHandlerCTor );
#endif
// ------------------------------------------------------------------------------------
// Register the file-oriented handlers that need to open and close the file themselves.
#if EnableDynamicMediaHandlers
allOK &= this->registerOwningHandler ( kXMP_MPEGFile, kMPEG2_HandlerFlags, MPEG2_CheckFormat, MPEG2_MetaHandlerCTor );
allOK &= this->registerOwningHandler ( kXMP_MPEG2File, kMPEG2_HandlerFlags, MPEG2_CheckFormat, MPEG2_MetaHandlerCTor );
#endif
if ( ! allOK ) XMP_Throw ( "Failure initializing linked-in file handlers", kXMPErr_InternalFailure );
} // HandlerRegistry::initialize
// =================================================================================================
bool HandlerRegistry::registerFolderHandler( XMP_FileFormat format,
XMP_OptionBits flags,
CheckFolderFormatProc checkProc,
XMPFileHandlerCTor handlerCTor,
bool replaceExisting /*= false*/ )
{
XMP_Assert ( format != kXMP_UnknownFile );
XMP_Assert ( flags & kXMPFiles_HandlerOwnsFile );
XMP_Assert ( flags & kXMPFiles_FolderBasedFormat );
XMP_Assert ( (flags & kXMPFiles_CanInjectXMP) ? (flags & kXMPFiles_CanExpand) : 1 );
if ( replaceExisting )
{
//
// Remember previous file handler for this format.
// Reject if there is already a replacement.
//
if( mReplacedHandlers->find( format ) == mReplacedHandlers->end() )
{
XMPFileHandlerInfo* standardHandler = this->getHandlerInfo( format );
if( standardHandler != NULL )
{
mReplacedHandlers->insert( mReplacedHandlers->end(), XMPFileHandlerTablePair( format, *standardHandler ) );
}
else
{
// skip registration if there is nothing to replace
return false;
}
}
else
{
// skip registration if there is already a replacing handler registered for this format
return false;
}
// remove existing handler
this->removeHandler ( format );
}
else
{
// skip registration if there is already a handler registered for this format
if ( this->getFormatInfo ( format ) ) return false;
}
//
// register handler
//
XMPFileHandlerInfo handlerInfo ( format, flags, checkProc, handlerCTor );
mFolderHandlers->insert ( mFolderHandlers->end(), XMPFileHandlerTablePair ( format, handlerInfo ) );
return true;
} // HandlerRegistry::registerFolderHandler
// =================================================================================================
bool HandlerRegistry::registerNormalHandler( XMP_FileFormat format,
XMP_OptionBits flags,
CheckFileFormatProc checkProc,
XMPFileHandlerCTor handlerCTor,
bool replaceExisting /*= false*/ )
{
XMP_Assert ( format != kXMP_UnknownFile );
XMP_Assert ( ! (flags & kXMPFiles_HandlerOwnsFile) );
XMP_Assert ( ! (flags & kXMPFiles_FolderBasedFormat) );
XMP_Assert ( (flags & kXMPFiles_CanInjectXMP) ? (flags & kXMPFiles_CanExpand) : 1 );
if ( replaceExisting )
{
//
// Remember previous file handler for this format.
// Reject if there is already a replacement.
//
if( mReplacedHandlers->find( format ) == mReplacedHandlers->end() )
{
XMPFileHandlerInfo* standardHandler = this->getHandlerInfo( format );
if( standardHandler != NULL )
{
mReplacedHandlers->insert( mReplacedHandlers->end(), XMPFileHandlerTablePair( format, *standardHandler ) );
}
else
{
// skip registration if there is nothing to replace
return false;
}
}
else
{
// skip registration if there is already a replacing handler registered for this format
return false;
}
// remove existing handler
this->removeHandler ( format );
}
else
{
// skip registration if there is already a handler registered for this format
if ( this->getFormatInfo ( format ) ) return false;
}
//
// register handler
//
XMPFileHandlerInfo handlerInfo ( format, flags, checkProc, handlerCTor );
mNormalHandlers->insert ( mNormalHandlers->end(), XMPFileHandlerTablePair ( format, handlerInfo ) );
return true;
} // HandlerRegistry::registerNormalHandler
// =================================================================================================
bool HandlerRegistry::registerOwningHandler( XMP_FileFormat format,
XMP_OptionBits flags,
CheckFileFormatProc checkProc,
XMPFileHandlerCTor handlerCTor,
bool replaceExisting /*= false*/ )
{
XMP_Assert ( format != kXMP_UnknownFile );
XMP_Assert ( flags & kXMPFiles_HandlerOwnsFile );
XMP_Assert ( ! (flags & kXMPFiles_FolderBasedFormat) );
XMP_Assert ( (flags & kXMPFiles_CanInjectXMP) ? (flags & kXMPFiles_CanExpand) : 1 );
if ( replaceExisting )
{
//
// Remember previous file handler for this format.
// Reject if there is already a replacement.
//
if( mReplacedHandlers->find( format ) == mReplacedHandlers->end() )
{
XMPFileHandlerInfo* standardHandler = this->getHandlerInfo( format );
if( standardHandler != NULL )
{
mReplacedHandlers->insert( mReplacedHandlers->end(), XMPFileHandlerTablePair( format, *standardHandler ) );
}
else
{
// skip registration if there is nothing to replace
return false;
}
}
else
{
// skip registration if there is already a replacing handler registered for this format
return false;
}
// remove existing handler
this->removeHandler ( format );
}
else
{
// skip registration if there is already a handler registered for this format
if ( this->getFormatInfo ( format ) ) return false;
}
//
// register handler
//
XMPFileHandlerInfo handlerInfo ( format, flags, checkProc, handlerCTor );
mOwningHandlers->insert ( mOwningHandlers->end(), XMPFileHandlerTablePair ( format, handlerInfo ) );
return true;
} // HandlerRegistry::registerOwningHandler
// =================================================================================================
void HandlerRegistry::removeHandler ( XMP_FileFormat format ) {
XMPFileHandlerTablePos handlerPos;
handlerPos = mFolderHandlers->find ( format );
if ( handlerPos != mFolderHandlers->end() ) {
mFolderHandlers->erase ( handlerPos );
XMP_Assert ( ! this->getFormatInfo ( format ) );
return;
}
handlerPos = mNormalHandlers->find ( format );
if ( handlerPos != mNormalHandlers->end() ) {
mNormalHandlers->erase ( handlerPos );
XMP_Assert ( ! this->getFormatInfo ( format ) );
return;
}
handlerPos = mOwningHandlers->find ( format );
if ( handlerPos != mOwningHandlers->end() ) {
mOwningHandlers->erase ( handlerPos );
XMP_Assert ( ! this->getFormatInfo ( format ) );
return;
}
} // HandlerRegistry::removeHandler
// =================================================================================================
XMP_FileFormat HandlerRegistry::getFileFormat( const std::string & fileExt, bool addIfNotFound /*= false*/ )
{
if ( ! fileExt.empty() ) {
for ( int i=0; kFileExtMap[i].format != 0; ++i ) {
if ( fileExt == kFileExtMap[i].ext ) return kFileExtMap[i].format;
}
}
#if EnablePluginManager
return ResourceParser::getPluginFileFormat ( fileExt, addIfNotFound );
#else
return kXMP_UnknownFile;
#endif
}
// =================================================================================================
XMPFileHandlerInfo* HandlerRegistry::getHandlerInfo( XMP_FileFormat format )
{
XMPFileHandlerTablePos handlerPos;
handlerPos = mFolderHandlers->find( format );
if( handlerPos != mFolderHandlers->end() )
{
return &(handlerPos->second);
}
handlerPos = mNormalHandlers->find ( format );
if( handlerPos != mNormalHandlers->end() )
{
return &(handlerPos->second);
}
handlerPos = mOwningHandlers->find ( format );
if( handlerPos != mOwningHandlers->end() )
{
return &(handlerPos->second);
}
return NULL;
}
// =================================================================================================
XMPFileHandlerInfo* HandlerRegistry::getStandardHandlerInfo( XMP_FileFormat format )
{
XMPFileHandlerTablePos handlerPos = mReplacedHandlers->find( format );
if( handlerPos != mReplacedHandlers->end() )
{
return &(handlerPos->second);
}
else
{
return this->getHandlerInfo( format );
}
}
// =================================================================================================
bool HandlerRegistry::isReplaced( XMP_FileFormat format )
{
return ( mReplacedHandlers->find( format ) != mReplacedHandlers->end() );
}
// =================================================================================================
bool HandlerRegistry::getFormatInfo( XMP_FileFormat format, XMP_OptionBits* flags /*= 0*/ )
{
XMP_OptionBits voidOptionBits = 0;
if ( flags == 0 ) flags = &voidOptionBits;
XMPFileHandlerInfo* handler = this->getHandlerInfo( format );
if( handler != NULL )
{
*flags = handler->flags;
}
return ( handler != NULL );
} // HandlerRegistry::getFormatInfo
// =================================================================================================
XMPFileHandlerInfo* HandlerRegistry::pickDefaultHandler ( XMP_FileFormat format, const std::string & fileExt )
{
if ( format == kXMP_UnknownFile ) format = this->getFileFormat ( fileExt );
if ( format == kXMP_UnknownFile ) return 0;
XMPFileHandlerTablePos handlerPos;
handlerPos = mNormalHandlers->find ( format );
if ( handlerPos != mNormalHandlers->end() ) return &handlerPos->second;
handlerPos = mOwningHandlers->find ( format );
if ( handlerPos != mOwningHandlers->end() ) return &handlerPos->second;
handlerPos = mFolderHandlers->find ( format );
if ( handlerPos != mFolderHandlers->end() ) return &handlerPos->second;
return 0;
}
// =================================================================================================
XMPFileHandlerInfo* HandlerRegistry::selectSmartHandler( XMPFiles* session, XMP_StringPtr clientPath, XMP_FileFormat format, XMP_OptionBits openFlags )
{
// The normal case for selectSmartHandler is when OpenFile is given a string file path. All of
// the stages described below have slight special cases when OpenFile is given an XMP_IO object
// for client-managed I/O. In that case the only handlers considered are those for embedded XMP
// that do not need to own the file.
//
// There are 4 stages in finding a handler, ending at the first success:
// 1. If the client passes in a format, try that handler.
// 2. Try all of the folder-oriented handlers.
// 3. Try a file-oriented handler based on the file extension.
// 4. Try all of the file-oriented handlers.
//
// The most common case is almost certainly #3, so we want to get there quickly. Most of the
// time the client won't pass in a format, so #1 takes no time. The folder-oriented handler
// checks are preceded by minimal folder checks. These checks are meant to be fast in the
// failure case. The folder-oriented checks have to go before the general file-oriented checks
// because the client path might be to one of the inner files, and we might have a file-oriented
// handler for that kind of file, but we want to recognize the clip. More details are below.
//
// In brief, the folder-oriented formats use shallow trees with specific folder names and
// highly stylized file names. The user thinks of the tree as a collection of clips, each clip
// is stored as multiple files for video, audio, metadata, etc. The folder-oriented stage has
// to be first because there can be files in the structure that are also covered by a
// file-oriented handler.
//
// In the file-oriented case, the CheckProc should do as little as possible to determine the
// format, based on the actual file content. If that is not possible, use the format hint. The
// initial CheckProc calls (steps 1 and 3) has the presumed format in this->format, the later
// calls (step 4) have kXMP_UnknownFile there.
//
// The folder-oriented checks need to be well optimized since the formats are relatively rare,
// but have to go first and could require multiple file system calls to identify. We want to
// get to the first file-oriented guess as quickly as possible, that is the real handler most of
// the time.
//
// The folder-oriented handlers are for things like P2 and XDCAM that use files distributed in a
// well defined folder structure. Using a portion of P2 as an example:
// .../MyMovie
// CONTENTS
// CLIP
// 0001AB.XML
// 0002CD.XML
// VIDEO
// 0001AB.MXF
// 0002CD.MXF
// VOICE
// 0001AB.WAV
// 0002CD.WAV
//
// The user thinks of .../MyMovie as the container of P2 stuff, in this case containing 2 clips
// called 0001AB and 0002CD. The exact folder structure and file layout differs, but the basic
// concepts carry across all of the folder-oriented handlers.
//
// The client path can be a conceptual clip path like .../MyMovie/0001AB, or a full path to any
// of the contained files. For file paths we have to behave the same as the implied conceptual
// path, e.g. we don't want .../MyMovie/CONTENTS/VOICE/0001AB.WAV to invoke the WAV handler.
// There might also be a mapping from user friendly names to clip names (e.g. Intro to 0001AB).
// If so that is private to the handler and does not affect this code.
//
// In order to properly handle the file path input we have to look for the folder-oriented case
// before any of the file-oriented cases. And since these are relatively rare, hence fail most of
// the time, we have to get in and out fast in the not handled case. That is what we do here.
//
// The folder-oriented processing done here is roughly:
//
// 1. Get the state of the client path: does-not-exist, is-file, is-folder, is-other.
// 2. Reject is-folder and is-other, they can't possibly be a valid case.
// 3. For does-not-exist:
// 3a. Split the client path into a leaf component and root path.
// 3b. Make sure the root path names an existing folder.
// 3c. Make sure the root folder has a viable top level child folder (e.g. CONTENTS).
// 4. For is-file:
// 4a. Split the client path into a root path, grandparent folder, parent folder, and leaf name.
// 4b. Make sure the parent or grandparent has a viable name (e.g. CONTENTS).
// 5. Try the registered folder handlers.
//
// For the common case of "regular" files, we should only get as far as 3b. This is just 1 file
// system call to get the client path state and some string processing.
bool readOnly = XMP_OptionIsClear( openFlags, kXMPFiles_OpenForUpdate );
Host_IO::FileMode clientMode;
std::string rootPath;
std::string leafName;
std::string fileExt;
std::string emptyStr;
XMPFileHandlerInfo* handlerInfo = 0;
bool foundHandler = false;
if ( openFlags & kXMPFiles_ForceGivenHandler ) {
// We're being told to blindly use the handler for the given format and nothing else.
return this->pickDefaultHandler ( format, emptyStr ); // Picks based on just the format.
}
if ( session->UsesClientIO() ) {
XMP_Assert ( session->ioRef != 0 );
clientMode = Host_IO::kFMode_IsFile;
} else {
clientMode = Host_IO::GetFileMode( clientPath );
if ( (clientMode == Host_IO::kFMode_IsFolder) || (clientMode == Host_IO::kFMode_IsOther) ) return 0;
rootPath = clientPath;
XIO::SplitLeafName ( &rootPath, &leafName );
if ( leafName.empty() ) return 0;
if ( clientMode == Host_IO::kFMode_IsFile ) {
// Only extract the file extension for existing files. Non-existing files can only be
// logical clip names, and they don't have file extensions.
XIO::SplitFileExtension ( &leafName, &fileExt );
}
}
session->format = kXMP_UnknownFile; // Make sure it is preset for later checks.
session->openFlags = openFlags;
// If the client passed in a format, try that first.
if( format != kXMP_UnknownFile )
{
handlerInfo = this->pickDefaultHandler( format, emptyStr ); // Picks based on just the format.
if( handlerInfo != 0 )
{
if( ( session->ioRef == 0 ) && (! ( handlerInfo->flags & kXMPFiles_HandlerOwnsFile ) ) )
{
session->ioRef = XMPFiles_IO::New_XMPFiles_IO( clientPath, readOnly, &session->errorCallback);
if ( session->ioRef == 0 ) return 0;
}
session->format = format; // ! Hack to tell the CheckProc session is an initial call.
if( handlerInfo->flags & kXMPFiles_FolderBasedFormat )
{
#if 0
std::string gpName, parentName;
if ( clientMode == Host_IO::kFMode_IsFile ) {
XIO::SplitLeafName( &rootPath, &parentName );
XIO::SplitLeafName( &rootPath, &gpName );
if ( format != kXMP_XDCAM_FAMFile ) MakeUpperCase( &gpName ); // ! Save the original case for XDCAM-FAM.
MakeUpperCase( &parentName );
}
CheckFolderFormatProc CheckProc = (CheckFolderFormatProc) (handlerInfo->checkProc);
foundHandler = CheckProc ( handlerInfo->format, rootPath, gpName, parentName, leafName, session );
#else
// *** Don't try here yet. These are messy, needing existence checking and path processing.
// *** CheckFolderFormatProc CheckProc = (CheckFolderFormatProc) (handlerInfo->checkProc);
// *** foundHandler = CheckProc ( handlerInfo->format, rootPath, gpName, parentName, leafName, session );
// *** Don't let OpenStrictly cause an early exit:
if( openFlags & kXMPFiles_OpenStrictly ) openFlags ^= kXMPFiles_OpenStrictly;
#endif
}
else
{
bool tryThisHandler = true;
if( session->UsesClientIO() )
{
if ( (handlerInfo->flags & kXMPFiles_UsesSidecarXMP) ||
(handlerInfo->flags & kXMPFiles_HandlerOwnsFile) ) tryThisHandler = false;
}
if( tryThisHandler )
{
CheckFileFormatProc CheckProc = (CheckFileFormatProc) (handlerInfo->checkProc);
foundHandler = CheckProc ( format, clientPath, session->ioRef, session );
}
}
XMP_Assert( foundHandler || (session->tempPtr == 0) );
if ( foundHandler ) return handlerInfo;
handlerInfo = 0; // ! Clear again for later use.
}
if ( openFlags & kXMPFiles_OpenStrictly ) return 0;
}
#if EnableDynamicMediaHandlers // All of the folder handlers are for dynamic media.
// Try the folder handlers if appropriate.
if( session->UsesLocalIO() )
{
XMP_Assert ( handlerInfo == 0 );
XMP_Assert ( (clientMode == Host_IO::kFMode_IsFile) || (clientMode == Host_IO::kFMode_DoesNotExist) );
std::string gpName, parentName;
if( clientMode == Host_IO::kFMode_DoesNotExist )
{
// 3. For does-not-exist:
// 3a. Split the client path into a leaf component and root path.
// 3b. Make sure the root path names an existing folder.
// 3c. Make sure the root folder has a viable top level child folder.
// ! This does "return 0" on failure, the file does not exist so a normal file handler can't apply.
if ( Host_IO::GetFileMode ( rootPath.c_str() ) != Host_IO::kFMode_IsFolder ) return 0;
session->format = checkTopFolderName ( rootPath );
if ( session->format == kXMP_UnknownFile ) return 0;
handlerInfo = this->tryFolderHandlers( session->format, rootPath, gpName, parentName, leafName, session ); // ! Parent and GP are empty.
if ( handlerInfo == 0 && session->format == kXMP_AVCUltraFile )
{
session->format = kXMP_P2File;
handlerInfo = this->tryFolderHandlers( session->format, rootPath, gpName, parentName, leafName, session ); // ! Parent and GP are empty.
}
return handlerInfo; // ! Return found handler or 0.
}
XMP_Assert ( clientMode == Host_IO::kFMode_IsFile );
// 4. For is-file:
// 4a. Split the client path into root, grandparent, parent, and leaf.
// 4b. Make sure the parent or grandparent has a viable name.
// ! Don't "return 0" on failure, this has to fall through to the normal file handlers.
XIO::SplitLeafName( &rootPath, &parentName );
XIO::SplitLeafName( &rootPath, &gpName );
std::string origGPName ( gpName ); // ! Save the original case for XDCAM-FAM.
MakeUpperCase( &parentName );
MakeUpperCase( &gpName );
session->format = checkParentFolderNames( rootPath, gpName, parentName, leafName );
if( session->format != kXMP_UnknownFile )
{
if( (session->format == kXMP_XDCAM_FAMFile) &&
( (parentName == "CLIP") || (parentName == "EDIT") || (parentName == "SUB") ) )
{
// ! The standard says Clip/Edit/Sub, but we just shifted to upper case.
gpName = origGPName; // ! XDCAM-FAM has just 1 level of inner folder, preserve the "MyMovie" case.
}
handlerInfo = tryFolderHandlers ( session->format, rootPath, gpName, parentName, leafName, session );
if ( handlerInfo != 0 ) return handlerInfo;
}
}
#endif // EnableDynamicMediaHandlers
// Try an initial file-oriented handler based on the extension.
if( session->UsesLocalIO() )
{
handlerInfo = pickDefaultHandler ( kXMP_UnknownFile, fileExt ); // Picks based on just the extension.
if( handlerInfo != 0 )
{
if( (session->ioRef == 0) && (! (handlerInfo->flags & kXMPFiles_HandlerOwnsFile)) )
{
session->ioRef = XMPFiles_IO::New_XMPFiles_IO ( clientPath, readOnly, &session->errorCallback);
if ( session->ioRef == 0 ) return 0;
}
else if( (session->ioRef != 0) && (handlerInfo->flags & kXMPFiles_HandlerOwnsFile) )
{
delete session->ioRef; // Close is implicit in the destructor.
session->ioRef = 0;
}
session->format = handlerInfo->format; // ! Hack to tell the CheckProc this is an initial call.
CheckFileFormatProc CheckProc = (CheckFileFormatProc) (handlerInfo->checkProc);
foundHandler = CheckProc ( handlerInfo->format, clientPath, session->ioRef, session );
XMP_Assert ( foundHandler || (session->tempPtr == 0) );
if ( foundHandler ) return handlerInfo;
}
}
// Search the handlers that don't want to open the file themselves.
if( session->ioRef == 0 )
{
session->ioRef = XMPFiles_IO::New_XMPFiles_IO ( clientPath, readOnly, &session->errorCallback );
if ( session->ioRef == 0 ) return 0;
}
XMPFileHandlerTablePos handlerPos = mNormalHandlers->begin();
for( ; handlerPos != mNormalHandlers->end(); ++handlerPos )
{
session->format = kXMP_UnknownFile; // ! Hack to tell the CheckProc this is not an initial call.
handlerInfo = &handlerPos->second;
CheckFileFormatProc CheckProc = (CheckFileFormatProc) (handlerInfo->checkProc);
foundHandler = CheckProc ( handlerInfo->format, clientPath, session->ioRef, session );
XMP_Assert ( foundHandler || (session->tempPtr == 0) );
if ( foundHandler ) return handlerInfo;
}
// Search the handlers that do want to open the file themselves.
if( session->UsesLocalIO() )
{
delete session->ioRef; // Close is implicit in the destructor.
session->ioRef = 0;
handlerPos = mOwningHandlers->begin();
for( ; handlerPos != mOwningHandlers->end(); ++handlerPos )
{
session->format = kXMP_UnknownFile; // ! Hack to tell the CheckProc this is not an initial call.
handlerInfo = &handlerPos->second;
CheckFileFormatProc CheckProc = (CheckFileFormatProc) (handlerInfo->checkProc);
foundHandler = CheckProc ( handlerInfo->format, clientPath, session->ioRef, session );
XMP_Assert ( foundHandler || (session->tempPtr == 0) );
if ( foundHandler ) return handlerInfo;
}
}
// Failed to find a smart handler.
return 0;
} // HandlerRegistry::selectSmartHandler
// =================================================================================================
#if EnableDynamicMediaHandlers
XMPFileHandlerInfo* HandlerRegistry::tryFolderHandlers( XMP_FileFormat format,
const std::string & rootPath,
const std::string & gpName,
const std::string & parentName,
const std::string & leafName,
XMPFiles * parentObj )
{
bool foundHandler = false;
XMPFileHandlerInfo * handlerInfo = 0;
XMPFileHandlerTablePos handlerPos;
// We know we're in a possible context for a folder-oriented handler, so try them.
if( format != kXMP_UnknownFile )
{
// Have an explicit format, pick that or nothing.
handlerPos = mFolderHandlers->find ( format );
if( handlerPos != mFolderHandlers->end() )
{
handlerInfo = &handlerPos->second;
CheckFolderFormatProc CheckProc = (CheckFolderFormatProc) (handlerInfo->checkProc);
foundHandler = CheckProc ( handlerInfo->format, rootPath, gpName, parentName, leafName, parentObj );
XMP_Assert ( foundHandler || (parentObj->tempPtr == 0) );
}
}
else
{
// Try all of the folder handlers.
for( handlerPos = mFolderHandlers->begin(); handlerPos != mFolderHandlers->end(); ++handlerPos )
{
handlerInfo = &handlerPos->second;
CheckFolderFormatProc CheckProc = (CheckFolderFormatProc) (handlerInfo->checkProc);
foundHandler = CheckProc ( handlerInfo->format, rootPath, gpName, parentName, leafName, parentObj );
XMP_Assert ( foundHandler || (parentObj->tempPtr == 0) );
if ( foundHandler ) break; // ! Exit before incrementing handlerPos.
}
}
if ( ! foundHandler ) handlerInfo = 0;
return handlerInfo;
}
#endif
// =================================================================================================
#if EnableDynamicMediaHandlers
/*static*/ XMP_FileFormat HandlerRegistry::checkTopFolderName( const std::string & rootPath )
{
// This is called when the input path to XMPFiles::OpenFile does not name an existing file (or
// existing anything). We need to quickly decide if this might be a logical path for a folder
// handler. See if the root contains the top content folder for any of the registered folder
// handlers. This check does not have to be precise, the handler will do that. This does have to
// be fast.
//
// Since we don't have many folder handlers, this is simple hardwired code.
std::string childPath = rootPath;
childPath += kDirChar;
size_t baseLen = childPath.size();
// P2 .../MyMovie/CONTENTS/<group>/... - only check for CONTENTS/CLIP
childPath += "CONTENTS";
childPath += kDirChar;
childPath += "CLIP";
if ( Host_IO::GetFileMode ( childPath.c_str() ) == Host_IO::kFMode_IsFolder ) return kXMP_P2File;
childPath.erase ( baseLen );
// XDCAM-FAM .../MyMovie/<group>/... - only check for Clip and MEDIAPRO.XML
childPath += "Clip"; // ! Yes, mixed case.
if ( Host_IO::GetFileMode ( childPath.c_str() ) == Host_IO::kFMode_IsFolder ) {
childPath.erase ( baseLen );
childPath += "MEDIAPRO.XML";
if ( Host_IO::GetFileMode ( childPath.c_str() ) == Host_IO::kFMode_IsFile ) return kXMP_XDCAM_FAMFile;
}
childPath.erase ( baseLen );
// XDCAM-SAM .../MyMovie/PROAV/<group>/... - only check for PROAV/CLPR
childPath += "PROAV";
childPath += kDirChar;
childPath += "CLPR";
if ( Host_IO::GetFileMode ( childPath.c_str() ) == Host_IO::kFMode_IsFolder ) return kXMP_XDCAM_SAMFile;
childPath.erase ( baseLen );
// XDCAM-EX .../MyMovie/BPAV/<group>/... - check for BPAV/CLPR
childPath += "BPAV";
childPath += kDirChar;
childPath += "CLPR";
if ( Host_IO::GetFileMode ( childPath.c_str() ) == Host_IO::kFMode_IsFolder ) return kXMP_XDCAM_EXFile;
childPath.erase ( baseLen );
// Sony HDV .../MyMovie/VIDEO/HVR/<file>.<ext> - check for VIDEO/HVR
childPath += "VIDEO";
childPath += kDirChar;
childPath += "HVR";
if ( Host_IO::GetFileMode ( childPath.c_str() ) == Host_IO::kFMode_IsFolder ) return kXMP_SonyHDVFile;
childPath.erase ( baseLen );
return kXMP_UnknownFile;
} // CheckTopFolderName
#endif
// =================================================================================================
#if EnableDynamicMediaHandlers
/*static*/ XMP_FileFormat HandlerRegistry::checkParentFolderNames( const std::string& rootPath,
const std::string& gpName,
const std::string& parentName,
const std::string& leafName )
{
IgnoreParam(leafName);
// This is called when the input path to XMPFiles::OpenFile names an existing file. We need to
// quickly decide if this might be inside a folder-handler's structure. See if the containing
// folders might match any of the registered folder handlers. This check does not have to be
// precise, the handler will do that. This does have to be fast.
//
// Since we don't have many folder handlers, this is simple hardwired code. Note that the caller
// has already shifted the names to upper case.
// P2 .../MyMovie/CONTENTS/<group>/<file>.<ext> - check CONTENTS and <group>
if ( (gpName == "CONTENTS") && CheckP2ContentChild ( parentName ) ) return kXMP_P2File;
// XDCAM-EX .../MyMovie/BPAV/CLPR/<clip>/<file>.<ext> - check for BPAV/CLPR
// ! This must be checked before XDCAM-SAM because both have a "CLPR" grandparent.
if ( gpName == "CLPR" ) {
std::string tempPath, greatGP;
tempPath = rootPath;
XIO::SplitLeafName ( &tempPath, &greatGP );
MakeUpperCase ( &greatGP );
if ( greatGP == "BPAV" ) return kXMP_XDCAM_EXFile;
}
// XDCAM-FAM .../MyMovie/<group>/<file>.<ext> - check that <group> is CLIP, or EDIT, or SUB
// ! The standard says Clip/Edit/Sub, but the caller has already shifted to upper case.
if ( (parentName == "CLIP") || (parentName == "EDIT") || (parentName == "SUB") ) return kXMP_XDCAM_FAMFile;
// XDCAM-SAM .../MyMovie/PROAV/<group>/<clip>/<file>.<ext> - check for PROAV and CLPR or EDTR
if ( (gpName == "CLPR") || (gpName == "EDTR") ) {
std::string tempPath, greatGP;
tempPath = rootPath;
XIO::SplitLeafName ( &tempPath, &greatGP );
MakeUpperCase ( &greatGP );
if ( greatGP == "PROAV" ) return kXMP_XDCAM_SAMFile;
}
// Sony HDV .../MyMovie/VIDEO/HVR/<file>.<ext> - check for VIDEO and HVR
if ( (gpName == "VIDEO") && (parentName == "HVR") ) return kXMP_SonyHDVFile;
return kXMP_UnknownFile;
} // CheckParentFolderNames
#endif
|