IMPSengine/messageencoder/wbxml/src/ImpsEncodeWbXml.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2003 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:  Imps engine wbxml message encoder
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    <e32std.h>
       
    22 #include    <apparc.h>
       
    23 #include    <s32mem.h>
       
    24 #include    <utf.h>
       
    25 #include    <e32math.h>
       
    26 #include    <miutconv.h>
       
    27 //#include    <mentact.h>
       
    28 
       
    29 #include    "ImpsWbXmlData.h"
       
    30 #include    "ImpsEncodewbxml.h"
       
    31 #include    "ImpsDataAccessorapi.h"
       
    32 #include    "ImpsErrors.h"
       
    33 #include    "Imps_1_1_Tokens.h"
       
    34 #include    "Imps_1_2_Tokens.h"
       
    35 #include    "ImpsWbXmlCommon.h"
       
    36 #include    "ImpsCspDtd.h"
       
    37 #include    "ImpsUtils.h"
       
    38 #include    "ImpsServices.h"
       
    39 #include    "ImpsVariantAPI.h"
       
    40 #include    "impsfields.h"
       
    41 #include    "ImpsDataUtils.h"
       
    42 
       
    43 // ================= LOCAL FUNCTIONS ====================
       
    44 
       
    45 // ================= MEMBER FUNCTIONS =======================
       
    46 
       
    47 // C++ default constructor can NOT contain any code, that
       
    48 // might leave.
       
    49 //
       
    50 CImpsEncodeWbXml::CImpsEncodeWbXml( ):
       
    51     iCspVersion( EImpsCspVersion11 )
       
    52     {
       
    53 
       
    54     }
       
    55 
       
    56 // default constructor can leave.
       
    57 void CImpsEncodeWbXml::ConstructL()
       
    58     {
       
    59   
       
    60     iElementValue = HBufC::NewL(KImpsWbXmlMaxStringLength);
       
    61     iStoreValue = HBufC8::NewL(KImpsWbXmlMaxStringLength);
       
    62 
       
    63     // create handler for wbxml data
       
    64     iWbXmlData = CImpsWbXmlData::NewL( );
       
    65 
       
    66     iCurrentDictionary = iDictionaryCount = 0;
       
    67     iDictionaries[iDictionaryCount++] = 
       
    68         (NW_WBXML_Dictionary_s*)&NW_Imps_1_1_WBXMLDictionary;
       
    69     iDictionaries[iDictionaryCount++] = 
       
    70         (NW_WBXML_Dictionary_s*)&NW_Imps_1_2_WBXMLDictionary;
       
    71     if(NW_WBXML_Dictionary_initialize (iDictionaryCount,iDictionaries) != NW_STAT_SUCCESS)
       
    72     {
       
    73         User::Leave(KImpsErrorEncode);
       
    74     }
       
    75 
       
    76     // PEC in use ?
       
    77     CImpsVariant* dpb = CImpsVariant::NewLC();
       
    78     if (dpb->IsFeatureSupportedL(EDpb))
       
    79     {
       
    80         iXmlUtils = CImpsXmlUtils::NewL( );
       
    81         iPEC = ETrue;
       
    82     }
       
    83     else
       
    84     {
       
    85         iXmlUtils = NULL;
       
    86         iPEC = EFalse;
       
    87     }
       
    88     
       
    89    CleanupStack::PopAndDestroy();
       
    90     
       
    91     }
       
    92 
       
    93 // Two-phased constructor.
       
    94 CImpsEncodeWbXml* CImpsEncodeWbXml::NewL( )
       
    95     {
       
    96 
       
    97     CImpsEncodeWbXml* self = new (ELeave) CImpsEncodeWbXml( );
       
    98     CleanupStack::PushL( self );
       
    99     self->ConstructL();
       
   100     CleanupStack::Pop();
       
   101 
       
   102     return self;
       
   103     }
       
   104 
       
   105     
       
   106 // Destructor
       
   107 CImpsEncodeWbXml::~CImpsEncodeWbXml()
       
   108     {
       
   109     
       
   110     delete iWbXmlData;
       
   111     delete iElementValue;
       
   112     delete iStoreValue;
       
   113     delete iXmlUtils;
       
   114     NW_WBXML_Dictionary_destroy(); 
       
   115     
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------
       
   119 // CImpsEncodeXml::EncodeMessageL
       
   120 // wbxml message encoder main function, which provides API
       
   121 // class aImpsData contains message data,
       
   122 // encoded message is stored to aEncodeBuffer
       
   123 // ---------------------------------------------------------
       
   124 //
       
   125 void CImpsEncodeWbXml::EncodeMessageL(
       
   126     MImpsDataAccessor& aImpsData,
       
   127     CBufFlat& aEncodeBuffer)
       
   128     {
       
   129 
       
   130     // INIT BUFFERS
       
   131     iImpsData = &aImpsData;
       
   132     iEncodeBuffer = &aEncodeBuffer;
       
   133         
       
   134     // CREATE EMPTY DOM DOCUMENT TREE
       
   135     NW_TinyDom_Handle_t handle;
       
   136     
       
   137     // set up the version related things
       
   138     iCspVersion = iImpsData->GetImpsFields()->CspVersion();
       
   139     
       
   140     TInt cspVersion = 
       
   141         ((iCspVersion == EImpsCspVersion11)?KImpsWvCsp11PublicIdentifier:KImpsWvCsp12PublicIdentifier);
       
   142 
       
   143 
       
   144 	iCurrentDictionary =
       
   145     	((iCspVersion == EImpsCspVersion11)? 0 : 1);	
       
   146     	
       
   147     iDocument = NW_DOM_DocumentNode_createDocumentWithNumberPublicId(&handle,
       
   148     KImpsWbXmlVersion,cspVersion,KImpsDefaultCharset,NW_TRUE,NW_TRUE);
       
   149     if(!iDocument)
       
   150     {
       
   151         User::Leave(KImpsErrorEncode);
       
   152     }
       
   153 
       
   154     // check for possible buffer allocation failure
       
   155     if(!iElementValue)
       
   156     {
       
   157         iElementValue = HBufC::NewL(KImpsWbXmlMaxStringLength);
       
   158     }
       
   159     if(!iStoreValue)
       
   160     {
       
   161         iStoreValue = HBufC8::NewL(KImpsWbXmlMaxStringLength);
       
   162     }
       
   163 
       
   164     // create key for data accessor
       
   165     iAccessKey = iImpsData->NewKeyL();
       
   166     iNameSpace = KImpsNameSpaceCspMessage;
       
   167 
       
   168 
       
   169     // start building DOM document tree
       
   170     TInt rcode = KErrNone;
       
   171     TBufC<16> root(KImpsWvCspMessage);
       
   172     TRAPD(error,rcode = EncodeElementL(root,iDocument,0))
       
   173     if(error != KErrNone || rcode != KErrNone)
       
   174     {
       
   175     
       
   176         // FAILURE !
       
   177         // delete DOM document
       
   178         NW_DOM_DocumentNode_Delete(iDocument);
       
   179         // delete access key        
       
   180         iAccessKey->Destroy( );
       
   181         if(rcode != KErrNone)
       
   182         {
       
   183             User::Leave(rcode);
       
   184         }
       
   185         else
       
   186         {
       
   187             User::Leave(KImpsErrorEncode);
       
   188         }
       
   189     }
       
   190     iAccessKey->Destroy( );
       
   191 
       
   192     // ENCODE MESSAGE
       
   193     TUint32 size = 0;
       
   194     unsigned char *buffer = NULL;
       
   195     NW_Encoder_t WbXmlEncoder;
       
   196     if(NW_Encoder_encodeWBXML(&WbXmlEncoder,iDocument,NW_TRUE,&size,&buffer) != NW_STAT_SUCCESS)
       
   197     {
       
   198         // FAILURE !
       
   199         // delete DOM document
       
   200         NW_DOM_DocumentNode_Delete(iDocument);
       
   201         if(buffer)
       
   202         {
       
   203             free(buffer);
       
   204         }
       
   205         User::Leave(KImpsErrorEncode);
       
   206     }
       
   207 
       
   208     // put the new public ID there
       
   209 //    if (size >0)
       
   210 //        {
       
   211 //        buffer[1] = 0x10;
       
   212 //        }
       
   213     
       
   214     // copy message to aEncodeBuffer
       
   215     if((TInt)size > iEncodeBuffer->Size())
       
   216     {
       
   217         iEncodeBuffer->Reset();
       
   218     }
       
   219     
       
   220     iEncodeBuffer->ResizeL(size);
       
   221     iEncodeBuffer->Write(0,buffer,size);
       
   222     free(buffer);
       
   223 
       
   224     // delete DOM document
       
   225     NW_DOM_DocumentNode_Delete(iDocument);
       
   226 
       
   227     }
       
   228 
       
   229 // ---------------------------------------------------------
       
   230 // CImpsEncodeWbXml::ReadElementDeclaration
       
   231 // method reads elements DTD declaration
       
   232 // ---------------------------------------------------------
       
   233 //
       
   234 TPtrC CImpsEncodeWbXml::ReadElementDeclaration(TDesC &aElement)
       
   235     {
       
   236 
       
   237     
       
   238     TPtrC dtd;
       
   239     if ( iCspVersion == EImpsCspVersion11 ) 
       
   240         {
       
   241         dtd.Set(KCspHeaderDtd11[NULL]);
       
   242         }
       
   243     else
       
   244         {
       
   245         dtd.Set(KCspHeaderDtd12[NULL]);
       
   246         }
       
   247     
       
   248     TInt i(0);
       
   249     TInt count = 0;
       
   250 
       
   251     if(iNameSpace == KImpsNameSpaceTransactionContent)
       
   252         {
       
   253         count = ((iCspVersion == EImpsCspVersion11)?KImpsTransactionDtd11Count:KImpsTransactionDtd12Count);
       
   254         }
       
   255     else if(iNameSpace == KImpsNameSpacePresenceSubList)
       
   256         {
       
   257         count = ((iCspVersion == EImpsCspVersion11)?KImpsPresenceDtd11Count:KImpsPresenceDtd12Count);
       
   258         }
       
   259     else
       
   260         {
       
   261         count = ((iCspVersion == EImpsCspVersion11)?KImpsHeaderDtd11Count:KImpsHeaderDtd12Count);
       
   262         }
       
   263     for(i=0;i<count;i++)
       
   264     {
       
   265         switch(iNameSpace)
       
   266         {
       
   267          
       
   268         case KImpsNameSpaceCspMessage:
       
   269             if ( iCspVersion == EImpsCspVersion11 )
       
   270                 {
       
   271                 dtd.Set(KCspHeaderDtd11[i]);
       
   272                 }
       
   273             else
       
   274                 {
       
   275                 dtd.Set(KCspHeaderDtd12[i]);
       
   276                 }
       
   277             break;
       
   278 
       
   279         case KImpsNameSpaceTransactionContent:
       
   280             if ( iCspVersion == EImpsCspVersion11 )
       
   281                 {
       
   282                 dtd.Set(KCspContentDtd11[i]);
       
   283                 }
       
   284             else
       
   285                 {
       
   286                 dtd.Set(KCspContentDtd12[i]);
       
   287                 }
       
   288             break;
       
   289 
       
   290         case KImpsNameSpacePresenceSubList:
       
   291             if ( iCspVersion == EImpsCspVersion11 )
       
   292                 {
       
   293                 dtd.Set(KCspPresenceDtd11[i]);
       
   294                 }
       
   295             else
       
   296                 {
       
   297                 dtd.Set(KCspPresenceDtd12[i]);
       
   298                 }
       
   299             break;
       
   300 
       
   301         }
       
   302         if(dtd.Find(aElement) == 1 && dtd.Locate(' ') == (aElement.Length() + 1))
       
   303         {
       
   304             break;
       
   305         }
       
   306     }
       
   307     
       
   308     return(dtd);
       
   309     
       
   310     }
       
   311 
       
   312 // ---------------------------------------------------------
       
   313 // CImpsEncodeWbXml::EncodeElementL
       
   314 // method adds one node to DOM tree
       
   315 // ---------------------------------------------------------
       
   316 //
       
   317 TInt CImpsEncodeWbXml::EncodeElementL(TDesC &aElement, NW_DOM_ElementNode_t* aNode,TInt aIndex)
       
   318     {
       
   319 
       
   320     TBuf<sizeof(SImpsValueList)> valuebuf(sizeof(SImpsValueList));
       
   321     SImpsValueList* valuelist = (SImpsValueList*)valuebuf.Ptr();
       
   322 
       
   323     // Read element declaration
       
   324     TPtrC element = ReadElementDeclaration(aElement);
       
   325 
       
   326     // prepare child element list
       
   327     ReadElementList(element,valuelist);
       
   328 
       
   329     // Get element WBXML token
       
   330     TBool extraelem = EFalse;
       
   331     TInt token = ReadToken(element);
       
   332 
       
   333     TInt elementcounter = 0; 
       
   334     TBool literal = false;
       
   335 
       
   336     if(token == KErrNotFound)
       
   337     {
       
   338     	// check for the extension token APIClient as it is not in the dictionary
       
   339         if ( (aElement.CompareF(KImpsAPIClient))==0 ) 
       
   340             {
       
   341             literal = true;
       
   342             }
       
   343         else
       
   344             {
       
   345         	extraelem = ETrue;
       
   346         	elementcounter = aIndex;
       
   347             }
       
   348     }
       
   349 
       
   350     // Update data accessor key (root element is left out)
       
   351     TInt primitive = KErrNone;
       
   352     if(token != KImpsMessageRootElement && !extraelem)
       
   353     {
       
   354         if ( literal ) 
       
   355             {
       
   356             // add the APIClient key to accessor key
       
   357             iAccessKey->AddL(CREATEKEY(EImpsKeyAPIClient,aIndex),EImpsKeyTypeIM);
       
   358             }
       
   359         else
       
   360             {
       
   361         	primitive = SetDataAccessorKeyL(token,aIndex);
       
   362             }
       
   363     }
       
   364 							
       
   365     TInt rcode = KErrNotFound;
       
   366     NW_DOM_ElementNode_t *node = NULL;
       
   367 	TInt exrcode = KErrNotFound;
       
   368         
       
   369     if(iImpsData->CheckBranchExistenceL(iAccessKey) || extraelem)
       
   370     {
       
   371         if ( literal ) 
       
   372             {
       
   373             HBufC8* literalToken = HBufC8::NewL(aElement.Length()+1);// for the \0 at the end
       
   374             CleanupStack::PushL(literalToken);
       
   375             literalToken->Des().Copy(aElement);
       
   376 
       
   377             // create an element node from string for extension element
       
   378             NW_String_t* elementString = NW_String_new();
       
   379             NW_String_initialize(elementString, (void*)literalToken->Des().PtrZ(), HTTP_utf_8);
       
   380             node = NW_DOM_DocumentNode_createElementNode(iDocument, elementString);
       
   381             NW_String_delete(elementString);
       
   382             
       
   383             // literalToken has to be deleted as last because NW_String keeps the pointer
       
   384             CleanupStack::PopAndDestroy(); // literalToken
       
   385             }        
       
   386         else
       
   387             {
       
   388             node = NW_DOM_DocumentNode_createElementNodeByToken(iDocument,(TUint16)token);
       
   389             }
       
   390 
       
   391             if(node != NULL || extraelem)
       
   392 			    {
       
   393 			        if((rcode = EncodeAttributes(token,node)) == KErrNone || extraelem)
       
   394 			        {
       
   395 			            if(!(valuelist->Flags & KImpsValueListEmpty))
       
   396 			            {
       
   397 			                TPtrC value = ReadNextValue(valuelist,element);
       
   398 			                if(value.Find(KPcData) != KErrNotFound)
       
   399 			                {
       
   400 			                    // encode value
       
   401 			                    rcode = EncodeValueL(token,node,aIndex);
       
   402 			                }
       
   403 			                else
       
   404 			                {
       
   405 			                    for(;;)
       
   406 			                    {
       
   407 			                        if(valuelist->Flags & KImpsValueListAlternative && !extraelem)
       
   408 			                        {
       
   409 			                            if(primitive == KErrNotFound)
       
   410 			                            {
       
   411 			                                TImpsKeyType type = EImpsKeyTypeIM;
       
   412 			                                iImpsData->RestoreAlternativeL(iAccessKey,primitive,aIndex,type);
       
   413 			                            }
       
   414 			                            // Get the element name string from the 1.2 dictionary as it has all the elements
       
   415 			//                                TPtrC elementName(KNullDesC);
       
   416 			                            
       
   417 			                            TUint16* elementNamePtr = (TUint16*)NW_WBXML_Dictionary_getTagByFqToken((NW_Uint32)(2<<16)+primitive)->bytes;
       
   418 			                            if (elementNamePtr)
       
   419 			                            	{
       
   420 			                            	value.Set(elementNamePtr);
       
   421 			                            	}
       
   422 										else
       
   423 											{
       
   424 											value.Set(KNullDesC);
       
   425 											}
       
   426 			                            
       
   427 			                            //value.Set(iWbXmlData->GetDtdValue(token,primitive));
       
   428 			                        }
       
   429 			                        if(extraelem)
       
   430 			                        {
       
   431 			                            node = aNode;
       
   432 			                        }
       
   433 			                        rcode = EncodeElementL(value,node,elementcounter);
       
   434 									if(rcode == KErrNone && extraelem)
       
   435 									{
       
   436 										exrcode = KErrNone;
       
   437 									}
       
   438 									value.Set(FetchNextElement(valuelist,elementcounter,rcode,element,value));
       
   439 									if(rcode == KErrNotFound)
       
   440 									{
       
   441 										if(extraelem) rcode = exrcode;
       
   442 										else rcode = KErrNone;
       
   443 										break;
       
   444 									}
       
   445 									if(rcode == KImpsErrorEncode || rcode == KImpsErrorValidate)
       
   446 			                        {
       
   447 			                            if(extraelem) 
       
   448 			                                {
       
   449 			                                rcode = KErrNotFound;
       
   450 			                                }
       
   451 										else 
       
   452 			                                {
       
   453 			                                rcode = KImpsErrorValidate;
       
   454 			                                }
       
   455 			                            break;
       
   456 			                        }		
       
   457 			                    }
       
   458 			                }
       
   459 			            }
       
   460 			            if(rcode == KErrNone && !extraelem)
       
   461 			            {
       
   462 			                // append node to parent node
       
   463 			                NW_Status_t status = NW_DOM_Node_appendChild((NW_TinyTree_Node_s*)aNode,node);
       
   464 			                if(status != NW_STAT_SUCCESS) 
       
   465 			                    {
       
   466 			                    rcode = KImpsErrorEncode;
       
   467 			                    }
       
   468 			            }
       
   469 			        }
       
   470 			        else
       
   471 			        {
       
   472 			            rcode = KImpsErrorEncode;
       
   473 			        }
       
   474 			    }
       
   475 			    else
       
   476 			    {
       
   477 			        rcode = KImpsErrorEncode;
       
   478             	}
       
   479     	}
       
   480     else
       
   481     {
       
   482         if(token == KImpsWbXmlContentSize)
       
   483         {
       
   484             // special case, content size must be calculated
       
   485             rcode = SetContentSizeL(token,aNode);
       
   486         }
       
   487         else
       
   488         {
       
   489             rcode = KErrNotFound;
       
   490         }
       
   491     }
       
   492 
       
   493     // update data access key
       
   494     if(!extraelem)
       
   495     {
       
   496         PopAccessKeyL(token);
       
   497     }
       
   498 
       
   499     return(rcode);
       
   500     }
       
   501 
       
   502 // ---------------------------------------------------------
       
   503 // CImpsEncodeWbXml::SetContentSize
       
   504 // method counts message content size
       
   505 // ---------------------------------------------------------
       
   506 //
       
   507 TInt CImpsEncodeWbXml::SetContentSizeL(TInt aToken,NW_DOM_ElementNode_t* aNode)
       
   508     {
       
   509     iAccessKey->PopL(2);
       
   510     iAccessKey->AddL(EImpsKeyContentData);
       
   511     
       
   512     TInt size(0);
       
   513     if ( TImpsDataUtils::GetContentDataTypeL( iImpsData, 0 ) == EImpsDataTypeDesc )
       
   514         {
       
   515     TDesC *p;
       
   516         if ( iImpsData->RestoreDescL(iAccessKey,p) )
       
   517             {
       
   518             size = p->Length();   
       
   519             }
       
   520         }
       
   521      else
       
   522          {
       
   523          TDesC8 *p;
       
   524          if ( iImpsData->RestoreDesc8L(iAccessKey,p) )
       
   525             {
       
   526             size = p->Length();   
       
   527             }
       
   528          }
       
   529 
       
   530     iAccessKey->PopL();
       
   531     iAccessKey->AddL(EImpsKeyMessageInfo);
       
   532     iAccessKey->AddL(EImpsKeyContentSize);
       
   533     iImpsData->StoreIntegerL(iAccessKey,size);
       
   534 
       
   535     NW_DOM_ElementNode_t *node = NULL;
       
   536     TInt rcode = KErrNone;
       
   537 
       
   538     if((node = NW_DOM_DocumentNode_createElementNodeByToken(iDocument,(TUint16)aToken)) != NULL)
       
   539     {
       
   540         rcode = EncodeValueL(aToken,node,0);
       
   541       
       
   542         if(rcode == KErrNone)
       
   543         {
       
   544             // append node to parent node
       
   545             NW_Status_t status = NW_DOM_Node_appendChild((NW_TinyTree_Node_s*)aNode,node);
       
   546             if(status != NW_STAT_SUCCESS)
       
   547             {
       
   548                 rcode = KImpsErrorEncode;   
       
   549             }
       
   550         }
       
   551         
       
   552     }   
       
   553     else
       
   554     {
       
   555         return(KImpsErrorEncode);
       
   556     }
       
   557 
       
   558     return(rcode);
       
   559     }
       
   560 
       
   561 
       
   562 // ---------------------------------------------------------
       
   563 // CImpsEncodeWbXml::PopAccessKeyL
       
   564 // method updates data accessor key
       
   565 // ---------------------------------------------------------
       
   566 //
       
   567 void CImpsEncodeWbXml::PopAccessKeyL(TInt aToken)
       
   568     {
       
   569 
       
   570     if(iAccessKey->Count())
       
   571     {
       
   572         iAccessKey->PopL( );
       
   573         switch(aToken)
       
   574         {
       
   575             case KImpsWbXmlTransactionContent:
       
   576             iNameSpace = KImpsNameSpaceCspMessage;
       
   577             break;
       
   578             
       
   579             case KImpsWbXmlPresenceSubList:
       
   580             iNameSpace = KImpsNameSpaceTransactionContent;
       
   581             break;
       
   582                 
       
   583             default:
       
   584             break;
       
   585         }
       
   586     }
       
   587 
       
   588     }
       
   589 // ---------------------------------------------------------
       
   590 // CImpsEncodeWbXml::FetchNextElement
       
   591 // medhod fetches next child element if exists
       
   592 // ---------------------------------------------------------
       
   593 //
       
   594 TPtrC CImpsEncodeWbXml::FetchNextElement(SImpsValueList* aValueList,TInt& aElementCounter,
       
   595                                          TInt& aRcode, TPtrC aElementList, TPtrC aCurrentElement)
       
   596     {
       
   597 
       
   598     TPtrC value;
       
   599 
       
   600     if(aRcode == KErrNone)
       
   601     {
       
   602         if(aValueList->Flags & KImpsValueListMultiple ||
       
   603            aValueList->Flags & KImpsValueListOnceOrMore)
       
   604         {
       
   605             value.Set(aCurrentElement);
       
   606             aElementCounter++;
       
   607             aRcode = KErrNone;
       
   608         }
       
   609         else
       
   610         {
       
   611             if(!(aValueList->Flags & KImpsValueListEnd) && !(aValueList->Flags & KImpsValueListAlternative))
       
   612             {
       
   613                 value.Set(ReadNextValue(aValueList,aElementList));
       
   614                 aElementCounter = 0;
       
   615                 aRcode = KErrNone;
       
   616             }
       
   617             else
       
   618             {
       
   619                 aRcode = KErrNotFound;
       
   620             }
       
   621         }
       
   622     }
       
   623     else
       
   624     {
       
   625         if(aRcode == KErrNotFound)
       
   626         {
       
   627             if(aValueList->Flags & KImpsValueListMultiple ||
       
   628                aValueList->Flags & KImpsValueListOptional ||
       
   629                aValueList->Flags & KImpsValueListAlternative)               
       
   630             {
       
   631                 if((aValueList->Flags & KImpsValueListAlternative) && aElementCounter)
       
   632                 {
       
   633                     aRcode = KErrNotFound;
       
   634                 }
       
   635                 else if(!(aValueList->Flags & KImpsValueListEnd))
       
   636                 {
       
   637                     value.Set(ReadNextValue(aValueList,aElementList));
       
   638                     aElementCounter = 0;    
       
   639                     aRcode = KErrNone;
       
   640                 }
       
   641                 else
       
   642                 {
       
   643                     if(aValueList->Flags & KImpsValueListAlternative)
       
   644                     {
       
   645                         aRcode = KImpsErrorEncode;
       
   646                     }
       
   647                     else
       
   648                     {
       
   649                         aRcode = KErrNotFound;
       
   650                     }
       
   651                 }
       
   652             }
       
   653             else if(aValueList->Flags & KImpsValueListOnceOrMore && aElementCounter)
       
   654             {
       
   655                 if(!(aValueList->Flags & KImpsValueListEnd))
       
   656                 {
       
   657                     if(!(aValueList->Flags & KImpsValueListEnd))
       
   658                     {
       
   659                         value.Set(ReadNextValue(aValueList,aElementList));
       
   660                         aElementCounter = 0;          
       
   661                         aRcode = KErrNone;
       
   662                     }
       
   663                     else
       
   664                     {
       
   665                         aRcode = KErrNotFound;
       
   666                     }
       
   667                 }
       
   668             }             
       
   669             else
       
   670             {
       
   671                 aRcode = KImpsErrorEncode;
       
   672             }
       
   673         }          
       
   674     }
       
   675     
       
   676     return value;
       
   677 
       
   678     }
       
   679 // ---------------------------------------------------------
       
   680 // CImpsEncodeWbXml::SetDataAccessorKeyL
       
   681 // method updates data accessor key
       
   682 // ---------------------------------------------------------
       
   683 //
       
   684 TInt CImpsEncodeWbXml::SetDataAccessorKeyL(TInt aToken, TInt aIndex)
       
   685     {
       
   686 
       
   687     TInt impskey = KErrNotFound;
       
   688     TInt primitive = KErrNotFound;
       
   689 
       
   690 	impskey = aToken;
       
   691     switch(iNameSpace)
       
   692     {
       
   693     
       
   694     case KImpsNameSpaceCspMessage:
       
   695     
       
   696         iKeyType = EImpsKeyTypeIM;
       
   697 //        impskey = iWbXmlData->GetHeaderAccessKey(aToken);
       
   698         iAccessKey->AddL(CREATEKEY(impskey,aIndex),iKeyType);
       
   699         break;
       
   700         
       
   701     case KImpsNameSpaceTransactionContent:
       
   702         iKeyType = EImpsKeyTypeIM;
       
   703 //        impskey = iWbXmlData->GetContentAccessKey(aToken);
       
   704         iAccessKey->AddL(CREATEKEY(impskey,aIndex),iKeyType);
       
   705         break;
       
   706     
       
   707     case KImpsNameSpacePresenceSubList:
       
   708         iKeyType = EImpsKeyTypePre;
       
   709 //        impskey = iWbXmlData->GetPresenceAccessKey(aToken);
       
   710                 // impskey needs to be changed because 
       
   711                 // the ContentType is in another namespace
       
   712                 if ( impskey == EImpsKeyContentType)
       
   713                     {
       
   714                     iKeyType = EImpsKeyTypeIM;
       
   715                     }
       
   716         iAccessKey->AddL(CREATEKEY(impskey,aIndex),iKeyType);
       
   717         break;
       
   718 
       
   719     default:
       
   720         break;
       
   721     
       
   722     }
       
   723 
       
   724     if(aToken == KImpsWbXmlTransactionContent)
       
   725     {
       
   726         iNameSpace = KImpsNameSpaceTransactionContent;
       
   727 
       
   728         if(iPEC)
       
   729         {
       
   730             // pure data handle
       
   731             primitive = iXmlUtils->XmlToTransactionContentL(*iImpsData);
       
   732         }
       
   733     }
       
   734 
       
   735     if(aToken == KImpsWbXmlPresenceSubList)
       
   736     {
       
   737         iNameSpace = KImpsNameSpacePresenceSubList;
       
   738     }
       
   739     
       
   740     return(primitive);
       
   741     }
       
   742 
       
   743 // ---------------------------------------------------------
       
   744 // CImpsEncodeWbXml::ReadElementList
       
   745 // method inits child element list
       
   746 // ---------------------------------------------------------
       
   747 //
       
   748 void CImpsEncodeWbXml::ReadElementList(TPtrC aElement, SImpsValueList* aElementList)
       
   749     {
       
   750 
       
   751     aElementList->Begin = aElement.Locate('(');
       
   752     if(aElementList->Begin == KErrNotFound) 
       
   753     {
       
   754         aElementList->Flags |= KImpsValueListEmpty;
       
   755     }
       
   756     else
       
   757     {
       
   758         aElementList->Length  = aElement.Length() - aElementList->Begin;
       
   759         aElementList->Flags = 0;
       
   760         aElementList->Next = aElementList->Begin + 1;
       
   761     }
       
   762 
       
   763     }
       
   764 
       
   765 // ---------------------------------------------------------
       
   766 // CImpsEncodeWbXml::ReadToken
       
   767 // method reads WBXML token corresponding to element name
       
   768 // ---------------------------------------------------------
       
   769 //
       
   770 TInt CImpsEncodeWbXml::ReadToken(TPtrC aElement)
       
   771     {
       
   772         
       
   773     TInt length = aElement.Locate(' ')-1;
       
   774     iElementValue->Des().Zero();
       
   775     for(TInt i=0;i<length;i++)
       
   776     {
       
   777         iElementValue->Des().Append(aElement[i+1]);
       
   778     }
       
   779     iElementValue->Des().Append(NULL);
       
   780 
       
   781     return(NW_WBXML_Dictionary_getTagToken(iDictionaries[iCurrentDictionary],(NW_String_UCS2Buff_t*)iElementValue->Ptr(),NW_TRUE));
       
   782 
       
   783     }
       
   784 // ---------------------------------------------------------
       
   785 // CImpsEncodeWbXml::StoreInteger
       
   786 // method stores integer value
       
   787 // ---------------------------------------------------------
       
   788 //
       
   789 void CImpsEncodeWbXml::StoreInteger(TUint32 aValue, TPtr8 aBuffer)
       
   790     {
       
   791     
       
   792     TInt i = 0;
       
   793     for(i=3;i>0;i--)
       
   794     {
       
   795         if((aValue >> (8*i)) & 0xff) break;
       
   796     }
       
   797     for(;i>0;i--)
       
   798     {
       
   799         aBuffer.Append((TUint8)(aValue >> (8*i)));
       
   800     }
       
   801     
       
   802     aBuffer.Append((TUint8)aValue);
       
   803     }
       
   804 // ---------------------------------------------------------
       
   805 // CImpsEncodeWbXml::EncodeAttributes
       
   806 // method adds attribute values to (one) DOM node if exists
       
   807 // ---------------------------------------------------------
       
   808 //
       
   809 TInt CImpsEncodeWbXml::EncodeAttributes(TInt aToken, NW_DOM_ElementNode_t* aNode)
       
   810     {
       
   811     
       
   812     TUint value = iWbXmlData->GetWbXmlAttributeTokenValue(aToken, iCspVersion);
       
   813     if(value)
       
   814     {
       
   815         TPtrC8 str = iWbXmlData->GetWbXmlAttributeStringValue(aToken, iCspVersion);
       
   816         if(str.Length())
       
   817         {
       
   818             NW_String_initialize(&iString,(TUint8*)str.Ptr(),HTTP_utf_8);
       
   819             if(NW_DOM_AttrVal_initFromString(&iAttribute,&iString) != NW_STAT_SUCCESS)
       
   820             {
       
   821              
       
   822                 return KImpsErrorEncode;
       
   823             }
       
   824         }
       
   825         if(NW_DOM_ElementNode_setAttributeByToken(aNode,(NW_Uint16)value,&iAttribute) != NW_STAT_SUCCESS)
       
   826         {
       
   827             return KImpsErrorEncode;
       
   828         }
       
   829     }
       
   830     else
       
   831     	{
       
   832     	// ExtBlock handling is a special case
       
   833     	if (aToken == EImpsKeyExtBlock)
       
   834     		{
       
   835 	    	NW_String_initialize(&iString,(TUint8*)KAPIClientAttributeValue().Ptr(),HTTP_utf_8);
       
   836             if(NW_DOM_AttrVal_initFromString(&iAttribute,&iString) != NW_STAT_SUCCESS)
       
   837 	            {
       
   838              
       
   839                 return KImpsErrorEncode;
       
   840     	        }
       
   841 	    	
       
   842 	    	NW_String_initialize(&iString,(TUint8*)KAPIClientAttribute().Ptr(),HTTP_utf_8);
       
   843     		if (NW_DOM_ElementNode_setAttributeByAttrVal(aNode, &iString, &iAttribute )!= NW_STAT_SUCCESS)
       
   844 	            {
       
   845                 return KImpsErrorEncode;
       
   846 	            }
       
   847     		}
       
   848     	}
       
   849     
       
   850     return(KErrNone);
       
   851     }
       
   852 // ---------------------------------------------------------
       
   853 // CImpsEncodeWbXml::EncodeValueL
       
   854 // method reads element value from data accessor and stores
       
   855 // it to DOM tree
       
   856 // ---------------------------------------------------------
       
   857 //
       
   858 TInt CImpsEncodeWbXml::EncodeValueL(TInt aToken, NW_DOM_TextNode_t *aNode,TInt aIndex)
       
   859     {
       
   860     // this removes compiler warning
       
   861     aIndex = KErrNone;
       
   862     TInt    rcode       = aIndex;
       
   863     TInt    elemcount   = 0;
       
   864 
       
   865     switch(iImpsData->KeyTypeL(iAccessKey))
       
   866     {
       
   867     case EImpsDataTypeContentData:
       
   868         { 
       
   869          if ( TImpsDataUtils::GetContentDataTypeL( iImpsData, 0 ) == EImpsDataTypeDesc8 )
       
   870             {
       
   871             rcode = EncodeString8L(aToken,elemcount);
       
   872             }
       
   873          else
       
   874             {
       
   875             rcode = EncodeStringL(aToken,elemcount);
       
   876             }
       
   877         }
       
   878         break;
       
   879     case EImpsDataTypeDesc8:
       
   880         rcode = EncodeString8L(aToken,elemcount);
       
   881         break;
       
   882 
       
   883     case EImpsDataTypeDesc:
       
   884         rcode = EncodeStringL(aToken,elemcount);
       
   885         break;
       
   886 
       
   887     case EImpsDataTypeInt:
       
   888         rcode = EncodeIntegerL(aToken,elemcount);
       
   889         break;
       
   890 
       
   891     case EImpsDataTypeBoolean:
       
   892         rcode = EncodeBooleanL(aToken,elemcount);
       
   893         break;
       
   894 
       
   895     case EImpsDataTypeExt:
       
   896         rcode = EncodeExtL(aToken,elemcount);
       
   897         break;
       
   898 
       
   899     default:
       
   900         rcode = KErrNotFound;
       
   901     }
       
   902 
       
   903     if(rcode == KErrNone)
       
   904     {
       
   905         for(TInt i=0;i<elemcount;i++)
       
   906         {
       
   907             NW_DOM_TextNode_t *node = NW_DOM_DocumentNode_createTextNodeWithTextItem(iDocument,&iTextItem[i]);
       
   908             if(node)
       
   909             {
       
   910             if(NW_DOM_Node_appendChild((NW_TinyTree_Node_s*)aNode,node) != NW_STAT_SUCCESS)
       
   911                 {
       
   912                 rcode = KImpsErrorEncode;
       
   913                 break;
       
   914                 }
       
   915             }
       
   916         }
       
   917     }
       
   918     if(iStoreValue->Des().Length() > KImpsWbXmlMaxBufferSize)
       
   919     {
       
   920         delete iStoreValue;
       
   921         iStoreValue = NULL;
       
   922         iStoreValue = HBufC8::NewL(KImpsWbXmlMaxStringLength);
       
   923     }
       
   924     
       
   925     return(rcode);
       
   926     }
       
   927 
       
   928 // ---------------------------------------------------------
       
   929 // CImpsEncodeWbXml::EncodeStringL
       
   930 // method stores string value to DOM node
       
   931 // ---------------------------------------------------------
       
   932 //
       
   933 TInt CImpsEncodeWbXml::EncodeStringL(TInt aToken,TInt& aElemCount)
       
   934     {
       
   935 
       
   936     NW_Status_t status = NW_STAT_SUCCESS;
       
   937     TInt rcode = KErrNone;
       
   938     TInt elem = 0;
       
   939     TInt value = 0;
       
   940     TInt maxlength = 0;
       
   941 
       
   942     TDesC *p;
       
   943     if(iImpsData->RestoreDescL(iAccessKey,p))
       
   944     {
       
   945         // presence data handling
       
   946         if(iNameSpace == KImpsNameSpacePresenceSubList)
       
   947         {
       
   948             TInt token = iAccessKey->GetElementL((iAccessKey->Count() - 2),iKeyType);
       
   949 
       
   950 //            // Lookup the value string in dictionary: NW_FALSE = no case sensitive
       
   951 //            if((elem = NW_WBXML_Dictionary_getAttributeToken(
       
   952 //                &NW_Imps_1_2_WBXMLDictionary, (NW_String_UCS2Buff_t*)p->Ptr(), 0, NW_FALSE)) != KErrNotFound)
       
   953             if((elem = iWbXmlData->GetWbXmlElementValue(token,p)) != KImpsWbXmlDataNotFound)
       
   954             {
       
   955                 status = NW_DOM_TextItem_initFromExtensionInt(&iTextItem[aElemCount++],NW_WBXML_EXT_T_0,elem);
       
   956                 if(status != NW_STAT_SUCCESS)
       
   957                 {
       
   958                     rcode = KImpsErrorEncode;
       
   959                 }
       
   960                 return rcode; // do not continue as we already added the string as extension
       
   961             }
       
   962         }
       
   963 
       
   964 //        if((elem = NW_WBXML_Dictionary_getAttributeToken(
       
   965 //            &NW_Imps_1_2_WBXMLDictionary, (NW_String_UCS2Buff_t*)p->Ptr(), 0, NW_FALSE)) != KErrNotFound)
       
   966         if((elem = iWbXmlData->GetWbXmlElementValue(aToken,p)) != KImpsWbXmlDataNotFound)
       
   967         {
       
   968             status = NW_DOM_TextItem_initFromExtensionInt(&iTextItem[aElemCount++],NW_WBXML_EXT_T_0,elem);
       
   969             if(status != NW_STAT_SUCCESS)
       
   970             {
       
   971                 rcode = KImpsErrorEncode;
       
   972             }
       
   973             else
       
   974             {
       
   975                 rcode = KErrNone;
       
   976             }
       
   977             return(rcode);
       
   978         }
       
   979         iWbXmlData->GetWbXmlElementValue(aToken,value,maxlength);
       
   980 		TPtr8 p8 = iStoreValue->Des();
       
   981 		TInt size = CountContentSize(p);
       
   982 		if(size >= p8.MaxLength())
       
   983 		{
       
   984 			delete iStoreValue;
       
   985 			iStoreValue = NULL;
       
   986 			iStoreValue = HBufC8::NewL(size+1);
       
   987 			p8.Set(iStoreValue->Des( ));
       
   988 		}
       
   989 		if(CnvUtfConverter::ConvertFromUnicodeToUtf8(p8,*p) == KErrNone)
       
   990 		{
       
   991 			// if its content data, only then escape XML which will convert
       
   992 			// special characters like & to &amp;.
       
   993 			if (0x0D == aToken)
       
   994 			{
       
   995 			// Fix TSW Error Id: EDZG-7KXCAF 
       
   996 			iStoreValue = iXmlUtils->EscapeXmlL(p8).AllocL();
       
   997 			iStoreValue = iStoreValue->ReAllocL(iStoreValue->Length() + 1 );
       
   998 			}	
       
   999 					
       
  1000 			iStoreValue->Des().Append(0);
       
  1001 			TInt offset = 0;
       
  1002 			elem = SearchPrefix(iStoreValue->Des(),offset);
       
  1003 			if(elem != KErrNotFound)
       
  1004 			{
       
  1005 				status = NW_DOM_TextItem_initFromExtensionInt(&iTextItem[aElemCount++],NW_WBXML_EXT_T_0,elem);
       
  1006 				if(status != NW_STAT_SUCCESS)
       
  1007 				{
       
  1008 					return(KImpsErrorEncode);
       
  1009 				}
       
  1010 			}
       
  1011 
       
  1012 			NW_String_initialize(&iString,(TUint8*)iStoreValue->Des().Mid(offset).Ptr(),HTTP_utf_8);
       
  1013 			status = NW_DOM_TextItem_initFromString(&iTextItem[aElemCount++],&iString);
       
  1014 			if(status != NW_STAT_SUCCESS)
       
  1015 			{
       
  1016 				rcode = KImpsErrorEncode;
       
  1017 			}
       
  1018 		}
       
  1019 		else
       
  1020 		{
       
  1021 			rcode = KImpsErrorEncode;
       
  1022 		}
       
  1023     }
       
  1024     else
       
  1025     {
       
  1026         rcode = KErrNotFound;
       
  1027     }
       
  1028 
       
  1029     return(rcode);
       
  1030 }
       
  1031 
       
  1032 // ---------------------------------------------------------
       
  1033 // CImpsEncodeWbXml::EncodeString8L
       
  1034 // method stores 8 bit string value to DOM node
       
  1035 // ---------------------------------------------------------
       
  1036 //
       
  1037 TInt CImpsEncodeWbXml::EncodeString8L(TInt aToken,TInt& aElemCount)
       
  1038     {
       
  1039     
       
  1040     NW_Status_t status = NW_STAT_SUCCESS;
       
  1041     TInt rcode = KErrNone;
       
  1042     TInt elem = 0;
       
  1043     TInt value = 0;
       
  1044     TInt maxlength;
       
  1045 
       
  1046     TDesC8 *p8;
       
  1047     if(iImpsData->RestoreDesc8L(iAccessKey,p8))
       
  1048     {
       
  1049         if((elem = iWbXmlData->GetWbXmlElementValue(aToken,value,maxlength))
       
  1050                  != KImpsWbXmlDataNotFound)
       
  1051         {
       
  1052             StoreInteger(elem,iStoreValue->Des());
       
  1053             iString.storage = (TUint8*)iStoreValue->Ptr();
       
  1054             iString.length = iStoreValue->Length();
       
  1055         }
       
  1056 		if(p8->Length() >= iStoreValue->Des().MaxLength())
       
  1057 		{
       
  1058 			delete iStoreValue;
       
  1059 			iStoreValue = NULL;
       
  1060 			iStoreValue = HBufC8::NewL(p8->Length() + 1);
       
  1061 		}
       
  1062 		iStoreValue->Des().Copy(*p8);
       
  1063 /*
       
  1064 		if(aToken == KImpsWbXmlDirectContent || aToken == KImpsWbXmlContainedvCard)
       
  1065 		{
       
  1066 			iOpaque.data = (TUint8*)iStoreValue->Ptr();
       
  1067 			iOpaque.length = iStoreValue->Length();
       
  1068 			status = NW_DOM_TextItem_initFromOpaque(&iTextItem[aElemCount++],iOpaque.length,iOpaque.data);
       
  1069 			if(status != NW_STAT_SUCCESS)
       
  1070 			{
       
  1071 				rcode = KImpsErrorEncode;
       
  1072 			}
       
  1073 		}
       
  1074 		else
       
  1075 */
       
  1076 		{
       
  1077 			iStoreValue->Des().Append(0);
       
  1078 			NW_String_initialize(&iString,(TUint8*)iStoreValue->Des().Ptr(),HTTP_utf_8);
       
  1079 			status = NW_DOM_TextItem_initFromString(&iTextItem[aElemCount++],&iString);
       
  1080 			if(status != NW_STAT_SUCCESS)
       
  1081 			{
       
  1082 				rcode = KImpsErrorEncode;
       
  1083 			}
       
  1084 		}
       
  1085 
       
  1086     }
       
  1087     else
       
  1088     {
       
  1089         rcode = KErrNotFound;
       
  1090     }
       
  1091 
       
  1092     return(rcode);
       
  1093     
       
  1094     }
       
  1095 
       
  1096 // ---------------------------------------------------------
       
  1097 // CImpsEncodeWbXml::EncodeExt
       
  1098 // method stores extension value to DOM node
       
  1099 // ---------------------------------------------------------
       
  1100 //
       
  1101 TInt CImpsEncodeWbXml::EncodeExtL(TInt aToken,TInt& aElemCount)
       
  1102     {
       
  1103     
       
  1104     NW_Status_t status = NW_STAT_SUCCESS;
       
  1105     TInt rcode = KErrNone;
       
  1106     TInt elem = 0;
       
  1107     TInt value = 0;
       
  1108     TInt maxlength;
       
  1109 
       
  1110     TDesC8 *p8;
       
  1111     if(iImpsData->RestoreDesc8L(iAccessKey,p8))
       
  1112     {
       
  1113         if((elem = iWbXmlData->GetWbXmlElementValue(aToken,value,maxlength))
       
  1114                  != KImpsWbXmlDataNotFound)
       
  1115         {
       
  1116             StoreInteger(elem,iStoreValue->Des());
       
  1117             iString.storage = (TUint8*)iStoreValue->Ptr();
       
  1118             iString.length = iStoreValue->Length();
       
  1119         }
       
  1120         if(maxlength && p8->Length() > maxlength)
       
  1121         {
       
  1122             rcode = KImpsErrorEncode;
       
  1123         }
       
  1124         else
       
  1125         {
       
  1126             if(p8->Length() >= iStoreValue->Des().MaxLength())
       
  1127             {
       
  1128                 delete iStoreValue;
       
  1129                 iStoreValue = NULL;
       
  1130                 iStoreValue = HBufC8::NewL(p8->Length() + 1);
       
  1131             }
       
  1132             iStoreValue->Des().Copy(*p8);
       
  1133             iStoreValue->Des().Append(0);
       
  1134             NW_String_initialize(&iString,(TUint8*)iStoreValue->Des().Ptr(),HTTP_utf_8);
       
  1135             status = NW_DOM_TextItem_initFromString(&iTextItem[aElemCount++],&iString);
       
  1136             if(status != NW_STAT_SUCCESS)
       
  1137             {
       
  1138                 rcode = KImpsErrorEncode;
       
  1139             }
       
  1140         }
       
  1141     }
       
  1142     else
       
  1143     {
       
  1144         rcode = KErrNotFound;
       
  1145     }
       
  1146 
       
  1147     return(rcode);
       
  1148     }
       
  1149 
       
  1150 // ---------------------------------------------------------
       
  1151 // CImpsEncodeWbXml::EncodeIntegerL
       
  1152 // method stores integer value to DOM node
       
  1153 // ---------------------------------------------------------
       
  1154 //
       
  1155 TInt CImpsEncodeWbXml::EncodeIntegerL(TInt aToken,TInt& aElemCount)
       
  1156     {
       
  1157     
       
  1158     NW_Status_t status = NW_STAT_SUCCESS;
       
  1159     TInt rcode = KErrNone;
       
  1160     TInt elem;
       
  1161     TInt value;
       
  1162     TInt maxlength;
       
  1163 
       
  1164     if(iImpsData->RestoreIntegerL(iAccessKey,value))
       
  1165     {
       
  1166         elem = iWbXmlData->GetWbXmlElementValue(aToken,value,maxlength);
       
  1167         if(elem == KImpsWbXmlDataNotFound)
       
  1168         {
       
  1169             TPtrC8 p = iWbXmlData->GetWbXmlStringValue(aToken,value);
       
  1170             if(p.Length())
       
  1171             {
       
  1172                 NW_String_initialize(&iString,(TUint8*)p.Ptr(),HTTP_utf_8);
       
  1173                 status = NW_DOM_TextItem_initFromString(&iTextItem[aElemCount++],&iString);
       
  1174                 if(status != NW_STAT_SUCCESS)
       
  1175                 {
       
  1176                     rcode = KImpsErrorEncode;
       
  1177                 }
       
  1178             }
       
  1179             else
       
  1180             {
       
  1181                 iStoreValue->Des().Zero();
       
  1182                 StoreInteger(value,iStoreValue->Des());
       
  1183                 iOpaque.data = (TUint8*)iStoreValue->Ptr();
       
  1184                 iOpaque.length = iStoreValue->Length();
       
  1185                 status = NW_DOM_TextItem_initFromOpaque(&iTextItem[aElemCount++],iOpaque.length,iOpaque.data);
       
  1186                 if(status != NW_STAT_SUCCESS)
       
  1187                 {
       
  1188                     rcode = KImpsErrorEncode;
       
  1189                 }
       
  1190             }
       
  1191         }
       
  1192         else
       
  1193         {
       
  1194             status = NW_DOM_TextItem_initFromExtensionInt(&iTextItem[aElemCount++],NW_WBXML_EXT_T_0,elem);
       
  1195             if(status != NW_STAT_SUCCESS)
       
  1196             {
       
  1197                 rcode = KImpsErrorEncode;
       
  1198             }
       
  1199         }
       
  1200     }
       
  1201     else
       
  1202     {
       
  1203         rcode = KErrNotFound;
       
  1204     }
       
  1205 
       
  1206     return(rcode);
       
  1207     }
       
  1208 
       
  1209 // ---------------------------------------------------------
       
  1210 // CImpsEncodeWbXml::SearchPrefix
       
  1211 // method searches prefixes from given string
       
  1212 // ---------------------------------------------------------
       
  1213 //
       
  1214 TInt CImpsEncodeWbXml::SearchPrefix(TPtrC8 aString,TInt& aOffset)
       
  1215     {
       
  1216     
       
  1217     TPtrC8 ptr(KImpsHttps8);
       
  1218     if(aString.Find(ptr) ==  0)
       
  1219     {
       
  1220         aOffset = ptr.Length();
       
  1221         return(KImpsWbXmlHttps);
       
  1222     }
       
  1223 
       
  1224     ptr.Set(KImpsHttp8);
       
  1225     if(aString.Find(ptr) == 0)
       
  1226     {
       
  1227         aOffset = ptr.Length();
       
  1228         return(KImpsWbXmlHttp);
       
  1229     }
       
  1230 
       
  1231     return(KErrNotFound);
       
  1232     
       
  1233     }
       
  1234 
       
  1235 // ---------------------------------------------------------
       
  1236 // CImpsEncodeWbXml::EncodeBooleanL
       
  1237 // method stores boolean value to DOM node
       
  1238 // ---------------------------------------------------------
       
  1239 //
       
  1240 TInt CImpsEncodeWbXml::EncodeBooleanL(TInt aToken,TInt& aElemCount)
       
  1241     {
       
  1242     
       
  1243     NW_Status_t status = NW_STAT_SUCCESS;
       
  1244     TInt elem;
       
  1245     TInt value;
       
  1246     TInt maxlength;
       
  1247     TInt rcode = KErrNone;
       
  1248 
       
  1249     if(iImpsData->RestoreBooleanL(iAccessKey,value))
       
  1250     {
       
  1251         elem = iWbXmlData->GetWbXmlElementValue(aToken,value,maxlength);
       
  1252         if(elem == KImpsWbXmlDataNotFound)
       
  1253         {
       
  1254             rcode = KErrNotFound;
       
  1255         }
       
  1256         else
       
  1257         {
       
  1258             status = NW_DOM_TextItem_initFromExtensionInt(&iTextItem[aElemCount++],NW_WBXML_EXT_T_0,elem);
       
  1259             if(status != NW_STAT_SUCCESS)
       
  1260             {
       
  1261                 rcode = KImpsErrorEncode;
       
  1262             }
       
  1263         }
       
  1264     }
       
  1265     else
       
  1266     {
       
  1267         rcode = KErrNotFound;
       
  1268     }
       
  1269 
       
  1270     return(rcode);
       
  1271     }
       
  1272 
       
  1273 // ---------------------------------------------------------
       
  1274 // CImpsEncodeWbXml::ReadNextValue
       
  1275 // method reads next value in element declaration
       
  1276 // ---------------------------------------------------------
       
  1277 //
       
  1278 TPtrC CImpsEncodeWbXml::ReadNextValue(SImpsValueList* aValueList,TPtrC aValues)
       
  1279     {
       
  1280 
       
  1281     aValueList->Flags &= KImpsValueListAlternative;
       
  1282     TBool end = EFalse;
       
  1283 
       
  1284     while(aValues[aValueList->Next] == KImpsSpace) aValueList->Next++;
       
  1285     aValueList->Current = aValueList->Next;
       
  1286 
       
  1287     TInt length = 0;
       
  1288     while(aValues[aValueList->Next] != ',' && 
       
  1289           aValues[aValueList->Next] != ')' &&
       
  1290           aValues[aValueList->Next] != '|')
       
  1291     {
       
  1292     
       
  1293         if(aValues[aValueList->Next] == '?')
       
  1294         {
       
  1295             aValueList->Flags |= KImpsValueListOptional;
       
  1296             end = ETrue;
       
  1297         }
       
  1298 
       
  1299         if(aValues[aValueList->Next] == '+')
       
  1300         {
       
  1301             aValueList->Flags |= KImpsValueListOnceOrMore;
       
  1302             end = ETrue;
       
  1303         }
       
  1304         if(aValues[aValueList->Next] == '*')
       
  1305         {
       
  1306             aValueList->Flags |= KImpsValueListMultiple;
       
  1307             end = ETrue;
       
  1308         }
       
  1309         if(aValues[aValueList->Next] == KImpsSpace)
       
  1310         {
       
  1311             end = ETrue;
       
  1312         }
       
  1313         aValueList->Next++;
       
  1314         if(end == EFalse) length++;
       
  1315     }
       
  1316     
       
  1317     if(aValues[aValueList->Next] == '|')
       
  1318     {
       
  1319         aValueList->Flags |= KImpsValueListAlternative;
       
  1320     }
       
  1321 
       
  1322     if(aValues[aValueList->Next] == ')')
       
  1323     {
       
  1324         aValueList->Flags |= KImpsValueListEnd;
       
  1325     }
       
  1326     aValueList->Next++;
       
  1327 
       
  1328     return(aValues.Mid(aValueList->Current,length));
       
  1329     }
       
  1330 
       
  1331 // ---------------------------------------------------------
       
  1332 // CImpsEncodeWbXml::MimeType
       
  1333 // method returns wbxml message MIME type
       
  1334 // ---------------------------------------------------------
       
  1335 //
       
  1336 TPtrC8 CImpsEncodeWbXml::MimeType( )
       
  1337     {
       
  1338     
       
  1339     return TPtrC8(KImpsWbXmlMessageMimeType);
       
  1340     
       
  1341     }
       
  1342 
       
  1343 // ---------------------------------------------------------
       
  1344 // CImpsEncodeWbXml::CountContentSize
       
  1345 // method count string size
       
  1346 // ---------------------------------------------------------
       
  1347 //
       
  1348 TInt CImpsEncodeWbXml::CountContentSize(TDesC*& aPtr)
       
  1349     {
       
  1350 
       
  1351     TInt size = 0;
       
  1352     for (TInt j = 0; j < aPtr->Length(); j++)
       
  1353     {          
       
  1354         TChar byte = TChar(aPtr->Ptr()[j]);
       
  1355         if ( byte < 0x0080)
       
  1356             size += 0;
       
  1357         else if ( byte < 0x0800)
       
  1358             size += 1;
       
  1359         else if ( byte < 0x10000)
       
  1360             size += 2;
       
  1361         else if ( byte < 0x20000)
       
  1362             size += 3;
       
  1363         else if ( byte < 0x4000000)
       
  1364             size += 4;
       
  1365         else
       
  1366             size += 5;
       
  1367      }
       
  1368      size += aPtr->Length();
       
  1369      return(size);
       
  1370     }
       
  1371 
       
  1372 //
       
  1373 // ---------------------------------------------------------
       
  1374 // NewEncoderL()
       
  1375 // wbxml encoder create function
       
  1376 // Returns: C-class entity of abstract MImpsEncoder class
       
  1377 // ---------------------------------------------------------
       
  1378 //
       
  1379 
       
  1380 EXPORT_C GLDEF_C MImpsEncoder* NewEncoderL( )
       
  1381     {
       
  1382     
       
  1383     return (MImpsEncoder*) CImpsEncodeWbXml::NewL( );
       
  1384     
       
  1385     }
       
  1386 
       
  1387 // ---------------------------------------------------------
       
  1388 // CImpsEncodeWbXml::Destroy
       
  1389 // message encoder destructor
       
  1390 // ---------------------------------------------------------
       
  1391 //
       
  1392 void CImpsEncodeWbXml::Destroy( )
       
  1393     {
       
  1394     
       
  1395     delete this;
       
  1396     
       
  1397     }
       
  1398 
       
  1399 //  End of File  
       
  1400