devicediagnosticsfw/diagframework/src/diagpluginloaderdependencyparser.cpp
branchRCL_3
changeset 26 19bba8228ff0
parent 0 b497e44ab2fc
equal deleted inserted replaced
25:b183ec05bd8c 26:19bba8228ff0
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Parse dependency information from XML in ECOM plug-in
       
    15                  resource files
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // CLASS DECLARATION
       
    21 #include "diagpluginloaderdependencyparser.h"
       
    22 
       
    23 // SYSTEM INCLUDE FILES
       
    24 #include <xml/parser.h>         // CParser
       
    25 #include <badesca.h>
       
    26 #include <e32svr.h>
       
    27 #include <DiagFrameworkDebug.h> // LOGSTRING
       
    28 
       
    29 // USER INCLUDE FILES
       
    30 
       
    31 using namespace Xml;
       
    32 
       
    33 namespace DiagPluginPool
       
    34     {
       
    35 // ============================================================================
       
    36 // LOCAL DATA
       
    37 // ============================================================================
       
    38 
       
    39 _LIT8( KParserMIMEType, "text/xml" );
       
    40 _LIT8( KRootXmlTag, "diagplugininfo" );
       
    41 _LIT8( KOrderNumberTag, "order" );
       
    42 _LIT8( KServiceProvidedTag, "serviceprovided" );
       
    43 _LIT8( KServicesRequiredTag, "servicesrequired" );
       
    44 _LIT8( KServiceTag, "service" );
       
    45 _LIT8( KNameAttrTag, "name" );
       
    46 _LIT8( KNumberAttrTag, "number" );
       
    47 
       
    48 // ============================================================================
       
    49 // MEMBER FUNCTIONS( CDepdendencyParser )
       
    50 // ============================================================================
       
    51 
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // NewL
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 CDependencyParser* CDependencyParser::NewL()
       
    58     {
       
    59     CDependencyParser* self = CDependencyParser::NewLC();
       
    60     CleanupStack::Pop( self );
       
    61     return self;
       
    62     }
       
    63 
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // NewLC
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 CDependencyParser* CDependencyParser::NewLC()
       
    70     {
       
    71     CDependencyParser* self = new( ELeave )CDependencyParser;
       
    72     CleanupStack::PushL( self );
       
    73     self->ConstructL();
       
    74     return self;
       
    75     } 
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // ~CDependencyParser
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 CDependencyParser::~CDependencyParser()
       
    82     {
       
    83     delete iParser;
       
    84     delete iServiceProvided;
       
    85     if ( iServicesRequired )
       
    86         {
       
    87         iServicesRequired->Reset();
       
    88         }
       
    89     delete iServicesRequired;
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // ParseL
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 void CDependencyParser::ParseL( const TDesC8& aOpaqueData, const TDesC8& aDefaultData )
       
    97     {
       
    98     
       
    99     ASSERT( aOpaqueData.Length() );    
       
   100     ASSERT( aDefaultData.Length() );        
       
   101     
       
   102     // Start out by deleting and zeroing old elements
       
   103     if ( iHasBeenRun )
       
   104         {        
       
   105         ResetL();
       
   106         }        
       
   107            
       
   108     iHasBeenRun = ETrue;
       
   109     
       
   110     // Get Parent UID    
       
   111     iParseStatus = ParseToUid( aDefaultData, iParentUid );       
       
   112     
       
   113     // Do not parse opaque data if UID is invalid because plug-in will not be loaded
       
   114     // when data error is detected.        
       
   115     if ( iParseStatus == KErrNone )    
       
   116         {
       
   117         // Parse buffer in one fell swoop            
       
   118         iParser->ParseBeginL();    
       
   119         iParser->ParseL( aOpaqueData );    
       
   120         iParser->ParseEndL();            
       
   121         }            
       
   122     
       
   123     // Check for error from parser
       
   124     User::LeaveIfError( iParseStatus );
       
   125     
       
   126     // Check for error in format
       
   127     if ( iBadXML )
       
   128         {
       
   129         User::Leave( KErrCorrupt );
       
   130         }
       
   131     }
       
   132    
       
   133 // ---------------------------------------------------------------------------
       
   134 // GetOrderNumber
       
   135 // ---------------------------------------------------------------------------
       
   136 //    
       
   137 TInt CDependencyParser::GetOrderNumber() const
       
   138     {
       
   139     return iOrderNumber;
       
   140     }
       
   141     
       
   142 // ---------------------------------------------------------------------------
       
   143 // GetServiceProvided
       
   144 // ---------------------------------------------------------------------------
       
   145 //    
       
   146 HBufC* CDependencyParser::GetServiceProvided()
       
   147     {
       
   148     HBufC* ret = iServiceProvided;
       
   149     iServiceProvided = NULL;
       
   150     return ret;
       
   151     }
       
   152  
       
   153 // ---------------------------------------------------------------------------
       
   154 // GetServicesRequired
       
   155 // ---------------------------------------------------------------------------
       
   156 //   
       
   157 CDesC16ArrayFlat* CDependencyParser::GetServicesRequired()
       
   158     {
       
   159     // Ownership is passed to caller
       
   160     CDesC16ArrayFlat* ret = iServicesRequired;    
       
   161     iServicesRequired = NULL;
       
   162     return ret;
       
   163     }
       
   164     
       
   165 // ---------------------------------------------------------------------------
       
   166 // GetServicesRequired
       
   167 // ---------------------------------------------------------------------------
       
   168 //       
       
   169 const TUid& CDependencyParser::GetParentUid() const
       
   170     {
       
   171     return iParentUid;       
       
   172     }
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // From Xml::MContentHandler
       
   176 // OnStartElementL
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 void CDependencyParser::OnStartElementL(
       
   180     const RTagInfo& aElement,
       
   181     const RAttributeArray& aAttributes,
       
   182     TInt aErrorCode
       
   183     )
       
   184     {
       
   185     const TDesC8& elementName8 = aElement.LocalName().DesC();
       
   186     
       
   187 #ifdef _DEBUG   
       
   188     HBufC* elementName16 = Convert8BitTo16BitBufferLC( elementName8 );
       
   189     LOGSTRING3( "OnStartElementL %S %d\n", elementName16, aErrorCode )
       
   190     CleanupStack::PopAndDestroy( elementName16 );    
       
   191 #endif
       
   192     
       
   193     /*
       
   194     Parse XML tags in the following sample format:
       
   195     
       
   196     <diagplugininfo>
       
   197        <order number="5"/>
       
   198        <serviceprovided name="Browser"/>
       
   199        <servicesrequired>"
       
   200            <service name="SIM"/>
       
   201            <service name="Graphics"/>
       
   202        </servicesrequired>
       
   203    </diagplugininfo>   
       
   204     */        
       
   205     
       
   206     if ( !iInsideRootNode )
       
   207         {
       
   208         // Look for root node
       
   209         if ( ! elementName8.Compare( KRootXmlTag ) )
       
   210             {
       
   211             iInsideRootNode = ETrue;
       
   212             }
       
   213         else
       
   214             {
       
   215             LOGSTRING( "Error: unexpected element looking for root node.\n" )
       
   216             iBadXML = ETrue;
       
   217             }
       
   218         }
       
   219     else // Look for "body" nodes
       
   220         {
       
   221         // Check order number
       
   222         if ( ! elementName8.Compare( KOrderNumberTag ) )
       
   223             {
       
   224             LOGSTRING("Order number\n")
       
   225             if ( ValidateXmlTag( aAttributes, iFoundOrderNumber ))
       
   226                 {
       
   227                 if ( ! ParseNumericAttribute( aAttributes[0], iOrderNumber ) )
       
   228                     {
       
   229                     LOGSTRING2( "Order Number: \'%d\'\n", iOrderNumber )
       
   230                     }
       
   231                 else
       
   232                     {                                       
       
   233                     iBadXML = ETrue;
       
   234                     }
       
   235                 }           
       
   236             }
       
   237         // Check service provided
       
   238         else if ( ! elementName8.Compare( KServiceProvidedTag ) )
       
   239             {
       
   240             LOGSTRING("Service Provided\n")
       
   241             
       
   242             if ( ValidateXmlTag( aAttributes, iFoundServiceProvided ))
       
   243                 {
       
   244                 if ( ! ParseStringAttributeL( aAttributes[0], iServiceProvided ) )
       
   245                                             
       
   246                     {                    
       
   247                     LOGSTRING2( "Service Provided \"%S\"\n", iServiceProvided )
       
   248                     }
       
   249                 else
       
   250                     {                    
       
   251                     iBadXML = ETrue;
       
   252                     }
       
   253                 }              
       
   254             }
       
   255 
       
   256         // Check services required block
       
   257         else if ( ! elementName8.Compare( KServicesRequiredTag ) )
       
   258             {
       
   259             if ( ! iFoundServicesRequired )
       
   260                 {                
       
   261                 iParsingServicesRequired = ETrue;
       
   262                 iFoundServicesRequired = ETrue;
       
   263                 }
       
   264             else
       
   265                 {                
       
   266                 LOGSTRING( "Error: unexpected services required tag found.\n" )
       
   267                 iBadXML = ETrue;
       
   268                 }
       
   269             }
       
   270         
       
   271         // Check service tag
       
   272         else if ( ! elementName8.Compare( KServiceTag ) )
       
   273             {
       
   274             LOGSTRING("Service\n")
       
   275             
       
   276             // OK to have multiple service tags so use dummy as "found" flag
       
   277             TBool Dummy = EFalse;
       
   278             
       
   279             if ( ValidateXmlTag( aAttributes, Dummy ))
       
   280                 {
       
   281                 HBufC* newService = NULL;
       
   282                 
       
   283                 if ( ! ParseStringAttributeL( aAttributes[0],
       
   284                                                 newService ) )
       
   285                     {
       
   286                     LOGSTRING2( "Got service required \"%S\"\n", newService )
       
   287                     CleanupStack::PushL( newService );
       
   288                     iServicesRequired->AppendL( *newService );
       
   289                     CleanupStack::PopAndDestroy( newService );                    
       
   290                     }
       
   291                 else
       
   292                     {
       
   293                     iBadXML = ETrue;
       
   294                     }
       
   295                 }            
       
   296             }
       
   297         // Unexpected tag            
       
   298         else
       
   299             {
       
   300             LOGSTRING( "Error: unexpected XML tag found.\n" )
       
   301             iBadXML = ETrue;
       
   302             }
       
   303         }        
       
   304     }       
       
   305 
       
   306 // ---------------------------------------------------------------------------
       
   307 // From Xml::MContentHandler
       
   308 // OnEndElementL
       
   309 // ---------------------------------------------------------------------------
       
   310 //    
       
   311 void CDependencyParser::OnEndElementL( const Xml::RTagInfo& aElement, TInt aErrorCode )
       
   312     {
       
   313     const TDesC8& elementName8 = aElement.LocalName().DesC();
       
   314     
       
   315  #ifdef _DEBUG
       
   316     HBufC* elementName16 = Convert8BitTo16BitBufferLC( elementName8 );
       
   317     LOGSTRING3( "OnEndElementL %S %d\n", elementName16, aErrorCode )
       
   318     CleanupStack::PopAndDestroy( elementName16 );
       
   319  #endif
       
   320     
       
   321     // Check for end services required block
       
   322     if ( ! elementName8.Compare( KServicesRequiredTag ) )
       
   323         {
       
   324         iParsingServicesRequired = EFalse;
       
   325         }    
       
   326         
       
   327     // Check for end of root block
       
   328     if ( ! elementName8.Compare( KRootXmlTag ) )        
       
   329         {            
       
   330         iInsideRootNode = EFalse;
       
   331         }
       
   332     }
       
   333 
       
   334 // ---------------------------------------------------------------------------
       
   335 // From Xml::MContentHandler
       
   336 // OnContentL
       
   337 // ---------------------------------------------------------------------------
       
   338 //     
       
   339 void CDependencyParser::OnContentL( const TDesC8& /*aBytes*/, TInt aErrorCode )
       
   340     {    
       
   341     LOGSTRING2( "OnContentL %d\n", aErrorCode )
       
   342     }    
       
   343     
       
   344 // ---------------------------------------------------------------------------
       
   345 // From Xml::MContentHandler
       
   346 // OnError
       
   347 // ---------------------------------------------------------------------------
       
   348 //    
       
   349 void CDependencyParser::OnError( TInt aErrorCode )
       
   350     {
       
   351     LOGSTRING2( "OnError %d\n", aErrorCode )
       
   352         
       
   353     iParseStatus = aErrorCode;
       
   354     }  
       
   355     
       
   356 // ============================================================================
       
   357 // PRIVATE MEMBER FUNCTIONS( CDepdendencyParser )
       
   358 // ============================================================================    
       
   359     
       
   360 // ---------------------------------------------------------------------------
       
   361 // Convert8BitTo16BitBufferLC
       
   362 // ---------------------------------------------------------------------------
       
   363 //  
       
   364 HBufC* CDependencyParser::Convert8BitTo16BitBufferLC( const TDesC8& Des ) const
       
   365     {
       
   366     HBufC* buf = HBufC::NewLC( Des.Length() );
       
   367     buf->Des().Copy( Des );
       
   368     return buf;
       
   369     }
       
   370 
       
   371 // ---------------------------------------------------------------------------
       
   372 // ParseNumericAttribute
       
   373 // ---------------------------------------------------------------------------
       
   374 //  
       
   375 TBool CDependencyParser::ParseNumericAttribute( const RAttribute& aAttribute, 
       
   376                                                 TInt& aNumericAttribute )
       
   377     {
       
   378     TBool corrupt = EFalse;
       
   379     const TDesC8& attrName = aAttribute.Attribute().LocalName().DesC();
       
   380     const TDesC8& attrVal = aAttribute.Value().DesC();
       
   381 
       
   382     // Attribute name should be "number"
       
   383     if ( attrName.Compare( KNumberAttrTag ) )
       
   384         {
       
   385         LOGSTRING( "Error: expected number attribute.\n" )
       
   386         corrupt = ETrue;
       
   387         }
       
   388     else
       
   389         {
       
   390         // Parse out order number
       
   391         TLex8 lex( attrVal );
       
   392         TInt rc=lex.Val( aNumericAttribute );
       
   393         if ( rc != KErrNone )
       
   394             {
       
   395             LOGSTRING2( "Error: Could not parse attribute( rc=%d )", rc )
       
   396             corrupt = ETrue;
       
   397             }
       
   398         }
       
   399 
       
   400     return corrupt;
       
   401     }
       
   402 
       
   403 // ---------------------------------------------------------------------------
       
   404 // ParseStringAttributeL
       
   405 // ---------------------------------------------------------------------------
       
   406 //
       
   407 TBool CDependencyParser::ParseStringAttributeL( const RAttribute& aAttribute, 
       
   408                                                 HBufC*& aStringAttribute )
       
   409     {
       
   410     TBool corrupt = EFalse;
       
   411     const TDesC8& attrName = aAttribute.Attribute().LocalName().DesC();
       
   412     const TDesC8& attrVal = aAttribute.Value().DesC();
       
   413 
       
   414     // Attribute name should be "name"
       
   415     if ( attrName.Compare( KNameAttrTag ) )
       
   416         {
       
   417         LOGSTRING( "Error: expected name attribute.\n" )
       
   418         corrupt = ETrue;
       
   419         }
       
   420     else
       
   421         {
       
   422         aStringAttribute = Convert8BitTo16BitBufferLC( attrVal );
       
   423         CleanupStack::Pop( aStringAttribute );
       
   424         }
       
   425 
       
   426     return corrupt;
       
   427     }
       
   428 
       
   429 // ----------------------------------------------------------------------------
       
   430 // ParseToUid
       
   431 // ----------------------------------------------------------------------------
       
   432 //
       
   433 TInt CDependencyParser::ParseToUid( const TDesC8& aSource, TUid& aTarget )
       
   434     {
       
   435     // Remove required "0x" from the descriptor
       
   436     _LIT8( KHexPrefix, "0x" );
       
   437 
       
   438     TPtrC8 pSource( aSource );
       
   439     const TInt prefixPosition = pSource.Find( KHexPrefix );
       
   440     if ( prefixPosition == KErrNotFound )
       
   441         {
       
   442         return KErrCorrupt;            
       
   443         }
       
   444     else
       
   445         {
       
   446         pSource.Set( aSource.Mid( prefixPosition + KHexPrefix().Length() ) );
       
   447         }            
       
   448 
       
   449     // Parse to integer
       
   450     TLex8 lex( pSource );
       
   451     TUint integer = 0;
       
   452 
       
   453     // Parse using TRadix::EHex as radix:
       
   454     const TInt err = lex.Val( integer, EHex );
       
   455     aTarget.iUid = integer;
       
   456    
       
   457     return err;
       
   458     }
       
   459     
       
   460 
       
   461 // ---------------------------------------------------------------------------
       
   462 // ValidateXmlTag
       
   463 // Validate Xml tag following a few standard rules that apply to
       
   464 // all XML tags in this format:
       
   465 // 1) Only one attribute is in each XML tag
       
   466 // 2) Only one of any given tag is allowed
       
   467 // ---------------------------------------------------------------------------
       
   468 //
       
   469 TBool CDependencyParser::ValidateXmlTag( const Xml::RAttributeArray& aAttributes,
       
   470                                          TBool& aFoundTag )
       
   471     {         
       
   472     if ( aFoundTag )
       
   473         {
       
   474         LOGSTRING( "Error: multiple tags found.  Ignoring subsequent values.\n" )        
       
   475         }
       
   476     else 
       
   477         {
       
   478         aFoundTag = ETrue;
       
   479         
       
   480         if ( aAttributes.Count() == 1 )
       
   481             {
       
   482             return ETrue;
       
   483             }
       
   484         else if ( aAttributes.Count() > 1 )
       
   485             {
       
   486             LOGSTRING( "Error: Extra data after attribute tag.\n" )
       
   487             iBadXML = ETrue;
       
   488             }
       
   489         else
       
   490             {
       
   491             LOGSTRING( "Error: No attributes found.\n" )
       
   492             iBadXML = ETrue;
       
   493             }
       
   494         }                
       
   495         
       
   496     return EFalse;        
       
   497     }    
       
   498 
       
   499 // ---------------------------------------------------------------------------
       
   500 // ResetL
       
   501 // ---------------------------------------------------------------------------
       
   502 //
       
   503 void CDependencyParser::ResetL()
       
   504     {
       
   505     delete iServiceProvided;
       
   506     iServiceProvided = NULL;
       
   507             
       
   508     if ( iServicesRequired )
       
   509         {
       
   510         iServicesRequired->Reset();
       
   511         }
       
   512     delete iServicesRequired;
       
   513     iServicesRequired = new( ELeave )CDesC16ArrayFlat( 1 );
       
   514             
       
   515     iOrderNumber = 0;
       
   516     iParentUid = TUid::Uid(0);
       
   517     
       
   518     // Reset all flags
       
   519     iParsingServicesRequired = EFalse;
       
   520     iBadXML = EFalse;
       
   521     iInsideRootNode = EFalse;
       
   522     iFoundOrderNumber = EFalse;
       
   523     iFoundServiceProvided = EFalse;
       
   524     iFoundServicesRequired = EFalse;
       
   525     iParseStatus = 0;
       
   526     }
       
   527         
       
   528 // ============================================================================
       
   529 // PRIVATE CONSTRUCTORS ( CDepdendencyParser )
       
   530 // ============================================================================           
       
   531         
       
   532 // ---------------------------------------------------------------------------
       
   533 // CDependencyParser::CDependencyParser
       
   534 // ---------------------------------------------------------------------------
       
   535 //
       
   536 CDependencyParser::CDependencyParser()
       
   537     {    //lint !e1927 variables not showing up in initializer list
       
   538          // initialized in CBase constructor
       
   539     }    //lint !e1744 member variables possibly not initialized
       
   540     
       
   541 // ---------------------------------------------------------------------------
       
   542 // ConstructL
       
   543 // ---------------------------------------------------------------------------
       
   544 //
       
   545 void CDependencyParser::ConstructL()
       
   546     {        
       
   547     iParser = CParser::NewL( KParserMIMEType, *this );            
       
   548     iServicesRequired = new( ELeave )CDesC16ArrayFlat( 1 );
       
   549     }       
       
   550     
       
   551 
       
   552 // ============================================================================
       
   553 // Unused pure virtual functions from Xml::MContentHandler
       
   554 // ============================================================================
       
   555 
       
   556 void CDependencyParser::OnStartDocumentL( const RDocumentParameters& /* aDocParam */, TInt /* aErrorCode */ )
       
   557     {
       
   558     }
       
   559 
       
   560 void CDependencyParser::OnEndDocumentL( TInt /* aErrorCode */ )
       
   561     {
       
   562     }
       
   563 
       
   564 void CDependencyParser::OnStartPrefixMappingL( const RString& /* aPrefix */, const RString& /* aUri */, TInt /* aErrorCode */ )
       
   565     {
       
   566     }
       
   567 
       
   568 void CDependencyParser::OnEndPrefixMappingL( const RString& /* aPrefix */, TInt /* aErrorCode */ )
       
   569     {
       
   570     }
       
   571 
       
   572 void CDependencyParser::OnIgnorableWhiteSpaceL( const TDesC8& /* aBytes */, TInt /* aErrorCode */ )
       
   573     {
       
   574     }
       
   575 
       
   576 void CDependencyParser::OnSkippedEntityL( const RString& /* aName */, TInt /* aErrorCode */ )
       
   577     {
       
   578     }
       
   579 
       
   580 void CDependencyParser::OnProcessingInstructionL( const TDesC8& /* aTarget */, const TDesC8& /* aData */, TInt /* aErrorCode */ )
       
   581     {
       
   582     }
       
   583 
       
   584 void CDependencyParser::OnExtensionL( const RString& /* aData */, TInt /* aToken */, TInt /* aErrorCode */ )
       
   585     {
       
   586     }
       
   587 
       
   588 TAny* CDependencyParser::GetExtendedInterface( const TInt32 /* aUid */ )
       
   589     {
       
   590     return 0;
       
   591     }
       
   592     
       
   593     } // namespace DiagPluginPool
       
   594 
       
   595 // End of File
       
   596