ncdengine/provider/server/src/ncdcontentdescriptor.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     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:   Implementation of CNcdContentDescriptor
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "ncdcontentdescriptor.h"
       
    20 #include "catalogsconstants.h"
       
    21 #include "catalogsutils.h"
       
    22 
       
    23 // ---------------------------------------------------------------------------
       
    24 // 
       
    25 // ---------------------------------------------------------------------------
       
    26 //    
       
    27 CNcdContentDescriptor* CNcdContentDescriptor::NewL()
       
    28     {
       
    29     CNcdContentDescriptor* self = NewLC();
       
    30     CleanupStack::Pop();
       
    31 
       
    32     return self;
       
    33     }
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // 
       
    37 // ---------------------------------------------------------------------------
       
    38 //    
       
    39 CNcdContentDescriptor* CNcdContentDescriptor::NewLC()
       
    40     {
       
    41     CNcdContentDescriptor* self = new (ELeave) CNcdContentDescriptor();
       
    42     CleanupStack::PushL( self );
       
    43     self->ConstructL();
       
    44 
       
    45     return self;
       
    46     }
       
    47             
       
    48 // ---------------------------------------------------------------------------
       
    49 // 
       
    50 // ---------------------------------------------------------------------------
       
    51 //    
       
    52 void CNcdContentDescriptor::ConstructL()
       
    53     {
       
    54     iDescriptorType = KNullDesC().AllocL();
       
    55     iDescriptor = KNullDesC().AllocL();
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // 
       
    60 // ---------------------------------------------------------------------------
       
    61 //    
       
    62 CNcdContentDescriptor::~CNcdContentDescriptor()
       
    63     {
       
    64     ClearAllData();
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // 
       
    69 // ---------------------------------------------------------------------------
       
    70 //    
       
    71 void CNcdContentDescriptor::SetDescriptorL( const TDesC& aDescriptorType, 
       
    72                                             const TDesC16& aDescriptor )
       
    73     {
       
    74     DLTRACEIN((""));
       
    75     ClearAllData();
       
    76     
       
    77     iDescriptorType = aDescriptorType.AllocL();
       
    78     iDescriptor = aDescriptor.AllocL();
       
    79     }
       
    80 
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // 
       
    84 // ---------------------------------------------------------------------------
       
    85 //    
       
    86 void CNcdContentDescriptor::SetDescriptorL( const TDesC& aDescriptorType, 
       
    87                                             const TDesC8& aDescriptor )
       
    88     {    
       
    89     DLTRACEIN((""));
       
    90     ClearAllData();
       
    91     
       
    92     iDescriptorType = aDescriptorType.AllocL();    
       
    93     iDescriptor = ConvertUtf8ToUnicodeL( aDescriptor );
       
    94     }
       
    95 
       
    96 
       
    97 // ---------------------------------------------------------------------------
       
    98 // 
       
    99 // ---------------------------------------------------------------------------
       
   100 //    
       
   101 HBufC* CNcdContentDescriptor::ExtractDataL( const TDesC& aStartTag, 
       
   102                                          const TDesC& aEndTag,
       
   103                                          const TDesC& aPattern )
       
   104     {
       
   105     TInt beginningOfData = 0;
       
   106     TInt dataLength = 0;
       
   107     HBufC* extractedData = NULL;
       
   108 
       
   109     if ( aEndTag.Length() > 0 )
       
   110         {
       
   111         // End tag has been given
       
   112 
       
   113         // Find start tag
       
   114         beginningOfData = iDescriptor->FindF( aStartTag );
       
   115         User::LeaveIfError( beginningOfData );
       
   116 
       
   117         // Get beginning of the data
       
   118         beginningOfData += aStartTag.Length();
       
   119 
       
   120         // Find end tag
       
   121         dataLength = iDescriptor->FindF( aEndTag );
       
   122         User::LeaveIfError( dataLength );
       
   123         // Check validity of end tag position
       
   124         if ( dataLength < beginningOfData )
       
   125             {
       
   126             User::Leave( KErrCorrupt );
       
   127             }
       
   128         // Set data length
       
   129         dataLength -= beginningOfData;
       
   130 
       
   131         // Get data
       
   132         extractedData = iDescriptor->Mid( beginningOfData, 
       
   133             dataLength ).AllocLC();
       
   134         
       
   135         // Check if pattern is given
       
   136         if ( aPattern != KNullDesC )
       
   137             {
       
   138             // If pattern matches extracted mime type, try extract next tag
       
   139             if ( extractedData->MatchF( aPattern ) != KErrNotFound )
       
   140                 {       
       
   141                 // Get the end of end tag
       
   142                 TInt endOfEndTag = beginningOfData + dataLength + 
       
   143                     aEndTag.Length();
       
   144                     
       
   145                 // Get the rest of iDescriptor to find the next tag
       
   146                 TPtrC16 restOfDescriptor = iDescriptor->Mid( endOfEndTag );
       
   147                         
       
   148                 // Find start tag                       
       
   149                 beginningOfData = restOfDescriptor.FindF( aStartTag );
       
   150                         
       
   151                 if ( beginningOfData != KErrNotFound )
       
   152                     {       
       
   153                     User::LeaveIfError( beginningOfData );
       
   154 
       
   155                     // Get beginning of the data
       
   156                     beginningOfData += aStartTag.Length();
       
   157 
       
   158                     // Find end tag
       
   159                     dataLength = restOfDescriptor.FindF( aEndTag );
       
   160                     User::LeaveIfError( dataLength );
       
   161                     // Check validity of end tag position
       
   162                     if ( dataLength < beginningOfData )
       
   163                         {
       
   164                         User::Leave( KErrCorrupt );
       
   165                         }
       
   166                     // Set data length
       
   167                     dataLength -= beginningOfData;
       
   168 
       
   169                     CleanupStack::PopAndDestroy( extractedData );
       
   170                                         
       
   171                     // Get data
       
   172                     extractedData = restOfDescriptor.Mid( 
       
   173                         beginningOfData, dataLength ).AllocLC();
       
   174                     }
       
   175                 }
       
   176             }
       
   177         }
       
   178     else
       
   179         {
       
   180         // End tag was not given
       
   181 
       
   182         // Find start tag
       
   183         beginningOfData = iDescriptor->FindF( aStartTag );
       
   184         User::LeaveIfError( beginningOfData );
       
   185 
       
   186         // Get beginning of the data
       
   187         beginningOfData += aStartTag.Length();
       
   188 
       
   189         const TDesC& descriptor = iDescriptor->Des();
       
   190 
       
   191         while ( 1 )
       
   192             {
       
   193             // Find end of the line
       
   194             while ( descriptor[beginningOfData + dataLength] != 0x0A &&
       
   195                    descriptor[beginningOfData + dataLength] != 0x0D &&
       
   196                    descriptor[beginningOfData + dataLength] != 0x1A )
       
   197                 {
       
   198                 dataLength++;
       
   199                 // If end of the descriptor is reached,
       
   200                 // use that as end of the line
       
   201                 if ( beginningOfData + dataLength == iDescriptor->Length() )
       
   202                     {
       
   203                     break;
       
   204                     }
       
   205                 }
       
   206 
       
   207             // Check if we have some data extracted already
       
   208             if ( extractedData == NULL )
       
   209                 {
       
   210                 // No previously extracted data found.
       
   211                 // Create new heap.
       
   212                 extractedData = iDescriptor->Mid( beginningOfData, 
       
   213                     dataLength ).AllocLC();
       
   214                 }
       
   215             else
       
   216                 {
       
   217                 // Some previously extracted data found.
       
   218                 // Realloc new heap and append new data into it.
       
   219                 HBufC* newBuffer = extractedData->ReAllocL( 
       
   220                     extractedData->Length() + dataLength );
       
   221                 CleanupStack::Pop( extractedData );
       
   222                 extractedData = newBuffer;
       
   223                 CleanupStack::PushL( extractedData );
       
   224                 extractedData->Des().Append( 
       
   225                     iDescriptor->Mid( beginningOfData, dataLength ) );
       
   226                 }
       
   227 
       
   228             // If we are at the end of the file, we are done here
       
   229             if ( beginningOfData + dataLength == iDescriptor->Length() )
       
   230                 {
       
   231                 break;
       
   232                 }
       
   233 
       
   234             // Find start of the next line
       
   235             while ( descriptor[beginningOfData + dataLength] == 0x0A ||
       
   236                    descriptor[beginningOfData + dataLength] == 0x0D ||
       
   237                    descriptor[beginningOfData + dataLength] == 0x1A )
       
   238                 {
       
   239                 dataLength++;
       
   240                 if ( beginningOfData + dataLength == iDescriptor->Length() )
       
   241                     {
       
   242                     // End of the descriptor reached
       
   243                     break;
       
   244                     }
       
   245                 }
       
   246 
       
   247             // Check if data continues on the next line
       
   248             if ( beginningOfData + dataLength == iDescriptor->Length() ||
       
   249                 descriptor[beginningOfData + dataLength] != 0x20 )
       
   250                 {
       
   251                 // Data doesn't continue on the next line
       
   252                 break;
       
   253                 }
       
   254 
       
   255             // Data continues on the next line.
       
   256             // Set beginning of the data.
       
   257             beginningOfData += dataLength + 1;
       
   258             dataLength = 0;
       
   259             }
       
   260         }
       
   261 
       
   262     CleanupStack::Pop( extractedData );
       
   263     return extractedData;
       
   264     }
       
   265 
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 // 
       
   269 // ---------------------------------------------------------------------------
       
   270 //    
       
   271 const TDesC& CNcdContentDescriptor::DataUriL()
       
   272     {
       
   273     DLTRACEIN((""));
       
   274     _LIT( KObjectUriStartTag, "<objecturi>" );
       
   275     _LIT( KObjectUriEndTag, "</objecturi>" );
       
   276 
       
   277     _LIT( KMidletJarUriStartTag, "midlet-jar-url: " );
       
   278 
       
   279     // Dont parse data URI again, if it has been already parsed
       
   280     if ( iDataUri != NULL )
       
   281         {
       
   282         DLTRACEOUT(( _L("DataUri already parsed: %S"), iDataUri ));
       
   283         return *iDataUri;
       
   284         }
       
   285 
       
   286     if ( iDescriptorType->CompareF( KDescriptorTypeOdd ) == 0 )
       
   287         {
       
   288         // Descriptor type is DD
       
   289 
       
   290         TRAPD( err, 
       
   291                iDataUri = ExtractDataL( KObjectUriStartTag, 
       
   292                                         KObjectUriEndTag ) );
       
   293         if ( err != KErrNone )
       
   294             {
       
   295             return KNullDesC;
       
   296             }
       
   297 
       
   298         // Check validity of the URI
       
   299         if ( iDataUri->MatchF( KHttpMatchString ) == 0 )
       
   300             {
       
   301             return *iDataUri;
       
   302             }
       
   303         else
       
   304             {
       
   305             // Data URI is invalid, clear data URI
       
   306             delete iDataUri;
       
   307             iDataUri = NULL;
       
   308             iDataUri = KNullDesC().AllocL();
       
   309             }
       
   310         }
       
   311     else if ( iDescriptorType->CompareF( KDescriptorTypeJad ) == 0 )
       
   312         {
       
   313         // Descriptor type is JAD
       
   314 
       
   315         TRAPD( err, 
       
   316                iDataUri = ExtractDataL( KMidletJarUriStartTag, 
       
   317                                         KNullDesC ) );
       
   318         if ( err != KErrNone )
       
   319             {
       
   320             return KNullDesC;
       
   321             }
       
   322 
       
   323         // Check validity of the URI
       
   324         if ( iDataUri->MatchF( KHttpMatchString ) == 0 )
       
   325             {
       
   326             return *iDataUri;
       
   327             }
       
   328         else
       
   329             {
       
   330             // Data URI is invalid, clear data URI
       
   331             delete iDataUri;
       
   332             iDataUri = NULL;
       
   333             iDataUri = KNullDesC().AllocL();
       
   334             }
       
   335         }
       
   336     
       
   337     DLTRACEOUT(("Not JAD nor DD"));
       
   338     return KNullDesC;
       
   339     }
       
   340 
       
   341 
       
   342 // ---------------------------------------------------------------------------
       
   343 // 
       
   344 // ---------------------------------------------------------------------------
       
   345 //    
       
   346 const TDesC& CNcdContentDescriptor::InstallNotificationUri()
       
   347     {
       
   348     DLTRACEIN((""));
       
   349     _LIT( KInstNotifyUriStartTag, "<installnotifyuri>" );
       
   350     _LIT( KInstNotifyUriEndTag, "</installnotifyuri>" );
       
   351 
       
   352     _LIT( KMidletInstNotifyStartTag, "midlet-install-notify: " );
       
   353 
       
   354     // Dont parse install notification URI again, if it has been already parsed
       
   355     if ( iInstallNotificationUri )
       
   356         {
       
   357         DLTRACEOUT(( _L("Install notification URI: %S"), 
       
   358             iInstallNotificationUri ));
       
   359         return *iInstallNotificationUri;
       
   360         }
       
   361 
       
   362     if ( iDescriptorType->CompareF( KDescriptorTypeOdd ) == 0 )
       
   363         {
       
   364         // Descriptor type is DD
       
   365 
       
   366         TRAPD( err, 
       
   367                iInstallNotificationUri = ExtractDataL( KInstNotifyUriStartTag,
       
   368                                                        KInstNotifyUriEndTag ) );
       
   369         if ( err != KErrNone )
       
   370             {
       
   371             return KNullDesC;
       
   372             }
       
   373         DLTRACEOUT(( _L("Install notification URI: %S"), iInstallNotificationUri ));
       
   374         return *iInstallNotificationUri;
       
   375         }
       
   376     else if ( iDescriptorType->CompareF( KDescriptorTypeJad ) == 0 )
       
   377         {
       
   378         // Descriptor type is JAD
       
   379 
       
   380         TRAPD( err, 
       
   381                iInstallNotificationUri = ExtractDataL( KMidletInstNotifyStartTag, 
       
   382                                                        KNullDesC ) );
       
   383         if ( err != KErrNone )
       
   384             {
       
   385             return KNullDesC;
       
   386             }
       
   387         DLTRACEOUT(( _L("Install notification URI: %S"), 
       
   388             iInstallNotificationUri ));
       
   389         return *iInstallNotificationUri;
       
   390         }
       
   391 
       
   392     return KNullDesC;
       
   393     }
       
   394 
       
   395 
       
   396 // ---------------------------------------------------------------------------
       
   397 // 
       
   398 // ---------------------------------------------------------------------------
       
   399 //    
       
   400 const TDesC& CNcdContentDescriptor::FileName()
       
   401     {
       
   402     DLTRACEIN((""));
       
   403     _LIT( KNameStartTag, "<name>" );
       
   404     _LIT( KNameEndTag, "</name>" );
       
   405 
       
   406     _LIT( KMidletNameStartTag, "midlet-name: " );
       
   407 
       
   408     // Dont parse file name again, if it has been already parsed
       
   409     if ( iFileName != NULL )
       
   410         {
       
   411         return *iFileName;
       
   412         }
       
   413 
       
   414     if ( iDescriptorType->CompareF( KDescriptorTypeOdd ) == 0 )
       
   415         {
       
   416         // Descriptor type is DD
       
   417 
       
   418         TRAPD( err, iFileName = ExtractDataL( KNameStartTag, KNameEndTag ) );
       
   419         if ( err != KErrNone )
       
   420             {
       
   421             return KNullDesC;
       
   422             }
       
   423         return *iFileName;
       
   424         }
       
   425     else if ( iDescriptorType->CompareF( KDescriptorTypeJad ) == 0 )
       
   426         {
       
   427         // Descriptor type is JAD
       
   428 
       
   429         TRAPD( err, 
       
   430                iFileName = ExtractDataL( KMidletNameStartTag, KNullDesC ) );
       
   431         if ( err != KErrNone )
       
   432             {
       
   433             return KNullDesC;
       
   434             }
       
   435         return *iFileName;
       
   436         }
       
   437 
       
   438     return KNullDesC;
       
   439     }
       
   440 
       
   441 // ---------------------------------------------------------------------------
       
   442 // 
       
   443 // ---------------------------------------------------------------------------
       
   444 //    
       
   445 const TDesC& CNcdContentDescriptor::MimeType()
       
   446     {
       
   447     DLTRACEIN((""));
       
   448     _LIT( KTypeStartTag, "<type>" );
       
   449     _LIT( KTypeEndTag, "</type>" );
       
   450     _LIT( KMimeTypeDrmMessage, "application/vnd.oma.drm.message" );
       
   451 
       
   452     // Dont parse mime type again, if it has been already parsed
       
   453     if ( iMimeType != NULL )
       
   454         {
       
   455         return *iMimeType;
       
   456         }
       
   457 
       
   458     if ( iDescriptorType->CompareF( KDescriptorTypeOdd ) == 0 )
       
   459         {
       
   460         // Descriptor type is DD
       
   461 
       
   462         TRAPD( err, 
       
   463                iMimeType = ExtractDataL( KTypeStartTag, KTypeEndTag, 
       
   464                 KMimeTypeDrmMessage ) 
       
   465             );
       
   466         if ( err != KErrNone  )
       
   467             {
       
   468             return KNullDesC;
       
   469             }
       
   470 
       
   471         return *iMimeType;
       
   472         }
       
   473 
       
   474     return KNullDesC;
       
   475     }
       
   476 
       
   477 // ---------------------------------------------------------------------------
       
   478 // 
       
   479 // ---------------------------------------------------------------------------
       
   480 //    
       
   481 const TDesC& CNcdContentDescriptor::Descriptor() const
       
   482     {
       
   483     return *iDescriptor;
       
   484     }
       
   485 
       
   486 // ---------------------------------------------------------------------------
       
   487 // 
       
   488 // ---------------------------------------------------------------------------
       
   489 //    
       
   490 const TDesC& CNcdContentDescriptor::DescriptorType() const
       
   491     {
       
   492     return *iDescriptorType;
       
   493     }
       
   494 
       
   495 
       
   496 // ---------------------------------------------------------------------------
       
   497 // Clears all member variables
       
   498 // ---------------------------------------------------------------------------
       
   499 //    
       
   500 void CNcdContentDescriptor::ClearAllData()
       
   501     {
       
   502     delete iDescriptorType;
       
   503     iDescriptorType = NULL;
       
   504     delete iDescriptor;
       
   505     iDescriptor = NULL;
       
   506 
       
   507     delete iDataUri;
       
   508     iDataUri = NULL;
       
   509     
       
   510     delete iInstallNotificationUri;
       
   511     iInstallNotificationUri = NULL;
       
   512     
       
   513     delete iFileName;
       
   514     iFileName = NULL;
       
   515     
       
   516     delete iMimeType;
       
   517     iMimeType = NULL;    
       
   518     }
       
   519 
       
   520