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