upnpsharing/upnpgstwrapper/src/upnprenderercfgparser.cpp
branchIOP_Improvements
changeset 40 08b5eae9f9ff
equal deleted inserted replaced
39:6369bfd1b60d 40:08b5eae9f9ff
       
     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:  Implementation of UPnP GStreamer wrapper
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDES
       
    19 #include <xml/parser.h>
       
    20 #include <xml/parserfeature.h>
       
    21 #include <xml/matchdata.h>
       
    22 #include <xml/xmlparsererrors.h>
       
    23 #include <f32file.h>
       
    24 #include <s32file.h>
       
    25 #include <upnpaction.h>
       
    26 #include <upnpstring.h>
       
    27 
       
    28 #include "upnprenderercfg.h"
       
    29 #include "upnprenderercfgparser.h"
       
    30 
       
    31 _LIT( KComponentLogfile, "upnprenderercfgparser.txt");
       
    32 #include "upnplog.h"
       
    33 
       
    34 #define __FUNC_LOG __LOG8_1( "%s", __PRETTY_FUNCTION__ );
       
    35 
       
    36 //tags
       
    37 _LIT8( KTranscoding,             "transcoding" );
       
    38 _LIT8( KPipeline,                "pipeline" );
       
    39 _LIT8( KProtocolInfo,            "protocolInfo" );
       
    40 _LIT8( KSizeMultiplier,          "sizeMultiplier" );
       
    41 _LIT8( KRenderer,                "renderer" );
       
    42 _LIT8( KConfig,                  "config" );
       
    43 
       
    44 //attributes
       
    45 _LIT8( KExactMatch,              "exactMatch" );
       
    46 _LIT8( KModelName,               "modelName" );
       
    47 _LIT8( KSrcMimeType,             "srcMimeType" );
       
    48 
       
    49 _LIT8( KFalse,                    "0" );
       
    50 
       
    51 _LIT8( KXmlMimeType,             "text/xml" );
       
    52 _LIT8( KLIB2XML,                 "libxml2" );
       
    53 
       
    54 const TUint8 KCharSpace = 32; // Space
       
    55 const TUint8 KCharDel = 127;  // DEL
       
    56 
       
    57 using namespace Xml;
       
    58 
       
    59 // --------------------------------------------------------------------------
       
    60 // CUpnpRendererCfgParser::NewL()
       
    61 // (See comments in header file)
       
    62 // --------------------------------------------------------------------------
       
    63 //
       
    64 EXPORT_C CUpnpRendererCfgParser* CUpnpRendererCfgParser::NewL()
       
    65     {
       
    66     __FUNC_LOG;
       
    67     
       
    68     CUpnpRendererCfgParser* self = CUpnpRendererCfgParser::NewLC();
       
    69     CleanupStack::Pop( self );    
       
    70     return self;
       
    71     }    
       
    72 
       
    73 // --------------------------------------------------------------------------
       
    74 // CUpnpRendererCfgParser::NewLC()
       
    75 // (See comments in header file)
       
    76 // --------------------------------------------------------------------------
       
    77 //
       
    78 EXPORT_C CUpnpRendererCfgParser* CUpnpRendererCfgParser::NewLC()
       
    79     {
       
    80     __FUNC_LOG;
       
    81     
       
    82     CUpnpRendererCfgParser* self = new( ELeave ) CUpnpRendererCfgParser();
       
    83     CleanupStack::PushL( self );
       
    84     self->ConstructL();
       
    85     return self;    
       
    86     }
       
    87 
       
    88 // --------------------------------------------------------------------------
       
    89 // CUpnpRendererCfgParser::~CUpnpRendererCfgParser()
       
    90 // (See comments in header file)
       
    91 // --------------------------------------------------------------------------
       
    92 //
       
    93 EXPORT_C CUpnpRendererCfgParser::~CUpnpRendererCfgParser()
       
    94     {   
       
    95     __FUNC_LOG; 
       
    96     
       
    97     delete iParser;
       
    98     delete iMatchData;
       
    99     
       
   100     iFs.Close();
       
   101     }
       
   102     
       
   103 // --------------------------------------------------------------------------
       
   104 // CUpnpRendererCfgParser::CUpnpRendererCfgParser()
       
   105 // (See comments in header file)
       
   106 // --------------------------------------------------------------------------
       
   107 //
       
   108 CUpnpRendererCfgParser::CUpnpRendererCfgParser()
       
   109     : iSizeMultiplier( KErrNotFound )
       
   110     {
       
   111     __FUNC_LOG;
       
   112     // No implementation needed    
       
   113     }
       
   114 
       
   115 // --------------------------------------------------------------------------
       
   116 // CUpnpRendererCfgParser::ConstructL()
       
   117 // (See comments in header file)
       
   118 // --------------------------------------------------------------------------
       
   119 //    
       
   120 void CUpnpRendererCfgParser::ConstructL()
       
   121     {
       
   122     __FUNC_LOG;
       
   123     
       
   124     iMatchData = CMatchData::NewL();
       
   125     iMatchData->SetMimeTypeL( KXmlMimeType ); 
       
   126     iMatchData->SetVariantL( KLIB2XML ); 
       
   127     iParser = CParser::NewL( *iMatchData, *this );    
       
   128     iParser->EnableFeature( ESendFullContentInOneChunk );
       
   129     
       
   130     User::LeaveIfError( iFs.Connect() );
       
   131     }
       
   132 
       
   133 // --------------------------------------------------------------------------
       
   134 // CUpnpRendererCfgParser::ParseRendererCfgL()
       
   135 // (See comments in header file)
       
   136 // --------------------------------------------------------------------------
       
   137 //
       
   138 EXPORT_C void CUpnpRendererCfgParser::ParseRendererCfgL( 
       
   139         CUpnpRendererCfg& aRendereConfig )
       
   140     {     
       
   141     __FUNC_LOG;             
       
   142     
       
   143     if( aRendereConfig.iPipeline || 
       
   144         aRendereConfig.iProtocolInfo || 
       
   145         aRendereConfig.iSizeMultiplier != KErrNotFound )
       
   146         {
       
   147         //these values should be still untouched
       
   148         User::Leave( KErrArgument );
       
   149         }
       
   150     
       
   151     iModelName = aRendereConfig.iModelName;
       
   152     iSrcMimeType = aRendereConfig.iSrcMimeType;
       
   153     
       
   154     //syncronous call -> returns when the file is fully parsed
       
   155     XMLParseL( *aRendereConfig.iConfigFile );
       
   156 
       
   157     if( !iPipeline || !iProtocolInfo || iSizeMultiplier == KErrNotFound )
       
   158         {
       
   159         __LOG8_1( "Couldn't find all the config elements for %S", 
       
   160                 iModelName );
       
   161         
       
   162         //delete allocated bufs and reset vars
       
   163         delete iPipeline; iPipeline = NULL;        
       
   164         delete iProtocolInfo; iProtocolInfo = NULL;                
       
   165         
       
   166         //make sure that everything is in init state
       
   167         Reset();
       
   168         
       
   169         User::Leave( KErrNotFound );
       
   170         }
       
   171             
       
   172     aRendereConfig.iPipeline = iPipeline; //ownership transfer 
       
   173     aRendereConfig.iProtocolInfo = iProtocolInfo; //ownership transfer    
       
   174     aRendereConfig.iSizeMultiplier = iSizeMultiplier;    
       
   175     
       
   176     //make sure that everything is in init state
       
   177     Reset();
       
   178     }
       
   179 
       
   180 // --------------------------------------------------------------------------
       
   181 // CUpnpRendererCfgParser::XMLParseL()
       
   182 // (See comments in header file)
       
   183 // --------------------------------------------------------------------------
       
   184 //
       
   185 void CUpnpRendererCfgParser::XMLParseL( const TDesC& aFile )
       
   186     {
       
   187     __FUNC_LOG;
       
   188    
       
   189     RFile file;
       
   190     User::LeaveIfError( file.Open( iFs, aFile , EFileRead ) );
       
   191     CleanupClosePushL( file );
       
   192     
       
   193     TInt size;
       
   194     User::LeaveIfError( file.Size( size ) );
       
   195     
       
   196     HBufC8* buf = HBufC8::NewLC( size );
       
   197     TPtr8 ptr = buf->Des();
       
   198     User::LeaveIfError( file.Read( ptr ) );
       
   199     
       
   200     Xml::ParseL( *iParser, *buf );
       
   201     
       
   202     CleanupStack::PopAndDestroy( buf );
       
   203     CleanupStack::PopAndDestroy( &file );    
       
   204     }
       
   205 
       
   206 // --------------------------------------------------------------------------
       
   207 // CUpnpRendererCfgParser::Reset()
       
   208 // (See comments in header file)
       
   209 // --------------------------------------------------------------------------
       
   210 //
       
   211 void CUpnpRendererCfgParser::Reset()
       
   212     {
       
   213     __FUNC_LOG;
       
   214         
       
   215     iCurrentElement = EUnknown;
       
   216     iCorrectModel = EFalse;
       
   217     iCorrectTranscoding = EFalse;
       
   218     iModelName = NULL;
       
   219     iSrcMimeType = NULL;
       
   220     iPipeline = NULL;
       
   221     iProtocolInfo = NULL;
       
   222     iSizeMultiplier = KErrNotFound;
       
   223     }
       
   224     
       
   225 // --------------------------------------------------------------------------
       
   226 // CUpnpRendererCfgParser::Element()
       
   227 // (See comments in header file)
       
   228 // --------------------------------------------------------------------------
       
   229 //
       
   230 CUpnpRendererCfgParser::TCfgElements CUpnpRendererCfgParser::Element(  
       
   231         const RTagInfo& aElement )
       
   232     {
       
   233     __FUNC_LOG;
       
   234     
       
   235     TCfgElements element = EUnknown;
       
   236     
       
   237     const TDesC8& desName = aElement.LocalName().DesC();   
       
   238     
       
   239     if( desName.CompareF( KTranscoding ) == 0 )
       
   240         {
       
   241         element = ETranscoding;
       
   242         }
       
   243     else if( desName.CompareF( KPipeline ) == 0 )
       
   244         {
       
   245         element = EPipeline;
       
   246         }
       
   247     else if( desName.CompareF( KProtocolInfo ) == 0 )
       
   248         {
       
   249         element = EProtocolInfo;
       
   250         }
       
   251     else if( desName.CompareF( KSizeMultiplier ) == 0 )
       
   252         {
       
   253         element = ESizeMultiplier;
       
   254         }
       
   255     else if( desName.CompareF( KRenderer ) == 0 )
       
   256         {
       
   257         element = ERenderer;
       
   258         }
       
   259     else if( desName.CompareF( KConfig ) == 0 )
       
   260         {
       
   261         element = EConfig;
       
   262         }
       
   263     else
       
   264        {
       
   265        __LOG8_1( "Error: Unknown element %S", &desName );
       
   266        __ASSERT( EFalse, __FILE__, __LINE__ );
       
   267        }
       
   268        
       
   269     return element;
       
   270     }
       
   271     
       
   272 // --------------------------------------------------------------------------
       
   273 // CUpnpRendererCfgParser::ContainsAttribute()
       
   274 // (See comments in header file)
       
   275 // --------------------------------------------------------------------------
       
   276 //
       
   277 TBool CUpnpRendererCfgParser::ContainsAttribute( 
       
   278         const RAttributeArray& aAttributes, const TDesC8& aAttributeName, 
       
   279         const TDesC8& aAttributeValue, TBool aExactValueMatch )
       
   280     {
       
   281     __FUNC_LOG;
       
   282       
       
   283     TInt attributeCount = aAttributes.Count();
       
   284     for( TInt i = 0; i < attributeCount; i++ )
       
   285         {
       
   286         Xml::RAttribute attr = aAttributes[i];                      
       
   287 
       
   288         if( attr.Attribute().LocalName().DesC() == aAttributeName )
       
   289             {
       
   290             if( aExactValueMatch )
       
   291                 {
       
   292                 if( aAttributeValue.CompareF( attr.Value().DesC() ) == 0 )
       
   293                     {
       
   294                     //requested exact value match found
       
   295                     return ETrue;
       
   296                     }
       
   297                 }
       
   298             else if( aAttributeValue.FindF( attr.Value().DesC() ) >= 0 )
       
   299                 {
       
   300                 //requested non-exact value match (sub-string) found
       
   301                 return ETrue;
       
   302                 }
       
   303             }
       
   304         }
       
   305         
       
   306     return EFalse;
       
   307     }
       
   308 
       
   309 // --------------------------------------------------------------------------
       
   310 // CUpnpRendererCfgParser::OnStartDocumentL()
       
   311 // (See comments in header file)
       
   312 // --------------------------------------------------------------------------
       
   313 //
       
   314 void CUpnpRendererCfgParser::OnStartDocumentL( 
       
   315         const RDocumentParameters& /*aDocParam*/,
       
   316         TInt /*aErrorCode*/ )
       
   317     {    
       
   318     __FUNC_LOG;
       
   319     
       
   320     // No implementation needed
       
   321     }    
       
   322 
       
   323 // --------------------------------------------------------------------------
       
   324 // CUpnpRendererCfgParser::OnEndDocumentL()
       
   325 // (See comments in header file)
       
   326 // --------------------------------------------------------------------------
       
   327 //
       
   328 void CUpnpRendererCfgParser::OnEndDocumentL( TInt /*aErrorCode*/ )
       
   329     {
       
   330     __FUNC_LOG;
       
   331     
       
   332     // No implementation needed
       
   333     }   
       
   334 
       
   335 // --------------------------------------------------------------------------
       
   336 // CUpnpRendererCfgParser::OnStartElementL()
       
   337 // (See comments in header file)
       
   338 // --------------------------------------------------------------------------
       
   339 //
       
   340 void CUpnpRendererCfgParser::OnStartElementL( const RTagInfo& aElement,
       
   341         const RAttributeArray& aAttributes, TInt aErrorCode )
       
   342     {    
       
   343     __FUNC_LOG;
       
   344     __ASSERT( !aErrorCode, __FILE__, __LINE__ );
       
   345         
       
   346     iCurrentElement = Element( aElement );
       
   347     
       
   348     switch( iCurrentElement )
       
   349         {
       
   350         case EConfig:         //fall through
       
   351         case EPipeline:       //fall through
       
   352         case EProtocolInfo:   //fall through
       
   353         case ESizeMultiplier:
       
   354             {
       
   355             break;
       
   356             }
       
   357         case ERenderer:
       
   358             {      
       
   359             TBool exactMatch = ETrue;
       
   360             if( ContainsAttribute( aAttributes, KExactMatch, KFalse, 
       
   361                     ETrue ) )
       
   362                 {
       
   363                 exactMatch = EFalse;
       
   364                 }
       
   365                     
       
   366             if( ContainsAttribute( aAttributes, KModelName, *iModelName, 
       
   367                     exactMatch ) )
       
   368                 {
       
   369                 iCorrectModel = ETrue;
       
   370                 }
       
   371             else
       
   372                 {
       
   373                 iCorrectModel = EFalse;
       
   374                 } 
       
   375             break;
       
   376             }
       
   377         case ETranscoding:
       
   378             {
       
   379             if( iCorrectModel &&
       
   380                 ContainsAttribute( aAttributes, KSrcMimeType, *iSrcMimeType,
       
   381                     ETrue ) )
       
   382                 {
       
   383                 // correct transcoding found 
       
   384                 // -> assert if something is already set
       
   385                 __ASSERT( !iPipeline, __FILE__, __LINE__ );        
       
   386                 __ASSERT( !iProtocolInfo, __FILE__, __LINE__ );     
       
   387                 __ASSERT( iSizeMultiplier == KErrNotFound, __FILE__, 
       
   388                         __LINE__ );     
       
   389                                             
       
   390                 iCorrectTranscoding = ETrue;
       
   391                 }
       
   392             break;
       
   393             }
       
   394         default:
       
   395             {
       
   396             __ASSERT( EFalse, __FILE__, __LINE__ );
       
   397             }
       
   398         }  
       
   399     }
       
   400 
       
   401 // --------------------------------------------------------------------------
       
   402 // CUpnpRendererCfgParser::OnEndElementL()
       
   403 // (See comments in header file)
       
   404 // --------------------------------------------------------------------------
       
   405 //
       
   406 void CUpnpRendererCfgParser::OnEndElementL( const RTagInfo& aElement, 
       
   407         TInt /*aErrorCode*/ )
       
   408     {
       
   409     __FUNC_LOG;
       
   410     
       
   411     if( iCurrentElement == EUnknown )
       
   412         {
       
   413         //at least second OnEndElementL callback in a row -> fetch enum
       
   414         iCurrentElement = Element( aElement );
       
   415         }
       
   416     
       
   417     switch( iCurrentElement )
       
   418         {
       
   419         case EPipeline:     //fall through
       
   420         case EProtocolInfo: //fall through
       
   421         case ESizeMultiplier:      
       
   422             {
       
   423             break;
       
   424             }
       
   425         case EUnknown:      //fall through
       
   426         case EConfig:       //fall through
       
   427         case ERenderer:
       
   428             {
       
   429             iCorrectModel = EFalse;
       
   430             //fall through
       
   431             }
       
   432         case ETranscoding:
       
   433             {
       
   434             iCorrectTranscoding = EFalse;
       
   435             break;
       
   436             }
       
   437         default:
       
   438             {
       
   439             __ASSERT( EFalse, __FILE__, __LINE__ );
       
   440             }            
       
   441         }    
       
   442         
       
   443     //exiting from element -> reset current element
       
   444     iCurrentElement = EUnknown;
       
   445     }
       
   446 
       
   447 // --------------------------------------------------------------------------
       
   448 // CUpnpRendererCfgParser::OnContentL()
       
   449 // (See comments in header file)
       
   450 // --------------------------------------------------------------------------
       
   451 //
       
   452 void CUpnpRendererCfgParser::OnContentL( const TDesC8& aBytes, 
       
   453         TInt aErrorCode )
       
   454     {
       
   455     __FUNC_LOG;
       
   456     __ASSERT( !aErrorCode, __FILE__, __LINE__ );
       
   457     
       
   458     TUint8 firstChar = aBytes[0];    
       
   459     if( firstChar <= KCharSpace || firstChar == KCharDel )
       
   460         {
       
   461         //1st char of the content is a special char -> skip
       
   462         return;
       
   463         }
       
   464     
       
   465     switch( iCurrentElement )
       
   466         {
       
   467         case EConfig:   //fall through
       
   468         case ERenderer: //fall through
       
   469         case ETranscoding:
       
   470             break;
       
   471         case EPipeline:
       
   472             {
       
   473             if( iCorrectTranscoding )
       
   474                 {
       
   475                 //shouldn't contain any parsed value yet
       
   476                 __ASSERT( !iPipeline, __FILE__, __LINE__ );
       
   477                        
       
   478                 iPipeline = HBufC8::NewL( aBytes.Length() );
       
   479                 iPipeline->Des().Copy( aBytes ); 
       
   480                 }
       
   481             break;
       
   482             }
       
   483         case EProtocolInfo:
       
   484             {
       
   485             if( iCorrectTranscoding )
       
   486                 {
       
   487                 //shouldn't contain any parsed value yet
       
   488                 __ASSERT( !iProtocolInfo, __FILE__, __LINE__ );
       
   489                 
       
   490                 iProtocolInfo = HBufC8::NewL( aBytes.Length() );
       
   491                 iProtocolInfo->Des().Copy( aBytes ); 
       
   492                 }
       
   493             break;
       
   494             }
       
   495         case ESizeMultiplier:
       
   496             {
       
   497             if( iCorrectTranscoding )
       
   498                 {
       
   499                 //shouldn't contain any parsed value yet
       
   500                 __ASSERT( iSizeMultiplier == KErrNotFound, __FILE__, 
       
   501                         __LINE__ );
       
   502                 
       
   503                 TLex8 lex;
       
   504                 lex.Assign( aBytes );                
       
   505                 TInt err = lex.Val( iSizeMultiplier );
       
   506                 
       
   507                 __ASSERT( !err, __FILE__, __LINE__ );
       
   508                 }
       
   509             break;
       
   510             }
       
   511         default:
       
   512             {
       
   513             __ASSERT( EFalse, __FILE__, __LINE__ );
       
   514             }            
       
   515         }
       
   516     }
       
   517 
       
   518 // --------------------------------------------------------------------------
       
   519 // CUpnpRendererCfgParser::OnStartPrefixMappingL()
       
   520 // (See comments in header file)
       
   521 // --------------------------------------------------------------------------
       
   522 //
       
   523 void CUpnpRendererCfgParser::OnStartPrefixMappingL( 
       
   524         const RString& /*aPrefix*/, const RString& /*aUri*/, 
       
   525         TInt /*aErrorCode*/ )
       
   526     {
       
   527     __FUNC_LOG;
       
   528     
       
   529     // No implementation needed
       
   530     }
       
   531 // --------------------------------------------------------------------------
       
   532 // CUpnpRendererCfgParser::OnEndPrefixMappingL()
       
   533 // (See comments in header file)
       
   534 // --------------------------------------------------------------------------
       
   535 //
       
   536 void CUpnpRendererCfgParser::OnEndPrefixMappingL( const RString& /*aPrefix*/,
       
   537         TInt /*aErrorCode*/ )
       
   538     {
       
   539     __FUNC_LOG;
       
   540     
       
   541     // No implementation needed
       
   542     }
       
   543 
       
   544 // --------------------------------------------------------------------------
       
   545 // CUpnpRendererCfgParser::OnIgnorableWhiteSpaceL()
       
   546 // (See comments in header file)
       
   547 // --------------------------------------------------------------------------
       
   548 //
       
   549 void CUpnpRendererCfgParser::OnIgnorableWhiteSpaceL( const TDesC8& /*aBytes*/, 
       
   550         TInt /*aErrorCode*/ )
       
   551     {
       
   552     __FUNC_LOG;
       
   553     
       
   554     // No implementation needed
       
   555     }
       
   556 
       
   557 // --------------------------------------------------------------------------
       
   558 // CUpnpRendererCfgParser::OnSkippedEntityL()
       
   559 // (See comments in header file)
       
   560 // --------------------------------------------------------------------------
       
   561 //
       
   562 void CUpnpRendererCfgParser::OnSkippedEntityL( const RString& /*aName*/, 
       
   563         TInt /*aErrorCode*/ )
       
   564     {
       
   565     __FUNC_LOG;
       
   566     
       
   567     // No implementation needed
       
   568     }
       
   569 
       
   570 // --------------------------------------------------------------------------
       
   571 // CUpnpRendererCfgParser::OnProcessingInstructionL()
       
   572 // (See comments in header file)
       
   573 // --------------------------------------------------------------------------
       
   574 //
       
   575 void CUpnpRendererCfgParser::OnProcessingInstructionL( 
       
   576         const TDesC8& /*aTarget*/, 
       
   577         const TDesC8& /*aData*/,
       
   578         TInt /*aErrorCode*/ )
       
   579     {
       
   580     __FUNC_LOG;
       
   581     
       
   582     // No implementation needed
       
   583     } 
       
   584 
       
   585 // --------------------------------------------------------------------------
       
   586 // CUpnpRendererCfgParser::OnError()
       
   587 // (See comments in header file)
       
   588 // --------------------------------------------------------------------------
       
   589 //
       
   590 void CUpnpRendererCfgParser::OnError( TInt /*aErrorCode*/ )
       
   591     {
       
   592     __FUNC_LOG;
       
   593     
       
   594     // No implementation needed
       
   595     }  
       
   596 
       
   597 // --------------------------------------------------------------------------
       
   598 // CUpnpRendererCfgParser::GetExtendedInterface()
       
   599 // (See comments in header file)
       
   600 // --------------------------------------------------------------------------
       
   601 //
       
   602 TAny* CUpnpRendererCfgParser::GetExtendedInterface( const TInt32 /*aUid*/ )
       
   603     {
       
   604     __FUNC_LOG;
       
   605     
       
   606     // No implementation needed
       
   607     return NULL;
       
   608     }
       
   609 
       
   610 
       
   611 // end of file