launcher/engine/src/launcherxmlparser.cpp
branchRCL_3
changeset 21 b3cee849fa46
equal deleted inserted replaced
20:48060abbbeaf 21:b3cee849fa46
       
     1 /*
       
     2 * Copyright (c) 2010 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 #include <f32file.h>
       
    19 #include <xml/documentparameters.h>
       
    20 #include "launcherxmlparser.h"
       
    21 #include "launchertraces.h"
       
    22 
       
    23 _LIT8(KXmlMimeType, "text/xml");
       
    24 
       
    25 /**
       
    26  * XML element names
       
    27  */
       
    28 _LIT8(KDll, "dll");
       
    29 _LIT8(KDllName, "dllname");
       
    30 _LIT8(KUID1, "uid1");
       
    31 _LIT8(KUID2, "uid2");
       
    32 _LIT8(KUID3, "uid3");
       
    33 _LIT8(KSID, "sid");
       
    34 _LIT8(KCapability, "capability");
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 
       
    38 CLauncherXMLParser::CLauncherXMLParser(RFs& aFs)
       
    39 :
       
    40 CActive( EPriorityIdle ),
       
    41 iParser(0),
       
    42 iFileSession(aFs),
       
    43 iParsedBytes(0)
       
    44     {
       
    45     CActiveScheduler::Add( this );
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 
       
    50 void CLauncherXMLParser::ConstructL()
       
    51     {
       
    52     LOGSTRING("Launcher: CLauncherXMLParser::ConstructL");
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 
       
    57 CLauncherXMLParser* CLauncherXMLParser::NewL(RFs& aFs)
       
    58     {
       
    59     LOGSTRING("Launcher: CLauncherXMLParser::NewL");
       
    60     CLauncherXMLParser* self = CLauncherXMLParser::NewLC(aFs);    
       
    61     CleanupStack::Pop();
       
    62     return self;
       
    63     }
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 
       
    67 CLauncherXMLParser* CLauncherXMLParser::NewLC(RFs& aFs)
       
    68     {
       
    69     LOGSTRING("Launcher: CLauncherXMLParser::NewLC");
       
    70     CLauncherXMLParser* self = new (ELeave) CLauncherXMLParser(aFs);
       
    71     CleanupStack::PushL(self);
       
    72     self->ConstructL();
       
    73     return self;
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 
       
    78 CLauncherXMLParser::~CLauncherXMLParser()
       
    79     {
       
    80     LOGSTRING("Launcher: CLauncherXMLParser::~CLauncherXMLParser");
       
    81     Cancel();
       
    82     delete iParser;
       
    83     delete iCurrentDllElement;    
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 
       
    88 void CLauncherXMLParser::ClearXMLDataBuffer()
       
    89     {
       
    90     iXMLDataBuffer.Zero();
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 
       
    95 void CLauncherXMLParser::DoCancel()
       
    96     {
       
    97     LOGSTRING("Launcher: CLauncherXMLParser::DoCancel");
       
    98     iIgnoreError = ETrue;
       
    99     if( iParser )
       
   100         {
       
   101         TRAP_IGNORE(iParser->ParseEndL());
       
   102         }
       
   103     iFile.Close();
       
   104     ClearXMLDataBuffer();
       
   105     }
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // Gives current buffer content to parser for processing. Then reads
       
   109 // next part of the file in the buffer and waits for next scheduled run.
       
   110 
       
   111 void CLauncherXMLParser::RunL()
       
   112     {
       
   113     TInt err = iStatus.Int();
       
   114     LOGSTRING2("Launcher: CLauncherXMLParser::RunL - iStatus: %d", err);
       
   115     if( err != KErrNone )
       
   116         {
       
   117         iObserver->DocumentParsedL(err); // Observer should cancel parsing        
       
   118         iFile.Close();
       
   119         ClearXMLDataBuffer();
       
   120         }
       
   121     else
       
   122         {        
       
   123         iParsedBytes += iXMLDataBuffer.Length();
       
   124         iObserver->ParsingProgressedL(iParsedBytes);
       
   125         // Check if the end of the file is reached:
       
   126         if( iXMLDataBuffer.Length() > 0)
       
   127             {
       
   128             // Parse the data in buffer 
       
   129             LOGSTRING("Launcher: CLauncherXMLParser::RunL: Starting XML parsing");
       
   130             if( iParser )
       
   131                 {
       
   132                 iParser->ParseL( iXMLDataBuffer );
       
   133                 }
       
   134             else
       
   135                 {
       
   136                 LOGSTRING("CLauncherXMLParser::RunL - Error: NULL parser");
       
   137                 User::Leave(KErrGeneral);
       
   138                 }
       
   139             // Read new data from XML file to buffer:
       
   140             iFile.Read( iXMLDataBuffer, KXMLBufferSize, iStatus );
       
   141             SetActive();
       
   142             }
       
   143         else
       
   144             {
       
   145             // End of the file reached. Stop parsing and close the file:
       
   146             LOGSTRING("Launcher: CLauncherXMLParser::RunL: Data parsed. Stopping");
       
   147             if( iParser )
       
   148                 {
       
   149                 iParser->ParseEndL();
       
   150                 }
       
   151             iFile.Close();
       
   152             ClearXMLDataBuffer();
       
   153             }    
       
   154         }
       
   155     }
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 
       
   159 void CLauncherXMLParser::ParseL(const TDesC& aFilePath, MLauncherParserObserver* aObserver)
       
   160     {
       
   161     LOGSTRING2("Launcher: CLauncherXMLParser::ParseL: %S", &aFilePath);
       
   162     iIgnoreError = EFalse;
       
   163     iParsedBytes = 0;
       
   164     if ( IsActive() )
       
   165         {
       
   166         Cancel();
       
   167         }
       
   168     
       
   169     if( aObserver == 0 )
       
   170         {
       
   171         LOGSTRING("Launcher: CLauncherXMLParser::ParseL: Error: Observer is a NULL pointer.");
       
   172         User::Leave(KErrArgument);
       
   173         }
       
   174     
       
   175     // Open the XML-file
       
   176     TInt err = iFile.Open( iFileSession, aFilePath, EFileRead );
       
   177     User::LeaveIfError( err );
       
   178     
       
   179     // Create and start XML-parser:
       
   180     delete iParser;
       
   181     iParser = 0;
       
   182     iParser = CParser::NewL(KXmlMimeType, *this);        
       
   183         
       
   184     // Set observer:
       
   185     iObserver = aObserver;
       
   186     
       
   187     // Initialize the buffer and read first part of the XML-file:
       
   188     ClearXMLDataBuffer();
       
   189     iFile.Read( iXMLDataBuffer, KXMLBufferSize, iStatus );
       
   190     SetActive();   
       
   191     iParser->ParseBeginL(); // Reset the parser to xml-filetype
       
   192     }
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 
       
   196 void CLauncherXMLParser::OnStartDocumentL(const Xml::RDocumentParameters& /*aDocParam*/, TInt aErrorCode)
       
   197     {    
       
   198     LOGSTRING2("Launcher: CLauncherXMLParser::OnStartDocumentL (Error code: %d)", aErrorCode);
       
   199     if( aErrorCode != KErrNone )
       
   200         {
       
   201         iObserver->DocumentParsedL(aErrorCode);
       
   202         }
       
   203     }
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 
       
   207 void CLauncherXMLParser::OnEndDocumentL(TInt aErrorCode)
       
   208     {    
       
   209     LOGSTRING2("Launcher: CLauncherXMLParser::OnEndDocumentL (Error code: %d)", aErrorCode);    
       
   210     iObserver->DocumentParsedL(aErrorCode);
       
   211     }
       
   212 
       
   213 // ---------------------------------------------------------------------------
       
   214 
       
   215 void CLauncherXMLParser::OnStartPrefixMappingL( const RString& /*aPrefix*/, 
       
   216                                                 const RString& /*aUri*/, 
       
   217                                                 TInt aErrorCode)
       
   218     {    
       
   219     LOGSTRING2("Launcher: CLauncherXMLParser::OnStartPrefixMappingL (Error code: %d)", aErrorCode);
       
   220     if( aErrorCode != KErrNone )
       
   221         {
       
   222         iObserver->DocumentParsedL(aErrorCode);
       
   223         }
       
   224     }
       
   225 
       
   226 // ---------------------------------------------------------------------------
       
   227 
       
   228 void CLauncherXMLParser::OnEndPrefixMappingL(const RString& /*aPrefix*/, TInt aErrorCode)
       
   229     {
       
   230     LOGSTRING2("Launcer: CLauncherXMLParser::OnEndPrefixMappingL (Error code: %d)", aErrorCode);
       
   231     if( aErrorCode != KErrNone )
       
   232         {
       
   233         iObserver->DocumentParsedL(aErrorCode);
       
   234         }
       
   235     }
       
   236 
       
   237 // ---------------------------------------------------------------------------
       
   238 
       
   239 void CLauncherXMLParser::OnIgnorableWhiteSpaceL(const TDesC8& /*aBytes*/, TInt aErrorCode)
       
   240     {
       
   241     LOGSTRING2("Launcher: CLauncherXMLParser::OnIgnorableWhiteSpaceL (Error code: %d)", aErrorCode);
       
   242     if( aErrorCode != KErrNone )
       
   243         {
       
   244         iObserver->DocumentParsedL(aErrorCode);
       
   245         }
       
   246     }
       
   247 
       
   248 // ---------------------------------------------------------------------------
       
   249 
       
   250 void CLauncherXMLParser::OnSkippedEntityL(const RString& /*aName*/, TInt aErrorCode)
       
   251     {
       
   252     LOGSTRING2("Launcher: CLauncherXMLParser::OnSkippedEntityL (Error code: %d)", aErrorCode);
       
   253     if( aErrorCode != KErrNone )
       
   254         {
       
   255         iObserver->DocumentParsedL(aErrorCode);
       
   256         }
       
   257     }
       
   258 
       
   259 // ---------------------------------------------------------------------------
       
   260 
       
   261 void CLauncherXMLParser::OnProcessingInstructionL(  const TDesC8& /*aTarget*/, 
       
   262                                     const TDesC8& /*aData*/, 
       
   263                                     TInt aErrorCode)
       
   264     {
       
   265     LOGSTRING2("Launcher: CLauncherXMLParser::OnProcessingInstructionL (Error code: %d)", aErrorCode);
       
   266     if( aErrorCode != KErrNone )
       
   267         {
       
   268         iObserver->DocumentParsedL(aErrorCode);
       
   269         }
       
   270     }
       
   271 
       
   272 // ---------------------------------------------------------------------------
       
   273 
       
   274 void CLauncherXMLParser::OnError(TInt aErrorCode)
       
   275     {
       
   276     LOGSTRING2("Launcher: CLauncherXMLParser::OnError: %d", aErrorCode);    
       
   277     if( iIgnoreError == EFalse )
       
   278         {        
       
   279         TRAP_IGNORE(iObserver->DocumentParsedL(aErrorCode));
       
   280         }
       
   281     }
       
   282 
       
   283 // ---------------------------------------------------------------------------
       
   284 
       
   285 TAny* CLauncherXMLParser::GetExtendedInterface(const TInt32 aUid)
       
   286     {
       
   287     (void)aUid;
       
   288     LOGSTRING2("Launcher: CLauncherXMLParser::GetExtendedInterface (UID: %d)", aUid);
       
   289     return 0;
       
   290     }
       
   291 
       
   292 // ---------------------------------------------------------------------------
       
   293 
       
   294 void CLauncherXMLParser::OnStartElementL(   const RTagInfo& aElement,
       
   295                                             const RAttributeArray& /*aAttributes*/,
       
   296                                             TInt aErrorCode)
       
   297     {
       
   298     LOGSTRING2("Launcher: CLauncherXMLParser::OnStartElementL (Error code: %d)", aErrorCode);
       
   299     if( aErrorCode != KErrNone )
       
   300         {
       
   301         iObserver->DocumentParsedL(aErrorCode);
       
   302         }
       
   303     else
       
   304         {
       
   305         // Save XML-element name:
       
   306         iCurrentElementName = aElement.LocalName().DesC();
       
   307     
       
   308         // If this is 'dll'-element, initialize new DLL element instance:
       
   309         if( iCurrentDllElement == 0 && iCurrentElementName == KDll )
       
   310             {
       
   311             iCurrentDllElement = CLauncherDLLElement::NewL();
       
   312             }
       
   313         // Clear contents buffer:
       
   314         iCurrentContent.Zero();
       
   315         }
       
   316     }
       
   317 
       
   318 // ---------------------------------------------------------------------------
       
   319 
       
   320 void CLauncherXMLParser::OnEndElementL(const RTagInfo& aElement, TInt aErrorCode)
       
   321     {   
       
   322     LOGSTRING2("Launcher: CLauncherXMLParser::OnEndElementL (Error code: %d)", aErrorCode);
       
   323     if( aErrorCode != KErrNone )
       
   324         {
       
   325         iObserver->DocumentParsedL(aErrorCode);
       
   326         }
       
   327     else
       
   328         {
       
   329         // Save XML-element's name:        
       
   330         iCurrentElementName = aElement.LocalName().DesC();    
       
   331         TUid tmpUID;
       
   332 
       
   333         if( IsDataElement() )
       
   334             {        
       
   335             // Check that we have a pointer to parent DLL element
       
   336             if( iCurrentDllElement == 0 )
       
   337                 {
       
   338                 LOGSTRING("Launcher: CLauncherXMLParser: Error in parsing xml (parent DLL element missing).");
       
   339                 User::Leave(KErrGeneral);
       
   340                 }
       
   341             // DLL name
       
   342             if( iCurrentElementName == KDllName )
       
   343                 {
       
   344                 TFileName dllName;
       
   345                 dllName.Copy(iCurrentContent);
       
   346                 iCurrentDllElement->SetNameL(dllName);
       
   347                 }
       
   348             // UID1
       
   349             else if( iCurrentElementName == KUID1 )
       
   350                 {
       
   351                 tmpUID.iUid = ConvertDes8ToUint32L(iCurrentContent);
       
   352                 iCurrentDllElement->SetUID1L(tmpUID);
       
   353                 }
       
   354             // UID2
       
   355             else if( iCurrentElementName == KUID2 )
       
   356                 {
       
   357                 tmpUID.iUid = ConvertDes8ToUint32L(iCurrentContent);
       
   358                 iCurrentDllElement->SetUID2L(tmpUID);
       
   359                 }
       
   360             // UID3
       
   361             else if( iCurrentElementName == KUID3 )
       
   362                 {
       
   363                 tmpUID.iUid = ConvertDes8ToUint32L(iCurrentContent);
       
   364                 iCurrentDllElement->SetUID3L(tmpUID);
       
   365                 }
       
   366             // SID
       
   367             else if( iCurrentElementName == KSID )
       
   368                 {
       
   369                 tmpUID.iUid = ConvertDes8ToUint32L(iCurrentContent);
       
   370                 iCurrentDllElement->SetSIDL(tmpUID);
       
   371                 }
       
   372             // Capability
       
   373             else if( iCurrentElementName == KCapability )
       
   374                 {            
       
   375                 iCurrentDllElement->SetCapabilityL(ConvertDes8ToUint32L(iCurrentContent));
       
   376                 }
       
   377             }
       
   378         else if( iCurrentElementName == KDll )
       
   379             {
       
   380             // DLL element parsed, give current DLL object to observer:
       
   381             iObserver->ElementParsedL(*iCurrentDllElement);
       
   382             }
       
   383         }
       
   384     }
       
   385 
       
   386 // ---------------------------------------------------------------------------
       
   387 
       
   388 TBool CLauncherXMLParser::IsDataElement()
       
   389     {
       
   390     if( iCurrentElementName == KDllName ||
       
   391         iCurrentElementName == KUID1 ||
       
   392         iCurrentElementName == KUID2 ||
       
   393         iCurrentElementName == KUID3 ||
       
   394         iCurrentElementName == KSID ||
       
   395         iCurrentElementName == KCapability )
       
   396         {
       
   397         return ETrue;
       
   398         }
       
   399     return EFalse;
       
   400     }
       
   401 
       
   402 // ---------------------------------------------------------------------------
       
   403 // Reads content of an xml-element.
       
   404 
       
   405 void CLauncherXMLParser::OnContentL(const TDesC8& aBytes, TInt aErrorCode)
       
   406     {
       
   407     LOGSTRING2("Launcher: CLauncherXMLParser::OnContentL (Error code: %d)", aErrorCode);
       
   408     if( aErrorCode != KErrNone )
       
   409         {
       
   410         iObserver->DocumentParsedL(aErrorCode);
       
   411         }
       
   412     else if( iCurrentElementName.Length() == 0)
       
   413         {
       
   414         LOGSTRING("Launcher: CLauncherXMLParser: Error in parsing xml (element name missing).");
       
   415         User::Leave(KErrGeneral);
       
   416         }
       
   417     iCurrentContent.Append(aBytes);
       
   418     }
       
   419 
       
   420 // ---------------------------------------------------------------------------
       
   421 
       
   422 TUint32 CLauncherXMLParser::ConvertDes8ToUint32L(const TDesC8& aStr)
       
   423     {
       
   424     LOGSTRING("Launcher: CLauncherXMLParser::ConvertDes8ToUintL");
       
   425     TUint32 uintVal = 0;
       
   426     TLex8 lex(aStr);
       
   427     TInt errorCode=lex.Val(uintVal, EHex);
       
   428     User::LeaveIfError(errorCode);
       
   429     return uintVal;
       
   430     }