summaryrefslogtreecommitdiff
path: root/XMPFiles/source/PluginHandler/XMPAtoms.cpp
blob: 42f97327f0ccd983f1320e0b7a5ed3c0388cd9fe (plain)
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
// =================================================================================================
// 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 "XMPAtoms.h"
#include "XMPFiles/source/HandlerRegistry.h"

using namespace Common;
#if EnablePluginManager
namespace XMP_PLUGIN
{

struct XMPAtomMapping
{
	XMP_StringPtr	name;
	XMPAtom			atom;
};

const XMPAtomMapping kXMPAtomVec[] =
{
	{ "",							emptyStr_K },
	{ "Handler",					Handler_K },
	{ "Extensions",					Extensions_K },
	{ "Extension",					Extension_K },
	{ "FormatIDs",					FormatIDs_K },
	{ "FormatID",					FormatID_K },
	{ "HandlerType",				HandlerType_K },
	{ "Priority",					OverwriteHdl_K },
	{ "HandlerFlags",				HandlerFlags_K },
	{ "HandlerFlag",				HandlerFlag_K },
	{ "SerializeOptions",			SerializeOptions_K },
	{ "SerializeOption",			SerializeOption_K },
	{ "Version",					Version_K },
	{ "CheckFormat",				CheckFormat_K },
	{ "Name",						Name_K },
	{ "Offset",						Offset_K },
	{ "Length",						Length_K },
	{ "ByteSeq",					ByteSeq_K },

	// Handler types
	{ "NormalHandler",				NormalHandler_K },
	{ "OwningHandler",				OwningHandler_K },
	{ "FolderHandler",				FolderHandler_K },

	// Handler flags
	{ "kXMPFiles_CanInjectXMP",			kXMPFiles_CanInjectXMP_K },
	{ "kXMPFiles_CanExpand",			kXMPFiles_CanExpand_K },
	{ "kXMPFiles_CanRewrite",			kXMPFiles_CanRewrite_K },
	{ "kXMPFiles_PrefersInPlace",		kXMPFiles_PrefersInPlace_K },
	{ "kXMPFiles_CanReconcile",			kXMPFiles_CanReconcile_K },
	{ "kXMPFiles_AllowsOnlyXMP",		kXMPFiles_AllowsOnlyXMP_K },
	{ "kXMPFiles_ReturnsRawPacket",		kXMPFiles_ReturnsRawPacket_K },
	{ "kXMPFiles_HandlerOwnsFile",		kXMPFiles_HandlerOwnsFile_K },
	{ "kXMPFiles_AllowsSafeUpdate",		kXMPFiles_AllowsSafeUpdate_K },
	{ "kXMPFiles_NeedsReadOnlyPacket",	kXMPFiles_NeedsReadOnlyPacket_K },
	{ "kXMPFiles_UsesSidecarXMP",		kXMPFiles_UsesSidecarXMP_K },
	{ "kXMPFiles_FolderBasedFormat",	kXMPFiles_FolderBasedFormat_K },
	{ "kXMPFiles_NeedsPreloading",		kXMPFiles_NeedsPreloading_K },

	// Serialize option
	{ "kXMP_OmitPacketWrapper",			kXMP_OmitPacketWrapper_K },
	{ "kXMP_ReadOnlyPacket",			kXMP_ReadOnlyPacket_K },
	{ "kXMP_UseCompactFormat",			kXMP_UseCompactFormat_K },
	{ "kXMP_UseCanonicalFormat",		kXMP_UseCanonicalFormat_K },
	{ "kXMP_IncludeThumbnailPad",		kXMP_IncludeThumbnailPad_K },
	{ "kXMP_ExactPacketLength",			kXMP_ExactPacketLength_K },
	{ "kXMP_OmitAllFormatting",			kXMP_OmitAllFormatting_K },
	{ "kXMP_OmitXMPMetaElement",		kXMP_OmitXMPMetaElement_K },
	{ "kXMP_EncodingMask",				kXMP_EncodingMask_K },
	{ "kXMP_EncodeUTF8",				kXMP_EncodeUTF8_K },
	{ "kXMP_EncodeUTF16Big",			kXMP_EncodeUTF16Big_K },
	{ "kXMP_EncodeUTF16Little",			kXMP_EncodeUTF16Little_K },
	{ "kXMP_EncodeUTF32Big",			kXMP_EncodeUTF32Big_K },
	{ "kXMP_EncodeUTF32Little",			kXMP_EncodeUTF32Little_K }
	
};


XMPAtomsMap* ResourceParser::msXMPAtoms = NULL;
void ResourceParser::clear()
{
	mUID.clear();
	mFileExtensions.clear();
	mFormatIDs.clear();
	mCheckFormat.clear();
	mHandler = FileHandlerSharedPtr();
	mFlags = mSerializeOption = mType = 0;
	mVersion = 0.0;
}

void ResourceParser::addHandler()
{
	if( mUID.empty() || ( mFileExtensions.empty() && mFormatIDs.empty() ) || !isValidHandlerType( mType ) || mFlags == 0 )
	{
		XMP_Throw( "Atleast one of uid, format, ext, typeStr, flags non-valid ...", kXMPErr_Unavailable );
	}
	else
	{
		mHandler->setHandlerFlags( mFlags );
		mHandler->setHandlerType( mType );
		mHandler->setSerializeOption( mSerializeOption );
		mHandler->setOverwriteHandler( mOverwriteHandler );
		if( mVersion != 0.0)
		{
			mHandler->setVersion( mVersion );
		}
		
		// A plugin could define the XMP_FileFormat value in manifest file through keyword "FormatID" and
		// file extensions for NormalHandler and OwningHandler through keyword "Extension".
		// If Both are defined then give priority to FormatID.

		std::set<XMP_FileFormat> formatIDs = mFormatIDs.empty() ? mFileExtensions : mFormatIDs;
		for( std::set<XMP_FileFormat>::const_iterator it = formatIDs.begin(); it != formatIDs.end(); ++it )
		{
			PluginManager::addFileHandler(*it, mHandler);
		}
	}
}

bool ResourceParser::parseElementAttrs( const XML_Node * xmlNode, bool isTopLevel )
{
	XMPAtom nodeAtom = getXMPAtomFromString( xmlNode->name );
	if( nodeAtom == Handler_K )
		this->clear();

	XML_cNodePos currAttr = xmlNode->attrs.begin();
	XML_cNodePos endAttr  = xmlNode->attrs.end();

	for( ; currAttr != endAttr; ++currAttr ) 
	{
		XMP_OptionBits oneflag=0;
		XMP_FileFormat formatID = kXMP_UnknownFile;
		std::string formatStr;
		XMPAtom attrAtom = getXMPAtomFromString( (*currAttr)->name );
		switch(nodeAtom)
		{
		case Handler_K:
			switch(attrAtom)
			{
			case Name_K:
				this->mUID = (*currAttr)->value;
				mHandler = FileHandlerSharedPtr( new FileHandler( this->mUID, 0, 0, mModule ) );
				break;
			case Version_K:
				sscanf( (*currAttr)->value.c_str(), "%lf", &this->mVersion );
				break;
			case HandlerType_K:
				this->mType = getXMPAtomFromString( (*currAttr)->value );
				break;
			case OverwriteHdl_K:
				this->mOverwriteHandler = ( (*currAttr)->value == "true" );
				break;
			default:
				//fix for bug 3565147: Plugin Architecture: If any unknown node is present in the Plugin Manifest then the Plugin is not loaded
				//XMP_Throw( "Invalid Attr in Handler encountered in resource file", kXMPErr_Unavailable );
				break;
			}
			break;
		case CheckFormat_K:
			switch(attrAtom)
			{
			case Offset_K:
				this->mCheckFormat.mOffset = atoi( (*currAttr)->value.c_str() );
				break;
			case Length_K:
				this->mCheckFormat.mLength = atoi( (*currAttr)->value.c_str() );
				break;
			case ByteSeq_K:
				this->mCheckFormat.mByteSeq = (*currAttr)->value;
				break;
			default:
				//fix for bug 3565147: Plugin Architecture: If any unknown node is present in the Plugin Manifest then the Plugin is not loaded
				//XMP_Throw( "Invalid Attr in CheckFormat encountered in resource file", kXMPErr_Unavailable );
				break;
			}
			break;
		case Extension_K:
			switch(attrAtom)
			{
			case Name_K:
				this->mFileExtensions.insert( HandlerRegistry::getInstance().getFileFormat( (*currAttr)->value, true) );
				break;
			default:
				//fix for bug 3565147: Plugin Architecture: If any unknown node is present in the Plugin Manifest then the Plugin is not loaded
				//XMP_Throw( "Invalid Attr in Extension encountered in resource file", kXMPErr_Unavailable );
				break;
			}
			break;
		case FormatID_K:
			switch(attrAtom)
			{
			case Name_K:
				formatStr.assign( (*currAttr)->value );
				
				// Convert string into 4-byte string by appending space '0x20' character.
				for( size_t j=formatStr.size(); j<4; j++ )
					formatStr.push_back(' ');
				
				formatID = GetUns32BE( formatStr.c_str() );
				this->mFormatIDs.insert( formatID );
				break;
			default:
				//fix for bug 3565147: Plugin Architecture: If any unknown node is present in the Plugin Manifest then the Plugin is not loaded
				//XMP_Throw( "Invalid Attr in FormatID encountered in resource file", kXMPErr_Unavailable );
				break;
			}
			break;
		case HandlerFlag_K:
			switch(attrAtom)
			{
			case Name_K:
				oneflag = getHandlerFlag( (*currAttr)->value );
				//fix for bug 3565147: Plugin Architecture: If any unknown node is present in the Plugin Manifest then the Plugin is not loaded
				//if( 0 == oneflag )
				//	XMP_Throw( "Invalid handler flag found in resource file...", kXMPErr_Unavailable );
				this->mFlags |= oneflag;
				break;
			default:
				//fix for bug 3565147: Plugin Architecture: If any unknown node is present in the Plugin Manifest then the Plugin is not loaded
				//XMP_Throw( "Invalid handler flag found in resource file...", kXMPErr_Unavailable );
				break;
			}
			break;
		case SerializeOption_K:
			switch(attrAtom)
			{
			case Name_K:
				oneflag = getSerializeOption( (*currAttr)->value );
				//fix for bug 3565147: Plugin Architecture: If any unknown node is present in the Plugin Manifest then the Plugin is not loaded
				//if( 0 == oneflag )
				//	XMP_Throw( "Invalid serialize option found in resource file...", kXMPErr_Unavailable );
				this->mSerializeOption |= oneflag;
				break;
			default:
				//fix for bug 3565147: Plugin Architecture: If any unknown node is present in the Plugin Manifest then the Plugin is not loaded
				//XMP_Throw( "Invalid handler flag found in resource file...", kXMPErr_Unavailable );
				break;
			}
			break;
		default:
			break;
		}
	}

	if( nodeAtom == CheckFormat_K )
		mHandler->addCheckFormat( mCheckFormat );

	return (nodeAtom == Handler_K) ? true : false;
}	// parseElementAttrs

void ResourceParser::parseElement( const XML_Node * xmlNode, bool isTopLevel )
{
	bool HandlerFound = this->parseElementAttrs( xmlNode, isTopLevel );
	this->parseElementList ( xmlNode, false );

	if(HandlerFound)
	{
		this->addHandler();
	}
}

void ResourceParser::parseElementList( const XML_Node * xmlParent, bool isTopLevel )
{
	initialize(); //Make sure XMPAtoms are initialized.

	XML_cNodePos currChild = xmlParent->content.begin();
	XML_cNodePos endChild  = xmlParent->content.end();

	for( ; currChild != endChild; ++currChild )
	{
		if( (*currChild)->IsWhitespaceNode() ) continue;
		this->parseElement( *currChild, isTopLevel );
	}
}

bool ResourceParser::initialize()
{
	//Check if already populated.
	if( msXMPAtoms == NULL )
	{
		msXMPAtoms = new XMPAtomsMap();
		
		//Create XMPAtomMap from XMPAtomVec, as we need to do a lot of searching while processing the resource file.
		int count = sizeof(kXMPAtomVec)/sizeof(XMPAtomMapping);
		for( int i=0; i<count; i++ )
		{
			XMP_StringPtr name = kXMPAtomVec[i].name;
			XMPAtom atom = kXMPAtomVec[i].atom;
			(*msXMPAtoms)[name] = atom;
		}
	}
	return true;
}

void ResourceParser::terminate()
{
	delete msXMPAtoms;
	msXMPAtoms = NULL;
}

XMPAtom ResourceParser::getXMPAtomFromString( const std::string & stringAtom )
{
	XMPAtomsMap::const_iterator it = msXMPAtoms->find(stringAtom);

	if( it != msXMPAtoms->end() )
		return it->second;

	return XMPAtomNull;
}

//static 
XMP_OptionBits ResourceParser::getHandlerFlag( const std::string & stringAtom )
{
	XMPAtom atom = getXMPAtomFromString( stringAtom );

	if( !isValidXMPAtom(atom) )
		return 0;

	switch( atom )
	{
	case kXMPFiles_CanInjectXMP_K:
		return kXMPFiles_CanInjectXMP;
	case kXMPFiles_CanExpand_K:
		return kXMPFiles_CanExpand;
	case kXMPFiles_CanRewrite_K:
		return kXMPFiles_CanRewrite;
	case kXMPFiles_PrefersInPlace_K:
		return kXMPFiles_PrefersInPlace;
	case kXMPFiles_CanReconcile_K:
		return kXMPFiles_CanReconcile;
	case kXMPFiles_AllowsOnlyXMP_K:
		return kXMPFiles_AllowsOnlyXMP;
	case kXMPFiles_ReturnsRawPacket_K:
		return kXMPFiles_ReturnsRawPacket;
	case kXMPFiles_HandlerOwnsFile_K:
		return kXMPFiles_HandlerOwnsFile;
	case kXMPFiles_AllowsSafeUpdate_K:
		return kXMPFiles_AllowsSafeUpdate;
	case kXMPFiles_NeedsReadOnlyPacket_K:
		return kXMPFiles_NeedsReadOnlyPacket;
	case kXMPFiles_UsesSidecarXMP_K:
		return kXMPFiles_UsesSidecarXMP;
	case kXMPFiles_FolderBasedFormat_K:
		return kXMPFiles_FolderBasedFormat;
	case kXMPFiles_NeedsPreloading_K:
		return kXMPFiles_NeedsPreloading;
	default:
		//fix for bug 3565147: Plugin Architecture: If any unknown node is present in the Plugin Manifest then the Plugin is not loaded
		//XMP_Throw( "Invalid PluginhandlerFlag ...", kXMPErr_Unavailable );
		return 0;
	}
}

//static 
XMP_OptionBits ResourceParser::getSerializeOption( const std::string & stringAtom )
{
	XMPAtom atom = getXMPAtomFromString( stringAtom );

	if( !isValidXMPAtom(atom) )
		return 0;

	switch( atom )
	{
	case kXMP_OmitPacketWrapper_K:
		return kXMP_OmitPacketWrapper;
    case kXMP_ReadOnlyPacket_K:
		return kXMP_ReadOnlyPacket;
    case kXMP_UseCompactFormat_K:
		return kXMP_UseCompactFormat;
    case kXMP_UseCanonicalFormat_K:
		return kXMP_UseCanonicalFormat;
    case kXMP_IncludeThumbnailPad_K:
		return kXMP_IncludeThumbnailPad;
    case kXMP_ExactPacketLength_K:
		return kXMP_ExactPacketLength;
    case kXMP_OmitAllFormatting_K:
		return kXMP_OmitAllFormatting;
    case kXMP_OmitXMPMetaElement_K:
		return kXMP_OmitXMPMetaElement;
    case kXMP_EncodingMask_K:
		return kXMP_EncodingMask;
    case kXMP_EncodeUTF8_K:
		return kXMP_EncodeUTF8;
    case kXMP_EncodeUTF16Big_K:
		return kXMP_EncodeUTF16Big;
    case kXMP_EncodeUTF16Little_K:
		return kXMP_EncodeUTF16Little;
    case kXMP_EncodeUTF32Big_K:
		return kXMP_EncodeUTF32Big;
    case kXMP_EncodeUTF32Little_K:
		return kXMP_EncodeUTF32Little;
	default:
		//fix for bug 3565147: Plugin Architecture: If any unknown node is present in the Plugin Manifest then the Plugin is not loaded
		//XMP_Throw( "Invalid Serialize Option ...", kXMPErr_Unavailable );
		return 0;
	}
}

XMP_FileFormat ResourceParser::getPluginFileFormat( const std::string & fileExt, bool AddIfNotFound )
{
	XMP_FileFormat format = kXMP_UnknownFile;

	if( msXMPAtoms )
	{
		XMPAtomsMap::const_iterator iter = msXMPAtoms->find( fileExt );
		if( iter != msXMPAtoms->end() )
		{
			format = iter->second;
		}
		else if( AddIfNotFound )
		{
			std::string formatStr(fileExt);
			MakeUpperCase(&formatStr);
			for( size_t j=formatStr.size(); j<4; j++ )	// Convert "pdf" to "PDF "
				formatStr.push_back(' ');
			format = GetUns32BE( formatStr.c_str() ); //Convert "PDF " into kXMP_PDFFile
			(*msXMPAtoms)[ fileExt ] = format;
		}
	}
	
	return format;
}

} //namespace XMP_PLUGIN
#endif