webservices/wsstar/wsstarpolicy/src/policyassertion.cpp
changeset 0 62f9d29f7211
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2009 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 
       
    22 
       
    23 
       
    24 #include "policyassertion.h"
       
    25 #include "xorcompositeassertion.h"
       
    26 #include "andcompositeassertion.h"
       
    27 #include "wspolicywriter.h"
       
    28 #include "sendebug.h"
       
    29 CPolicyAssertion* CPolicyAssertion::NewL()
       
    30 {
       
    31     CPolicyAssertion* pSelf = CPolicyAssertion::NewLC();
       
    32     CleanupStack::Pop(pSelf);
       
    33     return pSelf;
       
    34 
       
    35 }
       
    36 CPolicyAssertion* CPolicyAssertion::NewLC()
       
    37 {
       
    38     CPolicyAssertion* pSelf = new (ELeave) CPolicyAssertion();
       
    39     CleanupStack::PushL(pSelf);
       
    40     pSelf->ConstructL();
       
    41     return pSelf;
       
    42 
       
    43 }
       
    44 CPolicyAssertion* CPolicyAssertion::NewL(CPolicyAssertion* aValue)
       
    45 {
       
    46     CPolicyAssertion* pSelf = CPolicyAssertion::NewLC(aValue);
       
    47     CleanupStack::Pop(pSelf);
       
    48     return pSelf;
       
    49     
       
    50 }
       
    51 CPolicyAssertion* CPolicyAssertion::NewLC(CPolicyAssertion* aValue)
       
    52 {
       
    53     CPolicyAssertion* pSelf = new (ELeave) CPolicyAssertion();
       
    54     CleanupStack::PushL(pSelf);
       
    55     pSelf->ConstructL(aValue);
       
    56     return pSelf;    
       
    57 }
       
    58 
       
    59 CPolicyAssertion::~CPolicyAssertion()
       
    60 {
       
    61     delete iName;
       
    62     delete iId;
       
    63     delete iMetadataEndpoint;
       
    64 }
       
    65 CPolicyAssertion::CPolicyAssertion():
       
    66 iName(NULL),
       
    67 iId(NULL),
       
    68 iMetadataEndpoint(NULL)
       
    69 {
       
    70 
       
    71 }
       
    72 void CPolicyAssertion::ConstructL()
       
    73 {
       
    74 
       
    75 }
       
    76 
       
    77 void CPolicyAssertion::ConstructL(CPolicyAssertion* aValue)
       
    78 {
       
    79     ConstructL();
       
    80 
       
    81     RPolicyTerms terms = aValue->GetTerms();
       
    82     
       
    83     if(terms.Count() > 0)
       
    84         AddTermsCopyL(terms);
       
    85     
       
    86 }
       
    87 TAssertionType CPolicyAssertion::Type()
       
    88 {
       
    89     return ECompositePolicyType;
       
    90 }
       
    91 MAssertion* CPolicyAssertion::NormalizeL()
       
    92 {
       
    93     return NormalizeL(NULL);
       
    94 }
       
    95 MAssertion*	CPolicyAssertion::NormalizeL(CPolicyRegistry* aRegistry)
       
    96 {
       
    97     if (IsNormalized()) 
       
    98     {
       
    99     	return this;
       
   100     }
       
   101 //	TPtrC8 xmlBase = Name();
       
   102 //	TPtrC8 id = Id();
       
   103 	CPolicyAssertion* policy = CPolicyAssertion::NewL(); //new Policy(xmlBase, id);
       
   104 
       
   105 	CXorCompositeAssertion* XOR = CXorCompositeAssertion::NewL();
       
   106 	CAndCompositeAssertion* AND = CAndCompositeAssertion::NewL();	
       
   107 	
       
   108     RPolicyTerms childXorTermList;
       
   109     
       
   110 	RPointerArray<MAssertion> terms = GetTerms();
       
   111 	MAssertion* term = NULL;
       
   112 	MAssertion* result = NULL;
       
   113     TInt termCount = terms.Count();
       
   114 	for (TInt i = 0; i< termCount; i++)
       
   115 	{
       
   116 		term = terms[i];
       
   117 		result = term->NormalizeL(aRegistry);
       
   118 
       
   119 		if(dynamic_cast<CPolicyAssertion*>(result))
       
   120 		{
       
   121 			CXorCompositeAssertion* tempXor = (CXorCompositeAssertion*) result->GetTerms()[0];
       
   122 
       
   123 			if (tempXor->Size() != 1)
       
   124 			{
       
   125 			//result is Policy and have an XOR
       
   126 			//delete the policy and put XOR as result
       
   127 //				result = Xor;
       
   128                 
       
   129 			    CPolicyAssertion* ele = (CPolicyAssertion*)result;
       
   130 			    result = NULL;
       
   131 			    result = CXorCompositeAssertion::NewL();
       
   132 			    
       
   133 			    RPolicyTerms xorTerms = tempXor->GetTerms();
       
   134 			    result->AddTermsCopyL(xorTerms);
       
   135 			    
       
   136 			    delete ele;
       
   137 			    ele = NULL;
       
   138             } 
       
   139 			else 
       
   140 			{
       
   141 			    MAssertion* tempTerm = tempXor->GetTerms()[0];
       
   142 			    if(tempTerm)
       
   143 			    {
       
   144                     RPolicyTerms terms2 = tempTerm->GetTerms();
       
   145     				AND->AddTermsCopyL(terms2);
       
   146 //    				terms2.Close();
       
   147 			    }
       
   148     				CPolicyAssertion* eleToDel = (CPolicyAssertion*)result;
       
   149     				delete eleToDel;
       
   150     				eleToDel = NULL;
       
   151     				result = NULL;
       
   152 		
       
   153 				continue;
       
   154 			}
       
   155 		}
       
   156 
       
   157 		if(dynamic_cast<CXorCompositeAssertion*>(result))
       
   158 		{
       
   159 
       
   160 			if (((CXorCompositeAssertion*) result)->IsEmpty())
       
   161 			{
       
   162 //				CXorCompositeAssertion* emptyXor = CXorCompositeAssertion::NewL();
       
   163 //				emptyXor->SetNormalized(ETrue);
       
   164 //                result->AddTermL(AND);
       
   165 				policy->AddTerm(result);
       
   166 				policy->SetNormalized(ETrue);
       
   167                 delete AND;
       
   168                 delete XOR;
       
   169                 childXorTermList.Close();
       
   170 				return policy;
       
   171 			}
       
   172 
       
   173 			childXorTermList.Append(result);
       
   174 			continue;
       
   175 		}
       
   176 
       
   177 		if(dynamic_cast<CAndCompositeAssertion*>(result))
       
   178 		{
       
   179 
       
   180 			if (result->IsEmpty()) 
       
   181 			{
       
   182 				CAndCompositeAssertion* emptyAnd = CAndCompositeAssertion::NewL();
       
   183 				XOR->AddTerm(emptyAnd);
       
   184 
       
   185 			} 
       
   186 			else 
       
   187 			{
       
   188 			    RPolicyTerms terms3 = result->GetTerms();
       
   189 				AND->AddTermsCopyL(terms3);
       
   190 			}
       
   191                 CAndCompositeAssertion* eletoDel = (CAndCompositeAssertion*)result;
       
   192 				delete eletoDel;			
       
   193 			
       
   194 			continue;
       
   195 		}
       
   196 		
       
   197 		AND->AddTerm(result);
       
   198 	}
       
   199 
       
   200 ////////////////////////////////////////////////////	
       
   201 ////////////////////////////////////////////////////
       
   202 	// processing child-XORCompositeAssertions
       
   203 	if (childXorTermList.Count() > 1) {
       
   204 
       
   205 		for (TInt i = 0; i < childXorTermList.Count(); i++) {
       
   206 
       
   207 			for (TInt j = i; j < childXorTermList.Count(); j++) {
       
   208 
       
   209 				if (i != j) {
       
   210 					CXorCompositeAssertion* xorTermA = (CXorCompositeAssertion*) childXorTermList[i];
       
   211 					CXorCompositeAssertion* xorTermB = (CXorCompositeAssertion*) childXorTermList[j];
       
   212 
       
   213 					RPolicyTerms iterA = xorTermA->GetTerms();
       
   214 
       
   215 					for (TInt k = 0; k< iterA.Count(); k++)
       
   216 					{
       
   217 						MAssertion* andTermA = iterA[k];
       
   218                         
       
   219                         RPolicyTerms iterB = xorTermB->GetTerms();
       
   220 
       
   221     					for (TInt l = 0; l< iterB.Count(); l++)
       
   222     					{
       
   223 							MAssertion* andTermB = iterB[l];
       
   224 							CAndCompositeAssertion* anAndTerm = CAndCompositeAssertion::NewL();
       
   225 							RPolicyTerms termsAA = andTermA->GetTerms();
       
   226 							RPolicyTerms termsBB = andTermB->GetTerms();
       
   227 							anAndTerm->AddTermsCopyL(termsAA);
       
   228 							anAndTerm->AddTermsCopyL(termsBB);
       
   229 //							termsBB.Close();
       
   230 //							termsAA.Close();
       
   231 							XOR->AddTerm(anAndTerm);
       
   232 						}
       
   233 //    						iterB.Close();
       
   234 					}
       
   235 //    					iterA.Close();
       
   236 				}
       
   237 			}
       
   238 		}
       
   239 		for (TInt x = 0; x < childXorTermList.Count(); x++) 
       
   240 	    {
       
   241 	        CXorCompositeAssertion* xorTermTemp = (CXorCompositeAssertion*)childXorTermList[x];
       
   242 	        if(xorTermTemp)
       
   243 	            delete xorTermTemp;	        
       
   244 	    }
       
   245 	}
       
   246 	else if (childXorTermList.Count() == 1) 
       
   247 	{
       
   248 		CXorCompositeAssertion* xorTerm = (CXorCompositeAssertion*)childXorTermList[0];
       
   249 		XOR->AddTermsCopyL(xorTerm->GetTerms());
       
   250         delete xorTerm;
       
   251 	}
       
   252 
       
   253 	else if (childXorTermList.Count() == 0)
       
   254 	{
       
   255         if(AND->Size() > 0)
       
   256             XOR->AddTerm(AND);
       
   257         else
       
   258             delete AND;
       
   259         policy->AddTerm(XOR);
       
   260 		policy->SetNormalized(ETrue);
       
   261 		
       
   262         childXorTermList.Close();
       
   263     	return policy;
       
   264 	}
       
   265 
       
   266     childXorTermList.Close();
       
   267 	
       
   268 ////////////////////////////////////////////////////	
       
   269 ////////////////////////////////////////////////////
       
   270 	RPolicyTerms primTerms = AND->GetTerms();
       
   271 	RPolicyTerms andTerms = XOR->GetTerms();
       
   272     for(TInt p = 0; p < andTerms.Count(); p++)
       
   273     {
       
   274 		MAssertion* anAndTerm = andTerms[p];
       
   275 		anAndTerm->AddTermsCopyL(primTerms);
       
   276 	}
       
   277 	
       
   278     delete AND;
       
   279 	policy->AddTerm(XOR);
       
   280 	policy->SetNormalized(ETrue);
       
   281 	
       
   282 	return policy;
       
   283 
       
   284 }         
       
   285 MAssertion* CPolicyAssertion::IntersectL(MAssertion* aAssertion, CPolicyRegistry* aRegistry)
       
   286 {
       
   287 	MAssertion* normalizedMe = (IsNormalized()) ? this : NormalizeL(aRegistry);
       
   288 	if (!(dynamic_cast<CPolicyAssertion*>(normalizedMe))) 
       
   289 	{
       
   290 		return normalizedMe->IntersectL(aAssertion, aRegistry);
       
   291 	}
       
   292 
       
   293 	MAssertion* target = (aAssertion->IsNormalized()) ? aAssertion : aAssertion->NormalizeL(aRegistry);
       
   294 	short type = target->Type();
       
   295 
       
   296 	switch (type) 
       
   297 	{
       
   298 	case ECompositePolicyType: 
       
   299 	{
       
   300 		CPolicyAssertion* nPOLICY = CPolicyAssertion::NewL();
       
   301 		CXorCompositeAssertion* norm1 = (CXorCompositeAssertion*) normalizedMe->GetTerms()[0];
       
   302 		nPOLICY->AddTerm(norm1->IntersectL((CXorCompositeAssertion*) target->GetTerms()[0], NULL));
       
   303 		return nPOLICY;
       
   304 	}
       
   305 	case ECompositeXorType:
       
   306 	case ECompositeAndType:
       
   307 	case EPrimitiveType: 
       
   308 	{
       
   309 		CPolicyAssertion* nPOLICY = CPolicyAssertion::NewL();
       
   310 		nPOLICY->AddTerm((normalizedMe->GetTerms()[0])->IntersectL(target, NULL));
       
   311 		return nPOLICY;
       
   312 	}
       
   313 
       
   314 	default: 
       
   315 	{
       
   316 	}
       
   317 
       
   318 	}
       
   319 return NULL;	
       
   320 }
       
   321 MAssertion* CPolicyAssertion::MergeL(MAssertion* aAssertion, CPolicyRegistry* aRegistry)
       
   322 {
       
   323     MAssertion* normalizedMe = (IsNormalized()) ? this : NormalizeL(aRegistry);
       
   324 
       
   325 	if (!(dynamic_cast<CPolicyAssertion*>(normalizedMe))) 
       
   326 	{
       
   327 		return normalizedMe->MergeL(aAssertion, aRegistry);
       
   328 	}
       
   329 
       
   330 	CPolicyAssertion* nPOLICY = CPolicyAssertion::NewL();
       
   331 
       
   332 	MAssertion* target = (aAssertion->IsNormalized()) ? aAssertion : aAssertion->NormalizeL(aRegistry);
       
   333 	short type = target->Type();
       
   334 
       
   335     RPolicyTerms terms = normalizedMe->GetTerms();
       
   336     if(terms.Count() > 0)
       
   337     {
       
   338         CXorCompositeAssertion* xorTerm = (CXorCompositeAssertion*)terms[0];
       
   339             
       
   340     	switch (type) 
       
   341     	{
       
   342 
       
   343         	case ECompositePolicyType: 
       
   344         	{
       
   345         	    RPolicyTerms tgtTerms = target->GetTerms();
       
   346         	    if(tgtTerms.Count() > 0)
       
   347         	    {
       
   348                     CXorCompositeAssertion* tgtTerm = (CXorCompositeAssertion*)tgtTerms[0];
       
   349                     MAssertion* result = xorTerm->MergeL(tgtTerm, NULL);
       
   350                     nPOLICY->AddTerm(result);
       
   351         	    }
       
   352         		return nPOLICY;
       
   353         	}
       
   354         	case ECompositeXorType:
       
   355         	case ECompositeAndType:
       
   356         	case EPrimitiveType:
       
   357             {
       
   358                 MAssertion* result = xorTerm->MergeL(target, NULL);            
       
   359         		nPOLICY->AddTerm(result);
       
   360         		return nPOLICY;
       
   361         	}
       
   362 
       
   363         	default: 
       
   364         	{
       
   365         	}
       
   366 
       
   367     	}
       
   368     }
       
   369     return NULL;    
       
   370 }
       
   371 
       
   372 void CPolicyAssertion::SetNameL(TDesC8& aName)
       
   373 {
       
   374     iName = aName.AllocL();
       
   375 }          
       
   376 void CPolicyAssertion::SetIdL(TDesC8& aId)
       
   377 {
       
   378     iId = aId.AllocL();
       
   379 }
       
   380 TPtrC8 	CPolicyAssertion::Name()
       
   381 {
       
   382     return iName->Des();
       
   383 }     
       
   384 TPtrC8 	CPolicyAssertion::Id()
       
   385 {
       
   386     return iId->Des();
       
   387 }
       
   388 TPtrC8 CPolicyAssertion::PolicyURI()
       
   389 {
       
   390     return iName->Des();
       
   391 }