|
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 "andcompositeassertion.h" |
|
25 #include "xorcompositeassertion.h" |
|
26 #include "primitiveassertion.h" |
|
27 #include "policyassertion.h" |
|
28 |
|
29 CAndCompositeAssertion* CAndCompositeAssertion::NewL() |
|
30 { |
|
31 CAndCompositeAssertion* pSelf = CAndCompositeAssertion::NewLC(); |
|
32 CleanupStack::Pop(pSelf); |
|
33 return pSelf; |
|
34 |
|
35 } |
|
36 CAndCompositeAssertion* CAndCompositeAssertion::NewLC() |
|
37 { |
|
38 CAndCompositeAssertion* pSelf = new (ELeave) CAndCompositeAssertion(); |
|
39 CleanupStack::PushL(pSelf); |
|
40 pSelf->ConstructL(); |
|
41 return pSelf; |
|
42 |
|
43 } |
|
44 CAndCompositeAssertion* CAndCompositeAssertion::NewL(CAndCompositeAssertion* aValue) |
|
45 { |
|
46 CAndCompositeAssertion* pSelf = CAndCompositeAssertion::NewLC(aValue); |
|
47 CleanupStack::Pop(pSelf); |
|
48 return pSelf; |
|
49 |
|
50 } |
|
51 CAndCompositeAssertion* CAndCompositeAssertion::NewLC(CAndCompositeAssertion* aValue) |
|
52 { |
|
53 CAndCompositeAssertion* pSelf = new (ELeave) CAndCompositeAssertion(); |
|
54 CleanupStack::PushL(pSelf); |
|
55 pSelf->ConstructL(aValue); |
|
56 return pSelf; |
|
57 |
|
58 } |
|
59 |
|
60 CAndCompositeAssertion::~CAndCompositeAssertion() |
|
61 { |
|
62 |
|
63 } |
|
64 CAndCompositeAssertion::CAndCompositeAssertion() |
|
65 { |
|
66 |
|
67 } |
|
68 void CAndCompositeAssertion::ConstructL() |
|
69 { |
|
70 |
|
71 } |
|
72 void CAndCompositeAssertion::ConstructL(CAndCompositeAssertion* aValue) |
|
73 { |
|
74 ConstructL(); |
|
75 |
|
76 RPolicyTerms terms = aValue->GetTerms(); |
|
77 |
|
78 if(terms.Count() > 0) |
|
79 AddTermsCopyL(terms); |
|
80 |
|
81 // terms.Close(); |
|
82 } |
|
83 |
|
84 TAssertionType CAndCompositeAssertion::Type() |
|
85 { |
|
86 return ECompositeAndType; |
|
87 } |
|
88 void CAndCompositeAssertion::AddTerm(MAssertion* aAssertion) |
|
89 { |
|
90 if ((IsNormalized() && (aAssertion->Type() == EPrimitiveType))) |
|
91 { |
|
92 SetNormalized(EFalse); |
|
93 } |
|
94 |
|
95 CAssertion::AddTerm(aAssertion); |
|
96 } |
|
97 void CAndCompositeAssertion::AddTermCopyL(MAssertion* aAssertion) |
|
98 { |
|
99 if ((IsNormalized() && (aAssertion->Type() == EPrimitiveType))) |
|
100 { |
|
101 SetNormalized(EFalse); |
|
102 } |
|
103 |
|
104 CAssertion::AddTermCopyL(aAssertion); |
|
105 } |
|
106 MAssertion* CAndCompositeAssertion::NormalizeL(CPolicyRegistry* aRegistry){ |
|
107 |
|
108 if (IsNormalized()) |
|
109 { |
|
110 return this; |
|
111 } |
|
112 |
|
113 CAndCompositeAssertion* AND = CAndCompositeAssertion::NewL(); |
|
114 |
|
115 if (IsEmpty()) |
|
116 { |
|
117 AND->SetNormalized(ETrue); |
|
118 return AND; |
|
119 } |
|
120 |
|
121 ///////////////////////////////////////////////////////////// |
|
122 ///////////////////////////////////////////////////////////// |
|
123 RPolicyTerms XORs; |
|
124 |
|
125 RPointerArray<MAssertion> terms1 = GetTerms(); |
|
126 MAssertion* term = NULL; |
|
127 TInt termCount = terms1.Count(); |
|
128 |
|
129 for (TInt i = 0; i< termCount; i++) |
|
130 { |
|
131 |
|
132 term = terms1[i]; |
|
133 MAssertion* result = term; |
|
134 if(!dynamic_cast<CPolicyAssertion*>(term)) |
|
135 { |
|
136 //term has new value object, no need to copy |
|
137 result = term->NormalizeL(aRegistry); |
|
138 } |
|
139 |
|
140 if (dynamic_cast<CPolicyAssertion*>(result)) |
|
141 { |
|
142 CAndCompositeAssertion* wrapper = CAndCompositeAssertion::NewL(); |
|
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 |
|
152 result = NULL; |
|
153 //term has new value object, no need to copy |
|
154 result = wrapper->NormalizeL(aRegistry); |
|
155 delete wrapper; |
|
156 // termsPol1.Close(); //not needed any more |
|
157 } |
|
158 |
|
159 if (dynamic_cast<CXorCompositeAssertion*>(result)) |
|
160 { |
|
161 |
|
162 if (result->IsEmpty()) |
|
163 { |
|
164 result->SetNormalized(true); |
|
165 delete AND; |
|
166 return result; |
|
167 } |
|
168 XORs.Append(result); |
|
169 continue; |
|
170 |
|
171 } |
|
172 |
|
173 if (dynamic_cast<CAndCompositeAssertion*>(result)) |
|
174 { |
|
175 if (result->IsEmpty()) |
|
176 { |
|
177 CAndCompositeAssertion* eleToDel = (CAndCompositeAssertion*)result; |
|
178 delete eleToDel; |
|
179 } |
|
180 else |
|
181 { |
|
182 RPolicyTerms termsPol2 = result->GetTerms(); |
|
183 AND->AddTermsCopyL(termsPol2); |
|
184 |
|
185 CAndCompositeAssertion* eleToDel = (CAndCompositeAssertion*)result; |
|
186 delete eleToDel; |
|
187 |
|
188 // termsPol2.Close(); |
|
189 } |
|
190 continue; |
|
191 } |
|
192 |
|
193 AND->AddTerm(result); |
|
194 } |
|
195 |
|
196 // processing child-XORCompositeAssertions |
|
197 |
|
198 ///////////////////////////////////////////////////////////// |
|
199 ///////////////////////////////////////////////////////////// |
|
200 |
|
201 CXorCompositeAssertion* XOR = CXorCompositeAssertion::NewL(); |
|
202 |
|
203 if (XORs.Count() > 1) |
|
204 { |
|
205 for (int i = 0; i < XORs.Count(); i++) |
|
206 { |
|
207 |
|
208 for (int j = i; j < XORs.Count(); j++) |
|
209 { |
|
210 |
|
211 if (i != j) |
|
212 { |
|
213 CXorCompositeAssertion* XOR_A = (CXorCompositeAssertion*) XORs[i]; |
|
214 CXorCompositeAssertion* XOR_B = (CXorCompositeAssertion*) XORs[j]; |
|
215 |
|
216 RPolicyTerms iterA = XOR_A->GetTerms(); |
|
217 for (TInt k = 0; k< iterA.Count(); k++) |
|
218 { |
|
219 MAssertion* anAND_A = iterA[k]; |
|
220 RPolicyTerms iterB = XOR_B->GetTerms(); |
|
221 |
|
222 for (TInt l = 0; l< iterB.Count(); l++) |
|
223 { |
|
224 |
|
225 MAssertion* anAND_B = iterB[l]; |
|
226 CAndCompositeAssertion* nAND = CAndCompositeAssertion::NewL(); |
|
227 RPolicyTerms termsAA = anAND_A->GetTerms(); |
|
228 RPolicyTerms termsBB = anAND_B->GetTerms(); |
|
229 nAND->AddTermsCopyL(termsAA); |
|
230 nAND->AddTermsCopyL(termsBB); |
|
231 // termsAA.Close(); |
|
232 // termsBB.Close(); |
|
233 XOR->AddTerm(nAND); |
|
234 } |
|
235 // iterB.Close(); |
|
236 } |
|
237 // iterA.Close(); |
|
238 } |
|
239 } |
|
240 } |
|
241 |
|
242 } |
|
243 else if (XORs.Count() == 1) |
|
244 { |
|
245 CXorCompositeAssertion* XORterm = (CXorCompositeAssertion*)XORs[0]; |
|
246 RPolicyTerms termsPol3 = XORterm->GetTerms(); |
|
247 XOR->AddTermsCopyL(termsPol3); |
|
248 delete XORterm; |
|
249 // termsPol3.Close(); |
|
250 } |
|
251 |
|
252 XORs.Close(); |
|
253 |
|
254 ///////////////////////////////////////////////////////////// |
|
255 ///////////////////////////////////////////////////////////// |
|
256 |
|
257 if (XOR->IsEmpty()) |
|
258 { |
|
259 delete XOR; |
|
260 AND->SetNormalized(true); |
|
261 return AND; |
|
262 } |
|
263 |
|
264 if (AND->IsEmpty()) |
|
265 { |
|
266 delete AND; |
|
267 XOR->SetNormalized(true); |
|
268 return XOR; |
|
269 } |
|
270 |
|
271 ///////////////////////////////////////////////////////////// |
|
272 ///////////////////////////////////////////////////////////// |
|
273 |
|
274 RPolicyTerms primTerms = AND->GetTerms(); |
|
275 RPolicyTerms xTerms = XOR->GetTerms(); |
|
276 |
|
277 for (TInt x = 0; x< xTerms.Count(); x++) |
|
278 { |
|
279 MAssertion* rAND = xTerms[x]; |
|
280 rAND->AddTermsCopyL(primTerms); |
|
281 } |
|
282 |
|
283 delete AND; //we have copied everythign from AND so now we dont need it |
|
284 |
|
285 XOR->SetNormalized(ETrue); |
|
286 return XOR; |
|
287 |
|
288 } |
|
289 MAssertion* CAndCompositeAssertion::IntersectL(MAssertion* aAssertion, CPolicyRegistry* aRegistry) |
|
290 { |
|
291 MAssertion* normalizedMe = ((IsNormalized()) ? this : NormalizeL(aRegistry)); |
|
292 |
|
293 if (!(dynamic_cast<CAndCompositeAssertion*>(normalizedMe))) |
|
294 { |
|
295 return normalizedMe->IntersectL(aAssertion, aRegistry); |
|
296 } |
|
297 |
|
298 MAssertion* target = (aAssertion->IsNormalized()) ? aAssertion : aAssertion->NormalizeL(aRegistry); |
|
299 short type = target->Type(); |
|
300 |
|
301 switch (type) |
|
302 { |
|
303 |
|
304 case ECompositePolicyType: |
|
305 { |
|
306 CPolicyAssertion* nPOLICY = CPolicyAssertion::NewL(); |
|
307 nPOLICY->AddTerm(normalizedMe->IntersectL(target->GetTerms()[0], NULL)); |
|
308 return nPOLICY; |
|
309 } |
|
310 |
|
311 case ECompositeXorType: |
|
312 { |
|
313 CXorCompositeAssertion* nXOR = CXorCompositeAssertion::NewL(); |
|
314 RPointerArray<MAssertion> terms = target->GetTerms(); |
|
315 |
|
316 TInt termCount = terms.Count(); |
|
317 |
|
318 for (TInt i = 0; i< termCount; i++) |
|
319 { |
|
320 MAssertion* asser = normalizedMe->IntersectL(terms[i]); |
|
321 |
|
322 if (asser->Type() == ECompositeAndType) |
|
323 { |
|
324 nXOR->AddTerm(asser); |
|
325 } |
|
326 else |
|
327 { |
|
328 DeleteAssertion(asser); |
|
329 } |
|
330 |
|
331 } |
|
332 return nXOR; |
|
333 } |
|
334 |
|
335 case ECompositeAndType: |
|
336 { |
|
337 RPolicyTerms PRIMITIVES_A = ((normalizedMe->Size() > target->Size()) ? normalizedMe->GetTerms() : target->GetTerms()); |
|
338 RPolicyTerms PRIMITIVES_B = ((normalizedMe->Size() > target->Size()) ? target->GetTerms() : normalizedMe->GetTerms()); |
|
339 |
|
340 CPrimitiveAssertion* PRIMITIVE_A = NULL; |
|
341 CPrimitiveAssertion* PRIMITIVE_B = NULL; |
|
342 |
|
343 for (int i = 0; i < PRIMITIVES_A.Count(); i++) |
|
344 { |
|
345 PRIMITIVE_A = (CPrimitiveAssertion*) PRIMITIVES_A[i]; |
|
346 |
|
347 TBool flag = false; |
|
348 |
|
349 for (int j = 0; j < PRIMITIVES_B.Count(); j++) |
|
350 { |
|
351 PRIMITIVE_B = (CPrimitiveAssertion*) PRIMITIVES_B[j]; |
|
352 |
|
353 if (PRIMITIVE_A->Name().Compare(PRIMITIVE_B->Name()) == 0) |
|
354 { |
|
355 flag = true; |
|
356 break; |
|
357 } |
|
358 |
|
359 } |
|
360 |
|
361 if (!flag) |
|
362 { |
|
363 return CXorCompositeAssertion::NewL(); |
|
364 } |
|
365 |
|
366 MAssertion* a = PRIMITIVE_A->IntersectL(PRIMITIVE_B); |
|
367 |
|
368 if (dynamic_cast<CXorCompositeAssertion*>(a)) |
|
369 { |
|
370 DeleteAssertion(a); |
|
371 return CXorCompositeAssertion::NewL(); |
|
372 } |
|
373 else |
|
374 { |
|
375 DeleteAssertion(a); |
|
376 } |
|
377 |
|
378 } |
|
379 CAndCompositeAssertion* result = CAndCompositeAssertion::NewL(); |
|
380 result->AddTermsCopyL(PRIMITIVES_A); |
|
381 result->AddTermsCopyL(PRIMITIVES_B); |
|
382 return result; |
|
383 } |
|
384 |
|
385 case EPrimitiveType: |
|
386 { |
|
387 CQName* name = ((CPrimitiveAssertion*) target)->QName(); |
|
388 TBool isMatch = false; |
|
389 |
|
390 CQName* targetName = NULL; |
|
391 RPolicyTerms terms = normalizedMe->GetTerms(); |
|
392 for (TInt j = 0; j < terms.Count(); j++) |
|
393 { |
|
394 targetName = ((CPrimitiveAssertion*) terms[j])->QName(); |
|
395 |
|
396 if (name->Uri().Compare(targetName->Uri()) == 0) |
|
397 { |
|
398 isMatch = true; |
|
399 break; |
|
400 } |
|
401 } |
|
402 |
|
403 if (isMatch) { |
|
404 CAndCompositeAssertion* nAND = CAndCompositeAssertion::NewL(); |
|
405 RPolicyTerms tgtTerm = normalizedMe->GetTerms(); |
|
406 nAND->AddTerms(tgtTerm); |
|
407 nAND->AddTerm(target); |
|
408 return nAND; |
|
409 } |
|
410 |
|
411 return CXorCompositeAssertion::NewL(); |
|
412 } |
|
413 |
|
414 default: |
|
415 { |
|
416 } |
|
417 |
|
418 } |
|
419 return NULL; |
|
420 } |
|
421 MAssertion* CAndCompositeAssertion::MergeL(MAssertion* aAssertion, CPolicyRegistry* aRegistry) |
|
422 { |
|
423 MAssertion* normalizedMe = (IsNormalized()) ? this : NormalizeL(aRegistry); |
|
424 |
|
425 if (!(dynamic_cast<CAndCompositeAssertion*>(normalizedMe))) |
|
426 { |
|
427 return normalizedMe->MergeL(aAssertion, aRegistry); |
|
428 } |
|
429 |
|
430 MAssertion* target = (aAssertion->IsNormalized()) ? aAssertion : aAssertion->NormalizeL(aRegistry); |
|
431 |
|
432 switch (target->Type()) |
|
433 { |
|
434 |
|
435 case ECompositePolicyType: |
|
436 { |
|
437 CPolicyAssertion* nPOLICY = CPolicyAssertion::NewL(); |
|
438 RPolicyTerms xTerms = target->GetTerms(); |
|
439 if(xTerms.Count() > 0) |
|
440 { |
|
441 CXorCompositeAssertion* term = (CXorCompositeAssertion*)xTerms[0]; |
|
442 nPOLICY->AddTerm(normalizedMe->MergeL(term)); |
|
443 } |
|
444 return nPOLICY; |
|
445 } |
|
446 |
|
447 case ECompositeXorType: |
|
448 { |
|
449 |
|
450 CXorCompositeAssertion* nXOR = CXorCompositeAssertion::NewL(); |
|
451 RPolicyTerms xTerms = target->GetTerms(); |
|
452 for (TInt i=0; i< xTerms.Count(); i++) |
|
453 { |
|
454 CAndCompositeAssertion* AND = (CAndCompositeAssertion*) xTerms[i]; |
|
455 nXOR->AddTerm(normalizedMe->MergeL(AND)); |
|
456 } |
|
457 |
|
458 return nXOR; |
|
459 } |
|
460 |
|
461 case ECompositeAndType: |
|
462 { |
|
463 CAndCompositeAssertion* nAND = CAndCompositeAssertion::NewL(); |
|
464 RPolicyTerms terms = normalizedMe->GetTerms(); |
|
465 nAND->AddTermsCopyL(terms); |
|
466 RPolicyTerms targeterms = target->GetTerms(); |
|
467 nAND->AddTermsCopyL(targeterms); |
|
468 |
|
469 return nAND; |
|
470 } |
|
471 |
|
472 case EPrimitiveType: |
|
473 { |
|
474 CAndCompositeAssertion* nAND = CAndCompositeAssertion::NewL(); |
|
475 RPolicyTerms terms = normalizedMe->GetTerms(); |
|
476 nAND->AddTermsCopyL(terms); |
|
477 nAND->AddTermCopyL(target); |
|
478 |
|
479 return nAND; |
|
480 } |
|
481 |
|
482 } |
|
483 |
|
484 return NULL; |
|
485 } |
|
486 |