summaryrefslogtreecommitdiff
path: root/samples/source/ReadingXMPNewDOM.cpp
blob: 99d636ae1882cadcf3c8caf786debd42191ca16b (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
// =================================================================================================
// Copyright 2008 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. If you have received this file from a source other 
// than Adobe, then your use, modification, or distribution of it requires the prior written permission
// of Adobe.
// =================================================================================================

/**
* Tutorial solution for the Walkthrough 1 in the XMP Programmers Guide, Opening files and reading XMP.
* Demonstrates the basic use of the XMPFiles and XMPCore components, obtaining read-only XMP from a file
* and examining it through the XMP object.
*/

#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <fstream>
#include <iostream>

//#define ENABLE_XMP_CPP_INTERFACE 1;

// Must be defined to instantiate template classes
#define TXMP_STRING_TYPE std::string 

// Must be defined to give access to XMPFiles
#define XMP_INCLUDE_XMPFILES 1 

#define ENABLE_NEW_DOM_MODEL 1

// Ensure XMP templates are instantiated
#include "public/include/XMP.incl_cpp"

// Provide access to the API
#include "public/include/XMP.hpp"

#include "XMPCore/Interfaces/IDOMImplementationRegistry.h"
#include "XMPCore/Interfaces/IDOMParser.h"
#include "XMPCore/Interfaces/IDOMSerializer.h"
#include "XMPCore/Interfaces/IMetadata.h"
#include "XMPCore/Interfaces/ICoreObjectFactory.h"
#include "XMPCore/Interfaces/ISimpleNode.h"
#include "XMPCore/Interfaces/IStructureNode.h"
#include "XMPCore/Interfaces/IArrayNode.h"
#include "XMPCore/Interfaces/INameSpacePrefixMap.h"
#include "XMPCommon/Interfaces/IUTF8String.h"
#include "XMPCore/Interfaces/INodeIterator.h"

using namespace std;
using namespace AdobeXMPCore;


void GetLocalizedText(spIArrayNode titleNode, const char* specificLang, const char* genericLang, string lang)
{
	AdobeXMPCore::spINode currItem;
	const size_t itemLim = titleNode->ChildCount();
	size_t itemNum;

	spISimpleNode xmlLangQualifierNode, currItemNode;
	for (itemNum = 1; itemNum <= itemLim; ++itemNum)
	{
		currItem = titleNode->GetNodeAtIndex(itemNum);
		if (currItem != NULL)
		{
			xmlLangQualifierNode = currItem->QualifiersIterator()->GetNode()->ConvertToSimpleNode();
			if (!strcmp(xmlLangQualifierNode->GetValue()->c_str(), specificLang)) {
				currItemNode = currItem->ConvertToSimpleNode();
				cout << "dc:title in" <<" " << lang <<" " << currItemNode->GetValue()->c_str() << endl;
				return;
			}
		}
	}

	if (*genericLang != 0)
	{
		// Look for the first partial match with the generic language.
		const size_t genericLen = strlen(genericLang);
		for (itemNum = 1; itemNum <= itemLim; ++itemNum) {
			currItem = titleNode->GetNodeAtIndex(itemNum);
			xmlLangQualifierNode = currItem->QualifiersIterator()->GetNode()->ConvertToSimpleNode();
			XMP_StringPtr currLang = xmlLangQualifierNode->GetValue()->c_str();
			const size_t currLangSize = xmlLangQualifierNode->GetValue()->size();
			if ((currLangSize >= genericLen) &&
				!strncmp(currLang, genericLang, genericLen) &&
				((currLangSize == genericLen) || (currLang[genericLen] == '-')))
			{
				currItemNode = currItem->ConvertToSimpleNode();
				cout << "dc:title in" <<" " << lang << " " << currItemNode->GetValue()->c_str() << endl;
				return;
			}
		}
	}

	// Look for an 'x-default' item.
	for (itemNum = 1; itemNum <= itemLim; ++itemNum) {
		currItem = titleNode->GetNodeAtIndex(itemNum);
		xmlLangQualifierNode = currItem->QualifiersIterator()->GetNode()->ConvertToSimpleNode();
		if (!strcmp(xmlLangQualifierNode->GetValue()->c_str(), "x-default")) {
			currItemNode = currItem->ConvertToSimpleNode();
			cout << "dc:title in" <<" " << lang <<" " << currItemNode->GetValue()->c_str() << endl;
			return;
		}
	}

	// Everything failed, choose the first item.
	currItem = titleNode->GetNodeAtIndex(1);
	currItemNode = currItem->ConvertToSimpleNode();
	cout << "dc:title in" <<" " <<lang << " "<< currItemNode->GetValue() << endl;
	return;

}



void writeRDFToFile(string * rdf, string filename)
{
	ofstream outFile;
	outFile.open(filename.c_str(), ios::out);
	outFile << *rdf;
	outFile.close();
}


/**
*	Initializes the toolkit and attempts to open a file for reading metadata.Initially
* an attempt to open the file is done with a handler, if this fails then the file is opened with
* packet scanning.Once the file is open several properties are read and displayed in the console.
* The XMP object is then dumped to a text file and the resource file is closed.
*/

int main(int argc, const char * argv[])
{

	if (argc != 2) // 2 := command and 1 parameter
	{
		cout << "usage: ReadingXMP (filename)" << endl;
		return 0;
	}

	string filename = string(argv[1]);

	if (!SXMPMeta::Initialize())
	{
		cout << "Could not initialize toolkit!";
		return -1;
	}
	XMP_OptionBits options = 0;
#if UNIX_ENV
	options |= kXMPFiles_ServerMode;
#endif
	// Must initialize SXMPFiles before we use it
	if (!SXMPFiles::Initialize(options))
	{
		cout << "Could not initialize SXMPFiles.";
		return -1;
	}

	try
	{
		// Options to open the file with - read only and use a file handler
		XMP_OptionBits opts = kXMPFiles_OpenForRead | kXMPFiles_OpenUseSmartHandler;

		bool ok;
		SXMPFiles myFile;
		std::string status = "";

		// First we try and open the file
		ok = myFile.OpenFile(filename, kXMP_UnknownFile, opts);
		if (!ok)
		{
			status += "No smart handler available for " + filename + "\n";
			status += "Trying packet scanning.\n";

			// Now try using packet scanning
			opts = kXMPFiles_OpenForUpdate | kXMPFiles_OpenUsePacketScanning;
			ok = myFile.OpenFile(filename, kXMP_UnknownFile, opts);
		}
		
		// If the file is open then read the metadata
		if (ok)
		{
			
			cout << status << endl;
			cout << filename << " is opened successfully" << endl;
			// Create the xmp object and get the xmp data
			SXMPMeta meta;
			myFile.GetXMP(&meta);
			string buffer;
			meta.SerializeToBuffer(&buffer);
			writeRDFToFile(&buffer, "Image1RDF.txt");
			AdobeXMPCore::spIDOMImplementationRegistry DOMRegistry = AdobeXMPCore::IDOMImplementationRegistry::GetDOMImplementationRegistry();
			AdobeXMPCore:: spIDOMParser parser = DOMRegistry->GetParser("rdf");
			AdobeXMPCore ::spIMetadata metaNode = parser->Parse(buffer.c_str(), buffer.size());

			// Read a simple property
			AdobeXMPCore::spISimpleNode simpleNode = metaNode->GetSimpleNode(kXMP_NS_XMP, AdobeXMPCommon::npos, "CreatorTool", AdobeXMPCommon::npos);
			if (simpleNode != NULL)
			{
				string simpleNodeValue = simpleNode->GetValue()->c_str();
				cout << "CreatorTool = " << simpleNodeValue << endl;
			}

			// Get the first element in the dc:creator array
			AdobeXMPCore::spIArrayNode arrayNode = metaNode->GetArrayNode(kXMP_NS_DC, AdobeXMPCommon::npos, "creator", AdobeXMPCommon::npos);
			if (arrayNode != NULL)
			{
				AdobeXMPCore::spISimpleNode arrayNodeChild = arrayNode->GetSimpleNodeAtIndex(1);
				if (arrayNodeChild != NULL)
				{
					string arrayNodeChildValue = arrayNodeChild->GetValue()->c_str();
					cout << "dc:creator = " << arrayNodeChildValue << endl;
				}
			}

			// Get the the entire dc:subject array 
			AdobeXMPCore::spIArrayNode subjectArray = metaNode->GetArrayNode(kXMP_NS_DC, AdobeXMPCommon::npos, "subject", AdobeXMPCommon::npos);
			if (subjectArray != NULL)
			{
				sizet arraySize = subjectArray->ChildCount();
				for (sizet i = 1; i <= arraySize; i++)
				{
					AdobeXMPCore::spISimpleNode subjectChild = subjectArray->GetSimpleNodeAtIndex(i);
					if (subjectChild != NULL)
					{
						string propValue = subjectChild->GetValue()->c_str();
						cout << "dc:subject[" << i << "] = " << propValue << endl;
					}
				}
			}
			
		    // Get the dc:title for English and French
			
			
			AdobeXMPCore::spIArrayNode titleNode = metaNode->GetArrayNode(kXMP_NS_DC, AdobeXMPCommon::npos, "title", AdobeXMPCommon::npos);
			if (titleNode != NULL)
			{
				GetLocalizedText(titleNode, "en-US", "en", "English");
				GetLocalizedText(titleNode, "fr-FR", "fr", "French");
			}
			
			
		    
			// Get dc:MetadataDate			
			AdobeXMPCore::spISimpleNode dateNode = metaNode->GetSimpleNode(kXMP_NS_XMP, AdobeXMPCommon::npos, "MetadataDate", AdobeXMPCommon::npos);
			if(dateNode != NULL)
			{
				string date = dateNode->GetValue()->c_str();
				cout << "meta:MetadataDate = " << date << endl;
			}
			

			// See if the flash struct exists and see if it was used
			AdobeXMPCore::spIStructureNode flashNode = metaNode->GetStructureNode(kXMP_NS_EXIF, AdobeXMPCommon::npos, "Flash", AdobeXMPCommon::npos);
			if (flashNode != NULL)
			{
				AdobeXMPCore::spISimpleNode field = flashNode->GetSimpleNode(kXMP_NS_EXIF, AdobeXMPCommon::npos, "Fired", AdobeXMPCommon::npos);
				if (field != NULL)
				{
					string fieldValue = field->GetValue()->c_str();
					cout << "Flash Used = " << fieldValue << endl;
				}
			}
			// Close the SXMPFile.  The resource file is already closed if it was
			// opened as read only but this call must still be made.
			myFile.CloseFile();

		}
		else
		{
			cout << "Unable to open " << filename << endl;
		}
	}
	catch (XMP_Error & e)
	{
		cout << "ERROR: " << e.GetErrMsg() << endl;
	}

	// Terminate the toolkit
	SXMPFiles::Terminate();
	SXMPMeta::Terminate();

	return 0;
}