messagingapp/msgutils/unidatautils/unidatamodel/src/UniSmilUtils.cpp
changeset 25 84d9eb65b26f
equal deleted inserted replaced
23:238255e8b033 25:84d9eb65b26f
       
     1 /*
       
     2 * Copyright (c) 2005 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 *       Static utility class for manipulating SMIL DOM
       
    16 *
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #include <e32std.h>
       
    22 #include <gdi.h>
       
    23 
       
    24 #include <gmxmldocument.h>
       
    25 #include <gmxmlnode.h>
       
    26 #include <gmxmlelement.h>
       
    27 
       
    28 #include <ConformanceChecker.h>
       
    29 #include "UniSmilUtils.h"
       
    30 
       
    31 
       
    32 // ---------------------------------------------------------
       
    33 // UniSmilUtils::StringToIntValue
       
    34 // Convert a descriptor to TInt
       
    35 // ---------------------------------------------------------
       
    36 //
       
    37 EXPORT_C TInt UniSmilUtils::StringToIntValue( const TDesC& aString, 
       
    38                                                     TInt aDefault )
       
    39     {
       
    40     TInt value = aDefault;
       
    41     if( aString.Length() != 0 )
       
    42         {
       
    43         TChar c = aString[0];
       
    44         if( c.IsDigit() )
       
    45             {
       
    46             TLex luthor( aString );
       
    47             luthor.Val( value );
       
    48             }
       
    49         else
       
    50             {
       
    51             value = aDefault;
       
    52             }
       
    53         }
       
    54     return value;
       
    55     }
       
    56 
       
    57 
       
    58 // ---------------------------------------------------------
       
    59 // UniSmilUtils::IsMediaElem
       
    60 // ---------------------------------------------------------
       
    61 
       
    62 EXPORT_C TBool UniSmilUtils::IsMediaElem( CMDXMLNode* aNodePtr )
       
    63 {
       
    64     TBool ret = EFalse;
       
    65 
       
    66     if ( aNodePtr->NodeType() == CMDXMLNode::EElementNode &&
       
    67          UniSmilUtils::MediaElemType( aNodePtr) != EMsgMediaUnknown )
       
    68         {
       
    69         ret = ETrue;
       
    70         }
       
    71 
       
    72     return ret;
       
    73 }
       
    74 
       
    75 // ---------------------------------------------------------
       
    76 // UniSmilUtils::NextNode
       
    77 // ---------------------------------------------------------
       
    78 
       
    79 EXPORT_C CMDXMLNode* UniSmilUtils::NextNode(CMDXMLNode* aRefNode)
       
    80 {
       
    81     if( aRefNode->FirstChild() )
       
    82         {
       
    83         aRefNode = aRefNode->FirstChild();
       
    84         }
       
    85     else
       
    86         {
       
    87         if( aRefNode->NextSibling() )
       
    88             {
       
    89             // move to the next sibling if exists
       
    90             aRefNode = aRefNode->NextSibling();
       
    91             }
       
    92         else
       
    93             {
       
    94             // No siblings at this level. Go up to parent.
       
    95             // might need to go up several layers so WHILE rather than IF
       
    96             while ( aRefNode && !aRefNode->NextSibling() )
       
    97                 {
       
    98                 // no siblings, move back to parent.
       
    99                 aRefNode = aRefNode->ParentNode();
       
   100                 }
       
   101 
       
   102             // Check if we stopped because node has siblings.
       
   103             if ( aRefNode && aRefNode->NextSibling() )
       
   104                 {
       
   105                 aRefNode = aRefNode->NextSibling();
       
   106                 }
       
   107             }
       
   108         }
       
   109 
       
   110     return aRefNode;
       
   111 }
       
   112 
       
   113 // ---------------------------------------------------------
       
   114 // UniSmilUtils::RemoveChildren
       
   115 // ---------------------------------------------------------
       
   116 
       
   117 EXPORT_C void UniSmilUtils::RemoveChildren(
       
   118     CMDXMLNode* aParent )
       
   119     {
       
   120     CMDXMLNode* child = NULL;
       
   121 
       
   122     if ( aParent )
       
   123         {
       
   124         child = aParent->FirstChild();
       
   125         }
       
   126 
       
   127     while ( child )
       
   128         {
       
   129         if ( child->HasChildNodes() )
       
   130             {
       
   131             UniSmilUtils::RemoveChildren( child );
       
   132             }
       
   133         else
       
   134             {
       
   135             CMDXMLNode* n = child;
       
   136             child = child->NextSibling();
       
   137             aParent->RemoveChild( n );
       
   138             delete n;
       
   139             }
       
   140         }
       
   141     }
       
   142 
       
   143 
       
   144 // ---------------------------------------------------------
       
   145 // UniSmilUtils::ElemType
       
   146 // ---------------------------------------------------------
       
   147 
       
   148 EXPORT_C TMsgMediaType UniSmilUtils::MediaElemType(CMDXMLNode* aNodePtr) 
       
   149 {
       
   150     TMsgMediaType type(EMsgMediaUnknown);
       
   151 
       
   152     if ( aNodePtr )
       
   153         {
       
   154         TPtrC nName;
       
   155         nName.Set(aNodePtr->NodeName());
       
   156 
       
   157         if ( nName.Compare(KSMILWREleAnimation) == 0 )
       
   158             {
       
   159             type = EMsgMediaAnimation;
       
   160             }
       
   161         else if (nName.Compare(KSMILWREleAudio) == 0 )
       
   162             {
       
   163             type = EMsgMediaAudio;
       
   164             }
       
   165         // SVG included. No special SVG element tag exist.
       
   166         else if (nName.Compare(KSMILWREleImg) == 0 )
       
   167             {
       
   168             type = EMsgMediaImage;
       
   169             }
       
   170         else if (nName.Compare(KSMILWREleRef) == 0 )
       
   171             {
       
   172             type = EMsgMediaRef;
       
   173             }
       
   174         else if ( nName.Compare(KSMILWREleText) == 0 )
       
   175             {
       
   176             type = EMsgMediaText;
       
   177             }
       
   178         else if ( nName.Compare(KSMILWREleTextstream) == 0 )
       
   179             {
       
   180             type = EMsgMediaTextStream;
       
   181             }
       
   182         else if ( nName.Compare(KSMILWREleVideo) == 0 )
       
   183             {
       
   184             type = EMsgMediaVideo;
       
   185             }
       
   186         }
       
   187 
       
   188     return type;
       
   189 }
       
   190 
       
   191 // ---------------------------------------------------------
       
   192 // UniSmilUtils::GetId
       
   193 // ---------------------------------------------------------
       
   194 
       
   195 EXPORT_C TInt UniSmilUtils::GetId( CMDXMLNode* aNodePtr, TPtrC& aId )
       
   196     {
       
   197     TInt ret = KErrNotSupported;
       
   198 
       
   199     if( aNodePtr && aNodePtr->NodeType() == CMDXMLNode::EElementNode )
       
   200         {
       
   201         CMDXMLElement* e =(CMDXMLElement*) aNodePtr;
       
   202         if ( e->IsAttributeSpecified( KSMILWRParaId ))
       
   203             {
       
   204             e->GetAttribute(KSMILWRParaId, aId);
       
   205             ret = KErrNone;
       
   206             }
       
   207         else
       
   208             {
       
   209             ret = KErrNotFound;
       
   210             }
       
   211         }
       
   212     return ret;
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------
       
   216 // UniSmilUtils::GetMetaTag
       
   217 // ---------------------------------------------------------
       
   218 
       
   219 EXPORT_C TInt UniSmilUtils::GetMetaTag( CMDXMLNode* aNodePtr, 
       
   220                                    TPtrC& aName, 
       
   221                                    TPtrC& aContent )
       
   222     {
       
   223     TInt ret = KErrNotFound;
       
   224 
       
   225     if( aNodePtr &&
       
   226         aNodePtr->NodeType() == CMDXMLNode::EElementNode &&
       
   227         aNodePtr->NodeName().Compare(KSMILWREleMeta) == 0)
       
   228         {
       
   229         CMDXMLElement* e = (CMDXMLElement*) aNodePtr;
       
   230 
       
   231         if ( e->IsAttributeSpecified( KSMILWRParaName ) &&
       
   232              e->IsAttributeSpecified( KSMILWRParaContent ))
       
   233             {
       
   234             if (!e->GetAttribute(KSMILWRParaName, aName) &&
       
   235                 !e->GetAttribute(KSMILWRParaContent, aContent))
       
   236                 {
       
   237                 ret = KErrNone;
       
   238                 }
       
   239             }
       
   240         }
       
   241     return ret;
       
   242     }
       
   243 
       
   244 // ---------------------------------------------------------
       
   245 // UniSmilUtils::RegionId
       
   246 // ---------------------------------------------------------
       
   247 
       
   248 EXPORT_C TInt UniSmilUtils::GetRegionId( CMDXMLNode* aNodePtr, TPtrC& aId )
       
   249     {
       
   250     TInt ret = KErrNotSupported;
       
   251 
       
   252     if ( aNodePtr )
       
   253         {
       
   254         if ( UniSmilUtils::IsMediaElem( aNodePtr ) )
       
   255             {
       
   256             ret = ((CMDXMLElement*)aNodePtr)->GetAttribute(KSMILWRParaRegion, aId);
       
   257             }
       
   258         }
       
   259     return ret;
       
   260     }
       
   261 
       
   262 // ---------------------------------------------------------
       
   263 // UniSmilUtils::GetSrc
       
   264 // ---------------------------------------------------------
       
   265 
       
   266 EXPORT_C TPtrC UniSmilUtils::GetSrcL( CMDXMLNode* aNodePtr )
       
   267 {
       
   268     TPtrC src = TPtrC();
       
   269     if ( aNodePtr )
       
   270             {
       
   271         CMDXMLElement* e = (CMDXMLElement*) aNodePtr;
       
   272 
       
   273         if ( e && e->IsAttributeSpecified(KSMILWRParaSrc) )
       
   274             {
       
   275             User::LeaveIfError( e->GetAttribute(KSMILWRParaSrc, src) );
       
   276             }
       
   277         else
       
   278             {
       
   279             // Ok not to have src i.e. no default content.
       
   280             }
       
   281         }
       
   282 
       
   283     return src;
       
   284 }
       
   285 
       
   286 
       
   287 // ---------------------------------------------------------
       
   288 // UniSmilUtils::GetSmilTypeL
       
   289 //
       
   290 // ---------------------------------------------------------
       
   291 EXPORT_C TUniSmilType UniSmilUtils::GetSmilTypeL( CMDXMLDocument* aDom )
       
   292     {
       
   293 	if (!aDom)
       
   294 		{
       
   295 		return ENoSmil;
       
   296 		}
       
   297 
       
   298     if ( UniSmilUtils::IsTemplateSmil( aDom ) )
       
   299         {
       
   300         return ETemplateSmil;
       
   301         }
       
   302     else if ( !aDom->DocumentElement() ||
       
   303               !aDom->DocumentElement()->FirstChild() )
       
   304         {
       
   305         return ENoSmil;
       
   306         }
       
   307     else
       
   308         {
       
   309         CConformanceChecker* checker = CConformanceChecker::NewL();
       
   310         CleanupStack::PushL( checker );
       
   311         TBool isConf( EFalse );
       
   312 
       
   313         isConf = checker->Check( aDom, EMmsSmil_v2_0,
       
   314             EAllowVideoTag |
       
   315             EAllowNonMilliseconds |
       
   316             EAllowAllAttributes |
       
   317             EAllowMixedRegionDimensions |
       
   318             EAllowAnyRegionNames );
       
   319 
       
   320         CleanupStack::PopAndDestroy( checker );
       
   321 
       
   322         if( isConf )
       
   323             {
       
   324             return EMmsSmil;
       
   325             }
       
   326 
       
   327         }
       
   328     return E3GPPSmil;
       
   329     }
       
   330 
       
   331 
       
   332 // ---------------------------------------------------------
       
   333 // UniSmilUtils::IsTemplateSmil
       
   334 //
       
   335 // ---------------------------------------------------------
       
   336 
       
   337 EXPORT_C TBool UniSmilUtils::IsTemplateSmil( CMDXMLDocument* aDom )
       
   338     {
       
   339     TBool ret( EFalse );
       
   340 
       
   341     CMDXMLNode* n = aDom->DocumentElement()->FirstChild();
       
   342 
       
   343     while ( n )
       
   344         {
       
   345         if ( n->NodeName().Compare( KSMILWREleMeta ) == 0)
       
   346             {
       
   347             TPtrC v1, v2;
       
   348             if ( !UniSmilUtils::GetMetaTag( n, v1, v2 ) )
       
   349                 {
       
   350                 if ( v1.Compare( KSMILMetaSmilProfile ) == 0 &&
       
   351                      v2.Compare( KSMILMetaSmilProfileValue ) == 0)
       
   352                     {
       
   353                     ret = ETrue;
       
   354                     break;
       
   355                     }
       
   356                 }
       
   357             }
       
   358         else if ( n->NodeName().Compare( KSMILWREleBody ) == 0 )
       
   359             {
       
   360             // Cannot anymore have metatags
       
   361             break;
       
   362             }
       
   363         n = UniSmilUtils::NextNode( n );
       
   364         }
       
   365     return ret;
       
   366     }
       
   367 
       
   368 
       
   369 // EOF