webservices/wsxml/src/senbasefragment.cpp
changeset 0 62f9d29f7211
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:        
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 // INCLUDE FILES
       
    25 
       
    26 #include <flogger.h>
       
    27 #include "SenXmlElement.h" // header to optimized version of SenBaseElement
       
    28 #include "SenBaseFragment.h"
       
    29 #include "SenXmlConstants.h"
       
    30 #include "SenXmlUtils.h"
       
    31 
       
    32 #ifdef SYMBIAN_SECURE_ECOM 
       
    33     // for 2.8, 3.0 or newer:
       
    34     #include <xml/attribute.h> // needed for RAttributeArray
       
    35 #else // for 2.6 or olde
       
    36     #include "Attribute.h"
       
    37 #endif
       
    38 
       
    39 using namespace Xml;
       
    40 
       
    41 namespace
       
    42     {
       
    43     const TInt KFlatBufSize = 128;
       
    44     }
       
    45 
       
    46 // ***************************************************************************
       
    47 // Fragment class state constants are as following (declared in derivants):
       
    48 // KStateNotSet                     = -1;
       
    49 // KStateIgnore                     =  0; // even ones ignore(0),
       
    50 // KStateSave                       =  1; // and odd ones save(1)
       
    51 // KStateResume                     =  2;
       
    52 // KStateParsingFramework           =  4;
       
    53 // KStateParsingSoapFault           =  5;  // odd, save
       
    54 // KStateParsingServiceDescription  =  6;
       
    55 // KStateParsingResourceOffering    =  11; // odd, save
       
    56 // KStateParsingCredentials         =  12;
       
    57 // KStateParsingService             =  13; // odd state, saves content
       
    58 // KStateParsingPwTransforms        =  14;
       
    59 // KStateParsingSoapHeader          =  20; // ignore state (even num)
       
    60 // KStateParsingSoapBody            =  40; // ignore state (even num)
       
    61 // KStateParsingSingleCredential    =  122;
       
    62 // KStateParsingProviderPolicy      =  1222;
       
    63 
       
    64 
       
    65 EXPORT_C CSenBaseFragment* CSenBaseFragment::NewL( const CSenElement& aElement )
       
    66     {
       
    67     CSenBaseFragment* pNew = new (ELeave) CSenBaseFragment;
       
    68     CleanupStack::PushL(pNew);
       
    69     pNew->BaseConstructL(aElement);
       
    70     CleanupStack::Pop(); // pNew;
       
    71     return pNew;
       
    72     }
       
    73 
       
    74 EXPORT_C CSenBaseFragment* CSenBaseFragment::NewL( const TDesC8& aLocalName )
       
    75     {
       
    76     CSenBaseFragment* pNew = new (ELeave) CSenBaseFragment;
       
    77     CleanupStack::PushL(pNew);
       
    78     pNew->BaseConstructL(aLocalName);
       
    79     CleanupStack::Pop(); // pNew;
       
    80     return pNew;
       
    81     }
       
    82 
       
    83 EXPORT_C CSenBaseFragment* CSenBaseFragment::NewL(
       
    84     const TDesC8& aNsUri,
       
    85     const TDesC8& aLocalName
       
    86     )
       
    87     {
       
    88     CSenBaseFragment* pNew = new (ELeave) CSenBaseFragment;
       
    89     CleanupStack::PushL(pNew);
       
    90     pNew->BaseConstructL(aNsUri, aLocalName);
       
    91     CleanupStack::Pop(); // pNew;
       
    92     return pNew;
       
    93     }
       
    94 
       
    95 EXPORT_C CSenBaseFragment* CSenBaseFragment::NewL(
       
    96     const TDesC8& aNsUri,
       
    97     const TDesC8& aLocalName,
       
    98     const TDesC8& aQName,
       
    99     const RAttributeArray& aAttrs
       
   100     )
       
   101     {
       
   102     CSenBaseFragment* pNew = new (ELeave) CSenBaseFragment;
       
   103     CleanupStack::PushL(pNew);
       
   104     pNew->BaseConstructL(aNsUri, aLocalName, aQName, aAttrs);
       
   105     CleanupStack::Pop(); // pNew;
       
   106     return pNew;
       
   107     }
       
   108 
       
   109 EXPORT_C CSenBaseFragment* CSenBaseFragment::NewL( const TDesC8& aNsUri,
       
   110                                                    const TDesC8& aLocalName,
       
   111                                                    const TDesC8& aQName,
       
   112                                                    const RAttributeArray& aAttrs,
       
   113                                                    CSenElement& aParent )
       
   114     {
       
   115     CSenBaseFragment* pNew = new (ELeave) CSenBaseFragment;
       
   116     CleanupStack::PushL(pNew);
       
   117     pNew->BaseConstructL(aNsUri, aLocalName, aQName, aAttrs, aParent);
       
   118     CleanupStack::Pop(); // pNew;
       
   119     return pNew;
       
   120     }
       
   121 
       
   122 EXPORT_C CSenBaseFragment* CSenBaseFragment::NewL( const TDesC8& aNsUri, 
       
   123                                                    const TDesC8& aLocalName,
       
   124                                                    const TDesC8& aQName )
       
   125     {
       
   126     CSenBaseFragment* pNew = new (ELeave) CSenBaseFragment;
       
   127     CleanupStack::PushL(pNew);
       
   128     pNew->BaseConstructL(aNsUri, aLocalName, aQName);
       
   129     CleanupStack::Pop(); // pNew;
       
   130     return pNew;
       
   131     }
       
   132 
       
   133 EXPORT_C CSenBaseFragment::~CSenBaseFragment()
       
   134     {
       
   135     if(iElementOwned)
       
   136         {
       
   137         delete ipElement;
       
   138         }
       
   139 
       
   140     delete ipDelegate;
       
   141 
       
   142     if(ipContentWithNamespaces)
       
   143         {
       
   144         ipContentWithNamespaces->Reset();
       
   145         }
       
   146 
       
   147     iNamespaces.Reset(); // not owned
       
   148     iPrefixes.ResetAndDestroy(); // owned copies from parsed values..
       
   149     }
       
   150 
       
   151 EXPORT_C CSenBaseFragment::CSenBaseFragment()
       
   152 : iState(KStateIgnore),
       
   153   ipElement(NULL),
       
   154   iXmlReader(NULL),
       
   155   ipOwner(NULL),
       
   156   ipDelegate(NULL),
       
   157   ipContentWithNamespaces(NULL),
       
   158   hasContentWriteStream(EFalse),
       
   159   iElementOwned(ETrue)
       
   160     {
       
   161     }
       
   162 
       
   163 EXPORT_C void CSenBaseFragment::BaseConstructL( const CSenElement& aElement )
       
   164     {
       
   165 
       
   166     ipElement = CSenXmlElement::NewL(aElement.LocalName());
       
   167     ipElement->CopyFromL((CSenElement&)aElement);
       
   168     }
       
   169 
       
   170 EXPORT_C void CSenBaseFragment::BaseConstructL( const TDesC8& aLocalName )
       
   171     {
       
   172     ipElement = CSenXmlElement::NewL(aLocalName);
       
   173     }
       
   174 
       
   175 EXPORT_C void CSenBaseFragment::BaseConstructL( const TDesC8& aNsUri,
       
   176                                                 const TDesC8& aLocalName )
       
   177     {
       
   178     ipElement = CSenXmlElement::NewL(aNsUri, aLocalName);
       
   179     }
       
   180 
       
   181 EXPORT_C void CSenBaseFragment::BaseConstructL( const TDesC8& aNsUri,
       
   182                                                 const TDesC8& aLocalName,
       
   183                                                 const TDesC8& aQName,
       
   184                                                 const RAttributeArray& aAttrs )
       
   185     {
       
   186     ipElement = CSenXmlElement::NewL(aNsUri, aLocalName, aQName, aAttrs);
       
   187     }
       
   188 
       
   189 EXPORT_C void CSenBaseFragment::BaseConstructL( const TDesC8& aNsUri,
       
   190                                                 const TDesC8& aLocalName,
       
   191                                                 const TDesC8& aQName,
       
   192                                                 const RAttributeArray& aAttrs,
       
   193                                                 CSenElement& aParent )
       
   194     {
       
   195     // this transfers the ipElement ownership to the parent(!)
       
   196     ipElement = CSenXmlElement::NewL(aNsUri, aLocalName, aQName, aAttrs, aParent);
       
   197     }
       
   198 
       
   199 EXPORT_C void CSenBaseFragment::BaseConstructL( const TDesC8& aNsUri,
       
   200                                                 const TDesC8& aLocalName,
       
   201                                                 const TDesC8& aQName )
       
   202     {
       
   203     ipElement = CSenXmlElement::NewL(aNsUri, aLocalName, aQName);
       
   204     }
       
   205 
       
   206 // Leaves the the fragment without initialized ipElement
       
   207 EXPORT_C void CSenBaseFragment::BaseConstructL( CSenXmlReader& aReader )
       
   208     {
       
   209     iXmlReader = &aReader;
       
   210     }
       
   211 
       
   212 EXPORT_C CSenElement& CSenBaseFragment::AsElement()
       
   213     {
       
   214     __ASSERT_ALWAYS(ipElement != NULL, User::Panic(KSenXmlPanic, EFragmentElementNotInitialized));
       
   215     return *ipElement;
       
   216     }
       
   217 
       
   218 EXPORT_C CSenElement* CSenBaseFragment::ExtractElement()
       
   219     {
       
   220     CSenElement* pElement = ipElement;
       
   221     ipElement = NULL;
       
   222     return pElement;
       
   223     }
       
   224 
       
   225 EXPORT_C CSenXmlReader* CSenBaseFragment::Reader()
       
   226     {
       
   227     return iXmlReader;
       
   228     }
       
   229 
       
   230 EXPORT_C void CSenBaseFragment::SetReader( CSenXmlReader& aReader )
       
   231     {
       
   232     iXmlReader = &aReader;
       
   233     }
       
   234 
       
   235 EXPORT_C TInt CSenBaseFragment::BuildFrom( const TDesC8& aBuf )
       
   236     {
       
   237     TRAPD(err, ParseL(aBuf));
       
   238     return err;
       
   239     }
       
   240 
       
   241 
       
   242 EXPORT_C void CSenBaseFragment::ParseL( const TDesC8& aBuf )
       
   243     {
       
   244     iState = KStateIgnore;
       
   245     if(!iXmlReader)
       
   246         {
       
   247         User::Leave(KErrSenXmlReaderNotSet);
       
   248         }
       
   249     iXmlReader->SetContentHandler(*this);
       
   250     iXmlReader->ParseL(aBuf);
       
   251     }
       
   252 
       
   253 EXPORT_C void CSenBaseFragment::DelegateParsingL(MSenFragment& aDelegate)
       
   254     {
       
   255     // Delegate parsing to a new Fragment, until we encounter 
       
   256     // the end of an element with the given qualified name
       
   257     aDelegate.SetOwner(*this);
       
   258 
       
   259     if(!iXmlReader)
       
   260         {
       
   261         User::Leave(KErrSenXmlReaderNotSet);
       
   262         }
       
   263     aDelegate.ParseWithL(*iXmlReader);
       
   264     }
       
   265 
       
   266 EXPORT_C void CSenBaseFragment::DelegateParsingL( const TDesC8& aNsUri,
       
   267                                                   const TDesC8& aLocalName,
       
   268                                                   const TDesC8& aQName,
       
   269                                                   const RAttributeArray& aAttrs )
       
   270     {
       
   271     // Delegate parsing to a new Fragment, until we encounter the end of an
       
   272     // element with the given qualified name
       
   273     __ASSERT_ALWAYS( ipDelegate == NULL, User::Panic(KSenXmlPanic, EDelegatedFragmentAlreadySet) );
       
   274     ipDelegate = CSenBaseFragment::NewL(aNsUri, aLocalName, aQName);
       
   275 
       
   276     ipDelegate->SetAttributesL(aAttrs);
       
   277     DelegateParsingL(*ipDelegate);
       
   278     }
       
   279 
       
   280 EXPORT_C void CSenBaseFragment::ParseWithL(CSenXmlReader& aReader)
       
   281     {
       
   282     iXmlReader = &aReader;
       
   283     iState = KStateSave;
       
   284     iXmlReader->SetContentHandler(*this);
       
   285     }
       
   286 
       
   287 EXPORT_C void CSenBaseFragment::SetOwner(MSenFragment& aFragment)
       
   288     {
       
   289     ipOwner = &aFragment;
       
   290     AsElement().SetParent(&aFragment.AsElement());
       
   291     }
       
   292 
       
   293 EXPORT_C void CSenBaseFragment::ResumeParsingFromL( const TDesC8& aNsUri,
       
   294                                                     const TDesC8& aLocalName,
       
   295                                                     const TDesC8& aQName )
       
   296     {
       
   297     if(!iXmlReader)
       
   298         {
       
   299         User::Leave(KErrSenXmlReaderNotSet);
       
   300         }
       
   301 
       
   302     // listen to content handler
       
   303     iXmlReader->SetContentHandler(*this);
       
   304     
       
   305     // Destroy delegate
       
   306     if (ipDelegate)
       
   307         {
       
   308         CSenElement* pElement = ipDelegate->ExtractElement();
       
   309         CleanupStack::PushL(pElement); // IOP
       
   310         ipElement->AddElementL(*pElement);
       
   311         CleanupStack::Pop(); // pElement;
       
   312         delete ipDelegate; // free memory
       
   313         ipDelegate = NULL;
       
   314         }
       
   315     
       
   316     
       
   317     TInt currentState = KStateNotSet; // == -1
       
   318     if(ipElement)
       
   319         {
       
   320         const TDesC8& localName = ipElement->LocalName();
       
   321         const TDesC8& nsUri = ipElement->NamespaceURI();
       
   322     
       
   323         if(localName == aLocalName  && nsUri == aNsUri)
       
   324             {
       
   325             currentState = iState;
       
   326             // Before calling EndElementL, which may execute
       
   327             // unpredictable amount of code in derivants, the
       
   328             // the state must be set to "ignore"
       
   329             iState = KStateResume;
       
   330             }
       
   331         }
       
   332     
       
   333     EndElementL(aNsUri, aLocalName, aQName); // 2004-07-17
       
   334     // now check if current state was to be preserved
       
   335     if(currentState!=KStateNotSet) // IOP
       
   336         {
       
   337         // restore the state preserved prior to
       
   338         // "set ignore for endelement" -case
       
   339         iState = currentState;
       
   340         }
       
   341     }
       
   342 
       
   343 EXPORT_C void CSenBaseFragment::StartElementL( const TDesC8& aNsUri,
       
   344                                                const TDesC8& aLocalName,
       
   345                                                const TDesC8& aQName,
       
   346                                                const RAttributeArray& aAttrs )
       
   347     {
       
   348     // 2004-09-10: check & alloc moved out
       
   349     // from KStateIgnore case code segment below (where in Java)
       
   350     if(!ipElement)
       
   351         {
       
   352         ipElement = CSenXmlElement::NewL( aNsUri, aLocalName, aQName );
       
   353         }
       
   354 
       
   355     // we can assume, that ipElement is not NULL
       
   356     const TDesC8& localName = ipElement->LocalName();
       
   357     const TDesC8& nsUri = ipElement->NamespaceURI();
       
   358     if(iState == KStateIgnore)
       
   359         {
       
   360         if(localName == aLocalName && nsUri == aNsUri) 
       
   361             {
       
   362             iState = KStateSave;
       
   363             SetAttributesL(aAttrs);
       
   364             }
       
   365         }
       
   366     else if((iState & KStateSave) == KStateSave)
       
   367         {
       
   368         if(localName == aLocalName && nsUri == aNsUri)
       
   369             {
       
   370             // start a new BaseFragment otherwise we loose
       
   371             // track of nested elements with the same name
       
   372             DelegateParsingL(aNsUri, aLocalName, aQName, aAttrs);
       
   373             }
       
   374         else
       
   375             {
       
   376             WriteStartElementL(aNsUri, aLocalName, aQName, aAttrs);
       
   377             }
       
   378         }
       
   379     }
       
   380 
       
   381 EXPORT_C void CSenBaseFragment::EndElementL( const TDesC8& aNsUri,
       
   382                                              const TDesC8& aLocalName,
       
   383                                              const TDesC8& aQName )
       
   384     {
       
   385     AllocContentSaverL();
       
   386     const TDesC8& localName = ipElement->LocalName();
       
   387     const TDesC8& nsUri = ipElement->NamespaceURI();
       
   388 
       
   389     if(localName == aLocalName && nsUri == aNsUri)
       
   390         {
       
   391         if(iState != KStateResume && ipOwner)
       
   392             {
       
   393             ipOwner->ResumeParsingFromL(aNsUri, aLocalName, aQName);
       
   394             return; // This is mandatory, since ResumeParsingFromL 
       
   395                     // de-allocates delegate fragment!
       
   396             }
       
   397         iState = KStateIgnore;
       
   398         }
       
   399     if((iState & KStateSave) == KStateSave)
       
   400         {
       
   401         WriteEndElementL(aNsUri, aLocalName, aQName);
       
   402         }
       
   403     }
       
   404 
       
   405 // Closely the same as the code in Java ref-impl's BaseElement.append()
       
   406 EXPORT_C void CSenBaseFragment::CharactersL( const TDesC8& aChars,
       
   407                                              TInt /*aStart*/,
       
   408                                              TInt /*aLength*/ )
       
   409     {
       
   410     if((iState & KStateSave) == KStateSave)
       
   411         {
       
   412         AllocContentSaverL(); // get iWs if it is NULL
       
   413 
       
   414         HBufC8* pCharsCopy = NULL;
       
   415 
       
   416         if (SenXmlUtils::EncodeHttpCharactersL(aChars, pCharsCopy))
       
   417             {
       
   418             CleanupStack::PushL(pCharsCopy);
       
   419             iWs.WriteL(*pCharsCopy);
       
   420             CleanupStack::PopAndDestroy(); // pCharsCopy
       
   421             }
       
   422         else
       
   423             {
       
   424             iWs.WriteL(aChars);
       
   425             }
       
   426         }
       
   427     }
       
   428 
       
   429 EXPORT_C void CSenBaseFragment::SetAttributesL( const RAttributeArray& aAttrs )
       
   430     {
       
   431     SaveNamespacesL(aAttrs, ETrue);
       
   432     // Otherwise ignore attributes, these are up to the subclass to deal with.
       
   433     }
       
   434 
       
   435 EXPORT_C void CSenBaseFragment::AllocContentSaverL()
       
   436     {
       
   437     if(!hasContentWriteStream)
       
   438         {
       
   439         iWs = ipElement->ContentWriteStreamL();
       
   440         hasContentWriteStream = ETrue;
       
   441         }
       
   442     }
       
   443 
       
   444 EXPORT_C TInt CSenBaseFragment::StartDocument()
       
   445     {
       
   446     return 1;
       
   447     }
       
   448 
       
   449 EXPORT_C TInt CSenBaseFragment::EndDocument()
       
   450     {
       
   451     if ( hasContentWriteStream )
       
   452         {
       
   453         hasContentWriteStream = EFalse;
       
   454         iWs.Close();
       
   455         }
       
   456         
       
   457     if (ipElement) ((CSenXmlElement*)ipElement)->Compress();
       
   458     return 1;
       
   459     }
       
   460 
       
   461 EXPORT_C TInt CSenBaseFragment::StartElement( const TDesC8& aNsUri,
       
   462                                               const TDesC8& aLocalName,
       
   463                                               const TDesC8& aQName,
       
   464                                               const RAttributeArray& aAttrs )
       
   465     {
       
   466     TRAPD(err, StartElementL(aNsUri, aLocalName, aQName, aAttrs));
       
   467     return (err == KErrNone);
       
   468     }
       
   469 
       
   470 EXPORT_C TInt CSenBaseFragment::EndElement( const TDesC8& aNsUri,
       
   471                                             const TDesC8& aLocalName,
       
   472                                             const TDesC8& aQName )
       
   473     {
       
   474     TRAPD(err, EndElementL(aNsUri, aLocalName, aQName));
       
   475     return (err == KErrNone);
       
   476     }
       
   477 
       
   478 EXPORT_C TInt CSenBaseFragment::Characters( const TDesC8& aChars,
       
   479                                             const TInt aStart,
       
   480                                             const TInt aLength)
       
   481     {
       
   482     TRAPD(err, CharactersL(aChars, aStart, aLength));
       
   483     return (err == KErrNone);
       
   484     }
       
   485 
       
   486 EXPORT_C void CSenBaseFragment::ResetContentL()
       
   487     {
       
   488     AsElement().SetContentL(KNullDesC8);
       
   489     // iPrefixes.Reset();   // contents not owned, clearing is enough // 2005-09-26: wouldn't this leak memory?
       
   490     iPrefixes.ResetAndDestroy(); // owned copies from parsed values..
       
   491     iNamespaces.Reset(); // contents not owned, clearing is enough
       
   492     }
       
   493 
       
   494 EXPORT_C TPtrC8 CSenBaseFragment::Content()
       
   495     {
       
   496     TPtrC8 retVal;
       
   497     TRAPD(err, retVal.Set(ContentL());)
       
   498 
       
   499     if (err != KErrNone)
       
   500         {
       
   501         retVal.Set(KNullDesC8);
       
   502         }
       
   503     return retVal;
       
   504     }
       
   505 
       
   506 TPtrC8 CSenBaseFragment::ContentL()
       
   507     {
       
   508     TPtrC8 content = AsElement().Content();
       
   509 
       
   510     TInt nsCount(iNamespaces.Count());
       
   511     if(nsCount>0)
       
   512         {
       
   513         TInt i = content.Locate('>');
       
   514         if( i > -1 )
       
   515             {
       
   516             if(!ipContentWithNamespaces)
       
   517                 {
       
   518                 ipContentWithNamespaces = CBufFlat::NewL(KFlatBufSize);
       
   519                 }
       
   520             else
       
   521                 {
       
   522                 ipContentWithNamespaces->Reset(); //discard the old data in buf
       
   523                 }
       
   524 
       
   525             i--; // make sure we are before a possible "/>"
       
   526             TInt cut = i;
       
   527             RBufWriteStream ws;
       
   528             CleanupClosePushL(ws);
       
   529             ws.Open(*ipContentWithNamespaces);
       
   530             ws.WriteL(content.Left(cut));
       
   531             for(TInt n=0; n<nsCount; n++)
       
   532                 {
       
   533                 CSenNamespace* pNamespace = (CSenNamespace*)iNamespaces[i];
       
   534                 pNamespace->InsertL(ws);
       
   535                 }
       
   536             ws.WriteL(content.Mid(cut));
       
   537             CleanupStack::PopAndDestroy(); // RBufWriteStream.Close() for ws
       
   538             TPtr8 p8 = ipContentWithNamespaces->Ptr(0);
       
   539             return p8;
       
   540             }
       
   541         }
       
   542         else
       
   543             {
       
   544             return content;
       
   545             }
       
   546     return content;
       
   547     }
       
   548 
       
   549 EXPORT_C HBufC* CSenBaseFragment::AsXmlUnicodeL()
       
   550     {
       
   551     return AsElement().AsXmlUnicodeL();
       
   552     }
       
   553 
       
   554 EXPORT_C HBufC8* CSenBaseFragment::AsXmlL()
       
   555     {
       
   556     return AsElement().AsXmlL();
       
   557     }
       
   558 
       
   559 EXPORT_C void CSenBaseFragment::WriteAsXMLToL( RWriteStream& aWs )
       
   560     {
       
   561     AsElement().WriteAsXMLToL( aWs );
       
   562     }
       
   563 
       
   564 EXPORT_C TPtrC8 CSenBaseFragment::ContentOf( const TDesC8& aLocalName )
       
   565     {
       
   566     CSenElement* child = this->AsElement().Element(aLocalName);
       
   567     if(child)
       
   568         {
       
   569         return child->Content();
       
   570         }
       
   571     return KNullDesC8();
       
   572     }
       
   573 
       
   574 EXPORT_C MSenElement& CSenBaseFragment::SetContentOfL( const TDesC8& aLocalName,
       
   575                                                        const TDesC8& aContent )
       
   576     {
       
   577     CSenElement* pChildElement = AsElement().Element( aLocalName );
       
   578     if(!pChildElement)
       
   579         {
       
   580         // Add new element
       
   581         pChildElement =  &AsElement().AddElementL(aLocalName);
       
   582         }
       
   583     pChildElement->SetContentL(aContent);
       
   584     return *pChildElement;
       
   585     }
       
   586 
       
   587 EXPORT_C void CSenBaseFragment::WriteStartElementL( const TDesC8& /* aNsUri */,
       
   588                                                     const TDesC8& aLocalName,
       
   589                                                     const TDesC8& aQName,
       
   590                                                     const RAttributeArray& aAttrs )
       
   591     {
       
   592     AllocContentSaverL();
       
   593 
       
   594     // Write element name with namespace
       
   595     iWs.WriteL(KSenLessThan);
       
   596     if (aQName != KNullDesC8)
       
   597         {
       
   598         iWs.WriteL(aQName);
       
   599         }
       
   600     else
       
   601         {
       
   602         iWs.WriteL(aLocalName);
       
   603         }
       
   604 
       
   605     // Write attributes, if any
       
   606     HBufC8* pQName = NULL;
       
   607     TInt attCount(aAttrs.Count());
       
   608     for (TInt i = 0; i < attCount; i++)
       
   609             {
       
   610         TPtrC8 prefix = aAttrs[i].Attribute().Prefix().DesC();
       
   611         TPtrC8 lAttName = aAttrs[i].Attribute().LocalName().DesC();
       
   612         TPtrC8 attValue = aAttrs[i].Value().DesC();
       
   613         
       
   614         TPtrC8 qAttName = lAttName;
       
   615         if(prefix!=KNullDesC8)
       
   616             {
       
   617             pQName = HBufC8::NewLC( prefix.Length()
       
   618                         +KSenColon().Length()
       
   619                         +lAttName.Length());
       
   620             TPtr8 qname = pQName->Des();
       
   621             qname.Append(prefix);
       
   622             qname.Append(KSenColon);
       
   623             qname.Append(lAttName);
       
   624             qAttName.Set(qname);
       
   625             }
       
   626         
       
   627         
       
   628         iWs.WriteL(KSenSpace);
       
   629         iWs.WriteL(qAttName); // this is localname if no qualifier is set
       
   630         iWs.WriteL(KSenEqualsDblQuot);
       
   631         iWs.WriteL(attValue);
       
   632         iWs.WriteL(KSenDblQuot);
       
   633         
       
   634         TInt colon(KErrNotFound);
       
   635         colon = qAttName.Locate(':');
       
   636         // We check for namespaces declared here and for prefixes 
       
   637         // in attribute names and values.
       
   638         if (qAttName == KSenXmlns || lAttName == KSenXmlns)
       
   639             {
       
   640             // locally declared default namespace, 
       
   641             // no need to do anything special
       
   642             }
       
   643         else if(prefix == KSenXmlns) // was SenXmlUtils::StartsWith(...)
       
   644             {
       
   645             //locally declared prefix, remember that this prefix is OK
       
   646             HBufC8* pAttName = lAttName.AllocL();
       
   647             CleanupStack::PushL(pAttName);
       
   648 #ifdef EKA2
       
   649             iPrefixes.AppendL(pAttName);
       
   650 #else
       
   651             User::LeaveIfError(iPrefixes.Append(pAttName));
       
   652 #endif
       
   653             CleanupStack::Pop(); // pAttName
       
   654             }
       
   655         else if(colon != KErrNotFound)
       
   656             {
       
   657             //a qualified attribute name, ensure we have its namespace
       
   658             EnsureNamespace(qAttName.Left(colon));
       
   659             }
       
   660         
       
   661         colon = attValue.Locate(':');
       
   662         if(colon != KErrNotFound)
       
   663             {
       
   664             //encountered a qualified value, should ensure the namespace for
       
   665             //its prefix is included.
       
   666             EnsureNamespace(attValue.Left(colon));
       
   667             }
       
   668         if(prefix!=KNullDesC8)
       
   669             {       
       
   670             CleanupStack::PopAndDestroy(); // qAttName
       
   671             pQName = NULL;
       
   672             }
       
   673         }
       
   674 
       
   675     iWs.WriteL(KSenGreaterThan);
       
   676 
       
   677 
       
   678     if(aQName != KNullDesC8)
       
   679         {
       
   680         TInt colon(aQName.Locate(':'));
       
   681         if(colon != KErrNotFound)
       
   682             {
       
   683             EnsureNamespace(aQName.Left(colon));
       
   684             }
       
   685         }
       
   686 
       
   687     // Is this truly required!?
       
   688     if(aLocalName != KNullDesC8)
       
   689         {
       
   690         TInt colon(aLocalName.Locate(':'));
       
   691         if(colon != KErrNotFound)
       
   692             {
       
   693             EnsureNamespace(aLocalName.Left(colon));
       
   694             }
       
   695         }
       
   696     
       
   697     }
       
   698 
       
   699 EXPORT_C void CSenBaseFragment::WriteEndElementL( const TDesC8& /* aNsUri */,
       
   700                                                   const TDesC8& /* aLocalName */,
       
   701                                                   const TDesC8& aQName )
       
   702     {
       
   703     AllocContentSaverL();
       
   704     iWs.WriteL(KSenLessThanSlash);
       
   705     iWs.WriteL(aQName);
       
   706     iWs.WriteL(KSenGreaterThan);
       
   707     }
       
   708 
       
   709 EXPORT_C void CSenBaseFragment::EnsureNamespace( const TDesC8& aPrefix )
       
   710     {
       
   711     /* Check that the prefix has been declared.
       
   712      * If not try to find a namespace declaration for it
       
   713      * in the element.
       
   714      */
       
   715     TInt pos(KErrNotFound);
       
   716     TInt count(iPrefixes.Count());
       
   717     for(TInt i=0; i<count && pos == KErrNotFound; i++)
       
   718         {
       
   719         if(*iPrefixes[i] == aPrefix)
       
   720             {
       
   721             pos = i;
       
   722             break;
       
   723             }
       
   724         }
       
   725 
       
   726     if (pos == KErrNotFound)
       
   727         {
       
   728         const CSenNamespace* pNamespace = Namespace(aPrefix);
       
   729         if (!pNamespace)
       
   730             {
       
   731             pNamespace = ipElement->Namespace(aPrefix);
       
   732 
       
   733             if (pNamespace)
       
   734                 {
       
   735                 iNamespaces.Append(pNamespace); // no need for copying
       
   736                 }
       
   737             }
       
   738         }
       
   739     }
       
   740 
       
   741 EXPORT_C CSenNamespace* CSenBaseFragment::Namespace( const TDesC8& aPrefix )
       
   742     {
       
   743     if (aPrefix.Length() == 0) return NULL;
       
   744 
       
   745     CSenNamespace* pNamespace = NULL;
       
   746     TInt count(iNamespaces.Count());
       
   747     for (TInt i=0; i<count && !pNamespace; i++)
       
   748         {
       
   749         pNamespace = (CSenNamespace*) iNamespaces[i];
       
   750         if (pNamespace->Prefix() != aPrefix) pNamespace = NULL;
       
   751         }
       
   752 
       
   753     return pNamespace;
       
   754     }
       
   755 
       
   756 EXPORT_C void CSenBaseFragment::DetachL()
       
   757     {
       
   758     //First add all the namespaces referred to in the content.
       
   759     if (iNamespaces.Count() > 0)
       
   760         {
       
   761         TInt count(iNamespaces.Count());
       
   762         for (TInt i=0; i<count; i++)
       
   763             {
       
   764             CSenNamespace* pNamespace = (CSenNamespace*) iNamespaces[i];
       
   765             ipElement->AddNamespaceL(*pNamespace, EFalse);
       
   766             //TRAPD(err, ipElement->AddNamespaceL(*pNamespace, EFalse));
       
   767             //if (err) ;
       
   768             }
       
   769         }
       
   770     //Detach the element from its parent.
       
   771     ipElement->DetachL();
       
   772     }
       
   773 
       
   774 EXPORT_C void CSenBaseFragment::SaveNamespacesL( const RAttributeArray& aAttrs,
       
   775                                                  TBool aEnsure )
       
   776     {
       
   777     // we check for namespace declarations in here
       
   778     // for prefixes in attribute names and values.
       
   779 
       
   780     if (ipElement)
       
   781         {
       
   782         CSenElement* pElement = (CSenXmlElement*)ipElement;
       
   783 
       
   784         TInt colon = KErrNotFound;
       
   785         TInt length(aAttrs.Count());
       
   786 
       
   787         for (TInt i=0; i<length; i++)
       
   788             {
       
   789             TPtrC8 localName = aAttrs[i].Attribute().LocalName().DesC();
       
   790             TPtrC8 value = aAttrs[i].Value().DesC();
       
   791             TPtrC8 prefix = aAttrs[i].Attribute().Prefix().DesC();
       
   792             
       
   793             if (localName == KSenXmlns())
       
   794                 {
       
   795                 // this is a default name space declaration
       
   796                 pElement->SetNamespaceL(value);
       
   797                 }
       
   798             else if (localName == pElement->NsPrefix())
       
   799                 {
       
   800                 //we have a new declaration for the namespace of this element
       
   801                 pElement->SetNamespaceL(localName,value);
       
   802                 }
       
   803             else if (prefix == KSenXmlns())
       
   804                 {
       
   805                 // we have an additional namespace declaration
       
   806                 pElement->AddNamespaceL(localName,value);
       
   807                 }
       
   808 
       
   809             if (aEnsure)
       
   810                 {
       
   811                 //colon = qualifiedName.Locate(':');
       
   812                 //if (colon > -1)
       
   813                 if(localName!=KNullDesC8)
       
   814                     {
       
   815                     // attribute name is prefixed so ensure we have its
       
   816                     // namespace
       
   817                     //EnsureNamespace(qualifiedName.Mid(0,colon));
       
   818 
       
   819                     // attribute's name is elements prefix! // 2005-02-22
       
   820                     EnsureNamespace(localName);
       
   821                     }
       
   822 
       
   823                 colon = value.Locate(':');
       
   824                 if (colon > -1)
       
   825                     {
       
   826                     // attribute value is prefixed so ensure we have that
       
   827                     // namespace
       
   828                     EnsureNamespace(value.Mid(0,colon));
       
   829                     }
       
   830                 }
       
   831             }
       
   832         }
       
   833     }
       
   834 
       
   835 
       
   836 // New
       
   837 EXPORT_C const TDesC8& CSenBaseFragment::LocalName() const
       
   838     {
       
   839     __ASSERT_ALWAYS(ipElement != NULL, User::Panic(KSenXmlPanic, EFragmentElementNotInitialized));
       
   840     return ipElement->LocalName();
       
   841     }
       
   842 
       
   843 EXPORT_C const TDesC8& CSenBaseFragment::NsUri() const
       
   844     {
       
   845     __ASSERT_ALWAYS(ipElement != NULL, User::Panic(KSenXmlPanic, EFragmentElementNotInitialized));
       
   846     return ipElement->NamespaceURI();
       
   847     }
       
   848 
       
   849 EXPORT_C const TDesC8& CSenBaseFragment::NsPrefix() const
       
   850     {
       
   851     __ASSERT_ALWAYS(ipElement != NULL, User::Panic(KSenXmlPanic, EFragmentElementNotInitialized));
       
   852     return ipElement->NsPrefix();
       
   853     }
       
   854 
       
   855 // Do NOT change method signature; BC must be kept!
       
   856 EXPORT_C TInt CSenBaseFragment::StartEntity( TDesC8& aName )
       
   857     {
       
   858 #ifndef SYMBIAN_SECURE_ECOM        
       
   859     // This segment is required with Compact XML reader
       
   860     TInt leaveCode(KErrNone);
       
   861     if((iState & KStateSave) == KStateSave)
       
   862         {
       
   863         TRAP(leaveCode, AllocContentSaverL());
       
   864         if(leaveCode==KErrNone)
       
   865             {
       
   866             TRAP(leaveCode, iWs.WriteL(aName));
       
   867             }
       
   868         }
       
   869     if(leaveCode==KErrNone)
       
   870         {
       
   871         return ETrue; // success
       
   872         }
       
   873     else
       
   874         {
       
   875         return EFalse; // failure
       
   876         }
       
   877 #else // SYMBIAN_SECURE_ECOM       
       
   878     aName.Length();
       
   879     return KErrNone; // signal was successfully handled
       
   880 #endif // SYMBIAN_SECURE_ECOM    
       
   881     }
       
   882 
       
   883 
       
   884 EXPORT_C TInt CSenBaseFragment::Error( TInt /* aErrorCode */ )
       
   885     {
       
   886     //return aErrorCode;
       
   887     return KErrNone; // Error was "successfully" handled
       
   888     }
       
   889 
       
   890 EXPORT_C TBool CSenBaseFragment::ConsistsOfL( MSenFragment& aCandidate )
       
   891     {
       
   892     return AsElement().ConsistsOfL(aCandidate.AsElement());
       
   893     }
       
   894 
       
   895 
       
   896 // End of File