omads/omadsextensions/dsutils/emailxmlutils/src/NSmlEmailFileParser.cpp
changeset 0 dab8a81a92de
equal deleted inserted replaced
-1:000000000000 0:dab8a81a92de
       
     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 "nsmlxmlparser.h"
       
    25 
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CNSmlEmailFileParser::NewL
       
    31 // Two-phased constructor.
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 EXPORT_C CNSmlEmailFileParser* CNSmlEmailFileParser::NewL()
       
    35 	{
       
    36 	CNSmlEmailFileParser* self = CNSmlEmailFileParser::NewLC();
       
    37 	CleanupStack::Pop();
       
    38 
       
    39 	return self;
       
    40 	}
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // CNSmlEmailFileParser::NewLC
       
    44 // Two-phased constructor.
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 EXPORT_C CNSmlEmailFileParser* CNSmlEmailFileParser::NewLC()
       
    48 	{
       
    49 	CNSmlEmailFileParser* self = new (ELeave) CNSmlEmailFileParser();
       
    50 	CleanupStack::PushL(self);
       
    51 	self->ConstructL();
       
    52 
       
    53 	return self;
       
    54 	}
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CNSmlEmailFileParser::~CNSmlEmailFileParser
       
    58 // Destructor.
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 EXPORT_C CNSmlEmailFileParser::~CNSmlEmailFileParser()
       
    62 	{
       
    63 	// close rfs and open streams
       
    64 	if ( iWriteStream.Sink() )
       
    65 		iWriteStream.Close();
       
    66 
       
    67 	iRfs.Close();
       
    68 	delete iEmailItemFile;
       
    69 	}
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CNSmlEmailFileParser::ParseXml
       
    73 // Parses the email xml, that is located in the file whose name is given 
       
    74 // as aXml (path and filename)
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 EXPORT_C TNSmlParserGeneratorError CNSmlEmailFileParser::ParseXml( HBufC8* aXml )
       
    78 	{
       
    79 	if ( !aXml )
       
    80 		return EInvalidFilename;
       
    81 
       
    82 	HBufC* filename = Buf8ToBuf16( aXml );
       
    83 	if ( !filename )
       
    84 		{
       
    85 		return EOutOfMemory;
       
    86 		}
       
    87 
       
    88 	// open the stream and parse
       
    89 	TNSmlParserGeneratorError err = EErrorNone;
       
    90 	RFileReadStream rs;
       
    91 	if ( rs.Open(iRfs, *filename, KFileReadAccess) != KErrNone )
       
    92 		{
       
    93 		err = EInvalidFilename;
       
    94 		}
       
    95 	else
       
    96 		{
       
    97 		err = ParseXml( rs );
       
    98 		rs.Close();
       
    99 		}
       
   100 
       
   101 	delete filename;
       
   102 
       
   103 	return err;
       
   104 	}
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CNSmlEmailFileParser::ParseXml
       
   108 // Parses the xml from the given stream, and writes the emailitem - if present - to a temp file
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 EXPORT_C TNSmlParserGeneratorError CNSmlEmailFileParser::ParseXml( RReadStream& aRs )
       
   112 	{
       
   113 	iSetValues.Reset();
       
   114 	iCurrentState = ENone;
       
   115 	iLastState = ENone;
       
   116 
       
   117 	TRAPD(err, ParseFromStreamL( aRs ));
       
   118 
       
   119 	if( err == EErrorNone && iCurrentState != ENone )
       
   120 		return EInvalidXmlError;
       
   121 
       
   122 	return CheckError(err);
       
   123 	}
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // CNSmlEmailFileParser::GenerateXml
       
   127 // Generates the xml and writes it to the file whose name is given in aXml (path 
       
   128 // and filename).
       
   129 // -----------------------------------------------------------------------------
       
   130 //
       
   131 EXPORT_C TNSmlParserGeneratorError CNSmlEmailFileParser::GenerateXml( HBufC8*& aXml )
       
   132 	{
       
   133 	if ( !aXml )
       
   134 		return EInvalidFilename;
       
   135 
       
   136 	HBufC* filename = Buf8ToBuf16( aXml );
       
   137 	if ( !filename )
       
   138 		{
       
   139 		return EOutOfMemory;
       
   140 		}
       
   141 
       
   142 	// open the stream and generate
       
   143 	iWriteStream.Replace(iRfs, *filename, KFileWriteAccess);
       
   144 	TNSmlParserGeneratorError err = GenerateXml( iWriteStream );
       
   145 	iWriteStream.Close();
       
   146 
       
   147 	delete filename;
       
   148 
       
   149 	return err;
       
   150 	}
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // CNSmlEmailFileParser::GenerateXml
       
   154 // Generates the xml and writes it to the given stream.
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 EXPORT_C TNSmlParserGeneratorError CNSmlEmailFileParser::GenerateXml( RWriteStream& aWs )
       
   158 	{
       
   159 	TRAPD( err, GenerateEmailXmlL( aWs ) );
       
   160 
       
   161 	return CheckError(err);
       
   162 	}
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // CNSmlEmailFileParser::GenerateEmailXmlL
       
   166 // -----------------------------------------------------------------------------
       
   167 //
       
   168 void CNSmlEmailFileParser::GenerateEmailXmlL( RWriteStream& aWs )
       
   169 	{
       
   170 	ConvertIntoEntitiesL();
       
   171 
       
   172 	// count the size of the xml (does not include emailitem, since it is - if present - in a file)
       
   173 	TInt size = CNSmlEmailParser::CountXmlSizeL();
       
   174 	if ( iUseEmailItem ) size += 20; // extra for emailitem tags
       
   175 
       
   176 	// create a buffer for using the size
       
   177 	HBufC8* xml = HBufC8::NewLC(size);
       
   178 	TPtr8 ptr = xml->Des();
       
   179 
       
   180 	// append data to buffer
       
   181 	AppendElement(ptr, KEmailElement());
       
   182 
       
   183 	if ( iRead )
       
   184 		{
       
   185 		AppendElement(ptr, KEmailReadElement(), BooleanToString( iRead ));
       
   186 		}
       
   187 
       
   188 	if ( iForwarded )
       
   189 		{
       
   190 		AppendElement(ptr, KEmailForwardedElement(), BooleanToString( iForwarded ));
       
   191 		}
       
   192 
       
   193 	if ( iReplied )
       
   194 		{
       
   195 		AppendElement(ptr, KEmailRepliedElement(), BooleanToString( iReplied ));
       
   196 		}
       
   197 
       
   198 	if (iReceived != Time::NullTTime())
       
   199 		{
       
   200 		AppendElement(ptr, KEmailReceivedElement(), DateTimeToStringL(iReceived));
       
   201 		}
       
   202 
       
   203 	if (iCreated != Time::NullTTime())
       
   204 		{
       
   205 		AppendElement(ptr, KEmailCreatedElement(), DateTimeToStringL(iCreated));
       
   206 		}
       
   207 
       
   208 	if (iModified != Time::NullTTime())
       
   209 		{
       
   210 		AppendElement(ptr, KEmailModifiedElement(), DateTimeToStringL(iModified));
       
   211 		}
       
   212 
       
   213 	if ( iDeleted )
       
   214 		{
       
   215 		AppendElement(ptr, KEmailDeletedElement(), BooleanToString( iDeleted ));
       
   216 		}
       
   217 
       
   218 	if ( iFlagged )
       
   219 		{
       
   220 		AppendElement(ptr, KEmailFlaggedElement(), BooleanToString( iFlagged ));
       
   221 		}
       
   222 
       
   223 	// if emailitem should be used in generation, write it to the stream
       
   224 	if ( iUseEmailItem )
       
   225 		{
       
   226 		// append start tag and cdata start
       
   227 		AppendElement(ptr, KEmailItemElement());
       
   228 		ptr.Append(KCDataStart);
       
   229 
       
   230 		// open a stream to emailitem
       
   231 		RFileReadStream rs;
       
   232 		TInt err = rs.Open( iRfs, *iEmailItemFile, KFileReadAccess );
       
   233 		if ( err != KErrNone )
       
   234 			User::Leave( EInvalidFilename );
       
   235 		
       
   236 		CleanupClosePushL(rs);
       
   237 		
       
   238 		// write the buffer to the stream
       
   239 		aWs.WriteL(ptr);
       
   240 		
       
   241 		// write emailitem from stream
       
   242 		aWs.WriteL(rs);
       
   243 		
       
   244 		// close the opened read stream
       
   245 		CleanupStack::Pop(); // rs
       
   246 
       
   247 		// empty the buffer (i.e. set the length to zero)
       
   248 		ptr.SetLength(0);
       
   249 
       
   250 		// append emailitem end tag and cdata inner end
       
   251 		ptr.Append(KCDataEnd);
       
   252 		AppendEndElement(ptr, KEmailItemElement());
       
   253 		}
       
   254 
       
   255 	// append the rest of the data to the buffer
       
   256 	if ( iTruncated )
       
   257 		{
       
   258 		iTruncated->GenerateXmlL(ptr, this);
       
   259 		}
       
   260 
       
   261 	if ( iExt )
       
   262 		{
       
   263 		for (TInt i=0; i < iExt->Count(); ++i)
       
   264 			{
       
   265 			iExt->At(i)->GenerateXmlL(ptr, this);
       
   266 			}
       
   267 
       
   268 		}
       
   269 
       
   270 	AppendEndElement(ptr, KEmailElement());
       
   271 
       
   272 	// write the buffer to the stream and commit
       
   273 	aWs.WriteL(ptr);
       
   274 	aWs.CommitL();
       
   275 
       
   276 	CleanupStack::PopAndDestroy(); // xml
       
   277 
       
   278 	}
       
   279 
       
   280 // -----------------------------------------------------------------------------
       
   281 // CNSmlEmailFileParser::CountXmlSizeL
       
   282 // -----------------------------------------------------------------------------
       
   283 //
       
   284 EXPORT_C TInt CNSmlEmailFileParser::CountXmlSizeL()
       
   285 	{
       
   286 	// counting the size is not essential, since xml is written to a file
       
   287 	return KErrNotSupported;
       
   288 	}
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // CNSmlEmailFileParser::HandleNextEmailItemDataL
       
   292 // Next emailitem data
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 void CNSmlEmailFileParser::HandleNextEmailItemDataL( TPtrC8 aData )
       
   296 	{
       
   297 	// write the given piece of data into a file (stream)
       
   298 	iWriteStream.WriteL(aData);
       
   299 	}
       
   300 
       
   301 
       
   302 // -----------------------------------------------------------------------------
       
   303 // CNSmlEmailFileParser::HandleNextEmailItemStartL
       
   304 // Emailitem element starts
       
   305 // -----------------------------------------------------------------------------
       
   306 //
       
   307 void CNSmlEmailFileParser::HandleNextEmailItemStartL()
       
   308 	{
       
   309 	// open the write stream
       
   310 	User::LeaveIfError( iWriteStream.Replace( iRfs, *iEmailItemFile, KFileWriteAccess ) );
       
   311 	iReadingEmailItemData = ETrue;
       
   312 	}
       
   313 
       
   314 // -----------------------------------------------------------------------------
       
   315 // CNSmlEmailFileParser::HandleNextEmailItemEndL
       
   316 // Emailitem element ends
       
   317 // -----------------------------------------------------------------------------
       
   318 //
       
   319 void CNSmlEmailFileParser::HandleNextEmailItemEndL()
       
   320 	{
       
   321 	// close the write stream
       
   322 	iWriteStream.Close();
       
   323 	iReadingEmailItemData = EFalse;
       
   324 	}
       
   325 
       
   326 // -----------------------------------------------------------------------------
       
   327 // CNSmlEmailFileParser::ParseFromStreamL
       
   328 // -----------------------------------------------------------------------------
       
   329 //
       
   330 void CNSmlEmailFileParser::ParseFromStreamL( RReadStream& aRs )
       
   331 	{
       
   332 
       
   333 	// reset preprocess variables and parser
       
   334 	ResetPreProcessor();
       
   335 	ResetParserL();
       
   336 
       
   337 	// read and process the data piece by piece
       
   338 	HBufC8* piece = HBufC8::NewLC(KReadDataLength);
       
   339 	TPtr8 ptr = piece->Des();
       
   340 	TInt dataSize = aRs.Source()->SizeL();
       
   341 
       
   342 	TInt pos = 0;
       
   343 	iFirstPiece = ETrue;
       
   344 	for( pos = KReadDataLength; pos <= dataSize ; pos += KReadDataLength )
       
   345 		{
       
   346 		// read piece of data from the stream
       
   347 		aRs.ReadL(ptr, KReadDataLength);
       
   348 
       
   349 		// check that there are no incomplete cdata/comment/entity at the end
       
   350 		dataSize += RemoveIncompleteL( piece, aRs );
       
   351 
       
   352 		// pre-process
       
   353 		PreProcessPieceL( piece );
       
   354 
       
   355 		// parsing
       
   356 		TPtrC8 temp( *piece );
       
   357 		ParsePieceL( temp );
       
   358 		
       
   359 		iFirstPiece = EFalse;
       
   360 		}
       
   361 
       
   362 	TInt lastPartLength = dataSize - pos + KReadDataLength;
       
   363 	aRs.ReadL(ptr, lastPartLength);
       
   364 
       
   365 	PreProcessPieceL( piece );
       
   366 
       
   367 	// parsing
       
   368 	TPtrC8 temp( *piece );
       
   369 	ParsePieceL( temp, ETrue );
       
   370 
       
   371 	CleanupStack::PopAndDestroy(); // piece
       
   372 	}
       
   373 
       
   374 // -----------------------------------------------------------------------------
       
   375 // CNSmlEmailFileParser::ParsePieceL
       
   376 // -----------------------------------------------------------------------------
       
   377 //
       
   378 void CNSmlEmailFileParser::ParsePieceL( TPtrC8& aXml, TBool aLastPiece )
       
   379 	{
       
   380 
       
   381 	// check if last cdata has not ended yet
       
   382 	if ( iReadingCData )
       
   383 		{
       
   384 		TInt endPos = aXml.Find(KCDataEnd);
       
   385 		TPtrC8 cdata = _L8("");
       
   386 
       
   387 		if ( endPos == KErrNotFound && aLastPiece )
       
   388 			{
       
   389 			User::Leave(EInvalidCDataStructure);
       
   390 			}
       
   391 		else if ( endPos == KErrNotFound )
       
   392 			{
       
   393 			// cdata end was not found, read the whole piece as cdata
       
   394 			NextDataL( aXml );
       
   395 			return;
       
   396 			}
       
   397 		else 
       
   398 			{
       
   399 			// add data until cdata end to buffer
       
   400 			cdata.Set( aXml.Left( endPos ) );
       
   401 			aXml.Set( aXml.Right( aXml.Length() - endPos - KCDataEnd().Length() ) );			
       
   402 			iReadingCData = EFalse;
       
   403 			}
       
   404 
       
   405 		AddToCompleteL(cdata);
       
   406 		NextDataL(*iCompleteBuffer);
       
   407 		ResetBufferL(iCompleteBuffer);
       
   408 
       
   409 		}
       
   410 #ifndef __NO_XML_COMMENTS_
       
   411 	else if ( iReadingComment ) // check if last comment has not ended yet
       
   412 		{
       
   413 		TInt endPos = aXml.Find(KCommentEnd);
       
   414 
       
   415 		if ( endPos == KErrNotFound && aLastPiece )
       
   416 			{ // this is the last piece of data and end of comment has not been found -> error
       
   417 			User::Leave(EInvalidXmlError);
       
   418 			}
       
   419 		else if ( endPos == KErrNotFound )
       
   420 			{
       
   421 			// end of comment was not found, no need to parse this piece of data
       
   422 			return;
       
   423 			}
       
   424 		else 
       
   425 			{
       
   426 			// end of cdata was found, jump to that position
       
   427 			aXml.Set( aXml.Right( aXml.Length() - endPos - KCommentEnd().Length() ) );
       
   428 			iReadingComment = EFalse;
       
   429 			}
       
   430 		}
       
   431 #endif
       
   432 
       
   433 	// parse through the string
       
   434 	TText c;
       
   435 
       
   436 	for( TInt i=0; i < aXml.Length(); ++i )
       
   437 		{
       
   438 		c = aXml[i];
       
   439 		switch(c)
       
   440 			{
       
   441 			case KElementStart:
       
   442 				// if currently reading element, error
       
   443 				if( iReadingElementName )
       
   444 					{
       
   445 					User::Leave(EInvalidXmlError);
       
   446 					}
       
   447 
       
   448 				if( aXml.Length()-i >= KCDataStart().Length() &&
       
   449 					!aXml.Mid(i, KCDataStart().Length()).Compare(KCDataStart()) )
       
   450 					{ // cdata
       
   451 					aXml.Set( aXml.Right( aXml.Length() - i ) );
       
   452 					TInt endPos = aXml.Find(KCDataEnd);
       
   453 
       
   454 					TPtrC8 cdata = _L8("");
       
   455 					if ( endPos == KErrNotFound && aLastPiece )
       
   456 						{
       
   457 						User::Leave(EInvalidCDataStructure);
       
   458 						}
       
   459 					else if ( endPos == KErrNotFound )
       
   460 						{
       
   461 						// cdata end was not found, read all the rest as cdata
       
   462 						cdata.Set( aXml.Right( aXml.Length() - KCDataStart().Length() ) );
       
   463 						i = aXml.Length(); // move to the end
       
   464 						iReadingCData = ETrue;
       
   465 						}
       
   466 					else 
       
   467 						{
       
   468 						cdata.Set( aXml.Mid( KCDataStart().Length(), endPos - KCDataStart().Length() ) );
       
   469 						aXml.Set( aXml.Right( aXml.Length() - endPos - KCDataEnd().Length() ) );
       
   470 
       
   471 						i = -1;
       
   472 						}
       
   473 
       
   474 					// add current buffer to complete buffer
       
   475 					EntitiesToCharactersL( iBuffer, 0, iBuffer->Length() );
       
   476 					AddToCompleteL(*iBuffer);
       
   477 					ResetBufferL(iBuffer);
       
   478 					AddToCompleteL(cdata);
       
   479 					}
       
   480 #ifndef __NO_XML_COMMENTS_
       
   481 				else if( aXml.Length()-i >= KCommentStart().Length() &&
       
   482 					!aXml.Mid(i, KCommentStart().Length()).Compare(KCommentStart()) )
       
   483 					{ // comment
       
   484 					aXml.Set( aXml.Right( aXml.Length() - i ) );
       
   485 					TInt endPos = aXml.Find(KCommentEnd);
       
   486 
       
   487 					if ( endPos == KErrNotFound && aLastPiece)
       
   488 						{
       
   489 						// this is the last piece of data, and end of comment was not found -> error
       
   490 						User::Leave(EInvalidXmlError);
       
   491 						}
       
   492 					else if ( endPos == KErrNotFound )
       
   493 						{
       
   494 						// the end was not found, no need to parse the rest since it is comment
       
   495 						iReadingComment = ETrue;
       
   496 						return;
       
   497 						}
       
   498 					else
       
   499 						{
       
   500 						// the end of comment was found, move to the end of comment and start parsing 
       
   501 						// from there on
       
   502 						aXml.Set( aXml.Right( aXml.Length() - endPos - KCommentEnd().Length() ) );
       
   503 						}
       
   504 
       
   505 					i = -1;
       
   506 					}
       
   507 #endif
       
   508 				else 
       
   509 					{
       
   510 					// send the buffer
       
   511 					EntitiesToCharactersL( iBuffer, 0, iBuffer->Length() );
       
   512 					AddToCompleteL(*iBuffer);
       
   513 					NextDataL(*iCompleteBuffer);
       
   514 					ResetBufferL(iBuffer);
       
   515 					ResetBufferL(iCompleteBuffer);
       
   516 					iReadingElementName = ETrue;
       
   517 					}
       
   518 
       
   519 				break;
       
   520 			case KElementEnd:
       
   521 				// stop reading element name
       
   522 				if( !iReadingElementName )
       
   523 					{
       
   524 					User::Leave(EInvalidXmlError);
       
   525 					}
       
   526 				else 
       
   527 					{
       
   528 					NextElementL(*iBuffer);
       
   529 					ResetBufferL(iBuffer);
       
   530 					iReadingElementName = EFalse;
       
   531 					}
       
   532 				break;
       
   533 			default:
       
   534 				// add char to buffer
       
   535 				AddToBufferL(c, iBuffer);
       
   536 				break;
       
   537 			}
       
   538 		}
       
   539 
       
   540 	// send the data in the buffer
       
   541 	if ( ( iReadingEmailItemData || aLastPiece ) && !iReadingElementName )
       
   542 		{
       
   543 		EntitiesToCharactersL( iBuffer, 0, iBuffer->Length() );
       
   544 		AddToCompleteL(*iBuffer);
       
   545 		NextDataL(*iCompleteBuffer);
       
   546 		ResetBufferL(iBuffer);
       
   547 		ResetBufferL(iCompleteBuffer);
       
   548 		}
       
   549 
       
   550 	}
       
   551 
       
   552 // -----------------------------------------------------------------------------
       
   553 // CNSmlEmailFileParser::RemoveIncompleteL
       
   554 // Checks if there is incomplete cdata/comment/entity at the end of the given
       
   555 // piece. If one is found, it is removed from aPiece and the read mark of the
       
   556 // stream is moved backwards the length of the removed item (i.e. the incomplete
       
   557 // item will be read to the next piece as a whole).
       
   558 // -----------------------------------------------------------------------------
       
   559 //
       
   560 TUint CNSmlEmailFileParser::RemoveIncompleteL( HBufC8* aPiece, RReadStream& aStream )
       
   561 	{
       
   562 	// we need to check only the last nine characters
       
   563 	TUint removeSize = CheckPiece( aPiece->Right( 9 ) );
       
   564 
       
   565 	if ( removeSize )
       
   566 		{
       
   567 		// remove the incomplete item from aPiece
       
   568 		TPtr8 ptr = aPiece->Des();
       
   569 		ptr.Delete( aPiece->Length() - removeSize, removeSize );
       
   570 
       
   571 		// move backwards in reading the stream
       
   572 		aStream.Source()->SeekL( MStreamBuf::ERead, aStream.Source()->TellL(MStreamBuf::ERead) - removeSize );
       
   573 		}
       
   574 
       
   575 	// no need to loop the rest of aPiece. If xml is not correctly formed,
       
   576 	// an error occures later in the parser
       
   577 
       
   578 	return removeSize;
       
   579 	}
       
   580 
       
   581 // -----------------------------------------------------------------------------
       
   582 // CNSmlEmailFileParser::CheckPiece
       
   583 // -----------------------------------------------------------------------------
       
   584 //
       
   585 TUint CNSmlEmailFileParser::CheckPiece( const TPtrC8 aEnd ) const
       
   586 	{
       
   587 	// check that there are no incomplete cdata/comment/entity at the end	
       
   588 	// (cdata start, cdata end, cdata inner end, comment start, comment end, entities)
       
   589 	// ( <![CDATA[ , ]]> , <!-- , --> , &xxxx; , ]]]]>&gt; , ]]]]&gt;> )
       
   590 	// do not change the order
       
   591 
       
   592 	// possible inner cdata end
       
   593 	TInt pos = aEnd.Find( _L8("]]]") );
       
   594 	if ( pos != KErrNotFound )
       
   595 		{
       
   596 		return aEnd.Length() - pos;
       
   597 		}
       
   598 
       
   599 	// beginning part of comment or cdata
       
   600 	pos = aEnd.Find( _L8("<!") );
       
   601 	if ( pos != KErrNotFound )
       
   602 		{
       
   603 		return aEnd.Length() - pos;
       
   604 		}
       
   605 
       
   606 	// end of cdata
       
   607 	TText lastChar = aEnd[aEnd.Length()-1];
       
   608 	TText secondLastChar = aEnd[aEnd.Length()-2];
       
   609 	if ( lastChar == ']' && secondLastChar == ']' )
       
   610 		{
       
   611 		return 2;
       
   612 		}
       
   613 
       
   614 #ifndef __NO_XML_COMMENTS_
       
   615 	// comment end
       
   616 	if ( lastChar == '-' && secondLastChar == '-' )
       
   617 		{
       
   618 		return 2;
       
   619 		}
       
   620 #endif
       
   621 
       
   622 	// special chars
       
   623 	if ( lastChar == '<' || lastChar == ']' || lastChar == '-' )
       
   624 		{
       
   625 		return 1;
       
   626 		}
       
   627 
       
   628 	// possible entity
       
   629 	pos = aEnd.Find( _L8("&") );
       
   630 	if ( pos != KErrNotFound )
       
   631 		{
       
   632 		return aEnd.Length() - pos;
       
   633 		}
       
   634 
       
   635 	return 0;
       
   636 	}
       
   637 
       
   638 // -----------------------------------------------------------------------------
       
   639 // CNSmlEmailFileParser::PreProcessPieceL
       
   640 // -----------------------------------------------------------------------------
       
   641 //
       
   642 void CNSmlEmailFileParser::PreProcessPieceL( HBufC8* aXml )
       
   643 	{
       
   644 	// take a modifiable pointer
       
   645 	TPtr8 xml = aXml->Des();
       
   646 
       
   647 	// the string used for searching and moving in the string
       
   648 	TPtrC8 searchString(*aXml);
       
   649 
       
   650 	// the current position in the original string
       
   651 	TInt searchStartPos = 0;
       
   652 
       
   653 	// if start has been found and end has not, try to find end first
       
   654 	if ( iCdataEndPos == KErrNotFound && iCdataStartPos != KErrNotFound && iCdataFoundFromFirstPiece )
       
   655 		{
       
   656 		// we're looking for a cdata end
       
   657 		iCdataEndPos = searchString.Find(KCDataEnd);
       
   658 
       
   659 		// if the end was found, remove it and start looking for a cdata start
       
   660 		if ( iCdataEndPos != KErrNotFound )
       
   661 			{
       
   662 			// remove cdata end
       
   663 			xml.Delete(iCdataEndPos, KCDataEnd().Length());
       
   664 			searchStartPos = iCdataEndPos;
       
   665 			searchString.Set( xml.Right(xml.Length() - searchStartPos) );
       
   666 			}
       
   667 		else
       
   668 			{
       
   669 			// end was not found, return
       
   670 			return;
       
   671 			}
       
   672 		}
       
   673 	
       
   674 	iCdataStartPos = searchString.Find(KCDataStart);
       
   675 	
       
   676 	// If CDATA is not found from beginning then data is not inside CDATA and then
       
   677 	// preprocessing is not needed
       
   678 	if ( iCdataStartPos != 0 && iFirstPiece )
       
   679 	    {
       
   680 	    return;
       
   681 	    }
       
   682 	else if ( !iFirstPiece && !iCdataFoundFromFirstPiece )
       
   683 		{
       
   684 		return;
       
   685 		}
       
   686 	else if ( iCdataStartPos == 0 && iFirstPiece )
       
   687 		{
       
   688 		iCdataFoundFromFirstPiece = ETrue;	
       
   689 		}
       
   690 	
       
   691 	
       
   692 	// while cdata is found
       
   693 	while ( iCdataStartPos != KErrNotFound )
       
   694 		{
       
   695 		iCdataStartPos += searchStartPos;
       
   696 		
       
   697 		// find an end of cdata before entities are converted
       
   698 		iCdataEndPos = searchString.Find(KCDataEnd);
       
   699 		
       
   700 		// convert entities between search start and cdata start
       
   701 		TInt entityChange = EntitiesToCharactersL(aXml, searchStartPos, iCdataStartPos);
       
   702 		xml.Set(aXml->Des());
       
   703 		iCdataStartPos += entityChange;
       
   704 		
       
   705 		if ( iCdataEndPos != KErrNotFound )
       
   706 			{
       
   707 			iCdataEndPos += entityChange;
       
   708 			iCdataEndPos += searchStartPos;
       
   709 			
       
   710 			// if the end is before start -> error
       
   711 			if ( iCdataEndPos < iCdataStartPos )
       
   712 				User::Leave( EInvalidCDataStructure );
       
   713 			
       
   714 			// remove cdata end
       
   715 			xml.Delete(iCdataEndPos, KCDataEnd().Length());
       
   716 			// remove cdata start
       
   717 			xml.Delete(iCdataStartPos, KCDataStart().Length());
       
   718 			
       
   719 			searchStartPos = iCdataEndPos - KCDataStart().Length();
       
   720 			searchString.Set( xml.Right(xml.Length() - searchStartPos) );
       
   721 			}
       
   722 		else
       
   723 			{
       
   724 			// remove cdata start
       
   725 			xml.Delete(iCdataStartPos, KCDataStart().Length());
       
   726 			
       
   727 			// since there was no end found, return
       
   728 			return;
       
   729 			}
       
   730 		
       
   731 		// find a new cdata start that is after the found end
       
   732 		iCdataStartPos = searchString.Find(KCDataStart);
       
   733 		
       
   734 		}
       
   735 	
       
   736 	if ( iCdataStartPos == KErrNotFound )
       
   737 		{
       
   738 		EntitiesToCharactersL(aXml, searchStartPos, aXml->Length());
       
   739 		}
       
   740 
       
   741 	}
       
   742 
       
   743 // -----------------------------------------------------------------------------
       
   744 // CNSmlEmailFileParser::ResetParserL
       
   745 // Resets the state of the parser, should be called before starting the parsing.
       
   746 // -----------------------------------------------------------------------------
       
   747 //
       
   748 void CNSmlEmailFileParser::ResetParserL()
       
   749 	{
       
   750 	ResetBufferL(iBuffer);
       
   751 	ResetBufferL(iCompleteBuffer);
       
   752 	iReadingElementName = EFalse;
       
   753 	iReadingCData = EFalse;
       
   754 	iReadingEmailItemData = EFalse;
       
   755 	iFirstPiece = EFalse;
       
   756 	iCdataFoundFromFirstPiece = EFalse;
       
   757 	}
       
   758 
       
   759 // -----------------------------------------------------------------------------
       
   760 // CNSmlEmailFileParser::ResetPreProcessor
       
   761 // Resets the variables used in preprocessor.
       
   762 // -----------------------------------------------------------------------------
       
   763 //
       
   764 void CNSmlEmailFileParser::ResetPreProcessor()
       
   765 	{
       
   766 	iCdataStartPos = KErrNotFound;
       
   767 	iCdataEndPos = KErrNotFound;
       
   768 	}
       
   769 
       
   770 // -----------------------------------------------------------------------------
       
   771 // CNSmlEmailFileParser::Buf8ToBuf16
       
   772 // -----------------------------------------------------------------------------
       
   773 //
       
   774 HBufC* CNSmlEmailFileParser::Buf8ToBuf16( const HBufC8* buf8 ) const
       
   775 	{
       
   776 	HBufC* buf16 = HBufC::New(buf8->Length());
       
   777 	if ( buf16 )
       
   778 		{
       
   779 		TPtr ptr = buf16->Des();
       
   780 		ptr.Copy( *buf8 );
       
   781 		}
       
   782 
       
   783 	return buf16;
       
   784 	}
       
   785 
       
   786 // -----------------------------------------------------------------------------
       
   787 // CNSmlEmailFileParser::CNSmlEmailFileParser
       
   788 // Constructor.
       
   789 // -----------------------------------------------------------------------------
       
   790 //
       
   791 CNSmlEmailFileParser::CNSmlEmailFileParser()
       
   792 	: iUseEmailItem(ETrue)
       
   793 	{
       
   794 	}
       
   795 
       
   796 // -----------------------------------------------------------------------------
       
   797 // CNSmlEmailFileParser::ConstructL
       
   798 // Second phase construction.
       
   799 // -----------------------------------------------------------------------------
       
   800 //
       
   801 void CNSmlEmailFileParser::ConstructL()
       
   802 	{
       
   803 	CNSmlEmailParser::ConstructL();
       
   804 	User::LeaveIfError( iRfs.Connect() );
       
   805 	
       
   806 	// create path+filename for emailitem
       
   807     iEmailItemFile = HBufC::NewL( KMaxEmailItemFileNameLength );
       
   808     TPtr file = iEmailItemFile->Des();
       
   809 
       
   810 	file.Append( KEmailItemFile );
       
   811 
       
   812 	}
       
   813 
       
   814 //  End of File