webservices/wsstar/wsstarpolicy/src/wspolicy.cpp
changeset 0 62f9d29f7211
child 1 272b002df977
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 
       
    22 
       
    23 
       
    24 
       
    25 // INCLUDE FILES
       
    26 #include "wspolicy.h"
       
    27 #include <f32file.h>
       
    28 #include <xmlengdom.h>
       
    29 #include <SenXmlUtils.h>
       
    30 #include "senguidgen.h"
       
    31 #include "SenXmlElement.h"
       
    32 #include "SenDateUtils.h"
       
    33 #include "wspolicywriter.h"
       
    34 #include "senxmldebug.h"
       
    35 
       
    36 using namespace WSPolicy;
       
    37 namespace
       
    38 {
       
    39     _LIT8(KIntersect, " intersect ");
       
    40     _LIT8(KMerge, " merge ");
       
    41     _LIT8(KValidUntil, "ValidUntil");
       
    42     const TUint KValidityTime = +7;
       
    43     const TUint KFlatBufSize = 128;
       
    44 }
       
    45 
       
    46 EXPORT_C CWSPolicy* CWSPolicy::NewL()
       
    47     {
       
    48     CWSPolicy* pSelf = CWSPolicy::NewLC();
       
    49     CleanupStack::Pop(pSelf);
       
    50     return pSelf;
       
    51     }
       
    52 
       
    53 EXPORT_C CWSPolicy* CWSPolicy::NewLC()
       
    54     {
       
    55     CWSPolicy* pSelf = new (ELeave) CWSPolicy();
       
    56     CleanupStack::PushL(pSelf);
       
    57     pSelf->ConstructL();
       
    58     return pSelf;
       
    59     }
       
    60 
       
    61 EXPORT_C CWSPolicy* CWSPolicy::NewL(TDesC8& aPolicyUri, TDesC8& aPolicy)
       
    62     {
       
    63     CWSPolicy* pSelf = CWSPolicy::NewLC(aPolicyUri, aPolicy);
       
    64     CleanupStack::Pop(pSelf);
       
    65     return pSelf;
       
    66     }
       
    67     
       
    68 EXPORT_C CWSPolicy* CWSPolicy::NewLC(TDesC8& aPolicyUri, TDesC8& aPolicy)
       
    69     {
       
    70     CWSPolicy* pSelf = new (ELeave) CWSPolicy();
       
    71     CleanupStack::PushL(pSelf);
       
    72     pSelf->BaseConstructL(aPolicyUri, aPolicy);
       
    73     return pSelf;
       
    74     }
       
    75 
       
    76 EXPORT_C CWSPolicy* CWSPolicy::NewL(CSenElement* aPolicy)
       
    77     {
       
    78      CWSPolicy* pSelf = CWSPolicy::NewLC(aPolicy);
       
    79     CleanupStack::Pop(pSelf);
       
    80     return pSelf;
       
    81     }
       
    82     
       
    83 EXPORT_C CWSPolicy* CWSPolicy::NewLC(CSenElement* aPolicy)
       
    84     {
       
    85     CWSPolicy* pSelf = new (ELeave) CWSPolicy();
       
    86     CleanupStack::PushL(pSelf);
       
    87     pSelf->BaseConstructL(aPolicy);
       
    88     return pSelf;   
       
    89     }
       
    90 CWSPolicy::~CWSPolicy()
       
    91     {
       
    92     delete iReader;
       
    93     delete iWriter;
       
    94     delete iInDoc;
       
    95     delete iOutDoc;
       
    96     delete iNormaPolicyAssert; //owned
       
    97     delete iPolicyId;
       
    98     delete iPolicyName;
       
    99     delete iPolicyUri;
       
   100     }
       
   101 
       
   102 EXPORT_C CWSPolicy::CWSPolicy()
       
   103 : iNormPolicy(NULL),
       
   104 iNormaPolicyAssert(NULL),
       
   105 iNormalized(EFalse),
       
   106 iExpired(ETrue)
       
   107 
       
   108     {
       
   109     }
       
   110     
       
   111 EXPORT_C void CWSPolicy::ConstructL()
       
   112     {
       
   113     iReader = CWSPolicyReader::NewL();
       
   114     iWriter = CWSPolicyWriter::NewL();
       
   115     iInDoc= CMyXml::NewL();
       
   116     iOutDoc = CMyXml::NewL();
       
   117     iNormPolicy = NULL;
       
   118     count = 0;
       
   119 
       
   120     TTime now;
       
   121     iExpiry.UniversalTime();
       
   122     TTimeIntervalDays interval(KValidityTime);
       
   123     iExpiry+=interval;
       
   124     }
       
   125     
       
   126 EXPORT_C TInt CWSPolicy::SetPolicyL(TDesC8& aPolicyUri, TDesC8& aPolicy)
       
   127     {
       
   128     TRAPD (error , BaseConstructL(aPolicyUri, aPolicy));
       
   129     return error;
       
   130     }
       
   131     
       
   132 void CWSPolicy::BaseConstructL(TDesC8& aPolicyUri, TDesC8& aPolicy)
       
   133     {
       
   134     ConstructL();
       
   135 
       
   136     if(aPolicyUri.Length() > 0 && aPolicy.Length() > 0)
       
   137     {
       
   138         TInt error = iInDoc->ReadFromDescL(aPolicy);
       
   139         if(error != KErrNone)
       
   140             return;
       
   141 
       
   142         
       
   143         CSenElement* policy = iInDoc->XMLDocL();
       
   144         if(policy)
       
   145         {
       
   146             
       
   147     //        CSenElement* policy = Policy();
       
   148             TPtrC8 policyUri = CWSPolicyUtils::PolicyUriL(policy);
       
   149 
       
   150             if(policyUri == KNullDesC8())
       
   151             {
       
   152                 iPolicyUri = aPolicyUri.AllocL();            
       
   153                 AddPolicyMetadatEndpointL(policy);
       
   154             }
       
   155             else
       
   156             {
       
   157                 iPolicyUri = policyUri.AllocL();
       
   158             }
       
   159             
       
   160 
       
   161             TPtrC8 name = CWSPolicyUtils::PolicyNameL(policy);
       
   162             if(name == KNullDesC8())
       
   163             {
       
   164                 iPolicyName = aPolicyUri.AllocL();
       
   165                 AddPolicyNameL(policy);    
       
   166                 
       
   167             }
       
   168             else
       
   169             {
       
   170                 iPolicyName = name.AllocL();
       
   171             }
       
   172             
       
   173             TPtrC8 id = PolicyIdL(policy);
       
   174             iPolicyId = id.AllocL();
       
   175             
       
   176             
       
   177             CSenElement* expiry = GetExpiryTagL();	//codescannerwarnings 
       
   178             if(EvaluateExpiryL(expiry))	//codescannerwarnings 
       
   179             {
       
   180                AddPolicyValidityL(policy);
       
   181             }
       
   182             else
       
   183             {
       
   184                 AddPolicyValidityL(policy);
       
   185             }
       
   186             //We have modifed the original XMLElement. Now its safe to 
       
   187             //convert this element into PolicyAssertion
       
   188         }        
       
   189     }
       
   190     }
       
   191 
       
   192 void CWSPolicy::BaseConstructL(CSenElement* aPolicy)
       
   193     {
       
   194     if(aPolicy)
       
   195         {
       
   196         TPtrC8 name = CWSPolicyUtils::PolicyNameL(aPolicy);
       
   197         TPtrC8 uri = CWSPolicyUtils::PolicyUriL(aPolicy);
       
   198         TPtrC8 id = PolicyIdL(aPolicy);
       
   199 
       
   200         if(id != KNullDesC8())
       
   201             iPolicyId = id.AllocL();
       
   202         
       
   203         HBufC8* policy = aPolicy->AsXmlL();
       
   204         CleanupStack::PushL(policy);
       
   205         BaseConstructL(uri, *policy);
       
   206         CleanupStack::PopAndDestroy(policy);
       
   207         }
       
   208     }
       
   209     
       
   210 EXPORT_C TBool CWSPolicy::Normalized()
       
   211     { 
       
   212     return iNormalized;
       
   213     }
       
   214 //returns the descriptor of NON-NORMALIZED/NORMALIZED Policy
       
   215 EXPORT_C HBufC8* CWSPolicy::PolicyAsXmlL()
       
   216     {
       
   217     CSenElement* policy = iOutDoc->XMLDocL();
       
   218     if(policy && Normalized())
       
   219     {
       
   220         return policy->AsXmlL();
       
   221     }
       
   222     else
       
   223     {
       
   224         policy = iInDoc->XMLDocL();
       
   225         if(policy)
       
   226         {
       
   227             return policy->AsXmlL();
       
   228         }
       
   229     }
       
   230     return NULL;    
       
   231     }
       
   232 //returns the xml element of NON-NORMALIZED/NORMALIZED Policy    
       
   233 EXPORT_C    CSenElement* CWSPolicy::PolicyL()	//codescannerwarnings
       
   234     {
       
   235     CSenElement* policy = iOutDoc->XMLDocL();
       
   236     if(policy && Normalized())
       
   237     {
       
   238         return policy;
       
   239     }
       
   240     else
       
   241     {
       
   242         policy = iInDoc->XMLDocL();
       
   243         if(policy)
       
   244         {
       
   245             return policy;
       
   246         }
       
   247     }
       
   248     return NULL;    
       
   249     }
       
   250 EXPORT_C CSenElement* CWSPolicy::NonNormalizedPolicyL()	//codescannerwarnings
       
   251 {
       
   252     if(iInDoc)
       
   253     {
       
   254         CSenElement* policy = iInDoc->XMLDocL();
       
   255         if(policy)
       
   256         {
       
   257             return policy;
       
   258         }
       
   259     }
       
   260     return NULL;    
       
   261 }
       
   262 MAssertion* CWSPolicy::NormaAssertion()
       
   263 {
       
   264     if(iNormaPolicyAssert)
       
   265         return iNormaPolicyAssert;
       
   266     else
       
   267         return NULL;
       
   268 }
       
   269 EXPORT_C TBool CWSPolicy::NormalizePolicyL(CPolicyRegistry* aRegistry)
       
   270     {
       
   271     TBool retval = EFalse;
       
   272     TRAPD(error, retval = NormalizePL(aRegistry));
       
   273     if(error ==KErrNone)
       
   274         return retval;
       
   275     else
       
   276         return EFalse;
       
   277     }
       
   278 TInt CWSPolicy::NormalizePL(CPolicyRegistry* aRegistry)
       
   279     {
       
   280         CPolicyAssertion* poolicy = iReader->GetPolicyL(PolicyL());	//codescannerwarnings
       
   281     
       
   282     if(!poolicy)
       
   283         {
       
   284         return EFalse;
       
   285         }
       
   286     if(poolicy->IsNormalized())
       
   287         {
       
   288         return iNormalized;
       
   289         }
       
   290     TInt error = KErrNone;    
       
   291     
       
   292     MAssertion* normalized = NULL;
       
   293     TRAP(error, normalized = poolicy->NormalizeL(aRegistry));
       
   294 	if(error == KErrNone && normalized)
       
   295 	    {
       
   296         iNormalized = ETrue;
       
   297         iNormaPolicyAssert = (CPolicyAssertion*)normalized;
       
   298         delete poolicy;
       
   299 //        delete iInDoc;
       
   300 //        iInDoc = NULL;
       
   301 
       
   302         CBufFlat *pBuf = CBufFlat::NewL(KFlatBufSize);
       
   303         CleanupStack::PushL(pBuf);
       
   304         TPtrC8 p = iWriter->WriteToBufL(*pBuf, iNormaPolicyAssert);
       
   305         TInt error = iOutDoc->ReadFromDescL(p);
       
   306         CleanupStack::PopAndDestroy(pBuf);
       
   307 
       
   308         if(error == KErrNone)
       
   309             {
       
   310             iNormPolicy = iOutDoc->XMLDocL();
       
   311             if(iNormPolicy)
       
   312                 {
       
   313                 TPtrC8 policyUri = CWSPolicyUtils::PolicyUriL(iNormPolicy);
       
   314                 if(policyUri == KNullDesC8())
       
   315                     {
       
   316                     AddPolicyMetadatEndpointL(iNormPolicy);
       
   317                     }
       
   318 
       
   319                 TPtrC8 name = CWSPolicyUtils::PolicyNameL(iNormPolicy);
       
   320                 if(name == KNullDesC8())
       
   321                     {
       
   322                     AddPolicyNameL(iNormPolicy); 
       
   323                     AddPolicyIdL(iNormPolicy);
       
   324                     
       
   325                     }
       
   326                 }
       
   327             }
       
   328         else
       
   329             {
       
   330             iNormPolicy = NULL;
       
   331             }
       
   332             
       
   333 
       
   334 	    return ETrue;
       
   335 	    }
       
   336 	else
       
   337 	    {
       
   338 	    return EFalse;      
       
   339 	    }
       
   340     
       
   341     }
       
   342     
       
   343 EXPORT_C CWSPolicy* CWSPolicy::IntersectPolicyL(CWSPolicy* aPolicy)
       
   344     {
       
   345     CWSPolicy* intersectedPolicy = NULL; 
       
   346     TRAPD(error, intersectedPolicy = IntersectPL(aPolicy));	//codescannerwarnings
       
   347     if(error ==KErrNone)    
       
   348         return intersectedPolicy;
       
   349     else
       
   350         {
       
   351         delete intersectedPolicy;
       
   352         return NULL;
       
   353         }
       
   354     
       
   355     }
       
   356 CWSPolicy* CWSPolicy::IntersectPL(CWSPolicy* aPolicy)	//codescannerwarnings
       
   357     {
       
   358     MAssertion* pol1 = NormaAssertion();
       
   359     MAssertion* pol2 = aPolicy->NormaAssertion();
       
   360 
       
   361 //Only normalized policies can be intersected so if we provide a non normalized policy we will get NULL    
       
   362     if(pol1->IsNormalized() && pol2->IsNormalized())    
       
   363     {
       
   364         MAssertion* result = pol1->IntersectL(pol2);
       
   365 
       
   366         if(result)
       
   367         {
       
   368             CPolicyAssertion* intersected = (CPolicyAssertion*)result;
       
   369             
       
   370             CBufFlat *pBuf = CBufFlat::NewL(KFlatBufSize);
       
   371             CleanupStack::PushL(pBuf);
       
   372             TPtrC8 p = iWriter->WriteToBufL(*pBuf, intersected);
       
   373             delete intersected; //we dont need it any more
       
   374             CWSPolicy* intersectedPolicy = NULL;            
       
   375             if(p.Length() > 0)
       
   376             {
       
   377                 TPtrC8 policy2Uri = CWSPolicyUtils::PolicyUriL(aPolicy->PolicyL());  //codescannerwarnings
       
   378                 HBufC8* nameBuf = HBufC8::NewLC(iPolicyUri->Length()+ policy2Uri.Length()+11);
       
   379                 TPtr8 name = nameBuf->Des();
       
   380                 name.Append(iPolicyUri->Des());
       
   381                 name.Append(KIntersect);
       
   382                 name.Append(policy2Uri);
       
   383             
       
   384                 intersectedPolicy = CWSPolicy::NewL(name, p);
       
   385                 CleanupStack::PopAndDestroy(nameBuf);
       
   386                 if(intersectedPolicy)
       
   387                 {
       
   388                     if(intersectedPolicy->NormalizePolicyL(NULL))
       
   389                     {
       
   390                         //return intersectedPolicy;
       
   391                     }
       
   392                     else //cannot normalize intersected so return NULL
       
   393                     {
       
   394                         delete intersectedPolicy;
       
   395                         intersectedPolicy = NULL;
       
   396                     }
       
   397                 }
       
   398                 else
       
   399                 {
       
   400                     delete intersectedPolicy;
       
   401                     intersectedPolicy = NULL;
       
   402                 }
       
   403                 
       
   404             }
       
   405             
       
   406             CleanupStack::PopAndDestroy(pBuf);
       
   407             return intersectedPolicy;
       
   408         }
       
   409     }
       
   410     
       
   411     return NULL;
       
   412     }
       
   413 
       
   414 
       
   415 EXPORT_C CWSPolicy* CWSPolicy::MergePolicyL(CWSPolicy* aPolicy)
       
   416     {
       
   417     CWSPolicy* mergedPolicy = NULL; 
       
   418     TRAPD(error, mergedPolicy = MergePL(aPolicy));
       
   419     if(error ==KErrNone)    
       
   420         return mergedPolicy;
       
   421     else
       
   422         {
       
   423         delete mergedPolicy;
       
   424         return NULL;
       
   425         }
       
   426 
       
   427     }
       
   428 
       
   429 CWSPolicy* CWSPolicy::MergePL(CWSPolicy* aPolicy)
       
   430     {
       
   431     MAssertion* pol1 = NormaAssertion();
       
   432     MAssertion* pol2 = aPolicy->NormaAssertion();
       
   433 
       
   434 //Only normalized policies can be merged so if we provide a non normalized policy we will get NULL    
       
   435     if(pol1->IsNormalized() && pol2->IsNormalized())    
       
   436     {
       
   437         MAssertion* result = pol1->MergeL(pol2);
       
   438   
       
   439         if(result)
       
   440         {
       
   441             CPolicyAssertion* merged = (CPolicyAssertion*)result;
       
   442 
       
   443             CBufFlat *pBuf = CBufFlat::NewL(KFlatBufSize);
       
   444             CleanupStack::PushL(pBuf);
       
   445             TPtrC8 p = iWriter->WriteToBufL(*pBuf, merged);
       
   446             delete merged; //we dont need it any more
       
   447             CWSPolicy* mergedPolicy = NULL;            
       
   448             if(p.Length() > 0)
       
   449             {
       
   450                 TPtrC8 policy2Uri = CWSPolicyUtils::PolicyUriL(aPolicy->PolicyL());  //codescannerwarnings
       
   451                 HBufC8* nameBuf = HBufC8::NewLC(iPolicyUri->Length()+ policy2Uri.Length()+7);
       
   452                 TPtr8 name = nameBuf->Des();
       
   453                 name.Append(iPolicyUri->Des());
       
   454                 name.Append(KMerge);
       
   455                 name.Append(policy2Uri);
       
   456             
       
   457                 mergedPolicy = CWSPolicy::NewL(name, p);
       
   458                 CleanupStack::PopAndDestroy(nameBuf);                
       
   459                 if(mergedPolicy)
       
   460                 {
       
   461                     if(mergedPolicy->NormalizePolicyL(NULL))
       
   462                     {
       
   463 //                        return mergedPolicy;
       
   464                     }
       
   465                     else //cannot normalize merged so return NULL
       
   466                     {
       
   467                         delete mergedPolicy;
       
   468                         mergedPolicy = NULL;
       
   469                     }
       
   470                 }
       
   471                 else
       
   472                 {
       
   473                     delete mergedPolicy;
       
   474                     mergedPolicy = NULL;
       
   475                     
       
   476                 }
       
   477                 
       
   478             }
       
   479             CleanupStack::PopAndDestroy(pBuf);
       
   480             return mergedPolicy;
       
   481         }
       
   482     }
       
   483     
       
   484     return NULL;
       
   485     }
       
   486 
       
   487 CSenElement* CWSPolicy::NormalizePolicy2L(CSenElement& aPolicy, CPolicyRegistry* aRegistry)
       
   488 {
       
   489     CPolicyAssertion* poolicy = iReader->GetPolicyL(&aPolicy);
       
   490     
       
   491     if(!poolicy)
       
   492         {
       
   493         return NULL;
       
   494         }
       
   495     if(poolicy->IsNormalized())
       
   496         {
       
   497         return iNormPolicy;
       
   498         }
       
   499     TInt error = KErrNone;    
       
   500     
       
   501     MAssertion* normalized = NULL;
       
   502     TRAP(error, normalized = poolicy->NormalizeL(aRegistry));
       
   503 	if(error == KErrNone && normalized)
       
   504 	    {
       
   505         iNormalized = ETrue;
       
   506         iNormaPolicyAssert = (CPolicyAssertion*)normalized;
       
   507         delete poolicy;
       
   508 //        delete iInDoc;
       
   509 //        iInDoc = NULL;
       
   510 
       
   511         CBufFlat *pBuf = CBufFlat::NewL(KFlatBufSize);
       
   512         CleanupStack::PushL(pBuf);
       
   513         TPtrC8 p = iWriter->WriteToBufL(*pBuf, iNormaPolicyAssert);        
       
   514         delete iNormaPolicyAssert;
       
   515         iNormaPolicyAssert = NULL;
       
   516         TInt error = iOutDoc->ReadFromDescL(p);
       
   517         CleanupStack::PopAndDestroy(pBuf);
       
   518 
       
   519         if(error == KErrNone)
       
   520             {
       
   521             iNormPolicy = iOutDoc->XMLDocL();
       
   522             if(iNormPolicy)
       
   523                 {
       
   524                 iNormPolicy = iOutDoc->ExtractElement();
       
   525                 }
       
   526             }
       
   527         else
       
   528             {
       
   529             iNormPolicy = NULL;
       
   530             }
       
   531 	    }
       
   532 	return iNormPolicy;
       
   533     
       
   534 }
       
   535 TPtrC8 CWSPolicy::PolicyIdL(CSenElement* aPolicy)
       
   536     {
       
   537     RPointerArray<CSenBaseAttribute>& attrs = aPolicy->AttributesL();
       
   538 
       
   539     CSenBaseAttribute* bs = NULL;
       
   540     TInt ele_count = attrs.Count();
       
   541          
       
   542     for (TInt j=0; j < ele_count; j++)
       
   543         {
       
   544             
       
   545         bs = (attrs)[j];
       
   546         if(bs)
       
   547 
       
   548         if(bs->Name().Compare(WSPolicy::KWsuId) == 0)
       
   549             {
       
   550              return bs->Value();
       
   551             }
       
   552          }
       
   553     GeneratePolicyIdL(aPolicy);  //codescannerwarnings
       
   554     
       
   555     return PolicyIdL(aPolicy);
       
   556     }
       
   557 
       
   558 TPtrC8 CWSPolicy::PolicyNameL(CSenElement* aPolicy)
       
   559     {
       
   560     RPointerArray<CSenBaseAttribute>& attrs = aPolicy->AttributesL();
       
   561     
       
   562     CSenBaseAttribute* bs = NULL;
       
   563     TInt ele_count = attrs.Count();
       
   564          
       
   565     for (TInt j=0; j < ele_count; j++)
       
   566         {
       
   567             
       
   568         bs = (attrs)[j];
       
   569         if(bs)
       
   570 
       
   571         if(bs->Name().Compare(KName) == 0)
       
   572             {
       
   573              return bs->Value();
       
   574             }
       
   575          }
       
   576     return KNullDesC8();
       
   577     }
       
   578 
       
   579 TInt CWSPolicy::AddPolicyNameL(CSenElement* aPolicy)
       
   580     {
       
   581   SenXmlUtils::AddAttributeL(*aPolicy, KName, iPolicyName->Des());
       
   582   return KErrNone; 
       
   583    
       
   584     }
       
   585 TInt CWSPolicy::AddPolicyMetadatEndpointL(CSenElement* aPolicy)
       
   586     {
       
   587   SenXmlUtils::AddAttributeL(*aPolicy, MSWSPolicy::KMetaDataLocalName, iPolicyUri->Des());
       
   588   return KErrNone; 
       
   589    
       
   590     }
       
   591 TInt CWSPolicy::AddPolicyIdL(CSenElement* aPolicy)
       
   592     {
       
   593     aPolicy->AddAttrL(KWsuId, iPolicyId->Des());
       
   594     aPolicy->AddNamespaceL(KWsuPrefix, KWsuNsUri);
       
   595     return KErrNone; 
       
   596    
       
   597     }
       
   598 void CWSPolicy::GeneratePolicyIdL(CSenElement* aPolicy)  //codescannerwarnings
       
   599     {
       
   600 
       
   601     CSenGuidGen* pGuidGenerator = CSenGuidGen::NewLC();
       
   602     HBufC8* pPolicyId = pGuidGenerator->GetRandomGuid8LC();//codescannerwarning
       
   603     TPtr8 policyIdDes = pPolicyId->Des();
       
   604     policyIdDes.LowerCase();
       
   605 
       
   606     aPolicy->AddAttrL(KWsuId, policyIdDes);
       
   607     aPolicy->AddNamespaceL(KWsuPrefix, KWsuNsUri);
       
   608             
       
   609     CleanupStack::PopAndDestroy(pPolicyId);
       
   610     CleanupStack::PopAndDestroy(pGuidGenerator);
       
   611     }
       
   612 
       
   613 TInt CWSPolicy::AddPolicyValidityL(CSenElement* aPolicy)
       
   614     {
       
   615     TBuf8<SenDateUtils::KXmlDateTimeMaxLength> ts;
       
   616     SenDateUtils::ToXmlDateTimeUtf82L(ts, iExpiry);
       
   617     if(ts.Length() > 0)
       
   618         {
       
   619         SenXmlUtils::AddAttributeL(*aPolicy, KValidUntil, ts);
       
   620         return KErrNone; 
       
   621         }
       
   622     return KErrNotFound;        
       
   623     }
       
   624     
       
   625 EXPORT_C TBool CWSPolicy::IsExpiredPolicyL()  //codescannerwarnings
       
   626     {
       
   627     CSenElement* expiry = GetExpiryTagL();	//codescannerwarnings 
       
   628     if(expiry && expiry->HasContent())
       
   629         {
       
   630         TPtrC8 expiryTime = expiry->Content();
       
   631      
       
   632         TTime now;
       
   633         now.UniversalTime();
       
   634      
       
   635         TBuf8<SenDateUtils::KXmlDateTimeMaxLength> ts;
       
   636         SenDateUtils::ToXmlDateTimeUtf82L(ts, now);
       
   637         
       
   638         if(ts > expiryTime)
       
   639             return ETrue;
       
   640         else
       
   641             return EFalse;
       
   642         }
       
   643     return EFalse; 
       
   644     }
       
   645     
       
   646 CSenElement* CWSPolicy::GetExpiryTagL()	//codescannerwarnings 
       
   647     {
       
   648      CSenElement* documentRoot = PolicyL();  //codescannerwarnings //(CSenElement*)&iPolicyNormalizer->AsElement().Root();
       
   649     if(documentRoot)     
       
   650     {
       
   651         RPointerArray<CSenElement> timeStamps;
       
   652         CleanupClosePushL(timeStamps);       
       
   653         CSenElement* validity = NULL;
       
   654         const CSenNamespace* msNsUri = documentRoot->Namespace(MSWSPolicy::KMSNsPrefix);
       
   655         if(msNsUri)
       
   656             {
       
   657             if(documentRoot->ElementsL(timeStamps, msNsUri->URI(), MSWSPolicy::KMetaDataValidity) == KErrNone)    
       
   658                 {
       
   659                 TInt validityStampsCount = timeStamps.Count();
       
   660                 
       
   661                 if(validityStampsCount > 0)
       
   662                     {
       
   663                     validity = timeStamps[0];//mws:metadatavalidity 
       
   664                     CleanupStack::PopAndDestroy();
       
   665                     return validity->Element(msNsUri->URI(), MSWSPolicy::KExpiry);
       
   666                     }
       
   667                 }
       
   668             
       
   669             }
       
   670         else
       
   671             {
       
   672             if(documentRoot->ElementsL(timeStamps, MSWSPolicy::KMetaDataValidity) == KErrNone)
       
   673                 {
       
   674                         
       
   675                 TInt validityStampsCount = timeStamps.Count();
       
   676                 
       
   677                 if(validityStampsCount > 0)
       
   678                     {
       
   679                     validity = timeStamps[0];//mws:metadatavalidity 
       
   680                     CleanupStack::PopAndDestroy();
       
   681                     return validity->Element(MSWSPolicy::KExpiry);
       
   682                     }
       
   683                 }
       
   684             
       
   685             }
       
   686 
       
   687         CleanupStack::PopAndDestroy();
       
   688     }
       
   689     return NULL;
       
   690     }
       
   691     
       
   692 TBool CWSPolicy::EvaluateExpiryL(CSenElement* aExpiry)	//codescannerwarnings 
       
   693 { //we have metadatavalidity element here ,
       
   694 //extract the Expiry value, and if its "UTC + 7 days" then replace it with expiry time
       
   695     TInt updateTime(KErrNotFound);
       
   696     if(aExpiry && aExpiry->HasContent())
       
   697     {
       
   698         TPtrC8 timeTag = aExpiry->Content();
       
   699         
       
   700         TInt utc = timeTag.Find(_L8("UTC"));        
       
   701         TInt days = timeTag.Find(_L8("days"));
       
   702         if(utc != KErrNotFound && days != KErrNotFound)
       
   703         {   //We found UTC +x days timestamp
       
   704             //replace this time stamp with expiry time starting from now
       
   705         
       
   706             TInt plus = timeTag.Locate('+');
       
   707             if(plus != KErrNotFound)
       
   708             {
       
   709                 TInt timeLength = days-plus-2;
       
   710                 TPtrC8 timeValue =   timeTag.Mid(plus+2,timeLength-1);
       
   711                 if(timeValue != KNullDesC8)
       
   712                 {
       
   713                     TInt value = 0;        
       
   714                     TLex8 lex(timeValue);
       
   715                     TInt retVal = lex.Val(value);
       
   716                     if(retVal == KErrNone)
       
   717                     {
       
   718                         if(value > 0)
       
   719                         {
       
   720                             TTime now;
       
   721                             now.UniversalTime();
       
   722                             TTimeIntervalDays interval(value);
       
   723                             now+=interval;
       
   724                             
       
   725                             TBuf8<SenDateUtils::KXmlDateTimeMaxLength> ts;
       
   726                             SenDateUtils::ToXmlDateTimeUtf82L(ts, now);
       
   727                             if(ts.Length() > 0)
       
   728                             {
       
   729                                 aExpiry->SetContentL(ts);
       
   730                                 iExpiry = now;
       
   731                                 updateTime = KErrNone;
       
   732                             }
       
   733                         }
       
   734                     }
       
   735                     else if(retVal == KErrGeneral || retVal == KErrOverflow)
       
   736                     {
       
   737                         
       
   738                     }
       
   739 
       
   740                 }
       
   741             }
       
   742         }
       
   743         else //the content is not in "UTC +7 days" format so try the XML time format
       
   744         {
       
   745             TTime now;
       
   746             now.UniversalTime();
       
   747      
       
   748             TBuf8<SenDateUtils::KXmlDateTimeMaxLength> ts;
       
   749             SenDateUtils::ToXmlDateTimeUtf82L(ts, now);
       
   750             TTime expiry = SenDateUtils::FromXmlDateTimeL(timeTag);
       
   751             if(expiry > now)
       
   752             {
       
   753               iExpiry = expiry;
       
   754               updateTime = KErrNone;  
       
   755             }
       
   756         
       
   757             
       
   758         }
       
   759     }
       
   760 
       
   761     if(updateTime == KErrNone)
       
   762         return ETrue;    
       
   763     else
       
   764         return EFalse;
       
   765 }
       
   766 
       
   767 CMyXml* CMyXml::NewL()
       
   768 {
       
   769      CMyXml* pSelf = CMyXml::NewLC();
       
   770     CleanupStack::Pop(pSelf);
       
   771     return pSelf;
       
   772 }
       
   773 CMyXml* CMyXml::NewLC()
       
   774 {
       
   775        CMyXml* pSelf = new (ELeave) CMyXml();
       
   776     CleanupStack::PushL(pSelf);
       
   777     pSelf->ConstructL();
       
   778     return pSelf;
       
   779   
       
   780 }
       
   781 CMyXml::~CMyXml()
       
   782 {
       
   783 }
       
   784 CMyXml::CMyXml() :iParsed(EFalse)
       
   785 {
       
   786     
       
   787 }
       
   788 void CMyXml::ConstructL()
       
   789 {
       
   790 }
       
   791 CSenElement* CMyXml::XMLDocL()
       
   792 {
       
   793     if(iParsed)
       
   794     {
       
   795         CSenElement* ele = NULL;
       
   796         TRAPD (error, ele = &AsElement());
       
   797         if(error == KErrNone)
       
   798         {
       
   799             if (ele->LocalName().Compare(KWsPolicy) == 0)
       
   800             {
       
   801                 return ele;
       
   802             }        
       
   803         }
       
   804     }
       
   805 
       
   806     return NULL;
       
   807 }
       
   808 TInt CMyXml::ReadFromDescL(const TDesC8& aData)
       
   809 {
       
   810 
       
   811 
       
   812     TInt size = aData.Length();
       
   813     if(size > 0)
       
   814         {
       
   815         iXmlReader = CSenXmlReader::NewL(KXmlParserMimeType); // use libxml2 sax parser + CodeScannerWarnings     
       
   816         TRAPD (err, ParseL(aData);)
       
   817         delete(iXmlReader);
       
   818         iXmlReader = NULL;
       
   819         
       
   820         if(err == KErrNone)
       
   821             iParsed = ETrue;
       
   822         
       
   823         return err;
       
   824         }
       
   825     return KErrArgument;        
       
   826 }   
       
   827 // END OF FILE