|         |      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 <s32strm.h> | 
|         |     25 #include <SenBaseElement.h> | 
|         |     26 #include <SenBaseAttribute.h> | 
|         |     27 #include <SenXmlUtils.h> | 
|         |     28 #include <SenXmlReader.h> | 
|         |     29 #include <SenDomFragment.h> | 
|         |     30  | 
|         |     31 // own simple | 
|         |     32 #include "simplecommon.h" | 
|         |     33 #include "simpleelement.h" | 
|         |     34 #include "simplenamespace.h" | 
|         |     35 #include "simpleattribute.h" | 
|         |     36 #include "simplebasedocument.h" | 
|         |     37 #include "simpleutils.h" | 
|         |     38  | 
|         |     39 // ================= MEMBER FUNCTIONS ======================= | 
|         |     40 // | 
|         |     41  | 
|         |     42 // ---------------------------------------------------------- | 
|         |     43 // CSimpleBaseDocument::CSimpleBaseDocument | 
|         |     44 // ---------------------------------------------------------- | 
|         |     45 // | 
|         |     46 CSimpleBaseDocument::CSimpleBaseDocument( ) | 
|         |     47 : iRoot( NULL ) | 
|         |     48     { | 
|         |     49     } | 
|         |     50  | 
|         |     51 // ---------------------------------------------------------- | 
|         |     52 // CSimpleBaseDocument::~CSimpleBaseDocument | 
|         |     53 // ---------------------------------------------------------- | 
|         |     54 // | 
|         |     55 CSimpleBaseDocument::~CSimpleBaseDocument() | 
|         |     56     { | 
|         |     57     // Reset and delete namespace C-class entities. | 
|         |     58     iNsps.ResetAndDestroy(); | 
|         |     59     // Just reset array of M-class entities,  | 
|         |     60     // do not try to delete entities. | 
|         |     61     iMNsps.Reset(); | 
|         |     62     // Delete root entity. | 
|         |     63     delete iRoot; | 
|         |     64     } | 
|         |     65  | 
|         |     66 // ---------------------------------------------------------- | 
|         |     67 // CSimpleBaseDocument::BaseConstructL | 
|         |     68 // ---------------------------------------------------------- | 
|         |     69 // | 
|         |     70 void CSimpleBaseDocument::BaseConstructL( | 
|         |     71     const TDesC8& aNsUri, | 
|         |     72     const TDesC8& aLocalName ) | 
|         |     73     { | 
|         |     74     iRoot = CSimpleElement::NewL( aNsUri, aLocalName ); | 
|         |     75     } | 
|         |     76  | 
|         |     77 // ---------------------------------------------------------- | 
|         |     78 // CSimpleBaseDocument::BaseConstructL | 
|         |     79 // ---------------------------------------------------------- | 
|         |     80 // | 
|         |     81 void CSimpleBaseDocument::BaseConstructL( | 
|         |     82     const TDesC8& aXml ) | 
|         |     83     {        | 
|         |     84     CSenXmlReader* reader = CSenXmlReader::NewL(); | 
|         |     85     CleanupStack::PushL(reader); | 
|         |     86     //create a CSenDomFragment | 
|         |     87     CSenDomFragment* pBase = CSenDomFragment::NewL(); | 
|         |     88     CleanupStack::PushL( pBase ); | 
|         |     89  | 
|         |     90     //must set the content handler | 
|         |     91     reader->SetContentHandler( *pBase ); | 
|         |     92     // and the reader | 
|         |     93     pBase->SetReader( *reader ); | 
|         |     94     //do the parsing | 
|         |     95     reader->ParseL( aXml ); | 
|         |     96  | 
|         |     97     // Get the root element | 
|         |     98     // OWNERSHIP IS TRANSFERRED | 
|         |     99     // | 
|         |    100     CSenElement* e = pBase->ExtractElement(); | 
|         |    101     const TDesC8& p8 = e->LocalName(); | 
|         |    102     ValidateXmlL( p8 ); | 
|         |    103     // This is a document root | 
|         |    104     iRoot = CSimpleElement::NewL( e, ETrue ); | 
|         |    105  | 
|         |    106     CleanupStack::PopAndDestroy( pBase ); | 
|         |    107     CleanupStack::PopAndDestroy( reader ); | 
|         |    108     } | 
|         |    109  | 
|         |    110 // ---------------------------------------------------------- | 
|         |    111 // CSimpleBaseDocument::DefaultNamespace | 
|         |    112 // ---------------------------------------------------------- | 
|         |    113 // | 
|         |    114 TPtrC8 CSimpleBaseDocument::DefaultNamespace() | 
|         |    115     { | 
|         |    116     TInt err(KErrNone); | 
|         |    117     TPtrC8 p8; | 
|         |    118     TRAP( err, p8.Set( DoDefaultNamespaceL() )); | 
|         |    119     if ( err ) | 
|         |    120         { | 
|         |    121         return TPtrC8(); | 
|         |    122         } | 
|         |    123     return p8; | 
|         |    124     } | 
|         |    125  | 
|         |    126 // ---------------------------------------------------------- | 
|         |    127 // CSimpleBaseDocument::AddNamespaceL | 
|         |    128 // ---------------------------------------------------------- | 
|         |    129 // | 
|         |    130 void CSimpleBaseDocument::AddNamespaceL( | 
|         |    131     const TDesC8& aPrefix, | 
|         |    132     const TDesC8& aUri ) | 
|         |    133     { | 
|         |    134     iRoot->BaseElement()->AddNamespaceL( aPrefix, aUri ); | 
|         |    135     } | 
|         |    136  | 
|         |    137 // ---------------------------------------------------------- | 
|         |    138 // CSimpleBaseDocument::NamespacesL | 
|         |    139 // ---------------------------------------------------------- | 
|         |    140 // | 
|         |    141 RPointerArray<MSimpleNamespace>& CSimpleBaseDocument::NamespacesL() | 
|         |    142     { | 
|         |    143     // Notice:  | 
|         |    144     // We have to maintain two arrays, one for C-classes and another | 
|         |    145     // for M-classes, since deletion of M-classes does not work. | 
|         |    146     // This is not very good design to return RpointerArray of | 
|         |    147     // M-classes, but let's not change API for this minor drawback. | 
|         |    148       | 
|         |    149     // reset old stuff | 
|         |    150     iNsps.ResetAndDestroy(); | 
|         |    151     iMNsps.Reset(); | 
|         |    152  | 
|         |    153     RPointerArray<CSenNamespace>& nsa = iRoot->BaseElement()->NamespacesL(); | 
|         |    154  | 
|         |    155     TInt elemCount = nsa.Count(); | 
|         |    156     for ( TInt i = 0; i < elemCount; i++ ) | 
|         |    157         { | 
|         |    158         CSenNamespace* ns = nsa[i]; | 
|         |    159         CSimpleNamespace* simpleNs = CSimpleNamespace::NewL( ns->Prefix(), ns->URI() ); | 
|         |    160         User::LeaveIfError( iNsps.Append( simpleNs ) ); | 
|         |    161         User::LeaveIfError( iMNsps.Append( simpleNs ) ); | 
|         |    162         } | 
|         |    163  | 
|         |    164     return iMNsps; | 
|         |    165     } | 
|         |    166  | 
|         |    167 // ---------------------------------------------------------- | 
|         |    168 // CSimpleBaseDocument::ExternalizeL | 
|         |    169 // ---------------------------------------------------------- | 
|         |    170 // | 
|         |    171 void CSimpleBaseDocument::ExternalizeL( RWriteStream& aStream ) | 
|         |    172     { | 
|         |    173     aStream.WriteL( KSimpleXmlStart ); | 
|         |    174     iRoot->BaseElement()->WriteAsXMLToL( aStream); | 
|         |    175     } | 
|         |    176  | 
|         |    177 // ---------------------------------------------------------- | 
|         |    178 // CSimpleBaseDocument::Close | 
|         |    179 // ---------------------------------------------------------- | 
|         |    180 // | 
|         |    181 void CSimpleBaseDocument::Close() | 
|         |    182     { | 
|         |    183     delete this; | 
|         |    184     } | 
|         |    185  | 
|         |    186 // ---------------------------------------------------------- | 
|         |    187 // CSimpleBaseDocument::LocalName | 
|         |    188 // ---------------------------------------------------------- | 
|         |    189 // | 
|         |    190 const TDesC8& CSimpleBaseDocument::LocalName() | 
|         |    191     { | 
|         |    192     return iRoot->LocalName(); | 
|         |    193     } | 
|         |    194  | 
|         |    195 // ---------------------------------------------------------- | 
|         |    196 // CSimpleBaseDocument::DefNamespaceL | 
|         |    197 // ---------------------------------------------------------- | 
|         |    198 // | 
|         |    199 MSimpleNamespace* CSimpleBaseDocument::DefNamespaceL() | 
|         |    200     { | 
|         |    201     return iRoot->DefNamespaceL(); | 
|         |    202     } | 
|         |    203  | 
|         |    204 // ---------------------------------------------------------- | 
|         |    205 // CSimpleBaseDocument::HasContent | 
|         |    206 // ---------------------------------------------------------- | 
|         |    207 // | 
|         |    208 TBool CSimpleBaseDocument::HasContent() | 
|         |    209     { | 
|         |    210     return iRoot->HasContent(); | 
|         |    211     } | 
|         |    212  | 
|         |    213 // ---------------------------------------------------------- | 
|         |    214 // CSimpleBaseDocument::ContentUnicodeL | 
|         |    215 // ---------------------------------------------------------- | 
|         |    216 // | 
|         |    217 HBufC* CSimpleBaseDocument::ContentUnicodeL() | 
|         |    218     { | 
|         |    219     return iRoot->ContentUnicodeL(); | 
|         |    220     } | 
|         |    221  | 
|         |    222 // ---------------------------------------------------------- | 
|         |    223 // CSimpleBaseDocument::SetContentUnicodeL | 
|         |    224 // ---------------------------------------------------------- | 
|         |    225 // | 
|         |    226 void CSimpleBaseDocument::SetContentUnicodeL( const TDesC& aContent ) | 
|         |    227     { | 
|         |    228     iRoot->SetContentUnicodeL( aContent ); | 
|         |    229     } | 
|         |    230  | 
|         |    231 // ---------------------------------------------------------- | 
|         |    232 // CSimpleBaseDocument::SimpleElementsL | 
|         |    233 // ---------------------------------------------------------- | 
|         |    234 // | 
|         |    235 TInt CSimpleBaseDocument::SimpleElementsL( RPointerArray<MSimpleElement>& aElementArray ) | 
|         |    236     { | 
|         |    237     return iRoot->SimpleElementsL( aElementArray ); | 
|         |    238     } | 
|         |    239  | 
|         |    240 // ---------------------------------------------------------- | 
|         |    241 // CSimpleBaseDocument::AttrValueLC | 
|         |    242 // ---------------------------------------------------------- | 
|         |    243 // | 
|         |    244 HBufC* CSimpleBaseDocument::AttrValueLC( const TDesC8& aName ) | 
|         |    245     { | 
|         |    246     return iRoot->AttrValueLC( aName ); | 
|         |    247     } | 
|         |    248  | 
|         |    249 // ---------------------------------------------------------- | 
|         |    250 // CSimpleBaseDocument::AttrValue | 
|         |    251 // ---------------------------------------------------------- | 
|         |    252 // | 
|         |    253 const TDesC8* CSimpleBaseDocument::AttrValue( const TDesC8& aName ) | 
|         |    254     { | 
|         |    255   return iRoot->AttrValue( aName ); | 
|         |    256     } | 
|         |    257  | 
|         |    258 // ---------------------------------------------------------- | 
|         |    259 // CSimpleBaseDocument::AddAttrL | 
|         |    260 // ---------------------------------------------------------- | 
|         |    261 // | 
|         |    262 void CSimpleBaseDocument::AddAttrL( const TDesC8& aName, const TDesC& aValue ) | 
|         |    263     { | 
|         |    264     iRoot->AddAttrL( aName, aValue ); | 
|         |    265     } | 
|         |    266  | 
|         |    267 // ---------------------------------------------------------- | 
|         |    268 // CSimpleBaseDocument::SimpleAttributesL | 
|         |    269 // ---------------------------------------------------------- | 
|         |    270 // | 
|         |    271 TInt CSimpleBaseDocument::SimpleAttributesL( RPointerArray<MSimpleAttribute>& aArray ) | 
|         |    272     { | 
|         |    273     return iRoot->SimpleAttributesL( aArray ); | 
|         |    274     } | 
|         |    275  | 
|         |    276 // ---------------------------------------------------------- | 
|         |    277 // CSimpleBaseDocument::SimpleParentL | 
|         |    278 // ---------------------------------------------------------- | 
|         |    279 // | 
|         |    280 MSimpleElement* CSimpleBaseDocument::SimpleParentL() | 
|         |    281     { | 
|         |    282     return iRoot->SimpleParentL( ); | 
|         |    283     } | 
|         |    284  | 
|         |    285 // ---------------------------------------------------------- | 
|         |    286 // CSimpleBaseDocument::DetachSimpleL | 
|         |    287 // ---------------------------------------------------------- | 
|         |    288 // | 
|         |    289 void CSimpleBaseDocument::DetachSimpleL() | 
|         |    290     { | 
|         |    291     return iRoot->DetachSimpleL( ); | 
|         |    292     } | 
|         |    293  | 
|         |    294  | 
|         |    295 // ---------------------------------------------------------- | 
|         |    296 // CSimpleBaseDocument::AddSimpleElementL | 
|         |    297 // ---------------------------------------------------------- | 
|         |    298 // | 
|         |    299 MSimpleElement* CSimpleBaseDocument::AddSimpleElementL( | 
|         |    300     const TDesC8& aNsUri, | 
|         |    301     const TDesC8& aLocalName ) | 
|         |    302     { | 
|         |    303     return iRoot->AddSimpleElementL( aNsUri, aLocalName ); | 
|         |    304     } | 
|         |    305  | 
|         |    306 // ---------------------------------------------------------- | 
|         |    307 // CSimpleBaseDocument::AddSimpleElementL | 
|         |    308 // ---------------------------------------------------------- | 
|         |    309 // | 
|         |    310 MSimpleElement* CSimpleBaseDocument::AddSimpleElementL( | 
|         |    311     const TDesC8& aLocalName ) | 
|         |    312     { | 
|         |    313     return iRoot->AddSimpleElementL( aLocalName ); | 
|         |    314     } | 
|         |    315  | 
|         |    316  | 
|         |    317 // ---------------------------------------------------------- | 
|         |    318 // CSimpleBaseDocument::RemoveSimpleElement | 
|         |    319 // ---------------------------------------------------------- | 
|         |    320 // | 
|         |    321 void CSimpleBaseDocument::RemoveSimpleElement( | 
|         |    322     const TDesC8& aNsUri, | 
|         |    323     const TDesC8& aLocalName ) | 
|         |    324     { | 
|         |    325     iRoot->RemoveSimpleElement( aNsUri, aLocalName ); | 
|         |    326     } | 
|         |    327  | 
|         |    328 // ---------------------------------------------------------- | 
|         |    329 // CSimpleBaseDocument::Root | 
|         |    330 // ---------------------------------------------------------- | 
|         |    331 // | 
|         |    332 CSimpleElement* CSimpleBaseDocument::Root() | 
|         |    333     { | 
|         |    334     return iRoot; | 
|         |    335     } | 
|         |    336      | 
|         |    337 // ---------------------------------------------------------- | 
|         |    338 // CSimpleBaseDocument::DoDefaultNamespaceL | 
|         |    339 // ---------------------------------------------------------- | 
|         |    340 // | 
|         |    341 TPtrC8 CSimpleBaseDocument::DoDefaultNamespaceL() | 
|         |    342     { | 
|         |    343     RPointerArray<CSenNamespace>& nsa = iRoot->BaseElement()->NamespacesL(); | 
|         |    344     TInt elemCount = nsa.Count(); | 
|         |    345     if ( !elemCount ) | 
|         |    346         { | 
|         |    347         User::Leave( KErrNotFound ); | 
|         |    348         } | 
|         |    349     CSenNamespace* ns = nsa[0]; | 
|         |    350     return ns->URI(); | 
|         |    351     }     | 
|         |    352  | 
|         |    353  | 
|         |    354  | 
|         |    355  |