XDMEngine/src/XdmDocument.cpp
changeset 0 c8caa15ef882
child 10 96b99892dd80
equal deleted inserted replaced
-1:000000000000 0:c8caa15ef882
       
     1 /*
       
     2 * Copyright (c) 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:   XDM Engine document
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <f32file.h>
       
    23 #include "XdmEngine.h"
       
    24 #include "XdmProtocol.h"
       
    25 #include "XdmDocument.h"
       
    26 #include "XdmOperation.h"
       
    27 #include "XdmDocumentNode.h"
       
    28 #include "XdmNodeAttribute.h"
       
    29 #include "XdmOperationFactory.h"
       
    30 
       
    31 // ----------------------------------------------------------
       
    32 // CXdmDocument::CXdmDocument
       
    33 // 
       
    34 // ----------------------------------------------------------
       
    35 //
       
    36 EXPORT_C CXdmDocument::CXdmDocument( CXdmEngine& aXdmEngine ) :
       
    37                                      CActive( EPriorityStandard ),
       
    38                                      iDocSubset( EFalse ),
       
    39                                      iXdmEngine( aXdmEngine )
       
    40                                                 
       
    41     {
       
    42     }
       
    43 
       
    44 // ----------------------------------------------------------
       
    45 // CXdmDocument::BaseConstructL( 16-bit )
       
    46 // 
       
    47 // ----------------------------------------------------------
       
    48 //
       
    49 EXPORT_C void CXdmDocument::BaseConstructL( TInt aOperationFactoryUid,
       
    50                                             const TDesC& aDocumentName )
       
    51     {
       
    52     delete iDocumentName;
       
    53     iDocumentName = NULL;
       
    54     iDocumentName = HBufC::NewL( aDocumentName.Length() );
       
    55     iDocumentName->Des().Copy( aDocumentName );
       
    56     iOperationFactory = CXdmOperationFactory::NewL( aOperationFactoryUid );
       
    57     }
       
    58 
       
    59 // ----------------------------------------------------------
       
    60 // CXdmDocument::BaseConstructL( 8-bit )
       
    61 // 
       
    62 // ----------------------------------------------------------
       
    63 //
       
    64 EXPORT_C void CXdmDocument::BaseConstructL( TInt aOperationFactoryUid,
       
    65                                             const TDesC8& aDocumentName )
       
    66     {
       
    67     delete iDocumentName;
       
    68     iDocumentName = NULL;
       
    69     iDocumentName = HBufC::NewL( aDocumentName.Length() );
       
    70     iDocumentName->Des().Copy( aDocumentName );
       
    71     iOperationFactory = CXdmOperationFactory::NewL( aOperationFactoryUid );
       
    72     }
       
    73 
       
    74 // ----------------------------------------------------
       
    75 // CXdmDocument::~CXdmDocument
       
    76 // 
       
    77 // ----------------------------------------------------
       
    78 //
       
    79 EXPORT_C CXdmDocument::~CXdmDocument()
       
    80     {
       
    81     #ifdef _DEBUG
       
    82         iXdmEngine.WriteToLog( _L8( "CXdmDocument::~CXdmDocument()" ) );
       
    83     #endif
       
    84     delete iDocumentName;
       
    85     EmptyOperationQueue();
       
    86     iChangeRequests.Close();
       
    87     delete iOperationFactory;
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------
       
    91 // CXdmDocument::EmptyOperationQueue
       
    92 // 
       
    93 // ---------------------------------------------------------
       
    94 //
       
    95 void CXdmDocument::EmptyOperationQueue()
       
    96     {
       
    97     #ifdef _DEBUG
       
    98         iXdmEngine.WriteToLog( _L8( "CXdmDocument::EmptyOperationQueue()" ) );
       
    99     #endif
       
   100     MXdmOperation* operation = NULL;
       
   101     TInt count = iChangeRequests.Count();
       
   102     for( TInt i = 0;i < count;i++ )
       
   103         {
       
   104         operation = iChangeRequests[i];
       
   105         iChangeRequests.Remove( i );    
       
   106         operation->Destroy();
       
   107         operation = NULL;
       
   108         }
       
   109     }
       
   110     
       
   111 // ----------------------------------------------------
       
   112 // CXdmDocument::FetchDataL
       
   113 // 
       
   114 // ----------------------------------------------------
       
   115 //
       
   116 EXPORT_C void CXdmDocument::FetchDataL( CXdmDocumentNode* aDocNode )
       
   117     {
       
   118     #ifdef _DEBUG
       
   119         iXdmEngine.WriteToLog( _L8( "CXdmDocument::FetchDataL()" ) );
       
   120     #endif
       
   121     MXdmOperation* operation = iOperationFactory->FetchL( *this, aDocNode );
       
   122     CleanupStack::PushL( operation );
       
   123     User::LeaveIfError( iChangeRequests.Append( operation ) );
       
   124     CleanupStack::Pop();  //operation
       
   125     }
       
   126 
       
   127 // ----------------------------------------------------
       
   128 // CXdmDocument::ReplaceL
       
   129 // 
       
   130 // ----------------------------------------------------
       
   131 //
       
   132 EXPORT_C void CXdmDocument::ReplaceL( CXdmDocumentNode* aOldNode,
       
   133                                       CXdmDocumentNode* aNewNode )
       
   134     {
       
   135     #ifdef _DEBUG
       
   136         iXdmEngine.WriteToLog( _L8( "CXdmDocument::ReplaceL()" ) );
       
   137     #endif
       
   138     if( aOldNode->Parent() == aNewNode->Parent() )
       
   139         {
       
   140         MXdmOperation* operation = iOperationFactory->ReplaceL(
       
   141                                     *this, aOldNode, aNewNode );
       
   142         CleanupStack::PushL( operation );
       
   143         User::LeaveIfError( iChangeRequests.Append( operation ) );
       
   144         CleanupStack::Pop();  //operation
       
   145         }
       
   146     else
       
   147         {
       
   148         #ifdef _DEBUG
       
   149             iXdmEngine.WriteToLog( _L8( "  Different parents, leaves!" ) );
       
   150         #endif
       
   151         User::Leave( KErrGeneral );
       
   152         }
       
   153     }
       
   154 
       
   155 // ----------------------------------------------------
       
   156 // CXdmDocument::ReplaceL
       
   157 // 
       
   158 // ----------------------------------------------------
       
   159 //
       
   160 EXPORT_C void CXdmDocument::ReplaceL()
       
   161     {
       
   162     #ifdef _DEBUG
       
   163         iXdmEngine.WriteToLog( _L8( "CXdmDocument::ReplaceL()" ) );
       
   164     #endif
       
   165     MXdmOperation* operation = iOperationFactory->ReplaceL( *this, NULL, NULL ); 
       
   166     CleanupStack::PushL( operation );
       
   167     User::LeaveIfError( iChangeRequests.Append( operation ) );
       
   168     CleanupStack::Pop();  //operation
       
   169     }
       
   170 
       
   171 // ----------------------------------------------------
       
   172 // CXdmDocument::InsertL
       
   173 // 
       
   174 // ----------------------------------------------------
       
   175 //
       
   176 EXPORT_C void CXdmDocument::InsertL( CXdmDocumentNode* aDocNode )
       
   177     {
       
   178     #ifdef _DEBUG
       
   179         iXdmEngine.WriteToLog( _L8( "CXdmDocument::InsertL()" ) );
       
   180     #endif
       
   181     __ASSERT_DEBUG( aDocNode != NULL, User::Panic( _L( "CXcapDocument" ), EDocNodeNull ) );
       
   182     MXdmOperation* operation = iOperationFactory->InsertL(
       
   183                                 *this, aDocNode );
       
   184     CleanupStack::PushL( operation );
       
   185     User::LeaveIfError( iChangeRequests.Append( operation ) );
       
   186     CleanupStack::Pop();  //operation
       
   187     }
       
   188 
       
   189 // ----------------------------------------------------
       
   190 // CXdmDocument::AppendL
       
   191 // 
       
   192 // ----------------------------------------------------
       
   193 //
       
   194 EXPORT_C void CXdmDocument::AppendL( CXdmDocumentNode* aDocNode )
       
   195     {
       
   196     #ifdef _DEBUG
       
   197         iXdmEngine.WriteToLog( _L8( "CXdmDocument::AppendL()" ) );
       
   198     #endif
       
   199     MXdmOperation* operation = 
       
   200         iOperationFactory->AppendL( *this, aDocNode );
       
   201     CleanupStack::PushL( operation );
       
   202     User::LeaveIfError( iChangeRequests.Append( operation ) );
       
   203     CleanupStack::Pop();  //operation
       
   204     }
       
   205       
       
   206 // ----------------------------------------------------
       
   207 // CXdmDocument::DeleteDataL
       
   208 // 
       
   209 // ----------------------------------------------------
       
   210 //
       
   211 EXPORT_C void CXdmDocument::DeleteDataL( CXdmDocumentNode* aDocNode )
       
   212     {
       
   213     #ifdef _DEBUG
       
   214         iXdmEngine.WriteToLog( _L8( "CXdmDocument::DeleteDataL()" ) );
       
   215     #endif
       
   216     MXdmOperation* operation = aDocNode == NULL ?
       
   217         iOperationFactory->DeletionL( *this, NULL ) :
       
   218         iOperationFactory->DeletionL( *this, aDocNode );
       
   219     CleanupStack::PushL( operation );
       
   220     User::LeaveIfError( iChangeRequests.Append( operation ) );
       
   221     CleanupStack::Pop();  //operation
       
   222     }
       
   223 
       
   224 // ----------------------------------------------------
       
   225 // CXdmDocument::Find
       
   226 // 
       
   227 // ----------------------------------------------------
       
   228 //
       
   229 EXPORT_C TInt CXdmDocument::Find( const CXdmDocumentNode& aTargetNode,
       
   230                                   RPointerArray<CXdmDocumentNode>& aResultArray ) const
       
   231     {
       
   232     #ifdef _DEBUG
       
   233         iXdmEngine.WriteToLog( _L8( "CXdmDocument::Find()" ) );
       
   234     #endif
       
   235     TInt error = KErrNone;
       
   236     CXdmDocumentNode* root = DocumentRoot();
       
   237     __ASSERT_ALWAYS( root != NULL, User::Panic( _L( "CXcapDocument" ), EDocNodeNull ) );
       
   238     if( root->NodeName().Compare( aTargetNode.NodeName() ) == 0 )
       
   239         {
       
   240         TBool found = EFalse;
       
   241         TInt attrCount = root->AttributeCount();
       
   242         TInt attrCount2 = aTargetNode.AttributeCount();
       
   243         if( attrCount > 0 && attrCount == attrCount2 )
       
   244             {
       
   245             SXdmAttribute16 attribute;
       
   246             for( TInt i = 0;i < attrCount;i++ )
       
   247                 {
       
   248                 attribute.iName.Set( aTargetNode.Attribute( i )->NodeName() );
       
   249                 attribute.iValue.Set( aTargetNode.Attribute( i )->AttributeValue() );
       
   250                 found = root->HasAttribute( attribute );
       
   251                 }
       
   252             }
       
   253         error = found ? aResultArray.Append( root ) : KErrNotFound;
       
   254         }
       
   255     else
       
   256         {
       
   257         #ifdef _DEBUG
       
   258             iXdmEngine.WriteToLog( _L8( " Root does not match, search child nodes" ) );
       
   259         #endif
       
   260         error = root->Find( aTargetNode, aResultArray ); 
       
   261         }
       
   262     TInt count = aResultArray.Count();
       
   263     return error == KErrNone && count > 0 ? count : KErrNotFound;
       
   264     }
       
   265     
       
   266 // ----------------------------------------------------
       
   267 // CXdmDocument::Find
       
   268 // 
       
   269 // ----------------------------------------------------
       
   270 //
       
   271 EXPORT_C TInt CXdmDocument::Find( const TDesC& aNodeName,
       
   272                                   RPointerArray<CXdmDocumentNode>& aResultArray,
       
   273                                   const RPointerArray<SXdmAttribute16>& aAttributeArray ) const
       
   274     {
       
   275     #ifdef _DEBUG
       
   276         iXdmEngine.WriteToLog( _L8( "CXdmDocument::Find()" ) );
       
   277     #endif
       
   278     TInt error = KErrNone;
       
   279     CXdmDocumentNode* root = DocumentRoot();
       
   280     __ASSERT_ALWAYS( root != NULL, User::Panic( _L( "CXcapDocument" ), EDocNodeNull ) );
       
   281     if( root->NodeName().Compare( aNodeName ) == 0 )
       
   282         {
       
   283         TBool found = EFalse;
       
   284         TInt attrCount = root->AttributeCount();
       
   285         if( attrCount > 0 )
       
   286             {
       
   287             for( TInt i = 0;i < attrCount;i++ )
       
   288                 found = root->HasAttribute( *aAttributeArray[i] );
       
   289             }
       
   290         error = found ? aResultArray.Append( root ) : KErrNotFound;
       
   291         }
       
   292     else
       
   293         {
       
   294         #ifdef _DEBUG
       
   295             iXdmEngine.WriteToLog( _L8( " Root does not match, search child nodes" ) );
       
   296         #endif
       
   297         error = root->Find( aNodeName, aResultArray, aAttributeArray ); 
       
   298         }
       
   299     TInt count = aResultArray.Count();
       
   300     return error == KErrNone && count > 0 ? count : KErrNotFound;
       
   301     }
       
   302 
       
   303 // ----------------------------------------------------
       
   304 // CXdmDocument::Find
       
   305 // 
       
   306 // ----------------------------------------------------
       
   307 //
       
   308 EXPORT_C TInt CXdmDocument::Find( const TDesC& aNodeName,
       
   309                                   RPointerArray<CXdmDocumentNode>& aResultArray ) const
       
   310     {
       
   311     #ifdef _DEBUG
       
   312         iXdmEngine.WriteToLog( _L8( "CXdmDocument::Find()" ) );
       
   313     #endif
       
   314     TInt error = KErrNone;
       
   315     CXdmDocumentNode* root = DocumentRoot();
       
   316     __ASSERT_ALWAYS( root != NULL, User::Panic( _L( "CXcapDocument" ), EDocNodeNull ) );
       
   317     if( root->NodeName().Compare( aNodeName ) == 0 )
       
   318         error = aResultArray.Append( root );
       
   319     else
       
   320         {
       
   321         #ifdef _DEBUG
       
   322             iXdmEngine.WriteToLog( _L8( " Root does not match, search child nodes" ) );
       
   323         #endif
       
   324         error = root->Find( aNodeName, aResultArray ); 
       
   325         }
       
   326     TInt count = aResultArray.Count();
       
   327     return error == KErrNone && count > 0 ? count : KErrNotFound;                            
       
   328     }
       
   329     
       
   330 // ----------------------------------------------------
       
   331 // CXcapDocument::operator==
       
   332 // 
       
   333 // ----------------------------------------------------
       
   334 //
       
   335 EXPORT_C TBool CXdmDocument::operator==( CXdmDocument& aDocument ) const
       
   336     {
       
   337     CXdmDocumentNode* root1 = DocumentRoot();
       
   338     CXdmDocumentNode* root2 = aDocument.DocumentRoot();
       
   339     __ASSERT_ALWAYS( root1 != NULL, User::Panic( _L( "CXcapDocument" ), EDocNodeNull ) );
       
   340     __ASSERT_ALWAYS( root2 != NULL, User::Panic( _L( "CXcapDocument" ), EDocNodeNull ) );
       
   341     return Name().Compare( aDocument.Name() ) == 0 ? *root1 == *root2 : EFalse;
       
   342     }
       
   343     
       
   344 // ----------------------------------------------------
       
   345 // CXdmDocument::Name
       
   346 // 
       
   347 // ----------------------------------------------------
       
   348 //
       
   349 EXPORT_C TPtrC CXdmDocument::Name() const
       
   350     {
       
   351     return iDocumentName != NULL ? iDocumentName->Des() : TPtrC();
       
   352     }
       
   353 
       
   354 // ----------------------------------------------------
       
   355 // CXdmDocument::Find
       
   356 // 
       
   357 // ----------------------------------------------------
       
   358 //
       
   359 EXPORT_C TInt CXdmDocument::Find( const TDesC& aAttributeName,
       
   360                                   RPointerArray<CXdmNodeAttribute>& aResultArray ) const
       
   361     {
       
   362     #ifdef _DEBUG
       
   363         iXdmEngine.WriteToLog( _L8( "CXdmDocument::Find()" ) );
       
   364     #endif
       
   365     TInt error = KErrNone;
       
   366     CXdmDocumentNode* root = DocumentRoot();
       
   367     __ASSERT_ALWAYS( root != NULL, User::Panic( _L( "CXdmDocument" ), EDocNodeNull ) );
       
   368     error = DoFindAttributes( root, aAttributeName, aResultArray ); 
       
   369     TInt count = aResultArray.Count();
       
   370     return error == KErrNone && count > 0 ? count : KErrNotFound;                
       
   371     }
       
   372 
       
   373 // ----------------------------------------------------
       
   374 // CXdmDocument::RemoveFromModelL
       
   375 // 
       
   376 // ----------------------------------------------------
       
   377 //
       
   378 EXPORT_C void CXdmDocument::RemoveFromModelL( CXdmDocumentNode* aChileNode )
       
   379     {
       
   380     #ifdef _DEBUG
       
   381         iXdmEngine.WriteToLog( _L8( "CXdmDocument::RemoveFromModel()" ) );
       
   382     #endif
       
   383     if( !iDocSubset )
       
   384         {       
       
   385         CXdmDocumentNode* parent = aChileNode->Parent();
       
   386         parent != NULL ? parent->RemoveChileNodeL( aChileNode ) : User::Leave( KErrArgument);
       
   387         }
       
   388     else User::Leave( KErrGeneral );
       
   389     }
       
   390 
       
   391 // ----------------------------------------------------
       
   392 // CXdmDocument::ResetSubset
       
   393 // 
       
   394 // ----------------------------------------------------
       
   395 //
       
   396 EXPORT_C void CXdmDocument::ResetSubset()
       
   397     {
       
   398     iDocSubset = EFalse;
       
   399     }
       
   400       
       
   401 // ----------------------------------------------------
       
   402 // CXdmDocument::AppendToModelL
       
   403 // 
       
   404 // ----------------------------------------------------
       
   405 //
       
   406 EXPORT_C void CXdmDocument::AppendToModelL( CXdmDocumentNode* aNewNode,
       
   407                                             CXdmDocumentNode* aParentNode )
       
   408     {
       
   409     #ifdef _DEBUG
       
   410         iXdmEngine.WriteToLog( _L8( "CXdmDocument::AppendToModelL()" ) );
       
   411     #endif
       
   412     if( !iDocSubset )
       
   413         {
       
   414         if( aNewNode != NULL && aParentNode != NULL )
       
   415             aParentNode->AppendChileNodeL( aNewNode );
       
   416         }
       
   417     else User::Leave( KErrGeneral );
       
   418     }
       
   419         
       
   420 // ----------------------------------------------------
       
   421 // CXdmDocument::ReplaceInModelL
       
   422 // 
       
   423 // ----------------------------------------------------
       
   424 //
       
   425 EXPORT_C void CXdmDocument::ReplaceInModelL( CXdmDocumentNode* aNewNode,
       
   426                                              CXdmDocumentNode* aTargetNode )
       
   427     {
       
   428     #ifdef _DEBUG
       
   429         iXdmEngine.WriteToLog( _L8( "CXdmDocument::ReplaceInModelL()" ) );
       
   430     #endif
       
   431     if( !iDocSubset )
       
   432         {
       
   433         CXdmDocumentNode* parent = aTargetNode->Parent();
       
   434         parent != NULL ? parent->ReplaceChileNodeL( aNewNode, aTargetNode ) : User::Leave( KErrArgument);                                    
       
   435         }
       
   436     else User::Leave( KErrGeneral );
       
   437     }
       
   438                                        
       
   439 // ----------------------------------------------------
       
   440 // CXdmDocument::DocumentSubsetL
       
   441 // 
       
   442 // ----------------------------------------------------
       
   443 //
       
   444 EXPORT_C CXdmDocumentNode* CXdmDocument::DocumentSubsetL( const TDesC& aNodePath )
       
   445     {
       
   446     #ifdef _DEBUG
       
   447         iXdmEngine.WriteToLog( _L8( "CXdmDocument::DocumentSubsetL()" ) );
       
   448     #endif
       
   449     TBool ready = EFalse;
       
   450     HBufC* temp = HBufC::NewLC( aNodePath.Length() );
       
   451     temp->Des().Copy( aNodePath );
       
   452     TPtr nodePath( temp->Des() );
       
   453     while( !ready && nodePath.Length() != 0 )
       
   454         {
       
   455         TPtrC part;
       
   456         TInt index = nodePath.LocateF( KXdmPathSeparatorChar );
       
   457         if( index > 0 )
       
   458             part.Set( nodePath.Left( index ) );
       
   459         else
       
   460             {
       
   461             part.Set( nodePath );
       
   462             ready = ETrue;
       
   463             }
       
   464         AppendPathPartL( part );
       
   465         nodePath.Delete( 0, index + 1 );
       
   466         }
       
   467     iDocSubset = ETrue;
       
   468     CleanupStack::PopAndDestroy();  //temp
       
   469     return CurrentExtent();
       
   470     }
       
   471         
       
   472 // ----------------------------------------------------
       
   473 // CXdmDocument::DoFindAttributes
       
   474 // 
       
   475 // ----------------------------------------------------
       
   476 //
       
   477 TInt CXdmDocument::DoFindAttributes( CXdmDocumentNode* aNode,
       
   478 								     const TDesC& aAttributeName,
       
   479                                      RPointerArray<CXdmNodeAttribute>& aResultArray ) const
       
   480     {
       
   481     #ifdef _DEBUG
       
   482         iXdmEngine.WriteToLog( _L8( "CXdmDocument::DoFindAttributes()" ) );
       
   483     #endif
       
   484     TInt error = KErrNone;
       
   485 	CXdmDocumentNode* node = aNode;
       
   486 	TInt attrCount = aNode->AttributeCount();
       
   487     for( TInt i = 0; i < attrCount; i++ )
       
   488         {
       
   489 		CXdmNodeAttribute* attr = node->Attribute( i );
       
   490 	   	if( !attr->NodeName().CompareF( aAttributeName ) )
       
   491     	   	error = aResultArray.Append( attr );
       
   492 	   	}
       
   493 	TInt nodeCount = node->NodeCount();
       
   494 	for( TInt i = 0; i < nodeCount; i++ )
       
   495 		{
       
   496 		CXdmDocumentNode* child = node->ChileNode( i );
       
   497 	   	DoFindAttributes ( child, aAttributeName, aResultArray );
       
   498 		}  
       
   499     TInt count = aResultArray.Count();
       
   500     return error == KErrNone && count > 0 ? count : KErrNotFound;            
       
   501     }
       
   502         
       
   503 
       
   504 
       
   505 
       
   506