webservices/wsstar/wsstarpolicy/src/PolicyNormalizer.cpp
changeset 0 62f9d29f7211
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2006-2006 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 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <f32file.h>
       
    23 #include "PolicyNormalizer.h"
       
    24 #include "SenXmlElement.h"
       
    25 #include "WSPolicyRegistry.h"
       
    26 
       
    27 using namespace WSPolicy;
       
    28 
       
    29 namespace
       
    30 {
       
    31 _LIT( KOutPath, "c:\\logs\\out.xml");
       
    32 _LIT8(KColon, ":");
       
    33 //_LIT8( KServicePolicyLocalName, "ServicePolicy");
       
    34 }
       
    35 
       
    36     
       
    37     
       
    38 CPolicyNormalizer* CPolicyNormalizer::NewL()
       
    39     {
       
    40     CPolicyNormalizer* pSelf =
       
    41         CPolicyNormalizer::NewLC();
       
    42     CleanupStack::Pop(pSelf);
       
    43     return pSelf;
       
    44     }
       
    45 
       
    46 CPolicyNormalizer* CPolicyNormalizer::NewLC()
       
    47     {
       
    48     CPolicyNormalizer* pSelf =
       
    49                         new (ELeave) CPolicyNormalizer();
       
    50     CleanupStack::PushL(pSelf);
       
    51     pSelf->ConstructL();
       
    52     return pSelf;
       
    53     }
       
    54 
       
    55 CPolicyNormalizer::~CPolicyNormalizer()
       
    56 {
       
    57     delete iXmlReader;
       
    58     delete iPath;
       
    59     delete iNewPolicy;
       
    60     iOrphanElements.ResetAndDestroy();
       
    61 }
       
    62 
       
    63 CPolicyNormalizer::CPolicyNormalizer()
       
    64     {
       
    65     }
       
    66 void CPolicyNormalizer::ConstructL()
       
    67     {
       
    68 //    BaseConstructL(KServicePolicyLocalName());
       
    69     iXmlReader = CSenXmlReader::NewL();
       
    70     iNewPolicy = CSenXmlElement::NewL(KWsPolicy);
       
    71     count = 0;
       
    72     }
       
    73     
       
    74 
       
    75 
       
    76 CSenElement* CPolicyNormalizer::NormalizeL(CPolicyRegistry* aRegistry)
       
    77 {
       
    78 
       
    79     CSenElement& element = AsElement();
       
    80     TPtrC8 name = element.LocalName();
       
    81 
       
    82     if (name.Compare(KWsPolicy) == 0)
       
    83     {
       
    84         iNewPolicy->CopyFromL(element);
       
    85         
       
    86         RPointerArray<CSenElement>& children = iNewPolicy->ElementsL();
       
    87         CleanupClosePushL(children);
       
    88         TInt childCount = children.Count();
       
    89         
       
    90         if(childCount > 0)
       
    91         {
       
    92             CSenElement* pNextChild;   
       
    93             TInt i = 0;
       
    94             
       
    95             while (i < childCount)
       
    96             {   
       
    97                 
       
    98                 pNextChild = children[childCount-1];
       
    99                 CSenElement* newElement = iNewPolicy->RemoveElement(*pNextChild);
       
   100                 delete newElement;
       
   101                 childCount--;
       
   102             }
       
   103                 
       
   104         }
       
   105         CleanupStack::PopAndDestroy(); //children
       
   106        ProcessPolicyReferenceL(&element, aRegistry);
       
   107        ProcessPolicyTermL(&element, iNewPolicy);
       
   108        ProcessLogicL(iNewPolicy);
       
   109        ProcessAssertionBranchingL(iNewPolicy);
       
   110        
       
   111 //#ifdef _SENDEBUG       
       
   112        WriteL(iNewPolicy);
       
   113 //#endif
       
   114        return iNewPolicy;
       
   115     }
       
   116 
       
   117     return NULL; //&element;   
       
   118 
       
   119 }
       
   120 
       
   121 TPtrC8 CPolicyNormalizer::GetReferenceUriL(CSenElement* aReference)
       
   122     {
       
   123     RPointerArray<CSenBaseAttribute>& attrs = aReference->AttributesL();
       
   124     
       
   125     CSenBaseAttribute* bs = NULL;
       
   126     TInt ele_count = attrs.Count();
       
   127          
       
   128     for (TInt j=0; j < ele_count; j++)
       
   129         {
       
   130             
       
   131          bs = (attrs)[j];
       
   132          if(bs->Name().Compare(KUri) == 0)
       
   133          {
       
   134             return bs->Value();
       
   135          }
       
   136             
       
   137               
       
   138          }
       
   139     return KNullDesC8();
       
   140     }
       
   141     
       
   142 TInt CPolicyNormalizer::ReplacePolicyReferenceL(CSenElement* aReferenceElement, CSenElement* aReferredPolicy)
       
   143     {
       
   144     //get the parent of policy, remove referenceElement and add referred policy there 
       
   145     
       
   146     CSenElement* parent = aReferenceElement->Parent();
       
   147     
       
   148     if(!parent)
       
   149         return KErrNotFound;
       
   150     //add all policy child elements here and then copy those element's contents 
       
   151     //from referred policy,
       
   152     //the referred policy element is owned by SD so do not use it
       
   153     
       
   154      RPointerArray<CSenElement>& children = aReferredPolicy->ElementsL();
       
   155         TInt childCount = children.Count();
       
   156         
       
   157         if(childCount > 0)
       
   158         {
       
   159             CSenElement* pNextChild;   
       
   160             TInt i = 0;
       
   161             
       
   162             while (i < childCount)
       
   163             {   
       
   164                 
       
   165                 pNextChild = children[i];
       
   166                 TPtrC8 localName = pNextChild->LocalName();
       
   167                 TPtrC8 nsUri = pNextChild->NamespaceURI();
       
   168                 CSenElement& newElement = parent->AddElementL(nsUri, localName);
       
   169                 newElement.CopyFromL(*pNextChild);                
       
   170                 i++;
       
   171             }
       
   172         }
       
   173 
       
   174 //now we have resolved the reference so we dont need the PolicyReference element    
       
   175     CSenElement* removed = parent->RemoveElement(*aReferenceElement);
       
   176     
       
   177     if(removed)
       
   178         {
       
   179         delete removed;
       
   180         return KErrNone;
       
   181         }
       
   182     return KErrNotFound;
       
   183     }
       
   184 
       
   185 CSenElement* CPolicyNormalizer::ProcessPolicyReferenceL(CSenElement* aPolicy, CPolicyRegistry* aRegistry)
       
   186 {    //Loop for wsp:PolicyReference element and if found then replace
       
   187     // it with wsp:All and copy everything from found policy to here
       
   188     if(aRegistry == NULL)
       
   189         return NULL;
       
   190     //Check if there is a reference if yes then resolve it
       
   191     RPointerArray<CSenElement> referenceChildren;
       
   192     
       
   193     if(aPolicy->ElementsL(referenceChildren, KWsPolicyReference) == KErrNone)
       
   194         {
       
   195         TInt childCount = referenceChildren.Count();
       
   196         TInt i = 0;
       
   197         CSenElement* pNextChild;
       
   198 
       
   199         while (i < childCount)
       
   200             {
       
   201             pNextChild = referenceChildren[i];
       
   202             TPtrC8 localName = pNextChild->LocalName();
       
   203             TPtrC8 uri = GetReferenceUriL(pNextChild);
       
   204             _LIT8(KHash, "#");
       
   205             
       
   206             HBufC8* aRippedUri = NULL;
       
   207              
       
   208             if(uri.Left(1).Compare(KHash) == 0)
       
   209                aRippedUri = uri.Right(uri.Length()-1).AllocL();
       
   210             
       
   211             if(aRippedUri->Length() > 0)
       
   212                 {
       
   213                 CSenElement* referedPolicy = aRegistry->LookupPolicy(aRippedUri->Des());
       
   214                 if(referedPolicy)
       
   215                     {
       
   216                     ReplacePolicyReferenceL(pNextChild,referedPolicy);
       
   217                     }
       
   218                 }
       
   219                 delete aRippedUri;
       
   220                 
       
   221             i++;
       
   222             }
       
   223         }
       
   224         
       
   225     //Check in all children recursively PolicyReferences and resolve them
       
   226     RPointerArray<CSenElement>& children = aPolicy->ElementsL();
       
   227     TInt childCount = children.Count();    
       
   228 
       
   229     CSenElement* pNextChild;
       
   230     TInt i=0;
       
   231 
       
   232     while (i < childCount)
       
   233     {
       
   234         pNextChild = children[i];
       
   235         TPtrC8 localName = pNextChild->LocalName();
       
   236         ProcessPolicyReferenceL(pNextChild, aRegistry);
       
   237         i++;
       
   238     }
       
   239     
       
   240   return NULL;  
       
   241 }
       
   242 
       
   243 TBool CPolicyNormalizer::IsOptionalL(CSenElement* aAssertion)
       
   244 {
       
   245     // 7, 21, 23, 24
       
   246     RPointerArray<CSenBaseAttribute>& attrs = aAssertion->AttributesL();
       
   247     
       
   248     CSenBaseAttribute* bs = NULL;
       
   249     TInt ele_count = attrs.Count();
       
   250          
       
   251     for (TInt j=0; j < ele_count; j++)
       
   252         {
       
   253             
       
   254          bs = (attrs)[j];
       
   255          if(bs->Name().Compare(KWspOptinal) == 0)
       
   256          {
       
   257             if(bs->Value() == KWspOptinalTrue)
       
   258                 return ETrue;
       
   259          }
       
   260             
       
   261               
       
   262          }
       
   263     return EFalse;
       
   264 }
       
   265 
       
   266 CSenElement* CPolicyNormalizer::ProcessOptionalElement(CSenElement* /*aAssertion*/, CSenElement* aNormalAssertion)
       
   267     {
       
   268     return aNormalAssertion;
       
   269     }
       
   270 
       
   271 CSenElement* CPolicyNormalizer::ProcessAssertionTermL(CSenElement* aAssertion, CSenElement* aNormalAssertion)
       
   272 {
       
   273     TPtrC8 assertionName = aAssertion->LocalName();
       
   274     TPtrC8 assertionNsUri = aAssertion->NamespaceURI();
       
   275     TPtrC8 nsPrefix = aAssertion->NsPrefix();
       
   276     TBuf8<255> qname(nsPrefix);
       
   277     qname.Append(':');
       
   278     qname.Append(assertionName);
       
   279     CSenElement& newNormalForm = aNormalAssertion->AddElementL(assertionNsUri,assertionName, qname);
       
   280   
       
   281     if(IsOptionalL(aAssertion))
       
   282     {
       
   283         if (CopySenElementWithoutOptionL(aAssertion, &newNormalForm))
       
   284         {
       
   285             CSenElement* parent = aNormalAssertion->Parent();
       
   286             AddAndElementL(parent);
       
   287         }
       
   288     }
       
   289     else
       
   290     {
       
   291         CopySenElementL(aAssertion, &newNormalForm);    
       
   292     }
       
   293     
       
   294     
       
   295     
       
   296 
       
   297     RPointerArray<CSenElement>& children = aAssertion->ElementsL();
       
   298     TInt childCount = children.Count();  
       
   299     
       
   300     if (childCount > 0)
       
   301     {
       
   302         CSenElement* pNextChild;
       
   303         TInt i =0;
       
   304         while (i < childCount)
       
   305         {
       
   306         
       
   307             pNextChild = children[i]; 
       
   308             TPtrC8 localName = pNextChild->LocalName();
       
   309             
       
   310             
       
   311             if(localName.Compare(KXorCompositeAssertion) == 0)
       
   312             {
       
   313 
       
   314             }
       
   315             else if (localName.Compare(KAndCompositeAssertion) == 0)
       
   316             {
       
   317 
       
   318             }
       
   319             else if (localName.Compare(KWsPolicy) == 0 )
       
   320             {
       
   321                 CSenElement* aNewElement = AddPolicyElementL(&newNormalForm);
       
   322                 aNewElement = ProcessPolicyTermL(pNextChild,aNewElement);
       
   323 
       
   324             }
       
   325             else
       
   326             {//if asserion is not well defined and dont have  apolicy object here 
       
   327                //then it should add one and then add XOR and AND
       
   328 //                 CSenElement* aNewElement = AddPolicyElementL(&newNormalForm);
       
   329 //                 CSenElement* pChildXor = AddXorElementL(aNewElement);
       
   330 //                 CSenElement* pChildAnd = AddAndElementL(pChildXor);
       
   331                  aNormalAssertion = ProcessAssertionTermL(pNextChild, &newNormalForm);
       
   332             }
       
   333             
       
   334             i++;    
       
   335         }
       
   336     }
       
   337     
       
   338   	return aNormalAssertion;
       
   339 }
       
   340 
       
   341 CSenElement* CPolicyNormalizer::ProcessPolicyTermL(CSenElement* aAssertion, CSenElement* aNormalAssertion)
       
   342 {
       
   343      
       
   344     RPointerArray<CSenElement>& children = aAssertion->ElementsL();
       
   345     TInt childCount = children.Count();    
       
   346     CSenElement* pNextChild;
       
   347     
       
   348     if(childCount == 0)
       
   349     {
       
   350       CSenElement* pXor = AddXorElementL(aNormalAssertion);
       
   351       CSenElement* pAnd = AddAndElementL(pXor);
       
   352       return aNormalAssertion;
       
   353     }
       
   354     TInt i=0;
       
   355     
       
   356     CSenElement* assertionParent = aNormalAssertion;
       
   357    
       
   358 
       
   359     while (i < childCount)
       
   360     {
       
   361         pNextChild = children[i];
       
   362          
       
   363         
       
   364         TPtrC8 localName = pNextChild->LocalName();
       
   365 
       
   366         if(localName.Compare(WSPolicy::KXorCompositeAssertion) == 0)
       
   367         {
       
   368             TPtrC8 nt = aNormalAssertion->LocalName();
       
   369 
       
   370             if(nt.Compare(WSPolicy::KWsPolicy)== 0) //PERFECT we need it
       
   371             {
       
   372             //        aNormalAssertion = 
       
   373                 ProcessXORTermL(pNextChild,aNormalAssertion);
       
   374             }
       
   375 
       
   376             else if(nt.Compare(WSPolicy::KXorCompositeAssertion)== 0)
       
   377             {
       
   378                CSenElement* NewXorParent =  aNormalAssertion->Parent();
       
   379                ProcessXORTermL(pNextChild,NewXorParent); 
       
   380             }
       
   381             else if(nt.Compare(WSPolicy::KAndCompositeAssertion)== 0)
       
   382             {
       
   383                CSenElement* NewXorParent =  aNormalAssertion->Parent()->Parent();
       
   384                ProcessXORTermL(pNextChild,NewXorParent);
       
   385             }
       
   386             else
       
   387             {
       
   388                 //SHOULDNT BE  A CASE CAZ then we have XOR in some assertion
       
   389                 //It should always be in a POLICY tag
       
   390             }
       
   391         }
       
   392         
       
   393     
       
   394         else if (localName.Compare(WSPolicy::KAndCompositeAssertion) == 0)
       
   395         {
       
   396             CSenElement* pNormChild = AddXorElementL(aNormalAssertion);
       
   397             
       
   398     //        aNormalAssertion = 
       
   399             ProcessANDTermL(pNextChild, pNormChild);
       
   400         }
       
   401             
       
   402         
       
   403         else if (localName.Compare(WSPolicy::KWsPolicy) == 0 )
       
   404         {
       
   405             TPtrC8 parentName = aNormalAssertion->LocalName();
       
   406             if(parentName.Compare(WSPolicy::KXorCompositeAssertion) == 0)
       
   407             {
       
   408                 CSenElement* pChildXor = AddXorElementL(aNormalAssertion);
       
   409                 CSenElement* pChildAnd = AddAndElementL(pChildXor);
       
   410                 
       
   411             }
       
   412             else if(parentName.Compare(WSPolicy::KAndCompositeAssertion) == 0)
       
   413             {
       
   414             }
       
   415             else if(parentName.Compare(WSPolicy::KWsPolicy) == 0)
       
   416             {
       
   417                 CSenElement* pChildXor = AddXorElementL(aNormalAssertion);
       
   418                 CSenElement* pChildAnd = AddAndElementL(pChildXor);
       
   419                
       
   420             }
       
   421             else
       
   422             {
       
   423                 CSenElement* pChildXor = AddXorElementL(aNormalAssertion);
       
   424                 CSenElement* pChildAnd = AddAndElementL(pChildXor);                
       
   425             }
       
   426         }
       
   427         else    //its an Assertion
       
   428         {
       
   429             TPtrC8 parentName = assertionParent->LocalName();
       
   430             //if parent is Policy -> add XOR and AND and then process 
       
   431             //assertion- >new parent for same level is AND
       
   432             
       
   433             //if parent is XOR -> Add AND and then process assertion. at this level the parent will be AND
       
   434             //if parent is AND just process assertion
       
   435             if(IsOptionalL(pNextChild))
       
   436             {
       
   437                 if(parentName.Compare(WSPolicy::KWsPolicy) == 0)
       
   438                 {
       
   439                     CSenElement* pXor = AddXorElementL(assertionParent);
       
   440                     CSenElement* pAnd = AddAndElementL(pXor);
       
   441     //                aNormalAssertion = 
       
   442                     ProcessAssertionTermL(pNextChild, pAnd);
       
   443                 }
       
   444                 else if(parentName.Compare(WSPolicy::KAndCompositeAssertion) == 0)
       
   445                 {
       
   446                     CSenElement* parentNow = assertionParent;
       
   447                     assertionParent = assertionParent->Parent();
       
   448                     CSenElement* policyParent= assertionParent->Parent();
       
   449                     CSenElement* pXor = AddXorElementL(policyParent);
       
   450                     CSenElement* pAnd = AddAndElementL(pXor);
       
   451     //              aNormalAssertion = 
       
   452                     ProcessAssertionTermL(pNextChild, pAnd);   
       
   453                     assertionParent = parentNow;  
       
   454 
       
   455                 }   
       
   456                 else if(parentName.Compare(WSPolicy::KXorCompositeAssertion) == 0)
       
   457                 {
       
   458                     
       
   459                 }
       
   460                 else
       
   461                 {
       
   462                     //probably error   
       
   463                 }
       
   464                 
       
   465  
       
   466             }
       
   467             else
       
   468             {
       
   469                 if(parentName.Compare(WSPolicy::KWsPolicy) == 0)
       
   470                 {
       
   471                     CSenElement* pXor = AddXorElementL(assertionParent);
       
   472                     CSenElement* pAnd = AddAndElementL(pXor);
       
   473                     assertionParent = pAnd;
       
   474     //                assertionParent =  ProcessAssertionTermL(pNextChild, pAnd);
       
   475                     
       
   476                 }
       
   477                 else if(parentName.Compare(WSPolicy::KXorCompositeAssertion) == 0)
       
   478                 {
       
   479     
       
   480                     CSenElement* pXor = AddXorElementL(assertionParent);
       
   481                     CSenElement* pAnd = AddAndElementL(pXor);
       
   482     //            aNormalAssertion = ProcessAssertionTermL(pNextChild, pAnd);
       
   483 
       
   484                     
       
   485                 }
       
   486                 else if(parentName.Compare(WSPolicy::KAndCompositeAssertion) == 0)
       
   487                 {
       
   488     
       
   489     //            aNormalAssertion = ProcessAssertionTermL(pNextChild, assertionParent);
       
   490                     
       
   491                 }
       
   492                 else 
       
   493                 {
       
   494                     CSenElement* pXor = AddXorElementL(assertionParent);
       
   495                     CSenElement* pAnd = AddAndElementL(pXor);
       
   496                     assertionParent = pAnd;
       
   497       //            aNormalAssertion =  ProcessAssertionTermL(pNextChild, pAnd);
       
   498                 }
       
   499                 
       
   500                 ProcessAssertionTermL(pNextChild, assertionParent);
       
   501             
       
   502             }
       
   503         }
       
   504         
       
   505     
       
   506 
       
   507      i++;   
       
   508     }
       
   509 
       
   510     return assertionParent;
       
   511 
       
   512 }
       
   513 CSenElement* CPolicyNormalizer::ProcessXORTermL(CSenElement* aAssertion, CSenElement* aNormalAssertion)
       
   514 {
       
   515     
       
   516     CSenElement* pNormChild = AddXorElementL(aNormalAssertion);
       
   517     
       
   518     RPointerArray<CSenElement>& children = aAssertion->ElementsL();
       
   519     TInt childCount = children.Count();
       
   520     TInt i = 0;
       
   521     
       
   522      while (i < childCount)
       
   523     {
       
   524         CSenElement* pChild = children[i];
       
   525         
       
   526         CSenElement* pNewChild = NULL;
       
   527 //        CSenElement* newChild = NULL;
       
   528         
       
   529         TPtrC8 childName = pChild->LocalName();
       
   530         
       
   531         if(childName.Compare(WSPolicy::KAndCompositeAssertion) == 0)
       
   532         {
       
   533             //If AND element then take child of AND
       
   534 //            CSenElement* newChild = 
       
   535             ProcessANDTermL(pChild, pNormChild);
       
   536             
       
   537         }
       
   538         else if (childName.Compare(WSPolicy::KXorCompositeAssertion) == 0)
       
   539         {
       
   540 
       
   541         }
       
   542         else if(childName.Compare(WSPolicy::KWsPolicy) == 0)
       
   543         {
       
   544             pNewChild = AddAndElementL(pNormChild);
       
   545             if(HasChildL(pChild) > 0)
       
   546                 ProcessPolicyTermL(pChild,pNewChild);
       
   547         }
       
   548         else
       
   549         {
       
   550             pNewChild = AddAndElementL(pNormChild);
       
   551 //            newChild = 
       
   552             ProcessAssertionTermL(pChild, pNewChild);
       
   553             //if Assertion then first we haveto add AND and then add Assertion
       
   554             //(assertion will be done by AND too)
       
   555         }
       
   556           
       
   557           i++;  
       
   558     }
       
   559     
       
   560     		
       
   561 	return pNormChild;	
       
   562 	
       
   563 }
       
   564 CSenElement* CPolicyNormalizer::ProcessANDTermL(CSenElement* aAssertion, CSenElement* aNormalAssertion)
       
   565 {
       
   566     CSenElement* pNormChild = AddAndElementL(aNormalAssertion);
       
   567     
       
   568     RPointerArray<CSenElement>& children = aAssertion->ElementsL();
       
   569     TInt childCount = children.Count();
       
   570 //    CSenElement* normalizedFinalChild = NULL;
       
   571     TInt i=0;
       
   572     while (i < childCount)
       
   573     {
       
   574         CSenElement* pChild = children[i];
       
   575 //        CSenElement* newChild = NULL;
       
   576         
       
   577         TPtrC8 childName = pChild->LocalName();
       
   578      
       
   579     if(childName.Compare(WSPolicy::KAndCompositeAssertion) == 0)
       
   580         {
       
   581         
       
   582         }
       
   583     else if (childName.Compare(WSPolicy::KXorCompositeAssertion) == 0)
       
   584         {
       
   585             //will be solved during collapsing
       
   586         
       
   587         }
       
   588     else if (childName.Compare(WSPolicy::KWsPolicy) == 0)
       
   589         {
       
   590             ProcessPolicyTermL(pChild,pNormChild);
       
   591             
       
   592         }
       
   593     else
       
   594         {
       
   595 //              normalizedFinalChild = 
       
   596               ProcessAssertionTermL(pChild, pNormChild);
       
   597         }
       
   598  
       
   599         i++; 
       
   600     }
       
   601        
       
   602 	return aNormalAssertion;
       
   603 }
       
   604 
       
   605 
       
   606 
       
   607 
       
   608 
       
   609 CSenElement* CPolicyNormalizer::ProcessLogicL(CSenElement* aTerm)
       
   610 {
       
   611     
       
   612      //if sibling names are same as <ExactlyOne>
       
   613     RPointerArray<CSenElement> matchingSiblings;
       
   614     TInt matchingSiblingCount = 0;
       
   615     if(aTerm->ElementsL(matchingSiblings, WSPolicy::KXorCompositeAssertion) == KErrNone)
       
   616         matchingSiblingCount= matchingSiblings.Count();
       
   617     if(matchingSiblingCount>1)
       
   618         {  //XOR elements should have AND elements in them else normalization error
       
   619         //Find elements
       
   620         CSenElement* firstSibling = matchingSiblings[0];
       
   621         
       
   622         TInt i = 1;
       
   623             while ( i < matchingSiblingCount)
       
   624             {
       
   625                 CSenElement* nextSibling = matchingSiblings[i];
       
   626                 firstSibling = ProcessAndAndLogicL(firstSibling, nextSibling);
       
   627 
       
   628                 i++;
       
   629             }
       
   630             TInt j = 0;
       
   631             while ( j < matchingSiblingCount)
       
   632             {
       
   633                 CSenElement* removed = aTerm->RemoveElement(*matchingSiblings[j]);
       
   634                 if(removed)
       
   635                     delete removed;
       
   636                 j++;                
       
   637             }
       
   638                         
       
   639 /****        aTerm->AddElementL(*firstSibling); */
       
   640          }
       
   641     
       
   642     RPointerArray<CSenElement>& children = aTerm->ElementsL();
       
   643     TInt childCount = children.Count();
       
   644     
       
   645     TInt i=0;
       
   646         
       
   647 
       
   648     while (i < childCount)
       
   649     {
       
   650         CSenElement* pChild = children[i];
       
   651         
       
   652         if(childCount == 1)
       
   653             ProcessLogicL(pChild);
       
   654         
       
   655         TPtrC8 childName = pChild->LocalName();
       
   656         TPtrC8 parentName = aTerm->LocalName();
       
   657     
       
   658         
       
   659         //if parent name and child name are same
       
   660         if(childName.Compare(parentName) == 0)
       
   661         {
       
   662                 if(childName.Compare(WSPolicy::KWsPolicy) == 0)
       
   663                 {
       
   664                     
       
   665                 }
       
   666                 else if(childName.Compare(WSPolicy::KAndCompositeAssertion) == 0)
       
   667                 {
       
   668                     
       
   669                 }
       
   670                 else if(childName.Compare(WSPolicy::KXorCompositeAssertion) == 0)
       
   671                 {
       
   672                   ProcessXorCollapseLogicL(aTerm,pChild);
       
   673                   continue;  
       
   674                 }   
       
   675           
       
   676         }
       
   677         
       
   678 
       
   679         i++;
       
   680     }
       
   681     
       
   682     matchingSiblings.Close();
       
   683     return aTerm;
       
   684 }
       
   685 
       
   686 CSenElement* CPolicyNormalizer::ProcessXorCollapseLogicL(CSenElement* aParent, CSenElement* aChild)
       
   687 {
       
   688         RPointerArray<CSenElement>& siblings = aParent->ElementsL();
       
   689         RPointerArray<CSenElement>& collapsingChildren = aChild->ElementsL();
       
   690         
       
   691         TInt childCount = collapsingChildren.Count();
       
   692         TInt i=0;
       
   693      
       
   694         RPointerArray<CSenElement> matchingSiblings;
       
   695         TInt matchingSiblingCount = 0;
       
   696         if(aParent->ElementsL(matchingSiblings, WSPolicy::KAndCompositeAssertion) == KErrNone)
       
   697             matchingSiblingCount= matchingSiblings.Count();
       
   698                 
       
   699      
       
   700         while (i < childCount)
       
   701         {
       
   702             CSenElement* child = collapsingChildren[i];
       
   703            
       
   704          
       
   705               for (TInt j = 0; j<matchingSiblingCount; j++)                    
       
   706                 {
       
   707                 
       
   708                     RPointerArray<CSenElement>& siblingsChildren = matchingSiblings[j]->ElementsL();
       
   709                     TInt matchingSiblingChildrenCount= siblingsChildren.Count();
       
   710                     
       
   711                     for (TInt k = 0; k<matchingSiblingChildrenCount; k++)
       
   712                     {
       
   713                         CSenElement* element = siblingsChildren[k];
       
   714                         child->AddElementL(*element);
       
   715  //                       TPtrC8 name = detachedSibling->LocalName();
       
   716                         
       
   717                     }
       
   718                     
       
   719                     
       
   720                 }
       
   721                 child->SetParent(aParent);
       
   722                 aParent->AddElementL(*child);
       
   723 
       
   724       
       
   725         i++;
       
   726         }
       
   727         
       
   728          CSenElement* thisOne = aParent->RemoveElement(*aChild);
       
   729          if(thisOne)
       
   730             delete thisOne;
       
   731         
       
   732         for (TInt j = 0; j<matchingSiblingCount; j++)  
       
   733         {
       
   734             CSenElement* thisOneagain = aParent->RemoveElement(*matchingSiblings[j]);    
       
   735             if(thisOneagain)
       
   736                 delete thisOneagain;
       
   737             
       
   738         }
       
   739         
       
   740         
       
   741         return aParent;
       
   742     
       
   743 
       
   744 }
       
   745 CSenElement* CPolicyNormalizer::ProcessAssertionBranchingL(CSenElement* aPolicy)
       
   746 {
       
   747   
       
   748   RPointerArray<CSenElement>& children = aPolicy->ElementsL();
       
   749   TInt childCount = children.Count();
       
   750   
       
   751   if(childCount == 0)
       
   752     return aPolicy;
       
   753   
       
   754   TInt i = 0;
       
   755   while (i < childCount)
       
   756     {
       
   757     CSenElement* pChild = children[i];
       
   758     TPtrC8 childName = pChild->LocalName();
       
   759     
       
   760     if( childName.Compare(WSPolicy::KWsPolicy) == 0 
       
   761         || childName.Compare(WSPolicy::KAndCompositeAssertion) == 0
       
   762         || childName.Compare(WSPolicy::KXorCompositeAssertion) == 0)
       
   763         {
       
   764         ProcessAssertionBranchingL(pChild);
       
   765         }   
       
   766     else //Assertion
       
   767         {
       
   768         DuplicateAssertionBranchL(aPolicy , pChild);
       
   769         }
       
   770     i++;
       
   771     }
       
   772   return aPolicy;
       
   773   }
       
   774 
       
   775 CSenElement* CPolicyNormalizer::DuplicateAssertionBranchL(CSenElement* /*aParent*/, CSenElement* aDuplicatingPolicy)
       
   776 {  //aDuplicatingPolicy is always an assertion 
       
   777 //check if aDuplicatingPolicy have two assertions, if yes then divide the assertion in 
       
   778   // T W O separate assertions
       
   779   //first check that if any of the two assertion themselve have 
       
   780   
       
   781   // if XOR have multiple ANDs then check if XOR parent is an Assertion. 
       
   782   // if yes then divide them in Two assertions.
       
   783   TPtrC8 asseryName = aDuplicatingPolicy->LocalName();
       
   784   TInt childCount = HasChildL(aDuplicatingPolicy);
       
   785   
       
   786   if( childCount== 0) //There is NO CHILD NESTING in assertion
       
   787         return aDuplicatingPolicy;  
       
   788   else if (childCount == 1) ////Assertion should always have only one <wsp:policy> element. 
       
   789   {
       
   790     TBool deleted = EFalse;
       
   791         CSenElement* assertionPolicyElement = aDuplicatingPolicy->Child(0); //policy element
       
   792         TPtrC8 policyName = assertionPolicyElement->LocalName();
       
   793         if(policyName.Compare(WSPolicy::KWsPolicy)== 0)
       
   794         { //now we should have XOR and AND elements
       
   795         
       
   796         CSenElement* assertionXORElement = assertionPolicyElement->Child(0); //XOR element
       
   797         //there should be only one XOR element (in normalize mode)
       
   798         TPtrC8 name1 = assertionXORElement->LocalName();
       
   799         TInt AndElementCount = HasChildL(assertionXORElement);
       
   800         //If it has only one AND child element then nuthing to do here but if more then one element
       
   801         // then it means that assertion have nesting and should have a different branch.
       
   802         if(AndElementCount == 0)
       
   803             return aDuplicatingPolicy;
       
   804         else if(AndElementCount == 1)
       
   805         {
       
   806              ProcessAssertionBranchingL(assertionXORElement->Child(0));   
       
   807         }
       
   808         else if(AndElementCount > 1)
       
   809         {
       
   810              RPointerArray<CSenElement>& assertionXorChildren = assertionXORElement->ElementsL(); //AND elements
       
   811              TInt i = 0;
       
   812              while (i < AndElementCount)
       
   813              {
       
   814                 //copy the root assertion to new tag
       
   815                 // Take out the replacing tag
       
   816                 // create two separate tag and replace them in the original and root copy tag
       
   817                 
       
   818                 CSenXmlElement* AND = (CSenXmlElement*)assertionXorChildren[i];
       
   819 //                ProcessAssertionBranchingL(AND);
       
   820                 
       
   821                 CSenXmlElement* rootAssertion = (CSenXmlElement*)FindRootAssertionL(AND);
       
   822                 if (!IsChildOfElement(rootAssertion, AND))
       
   823                 {
       
   824                     i++;
       
   825                     continue;
       
   826                 }
       
   827                 
       
   828 //create a duplicate tag,
       
   829                 //duplicating assertion                
       
   830                 TPtrC8 assertionCopyName = rootAssertion->LocalName();
       
   831                 TPtrC8 assertionCopyNsUri = rootAssertion->NamespaceURI();
       
   832                 CSenXmlElement* XORParent = (CSenXmlElement*)rootAssertion->Parent();
       
   833                 CSenElement* parentsBrandNewRootAssertion =AddAndElementL(XORParent->Parent());
       
   834                 
       
   835                 CSenElement& newDuplicateAssertion = parentsBrandNewRootAssertion->AddElementL(assertionCopyNsUri,assertionCopyName);
       
   836 
       
   837 //cut and paste the second AND to duplicate
       
   838                 
       
   839                 newDuplicateAssertion.CopyFromL(*rootAssertion);
       
   840                 
       
   841                 DeleteAsertionIndexL(aDuplicatingPolicy, &newDuplicateAssertion, i);
       
   842                 deleted = ETrue;
       
   843 //                newDuplicateAssertion.SetParent(parentsBrandNewRootAssertion);
       
   844                 //parentsBrandNewRootAssertion->ReplaceElementL(newDuplicateAssertion);
       
   845                 
       
   846                 //find from this new element the required assertion and then delete
       
   847                 // all of the optins from it except the current i+1 ALL element
       
   848              
       
   849                 i++;
       
   850              }
       
   851 
       
   852             if(deleted)                    
       
   853             {
       
   854              CSenElement* rootAssertion = FindRootAssertionL(aDuplicatingPolicy);
       
   855              CSenElement* AND = rootAssertion->Parent();
       
   856              CSenElement* XOR = AND->Parent();
       
   857              CSenElement* thisOne = XOR->RemoveElement(*AND);
       
   858              if(thisOne)             
       
   859                  iOrphanElements.Append(thisOne);
       
   860 //                delete thisOne; //P DuplicateAssertionBranch A T H E T I C   H A C K 
       
   861              //now we have solved the first level nesting
       
   862              //our original policy is changed so now use the original policy and solve it again
       
   863              CSenElement* documentRoot = (CSenElement*)&XOR->Root(); 
       
   864              ProcessAssertionBranchingL(documentRoot);
       
   865  
       
   866         }
       
   867         
       
   868         
       
   869         }
       
   870         }
       
   871   }
       
   872   else if(childCount > 1)
       
   873   {
       
   874   //this count is always one because its a child wsp:policy element, policy element 
       
   875   //could have multiple XOR elements . If it comes here something is W R O N G
       
   876     
       
   877   }
       
   878     //here is the nesting problem. the assertion policy should have only one 
       
   879    // Alternative = <XOR><AND>< --assertion-- ></AND></XOR> 
       
   880    // NO TWO XORs are allowed here
       
   881    return aDuplicatingPolicy;
       
   882 }
       
   883 CSenElement* CPolicyNormalizer::DeleteAsertionIndexL(CSenElement* aAssertion, CSenElement* anewAssertion, TInt aIndx)
       
   884 {
       
   885     TPtrC8 name = aAssertion->LocalName();
       
   886     TPtrC8 nsUri = aAssertion->NamespaceURI();
       
   887     TInt i = 0;
       
   888 
       
   889     RPointerArray<CSenElement>& assertionChildren = anewAssertion->ElementsL();
       
   890     TInt newAssertionChildCount = assertionChildren.Count();
       
   891     
       
   892     if(newAssertionChildCount == 0)
       
   893         return anewAssertion;
       
   894     
       
   895     TPtrC8 namee = anewAssertion->LocalName();
       
   896     if (name.Compare(anewAssertion->LocalName())== 0)
       
   897     { //delete children from this assertion except index child
       
   898         CSenXmlElement* PolicyElement = (CSenXmlElement*)anewAssertion->Child(0);
       
   899         CSenXmlElement* XORElement = (CSenXmlElement*)PolicyElement->Child(0);
       
   900         RPointerArray<CSenElement>& XorChildren = XORElement->ElementsL();
       
   901         TInt XORChildrenCount = XorChildren.Count();
       
   902          
       
   903         for (TInt k = 0; k <XORChildrenCount; k++)
       
   904         {
       
   905             if(k == aIndx)
       
   906             {
       
   907                 
       
   908             }
       
   909             else
       
   910             {
       
   911                 CSenElement* newElement = XORElement->RemoveElement(*XorChildren[XORChildrenCount-1-k]);
       
   912                 if(newElement)
       
   913                 {
       
   914                     delete newElement;               
       
   915                 //    break;    
       
   916                 }
       
   917                 
       
   918             }
       
   919                 
       
   920         }
       
   921         return anewAssertion;
       
   922     }
       
   923 
       
   924     while (i < newAssertionChildCount)
       
   925     {
       
   926     
       
   927         DeleteAsertionIndexL(aAssertion, assertionChildren[i], aIndx);
       
   928         i++;
       
   929     }
       
   930     
       
   931     return anewAssertion;
       
   932 }
       
   933 CSenElement* CPolicyNormalizer::FindRootAssertionL(CSenElement* aElement)
       
   934 {
       
   935 
       
   936     CSenElement* rootAssertionTag = NULL;
       
   937     
       
   938     CSenElement* documentRoot = (CSenElement*)&aElement->Root();
       
   939     CSenElement* doumentXOR = documentRoot->Child(0); //cant be more then one :p
       
   940     
       
   941     //find first assertion here  
       
   942     RPointerArray<CSenElement>& children = doumentXOR->ElementsL(); //AND elements
       
   943     TInt childCount = children.Count();
       
   944     
       
   945     if(childCount == 0) //something invalid with document
       
   946         return aElement;
       
   947   
       
   948     else if (childCount > 0) //onyl one AND element, Next child should be root Assertion
       
   949     {
       
   950         TInt i = 0;
       
   951         while (i < childCount)
       
   952         {            
       
   953             CSenElement* andTag = children[i];
       
   954         TInt andAssertions = HasChildL(andTag);
       
   955         
       
   956         if(andAssertions == 0)
       
   957             return aElement;
       
   958         else if(andAssertions == 1)
       
   959             {
       
   960                 TBool isChild = IsChildOfElement(andTag->Child(0), aElement);
       
   961                 if(isChild)
       
   962                 return andTag->Child(0);
       
   963             }
       
   964         else if (andAssertions > 1)
       
   965         {
       
   966             RPointerArray<CSenElement>& rootAssertions = andTag->ElementsL(); //AND elements
       
   967             TInt j = 0;
       
   968             while (j < andAssertions)
       
   969             {
       
   970                 
       
   971                 TBool isChild = IsChildOfElement(rootAssertions[j], aElement);
       
   972                 
       
   973                 if(isChild)
       
   974                 {
       
   975                      rootAssertionTag = rootAssertions[j];
       
   976                      break;
       
   977                 }
       
   978                 
       
   979                 j++;
       
   980                 }
       
   981             }
       
   982         i++;
       
   983         }
       
   984         
       
   985         
       
   986     }
       
   987     else if (childCount > 1)
       
   988     {
       
   989     
       
   990         
       
   991     }
       
   992     
       
   993     return rootAssertionTag;
       
   994 
       
   995 }
       
   996 TBool CPolicyNormalizer::IsChildOfElement(CSenElement* aParent, CSenElement* aChild)
       
   997 { //aParent and child botth are assertions
       
   998     
       
   999     CSenElement* rootChildAssertionTag = aChild; //->Parent();
       
  1000     TPtrC8 nqame1 = aParent->LocalName();
       
  1001     while (true)
       
  1002     {
       
  1003         
       
  1004         if(rootChildAssertionTag)
       
  1005         {
       
  1006             
       
  1007             TPtrC8 nqame2 = rootChildAssertionTag->LocalName();
       
  1008             
       
  1009             if(rootChildAssertionTag == aParent)
       
  1010             {
       
  1011                 return ETrue;
       
  1012         
       
  1013             }
       
  1014         }
       
  1015         else
       
  1016         {
       
  1017             return EFalse;    
       
  1018         }   
       
  1019         rootChildAssertionTag = rootChildAssertionTag->Parent();
       
  1020     }
       
  1021 }
       
  1022 CSenElement* CPolicyNormalizer::ProcessAndCollapseLogic(CSenElement* aParent, CSenElement* /*aChild*/)
       
  1023 {
       
  1024  return  aParent;  
       
  1025 }
       
  1026 
       
  1027 CSenElement* CPolicyNormalizer::ProcessAndAndLogicL(CSenElement* aFirstTerm, CSenElement* aSecondTerm )
       
  1028 {
       
  1029     //Two XOR XOR parameters are given which have same parent and have AND child elements,
       
  1030     //The AND child elements will be resolved according to Ditributive law 
       
  1031     //takes all child Assertion elements from SecondTerm and put them in first term
       
  1032     
       
  1033 
       
  1034     RPointerArray<CSenElement> matchingSiblingsFirst;
       
  1035     CleanupClosePushL(matchingSiblingsFirst);
       
  1036     
       
  1037     if(aFirstTerm->ElementsL(matchingSiblingsFirst, WSPolicy::KAndCompositeAssertion) == KErrNone
       
  1038        || aFirstTerm->ElementsL(matchingSiblingsFirst, WSPolicy::KXorCompositeAssertion) == KErrNone
       
  1039        || aFirstTerm->ElementsL(matchingSiblingsFirst, WSPolicy::KWsPolicy) == KErrNone
       
  1040        || aSecondTerm->ElementsL(matchingSiblingsFirst, WSPolicy::KAndCompositeAssertion) == KErrNone
       
  1041        || aSecondTerm->ElementsL(matchingSiblingsFirst, WSPolicy::KXorCompositeAssertion) == KErrNone
       
  1042        || aSecondTerm->ElementsL(matchingSiblingsFirst, WSPolicy::KWsPolicy) == KErrNone)
       
  1043        
       
  1044        
       
  1045        
       
  1046     { // they are NOT the last children so first go deep to last assertion and then process that assertion
       
  1047             ProcessLogicL(aFirstTerm);
       
  1048             ProcessLogicL(aSecondTerm);
       
  1049     }
       
  1050     CleanupStack::PopAndDestroy();//matchingSiblingsFirst
       
  1051     
       
  1052 //    else // they are the last children
       
  1053    
       
  1054      
       
  1055         RPointerArray<CSenElement>& siblings = aFirstTerm->ElementsL();
       
  1056         RPointerArray<CSenElement>& collapsingChildren = aSecondTerm->ElementsL();
       
  1057          
       
  1058         
       
  1059         TInt siblingsCount = siblings.Count();
       
  1060         TInt childCount = collapsingChildren.Count();
       
  1061         
       
  1062         if(siblingsCount == 0)
       
  1063             return aFirstTerm;
       
  1064         else if (childCount == 0)
       
  1065             return aSecondTerm;
       
  1066         
       
  1067         TInt i=0;
       
  1068         TInt j=0;
       
  1069         
       
  1070         CSenElement* collpasingElement = NULL;
       
  1071         CSenElement* returningElement = NULL;
       
  1072         if(siblingsCount <  childCount)
       
  1073         {
       
  1074             CSenElement& distributiveElement = aFirstTerm->Parent()->AddElementL(WSPolicy::KWsPolicyNsUri,WSPolicy::KXorCompositeAssertion);;
       
  1075 //            distributiveElement = CSenXmlElement::NewL(WSPolicy::KWsPolicyNsUri,WSPolicy::KXorCompositeAssertion);   
       
  1076 //            aFirstTerm->Parent()->AddElementL(*distributiveElement);
       
  1077 //            distributiveElement->SetParent(aFirstTerm->Parent());
       
  1078             
       
  1079             while(j < childCount)
       
  1080             { //chnage logic for <ALL/>
       
  1081 /*
       
  1082 
       
  1083 <XOR>
       
  1084     <AND>
       
  1085         <!--assertion1-->
       
  1086     </AND>
       
  1087     <AND/>
       
  1088 </XOR>
       
  1089 <XOR>
       
  1090     <AND>
       
  1091         <!--assertion3-->
       
  1092     </AND>
       
  1093     <AND>
       
  1094         <!--assertion4-->
       
  1095     </AND>
       
  1096 
       
  1097 </XOR>
       
  1098 
       
  1099 1.make assertion1 assertion 3
       
  1100 2. make assertion 2 assertion 3
       
  1101 3. Replace <AND/> with all the assertions from second <XOR> i.e. 
       
  1102 with 
       
  1103     <AND>
       
  1104         <!--assertion3-->
       
  1105     </AND>
       
  1106     <AND>
       
  1107         <!--assertion4-->
       
  1108     </AND>
       
  1109 */                
       
  1110                 CSenElement* newParentElement = collapsingChildren[j];
       
  1111                 
       
  1112                 TBool EmptyAndElement = EFalse;
       
  1113                 if (HasChildL(newParentElement) < 1)
       
  1114                 {
       
  1115                     EmptyAndElement = ETrue;
       
  1116                 }
       
  1117                 
       
  1118                 while(i < siblingsCount)
       
  1119                 {
       
  1120                     collpasingElement = siblings[i];
       
  1121 
       
  1122                     RPointerArray<CSenElement>& collpasEleAssertion = collpasingElement->ElementsL();
       
  1123                     TInt countt = collpasEleAssertion.Count();
       
  1124                     
       
  1125                     if(EmptyAndElement) //copy all elements from here to new parent
       
  1126                     {
       
  1127 
       
  1128                            CSenElement& ele = distributiveElement.AddElementL(collpasingElement->NamespaceURI(), collpasingElement->LocalName());
       
  1129                            ele.CopyFromL(*collpasingElement);
       
  1130                     }
       
  1131 //                    else if(collpasingElement->Child(0))
       
  1132 //                        newParentElement->AddElementL(*collpasingElement->Child(0));
       
  1133                     else
       
  1134                     { //This is an empty <ALL /> element so use distributive law to resolve it
       
  1135                        
       
  1136                        CSenElement* AndElement = AddAndElementL(&distributiveElement);
       
  1137                         //copy here all elements from first AND element and 2nd AND element
       
  1138                         
       
  1139                         RPointerArray<CSenElement>& ParentChildren = newParentElement->ElementsL();
       
  1140                         TInt countHere = ParentChildren.Count();
       
  1141                           for (TInt k = 0; k < countHere; k++)
       
  1142                             {
       
  1143                                 CSenElement* pkChild = ParentChildren[k];
       
  1144                                 CSenElement& here = AndElement->AddElementL(pkChild->NamespaceURI(), pkChild->LocalName());
       
  1145                                 here.CopyFromL(*pkChild);
       
  1146                             }
       
  1147                           for(TInt l = 0; l < countt; l++)
       
  1148                             {
       
  1149                                 CSenElement* paChild = collpasEleAssertion[l];
       
  1150                                 CSenElement& hereAgain = AndElement->AddElementL(paChild->NamespaceURI(), paChild->LocalName());
       
  1151                                 hereAgain.CopyFromL(*paChild);
       
  1152                                 
       
  1153                             }
       
  1154                           
       
  1155                     }
       
  1156                     i++;
       
  1157                 }
       
  1158                 i = 0;
       
  1159                 j++;
       
  1160             }
       
  1161             returningElement = &distributiveElement;
       
  1162             CSenElement* parentElement = aFirstTerm->Parent();
       
  1163 //            parentElement->RemoveElement(*aFirstTerm);
       
  1164             
       
  1165         }
       
  1166         else
       
  1167         {
       
  1168             CSenElement& distributiveElement = aFirstTerm->Parent()->AddElementL(WSPolicy::KWsPolicyNsUri,WSPolicy::KXorCompositeAssertion);;
       
  1169 //            distributiveElement = CSenXmlElement::NewL(WSPolicy::KWsPolicyNsUri,WSPolicy::KXorCompositeAssertion);   
       
  1170 //            aFirstTerm->Parent()->AddElementL(*distributiveElement);
       
  1171 //            distributiveElement->SetParent(aFirstTerm->Parent());
       
  1172             while(j < siblingsCount)
       
  1173             {
       
  1174                 CSenElement* newParentElement = siblings[j]; //AND element
       
  1175 
       
  1176                 TBool EmptyAndElement = EFalse;
       
  1177                 if (HasChildL(newParentElement) < 1)
       
  1178                 {
       
  1179                     EmptyAndElement = ETrue;
       
  1180                 }
       
  1181 
       
  1182         
       
  1183                 while(i < childCount)
       
  1184                 {
       
  1185                     collpasingElement = collapsingChildren[i]; //AND element
       
  1186 
       
  1187                     RPointerArray<CSenElement>& collpasEleAssertion = collpasingElement->ElementsL(); // AND element Assertions
       
  1188                     TInt countt = collpasEleAssertion.Count();
       
  1189                     
       
  1190                     if(EmptyAndElement) //copy all elements from here to new parent
       
  1191                     {
       
  1192                            CSenElement& ele = distributiveElement.AddElementL(collpasingElement->NamespaceURI(), collpasingElement->LocalName());
       
  1193                            ele.CopyFromL(*collpasingElement);
       
  1194                     }   
       
  1195 //                    else if(countt == 1)    //If AND has only one assertion
       
  1196 //                        newParentElement->AddElementL(*collpasingElement->Child(0));
       
  1197                     else //if(countt > 1)
       
  1198                     { //Distributive Law
       
  1199                     
       
  1200                         CSenElement* AndElement = AddAndElementL(&distributiveElement);
       
  1201                         //copy here all elements from first AND element and 2nd AND element
       
  1202                         
       
  1203                         RPointerArray<CSenElement>& ParentChildren = newParentElement->ElementsL();
       
  1204                         TInt countHere = ParentChildren.Count();
       
  1205                           for (TInt k = 0; k < countHere; k++)
       
  1206                             {
       
  1207                                 CSenElement* pkChild = ParentChildren[k];
       
  1208                                 CSenElement& here = AndElement->AddElementL(pkChild->NamespaceURI(), pkChild->LocalName());
       
  1209                                 here.CopyFromL(*pkChild);
       
  1210                             }
       
  1211                           for(TInt l = 0; l < countt; l++)
       
  1212                             {
       
  1213                                 CSenElement* paChild = collpasEleAssertion[l];
       
  1214                                 CSenElement& hereAgain = AndElement->AddElementL(paChild->NamespaceURI(), paChild->LocalName());
       
  1215                                 hereAgain.CopyFromL(*paChild);
       
  1216                             }
       
  1217                          
       
  1218                     }
       
  1219                     i++;
       
  1220                 }
       
  1221                 i = 0;
       
  1222                 j++;
       
  1223         }
       
  1224             returningElement = &distributiveElement; //; aFirstTerm;
       
  1225             CSenElement* parentElement = aSecondTerm->Parent();
       
  1226         }
       
  1227 
       
  1228     return  returningElement;
       
  1229     
       
  1230 }
       
  1231 
       
  1232 TInt CPolicyNormalizer::HasChildL(CSenElement* aTerm)
       
  1233 {
       
  1234     RPointerArray<CSenElement>& children = aTerm->ElementsL();
       
  1235     
       
  1236     TInt childCount = children.Count();
       
  1237     return childCount;
       
  1238     
       
  1239 }
       
  1240 
       
  1241 CSenElement* CPolicyNormalizer::AddAndElementL(CSenElement* aAssertion)
       
  1242 {
       
  1243    
       
  1244     CSenElement& newElement = aAssertion->AddElementL(WSPolicy::KWsPolicyNsUri,
       
  1245                                                  WSPolicy::KAndCompositeAssertion);
       
  1246     return &newElement;
       
  1247                                                                                                   
       
  1248 }
       
  1249 
       
  1250 CSenElement* CPolicyNormalizer::AddXorElementL(CSenElement* aAssertion)
       
  1251 {
       
  1252      CSenElement& newElement = aAssertion->AddElementL(WSPolicy::KWsPolicyNsUri,
       
  1253                                                  WSPolicy::KXorCompositeAssertion);
       
  1254        return &newElement;
       
  1255                                                                                                   
       
  1256 }
       
  1257 
       
  1258 CSenElement* CPolicyNormalizer::AddPolicyElementL(CSenElement* aAssertion)
       
  1259 {
       
  1260      CSenElement& newElement = aAssertion->AddElementL(WSPolicy::KWsPolicyNsUri,
       
  1261                                                  WSPolicy::KWsPolicy);
       
  1262        return &newElement;                                                                                                
       
  1263 }
       
  1264 
       
  1265 
       
  1266 
       
  1267 TBool CPolicyNormalizer::ValidateElementNsL(CSenElement* aAssertion)
       
  1268 {
       
  1269   
       
  1270   CSenElement& element = AsElement();
       
  1271   RPointerArray<CSenNamespace>& namespaces = element.NamespacesL();
       
  1272   
       
  1273   if(aAssertion->NamespaceURI().Length() < 1 )
       
  1274   {
       
  1275   
       
  1276    TPtrC8 elementName = aAssertion->LocalName();
       
  1277    TInt location = elementName.Find(KColon);
       
  1278    
       
  1279    if (location)
       
  1280        {
       
  1281         TPtrC8 nsPrefix = elementName.Left(location);
       
  1282           
       
  1283         CSenNamespace* ns = NULL;
       
  1284         TInt count = namespaces.Count();
       
  1285         for (TInt i=0; i < count; i++)
       
  1286             {
       
  1287             ns = (namespaces)[i];
       
  1288             if(ns->Prefix().Compare(nsPrefix) == 0)
       
  1289                 {
       
  1290                 aAssertion->SetNamespaceL(ns->Prefix(),ns->URI());
       
  1291                 return ETrue;
       
  1292                 }
       
  1293             }
       
  1294         }
       
  1295     }
       
  1296     return EFalse;
       
  1297 }
       
  1298 
       
  1299 
       
  1300 TBool CPolicyNormalizer::CopySenElementWithoutOptionL(CSenElement* aSource,CSenElement* aDestination)
       
  1301 {
       
  1302   
       
  1303   TPtrC8 sourceContent = aSource->Content();
       
  1304   TBool isTrue = EFalse;
       
  1305   if (sourceContent.Length() > 0)
       
  1306       {
       
  1307         aDestination->SetContentL(sourceContent);
       
  1308         
       
  1309       }
       
  1310 
       
  1311     RPointerArray<CSenBaseAttribute> sourceAttributes = aSource->AttributesL();
       
  1312     if (sourceAttributes.Count() > 0)
       
  1313         {
       
  1314         for (TInt i=0;i<sourceAttributes.Count(); i++)
       
  1315             {
       
  1316             CSenBaseAttribute* pBaseAttribute = sourceAttributes[i];
       
  1317             if(pBaseAttribute->Name().Compare(WSPolicy::KWspOptinal) == 0)
       
  1318             {
       
  1319                 if(pBaseAttribute->Value().Compare(WSPolicy::KWspOptinalTrue) == 0)
       
  1320                     isTrue = ETrue;                    
       
  1321             }
       
  1322                 
       
  1323             else
       
  1324                 aDestination->AddAttributeL(pBaseAttribute->Name(),pBaseAttribute->Value());
       
  1325             
       
  1326             }
       
  1327         }
       
  1328 //        sourceAttributes.Close();
       
  1329         return isTrue;  
       
  1330 }
       
  1331 
       
  1332 CSenElement* CPolicyNormalizer::CopySenElementL(CSenElement* aSource, CSenElement* aDestination)
       
  1333 {
       
  1334   TPtrC8 sourceContent = aSource->Content();
       
  1335   if (sourceContent.Length() > 0)
       
  1336       {
       
  1337         aDestination->SetContentL(sourceContent);
       
  1338         
       
  1339       }
       
  1340     aDestination->SetNamespaceL(aSource->NsPrefix(), aSource->NamespaceURI());
       
  1341 
       
  1342     RPointerArray<CSenBaseAttribute> sourceAttributes = aSource->AttributesL();
       
  1343     if (sourceAttributes.Count() > 0)
       
  1344         {
       
  1345         for (TInt i=0;i<sourceAttributes.Count(); i++)
       
  1346             {
       
  1347             CSenBaseAttribute* pBaseAttribute = sourceAttributes[i];
       
  1348 
       
  1349             aDestination->AddAttributeL(pBaseAttribute->Name(),
       
  1350                                         pBaseAttribute->Value());
       
  1351             
       
  1352             }
       
  1353         }
       
  1354 //        sourceAttributes.Close();
       
  1355         return aDestination;
       
  1356 }
       
  1357 
       
  1358 void CPolicyNormalizer::ReadFileL( TFileName aPath )
       
  1359     {
       
  1360     RFs fss;
       
  1361     User::LeaveIfError(fss.Connect());
       
  1362     CleanupClosePushL(fss);
       
  1363      
       
  1364     if(iPath)
       
  1365         {
       
  1366         delete iPath;
       
  1367         iPath = NULL;
       
  1368         }
       
  1369         
       
  1370     iPath = aPath.AllocL();
       
  1371     
       
  1372     TFileName iFileName(aPath);
       
  1373     RFile  xmlFile;
       
  1374     TInt err = xmlFile.Open(fss , iFileName, EFileRead );
       
  1375     
       
  1376     if(err == KErrNone)
       
  1377         {
       
  1378         TInt size;
       
  1379         xmlFile.Size(size);
       
  1380 
       
  1381         HBufC8 *iBuf=HBufC8::NewL(size);
       
  1382 
       
  1383         TPtr8 buf8ptr(iBuf->Des());
       
  1384         xmlFile.Read(buf8ptr,size);
       
  1385         xmlFile.Close();
       
  1386 
       
  1387         TInt leaveCode(KErrNone);
       
  1388         TRAP leaveCode, ParseL(buf8ptr) );
       
  1389         leaveCode = 0; // not used currently
       
  1390         delete iBuf;
       
  1391         iBuf = NULL;
       
  1392         }
       
  1393     CleanupStack::PopAndDestroy(1); // fss
       
  1394     }
       
  1395 
       
  1396 TInt CPolicyNormalizer::ReadL(TDesC8& aXmlDesc)
       
  1397     {
       
  1398     TInt err (KErrNone);
       
  1399     TInt size = aXmlDesc.Length();
       
  1400     if(size > 0)
       
  1401         {
       
  1402         HBufC8 *pBuf= aXmlDesc.AllocL();
       
  1403         CleanupStack::PushL(pBuf);
       
  1404         TPtr8 buf8ptr(pBuf->Des());
       
  1405         
       
  1406         TRAP (err, ParseL(buf8ptr));
       
  1407         
       
  1408         CleanupStack::PopAndDestroy(pBuf);
       
  1409         }
       
  1410         return err;
       
  1411     }
       
  1412 
       
  1413 void CPolicyNormalizer::WriteAllL(CSenElement* /*aXml*/)
       
  1414 {
       
  1415   RFs fss;
       
  1416        User::LeaveIfError(fss.Connect());
       
  1417         CleanupClosePushL(fss);
       
  1418         
       
  1419     TBuf<255> file;
       
  1420     _LIT( KFileName, "c:\\logs\\normalizer%d.xml");
       
  1421     file.Format(KFileName, count);
       
  1422     count++;
       
  1423     TPtrC OutputFile(file);
       
  1424     
       
  1425    TBuf<256> tempp(KOutPath);
       
  1426 //   tempp.Insert(tempp.Length() , iPath->Des());
       
  1427    
       
  1428 //   TFileName iFileName(tempp);
       
  1429    RFile  xmlFile;
       
  1430    xmlFile.Replace(fss , OutputFile, EFileWrite );
       
  1431    HBufC8* temp = iNewPolicy->AsXmlL();
       
  1432    xmlFile.Write(temp->Des());
       
  1433    xmlFile.Close();
       
  1434    delete temp;
       
  1435     
       
  1436    CleanupStack::PopAndDestroy(1); // fss
       
  1437   
       
  1438 }
       
  1439 void CPolicyNormalizer::WriteL(CSenElement* aXml)
       
  1440 {
       
  1441     RFs fss;
       
  1442         User::LeaveIfError(fss.Connect());
       
  1443         CleanupClosePushL(fss);
       
  1444         
       
  1445     TBuf<255> file;
       
  1446     _LIT( KFileName, "c:\\logs\\normalizer%d.xml");
       
  1447     file.Format(KFileName, count);
       
  1448     count++;
       
  1449     TPtrC OutputFile(file);
       
  1450     
       
  1451    TBuf<256> tempp(KOutPath);
       
  1452 //   tempp.Insert(tempp.Length() , iPath->Des());
       
  1453    
       
  1454 //   TFileName iFileName(tempp);
       
  1455    RFile  xmlFile;
       
  1456    xmlFile.Replace(fss , OutputFile, EFileWrite );
       
  1457    HBufC8* temp = aXml->AsXmlL();
       
  1458    xmlFile.Write(temp->Des());
       
  1459    xmlFile.Close();
       
  1460    delete temp;
       
  1461     
       
  1462    CleanupStack::PopAndDestroy(1); // fss
       
  1463    
       
  1464 }
       
  1465 
       
  1466 // End of file