creator/src/creator_randomdataparser.cpp
branchRCL_3
changeset 22 fad26422216a
parent 0 d6fe6244b863
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
       
     1 /*
       
     2 * Copyright (c) 2008 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <f32file.h>
       
    20 #include <utf.h>
       
    21 #include <xml/documentparameters.h>
       
    22 #include "creator_randomdataparser.h"
       
    23 #include "creator_traces.h"
       
    24 
       
    25 // XML element names
       
    26 _LIT(KRootElement, "creatordata");
       
    27 _LIT(KFirstName, "firstname");
       
    28 _LIT(KSurname, "surname");
       
    29 _LIT(KCompany, "company");
       
    30 _LIT(KAddress, "address");
       
    31 _LIT(KJobTitle, "jobtitle");
       
    32 _LIT(KPhoneNumber, "phonenumber");
       
    33 _LIT(KGroupName, "groupname");
       
    34 _LIT(KMeetingReason, "meetingreason");
       
    35 _LIT(KMeetingPlace, "meetingplace");
       
    36 _LIT(KMemoText, "memo");
       
    37 _LIT(KAnniversaryReason, "anniversary");
       
    38 _LIT(KToDoText, "todo");
       
    39 _LIT(KReminderText, "reminder");
       
    40 _LIT(KMessageSubject, "messagesubject");
       
    41 _LIT(KMessageText, "messagetext");
       
    42 _LIT(KCity, "city");
       
    43 _LIT(KCountry, "country");
       
    44 _LIT(KPostcode, "postcode");
       
    45 _LIT(KState, "state");
       
    46 _LIT(KPobox, "pobox");
       
    47 _LIT(KPrefix, "prefix");
       
    48 _LIT(KSuffix, "suffix");
       
    49 _LIT(KLandmarkName, "landmarkname");
       
    50 _LIT(KLandmarkDescription, "landmarkdescription");
       
    51 
       
    52 using namespace Xml;
       
    53 
       
    54 CCreatorRandomDataParser::CCreatorRandomDataParser() 
       
    55     {        
       
    56     }
       
    57 
       
    58 void CCreatorRandomDataParser::ConstructL()
       
    59     {
       
    60     LOGSTRING("Creator: CCreatorRandomDataParser::ConstructL");
       
    61 	iElementNameArray = new (ELeave) CDesCArrayFlat(32);
       
    62 	iElementNameArray->InsertL(EFirstName, KFirstName);
       
    63 	iElementNameArray->InsertL(ESurname, KSurname);
       
    64 	iElementNameArray->InsertL(ECompany, KCompany);
       
    65 	iElementNameArray->InsertL(EAddress, KAddress);
       
    66 	iElementNameArray->InsertL(EJobTitle, KJobTitle);
       
    67 	iElementNameArray->InsertL(EPhoneNumber, KPhoneNumber);
       
    68 	iElementNameArray->InsertL(EGroupName, KGroupName);
       
    69 	iElementNameArray->InsertL(EMeetingReason, KMeetingReason);
       
    70 	iElementNameArray->InsertL(EMeetingPlace, KMeetingPlace);
       
    71 	iElementNameArray->InsertL(EMemoText, KMemoText);
       
    72 	iElementNameArray->InsertL(EAnniversaryReason, KAnniversaryReason);
       
    73 	iElementNameArray->InsertL(EToDoText, KToDoText);
       
    74 	iElementNameArray->InsertL(EReminderText, KReminderText);
       
    75 	iElementNameArray->InsertL(EMessageSubject, KMessageSubject);
       
    76 	iElementNameArray->InsertL(EMessageText, KMessageText);
       
    77 	iElementNameArray->InsertL(ECity, KCity);
       
    78 	iElementNameArray->InsertL(ECountry, KCountry);
       
    79 	iElementNameArray->InsertL(EPostcode, KPostcode);
       
    80 	iElementNameArray->InsertL(EState, KState);
       
    81 	iElementNameArray->InsertL(EPobox, KPobox);
       
    82 	iElementNameArray->InsertL(EPrefix, KPrefix);
       
    83 	iElementNameArray->InsertL(ESuffix, KSuffix);
       
    84 	iElementNameArray->InsertL(ELandmarkName, KLandmarkName);
       
    85 	iElementNameArray->InsertL(ELandmarkDescription, KLandmarkDescription);
       
    86 	iContentData = HBufC::NewL(16);
       
    87     iResultArray = new (ELeave) CDesCArrayFlat(16);
       
    88     }
       
    89 
       
    90 CCreatorRandomDataParser* CCreatorRandomDataParser::NewL()
       
    91     {
       
    92     LOGSTRING("Creator: CCreatorRandomDataParser::NewL");
       
    93     CCreatorRandomDataParser* self = CCreatorRandomDataParser::NewLC();    
       
    94     CleanupStack::Pop();
       
    95     return self;
       
    96     }
       
    97 
       
    98 CCreatorRandomDataParser* CCreatorRandomDataParser::NewLC()
       
    99     {
       
   100     LOGSTRING("Creator: CCreatorRandomDataParser::NewLC");
       
   101     CCreatorRandomDataParser* self = new (ELeave) CCreatorRandomDataParser;
       
   102     CleanupStack::PushL(self);
       
   103     self->ConstructL();
       
   104     return self;
       
   105     }
       
   106 
       
   107 CCreatorRandomDataParser::~CCreatorRandomDataParser()
       
   108     {
       
   109     delete iParser;
       
   110     delete iResultArray;
       
   111     delete iContentData;
       
   112     delete iElementNameArray;
       
   113     }
       
   114 
       
   115 void CCreatorRandomDataParser::GetTextFileMode(RFile& aFile, TInt& aFileSize) 
       
   116     { 
       
   117     LOGSTRING("Creator: CCreatorRandomDataParser::GetTextFileMode");
       
   118     iScriptTextFormat = EFormatANSIASCII; 
       
   119 
       
   120     // if we are working with text files, check the type first   
       
   121     TBuf8<4> BOM; 
       
   122 
       
   123     // first check for UTF-16 
       
   124     if (aFileSize >= 2 && aFile.Read(0, BOM, 2) == KErrNone) 
       
   125         { 
       
   126         if (BOM.Length()==2 && BOM[0]==0xFF && BOM[1]==0xFE) 
       
   127             { 
       
   128             iScriptTextFormat = EFormatUTF16LE; 
       
   129             aFileSize -= 2; 
       
   130             return; 
       
   131             } 
       
   132         else if (BOM.Length()==2 && BOM[0]==0xFE && BOM[1]==0xFF) 
       
   133             { 
       
   134             iScriptTextFormat = EFormatUTF16BE; 
       
   135             aFileSize -= 2; 
       
   136             return;                 
       
   137             }       
       
   138         }
       
   139         // then check for UTF-8 
       
   140     if (aFileSize >= 3 && aFile.Read(0, BOM, 3) == KErrNone) 
       
   141         { 
       
   142         if (BOM.Length()==3 && BOM[0]==0xEF && BOM[1]==0xBB && BOM[2]==0xBF) 
       
   143             { 
       
   144             iScriptTextFormat = EFormatUTF8; 
       
   145             aFileSize -= 3; 
       
   146             return; 
       
   147             } 
       
   148         }
       
   149         
       
   150     // none of those, seek back to beginning 
       
   151     TInt pos(0); 
       
   152     aFile.Seek(ESeekStart, pos); 
       
   153     }    
       
   154  
       
   155 
       
   156 
       
   157 CDesCArrayFlat* CCreatorRandomDataParser::ParseL(const TDesC& aFileName, const TRandomDataType aDataType)
       
   158     {
       
   159     LOGSTRING2("Creator: CCreatorRandomDataParser::ParseL: %S", &aFileName);
       
   160     
       
   161     iDataType = aDataType;
       
   162     
       
   163     // Create and start XML-parser
       
   164     if( iParser == 0)
       
   165         {
       
   166         _LIT8(KXmlMimeType, "text/xml");
       
   167         iParser = CParser::NewL(KXmlMimeType, *this);        
       
   168         }
       
   169 
       
   170     RFs filesession;
       
   171     CleanupClosePushL(filesession);
       
   172     User::LeaveIfError( filesession.Connect() );
       
   173     
       
   174     iResultArray->Reset();
       
   175     
       
   176     Xml::ParseL(*iParser, filesession, aFileName);
       
   177     
       
   178     CleanupStack::PopAndDestroy(&filesession); // filesession 
       
   179     
       
   180     CDesCArrayFlat* tempArray = new (ELeave) CDesCArrayFlat(8);
       
   181     CleanupStack::PushL(tempArray);
       
   182     for (TInt i = 0; i < iResultArray->MdcaCount(); i++)
       
   183     	{
       
   184     	tempArray->AppendL(iResultArray->MdcaPoint(i));
       
   185     	}
       
   186     CleanupStack::Pop(tempArray);
       
   187     return tempArray;
       
   188     }
       
   189 
       
   190 void CCreatorRandomDataParser::OnStartDocumentL(const Xml::RDocumentParameters& /*aDocParam*/, TInt aErrorCode)
       
   191     {
       
   192     LOGSTRING2("Creator: CCreatorRandomDataParser::OnStartDocumentL (Error code: %d)", aErrorCode);
       
   193     User::LeaveIfError(aErrorCode);    
       
   194     iInsideRootElement = EFalse;
       
   195 	iContentParsingActive = EFalse;
       
   196     }
       
   197 
       
   198 void CCreatorRandomDataParser::OnEndDocumentL(TInt aErrorCode)
       
   199     {
       
   200     LOGSTRING2("Creator: CCreatorRandomDataParser::OnEndDocumentL (Error code: %d)", aErrorCode);
       
   201     User::LeaveIfError(aErrorCode);
       
   202     }
       
   203 
       
   204 void CCreatorRandomDataParser::OnStartPrefixMappingL(const RString& /*aPrefix*/, 
       
   205                                                      const RString& /*aUri*/, 
       
   206                                                      TInt aErrorCode)
       
   207     {
       
   208     LOGSTRING2("Creator: CCreatorRandomDataParser::OnStartPrefixMappingL (Error code: %d)", aErrorCode);
       
   209     User::LeaveIfError(aErrorCode);
       
   210     }
       
   211 
       
   212 void CCreatorRandomDataParser::OnEndPrefixMappingL(const RString& /*aPrefix*/, TInt aErrorCode)
       
   213     {
       
   214     LOGSTRING2("Creator: CCreatorRandomDataParser::OnEndPrefixMappingL (Error code: %d)", aErrorCode);
       
   215     User::LeaveIfError(aErrorCode);
       
   216     }
       
   217 
       
   218 void CCreatorRandomDataParser::OnIgnorableWhiteSpaceL(const TDesC8& /*aBytes*/, TInt aErrorCode)
       
   219     {
       
   220     LOGSTRING2("Creator: CCreatorRandomDataParser::OnIgnorableWhiteSpaceL (Error code: %d)", aErrorCode);
       
   221     User::LeaveIfError(aErrorCode);
       
   222     }
       
   223 
       
   224 void CCreatorRandomDataParser::OnSkippedEntityL(const RString& /*aName*/, TInt aErrorCode)
       
   225     {
       
   226     LOGSTRING2("Creator: CCreatorRandomDataParser::OnSkippedEntityL (Error code: %d)", aErrorCode);
       
   227     User::LeaveIfError(aErrorCode);
       
   228     }
       
   229 
       
   230 void CCreatorRandomDataParser::OnProcessingInstructionL(const TDesC8& /*aTarget*/, 
       
   231                                                         const TDesC8& /*aData*/, 
       
   232                                                         TInt aErrorCode)
       
   233     {
       
   234     LOGSTRING2("Creator: CCreatorRandomDataParser::OnProcessingInstructionL (Error code: %d)", aErrorCode);
       
   235     User::LeaveIfError(aErrorCode);
       
   236     }
       
   237 
       
   238 void CCreatorRandomDataParser::OnError(TInt aErrorCode)
       
   239     {
       
   240     LOGSTRING2("Creator: CCreatorRandomDataParser::OnError: %d", aErrorCode);
       
   241     
       
   242     iLastError = aErrorCode;
       
   243     }
       
   244 
       
   245 TAny* CCreatorRandomDataParser::GetExtendedInterface(const TInt32 aUid)
       
   246     {
       
   247     LOGSTRING2("Creator: CCreatorRandomDataParser::GetExtendedInterface (UID: %d)", aUid);
       
   248     return this;
       
   249     }
       
   250 
       
   251 void CCreatorRandomDataParser::OnStartElementL(const Xml::RTagInfo& aElement,
       
   252                                                const Xml::RAttributeArray& /*aAttributes*/,
       
   253                                                TInt aErrorCode)
       
   254     {
       
   255     LOGSTRING2("Creator: CCreatorRandomDataParser::OnStartElementL (Error code: %d)", aErrorCode);
       
   256     User::LeaveIfError(aErrorCode);
       
   257 
       
   258     const TDesC8& elementName8 = aElement.LocalName().DesC();        
       
   259     HBufC* elementName16 = Convert8BitTo16BitLC(elementName8);
       
   260     if (iInsideRootElement || (elementName16->Des() == KRootElement))
       
   261     	{
       
   262     	iInsideRootElement = ETrue;
       
   263     	}
       
   264     if (iInsideRootElement && (elementName16->Des() == iElementNameArray->MdcaPoint(iDataType)))
       
   265     	{
       
   266         iContentParsingActive = ETrue;
       
   267         iContentData->Des().Zero();
       
   268     	}
       
   269     CleanupStack::PopAndDestroy(elementName16);
       
   270     }
       
   271 
       
   272 void CCreatorRandomDataParser::OnEndElementL(const Xml::RTagInfo& aElement, TInt aErrorCode)
       
   273     {
       
   274     LOGSTRING2("Creator: CCreatorRandomDataParser::OnEndElementL (Error code: %d)", aErrorCode);
       
   275     User::LeaveIfError(aErrorCode);
       
   276 
       
   277     const TDesC8& elementName8 = aElement.LocalName().DesC();
       
   278     HBufC* elementName16 = Convert8BitTo16BitLC(elementName8);
       
   279     if (!iInsideRootElement || (elementName16->Des() == KRootElement))
       
   280     	{
       
   281     	iInsideRootElement = EFalse;
       
   282     	}
       
   283     CleanupStack::PopAndDestroy(elementName16);
       
   284 
       
   285     if (iContentParsingActive)
       
   286 		{
       
   287 	    if (iContentData->Des().Length() > 0)
       
   288 	    	{
       
   289 	    	iResultArray->AppendL(*iContentData);
       
   290 	    	}
       
   291         iContentParsingActive = EFalse;
       
   292 		}
       
   293     }
       
   294 
       
   295 void CCreatorRandomDataParser::OnContentL(const TDesC8& aBytes, TInt aErrorCode)
       
   296     {
       
   297     LOGSTRING2("Creator: CCreatorRandomDataParser::OnContentL (Error code: %d)", aErrorCode);
       
   298     User::LeaveIfError(aErrorCode);
       
   299     
       
   300     if (iContentParsingActive)
       
   301     	{
       
   302         HBufC* data16 = Convert8BitTo16BitLC(aBytes);
       
   303         TInt newLen = iContentData->Des().Length() + data16->Des().Length();
       
   304         iContentData = iContentData->ReAllocL(newLen);
       
   305         iContentData->Des().Append(*data16);
       
   306         CleanupStack::PopAndDestroy(data16);
       
   307     	}
       
   308     }
       
   309 
       
   310 HBufC16* CCreatorRandomDataParser::Convert8BitTo16BitLC(const TDesC8& aInput) 
       
   311     {     
       
   312     LOGSTRING("Creator: CCreatorRandomDataParser::Convert8BitTo16BitLC");
       
   313     HBufC16* output = NULL;
       
   314     
       
   315     output = CnvUtfConverter::ConvertToUnicodeFromUtf8L(aInput);
       
   316     
       
   317     CleanupStack::PushL(output);
       
   318     return output;          
       
   319     } 
       
   320 
       
   321 TInt CCreatorRandomDataParser::GetError() const 
       
   322 	{
       
   323     return iLastError;
       
   324 	}