summaryrefslogtreecommitdiff
path: root/XMPCore/source/DOMParserImpl.cpp
blob: d215b8cdeaf58f46e442670ff61ec4ca1cfb127a (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
// =================================================================================================
// ADOBE SYSTEMS INCORPORATED
// Copyright 2015 Adobe Systems Incorporated
// 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.
// =================================================================================================

#define IMPLEMENTATION_HEADERS_CAN_BE_INCLUDED 1
	#include "XMPCore/ImplHeaders/DOMParserImpl.h"
	#include "XMPCore/ImplHeaders/ClientDOMParserWrapperImpl.h"
#undef IMPLEMENTATION_HEADERS_CAN_BE_INCLUDED

#include "XMPCommon/Interfaces/IError_I.h"
#include "XMPCore/XMPCoreErrorCodes.h"
#include "XMPCore/Interfaces/IArrayNode_I.h"
#include "XMPCore/Interfaces/IMetadata_I.h"
#include "XMPCore/Interfaces/INodeIterator.h"
#include "XMPCommon/Interfaces/ISharedMutex.h"
#include "XMPCommon/Utilities/AutoSharedLock.h"
#include "XMPCommon/Utilities/TSmartPointers_I.h"

namespace AdobeXMPCore_Int {

	DOMParserImpl::DOMParserImpl()
		: mSharedMutex( ISharedMutex::CreateSharedMutex() ) { }

	spIDOMParser APICALL DOMParserImpl::Clone() const {
		DOMParserImpl * cloned = clone();
		if ( cloned ) {
			AutoSharedLock lock( mSharedMutex );
			cloned->mTreatKeyAsCaseInsensitiveCharBuffer = mTreatKeyAsCaseInsensitiveCharBuffer;
			cloned->mAllowDifferentValueTypesForExistingEntries = mAllowDifferentValueTypesForExistingEntries;
			if ( mKeysSet )
				cloned->mKeysSet = new KeysSet( mKeysSet->begin(), mKeysSet->end() );

			if ( mKeyValueTypeMap )
				cloned->mKeyValueTypeMap = new keyValueTypeMap( mKeyValueTypeMap->begin(), mKeyValueTypeMap->end() );

			for ( auto it = mMap.begin(), itEnd = mMap.end(); it != itEnd; ++it ) {
				cloned->mMap[ it->first ] = it->second;
			}
		}
		return MakeUncheckedSharedPointer( cloned, __FILE__, __LINE__, true );
	}

	spIMetadata APICALL DOMParserImpl::Parse( const char * buffer, sizet bufferLength ) {
		auto node = ParseAsNode( buffer, bufferLength );
		if ( node ) {
			switch ( node->GetNodeType() ) {
			case INode::kNTSimple:
			case INode::kNTArray:
				{
					spIMetadata meta = IMetadata::CreateMetadata();
					meta->AppendNode( node );
					return meta;
				}
				break;

			case INode::kNTStructure:
				{
					pIMetadata meta( NULL );
					try {
						meta = node->GetInterfacePointer< IMetadata >();
					} catch ( spcIError err ) {
						meta = NULL;
					}
					if ( meta ) {
						return MakeUncheckedSharedPointer( meta, __FILE__, __LINE__ );
					} else {
						spIMetadata meta = IMetadata::CreateMetadata();
						meta->AppendNode( node );
						return meta;
					}
				}
				break;

			default:
				NOTIFY_ERROR( IError::kEDGeneral, kGECInternalFailure, "Unhandled situation occured", IError::kESOperationFatal, false, false );
			}
		}
		return spIMetadata();
	}

	static void AppendAsChildren( const spINode & contextNode, const spINode & parsedNode ) {
		if ( !contextNode )
			NOTIFY_ERROR( IError::kEDParser, kPECInvalidContextNode, "Context Node is invalid", IError::kESOperationFatal, false, false );
		auto contextNodeType = contextNode->GetNodeType();
		if ( contextNodeType != INode::kNTStructure && contextNodeType != INode::kNTArray )
			NOTIFY_ERROR( IError::kEDParser, kPECContextNodeIsNonComposite, "Context Node is non composite", IError::kESOperationFatal,
				true, static_cast< sizet >( contextNodeType ) );
		pICompositeNode compositeContextNode = contextNode->GetInterfacePointer< ICompositeNode >();
		pIMetadata meta( NULL );
		try {
			meta = parsedNode->GetInterfacePointer< IMetadata >();
		} catch ( spcIError err ) {
			meta = NULL;
		}
		if ( meta ) {
			auto it = meta->Iterator();
			while ( it ) {
				auto childNode = it->GetNode();
				it = it->Next();
				childNode = meta->GetIMetadata_I()->RemoveNode( childNode->GetNameSpace(), childNode->GetName() );
				compositeContextNode->AppendNode( childNode );
			}
		} else {
			compositeContextNode->AppendNode( parsedNode );
		}
	}

	static void ReplaceChildren( pIArrayNode contextArrayNode, const spINode & parsedNode ) {
		contextArrayNode->Clear();
		pIMetadata meta( NULL );
		try {
			meta = parsedNode->GetInterfacePointer< IMetadata >();
		} catch ( spcIError err ) {
			meta = NULL;
		}
		if ( meta ) {
			auto it = meta->Iterator();
			while ( it ) {
				auto childNode = it->GetNode();
				it = it->Next();
				childNode = meta->GetIMetadata_I()->RemoveNode( childNode->GetNameSpace(), childNode->GetName() );
				contextArrayNode->AppendNode( childNode );	
			}
		} else {
			contextArrayNode->AppendNode( parsedNode );
		}
	}

	static void ReplaceChildren( pIStructureNode contextStructureNode, const spINode & parsedNode ) {
		pIMetadata meta( NULL );
		try {
			meta = parsedNode->GetInterfacePointer< IMetadata >();
		} catch ( spcIError err ) {
			meta = NULL;
		}
		if ( meta ) {
			auto it = meta->Iterator();
			while ( it ) {
				auto childNode = it->GetNode();
				it = it->Next();
				childNode = meta->GetIMetadata_I()->RemoveNode( childNode->GetNameSpace(), childNode->GetName() );
				contextStructureNode->ReplaceNode( childNode );		
			}
		} else {
			contextStructureNode->ReplaceNode( parsedNode );
		}
	}

	static void ReplaceChildren( const spINode & contextNode, const spINode & parsedNode ) {
		if ( !contextNode )
			NOTIFY_ERROR( IError::kEDParser, kPECInvalidContextNode, "Context Node is invalid", IError::kESOperationFatal, false, false );
		auto contextNodeType = contextNode->GetNodeType();
		if ( contextNodeType != INode::kNTStructure && contextNodeType != INode::kNTArray )
			NOTIFY_ERROR( IError::kEDParser, kPECContextNodeIsNonComposite, "Context Node is non composite", IError::kESOperationFatal,
				true, static_cast< sizet >( contextNodeType ) );
		switch ( contextNodeType ) {
		case INode::kNTArray:
			ReplaceChildren( contextNode->GetInterfacePointer< IArrayNode >(), parsedNode );
			break;

		case INode::kNTStructure:
			ReplaceChildren( contextNode->GetInterfacePointer< IStructureNode >(), parsedNode );
			break;
		}
	}

	static void AppendOrReplaceChildren( pIStructureNode contextStructureNode, const spINode & parsedNode ) {
		pIMetadata meta( NULL );
		try {
			meta = parsedNode->GetInterfacePointer< IMetadata >();
		} catch ( spcIError err ) {
			meta = NULL;
		}
		if ( meta ) {
			auto it = meta->Iterator();
			while ( it ) {
				auto childNode = it->GetNode();
				it = it->Next();
				childNode = meta->GetIMetadata_I()->RemoveNode( childNode->GetNameSpace(), childNode->GetName() );
				if ( contextStructureNode->GetIStructureNode_I()->GetNode( childNode->GetNameSpace(), childNode->GetName() ) )
					contextStructureNode->ReplaceNode( childNode );
				else
					contextStructureNode->AppendNode( childNode );	
			}
		} else {
			if ( contextStructureNode->GetIStructureNode_I()->GetNode( parsedNode->GetNameSpace(), parsedNode->GetName() ) )
				contextStructureNode->ReplaceNode( parsedNode );
			else
				contextStructureNode->AppendNode( parsedNode );
		}
	}

	static void AppendOrReplaceChildren( const spINode & contextNode, const spINode & parsedNode ) {
		if ( !contextNode )
			NOTIFY_ERROR( IError::kEDParser, kPECInvalidContextNode, "Context Node is invalid", IError::kESOperationFatal, false, false );
		auto contextNodeType = contextNode->GetNodeType();
		if ( contextNodeType != INode::kNTStructure && contextNodeType != INode::kNTArray )
			NOTIFY_ERROR( IError::kEDParser, kPECContextNodeIsNonComposite, "Context Node is non composite", IError::kESOperationFatal,
				true, static_cast< sizet >( contextNodeType ) );
		switch ( contextNodeType ) {
		case INode::kNTArray:
			ReplaceChildren( contextNode->GetInterfacePointer< IArrayNode >(), parsedNode );
			break;

		case INode::kNTStructure:
			AppendOrReplaceChildren( contextNode->GetInterfacePointer< IStructureNode >(), parsedNode );
			break;
		}
	}

	static void InsertBefore( const spINode & contextNode, const spINode & parsedNode ) {
		if ( !contextNode )
			NOTIFY_ERROR( IError::kEDParser, kPECInvalidContextNode, "Context Node is invalid", IError::kESOperationFatal, false, false );
		if ( !contextNode->IsArrayItem() )
			NOTIFY_ERROR( IError::kEDParser, kPECContextNodeParentIsNonArray, "Context Node's Parent is non array node", IError::kESOperationFatal, false, false );
		pIMetadata meta( NULL );
		try {
			meta = parsedNode->GetInterfacePointer< IMetadata >();
		} catch ( spcIError err ) {
			meta = NULL;
		}
		pIArrayNode parentArrayNode = contextNode->GetINode_I()->GetRawParentPointer()->GetInterfacePointer< IArrayNode >();
		if ( meta ) {
			auto it = meta->Iterator();
			while ( it ) {
				auto childNode = it->GetNode();
				it = it->Next();
				childNode = meta->GetIMetadata_I()->RemoveNode( childNode->GetNameSpace(), childNode->GetName() );
				parentArrayNode->InsertNodeAtIndex( childNode, contextNode->GetIndex() );
			}
		} else {
			parentArrayNode->InsertNodeAtIndex( parsedNode, contextNode->GetIndex() );
		}

	}

	static void InsertAfter( const spINode & contextNode, const spINode & parsedNode ) {
		if ( !contextNode )
			NOTIFY_ERROR( IError::kEDParser, kPECInvalidContextNode, "Context Node is invalid", IError::kESOperationFatal, false, false );
		if ( !contextNode->IsArrayItem() )
			NOTIFY_ERROR( IError::kEDParser, kPECContextNodeParentIsNonArray, "Context Node's Parent is non array node", IError::kESOperationFatal, false, false );
		pIMetadata meta( NULL );
		try {
			meta = parsedNode->GetInterfacePointer< IMetadata >();
		} catch ( spcIError err ) {
			meta = NULL;
		}
		pIArrayNode parentArrayNode = contextNode->GetINode_I()->GetRawParentPointer()->GetInterfacePointer< IArrayNode >();
		if ( meta ) {
			auto it = meta->Iterator();
			sizet index = contextNode->GetIndex() + 1;
			while ( it ) {
				auto childNode = it->GetNode();
				it = it->Next();
				childNode = meta->GetIMetadata_I()->RemoveNode( childNode->GetNameSpace(), childNode->GetName() );
				parentArrayNode->InsertNodeAtIndex( childNode, index );
				index = childNode->GetIndex() + 1;
			}
		} else {
			parentArrayNode->InsertNodeAtIndex( parsedNode, contextNode->GetIndex() + 1 );
		}
	}

	static void ReplaceNode( spINode & node, const spINode & parsedNode ) {
		if ( node && node->IsArrayItem() && node->GetNodeType() != parsedNode->GetNodeType() ) {
			NOTIFY_ERROR( IError::kEDDataModel, kDMECArrayItemTypeDifferent, "node type is different than what currently array can hold",
				IError_v1::kESOperationFatal, true, static_cast< sizet >( node->GetNodeType() ), true, static_cast< sizet >( parsedNode->GetNodeType() ) );
		}
		node = parsedNode;
	}

	void APICALL DOMParserImpl::ParseWithSpecificAction( const char * buffer, sizet bufferLength, eActionType actionType, spINode & node ) {
		auto parsedNode = ParseAsNode( buffer, bufferLength );

		if ( parsedNode ) {
			switch ( actionType ) {
			case kATAppendAsChildren:
				AppendAsChildren( node, parsedNode );
				break;

			case kATReplaceChildren:
				ReplaceChildren( node, parsedNode );
				break;

			case kATAppendOrReplaceChildren:
				AppendOrReplaceChildren( node, parsedNode );
				break;

			case kATInsertBefore:
				InsertBefore( node, parsedNode );
				break;

			case kATInsertAfter:
				InsertAfter( node, parsedNode );
				break;

			case kATReplace:
				ReplaceNode( node, parsedNode );
				break;

			default:
				NOTIFY_ERROR( IError::kEDGeneral, kGECNotImplemented, "Not yet implemented", IError::kESOperationFatal, true, static_cast< sizet >( actionType ) );
			}

		}
	}

	spISharedMutex APICALL DOMParserImpl::GetMutex() const {
		return mSharedMutex;
	}

	AdobeXMPCore::spIDOMParser IDOMParser_I::CreateDOMParser( pIClientDOMParser_base clientDOMParser ) {
		return MakeUncheckedSharedPointer( new ClientDOMParserWrapperImpl( clientDOMParser ), __FILE__, __LINE__, true );
	}


	void DOMParserImpl::SetErrorCallback(XMPMeta::ErrorCallbackInfo * ec) {
		mGenericErrorCallbackPtr = ec;
	}
}