|
1 /* |
|
2 * Copyright (c) 2004 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: Sources |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include <s32buf.h> |
|
22 #include <s32file.h> |
|
23 |
|
24 #include <nsmlfolderparser.h> |
|
25 #include "nsmlparserconstants.h" |
|
26 |
|
27 |
|
28 // ============================ MEMBER FUNCTIONS =============================== |
|
29 |
|
30 // ----------------------------------------------------------------------------- |
|
31 // CNSmlFolderParser::NewL |
|
32 // Two-phased constructor. |
|
33 // ----------------------------------------------------------------------------- |
|
34 // |
|
35 EXPORT_C CNSmlFolderParser* CNSmlFolderParser::NewL() |
|
36 { |
|
37 CNSmlFolderParser* self = CNSmlFolderParser::NewLC(); |
|
38 CleanupStack::Pop(); |
|
39 |
|
40 return self; |
|
41 } |
|
42 |
|
43 // ----------------------------------------------------------------------------- |
|
44 // CNSmlFolderParser::NewLC |
|
45 // Two-phased constructor. |
|
46 // ----------------------------------------------------------------------------- |
|
47 // |
|
48 EXPORT_C CNSmlFolderParser* CNSmlFolderParser::NewLC() |
|
49 { |
|
50 CNSmlFolderParser* self = new (ELeave) CNSmlFolderParser(); |
|
51 CleanupStack::PushL(self); |
|
52 self->ConstructL(); |
|
53 |
|
54 return self; |
|
55 } |
|
56 |
|
57 // ----------------------------------------------------------------------------- |
|
58 // CNSmlFolderParser::~CNSmlFolderParser |
|
59 // Destructor |
|
60 // ----------------------------------------------------------------------------- |
|
61 // |
|
62 EXPORT_C CNSmlFolderParser::~CNSmlFolderParser() |
|
63 { |
|
64 if(iExt) |
|
65 { |
|
66 iExt->ResetAndDestroy(); |
|
67 delete iExt; |
|
68 } |
|
69 |
|
70 if(iName) delete iName; |
|
71 if(iRole) delete iRole; |
|
72 |
|
73 if( iExtData ) delete iExtData; |
|
74 } |
|
75 |
|
76 // ----------------------------------------------------------------------------- |
|
77 // CNSmlFolderParser::ParseXml |
|
78 // Parses through the given XML and places the data to member variables. |
|
79 // Note: The method does not check whether the member variables |
|
80 // already contains data or not, but just writes it (if some of them are already |
|
81 // instantiated dynamically, they should be deleted before this method is called). |
|
82 // ----------------------------------------------------------------------------- |
|
83 // |
|
84 EXPORT_C TNSmlParserGeneratorError CNSmlFolderParser::ParseXml( HBufC8* aXml ) |
|
85 { |
|
86 |
|
87 TRAPD(cdataErr, PreProcessL(aXml)); |
|
88 if (cdataErr != EErrorNone) |
|
89 return (TNSmlParserGeneratorError)cdataErr; |
|
90 |
|
91 iSetValues.Reset(); |
|
92 iCurrentState = EFolderNone; |
|
93 iLastState = EFolderNone; |
|
94 TPtrC8 ptr(*aXml); |
|
95 |
|
96 TRAPD(err, ParseL( ptr )); |
|
97 |
|
98 if( err == EErrorNone && iCurrentState != EFolderNone ) |
|
99 return EInvalidXmlError; |
|
100 |
|
101 return CheckError(err); |
|
102 } |
|
103 |
|
104 // ----------------------------------------------------------------------------- |
|
105 // CNSmlFolderParser::GenerateXml |
|
106 // Generates Folder-XML using the values in member variables. aXml contains the |
|
107 // generated xml when the function returns successfully. Note: caller should not |
|
108 // instantiate the HBufC8, since this method first counts the size of the |
|
109 // xml to be generated and then instatiates the HBufC8 with a correct maximum size. |
|
110 // If the given buffer is not NULL, it is destroyed and a new one is instantiated. |
|
111 // Caller gets the control of the HBufC8 when the function returns. |
|
112 // ----------------------------------------------------------------------------- |
|
113 // |
|
114 EXPORT_C TNSmlParserGeneratorError CNSmlFolderParser::GenerateXml( HBufC8*& aXml ) |
|
115 { |
|
116 |
|
117 TRAPD( err, ConvertIntoEntitiesL(); // convert special characters into entities |
|
118 |
|
119 GenerateFolderXmlL( aXml ) ); |
|
120 |
|
121 return CheckError(err); |
|
122 } |
|
123 |
|
124 // ----------------------------------------------------------------------------- |
|
125 // CNSmlFolderParser::GenerateFolderXmlL |
|
126 // Private methods that does the actual folder xml generation |
|
127 // ----------------------------------------------------------------------------- |
|
128 // |
|
129 void CNSmlFolderParser::GenerateFolderXmlL( HBufC8*& aXml ) |
|
130 { |
|
131 // count the size of the xml |
|
132 TInt size = CountXmlSizeL(); |
|
133 |
|
134 // create buffer for the xml |
|
135 |
|
136 if ( aXml ) |
|
137 { |
|
138 delete aXml; |
|
139 aXml = NULL; |
|
140 } |
|
141 |
|
142 aXml = HBufC8::NewLC(size); |
|
143 TPtr8 ptr = aXml->Des(); |
|
144 |
|
145 AppendElement(ptr, KFolderElement()); |
|
146 |
|
147 if ( iName ) |
|
148 { |
|
149 AppendElement(ptr, KFolderNameElement(), *iName); |
|
150 } |
|
151 |
|
152 if (iCreated != Time::NullTTime()) |
|
153 { |
|
154 AppendElement(ptr, KFolderCreatedElement(), DateTimeToStringL(iCreated)); |
|
155 } |
|
156 |
|
157 if (iModified != Time::NullTTime()) |
|
158 { |
|
159 AppendElement(ptr, KFolderModifiedElement(), DateTimeToStringL(iModified)); |
|
160 } |
|
161 |
|
162 if (iAccessed != Time::NullTTime()) |
|
163 { |
|
164 AppendElement(ptr, KFolderAccessedElement(), DateTimeToStringL(iAccessed)); |
|
165 } |
|
166 |
|
167 if (iAttributes.AttributeCount() > 0) |
|
168 { |
|
169 iAttributes.GenerateXml(ptr, this); |
|
170 } |
|
171 |
|
172 if ( iRole ) |
|
173 { |
|
174 AppendElement(ptr, KFolderRoleElement(), *iRole); |
|
175 } |
|
176 |
|
177 if ( iExt ) |
|
178 { |
|
179 for (TInt i=0; i < iExt->Count(); ++i) |
|
180 { |
|
181 iExt->At(i)->GenerateXmlL(ptr, this); |
|
182 } |
|
183 |
|
184 } |
|
185 |
|
186 AppendEndElement(ptr, KFolderElement()); |
|
187 |
|
188 CleanupStack::Pop(); // aXml |
|
189 } |
|
190 |
|
191 // ----------------------------------------------------------------------------- |
|
192 // CNSmlFolderParser::CountXmlSizeL |
|
193 // Counts how long the xml string will be if it was generated using |
|
194 // the values currently in member variables. |
|
195 // ----------------------------------------------------------------------------- |
|
196 // |
|
197 EXPORT_C TInt CNSmlFolderParser::CountXmlSizeL() |
|
198 { |
|
199 TInt size = 0; |
|
200 |
|
201 size += SizeOfElements( KFolderElement() ); |
|
202 size += KCDataStart().Length() + KCDataEnd().Length(); |
|
203 |
|
204 if ( iName ) |
|
205 { |
|
206 size += SizeOfString( iName, KFolderNameElement() ); |
|
207 } |
|
208 |
|
209 if (iCreated != Time::NullTTime()) |
|
210 { |
|
211 size += SizeOfDatetime( KFolderCreatedElement() ); |
|
212 } |
|
213 |
|
214 if (iModified != Time::NullTTime()) |
|
215 { |
|
216 size += SizeOfDatetime( KFolderModifiedElement() ); |
|
217 } |
|
218 |
|
219 if (iAccessed != Time::NullTTime()) |
|
220 { |
|
221 size += SizeOfDatetime( KFolderAccessedElement() ); |
|
222 } |
|
223 |
|
224 // attributes, if any present |
|
225 if (iAttributes.AttributeCount() > 0) |
|
226 { |
|
227 size += iAttributes.CountXmlSize( this ); |
|
228 } |
|
229 |
|
230 if ( iRole ) |
|
231 { |
|
232 size += SizeOfString( iRole, KFolderRoleElement() ); |
|
233 } |
|
234 |
|
235 if ( iExt ) |
|
236 { |
|
237 for (TInt i=0; i < iExt->Count(); ++i) |
|
238 { |
|
239 size += iExt->At(i)->CountXmlSize( this ); |
|
240 } |
|
241 } |
|
242 |
|
243 return size; |
|
244 } |
|
245 |
|
246 // ----------------------------------------------------------------------------- |
|
247 // CNSmlFolderParser::AddExtL |
|
248 // Adds the given extension to iExt |
|
249 // ----------------------------------------------------------------------------- |
|
250 // |
|
251 EXPORT_C void CNSmlFolderParser::AddExtL( CNSmlExtData* aExt ) |
|
252 { |
|
253 iExt->AppendL(aExt); |
|
254 } |
|
255 |
|
256 // ----------------------------------------------------------------------------- |
|
257 // CNSmlFolderParser::NextElementL |
|
258 // Base class' ParseL method calls this method, when a new element is found. aElement |
|
259 // is the name of that element (string inside '<' and '>' characters). |
|
260 // ----------------------------------------------------------------------------- |
|
261 // |
|
262 void CNSmlFolderParser::NextElementL( TPtrC8 aElement ) |
|
263 { |
|
264 if (aElement.Length() == 0) |
|
265 User::Leave(EInvalidXmlError); |
|
266 |
|
267 TBool startElement = ETrue; |
|
268 |
|
269 if(aElement[0] == KCharacterSlash) |
|
270 { |
|
271 // end element |
|
272 // remove the '/' character from the beginning |
|
273 aElement.Set( aElement.Right( aElement.Length() - 1 ) ); |
|
274 startElement = EFalse; |
|
275 } |
|
276 #ifndef __NO_EMPTY_ELEMENTS_ |
|
277 else if (aElement[aElement.Length()-1] == KCharacterSlash) |
|
278 { |
|
279 // empty element |
|
280 // remove the '/' character from the end |
|
281 aElement.Set( aElement.Left( aElement.Length() - 1 ) ); |
|
282 |
|
283 // empty element == start element, empty data, end element |
|
284 NextElementL( aElement ); // simulates start element |
|
285 NextDataL( _L8("") ); // simulates the empty data |
|
286 |
|
287 HBufC8* end = HBufC8::NewLC(aElement.Length()+1); |
|
288 TPtr8 ptr = end->Des(); |
|
289 ptr.Append( _L8("/") ); |
|
290 ptr.Append( aElement ); |
|
291 |
|
292 NextElementL( ptr ); // simulates end element |
|
293 |
|
294 CleanupStack::PopAndDestroy(); // end |
|
295 |
|
296 return; |
|
297 } |
|
298 #endif |
|
299 |
|
300 // variables used for state, init to <Folder> and not set |
|
301 TNSmlCurrentFolderElement inner = EFolder; |
|
302 TNSmlCurrentFolderElement outer = EFolder; |
|
303 TBool isSet = EFalse; |
|
304 |
|
305 if( aElement == KFolderElement ) |
|
306 { |
|
307 outer = EFolderNone; |
|
308 isSet = iSetValues.iFolder; |
|
309 if ( startElement ) iSetValues.iFolder = ETrue; |
|
310 } |
|
311 else if( aElement == KFolderNameElement ) |
|
312 { |
|
313 inner = EFolderName; |
|
314 isSet = iSetValues.iName; |
|
315 } |
|
316 else if( aElement == KFolderCreatedElement ) |
|
317 { |
|
318 inner = EFolderCreated; |
|
319 isSet = iSetValues.iCreated; |
|
320 } |
|
321 else if( aElement == KFolderModifiedElement ) |
|
322 { |
|
323 inner = EFolderModified; |
|
324 isSet = iSetValues.iModified; |
|
325 } |
|
326 else if( aElement == KFolderAccessedElement ) |
|
327 { |
|
328 inner = EFolderAccessed; |
|
329 isSet = iSetValues.iAccessed; |
|
330 } |
|
331 else if( aElement == KFolderAttributesElement ) |
|
332 { |
|
333 inner = EAttributes; |
|
334 isSet = iSetValues.iAttributes; |
|
335 if ( startElement) iSetValues.iAttributes = ETrue; |
|
336 } |
|
337 else if( aElement == KAttributeHiddenElement ) |
|
338 { |
|
339 inner = EAttributesH; |
|
340 outer = EAttributes; |
|
341 isSet = iSetValues.iAttributesH; |
|
342 } |
|
343 else if( aElement == KAttributeSystemElement ) |
|
344 { |
|
345 inner = EAttributesS; |
|
346 outer = EAttributes; |
|
347 isSet = iSetValues.iAttributesS; |
|
348 } |
|
349 else if( aElement == KAttributeArchivedElement ) |
|
350 { |
|
351 inner = EAttributesA; |
|
352 outer = EAttributes; |
|
353 isSet = iSetValues.iAttributesA; |
|
354 } |
|
355 else if( aElement == KAttributeDeleteElement ) |
|
356 { |
|
357 inner = EAttributesD; |
|
358 outer = EAttributes; |
|
359 isSet = iSetValues.iAttributesD; |
|
360 } |
|
361 else if( aElement == KAttributeWritableElement ) |
|
362 { |
|
363 inner = EAttributesW; |
|
364 outer = EAttributes; |
|
365 isSet = iSetValues.iAttributesW; |
|
366 } |
|
367 else if( aElement == KAttributeReadableElement ) |
|
368 { |
|
369 inner = EAttributesR; |
|
370 outer = EAttributes; |
|
371 isSet = iSetValues.iAttributesR; |
|
372 } |
|
373 else if( aElement == KAttributeExecutableElement ) |
|
374 { |
|
375 inner = EAttributesX; |
|
376 outer = EAttributes; |
|
377 isSet = iSetValues.iAttributesX; |
|
378 } |
|
379 else if( aElement == KFolderRoleElement ) |
|
380 { |
|
381 inner = EFolderRole; |
|
382 isSet = iSetValues.iRole; |
|
383 } |
|
384 else if( aElement == KExtElement ) |
|
385 { |
|
386 inner = EFolderExt; |
|
387 |
|
388 if ( startElement ) |
|
389 { |
|
390 if( iExtData ) |
|
391 { |
|
392 delete iExtData; |
|
393 iExtData = NULL; |
|
394 } |
|
395 iExtData = CNSmlExtData::NewL(); |
|
396 iSetValues.iXNam = EFalse; |
|
397 } |
|
398 else |
|
399 { |
|
400 if( !iSetValues.iXNam ) User::Leave(EMandatoryFieldNotFound); // xnam not set |
|
401 iExt->AppendL(iExtData); |
|
402 iExtData = NULL; |
|
403 } |
|
404 } |
|
405 else if( aElement == KExtXNamElement ) |
|
406 { |
|
407 inner = EFolderExtXNam; |
|
408 outer = EFolderExt; |
|
409 isSet = iSetValues.iXNam; |
|
410 } |
|
411 else if( aElement == KExtXValElement ) |
|
412 { |
|
413 inner = EFolderExtXVal; |
|
414 outer = EFolderExt; |
|
415 } |
|
416 else |
|
417 { |
|
418 User::Leave(EInvalidXmlError); |
|
419 } |
|
420 |
|
421 |
|
422 // finally, change state depending on was the element start or end element |
|
423 if ( startElement ) |
|
424 { |
|
425 StartElementStateChangeL( outer, inner, isSet ); |
|
426 } |
|
427 else |
|
428 { |
|
429 EndElementStateChangeL( inner, outer ); |
|
430 } |
|
431 |
|
432 } |
|
433 |
|
434 // ----------------------------------------------------------------------------- |
|
435 // CNSmlFolderParser::NextDataL |
|
436 // Base class' ParseL method calls this method, when a data is read from xml, |
|
437 // but that data is not element name (data inside or between elements). |
|
438 // ----------------------------------------------------------------------------- |
|
439 // |
|
440 void CNSmlFolderParser::NextDataL( TPtrC8 aData ) |
|
441 { |
|
442 switch(iCurrentState) |
|
443 { |
|
444 case EFolderNone: |
|
445 case EFolder: |
|
446 case EAttributes: |
|
447 case EFolderExt: |
|
448 LeaveIfNotWhiteSpaceL( aData ); |
|
449 break; |
|
450 case EFolderName: |
|
451 iName = aData.AllocL(); |
|
452 iSetValues.iName = ETrue; |
|
453 break; |
|
454 case EFolderCreated: |
|
455 iCreated = StringToTTimeL(aData); |
|
456 iSetValues.iCreated = ETrue; |
|
457 break; |
|
458 case EFolderModified: |
|
459 iModified = StringToTTimeL(aData); |
|
460 iSetValues.iModified = ETrue; |
|
461 break; |
|
462 case EFolderAccessed: |
|
463 iAccessed = StringToTTimeL(aData); |
|
464 iSetValues.iAccessed = ETrue; |
|
465 break; |
|
466 case EAttributesH: |
|
467 iAttributes.iHidden = StringToBooleanL(aData); |
|
468 iSetValues.iAttributesH = ETrue; |
|
469 break; |
|
470 case EAttributesS: |
|
471 iAttributes.iSystem = StringToBooleanL(aData); |
|
472 iSetValues.iAttributesS = ETrue; |
|
473 break; |
|
474 case EAttributesA: |
|
475 iAttributes.iArchived = StringToBooleanL(aData); |
|
476 iSetValues.iAttributesA = ETrue; |
|
477 break; |
|
478 case EAttributesD: |
|
479 iAttributes.iDelete = StringToBooleanL(aData); |
|
480 iSetValues.iAttributesD = ETrue; |
|
481 break; |
|
482 case EAttributesW: |
|
483 iAttributes.iWritable = StringToBooleanL(aData); |
|
484 iSetValues.iAttributesW = ETrue; |
|
485 break; |
|
486 case EAttributesR: |
|
487 iAttributes.iReadable = StringToBooleanL(aData); |
|
488 iSetValues.iAttributesR = ETrue; |
|
489 break; |
|
490 case EAttributesX: |
|
491 iAttributes.iExecutable = StringToBooleanL(aData); |
|
492 iSetValues.iAttributesX = ETrue; |
|
493 break; |
|
494 case EFolderRole: |
|
495 iRole = aData.AllocL(); |
|
496 iSetValues.iRole = ETrue; |
|
497 break; |
|
498 case EFolderExtXNam: |
|
499 iExtData->iXNam = aData.AllocL(); |
|
500 iSetValues.iXNam = ETrue; |
|
501 break; |
|
502 case EFolderExtXVal: |
|
503 iExtData->AddXValL(aData.AllocL()); |
|
504 break; |
|
505 default: |
|
506 User::Leave(EUnknownError); // should never happen |
|
507 break; |
|
508 } |
|
509 } |
|
510 |
|
511 // ----------------------------------------------------------------------------- |
|
512 // CNSmlFolderParser::StartElementStateChangeL |
|
513 // Checks that the state change (new start element) is legal (right order of |
|
514 // elements and element not already set) and changes the state. |
|
515 // ----------------------------------------------------------------------------- |
|
516 // |
|
517 void CNSmlFolderParser::StartElementStateChangeL(TNSmlCurrentFolderElement aCurrentState, |
|
518 TNSmlCurrentFolderElement aNextState, |
|
519 TBool aIsSet) |
|
520 { |
|
521 if( aIsSet || iCurrentState != aCurrentState || aNextState < iLastState ) |
|
522 { |
|
523 User::Leave(EInvalidXmlError); |
|
524 } |
|
525 |
|
526 iLastState = iCurrentState; |
|
527 iCurrentState = aNextState; |
|
528 } |
|
529 |
|
530 // ----------------------------------------------------------------------------- |
|
531 // CNSmlFolderParser::EndElementStateChangeL |
|
532 // Checks that the state change (new end element) is legal (right order of |
|
533 // elements and element not already set) and changes the state. |
|
534 // ----------------------------------------------------------------------------- |
|
535 // |
|
536 void CNSmlFolderParser::EndElementStateChangeL(TNSmlCurrentFolderElement aCurrentState, |
|
537 TNSmlCurrentFolderElement aNextState ) |
|
538 { |
|
539 if( iCurrentState != aCurrentState ) |
|
540 { |
|
541 User::Leave(EInvalidXmlError); |
|
542 } |
|
543 |
|
544 iLastState = iCurrentState; |
|
545 iCurrentState = aNextState; |
|
546 } |
|
547 |
|
548 // ----------------------------------------------------------------------------- |
|
549 // CNSmlFolderParser::ConvertIntoEntitiesL |
|
550 // Converts special characters of this dataobject to corresponding |
|
551 // characters. |
|
552 // ----------------------------------------------------------------------------- |
|
553 // |
|
554 void CNSmlFolderParser::ConvertIntoEntitiesL() |
|
555 { |
|
556 // name |
|
557 if ( iName ) |
|
558 { |
|
559 CharactersToEntitiesL(iName, 0, iName->Length()); |
|
560 } |
|
561 |
|
562 // role |
|
563 if ( iRole ) |
|
564 { |
|
565 CharactersToEntitiesL(iRole, 0, iRole->Length()); |
|
566 } |
|
567 |
|
568 // extensions |
|
569 if ( iExt ) |
|
570 { |
|
571 for (TInt i=0; i < iExt->Count(); ++i) |
|
572 { |
|
573 iExt->At(i)->ConvertIntoEntitiesL(this); |
|
574 } |
|
575 } |
|
576 } |
|
577 |
|
578 // ----------------------------------------------------------------------------- |
|
579 // CNSmlFolderParser::CNSmlFolderParser |
|
580 // Constructor |
|
581 // ----------------------------------------------------------------------------- |
|
582 // |
|
583 CNSmlFolderParser::CNSmlFolderParser() |
|
584 : iCreated(Time::NullTTime()), iModified(Time::NullTTime()), |
|
585 iAccessed(Time::NullTTime()) |
|
586 { |
|
587 } |
|
588 |
|
589 // ----------------------------------------------------------------------------- |
|
590 // CNSmlFolderParser::ConstructL |
|
591 // Second phase construction |
|
592 // ----------------------------------------------------------------------------- |
|
593 // |
|
594 void CNSmlFolderParser::ConstructL() |
|
595 { |
|
596 iExt = new (ELeave) CNSmlExtDataArray(3); |
|
597 } |
|
598 |
|
599 // ----------------------------------------------------------------------------- |
|
600 // CNSmlFolderParser::TNSmlSetFolderValues::TNSmlSetFolderValues |
|
601 // ----------------------------------------------------------------------------- |
|
602 // |
|
603 CNSmlFolderParser::TNSmlSetFolderValues::TNSmlSetFolderValues() |
|
604 { |
|
605 Reset(); |
|
606 } |
|
607 |
|
608 // ----------------------------------------------------------------------------- |
|
609 // CNSmlFolderParser::TNSmlSetFolderValues::Reset |
|
610 // ----------------------------------------------------------------------------- |
|
611 // |
|
612 void CNSmlFolderParser::TNSmlSetFolderValues::Reset() |
|
613 { |
|
614 iFolder = EFalse; |
|
615 iName = EFalse; |
|
616 iCreated = EFalse; |
|
617 iModified = EFalse; |
|
618 iAccessed = EFalse; |
|
619 iAttributes = EFalse; |
|
620 iAttributesH = EFalse; |
|
621 iAttributesS = EFalse; |
|
622 iAttributesA = EFalse; |
|
623 iAttributesD = EFalse; |
|
624 iAttributesW = EFalse; |
|
625 iAttributesR = EFalse; |
|
626 iAttributesX = EFalse; |
|
627 iXNam = EFalse; |
|
628 iRole = EFalse; |
|
629 } |
|
630 |
|
631 // End of File |