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