simpleengine/xmlutils/src/simpleelement.cpp
changeset 0 c8caa15ef882
child 18 52d91a16fec3
equal deleted inserted replaced
-1:000000000000 0:c8caa15ef882
       
     1 /*
       
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:    Simple Engine
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #include <e32std.h>
       
    24 #include <SenBaseElement.h>
       
    25 #include <SenBaseAttribute.h>
       
    26 #include <SenXmlUtils.h>
       
    27 
       
    28 // own simple
       
    29 #include "simpleelement.h"
       
    30 #include "simplenamespace.h"
       
    31 #include "simpleattribute.h"
       
    32 #include "msimplenamespace.h"
       
    33 
       
    34 // ================= MEMBER FUNCTIONS =======================
       
    35 //
       
    36 
       
    37 // ----------------------------------------------------------
       
    38 // CSimpleElement::CSimpleElement
       
    39 // ----------------------------------------------------------
       
    40 //
       
    41 CSimpleElement::CSimpleElement( )
       
    42 : iBase(NULL), iRef(NULL)
       
    43     {
       
    44     }
       
    45 
       
    46 // ----------------------------------------------------------
       
    47 // CSimpleElement::~CSimpleElement
       
    48 // ----------------------------------------------------------
       
    49 //
       
    50 CSimpleElement::~CSimpleElement()
       
    51     {
       
    52     iAttributes.ResetAndDestroy();  
       
    53     iElements.ResetAndDestroy();      
       
    54     delete iBase;
       
    55     }
       
    56 
       
    57 // ----------------------------------------------------------
       
    58 // CSimpleElement::ConstructL
       
    59 // ----------------------------------------------------------
       
    60 //
       
    61 void CSimpleElement::ConstructL(
       
    62     const TDesC8& aNsUri,
       
    63   const TDesC8& aLocalName )
       
    64     {
       
    65     if ( aNsUri.Length() )
       
    66         {
       
    67         iBase = CSenBaseElement::NewL( aNsUri, aLocalName);
       
    68         }
       
    69     else
       
    70         {
       
    71         iBase = CSenBaseElement::NewL( aLocalName );
       
    72         }
       
    73     }
       
    74 
       
    75 // ----------------------------------------------------------
       
    76 // CSimpleElement::ConstructL
       
    77 // ----------------------------------------------------------
       
    78 //
       
    79 void CSimpleElement::ConstructL(
       
    80     CSenElement* aElem, TBool aOwn )
       
    81     {
       
    82     if ( aOwn )
       
    83         {
       
    84         iBase = aElem;
       
    85         }
       
    86     else
       
    87         {
       
    88         iRef = aElem;
       
    89         }
       
    90 
       
    91     }
       
    92 
       
    93 // ----------------------------------------------------------
       
    94 // CSimpleElement::NewL
       
    95 // ----------------------------------------------------------
       
    96 //
       
    97 CSimpleElement* CSimpleElement::NewL(
       
    98     const TDesC8& aNsUri,
       
    99     const TDesC8& aLocalName )
       
   100     {
       
   101     CSimpleElement* self = new (ELeave) CSimpleElement( );
       
   102     CleanupStack::PushL( self );
       
   103     self->ConstructL( aNsUri, aLocalName );
       
   104     CleanupStack::Pop( self );
       
   105     return self;
       
   106     }
       
   107 
       
   108 // ----------------------------------------------------------
       
   109 // CSimpleElement::NewL
       
   110 // ----------------------------------------------------------
       
   111 //
       
   112 CSimpleElement* CSimpleElement::NewL(
       
   113     CSenElement* aElement, TBool aOwn )
       
   114     {
       
   115     CSimpleElement* self = new (ELeave) CSimpleElement( );
       
   116     CleanupStack::PushL( self );
       
   117     self->ConstructL( aElement, aOwn );
       
   118     CleanupStack::Pop( self );
       
   119     return self;
       
   120     }
       
   121 
       
   122 // ----------------------------------------------------------
       
   123 // CSimpleElement::BaseElement
       
   124 // ----------------------------------------------------------
       
   125 //
       
   126 CSenElement* CSimpleElement::BaseElement()
       
   127     {
       
   128     return iBase ? iBase : iRef;
       
   129     }
       
   130 
       
   131 // ----------------------------------------------------------
       
   132 // CSimpleElement::Close
       
   133 // ----------------------------------------------------------
       
   134 //
       
   135 void CSimpleElement::Close()
       
   136     {
       
   137     delete this;
       
   138     }
       
   139 
       
   140 // ----------------------------------------------------------
       
   141 // CSimpleElement::LocalName
       
   142 // ----------------------------------------------------------
       
   143 //
       
   144 const TDesC8& CSimpleElement::LocalName()
       
   145     {
       
   146     return BaseElement()->LocalName();
       
   147     }
       
   148 
       
   149 // ----------------------------------------------------------
       
   150 // CSimpleElement::DefNamespaceL
       
   151 // ----------------------------------------------------------
       
   152 //
       
   153 MSimpleNamespace* CSimpleElement::DefNamespaceL()
       
   154     {
       
   155     const CSenNamespace* ns = BaseElement()->Namespace();
       
   156     if ( !ns )
       
   157         {
       
   158         return NULL;
       
   159         }
       
   160     CSimpleNamespace* simpleNs = CSimpleNamespace::NewL( ns->Prefix(), ns->URI() );
       
   161     return simpleNs;
       
   162     }
       
   163 
       
   164 // ----------------------------------------------------------
       
   165 // CSimpleElement::HasContent
       
   166 // ----------------------------------------------------------
       
   167 //
       
   168 TBool CSimpleElement::HasContent()
       
   169     {
       
   170     return BaseElement()->HasContent();
       
   171     }
       
   172 
       
   173 // ----------------------------------------------------------
       
   174 // CSimpleElement::ContentUnicodeL
       
   175 // ----------------------------------------------------------
       
   176 //
       
   177 HBufC* CSimpleElement::ContentUnicodeL()
       
   178     {
       
   179     // UTF-8 -> Unicode conversion   
       
   180     TRAPD( errx, SenXmlUtils::LeaveOnXmlEscapesL( BaseElement()->Content()));
       
   181     if ( errx )
       
   182         {
       
   183     	HBufC8* b8 = SenXmlUtils::DecodeHttpCharactersLC( BaseElement()->Content());
       
   184         HBufC16* uniB = SenXmlUtils::ToUnicodeLC( b8->Des() );
       
   185         CleanupStack::Pop( uniB );
       
   186         CleanupStack::PopAndDestroy( b8 );
       
   187         return uniB;    	        
       
   188         }
       
   189     else
       
   190         {
       
   191         return BaseElement()->ContentUnicodeL();
       
   192         }        
       
   193     }
       
   194     
       
   195 // ----------------------------------------------------------
       
   196 // CSimpleElement::SetContentUnicodeL
       
   197 // ----------------------------------------------------------
       
   198 //
       
   199 void CSimpleElement::SetContentUnicodeL( const TDesC& aContent )
       
   200     {
       
   201     // Unicode -> UTF-8 conversion    		    
       
   202     HBufC8* b8 = SenXmlUtils::ToUtf8LC( aContent);
       
   203     TRAPD( errx, SenXmlUtils::LeaveOnXmlEscapesL( b8->Des()));
       
   204     if ( errx )
       
   205         {
       
   206     	HBufC8* b8escaped = SenXmlUtils::EncodeHttpCharactersLC( b8->Des());
       
   207         BaseElement()->SetContentL( b8escaped->Des() );
       
   208         CleanupStack::PopAndDestroy( b8escaped );
       
   209         }
       
   210      else
       
   211         {
       
   212         BaseElement()->SetContentL( b8->Des() );        
       
   213         }
       
   214     CleanupStack::PopAndDestroy( b8);  
       
   215     }
       
   216     
       
   217 // ----------------------------------------------------------
       
   218 // CSimpleElement::SimpleElementsL
       
   219 // ----------------------------------------------------------
       
   220 //
       
   221 TInt CSimpleElement::SimpleElementsL( 
       
   222     RPointerArray<MSimpleElement>& aElementArray )
       
   223     {
       
   224     // Reset the output array first.
       
   225     aElementArray.Reset();
       
   226     iElements.ResetAndDestroy();
       
   227 
       
   228     // Go through CSenElements and convert them one by one and 
       
   229     // add to the output array.
       
   230     RPointerArray<CSenElement>& elems = BaseElement()->ElementsL();
       
   231     TInt elemCount = elems.Count();
       
   232     for ( TInt i = 0; i < elemCount; i++ )
       
   233         {
       
   234         CSenElement* pElement = elems[i];
       
   235         CSimpleElement* simple = CSimpleElement::NewL( pElement, EFalse );
       
   236         CleanupStack::PushL( simple );
       
   237         User::LeaveIfError( aElementArray.Append( simple ));
       
   238         CleanupStack::Pop( simple );
       
   239         User::LeaveIfError( iElements.Append( simple ) );
       
   240         }
       
   241     return elemCount ? KErrNone : KErrNotFound;
       
   242     }
       
   243 
       
   244 // ----------------------------------------------------------
       
   245 // CSimpleElement::AttrValueLC
       
   246 // ----------------------------------------------------------
       
   247 //
       
   248 HBufC* CSimpleElement::AttrValueLC( const TDesC8& aName )
       
   249     {
       
   250     // Get 8-bit data, ownership is not transferred
       
   251     const TDesC8* p8 = BaseElement()->AttrValue( aName );
       
   252     // UTF-8 -> Unicode conversion
       
   253     // new buffer is created
       
   254     HBufC16* b16 = SenXmlUtils::ToUnicodeLC( *p8 );
       
   255     return b16;
       
   256     }
       
   257 
       
   258 // ----------------------------------------------------------
       
   259 // CSimpleElement::AttrValue
       
   260 // ----------------------------------------------------------
       
   261 //
       
   262 const TDesC8* CSimpleElement::AttrValue( const TDesC8& aName )
       
   263     {
       
   264     // Get 8-bit data, ownership is not transferred
       
   265     return BaseElement()->AttrValue( aName );
       
   266     }
       
   267 
       
   268 // ----------------------------------------------------------
       
   269 // CSimpleElement::AddAttrL
       
   270 // ----------------------------------------------------------
       
   271 //
       
   272 void CSimpleElement::AddAttrL( const TDesC8& aName, const TDesC& aValue )
       
   273     {
       
   274     // Unicode -> UTF-8 conversion
       
   275     HBufC8* b8 = SenXmlUtils::ToUtf8LC( aValue);
       
   276     BaseElement()->AddAttrL( aName, b8->Des() );
       
   277     CleanupStack::PopAndDestroy( b8 );
       
   278     }
       
   279 
       
   280 // ----------------------------------------------------------
       
   281 // CSimpleElement::AddAttr8L
       
   282 // ----------------------------------------------------------
       
   283 //
       
   284 void CSimpleElement::AddAttr8L( const TDesC8& aName, const TDesC8& aValue )
       
   285     {
       
   286     BaseElement()->AddAttrL( aName, aValue );
       
   287     }
       
   288 
       
   289 // ----------------------------------------------------------
       
   290 // CSimpleElement::SimpleAttributesL
       
   291 // ----------------------------------------------------------
       
   292 //
       
   293 TInt CSimpleElement::SimpleAttributesL( RPointerArray<MSimpleAttribute>& aArray )
       
   294     {  
       
   295     // Reset the output array first.
       
   296     aArray.Reset();
       
   297     iAttributes.ResetAndDestroy();
       
   298 
       
   299     // Go through CSenAttributes and convert them one by one and add to the output array.
       
   300     RPointerArray<CSenBaseAttribute>& elems = BaseElement()->AttributesL();
       
   301     TInt elemCount = elems.Count();
       
   302     for ( TInt i = 0; i < elemCount; i++ )
       
   303         {
       
   304         CSenBaseAttribute* pAttr = elems[i];
       
   305         CSimpleAttribute* attr = CSimpleAttribute::NewL( pAttr );
       
   306         CleanupStack::PushL( attr );
       
   307         User::LeaveIfError(iAttributes.Append( attr ));
       
   308         CleanupStack::Pop( attr );        
       
   309         User::LeaveIfError(aArray.Append( attr ));
       
   310         }
       
   311     return elemCount ? KErrNone : KErrNotFound;
       
   312     }
       
   313 
       
   314 // ----------------------------------------------------------
       
   315 // CSimpleElement::SimpleParentL
       
   316 // OWNERSHIP IS TRANSFERRED
       
   317 // ----------------------------------------------------------
       
   318 //
       
   319 CSimpleElement* CSimpleElement::SimpleParentL()
       
   320     {
       
   321     CSenElement* el = BaseElement()->Parent();
       
   322     if ( !el )
       
   323         {
       
   324         return NULL;
       
   325         }   
       
   326     CSimpleElement* simple = CSimpleElement::NewL( el, ETrue );
       
   327     return simple;
       
   328     }
       
   329 
       
   330 // ----------------------------------------------------------
       
   331 // CSimpleElement::DetachSimpleL
       
   332 // ----------------------------------------------------------
       
   333 //
       
   334 void CSimpleElement::DetachSimpleL()
       
   335     {
       
   336     // Ownership in not transferred.
       
   337     CSenElement* e = BaseElement()->DetachL();
       
   338     if (!e)
       
   339         {
       
   340         User::Leave( KErrNotFound );
       
   341         }
       
   342     }
       
   343 
       
   344 // ----------------------------------------------------------
       
   345 // CSimpleElement::AddSimpleElementL
       
   346 // ----------------------------------------------------------
       
   347 //
       
   348 MSimpleElement* CSimpleElement::AddSimpleElementL(
       
   349     const TDesC8& aNsUri,
       
   350     const TDesC8& aLocalName )
       
   351     {
       
   352     CSenElement& e = BaseElement()->AddElementL( aNsUri, aLocalName );
       
   353     CSimpleElement* simple = CSimpleElement::NewL( &e, EFalse );
       
   354     return simple;
       
   355     }
       
   356 
       
   357 // ----------------------------------------------------------
       
   358 // CSimpleElement::AddSimpleElementL
       
   359 // ----------------------------------------------------------
       
   360 //
       
   361 MSimpleElement* CSimpleElement::AddSimpleElementL(
       
   362     const TDesC8& aLocalName )
       
   363     {
       
   364     CSenElement& e = BaseElement()->AddElementL( aLocalName );
       
   365     CSimpleElement* simple = CSimpleElement::NewL( &e, EFalse );
       
   366     return simple;
       
   367     }
       
   368 
       
   369 // ----------------------------------------------------------
       
   370 // CSimpleElement::RemoveSimpleElement
       
   371 // ----------------------------------------------------------
       
   372 //
       
   373 void CSimpleElement::RemoveSimpleElement(
       
   374     const TDesC8& aNsUri,
       
   375     const TDesC8& aLocalName )
       
   376     {
       
   377     CSenElement* e = BaseElement()->RemoveElement( aNsUri, aLocalName );
       
   378     delete e;
       
   379     }
       
   380 
       
   381