|
1 /* |
|
2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: CXcapUriParser |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 // INCLUDES |
|
22 #include <f32file.h> |
|
23 #include "XdmNamespace.h" |
|
24 #include "XcapUriParser.h" |
|
25 #include "XcapHttpOperation.h" |
|
26 #include "XcapEngineDefines.h" |
|
27 #include "XdmNodeAttribute.h" |
|
28 #include "XcapOperationFactory.h" |
|
29 |
|
30 // ================= MEMBER FUNCTIONS ======================= |
|
31 |
|
32 // --------------------------------------------------------- |
|
33 // C++ constructor can NOT contain any code, that |
|
34 // might leave. |
|
35 // --------------------------------------------------------- |
|
36 // |
|
37 CXcapUriParser::CXcapUriParser( CXdmDocument& aTargetDoc, |
|
38 CXcapOperationFactory& aOperationFactory ) : |
|
39 iTargetDoc( aTargetDoc ), |
|
40 iNodeType( EXdmElementUnspecified ), |
|
41 iOperationFactory( aOperationFactory ) |
|
42 { |
|
43 } |
|
44 |
|
45 // --------------------------------------------------------- |
|
46 // Two-phased constructor. |
|
47 // |
|
48 // --------------------------------------------------------- |
|
49 // |
|
50 CXcapUriParser* CXcapUriParser::NewL( CXdmDocument& aTargetDoc, |
|
51 CXcapOperationFactory& aOperationFactory ) |
|
52 { |
|
53 CXcapUriParser* self = new ( ELeave ) CXcapUriParser( aTargetDoc, aOperationFactory ); |
|
54 CleanupStack::PushL( self ); |
|
55 self->ConstructL(); |
|
56 CleanupStack::Pop(); |
|
57 return self; |
|
58 } |
|
59 |
|
60 // --------------------------------------------------------- |
|
61 // Destructor |
|
62 // |
|
63 // --------------------------------------------------------- |
|
64 // |
|
65 CXcapUriParser::~CXcapUriParser() |
|
66 { |
|
67 #ifdef _DEBUG |
|
68 iOperationFactory.WriteToLog( _L8( "CXcapUriParser::~CXcapUriParser()" ) ); |
|
69 #endif |
|
70 delete iUriBuffer; |
|
71 delete iCompleteUri; |
|
72 iNamespaceMappings.ResetAndDestroy(); |
|
73 iNamespaceMappings.Close(); |
|
74 } |
|
75 |
|
76 // --------------------------------------------------------- |
|
77 // CXcapUriParser::ConstructL |
|
78 // |
|
79 // --------------------------------------------------------- |
|
80 // |
|
81 void CXcapUriParser::ConstructL() |
|
82 { |
|
83 } |
|
84 |
|
85 // --------------------------------------------------------- |
|
86 // CXcapUriParser::AddNamespaceMappingL |
|
87 // |
|
88 // --------------------------------------------------------- |
|
89 // |
|
90 void CXcapUriParser::AddNamespaceMappingL( const TDesC8& aUri, const TDesC8& aPrefix ) |
|
91 { |
|
92 CXdmNamespace* ns = CXdmNamespace::NewL( aUri, aPrefix ); |
|
93 CleanupStack::PushL( ns ); |
|
94 User::LeaveIfError( iNamespaceMappings.Append( ns ) ); |
|
95 CleanupStack::Pop(); //ns |
|
96 } |
|
97 |
|
98 // --------------------------------------------------------- |
|
99 // CXcapUriParser::AppendNamespaceMappingsL |
|
100 // |
|
101 // --------------------------------------------------------- |
|
102 // |
|
103 void CXcapUriParser::AppendNamespaceMappingsL() |
|
104 { |
|
105 CXdmNamespace* ns = NULL; |
|
106 const TInt count = iNamespaceMappings.Count(); |
|
107 iUriBuffer->InsertL( iBufferPosition, KXcapUriQMark ); |
|
108 iBufferPosition = iBufferPosition + KXcapUriQMark().Length(); |
|
109 for( TInt i = 0;i < count;i++ ) |
|
110 { |
|
111 ns = iNamespaceMappings[i]; |
|
112 iUriBuffer->InsertL( iBufferPosition, KXcapUriXmlns ); |
|
113 iBufferPosition = iBufferPosition + KXcapUriXmlns().Length(); |
|
114 TPtrC8 prefix( ns->Prefix() ); |
|
115 iUriBuffer->InsertL( iBufferPosition, prefix ); |
|
116 iBufferPosition = iBufferPosition + prefix.Length(); |
|
117 iUriBuffer->InsertL( iBufferPosition, KXcapUriEquals ); |
|
118 iBufferPosition = iBufferPosition + KXcapUriEquals().Length(); |
|
119 TPtrC8 uri( ns->Uri() ); |
|
120 iUriBuffer->InsertL( iBufferPosition, uri ); |
|
121 iBufferPosition = iBufferPosition + uri.Length(); |
|
122 iUriBuffer->InsertL( iBufferPosition, KXcapUriQuote ); |
|
123 iBufferPosition = iBufferPosition + KXcapUriQuote().Length(); |
|
124 iUriBuffer->InsertL( iBufferPosition, KXcapParenthClose ); |
|
125 iBufferPosition = iBufferPosition + KXcapParenthClose().Length(); |
|
126 } |
|
127 } |
|
128 |
|
129 // --------------------------------------------------------- |
|
130 // CXcapUriParser::FinaliseL |
|
131 // |
|
132 // --------------------------------------------------------- |
|
133 // |
|
134 void CXcapUriParser::FinaliseL() |
|
135 { |
|
136 #ifdef _DEBUG |
|
137 iOperationFactory.WriteToLog( _L8( "CXcapUriParser::FinaliseL()" ) ); |
|
138 #endif |
|
139 delete iCompleteUri; |
|
140 iCompleteUri = NULL; |
|
141 iCompleteUri = HBufC8::NewL( iUriBuffer->Size() ); |
|
142 TPtr8 pointer( iCompleteUri->Des() ); |
|
143 iUriBuffer->Read( 0, pointer, iUriBuffer->Size() ); |
|
144 iUriBuffer->Reset(); |
|
145 delete iUriBuffer; |
|
146 iUriBuffer = NULL; |
|
147 } |
|
148 |
|
149 // --------------------------------------------------------- |
|
150 // CXcapUriParser::ParseL |
|
151 // |
|
152 // --------------------------------------------------------- |
|
153 // |
|
154 void CXcapUriParser::ParseL( const TPtrC8& aHttpUri ) |
|
155 { |
|
156 #ifdef _DEBUG |
|
157 iOperationFactory.WriteToLog( _L8( "CXcapUriParser::ParseL()" ) ); |
|
158 #endif |
|
159 iHttpUri = CONST_CAST( TPtrC8*, &aHttpUri ); |
|
160 if( iNodeType != EXdmElementUnspecified ) |
|
161 { |
|
162 HBufC8* path = DocSubsetPathL(); |
|
163 CleanupStack::PushL( path ); |
|
164 iBufferPosition = CreateBasicNodeUriL( path ); |
|
165 CleanupStack::PopAndDestroy(); //path |
|
166 iNodeType == EXdmElementAttribute ? FormatAttributeUriL() : FormatNodeUriL(); |
|
167 if( iNamespaceMappings.Count() > 0 ) |
|
168 AppendNamespaceMappingsL(); |
|
169 FinaliseL(); |
|
170 #ifdef _DEBUG |
|
171 TPtr8 desc( iCompleteUri->Des() ); |
|
172 iOperationFactory.WriteToLog( _L8( " Context: %S" ), &desc ); |
|
173 #endif |
|
174 } |
|
175 else |
|
176 { |
|
177 #ifdef _DEBUG |
|
178 iOperationFactory.WriteToLog( _L8( " Context is a whole document" ) ); |
|
179 #endif |
|
180 iCompleteUri = HBufC8::NewL( iHttpUri->Length() ); |
|
181 iCompleteUri->Des().Copy( *iHttpUri ); |
|
182 } |
|
183 } |
|
184 |
|
185 // ---------------------------------------------------------- |
|
186 // CXcapDocumentNode::DocSubsetPathL |
|
187 // |
|
188 // ---------------------------------------------------------- |
|
189 // |
|
190 HBufC8* CXcapUriParser::DocSubsetPathL() |
|
191 { |
|
192 HBufC8* ret = NULL; |
|
193 CXdmDocumentNode* current = iDocumentSubset->Parent(); |
|
194 CBufFlat* buffer = CBufFlat::NewL( 256 ); |
|
195 CleanupStack::PushL( buffer ); |
|
196 while( current != NULL ) |
|
197 { |
|
198 buffer->InsertL( 0, FormatOnePathElementLC( current )->Des() ); |
|
199 CleanupStack::PopAndDestroy(); //FormatOnePathElementLC |
|
200 current = current->Parent(); |
|
201 } |
|
202 TInt length = buffer->Size(); |
|
203 ret = HBufC8::NewL( length ); |
|
204 TPtr8 pointer( ret->Des() ); |
|
205 buffer->Read( 0, pointer, length ); |
|
206 CleanupStack::PopAndDestroy(); //buffer |
|
207 return ret; |
|
208 } |
|
209 |
|
210 // --------------------------------------------------------- |
|
211 // CXcapUriParser::FormatAttributeUriL |
|
212 // |
|
213 // --------------------------------------------------------- |
|
214 // |
|
215 HBufC8* CXcapUriParser::FormatOnePathElementLC( const CXdmDocumentNode* aPathElement ) |
|
216 { |
|
217 #ifdef _DEBUG |
|
218 HBufC8* name8 = aPathElement->EightBitNodeNameLC(); |
|
219 TPtrC8 elemName( name8->Des() ); |
|
220 TPtrC8 elemPref( aPathElement->Prefix() ); |
|
221 iOperationFactory.WriteToLog( _L8( "CXcapUriParser::FormatOnePathElementL()" ) ); |
|
222 if( elemPref.Length() > 0 ) |
|
223 iOperationFactory.WriteToLog( _L8( " Prefix: %S" ), &elemName ); |
|
224 iOperationFactory.WriteToLog( _L8( " Name: %S" ), &elemName ); |
|
225 CleanupStack::PopAndDestroy(); //name8 |
|
226 #endif |
|
227 TInt position = 0; |
|
228 CBufFlat* buffer = CBufFlat::NewL( 50 ); |
|
229 CleanupStack::PushL( buffer ); |
|
230 buffer->InsertL( position, KXCAPPathSeparator ); |
|
231 position = position + KXCAPPathSeparator().Length(); |
|
232 TPtrC8 prefix = aPathElement->Prefix(); |
|
233 if( prefix.Length() > 0 ) |
|
234 { |
|
235 buffer->InsertL( position, prefix ); |
|
236 position = position + prefix.Length(); |
|
237 buffer->InsertL( position, KXcapUriColon ); |
|
238 position = position + KXcapUriColon().Length(); |
|
239 } |
|
240 TPtrC8 name = aPathElement->EightBitNodeNameLC()->Des(); |
|
241 buffer->InsertL( position, name ); |
|
242 position = position + name.Length(); |
|
243 CleanupStack::PopAndDestroy(); //EightBitNodeNameLC |
|
244 if( aPathElement->AttributeCount() > 0 && |
|
245 !( iNodeType == EXdmElementAttribute && aPathElement == iDocumentSubset->Parent() ) ) |
|
246 AppendPredicatesL( position, buffer, aPathElement ); |
|
247 HBufC8* retBuf = HBufC8::NewL( buffer->Size() ); |
|
248 TPtr8 pointer( retBuf->Des() ); |
|
249 buffer->Read( 0, pointer, buffer->Size() ); |
|
250 CleanupStack::PopAndDestroy(); //buffer |
|
251 CleanupStack::PushL( retBuf ); |
|
252 return retBuf; |
|
253 } |
|
254 |
|
255 // --------------------------------------------------------- |
|
256 // CXcapUriParser::FormatAttributeUriL |
|
257 // |
|
258 // --------------------------------------------------------- |
|
259 // |
|
260 void CXcapUriParser::FormatAttributeUriL() |
|
261 { |
|
262 #ifdef _DEBUG |
|
263 iOperationFactory.WriteToLog( _L8( " CXcapUriParser::FormatAttributeUriL()" ) ); |
|
264 #endif |
|
265 iUriBuffer->InsertL( iBufferPosition, KXCAPAttributeIdentifier ); |
|
266 iBufferPosition = iBufferPosition + KXCAPAttributeIdentifier().Length(); |
|
267 iUriBuffer->InsertL( iBufferPosition, iDocumentSubset->EightBitNodeNameLC()->Des() ); |
|
268 CleanupStack::PopAndDestroy(); //EightBitNodeNameLC |
|
269 HBufC8* newUri = HBufC8::NewLC( iUriBuffer->Size() ); |
|
270 TPtr8 pointer( newUri->Des() ); |
|
271 iUriBuffer->Read( 0, pointer, iUriBuffer->Size() ); |
|
272 #ifdef _DEBUG |
|
273 DumpUriL( newUri ); |
|
274 #endif |
|
275 CleanupStack::PopAndDestroy(); //newUri |
|
276 } |
|
277 |
|
278 // --------------------------------------------------------- |
|
279 // CXcapUriParser::FormatNodeUriL |
|
280 // |
|
281 // --------------------------------------------------------- |
|
282 // |
|
283 void CXcapUriParser::FormatNodeUriL() |
|
284 { |
|
285 #ifdef _DEBUG |
|
286 iOperationFactory.WriteToLog( _L8( " CXcapUriParser::FormatNodeUriL()" ) ); |
|
287 #endif |
|
288 iNodeType = EXdmElementNode; |
|
289 TPtrC8 prefix = iDocumentSubset->Prefix(); |
|
290 if( prefix.Length() > 0 ) |
|
291 { |
|
292 iUriBuffer->InsertL( iBufferPosition, prefix ); |
|
293 iBufferPosition = iBufferPosition + prefix.Length(); |
|
294 iUriBuffer->InsertL( iBufferPosition, KXcapUriColon ); |
|
295 iBufferPosition = iBufferPosition + KXcapUriColon().Length(); |
|
296 } |
|
297 TPtrC8 name = iDocumentSubset->EightBitNodeNameLC()->Des(); |
|
298 iUriBuffer->InsertL( iBufferPosition, name ); |
|
299 iBufferPosition = iBufferPosition + name.Length(); |
|
300 CleanupStack::PopAndDestroy(); //EightBitNodeNameLC |
|
301 if( iDocumentSubset->AttributeCount() > 0 ) |
|
302 AppendPredicatesL( iBufferPosition, iUriBuffer, iDocumentSubset ); |
|
303 //For now, do not put a trailing '/' to the node path. This may have to be changed, though. |
|
304 //iUriBuffer->InsertL( iBufferPosition, KXCAPPathSeparator ); |
|
305 //iBufferPosition = iBufferPosition + TPtrC8( KXCAPPathSeparator ).Length(); |
|
306 HBufC8* newUri = HBufC8::NewLC( iUriBuffer->Size() ); |
|
307 TPtr8 pointer2( newUri->Des() ); |
|
308 iUriBuffer->Read( 0, pointer2, iUriBuffer->Size() ); |
|
309 #ifdef _DEBUG |
|
310 DumpUriL( newUri ); |
|
311 #endif |
|
312 CleanupStack::PopAndDestroy(); //newUri |
|
313 } |
|
314 |
|
315 // --------------------------------------------------------- |
|
316 // CXcapUriParser::AppendPredicatesL |
|
317 // |
|
318 // --------------------------------------------------------- |
|
319 // |
|
320 void CXcapUriParser::AppendPredicatesL( TInt& aPosition, |
|
321 CBufBase* aBuffer, |
|
322 const CXdmDocumentNode* aPathElement ) |
|
323 { |
|
324 #ifdef _DEBUG |
|
325 iOperationFactory.WriteToLog( _L8( " CXcapUriParser::AppendPredicatesL()" ) ); |
|
326 #endif |
|
327 TInt count = aPathElement->AttributeCount(); |
|
328 for( TInt i = 0;i < count;i++ ) |
|
329 { |
|
330 CXdmNodeAttribute* attribute = aPathElement->Attribute( i ); |
|
331 //Namespaces cannot be used as attributes |
|
332 if( attribute->NodeName().Find( _L( "xmlns" ) ) < 0 ) |
|
333 { |
|
334 aBuffer->InsertL( aPosition, KHTTPBracketOpen ); |
|
335 aPosition = aPosition + KHTTPBracketOpen().Length(); |
|
336 aBuffer->InsertL( aPosition, KXCAPAttributeIdentifier ); |
|
337 aPosition = aPosition + KXCAPAttributeIdentifier().Length(); |
|
338 TPtrC8 name = attribute->EightBitNodeNameLC()->Des(); |
|
339 aBuffer->InsertL( aPosition, name ); |
|
340 aPosition = aPosition + name.Length(); |
|
341 CleanupStack::PopAndDestroy(); //EightBitNodeNameLC |
|
342 aBuffer->InsertL( aPosition, KXCAPEquality ); |
|
343 aPosition = aPosition + KXCAPEquality().Length(); |
|
344 aBuffer->InsertL( aPosition, KHTTPQuotationMark ); |
|
345 aPosition = aPosition + KHTTPQuotationMark().Length(); |
|
346 TPtrC8 value = attribute->EightBitValueLC()->Des(); |
|
347 aBuffer->InsertL( aPosition, value ); |
|
348 aPosition = aPosition + value.Length(); |
|
349 CleanupStack::PopAndDestroy(); //EightBitValueLC |
|
350 aBuffer->InsertL( aPosition, KHTTPQuotationMark ); |
|
351 aPosition = aPosition + KHTTPQuotationMark().Length(); |
|
352 aBuffer->InsertL( aPosition, KHTTPBracketClose ); |
|
353 aPosition = aPosition + KHTTPBracketClose().Length(); |
|
354 } |
|
355 } |
|
356 } |
|
357 |
|
358 // --------------------------------------------------------- |
|
359 // CXcapUriParser::CreateBasicNodeUriL |
|
360 // |
|
361 // --------------------------------------------------------- |
|
362 // |
|
363 TInt CXcapUriParser::CreateBasicNodeUriL( HBufC8* aNodePath ) |
|
364 { |
|
365 #ifdef _DEBUG |
|
366 iOperationFactory.WriteToLog( _L8( " CXcapUriParser::CreateBasicNodeUriL()" ) ); |
|
367 #endif |
|
368 TInt position = 0; |
|
369 TPtr8 pointer( aNodePath->Des() ); |
|
370 iUriBuffer = CBufFlat::NewL( 256 ); |
|
371 iUriBuffer->InsertL( position, *iHttpUri ); |
|
372 position = iHttpUri->Length(); |
|
373 if( iHttpUri->LocateReverseF( KXcapPathSeparatorChar ) != iHttpUri->Length() - 1 ) |
|
374 { |
|
375 iUriBuffer->InsertL( position, KXCAPPathSeparator ); |
|
376 position = position + KXCAPPathSeparator().Length(); |
|
377 } |
|
378 iUriBuffer->InsertL( position, KXCAPNodeSeparator ); //"~~" |
|
379 position = position + KXCAPNodeSeparator().Length(); |
|
380 iUriBuffer->InsertL( position, pointer ); |
|
381 position = position + pointer.Length(); |
|
382 iUriBuffer->InsertL( position, KXCAPPathSeparator ); |
|
383 position = position + KXCAPPathSeparator().Length(); |
|
384 return position; |
|
385 } |
|
386 |
|
387 |
|
388 |
|
389 // --------------------------------------------------------- |
|
390 // CXcapUriParser::SetDocumentSubset |
|
391 // |
|
392 // --------------------------------------------------------- |
|
393 // |
|
394 void CXcapUriParser::SetDocumentSubset( const CXdmDocumentNode* aDocumentSubset ) |
|
395 { |
|
396 #ifdef _DEBUG |
|
397 iOperationFactory.WriteToLog( _L8( "CXcapUriParser::SetDocumentSubset() - Type: %d" ), |
|
398 aDocumentSubset->ElementType() ); |
|
399 #endif |
|
400 __ASSERT_DEBUG( aDocumentSubset != NULL, User::Panic( _L( "CXcapUriParser" ), 1 ) ); |
|
401 iDocumentSubset = CONST_CAST( CXdmDocumentNode*, aDocumentSubset ); |
|
402 iNodeType = iDocumentSubset->ElementType(); |
|
403 } |
|
404 |
|
405 // --------------------------------------------------------- |
|
406 // CXcapUriParser::DesC8 |
|
407 // |
|
408 // --------------------------------------------------------- |
|
409 // |
|
410 TPtrC8 CXcapUriParser::DesC8() const |
|
411 { |
|
412 return iCompleteUri != NULL ? iCompleteUri->Des() : TPtrC8(); |
|
413 } |
|
414 |
|
415 // --------------------------------------------------------- |
|
416 // CXcapUriParser::NodeType |
|
417 // |
|
418 // --------------------------------------------------------- |
|
419 // |
|
420 TXdmElementType CXcapUriParser::NodeType() const |
|
421 { |
|
422 return iNodeType; |
|
423 } |
|
424 |
|
425 #ifdef _DEBUG |
|
426 // --------------------------------------------------------- |
|
427 // CXcapUriParser::DumpUriL |
|
428 // |
|
429 // --------------------------------------------------------- |
|
430 // |
|
431 void CXcapUriParser::DumpUriL( HBufC8* aNodePath ) |
|
432 { |
|
433 RFile file; |
|
434 RFs session; |
|
435 TPtr8 pointer( aNodePath->Des() ); |
|
436 _LIT( KFilePath, "C:\\logs\\XDM\\Dump.txt" ); |
|
437 User::LeaveIfError( session.Connect() ); |
|
438 TInt error = file.Replace( session, KFilePath, EFileWrite ); |
|
439 if( error == KErrNone ) |
|
440 { |
|
441 file.Write( pointer ); |
|
442 file.Close(); |
|
443 } |
|
444 session.Close(); |
|
445 } |
|
446 #endif |
|
447 // End of File |
|
448 |