xdmprotocols/LocalProtocol/LocalOperations/src/LocalReplacement.cpp
branchRCL_3
changeset 17 2669f8761a99
parent 16 2580314736af
child 18 fbd2e7cec7ef
equal deleted inserted replaced
16:2580314736af 17:2669f8761a99
     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: CLocalReplacement
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDES
       
    22 #include <XdmDocumentNode.h>
       
    23 #include "XdmXmlParser.h"
       
    24 #include "LocalProtocol.h"
       
    25 #include "LocalDocument.h"
       
    26 #include "LocalReplacement.h"
       
    27 #include "LocalDocumentNode.h"
       
    28 #include "LocalNodeAttribute.h"
       
    29     
       
    30 // ---------------------------------------------------------
       
    31 // C++ constructor can NOT contain any code, that
       
    32 // might leave.
       
    33 // ---------------------------------------------------------
       
    34 //
       
    35 CLocalReplacement::CLocalReplacement( CLocalDocument& aTargetDoc,
       
    36                                       CLocalDocumentNode* aOldNode,
       
    37                                       CLocalDocumentNode* aNewNode,
       
    38                                       CLocalOperationFactory& aOperationFactory ) :
       
    39                                       CLocalOperationBase( aTargetDoc, aOperationFactory ),
       
    40                                       iNewNode( ( CLocalDocumentNode* )aNewNode ),
       
    41                                       iTargetNode( ( CLocalDocumentNode* )aOldNode ),
       
    42                                       iOperationType( iTargetNode == NULL ?
       
    43                                       EXdmDocument : EXdmPartialDocument )                                                
       
    44     {
       
    45     }
       
    46 
       
    47 // ---------------------------------------------------------
       
    48 // Two-phased constructor.
       
    49 //
       
    50 // ---------------------------------------------------------
       
    51 //
       
    52 CLocalReplacement* CLocalReplacement::NewL( CLocalDocument& aParentDoc,
       
    53                                             CLocalDocumentNode* aOldNode,
       
    54                                             CLocalDocumentNode* aNewNode,
       
    55                                             CLocalOperationFactory& aOperationFactory )
       
    56     {
       
    57     CLocalReplacement* self = new ( ELeave ) CLocalReplacement( aParentDoc, aOldNode, aNewNode, aOperationFactory );
       
    58     CleanupStack::PushL( self );
       
    59     self->BaseConstructL();
       
    60     CleanupStack::Pop();
       
    61     return self;
       
    62     }
       
    63       
       
    64 // ---------------------------------------------------------
       
    65 // CLocalReplacement::ExecuteL
       
    66 //
       
    67 // ---------------------------------------------------------
       
    68 //
       
    69 void CLocalReplacement::ExecuteL()
       
    70     {
       
    71     TPtrC fileName = iTargetDoc.XmlFilePath();
       
    72     TInt size = OpenDataFileL( fileName );
       
    73     if( size > 0 ) 
       
    74         {
       
    75         //Only a subset of a document
       
    76         if( iTargetDoc.IsSubset() )
       
    77             HandlePartialReplacementL( size );
       
    78         else if( iTargetNode != NULL )
       
    79             {
       
    80             CLocalDocumentNode* parent = ( CLocalDocumentNode* )iTargetNode->Parent();
       
    81             parent->ReplaceLocalL( iNewNode, iTargetNode );
       
    82             }
       
    83         else
       
    84             {
       
    85             iXmlFile.Close();
       
    86             ReplaceDataFileL();
       
    87             WriteFileL( iTargetDoc.DocumentRoot() );
       
    88             }
       
    89         }
       
    90     else WriteFileL( iTargetDoc.DocumentRoot() );
       
    91     iXmlFile.Close();
       
    92     }
       
    93 
       
    94 // ---------------------------------------------------------
       
    95 // CLocalReplacement::HandlePartialReplacementL
       
    96 //
       
    97 // ---------------------------------------------------------
       
    98 //
       
    99 void CLocalReplacement::HandlePartialReplacementL( TInt aDataLength )
       
   100     {
       
   101     TInt error = KErrNone;
       
   102     HBufC8* data = FetchXmlDataL( aDataLength );
       
   103     CleanupStack::PushL( data );
       
   104     TRAP( error, iXmlParser->ParseDocumentL( &iTargetDoc, *data ) );
       
   105     CXdmDocumentNode* parent = iTargetNode->Parent();
       
   106     RPointerArray<CXdmDocumentNode> parentResults;
       
   107     CleanupClosePushL( parentResults );
       
   108     RPointerArray<SXdmAttribute16> parentAttributes;
       
   109     CleanupClosePushL( parentAttributes );
       
   110     RPointerArray<CXdmDocumentNode> targetResults;
       
   111     CleanupClosePushL( targetResults );
       
   112     RPointerArray<SXdmAttribute16> targetAttributes;
       
   113     CleanupClosePushL( targetAttributes );
       
   114     TInt pAttrCount = parent->AttributeCount();
       
   115     TInt tAttrCount = iTargetNode->AttributeCount();
       
   116     if( pAttrCount > 0 )
       
   117         {
       
   118         SXdmAttribute16 attribute;
       
   119         for( TInt i = 0;i < pAttrCount;i++ )
       
   120             {
       
   121             attribute.iName.Set( parent->Attribute( i )->NodeName() );
       
   122             attribute.iValue.Set( parent->Attribute( i )->AttributeValue() );
       
   123             }
       
   124         User::LeaveIfError( parentAttributes.Append( &attribute ) );
       
   125         }
       
   126     if( tAttrCount > 0 )
       
   127         {
       
   128         SXdmAttribute16 attribute;
       
   129         for( TInt i = 0;i < tAttrCount;i++ )
       
   130             {
       
   131             attribute.iName.Set( iTargetNode->Attribute( i )->NodeName() );
       
   132             attribute.iValue.Set( iTargetNode->Attribute( i )->AttributeValue() );
       
   133             }
       
   134         User::LeaveIfError( targetAttributes.Append( &attribute ) );
       
   135         }
       
   136     iTargetDoc.Find( parent->NodeName(), parentResults, parentAttributes );
       
   137     iTargetDoc.Find( iTargetNode->NodeName(), targetResults, targetAttributes );
       
   138     if( parentResults.Count() == 1 && targetResults.Count() == 1 )
       
   139         {
       
   140         iXmlFile.Close();
       
   141         ReplaceDataFileL();
       
   142         CLocalDocumentNode* node = ( CLocalDocumentNode* )parentResults[0];
       
   143         CLocalDocumentNode* target = ( CLocalDocumentNode* )targetResults[0];
       
   144         node->ReplaceLocalL( iNewNode, target );
       
   145         WriteFileL( iTargetDoc.DocumentRoot() );
       
   146         }
       
   147     else User::Leave( KErrGeneral );
       
   148     CleanupStack::PopAndDestroy( 5 );  //targetAttributes, targetResults,
       
   149                                        //parentAttributes, parentResults, data
       
   150     }
       
   151                 
       
   152 // ---------------------------------------------------------
       
   153 // Symbian OS default constructor may leave.
       
   154 //
       
   155 // ---------------------------------------------------------
       
   156 //     
       
   157 void CLocalReplacement::Destroy()
       
   158     {
       
   159     delete this;
       
   160     }                       
       
   161 
       
   162 // ---------------------------------------------------------
       
   163 // Destructor
       
   164 //
       
   165 // ---------------------------------------------------------
       
   166 //
       
   167 CLocalReplacement::~CLocalReplacement()
       
   168     {
       
   169     
       
   170     }
       
   171 
       
   172 //  End of File  
       
   173