summaryrefslogtreecommitdiff
path: root/XMPCore/source/NameSpacePrefixMapImpl.cpp
blob: f95967e3f3871eaed5add57de29906a7613dc710 (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
// =================================================================================================
// 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/NameSpacePrefixMapImpl.h"
#undef IMPLEMENTATION_HEADERS_CAN_BE_INCLUDED

#include "XMPCommon/Interfaces/IUTF8String_I.h"
#include "XMPCommon/Interfaces/IError_I.h"
#include "XMPCommon/Utilities/AutoSharedLock.h"
#include "XMPCommon/Utilities/TSmartPointers_I.h"

#include "source/XMP_LibUtils.hpp"
#include "source/UnicodeInlines.incl_cpp"

#include "XMPCore/XMPCoreErrorCodes.h"
#include <cstring>

namespace AdobeXMPCore_Int {

	static sizet ActualLength( const char * parameter, sizet length ) {
		if ( length == npos && parameter ) return strlen( parameter );
		return length;
	}

	static bool VerifyParameters( bool prefixPresent, bool nameSpacePresent, const spcIUTF8String & prefix, const spcIUTF8String & nameSpace ) {
		bool allOk = true;
		if ( prefixPresent ) {
			if ( !prefix ) {
				NOTIFY_WARNING( IError_v1::kEDGeneral, kGECParametersNotAsExpected, "prefix is NULL", false, false );
				allOk = false;
			} else {
				if ( prefix->size() == 0) {
					NOTIFY_WARNING( IError_v1::kEDGeneral, kGECParametersNotAsExpected, "prefixLength is 0", false, false );
					allOk = false;
				}
			}
		}

		if ( nameSpacePresent ) {
			if ( !nameSpace ) {
				if ( !nameSpace ) {
					NOTIFY_WARNING( IError_v1::kEDGeneral, kGECParametersNotAsExpected, "nameSpace is NULL", false, false );
					allOk = false;
				} else {
					if ( nameSpace->size() == 0 ) {
						NOTIFY_WARNING( IError_v1::kEDGeneral, kGECParametersNotAsExpected, "nameSpaceLength is 0", false, false );
						allOk = false;
					}
				}
			}
		}

		return allOk;
	}

	static bool VerifyParameters( bool prefixPresent, bool nameSpacePresent, const char * prefix, const char * nameSpace, sizet prefixLength, sizet nameSpaceLength ) {
		bool allOk = true;
		if ( prefixPresent ) {
			if ( !prefix ) {
				NOTIFY_WARNING( IError_v1::kEDGeneral, kGECParametersNotAsExpected, "prefix is NULL", true, ( void * ) prefix );
				allOk = false;
			} else {
				if ( prefixLength == npos ) prefixLength = strlen( prefix );
				if ( prefixLength == 0 ) {
					NOTIFY_WARNING( IError_v1::kEDGeneral, kGECParametersNotAsExpected, "prefixLength is 0", true, prefixLength );
					allOk = false;
				}
			}
		}

		if ( nameSpacePresent ) {
			if ( !nameSpace ) {
				NOTIFY_WARNING( IError_v1::kEDGeneral, kGECParametersNotAsExpected, "nameSpace is NULL", true, ( void * ) nameSpace );
				allOk = false;
			} else {
				if ( nameSpaceLength == npos ) nameSpaceLength = strlen( nameSpace );
				if ( nameSpaceLength == 0 ) {
					NOTIFY_WARNING( IError_v1::kEDGeneral, kGECParametersNotAsExpected, "nameSpaceLength is 0", true, nameSpaceLength );
					allOk = false;
				}
			}
		}

		return allOk;
	}

	NameSpacePrefixMapImpl::NameSpacePrefixMapImpl() {}

	// All virtual functions
	bool APICALL NameSpacePrefixMapImpl::Insert( const char * prefix, sizet prefixLength, const char * nameSpace, sizet nameSpaceLength ) {
		if ( VerifyParameters( true, true, prefix, nameSpace, prefixLength, nameSpaceLength ) ) {
			prefixLength = ActualLength( prefix, prefixLength );
			nameSpaceLength = ActualLength( nameSpace, nameSpaceLength );
			try {
				VerifySimpleXMLName( prefix, prefix + prefixLength );
			} catch ( ... ) {
				NOTIFY_ERROR( IError_v1::kEDDataModel, kDMECBadXPath, "prefix is not a valid XML Name", IError_v1::kESOperationFatal, true, prefix );
				return false;
			}
			spcIUTF8String prefixStr = IUTF8String_I::CreateUTF8String( prefix, prefixLength );
			spcIUTF8String nameSpaceStr = IUTF8String_I::CreateUTF8String( nameSpace, nameSpaceLength );

			AutoSharedLock( mSharedMutex, true );

			// remove old entries
			if ( mPrefixToNameSpaceMap.find( prefixStr ) != mPrefixToNameSpaceMap.end() ) {
				spcIUTF8String oldValue = mPrefixToNameSpaceMap[ prefixStr ];
				mPrefixToNameSpaceMap.erase( prefixStr );
				mNameSpaceToPrefixMap.erase( oldValue );
			}

			if ( mNameSpaceToPrefixMap.find( nameSpaceStr ) != mNameSpaceToPrefixMap.end() ) {
				spcIUTF8String oldValue = mNameSpaceToPrefixMap[ nameSpaceStr ];
				mPrefixToNameSpaceMap.erase( oldValue );
				mNameSpaceToPrefixMap.erase( nameSpaceStr );
			}

			mNameSpaceToPrefixMap[ nameSpaceStr ] = prefixStr;
			mPrefixToNameSpaceMap[ prefixStr ] = nameSpaceStr;
			return true;
		}
		return false;
	}

	bool APICALL NameSpacePrefixMapImpl::RemovePrefix( const char * prefix, sizet prefixLength ) {
		if ( VerifyParameters( true, false, prefix, NULL, prefixLength, 0 ) ) {
			spcIUTF8String prefixStr = IUTF8String_I::CreateUTF8String( prefix, prefixLength );
			AutoSharedLock lock( mSharedMutex, true );
			if ( mPrefixToNameSpaceMap.find( prefixStr ) != mPrefixToNameSpaceMap.end() ) {
				spcIUTF8String nameSpaceStr = mPrefixToNameSpaceMap[ prefixStr ];
				mPrefixToNameSpaceMap.erase( prefixStr );
				mNameSpaceToPrefixMap.erase( nameSpaceStr );
				return true;
			}
		}
		return false;
	}

	bool APICALL NameSpacePrefixMapImpl::RemoveNameSpace( const char * nameSpace, sizet nameSpaceLength ) {
		if ( VerifyParameters( false, true, NULL, nameSpace, 0, nameSpaceLength ) ) {
			spcIUTF8String nameSpaceStr = IUTF8String_I::CreateUTF8String( nameSpace, nameSpaceLength );
			AutoSharedLock lock( mSharedMutex, true );
			if ( mNameSpaceToPrefixMap.find( nameSpaceStr ) != mNameSpaceToPrefixMap.end() ) {
				spcIUTF8String prefixStr = mNameSpaceToPrefixMap[ nameSpaceStr ];
				mPrefixToNameSpaceMap.erase( prefixStr );
				mNameSpaceToPrefixMap.erase( nameSpaceStr );
				return true;
			}
		}
		return false;
	}

	bool APICALL NameSpacePrefixMapImpl::IsPrefixPresent( const char * prefix, sizet prefixLength ) const {
		if ( VerifyParameters( true, false, prefix, NULL, prefixLength, 0 ) ) {
			spcIUTF8String prefixStr = IUTF8String_I::CreateUTF8String( prefix, prefixLength );
			AutoSharedLock lock( mSharedMutex, true );
			return mPrefixToNameSpaceMap.find( prefixStr ) != mPrefixToNameSpaceMap.end();
		}
		return false;
	}

	bool APICALL NameSpacePrefixMapImpl::IsNameSpacePresent( const char * nameSpace, sizet nameSpaceLength ) const {
		if ( VerifyParameters( false, true, NULL, nameSpace, 0, nameSpaceLength ) ) {
			spcIUTF8String nameSpaceStr = IUTF8String_I::CreateUTF8String( nameSpace, nameSpaceLength );
			AutoSharedLock lock( mSharedMutex, true );
			return mNameSpaceToPrefixMap.find( nameSpaceStr ) != mNameSpaceToPrefixMap.end();
		}
		return false;
	}

	spcIUTF8String APICALL NameSpacePrefixMapImpl::GetNameSpace( const char * prefix, sizet prefixLength ) const {
		if ( VerifyParameters( true, false, prefix, NULL, prefixLength, 0 ) ) {
			spcIUTF8String prefixStr = IUTF8String_I::CreateUTF8String( prefix, prefixLength );
			AutoSharedLock lock( mSharedMutex, true );
			auto it = mPrefixToNameSpaceMap.find( prefixStr );
			if ( it != mPrefixToNameSpaceMap.end() )
				return it->second;
		}
		return spcIUTF8String();
	}

	spcIUTF8String APICALL NameSpacePrefixMapImpl::GetNameSpace( const spcIUTF8String & prefix ) const {
		if ( VerifyParameters( true, false, prefix, spcIUTF8String() ) ) {
			AutoSharedLock lock( mSharedMutex, true );
			auto it = mPrefixToNameSpaceMap.find( prefix );
			if ( it != mPrefixToNameSpaceMap.end() )
				return it->second;
		}
		return spcIUTF8String();
	}

	spcIUTF8String APICALL NameSpacePrefixMapImpl::GetPrefix( const char * nameSpace, sizet nameSpaceLength ) const {
		if ( VerifyParameters( false, true, NULL, nameSpace, 0, nameSpaceLength ) ) {
			spcIUTF8String nameSpaceStr = IUTF8String_I::CreateUTF8String( nameSpace, nameSpaceLength );
			AutoSharedLock lock( mSharedMutex, true );
			auto it = mNameSpaceToPrefixMap.find( nameSpaceStr );
			if ( it != mNameSpaceToPrefixMap.end() )
				return it->second;
		}
		return spcIUTF8String();
	}

	spcIUTF8String APICALL NameSpacePrefixMapImpl::GetPrefix( const spcIUTF8String & nameSpace ) const {
		if ( VerifyParameters( false, true, spcIUTF8String(), nameSpace ) ) {
			AutoSharedLock lock( mSharedMutex, true );
			auto it = mNameSpaceToPrefixMap.find( nameSpace );
			if ( it != mNameSpaceToPrefixMap.end() )
				return it->second;
		}
		return spcIUTF8String();
	}

	sizet APICALL NameSpacePrefixMapImpl::Size() const __NOTHROW__ {
		AutoSharedLock lock( mSharedMutex, false );
		return mNameSpaceToPrefixMap.size();
	}

	void APICALL NameSpacePrefixMapImpl::Clear() __NOTHROW__ {
		AutoSharedLock lock( mSharedMutex, true );
		mNameSpaceToPrefixMap.clear();
		mPrefixToNameSpaceMap.clear();
	}

	spINameSpacePrefixMap APICALL NameSpacePrefixMapImpl::Clone() const {
		NameSpacePrefixMapImpl * ptr = new NameSpacePrefixMapImpl();
		if ( !ptr )
			NOTIFY_ERROR( IError_v1::kEDMemoryManagement, kMMECAllocationFailure, "Unable to allocate required memory", IError_v1::kESProcessFatal, false, false );
		AutoSharedLock lock( mSharedMutex, false );

		// Replacing the shallow copy with deep copy
		auto iterator = mPrefixToNameSpaceMap.begin();
		while ( iterator != mPrefixToNameSpaceMap.end() ) {
			ptr->mPrefixToNameSpaceMap[ iterator->first ] = iterator->second;
			iterator++;
		}

		iterator = mNameSpaceToPrefixMap.begin();
		while ( iterator != mNameSpaceToPrefixMap.end() ) {
			ptr->mNameSpaceToPrefixMap[ iterator->first ] = iterator->second;
			iterator++;
		}

		return MakeUncheckedSharedPointer( ptr, __FILE__, __LINE__ );
	}

	void APICALL NameSpacePrefixMapImpl::Merge( const spcINameSpacePrefixMap & otherMap ) {
		if ( otherMap ) {
			const NameSpacePrefixMapImpl * otherMapPtr = dynamic_cast< const NameSpacePrefixMapImpl * >( otherMap.get() );
			for (auto it = otherMapPtr->mPrefixToNameSpaceMap.begin(), itEnd = otherMapPtr->mPrefixToNameSpaceMap.end(); it != itEnd; ++it) {
				Insert( it->first->c_str(), it->first->size(), it->second->c_str(), it->second->size() );
			}
		}
	}

	// All static functions of _I class.

	static spINameSpacePrefixMap ManageDefaultNameSpacePrefixMap( bool destroy = false ) {
		static spINameSpacePrefixMap sDefaultNameSpacePrefixMap;
		if ( destroy ) {
			sDefaultNameSpacePrefixMap.reset();
		} else {
			if ( !sDefaultNameSpacePrefixMap ) {
				sDefaultNameSpacePrefixMap = INameSpacePrefixMap_I::CreateNameSpacePrefixMap();
				sDefaultNameSpacePrefixMap->EnableThreadSafety();
			}
		}
		return sDefaultNameSpacePrefixMap;
	}

	void INameSpacePrefixMap_I::CreateDefaultNameSpacePrefixMap() {
		ManageDefaultNameSpacePrefixMap( false );
	}

	void INameSpacePrefixMap_I::DestroyDefaultNameSapcePrefixMap() {
		ManageDefaultNameSpacePrefixMap( true );
	}

	bool INameSpacePrefixMap_I::InsertInDefaultNameSpacePrefixMap( const char * prefix, sizet prefixLength, const char * nameSpace, sizet nameSpaceLength ) {
		return ManageDefaultNameSpacePrefixMap( false )->Insert( prefix, prefixLength, nameSpace, nameSpaceLength );
	}
}

#if BUILDING_XMPCORE_LIB || SOURCE_COMPILING_XMPCORE_LIB

namespace AdobeXMPCore {
	using AdobeXMPCore_Int::MakeUncheckedSharedPointer;

	spINameSpacePrefixMap INameSpacePrefixMap_v1::CreateNameSpacePrefixMap() {
		return MakeUncheckedSharedPointer( new AdobeXMPCore_Int::NameSpacePrefixMapImpl(), __FILE__, __LINE__, true );
	}

	spcINameSpacePrefixMap INameSpacePrefixMap_v1::GetDefaultNameSpacePrefixMap() {
		return AdobeXMPCore_Int::ManageDefaultNameSpacePrefixMap( false );
	}

	spINameSpacePrefixMap INameSpacePrefixMap_v1::MakeShared( pINameSpacePrefixMap_base ptr ) {
		if ( !ptr ) return spINameSpacePrefixMap();
		pINameSpacePrefixMap p = INameSpacePrefixMap::GetInterfaceVersion() > 1 ? ptr->GetInterfacePointer< INameSpacePrefixMap >() : ptr;
		return MakeUncheckedSharedPointer( p, __FILE__, __LINE__, false );
	}
}

#endif  // BUILDING_XMPCORE_LIB || SOURCE_COMPILING_XMPCORE_LIB