messagingfw/msgtest/integration/email/src/testframeparse.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "testframeparse.h"
       
    17 #include "emailsmoke.h"
       
    18 
       
    19 _LIT(KTestErrorUnknown, "No more details available");
       
    20 _LIT(KInvalidNumberOfArguments, "[%4d] Invalid number of arguments, actual: %d, expected: %d");
       
    21 _LIT(KGeneralParseError, "[%4d] Error in command: %S, section: %S");
       
    22 _LIT(KNotParsedError, "[%4d] Command not recognised: %S, in section: %S");
       
    23 
       
    24 //
       
    25 //
       
    26 // CTestBaseCommandParser
       
    27 //
       
    28 
       
    29 CBaseCommandParser::~CBaseCommandParser()
       
    30 	{
       
    31 	delete iArgumentList;
       
    32 	delete iSupportedCommands;
       
    33 	delete iCurrentCommand;
       
    34 	delete iLastError;
       
    35 	}
       
    36 
       
    37 void CBaseCommandParser::ConstructL()
       
    38 	{
       
    39 	iArgumentList = new (ELeave) CDesCArrayFlat(3);
       
    40 	iSupportedCommands = new (ELeave) CDesCArrayFlat(1);
       
    41 	iLastError = HBufC::NewL(0);
       
    42 	iCurrentCommand = 0;
       
    43 	}
       
    44 
       
    45 void CBaseCommandParser::ParseL(const TDesC& aCommand, TTestDebugInfo aDebugInfo)
       
    46 // Extract the command and any argments from the given string
       
    47 	{
       
    48 	iDebugInfo = aDebugInfo;
       
    49 	iArgumentList->Reset();
       
    50 
       
    51 	delete iCurrentCommand;
       
    52 	iCurrentCommand = 0;
       
    53 	TInt commandIter = 0;
       
    54 	TInt argumentStart = 0;
       
    55 	while (commandIter <= aCommand.Length())
       
    56 		{
       
    57 		TBool commandFound = EFalse;
       
    58 		if (commandIter < aCommand.Length())
       
    59 			{
       
    60 			if (aCommand[commandIter] == ' ')
       
    61 				commandFound = ETrue;
       
    62 			}
       
    63 		else
       
    64 			{
       
    65 			commandFound = ETrue;
       
    66 			}
       
    67 		
       
    68 		if (commandFound)
       
    69 			{
       
    70 			if (commandIter > argumentStart)
       
    71 			// Command or argument found
       
    72 				{
       
    73 				TInt commandLength = commandIter - argumentStart;
       
    74 				
       
    75 				if (iCurrentCommand == 0)
       
    76 				// This is the main command
       
    77 					{
       
    78 					iCurrentCommand = HBufC::NewL(commandLength);
       
    79 					(*iCurrentCommand) = aCommand.Mid(argumentStart, commandLength);
       
    80 					}
       
    81 				else
       
    82 				// This is a command argument
       
    83 					{
       
    84 					iArgumentList->AppendL(aCommand.Mid(argumentStart, commandLength));
       
    85 					}
       
    86 
       
    87 				// Set the start of the next argument.
       
    88 				argumentStart = commandIter + 1;
       
    89 				}
       
    90 			}
       
    91 
       
    92 		commandIter++;
       
    93 		}
       
    94 
       
    95 	ProcessL();
       
    96 	}
       
    97 
       
    98 TBool CBaseCommandParser::CanParse(const TDesC& aCommand) const
       
    99 	{
       
   100 	TBool canParse = EFalse;
       
   101 
       
   102 	TInt commandCounter = iSupportedCommands->Count();
       
   103 	while ((commandCounter-- > 0) && (!canParse))
       
   104 	// Scan through each of the supported commands
       
   105 		{
       
   106 		TInt commandSize = (*iSupportedCommands)[commandCounter].Length();
       
   107 		if (commandSize <= aCommand.Length())
       
   108 		// The given command must be at least as long as the one it is being compared to
       
   109 			{
       
   110 			if (aCommand.Left(commandSize) == (*iSupportedCommands)[commandCounter])
       
   111 			// If the first character match
       
   112 				{
       
   113 				// Check that there are no more characters or that the next one is a whitespace
       
   114 				if (commandSize < aCommand.Length())
       
   115 					{
       
   116 					if (aCommand[commandSize] == ' ')
       
   117 						{
       
   118 						canParse = ETrue;
       
   119 						}
       
   120 					}
       
   121 				else
       
   122 					{
       
   123 					canParse = ETrue;
       
   124 					}
       
   125 				}
       
   126 			}
       
   127 		}
       
   128 
       
   129 	return canParse;
       
   130 	}
       
   131 
       
   132 CBaseCommandParser::CBaseCommandParser()
       
   133 	{
       
   134 	}
       
   135 
       
   136 void CBaseCommandParser::AddCommandL(const TDesC& aCommand)
       
   137 	{
       
   138 	iSupportedCommands->AppendL(aCommand);
       
   139 	}
       
   140 
       
   141 
       
   142 TDesC& CBaseCommandParser::ErrorL()
       
   143 	{
       
   144 	if (iLastError->Length() == 0)
       
   145 		{
       
   146 		delete iLastError;
       
   147 		iLastError = 0;
       
   148 		iLastError = HBufC::NewL(KTestErrorUnknown().Length());
       
   149 		(*iLastError) = KTestErrorUnknown;
       
   150 		}
       
   151 
       
   152 	return *iLastError;
       
   153 	}
       
   154 
       
   155 void CBaseCommandParser::CheckNumberOfArgumentsL(TInt aNumberOfArguments)
       
   156 	{
       
   157 	if (iArgumentList->Count() != aNumberOfArguments)
       
   158 		{
       
   159 		TBuf<128> errorString;
       
   160 		errorString.Format(KInvalidNumberOfArguments, iDebugInfo.LineNumber(), iArgumentList->Count(), aNumberOfArguments);
       
   161 		SetErrorL(errorString);
       
   162 		User::Leave(KErrArgument);
       
   163 		}
       
   164 	}
       
   165 
       
   166 void CBaseCommandParser::SetErrorL(const TDesC& aError)
       
   167 	{
       
   168 	delete iLastError;
       
   169 	iLastError = 0;
       
   170 	iLastError = HBufC::NewL(aError.Length());
       
   171 	(*iLastError) = aError;
       
   172 	}
       
   173 
       
   174 
       
   175 //
       
   176 //
       
   177 // CBaseSectionParser
       
   178 //
       
   179 
       
   180 void CBaseSectionParser::SetSectionL(const TDesC& aSectionName)
       
   181 	{
       
   182 	delete iSection;
       
   183 	iSection = 0;
       
   184 	iSection = iScript.GetSectionL(aSectionName);
       
   185 	}
       
   186 
       
   187 void CBaseSectionParser::ParseL()
       
   188 	{
       
   189 	// Go through each command in the section and parse it
       
   190 	TBool parsed = EFalse;
       
   191 	TBuf<512> currentCommand;
       
   192 	TBool commandNotRecognised = EFalse;
       
   193 	TInt lineNumber = 0;
       
   194 
       
   195 	while (((lineNumber = iSection->GetCurrentCommand(currentCommand)) > 0) && (!commandNotRecognised))
       
   196 		{
       
   197 		TInt commandParserIndex = iCommandParsers->Count();
       
   198 		parsed = EFalse;
       
   199 		while (commandParserIndex--)
       
   200 			// Look for a command parser that can parse the current command
       
   201 			{
       
   202 			if ((*iCommandParsers)[commandParserIndex]->CanParse(currentCommand))
       
   203 				{
       
   204 				TTestDebugInfo debugInfo(iScript, iSection->SectionPosition(), iSection->CurrentCommandPosition(), lineNumber);
       
   205 				TRAPD(err, (*iCommandParsers)[commandParserIndex]->ParseL(currentCommand, debugInfo));
       
   206 				if (err != KErrNone)
       
   207 					{
       
   208 					TBuf<128> errorString;
       
   209 					errorString.Format(KGeneralParseError, lineNumber, &currentCommand, &(iSection->SectionName()));
       
   210 					LogComment(errorString);
       
   211 					LogComment((*iCommandParsers)[commandParserIndex]->ErrorL());
       
   212 					User::Leave(err);
       
   213 					}
       
   214 				parsed = ETrue;
       
   215 				}
       
   216 			}
       
   217 
       
   218 		if (!parsed)
       
   219 			{
       
   220 			commandNotRecognised = ETrue;
       
   221 			TBuf<256> aError;
       
   222 			aError.Format(KNotParsedError, lineNumber, &currentCommand, &(iSection->SectionName()));
       
   223 			LogComment(aError);
       
   224 			User::Leave(KErrNotFound);
       
   225 			}
       
   226 
       
   227 		iSection->NextCommand();
       
   228 		}
       
   229 	}
       
   230 
       
   231 CBaseSectionParser::~CBaseSectionParser()
       
   232 	{
       
   233 	iCommandParsers->ResetAndDestroy();
       
   234 	delete iCommandParsers;
       
   235 	delete iSection;
       
   236 	}
       
   237 
       
   238 CBaseSectionParser::CBaseSectionParser(CTestScript& aScript) : iScript(aScript)
       
   239 	{
       
   240 	}
       
   241 
       
   242 void CBaseSectionParser::AddCommandParserL(CBaseCommandParser* aParser)
       
   243 	{
       
   244 	TRAPD(err, iCommandParsers->AppendL(aParser));
       
   245 	// If the parser can't be appended then delete it.
       
   246 	// Then leave with the original error.
       
   247 	if (err != KErrNone)
       
   248 		{
       
   249 		delete aParser;
       
   250 		User::Leave(err);
       
   251 		}
       
   252 	}
       
   253 
       
   254 void CBaseSectionParser::ConstructL(const TDesC& aSectionName)
       
   255 	{
       
   256 	iCommandParsers = new (ELeave) CArrayPtrFlat<CBaseCommandParser>(5);
       
   257 	SetSectionL(aSectionName);
       
   258 	}
       
   259