menufw/hierarchynavigator/hnmetadatamodel/src/hnconditionfactory.cpp
changeset 0 f72a12da539e
equal deleted inserted replaced
-1:000000000000 0:f72a12da539e
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "hnconditionfactory.h"
       
    20 #include "hnsimplecondition.h"
       
    21 #include "hncomplexcondition.h"
       
    22 #include "hnglobals.h"
       
    23 
       
    24 // ======== MEMBER FUNCTIONS ========
       
    25 
       
    26 // ---------------------------------------------------------------------------
       
    27 //
       
    28 // ---------------------------------------------------------------------------
       
    29 //
       
    30 CHnConditionInterface* HnConditionFactory::NewL( TXmlEngAttr aElement )
       
    31     {
       
    32     CHnConditionInterface* self = HnConditionFactory::NewLC( aElement );
       
    33     CleanupStack::Pop( self );
       
    34     return self;
       
    35     }
       
    36 
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 CHnConditionInterface* HnConditionFactory::NewLC( TXmlEngAttr aElement )
       
    43     {
       
    44     TPtrC8 condition = aElement.Value();
       
    45     CHnConditionInterface* self = ConstructConditionL( condition );
       
    46     CleanupStack::PushL( self );
       
    47     return self;
       
    48     }
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 CHnConditionInterface* HnConditionFactory::NewL( TDesC8 & aElement )
       
    55     {
       
    56     CHnConditionInterface* self = HnConditionFactory::NewLC( aElement );
       
    57     CleanupStack::Pop( self );
       
    58     return self;
       
    59     }
       
    60 
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 CHnConditionInterface* HnConditionFactory::NewLC( TDesC8 & aElement )
       
    67     {
       
    68     CHnConditionInterface* self = HnConditionFactory::ConstructConditionL( aElement );
       
    69     CleanupStack::PushL( self );
       
    70     return self;
       
    71     }
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 void HnConditionFactory::TrimConditionL( RBuf8 & aConditionString )
       
    78     {
       
    79     TBool ret;
       
    80     do
       
    81         {
       
    82         ret = EFalse;
       
    83         aConditionString.TrimAll();
       
    84         TInt conditionLastIndex = aConditionString.Length()-1;
       
    85         if (aConditionString[0] == EOpeningBrace 
       
    86                 && aConditionString[conditionLastIndex] == EClosingBrace )
       
    87             {
       
    88             aConditionString[0] = ESpace;
       
    89             aConditionString[conditionLastIndex] = ESpace;
       
    90             
       
    91             ret = ETrue;
       
    92             
       
    93             if ( CheckBraceIntegrityL( aConditionString ) )
       
    94                 {
       
    95                 aConditionString[0] = EOpeningBrace;
       
    96                 aConditionString[conditionLastIndex] = EClosingBrace;
       
    97                 ret = EFalse;
       
    98                 }
       
    99             }
       
   100         }
       
   101     while (ret);
       
   102     }
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 // ---------------------------------------------------------------------------
       
   107 //
       
   108 TBool HnConditionFactory::CheckBraceIntegrityL( TDesC8 & aConditionString )
       
   109     {
       
   110     TInt ret(KErrNone);
       
   111     TLex8 lexer( aConditionString );
       
   112     TInt lookForClosingBrace( 0 );
       
   113     while  ( !lexer.Eos() )
       
   114         {
       
   115         switch ( TUint( lexer.Get() ) )
       
   116             {
       
   117             case EOpeningBrace :
       
   118                 lookForClosingBrace++;
       
   119                 break;
       
   120             case EClosingBrace :
       
   121                 lookForClosingBrace--;
       
   122                 if (lookForClosingBrace < 0)
       
   123                     {
       
   124                     ret = KErrGeneral;
       
   125                     lexer.Inc( lexer.Remainder().Length() );
       
   126                     }
       
   127                 break;             
       
   128             }
       
   129         }
       
   130     
       
   131     if (lookForClosingBrace > 0)
       
   132         {
       
   133         ret = KErrGeneral;
       
   134         }
       
   135     
       
   136     return ret;  
       
   137     }
       
   138 // ---------------------------------------------------------------------------
       
   139 //
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 CHnConditionInterface* HnConditionFactory::ConstructConditionL(
       
   143         TDesC8 & aConditionString )
       
   144     {
       
   145     RBuf8 condition;
       
   146     CleanupClosePushL( condition );
       
   147     condition.CreateL( aConditionString );
       
   148     HnConditionFactory::TrimConditionL( condition );
       
   149     User::LeaveIfError( HnConditionFactory::CheckBraceIntegrityL(condition) );
       
   150     
       
   151     CHnConditionInterface* ret = NULL;
       
   152     
       
   153     if ( aConditionString.Find( HnLogicalRelations::KLogicalAnd8 ) == KErrNotFound
       
   154          && aConditionString.Find( HnLogicalRelations::KLogicalOr8 ) == KErrNotFound )
       
   155         {
       
   156         ret =  CHnSimpleCondition::NewL( condition );
       
   157         }
       
   158     else
       
   159         {
       
   160         ret = CHnComplexCondition::NewL( condition );
       
   161         }
       
   162     
       
   163     CleanupStack::PopAndDestroy( &condition );
       
   164     return ret;
       
   165     }
       
   166