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
|
// =================================================================================================
// Copyright Adobe
// Copyright 2010 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 "XMPFiles/source/NativeMetadataSupport/IReconcile.h"
#include "XMPFiles/source/NativeMetadataSupport/MetadataSet.h"
#include "XMPFiles/source/NativeMetadataSupport/IMetadata.h"
#include "XMPFiles/source/FormatSupport/Reconcile_Impl.hpp"
//-----------------------------------------------------------------------------
//
// WAVEReconcile::importNativeToXMP(...)
//
// Purpose: [static] Import native metadata container into XMP.
// This method is supposed to import all native metadata values
// that are listed in the property table from the IMetadata
// instance to the SXMPMeta instance.
//
//-----------------------------------------------------------------------------
bool IReconcile::importNativeToXMP( SXMPMeta& outXMP, const IMetadata& nativeMeta, const MetadataPropertyInfo* propertyInfo, bool xmpPriority )
{
bool changed = false;
std::string xmpValue;
XMP_Uns32 index = 0;
do
{
if( propertyInfo[index].mXMPSchemaNS != NULL )
{
bool xmpPropertyExists = false;
//
// need to know if the XMP property exists either
// if a priority needs to be considered or if
// the value isn't set by a native value
//
switch( propertyInfo[index].mXMPType )
{
case kXMPType_Simple:
{
xmpPropertyExists = outXMP.DoesPropertyExist( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName );
}
break;
case kXMPType_Localized:
{
std::string actualLang;
bool result = outXMP.GetLocalizedText( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName, "" , "x-default" , &actualLang, NULL, NULL );
// If existing and the appropriate one
xmpPropertyExists = result && (actualLang == "x-default");
}
break;
case kXMPType_Array:
case kXMPType_OrderedArray:
{
xmpPropertyExists = outXMP.DoesArrayItemExist( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName, 1 );
}
break;
default:
{
XMP_Throw( "Unknown XMP data type", kXMPErr_InternalFailure );
}
break;
}
//
// import native property if there is no priority or
// the XMP property doesn't exist yet
//
if( ! propertyInfo[index].mConsiderPriority || ! xmpPriority || ! xmpPropertyExists )
{
//
// if the native property exists
//
if( nativeMeta.valueExists( propertyInfo[index].mMetadataID ) )
{
//
// prepare native property value for XMP
// depending on the native data type
//
xmpValue.erase();
switch( propertyInfo[index].mNativeType )
{
case kNativeType_Str:
{
xmpValue = nativeMeta.getValue<std::string>( propertyInfo[index].mMetadataID );
}
break;
case kNativeType_StrASCII:
{
convertToASCII( nativeMeta.getValue<std::string>( propertyInfo[index].mMetadataID ), xmpValue );
}
break;
case kNativeType_StrLocal:
{
ReconcileUtils::NativeToUTF8( nativeMeta.getValue<std::string>( propertyInfo[index].mMetadataID ), xmpValue );
}
break;
case kNativeType_StrUTF8:
{
ReconcileUtils::NativeToUTF8( nativeMeta.getValue<std::string>( propertyInfo[index].mMetadataID ), xmpValue );
}
break;
case kNativeType_Uns64:
{
SXMPUtils::ConvertFromInt64( nativeMeta.getValue<XMP_Uns64>( propertyInfo[index].mMetadataID ), "%llu", &xmpValue );
}
break;
case kNativeType_Uns32:
{
SXMPUtils::ConvertFromInt( nativeMeta.getValue<XMP_Uns32>( propertyInfo[index].mMetadataID ), "%lu", &xmpValue );
}
break;
case kNativeType_Int32:
{
SXMPUtils::ConvertFromInt( nativeMeta.getValue<XMP_Int32>( propertyInfo[index].mMetadataID ), 0 /* default format */, &xmpValue );
}
break;
case kNativeType_Uns16:
{
SXMPUtils::ConvertFromInt( nativeMeta.getValue<XMP_Uns16>( propertyInfo[index].mMetadataID ), "%lu", &xmpValue );
}
break;
case kNativeType_Bool:
{
SXMPUtils::ConvertFromBool( nativeMeta.getValue<bool>( propertyInfo[index].mMetadataID ), &xmpValue );
}
break;
default:
{
XMP_Throw( "Unknown native data type", kXMPErr_InternalFailure );
}
break;
};
if( ! xmpValue.empty() )
{
//
// set the new XMP value depending on the
// XMP data type
//
switch( propertyInfo[index].mXMPType )
{
case kXMPType_Localized:
{
outXMP.SetLocalizedText( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName, NULL, "x-default", xmpValue.c_str() );
}
break;
case kXMPType_Array:
{
// Overwrite any existing array
outXMP.DeleteProperty( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName );
outXMP.AppendArrayItem( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName, kXMP_PropArrayIsUnordered, xmpValue.c_str(), kXMP_NoOptions );
}
break;
case kXMPType_OrderedArray:
{
// Overwrite any existing array
outXMP.DeleteProperty( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName );
outXMP.AppendArrayItem( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName, kXMP_PropArrayIsOrdered, xmpValue.c_str(), kXMP_NoOptions );
}
break;
default:
{
outXMP.SetProperty( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName, xmpValue.c_str() );
}
break;
}
changed = true;
}
}
else if( propertyInfo[index].mDeleteXMPIfNoNative && xmpPropertyExists )
{
//
// native value doesn't exists, delete the XMP property
//
outXMP.DeleteProperty( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName );
changed = xmpPropertyExists;
}
}
}
index++;
} while( propertyInfo[index].mXMPSchemaNS != NULL );
return changed;
}
//-----------------------------------------------------------------------------
//
// WAVEReconcile::exportXMPToNative(...)
//
// Purpose: [static] Export XMP values to native metadata container.
// This method is supposed to export all native metadata values
// that are listed in the property table from the XMP container
// to the IMetadata instance.
//
//-----------------------------------------------------------------------------
bool IReconcile::exportXMPToNative( IMetadata& outNativeMeta, SXMPMeta& inXMP, const MetadataPropertyInfo* propertyInfo, PropertyList * propertiesExportedSuccessfully /*= NULL*/ )
{
std::string xmpValue;
XMP_Uns32 index = 0;
do
{
if( propertyInfo[index].mXMPSchemaNS != NULL && propertyInfo[index].mExportPolicy != kExport_Never )
{
bool xmpPropertyExists = false;
//
// get value from XMP depending on
// the XMP data type
//
switch( propertyInfo[index].mXMPType )
{
case kXMPType_Localized:
{
std::string lang;
xmpPropertyExists = inXMP.GetLocalizedText( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName, NULL, "x-default", &lang, &xmpValue, 0 );
}
break;
case kXMPType_Array:
case kXMPType_OrderedArray:
{
// TOTRACK currently only the first array item is used
if( inXMP.CountArrayItems( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName ) > 0 )
{
xmpPropertyExists = inXMP.GetArrayItem( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName, 1, &xmpValue, 0 );
}
}
break;
default:
{
xmpPropertyExists = inXMP.GetProperty( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName, &xmpValue, 0 );
}
break;
}
//
// convert xmp value and set native property
// depending on the native data type and the export policy
//
if( xmpPropertyExists &&
( propertyInfo[index].mExportPolicy != kExport_InjectOnly ||
! outNativeMeta.valueExists( propertyInfo[index].mMetadataID ) ) )
{
switch( propertyInfo[index].mNativeType )
{
case kNativeType_StrASCII:
{
std::string ascii;
convertToASCII( xmpValue, ascii );
outNativeMeta.setValue<std::string>( propertyInfo[index].mMetadataID, ascii );
if ( propertiesExportedSuccessfully )
propertiesExportedSuccessfully->push_back( std::make_pair( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName ) );
}
break;
case kNativeType_Str:
case kNativeType_StrUTF8:
{
outNativeMeta.setValue<std::string>( propertyInfo[index].mMetadataID, xmpValue );
if ( propertiesExportedSuccessfully )
propertiesExportedSuccessfully->push_back( std::make_pair( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName ) );
}
break;
case kNativeType_StrLocal:
{
std::string value;
try
{
ReconcileUtils::UTF8ToLocal( xmpValue.c_str(), xmpValue.size(), &value );
outNativeMeta.setValue<std::string>( propertyInfo[index].mMetadataID, value );
if ( propertiesExportedSuccessfully )
propertiesExportedSuccessfully->push_back( std::make_pair( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName ) );
}
catch( XMP_Error& e )
{
if( e.GetID() != kXMPErr_Unavailable )
{
// rethrow exception if it wasn't caused
// by missing encoding functionality (UNIX)
throw e;
}
}
}
break;
case kNativeType_Uns64:
{
XMP_Int64 value = 0;
bool error = false;
try
{
value = SXMPUtils::ConvertToInt64( xmpValue );
}
catch( XMP_Error& e )
{
if ( e.GetID() != kXMPErr_BadParam )
{
// rethrow exception if it wasn't caused by an
// invalid parameter for the conversion
throw e;
}
error = true;
}
// Only write the value if it could be converted to a number and has a positive value
if( ! error && value >= 0 )
{
outNativeMeta.setValue<XMP_Uns64>( propertyInfo[index].mMetadataID, static_cast<XMP_Uns64>(value) );
if ( propertiesExportedSuccessfully )
propertiesExportedSuccessfully->push_back( std::make_pair( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName ) );
}
}
break;
case kNativeType_Uns32:
{
XMP_Int32 value = 0;
bool error = false;
try
{
value = (XMP_Int32)SXMPUtils::ConvertToInt( xmpValue );
}
catch( XMP_Error& e )
{
if ( e.GetID() != kXMPErr_BadParam )
{
// rethrow exception if it wasn't caused by an
// invalid parameter for the conversion
throw e;
}
error = true;
}
// Only write the value if it could be converted to a number and has a positive value
if( ! error && value >= 0 )
{
outNativeMeta.setValue<XMP_Uns32>( propertyInfo[index].mMetadataID, static_cast<XMP_Uns32>(value) );
if ( propertiesExportedSuccessfully )
propertiesExportedSuccessfully->push_back( std::make_pair( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName ) );
}
}
break;
case kNativeType_Int32:
{
XMP_Int32 value = 0;
bool error = false;
try
{
value = (XMP_Int32)SXMPUtils::ConvertToInt( xmpValue );
}
catch( XMP_Error& e )
{
if ( e.GetID() != kXMPErr_BadParam )
{
// rethrow exception if it wasn't caused by an
// invalid parameter for the conversion
throw e;
}
error = true;
}
// Only write the value if it could be converted to a number
if( ! error )
{
outNativeMeta.setValue<XMP_Int32>( propertyInfo[index].mMetadataID, static_cast<XMP_Int32>(value) );
if ( propertiesExportedSuccessfully )
propertiesExportedSuccessfully->push_back( std::make_pair( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName ) );
}
}
break;
case kNativeType_Uns16:
{
XMP_Int32 value = 0;
bool error = false;
try
{
value = (XMP_Int32)SXMPUtils::ConvertToInt( xmpValue );
}
catch( XMP_Error& e )
{
if ( e.GetID() != kXMPErr_BadParam )
{
// rethrow exception if it wasn't caused by an
// invalid parameter for the conversion
throw e;
}
error = true;
}
// Only write the value if it could be converted to a number and has a positive value
if( ! error && value >= 0 )
{
outNativeMeta.setValue<XMP_Uns16>( propertyInfo[index].mMetadataID, static_cast<XMP_Uns16>(value) );
if ( propertiesExportedSuccessfully )
propertiesExportedSuccessfully->push_back( std::make_pair( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName ) );
}
}
break;
case kNativeType_Bool:
{
bool value;
bool error = false;
try
{
value = SXMPUtils::ConvertToBool( xmpValue );
}
catch( XMP_Error& e )
{
if ( e.GetID() != kXMPErr_BadParam )
{
// rethrow exception if it wasn't caused by an
// invalid parameter for the conversion
throw e;
}
error = true;
}
// Only write the value if it could be converted to a number and has a positive value
if( ! error )
{
outNativeMeta.setValue<bool>( propertyInfo[index].mMetadataID, value );
if ( propertiesExportedSuccessfully )
propertiesExportedSuccessfully->push_back( std::make_pair( propertyInfo[index].mXMPSchemaNS, propertyInfo[index].mXMPPropName ) );
}
}
break;
default:
{
XMP_Throw( "Unknown native data type", kXMPErr_InternalFailure );
}
break;
}
}
else
{
if( propertyInfo[index].mExportPolicy == kExport_Always )
{
//
// delete native value if the corresponding XMP value doesn't exists
//
outNativeMeta.deleteValue( propertyInfo[index].mMetadataID );
}
}
}
index++;
} while( propertyInfo[index].mXMPSchemaNS != NULL );
return outNativeMeta.hasChanged();
}
//-----------------------------------------------------------------------------
//
// IReconcile::convertToASCII(...)
//
// Purpose: Converts input string to an ascii output string
// - terminates at first 0
// - replaces all non ascii with 0x3F ('?')
//
//-----------------------------------------------------------------------------
void IReconcile::convertToASCII( const std::string& input, std::string& output )
{
output.erase();
output.reserve( input.length() );
bool isUTF8 = ReconcileUtils::IsUTF8( input.c_str(), input.length() );
XMP_StringPtr iter = input.c_str();
for ( XMP_Uns32 i=0; i < input.length(); i++ )
{
XMP_Uns8 c = (XMP_Uns8) iter[i];
if ( c == 0 ) // early 0 termination, leave.
break;
if ( c > 127 ) // uft-8 multi-byte sequence.
{
if ( isUTF8 ) // skip all high bytes
{
// how many bytes in this ?
if ( c >= 0xC2 && c <= 0xDF )
i+=1; // 2-byte sequence
else if ( c >= 0xE0 && c <= 0xEF )
i+=2; // 3-byte sequence
else if ( c >= 0xF0 && c <= 0xF4 )
i+=3; // 4-byte sequence
else
continue; //invalid sequence, look for next 'low' byte ..
} // thereafter and 'else': just append a question mark:
output.append( 1, '?' );
}
else // regular valid ascii. 1 byte.
{
output.append( 1, iter[i] );
}
}
} // convertToASCII
|