|
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: CXmlFormatter |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 // INCLUDES |
|
22 #include <XdmDocument.h> |
|
23 #include <XdmDocumentNode.h> |
|
24 #include <XdmNodeAttribute.h> |
|
25 |
|
26 #include "xdmlogwriter.h" |
|
27 #include "XmlFormatter.h" |
|
28 #include "XmlParserDefines.h" |
|
29 #include "XmlParserNodePath.h" |
|
30 #include "XdmXmlParser.h" |
|
31 |
|
32 // ---------------------------------------------------------- |
|
33 // CXmlFormatter::CXmlFormatter |
|
34 // |
|
35 // ---------------------------------------------------------- |
|
36 // |
|
37 CXmlFormatter::CXmlFormatter( CXdmXmlParser& aParserMain ) : |
|
38 iTargetFound( EFalse ), |
|
39 iIsAppended( EFalse ), |
|
40 iParserMain( aParserMain ) |
|
41 { |
|
42 } |
|
43 |
|
44 // ---------------------------------------------------------- |
|
45 // CXmlFormatter::NewL |
|
46 // |
|
47 // ---------------------------------------------------------- |
|
48 // |
|
49 CXmlFormatter* CXmlFormatter::NewL( CXdmXmlParser& aParserMain ) |
|
50 { |
|
51 CXmlFormatter* self = new ( ELeave ) CXmlFormatter( aParserMain ); |
|
52 CleanupStack::PushL( self ); |
|
53 self->ConstructL(); |
|
54 CleanupStack::Pop(); |
|
55 return self; |
|
56 } |
|
57 |
|
58 // ---------------------------------------------------------- |
|
59 // CXmlFormatter::~CXmlFormatter |
|
60 // |
|
61 // ---------------------------------------------------------- |
|
62 // |
|
63 CXmlFormatter::~CXmlFormatter() |
|
64 { |
|
65 } |
|
66 |
|
67 // ---------------------------------------------------------- |
|
68 // CXmlFormatter::ConstructL |
|
69 // |
|
70 // ---------------------------------------------------------- |
|
71 // |
|
72 void CXmlFormatter::ConstructL() |
|
73 { |
|
74 } |
|
75 |
|
76 // --------------------------------------------------------- |
|
77 // CXmlFormatter::Prefix |
|
78 // |
|
79 // --------------------------------------------------------- |
|
80 // |
|
81 TPtrC8 CXmlFormatter::Prefix( const MXdmNodeInterface& aInterface ) |
|
82 { |
|
83 return aInterface.Prefix(); |
|
84 } |
|
85 |
|
86 // --------------------------------------------------------- |
|
87 // CXdmXmlContentHandler::Reset |
|
88 // |
|
89 // --------------------------------------------------------- |
|
90 // |
|
91 CXdmNodeAttribute* CXmlFormatter::TargetAttribute() const |
|
92 { |
|
93 return iTargetAttribute; |
|
94 } |
|
95 |
|
96 // --------------------------------------------------------- |
|
97 // CXmlFormatter::FindElementName |
|
98 // |
|
99 // --------------------------------------------------------- |
|
100 // |
|
101 TPtrC8 CXmlFormatter::FindElementName( const TDesC8& aElementString ) |
|
102 { |
|
103 TInt index = aElementString.LocateF( 32 ); |
|
104 if( index > 0 ) |
|
105 return aElementString.Left( index ); |
|
106 else return aElementString; |
|
107 } |
|
108 |
|
109 // --------------------------------------------------------- |
|
110 // CXmlFormatter::ResetFormatBuffer |
|
111 // |
|
112 // --------------------------------------------------------- |
|
113 // |
|
114 void CXmlFormatter::ResetFormatBuffer() |
|
115 { |
|
116 iFormBufferPos = 0; |
|
117 delete iFormatBuffer; |
|
118 iFormatBuffer = NULL; |
|
119 delete iNodePath; |
|
120 iNodePath = NULL; |
|
121 } |
|
122 |
|
123 |
|
124 // --------------------------------------------------------- |
|
125 // CXmlFormatter::BufferToStringLC |
|
126 // |
|
127 // --------------------------------------------------------- |
|
128 // |
|
129 HBufC8* CXmlFormatter::BufferToStringLC() |
|
130 { |
|
131 HBufC8* ret = HBufC8::NewLC( iFormatBuffer->Size() ); |
|
132 TPtr8 pointer( ret->Des() ); |
|
133 iFormatBuffer->Read( 0, pointer, iFormatBuffer->Size() ); |
|
134 ResetFormatBuffer(); |
|
135 return ret; |
|
136 } |
|
137 |
|
138 // --------------------------------------------------------- |
|
139 // CXmlFormatter::CheckSubsetPath |
|
140 // |
|
141 // --------------------------------------------------------- |
|
142 // |
|
143 void CXmlFormatter::CheckSubsetPath( const CXdmDocumentNode& aNode ) |
|
144 { |
|
145 iTargetFound = iNodePath->CheckNextNode( aNode ); |
|
146 } |
|
147 |
|
148 // ---------------------------------------------------------- |
|
149 // CXmlFormatter::FormatLC |
|
150 // |
|
151 // ---------------------------------------------------------- |
|
152 // |
|
153 HBufC8* CXmlFormatter::FormatLC( const TDesC8& aXmlFragment, |
|
154 const CXdmDocument* aDocument, |
|
155 const CXdmDocumentNode* aTargetNode ) |
|
156 { |
|
157 iIsAppended = EFalse; |
|
158 iTargetFound = EFalse; |
|
159 TBool addToRoot = EFalse; |
|
160 iFormatBuffer = CBufSeg::NewL( 128 ); |
|
161 iElementType = aTargetNode->ElementType(); |
|
162 iDocumentFragment.Set( aXmlFragment ); |
|
163 iNodePath = CXmlParserNodePath::NewL( iParserMain, iElementType, *aTargetNode, this ); |
|
164 CXdmDocumentNode* element = aDocument->DocumentRoot(); |
|
165 CXdmDocumentNode* root = element; |
|
166 CheckSubsetPath( *root ); |
|
167 if( iTargetFound ) |
|
168 { |
|
169 addToRoot = ETrue; |
|
170 iIsAppended = ETrue; |
|
171 } |
|
172 iFormatBuffer->InsertL( iFormBufferPos, KXmldocumentStart ); |
|
173 iFormBufferPos = iFormBufferPos + KXmldocumentStart().Length(); |
|
174 FormatNamespaceDeclarationsL( *root, *aDocument ); |
|
175 TInt count = element->NodeCount(); |
|
176 if( count > 0 ) |
|
177 { |
|
178 for( TInt i = 0;i < count;i++ ) |
|
179 { |
|
180 element = element->ChileNode( i ); |
|
181 FormatElementsL( element ); |
|
182 } |
|
183 } |
|
184 else FormatElementsL( element ); |
|
185 if( addToRoot ) |
|
186 AppendXmlFragmentL(); |
|
187 TPtrC8 name( root->EightBitNodeNameLC()->Des() ); |
|
188 TPtrC8 prefix = root->Prefix(); |
|
189 FormatElementEndL( prefix, name ); |
|
190 CleanupStack::PopAndDestroy(); //EightBitNodeNameLC() |
|
191 HBufC8* ret = BufferToStringLC(); |
|
192 return ret; |
|
193 } |
|
194 |
|
195 // --------------------------------------------------------- |
|
196 // CXmlFormatter::FormatElementsL |
|
197 // |
|
198 // --------------------------------------------------------- |
|
199 // |
|
200 void CXmlFormatter::FormatElementsL( CXdmDocumentNode* aCurrentNode ) |
|
201 { |
|
202 TBool ready = EFalse; |
|
203 TBool addNow = EFalse; |
|
204 if( !iTargetFound && !iIsAppended ) |
|
205 { |
|
206 CheckSubsetPath( *aCurrentNode ); |
|
207 addNow = iTargetFound && !iIsAppended; |
|
208 } |
|
209 TPtrC8 prefix = Prefix( *aCurrentNode ); |
|
210 TPtr8 pointer( aCurrentNode->ElementDataLC()->Des() ); |
|
211 FormatElementStartL( prefix, pointer ); |
|
212 while( !ready ) |
|
213 { |
|
214 if( aCurrentNode->IsLeafNode() ) |
|
215 FormatLeafElementL( aCurrentNode ); |
|
216 TInt nodeCount = aCurrentNode->NodeCount(); |
|
217 if( nodeCount > 0 ) |
|
218 { |
|
219 CXdmDocumentNode* child = NULL; |
|
220 for( TInt i = 0;i < nodeCount;i++ ) |
|
221 { |
|
222 child = aCurrentNode->ChileNode( i ); |
|
223 child->IsEmptyNode() && !child->IsLeafNode() ? |
|
224 FormatEmptyElementL( child ): |
|
225 FormatElementsL( child ); |
|
226 } |
|
227 ready = ETrue; |
|
228 } |
|
229 else ready = ETrue; |
|
230 } |
|
231 if( addNow ) |
|
232 AppendXmlFragmentL(); |
|
233 FormatElementEndL( prefix, FindElementName( pointer ) ); |
|
234 CleanupStack::PopAndDestroy(); //ElementDataLC() |
|
235 } |
|
236 |
|
237 // --------------------------------------------------------- |
|
238 // CXmlFormatter::AppendXmlFragmentL |
|
239 // |
|
240 // --------------------------------------------------------- |
|
241 // |
|
242 void CXmlFormatter::AppendXmlFragmentL() |
|
243 { |
|
244 iFormatBuffer->InsertL( iFormBufferPos, iDocumentFragment ); |
|
245 iFormBufferPos = iFormBufferPos + iDocumentFragment.Length(); |
|
246 iIsAppended = ETrue; |
|
247 } |
|
248 |
|
249 // ---------------------------------------------------------- |
|
250 // CXmlFormatter::FormatLC |
|
251 // |
|
252 // ---------------------------------------------------------- |
|
253 // |
|
254 HBufC8* CXmlFormatter::FormatLC( TBool aIsWholeDocument, |
|
255 const CXdmDocument* aDocument, |
|
256 const CXdmDocumentNode* aRootNode ) |
|
257 { |
|
258 iIsAppended = ETrue; |
|
259 iFormatBuffer = CBufSeg::NewL( 128 ); |
|
260 CXdmDocumentNode* root = CONST_CAST( CXdmDocumentNode*, aRootNode ); |
|
261 if( aIsWholeDocument ) |
|
262 { |
|
263 CXdmDocumentNode* element = NULL; |
|
264 iFormatBuffer->InsertL( iFormBufferPos, KXmldocumentStart ); |
|
265 iFormBufferPos = iFormBufferPos + KXmldocumentStart().Length(); |
|
266 FormatNamespaceDeclarationsL( *root, *aDocument ); |
|
267 TInt count = root->NodeCount(); |
|
268 if( count > 0 ) |
|
269 { |
|
270 for( TInt i = 0;i < count;i++ ) |
|
271 { |
|
272 element = root->ChileNode( i ); |
|
273 FormatElementsL( element ); |
|
274 } |
|
275 } |
|
276 } |
|
277 else FormatElementsL( root ); |
|
278 if( aIsWholeDocument ) |
|
279 { |
|
280 TPtrC8 name( aRootNode->EightBitNodeNameLC()->Des() ); |
|
281 TPtrC8 prefix = aRootNode->Prefix(); |
|
282 FormatElementEndL( prefix, name ); |
|
283 CleanupStack::PopAndDestroy(); //EightBitNodeNameLC() |
|
284 } |
|
285 HBufC8* ret = BufferToStringLC(); |
|
286 return ret; |
|
287 } |
|
288 |
|
289 // ---------------------------------------------------------- |
|
290 // CXmlFormatter::FormatNamespaceDeclarationsL |
|
291 // |
|
292 // ---------------------------------------------------------- |
|
293 // |
|
294 void CXmlFormatter::FormatNamespaceDeclarationsL( const CXdmDocumentNode& aRootNode, |
|
295 const MXdmNamespaceContainer& aContainer ) |
|
296 { |
|
297 #ifdef _DEBUG |
|
298 iParserMain.WriteToLog( _L8( "CXmlFormatter::FormatNamespaceDeclarationsL start" ) ); |
|
299 #endif |
|
300 TPtrC8 name( aRootNode.EightBitNodeNameLC()->Des() ); |
|
301 TPtrC8 prefix = Prefix( aRootNode ); |
|
302 iFormatBuffer->InsertL( iFormBufferPos, KStartBracketString ); |
|
303 iFormBufferPos = iFormBufferPos + KStartBracketString().Length(); |
|
304 if( prefix.Length() > 0 ) |
|
305 { |
|
306 iFormatBuffer->InsertL( iFormBufferPos, prefix ); |
|
307 iFormBufferPos = iFormBufferPos + prefix.Length(); |
|
308 iFormatBuffer->InsertL( iFormBufferPos, KPrefixSeparatorString ); |
|
309 iFormBufferPos = iFormBufferPos + KPrefixSeparatorString().Length(); |
|
310 } |
|
311 iFormatBuffer->InsertL( iFormBufferPos, name ); |
|
312 iFormBufferPos = iFormBufferPos + name.Length(); |
|
313 CleanupStack::PopAndDestroy(); //EightBitNodeNameLC() |
|
314 if( aContainer.Count() > 0 ) |
|
315 { |
|
316 TPtrC8 uri( _L8( "" ) ); |
|
317 TPtrC8 prefix( _L8( "" ) ); |
|
318 for( TInt i = 0;i < aContainer.Count();i++ ) |
|
319 { |
|
320 uri.Set( aContainer.Uri( i ) ); |
|
321 prefix.Set( aContainer.Prefix( i ) ); |
|
322 AppendNamespaceL( uri, prefix ); |
|
323 } |
|
324 } |
|
325 |
|
326 // go through possible attributes after namespaces |
|
327 for ( TInt i = 0; i < aRootNode.AttributeCount(); i++ ) |
|
328 { |
|
329 iFormatBuffer->InsertL( iFormBufferPos, KSpaceString ); |
|
330 iFormBufferPos = iFormBufferPos + KSpaceString().Length(); |
|
331 |
|
332 CXdmNodeAttribute* attr = aRootNode.Attribute( i ); |
|
333 TPtrC8 name( attr->EightBitNodeNameLC()->Des() ); |
|
334 iFormatBuffer->InsertL( iFormBufferPos, name ); |
|
335 iFormBufferPos = iFormBufferPos + name.Length(); |
|
336 CleanupStack::PopAndDestroy(); //EightBitNodeNameLC() |
|
337 iFormatBuffer->InsertL( iFormBufferPos, KEquelsSignString ); |
|
338 iFormBufferPos = iFormBufferPos + KEquelsSignString().Length(); |
|
339 iFormatBuffer->InsertL( iFormBufferPos, KQuotationSignString ); |
|
340 iFormBufferPos = iFormBufferPos + KQuotationSignString().Length(); |
|
341 TPtrC8 attrValue( attr->EightBitValueLC()->Des() ); |
|
342 iFormatBuffer->InsertL( iFormBufferPos, attrValue ); |
|
343 iFormBufferPos = iFormBufferPos + attrValue.Length(); |
|
344 CleanupStack::PopAndDestroy(); //EightBitValueLC() |
|
345 iFormatBuffer->InsertL( iFormBufferPos, KQuotationSignString ); |
|
346 iFormBufferPos = iFormBufferPos + KQuotationSignString().Length(); |
|
347 } |
|
348 |
|
349 iFormatBuffer->InsertL( iFormBufferPos, KEndBracketString ); |
|
350 iFormBufferPos = iFormBufferPos + KEndBracketString().Length(); |
|
351 iFormatBuffer->InsertL( iFormBufferPos, KNewlineString ); |
|
352 iFormBufferPos = iFormBufferPos + KNewlineString().Length(); |
|
353 #ifdef _DEBUG |
|
354 iParserMain.WriteToLog( _L8( "CXmlFormatter::FormatNamespaceDeclarationsL exit" ) ); |
|
355 #endif |
|
356 } |
|
357 |
|
358 // --------------------------------------------------------- |
|
359 // CXmlFormatter::FormatLeafElementL |
|
360 // |
|
361 // --------------------------------------------------------- |
|
362 // |
|
363 void CXmlFormatter::FormatLeafElementL( CXdmDocumentNode* aCurrentNode ) |
|
364 { |
|
365 TPtrC8 escape( aCurrentNode->EscapeLeafNodeContentLC()->Des() ); |
|
366 iFormatBuffer->InsertL( iFormBufferPos, escape ); |
|
367 iFormBufferPos = iFormBufferPos + escape.Length(); |
|
368 CleanupStack::PopAndDestroy(); //EscapeLeafNodeContentLC() |
|
369 } |
|
370 |
|
371 // --------------------------------------------------------- |
|
372 // CXmlFormatter::FormatEmptyElementL |
|
373 // |
|
374 // --------------------------------------------------------- |
|
375 // |
|
376 void CXmlFormatter::FormatEmptyElementL( CXdmDocumentNode* aEmptyNode ) |
|
377 { |
|
378 TPtr8 empty( aEmptyNode->ElementDataLC()->Des() ); |
|
379 iFormatBuffer->InsertL( iFormBufferPos, KStartBracketString ); |
|
380 iFormBufferPos = iFormBufferPos + KStartBracketString().Length(); |
|
381 TPtrC8 prefix = Prefix( *aEmptyNode ); |
|
382 if( prefix.Length() > 0 ) |
|
383 { |
|
384 iFormatBuffer->InsertL( iFormBufferPos, prefix ); |
|
385 iFormBufferPos = iFormBufferPos + prefix.Length(); |
|
386 iFormatBuffer->InsertL( iFormBufferPos, KPrefixSeparatorString ); |
|
387 iFormBufferPos = iFormBufferPos + KPrefixSeparatorString().Length(); |
|
388 } |
|
389 iFormatBuffer->InsertL( iFormBufferPos, empty ); |
|
390 iFormBufferPos = iFormBufferPos + empty.Length(); |
|
391 CleanupStack::PopAndDestroy(); //ElementDataLC() |
|
392 iFormatBuffer->InsertL( iFormBufferPos, KEmptyElementString ); |
|
393 iFormBufferPos = iFormBufferPos + TPtrC8( KEmptyElementString ).Length(); |
|
394 } |
|
395 |
|
396 // --------------------------------------------------------- |
|
397 // CXmlFormatter::FormatElementStartL |
|
398 // |
|
399 // --------------------------------------------------------- |
|
400 // |
|
401 void CXmlFormatter::FormatElementStartL( const TDesC8& aPrefix, |
|
402 const TDesC8& aElementData ) |
|
403 { |
|
404 iFormatBuffer->InsertL( iFormBufferPos, KStartBracketString ); |
|
405 iFormBufferPos = iFormBufferPos + KStartBracketString().Length(); |
|
406 if( aPrefix.Length() > 0 ) |
|
407 { |
|
408 iFormatBuffer->InsertL( iFormBufferPos, aPrefix ); |
|
409 iFormBufferPos = iFormBufferPos + aPrefix.Length(); |
|
410 iFormatBuffer->InsertL( iFormBufferPos, KPrefixSeparatorString ); |
|
411 iFormBufferPos = iFormBufferPos + KPrefixSeparatorString().Length(); |
|
412 } |
|
413 iFormatBuffer->InsertL( iFormBufferPos, aElementData ); |
|
414 iFormBufferPos = iFormBufferPos + aElementData.Length(); |
|
415 iFormatBuffer->InsertL( iFormBufferPos, KEndBracketString ); |
|
416 iFormBufferPos = iFormBufferPos + KEndBracketString().Length(); |
|
417 } |
|
418 |
|
419 // --------------------------------------------------------- |
|
420 // CXmlFormatter::FormatElementEndL |
|
421 // |
|
422 // --------------------------------------------------------- |
|
423 // |
|
424 void CXmlFormatter::FormatElementEndL( const TDesC8& aPrefix, |
|
425 const TDesC8& aElementName ) |
|
426 { |
|
427 iFormatBuffer->InsertL( iFormBufferPos, KStartBracketString ); |
|
428 iFormBufferPos = iFormBufferPos + KStartBracketString().Length(); |
|
429 iFormatBuffer->InsertL( iFormBufferPos, KSlashString ); |
|
430 iFormBufferPos = iFormBufferPos + KSlashString().Length(); |
|
431 if( aPrefix.Length() > 0 ) |
|
432 { |
|
433 iFormatBuffer->InsertL( iFormBufferPos, aPrefix ); |
|
434 iFormBufferPos = iFormBufferPos + aPrefix.Length(); |
|
435 iFormatBuffer->InsertL( iFormBufferPos, KPrefixSeparatorString ); |
|
436 iFormBufferPos = iFormBufferPos + KPrefixSeparatorString().Length(); |
|
437 } |
|
438 iFormatBuffer->InsertL( iFormBufferPos, aElementName ); |
|
439 iFormBufferPos = iFormBufferPos + aElementName.Length(); |
|
440 iFormatBuffer->InsertL( iFormBufferPos, KEndBracketString ); |
|
441 iFormBufferPos = iFormBufferPos + KEndBracketString().Length(); |
|
442 } |
|
443 |
|
444 // --------------------------------------------------------- |
|
445 // CXmlFormatter::AppendNamespaceL |
|
446 // |
|
447 // --------------------------------------------------------- |
|
448 // |
|
449 void CXmlFormatter::AppendNamespaceL( const TDesC8& aUri, const TDesC8& aPrefix ) |
|
450 { |
|
451 iFormatBuffer->InsertL( iFormBufferPos, KXmlNamespaceString ); |
|
452 iFormBufferPos = iFormBufferPos + KXmlNamespaceString().Length(); |
|
453 if( aPrefix.Length() > 0 ) //Not the default namespace |
|
454 { |
|
455 iFormatBuffer->InsertL( iFormBufferPos, KPrefixSeparatorString ); |
|
456 iFormBufferPos = iFormBufferPos + KPrefixSeparatorString().Length(); |
|
457 iFormatBuffer->InsertL( iFormBufferPos, aPrefix ); |
|
458 iFormBufferPos = iFormBufferPos + aPrefix.Length(); |
|
459 } |
|
460 iFormatBuffer->InsertL( iFormBufferPos, KEquelsSignString ); |
|
461 iFormBufferPos = iFormBufferPos + KEquelsSignString().Length(); |
|
462 iFormatBuffer->InsertL( iFormBufferPos, KQuotationSignString ); |
|
463 iFormBufferPos = iFormBufferPos + KQuotationSignString().Length(); |
|
464 iFormatBuffer->InsertL( iFormBufferPos, aUri ); |
|
465 iFormBufferPos = iFormBufferPos + aUri.Length(); |
|
466 iFormatBuffer->InsertL( iFormBufferPos, KQuotationSignString ); |
|
467 iFormBufferPos = iFormBufferPos + KQuotationSignString().Length(); |
|
468 } |
|
469 |
|
470 |
|
471 // End of File |