xdmprotocols/XdmXmlParser/src/XdmXmlContentHandler.cpp
changeset 0 c8caa15ef882
child 3 ca392eff7152
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:   CXdmXmlContentHandler
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <XdmDocument.h>
       
    21 #include <contenthandler.h>
       
    22 #include <XdmDocumentNode.h>
       
    23 #include <XdmNodeAttribute.h>
       
    24 #include "XdmNamespaceContainer.h"
       
    25 #include "XdmXmlParser.h"
       
    26 #include "XdmNodeInterface.h"
       
    27 #include "XmlParserNodePath.h"
       
    28 #include "XdmXmlContentHandler.h"
       
    29 
       
    30 // ---------------------------------------------------------
       
    31 // CXdmXmlContentHandler::CXdmXmlContentHandler
       
    32 //
       
    33 // ---------------------------------------------------------
       
    34 //
       
    35 CXdmXmlContentHandler::CXdmXmlContentHandler( CXdmXmlParser& aParserMain ) :
       
    36                                               iFinished( EFalse ),
       
    37                                               iTargetFound( EFalse ),
       
    38                                               iPartialDocument( EFalse ),
       
    39                                               iParserMain( aParserMain )
       
    40 
       
    41     {   
       
    42     }
       
    43     
       
    44 // ---------------------------------------------------------
       
    45 // CXdmXmlContentHandler::NewL
       
    46 //
       
    47 // ---------------------------------------------------------
       
    48 //
       
    49 CXdmXmlContentHandler* CXdmXmlContentHandler::NewL( CXdmXmlParser& aParserMain )
       
    50     {
       
    51     CXdmXmlContentHandler* self = new ( ELeave ) CXdmXmlContentHandler( aParserMain );
       
    52     CleanupStack::PushL( self );
       
    53     self->ConstructL();
       
    54     CleanupStack::Pop();
       
    55     return self;
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------
       
    59 // CXdmXmlContentHandler::ConstructL
       
    60 //
       
    61 // ---------------------------------------------------------
       
    62 //
       
    63 void CXdmXmlContentHandler::ConstructL()
       
    64     {
       
    65     iContentBuffer = CBufFlat::NewL( 50 ); 
       
    66     }
       
    67             
       
    68 // ---------------------------------------------------------
       
    69 // CXdmXmlContentHandler::~CXdmXmlContentHandler
       
    70 //
       
    71 // ---------------------------------------------------------
       
    72 //
       
    73 CXdmXmlContentHandler::~CXdmXmlContentHandler()
       
    74     { 
       
    75     delete iNodePath;
       
    76     delete iContentBuffer;
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------
       
    80 // CXdmXmlContentHandler::SetTarget
       
    81 //
       
    82 // ---------------------------------------------------------
       
    83 //
       
    84 void CXdmXmlContentHandler::SetTarget( const CXdmDocument& aTargetDocument )
       
    85     {
       
    86     iTargetDocument = CONST_CAST( CXdmDocument*, &aTargetDocument );
       
    87     iRootNode = iTargetDocument->DocumentRoot();
       
    88     iDocumentSubset = NULL;
       
    89     iIsRootNode = ETrue;
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------
       
    93 // CXdmXmlContentHandler::SetTarget
       
    94 //
       
    95 // ---------------------------------------------------------
       
    96 //
       
    97 void CXdmXmlContentHandler::SetTarget( const CXdmDocumentNode& aDocumentRoot )
       
    98     {
       
    99     iTargetDocument = NULL;
       
   100     iRootNode = CONST_CAST( CXdmDocumentNode*, &aDocumentRoot );
       
   101     iDocumentSubset = NULL;
       
   102     iIsRootNode = ETrue;
       
   103     }
       
   104     
       
   105 // ---------------------------------------------------------
       
   106 // CXdmXmlContentHandler::SetTarget 
       
   107 //
       
   108 // ---------------------------------------------------------
       
   109 //
       
   110 void CXdmXmlContentHandler::SetTargetL( const CXdmDocument& aTargetDocument,
       
   111                                         const CXdmDocumentNode& aTargetNode )
       
   112     {
       
   113     iElementType = aTargetNode.ElementType();
       
   114     iTargetDocument = CONST_CAST( CXdmDocument*, &aTargetDocument );
       
   115     if( iElementType == EXdmElementAttribute )
       
   116         {
       
   117         iTargetAttribute = ( CXdmNodeAttribute* )&aTargetNode;
       
   118         iDocumentSubset = CONST_CAST( CXdmDocumentNode*, aTargetNode.Parent() );
       
   119         }
       
   120     else 
       
   121         {
       
   122         iTargetAttribute = NULL;
       
   123         iDocumentSubset = CONST_CAST( CXdmDocumentNode*, &aTargetNode );
       
   124         }
       
   125     iNodePath = CXmlParserNodePath::NewL( iParserMain, iElementType, *iDocumentSubset, this );
       
   126     iRootNode = iDocumentSubset; 
       
   127     iPartialDocument = ETrue;
       
   128     iCurrentNode = NULL;
       
   129     }
       
   130 
       
   131 // ---------------------------------------------------------
       
   132 // CXdmXmlContentHandler::TargetAttribute
       
   133 //
       
   134 // ---------------------------------------------------------
       
   135 //
       
   136 CXdmNodeAttribute* CXdmXmlContentHandler::TargetAttribute() const
       
   137     {
       
   138     return iTargetAttribute;
       
   139     }
       
   140     
       
   141 // ---------------------------------------------------------
       
   142 // CXdmXmlContentHandler::Reset
       
   143 //
       
   144 // ---------------------------------------------------------
       
   145 //
       
   146 void CXdmXmlContentHandler::Reset()
       
   147     {
       
   148     iTargetDocument = NULL;
       
   149     iTargetAttribute = NULL;
       
   150     iDocumentSubset = NULL;
       
   151     iRootNode = NULL;
       
   152     iCurrentNode = NULL;
       
   153     delete iNodePath;
       
   154     iNodePath = NULL;
       
   155     iFinished = EFalse;
       
   156     iTargetFound = EFalse;
       
   157     iPartialDocument = EFalse;
       
   158     }
       
   159        
       
   160 // ---------------------------------------------------------
       
   161 // CXdmXmlContentHandler::GetExtendedInterface
       
   162 //
       
   163 // ---------------------------------------------------------
       
   164 //
       
   165 TAny* CXdmXmlContentHandler::GetExtendedInterface( const TInt32 /*aUid*/ )    
       
   166     {
       
   167     return NULL; 
       
   168     }
       
   169     
       
   170 // ---------------------------------------------------------
       
   171 // CXdmXmlContentHandler::OnContentL
       
   172 //
       
   173 // ---------------------------------------------------------
       
   174 //
       
   175 void CXdmXmlContentHandler::OnContentL( const TDesC8 &aBytes,
       
   176                                         TInt aErrorCode )
       
   177     {
       
   178     #ifdef _DEBUG
       
   179         iParserMain.WriteToLog( _L8( "CXmlNode::OnContentL()" ), aErrorCode );
       
   180         iParserMain.WriteToLog( _L8( "  Bytes: %S" ), &aBytes );
       
   181         iParserMain.WriteToLog( _L8( "  Error: %d" ), aErrorCode );
       
   182     #endif
       
   183     if( iPartialDocument && !iTargetFound )
       
   184         return;
       
   185     TInt length = aBytes.Length();
       
   186     TPtr8 desc( CONST_CAST( TUint8*, aBytes.Ptr() ), length, length );
       
   187     desc.TrimAll();
       
   188     if( desc.Length() > 0 )
       
   189         {
       
   190         iContentBuffer->InsertL( iContentIndex, desc );
       
   191         iContentIndex = iContentIndex + desc.Length();
       
   192         }
       
   193     }
       
   194     
       
   195 // ---------------------------------------------------------
       
   196 // CXdmXmlContentHandler::OnEndDocumentL
       
   197 //
       
   198 // ---------------------------------------------------------
       
   199 //
       
   200 void CXdmXmlContentHandler::OnEndDocumentL( TInt aErrorCode )
       
   201     {
       
   202     iIsRootNode = ETrue;
       
   203     #ifdef _DEBUG
       
   204         iParserMain.WriteToLog( _L8( "CXmlNode::OnEndDocumentL() - Error: %d "), aErrorCode );
       
   205     #endif
       
   206     }
       
   207 
       
   208 // ---------------------------------------------------------
       
   209 // CXdmXmlContentHandler::OnEndElementL
       
   210 //
       
   211 // ---------------------------------------------------------
       
   212 //
       
   213 void CXdmXmlContentHandler::OnEndElementL( const RTagInfo &aElement,
       
   214                                            TInt aErrorCode )
       
   215     {
       
   216     TPtrC8 element( aElement.LocalName().DesC() );
       
   217     #ifdef _DEBUG
       
   218         iParserMain.WriteToLog( _L8( "CXmlNode::OnEndElementL() - Element: %S  Error: %d "),
       
   219                                    &element, aErrorCode );
       
   220     #endif
       
   221     if( iCurrentNode != NULL && iContentBuffer->Size() > 0 )
       
   222         {
       
   223         TPtrC8 data( iContentBuffer->Ptr( 0 ) );
       
   224         iCurrentNode->SetLeafNode( ETrue );
       
   225         iCurrentNode->SetLeafNodeContentL( data );
       
   226         iContentBuffer->Reset();
       
   227         iContentIndex = 0;
       
   228         }
       
   229     if( iCurrentNode != NULL && iCurrentNode->NodeCount() == 0 )
       
   230         iCurrentNode->SetEmptyNode( ETrue );
       
   231     if( iPartialDocument )
       
   232         {
       
   233         if( !iFinished )
       
   234             {
       
   235             if( iCurrentNode != NULL )
       
   236                 {
       
   237                 CXdmDocumentNode* parent = iCurrentNode->Parent();
       
   238                 #ifdef _DEBUG
       
   239                     TBuf8<64> parentName;
       
   240                     TBuf8<64> currentName;
       
   241                     parentName.Copy( parent->NodeName() );
       
   242                     currentName.Copy( iCurrentNode->NodeName() );
       
   243                     iParserMain.WriteToLog( _L8( " Parent: %x - %S "), parent, &parentName );
       
   244                     iParserMain.WriteToLog( _L8( " Current: %x - %S"), iCurrentNode, &currentName );
       
   245                 #endif  
       
   246                 if( parent != NULL && iCurrentNode != parent )
       
   247                     iCurrentNode = iCurrentNode->Parent();
       
   248                 iFinished = iCurrentNode == iDocumentSubset->Parent();
       
   249                 if( iFinished )
       
   250                     iCurrentNode = NULL;
       
   251                 #ifdef _DEBUG
       
   252                     iParserMain.WriteToLog( _L8( " Current: %x"), iCurrentNode );
       
   253                 #endif   
       
   254                 }   
       
   255             }
       
   256         }
       
   257     else iCurrentNode = iCurrentNode->Parent();
       
   258     }
       
   259     
       
   260 // ---------------------------------------------------------
       
   261 // CXdmXmlContentHandler::OnEndPrefixMappingL
       
   262 //
       
   263 // ---------------------------------------------------------
       
   264 //
       
   265 void CXdmXmlContentHandler::OnEndPrefixMappingL( const RString& /*aPrefix*/,
       
   266                                                  TInt aErrorCode )
       
   267     {
       
   268     #ifdef _DEBUG
       
   269         iParserMain.WriteToLog( _L8( "CXmlNode::OnEndPrefixMappingL() - Error: %d "), aErrorCode );
       
   270     #endif
       
   271     }
       
   272     
       
   273 // ---------------------------------------------------------
       
   274 // CXdmXmlContentHandler::OnError
       
   275 //
       
   276 // ---------------------------------------------------------
       
   277 //
       
   278 void CXdmXmlContentHandler::OnError( TInt aErrorCode )
       
   279     {
       
   280     #ifdef _DEBUG
       
   281         iParserMain.WriteToLog( _L8( "CXmlNode::OnError() - Error: %d" ), aErrorCode );
       
   282     #endif
       
   283     }
       
   284     
       
   285 // ---------------------------------------------------------
       
   286 // CXdmXmlContentHandler::OnIgnorableWhiteSpaceL
       
   287 //
       
   288 // ---------------------------------------------------------
       
   289 //
       
   290 void CXdmXmlContentHandler::OnIgnorableWhiteSpaceL( const TDesC8& /*aBytes*/,
       
   291                                                     TInt /*aErrorCode*/ )
       
   292     {
       
   293     #ifdef _DEBUG
       
   294         //iParserMain.WriteToLog( _L8( "CXmlNode::OnIgnorableWhiteSpaceL() - Error: %d" ), aErrorCode );
       
   295     #endif
       
   296     }
       
   297 
       
   298 // ---------------------------------------------------------
       
   299 // CXdmXmlContentHandler::OnProcessingInstructionL
       
   300 //
       
   301 // ---------------------------------------------------------
       
   302 //
       
   303 void CXdmXmlContentHandler::OnProcessingInstructionL( const TDesC8& /*aTarget*/,
       
   304                                                       const TDesC8& /*aData*/,
       
   305                                                       TInt /*aErrorCode*/ )
       
   306     {
       
   307     #ifdef _DEBUG
       
   308        // iParserMain.WriteToLog( _L8( "CXmlNode::OnProcessingInstructionL() - Error: %d" ), aErrorCode );
       
   309     #endif
       
   310     }
       
   311     
       
   312 // ---------------------------------------------------------
       
   313 // CXdmXmlContentHandler::OnSkippedEntityL
       
   314 //
       
   315 // ---------------------------------------------------------
       
   316 //
       
   317 void CXdmXmlContentHandler::OnSkippedEntityL( const RString& /*aName*/,
       
   318                                               TInt /*aErrorCode*/ )
       
   319     {
       
   320     #ifdef _DEBUG
       
   321         //iParserMain.WriteToLog( _L8( "CXmlNode::OnSkippedEntityL() - Error: %d" ), aErrorCode );
       
   322     #endif
       
   323     }
       
   324 
       
   325 // ---------------------------------------------------------
       
   326 // CXdmXmlContentHandler::OnStartDocumentL
       
   327 //
       
   328 // ---------------------------------------------------------
       
   329 //
       
   330 void CXdmXmlContentHandler::OnStartDocumentL( const RDocumentParameters& /*aDocParam*/, TInt aErrorCode )
       
   331     {
       
   332     #ifdef _DEBUG
       
   333         iParserMain.WriteToLog( _L8( "CXmlNode::OnStartDocumentL() - Error: %d "), aErrorCode );
       
   334     #endif
       
   335     }
       
   336 
       
   337 // ---------------------------------------------------------
       
   338 // CXdmXmlContentHandler::OnStartElementL
       
   339 //
       
   340 // ---------------------------------------------------------
       
   341 //
       
   342 void CXdmXmlContentHandler::OnStartElementL( const RTagInfo& aElement,
       
   343                                              const RAttributeArray& aAttributes,
       
   344                                              TInt aErrorCode )
       
   345     {
       
   346     TPtrC8 name( aElement.LocalName().DesC() );
       
   347     TPtrC8 uri( aElement.Uri().DesC() );
       
   348     TPtrC8 prefix( aElement.Prefix().DesC() );
       
   349     #ifdef _DEBUG
       
   350         iParserMain.WriteToLog( _L8( "CXmlNode::OnStartElementL(): Error: %d" ), aErrorCode );
       
   351         iParserMain.WriteToLog( _L8( "  URI:     %S" ), &uri );
       
   352         iParserMain.WriteToLog( _L8( "  Name:    %S" ), &name );
       
   353         iParserMain.WriteToLog( _L8( "  Prefix:  %S" ), &prefix );
       
   354     #endif
       
   355     if( iPartialDocument )
       
   356         {   
       
   357         if( !iFinished )
       
   358             {
       
   359             if( iTargetFound )
       
   360                 {
       
   361                 iCurrentNode = iCurrentNode->CreateChileNodeL();
       
   362                 HandleNextElementL( aElement, aAttributes );
       
   363                 }
       
   364             else
       
   365                 {
       
   366                 if( iNodePath->CheckNextNodeL( name, aAttributes ) )
       
   367                     {
       
   368                     if( iElementType == EXdmElementAttribute )
       
   369                         AppendAttributeValueL( aAttributes );
       
   370                     else
       
   371                         {
       
   372                         iTargetFound = ETrue;
       
   373                         iCurrentNode = iDocumentSubset;
       
   374                         }
       
   375                     }
       
   376                 }
       
   377             }
       
   378         }
       
   379     else
       
   380         {
       
   381         iCurrentNode = iIsRootNode ? iRootNode : iCurrentNode->CreateChileNodeL();
       
   382         HandleNextElementL( aElement, aAttributes );
       
   383         iIsRootNode = EFalse;
       
   384         }    
       
   385     }
       
   386 
       
   387 // ---------------------------------------------------------
       
   388 // CXdmXmlContentHandler::HandleNextElementL
       
   389 //
       
   390 // ---------------------------------------------------------
       
   391 //
       
   392 void CXdmXmlContentHandler::HandleNextElementL( const RTagInfo& aElement,
       
   393                                                 const RAttributeArray& aAttributes )
       
   394     {
       
   395     #ifdef _DEBUG
       
   396         //iParserMain.WriteToLog( _L8( "CXdmXmlContentHandler::HandleNextElementL()") );
       
   397     #endif
       
   398     iCurrentNode->SetNameL( aElement.LocalName().DesC() );
       
   399     SetNamespaceInformationL( aElement, iCurrentNode );
       
   400     TInt count = aAttributes.Count();
       
   401     if( count > 0 )
       
   402         {
       
   403         CXdmNodeAttribute* attribute = NULL;
       
   404         for( TInt i = 0;i < count;i++ )
       
   405             {
       
   406             attribute = iCurrentNode->CreateAttributeL();
       
   407             CleanupStack::PushL( attribute );
       
   408             attribute->SetNameL( aAttributes[i].Attribute().LocalName().DesC() );
       
   409             attribute->SetAttributeValueL( aAttributes[i].Value().DesC() );
       
   410             CleanupStack::Pop();  //attribute
       
   411             }
       
   412         }
       
   413     }
       
   414                         
       
   415 // ---------------------------------------------------------
       
   416 // CXdmXmlContentHandler::CompileAttributesL
       
   417 //
       
   418 // ---------------------------------------------------------
       
   419 //
       
   420 void CXdmXmlContentHandler::AppendAttributeValueL( const RAttributeArray& aAttributes )
       
   421     {
       
   422     #ifdef _DEBUG
       
   423         //iParserMain.WriteToLog( _L8( "CXdmXmlContentHandler::AppendAttributeValueL()") );
       
   424     #endif
       
   425     TInt count = aAttributes.Count();
       
   426     TPtrC8 targetName( iTargetAttribute->EightBitNodeNameLC()->Des() );
       
   427     for( TInt i = 0;!iFinished && i < count;i++ )
       
   428         {
       
   429         TPtrC8 desc = aAttributes[i].Attribute().LocalName().DesC();
       
   430         #ifdef _DEBUG
       
   431             iParserMain.WriteToLog( _L8( "  Name of attribute %d: %S"), i, &desc );
       
   432         #endif
       
   433         if( targetName.Compare( desc ) == 0 )
       
   434             {
       
   435             iFinished = ETrue;
       
   436             TPtrC8 value( aAttributes[i].Value().DesC() );
       
   437             iTargetAttribute->SetAttributeValueL( value );
       
   438             }
       
   439         }
       
   440     CleanupStack::PopAndDestroy();  //desc
       
   441     }
       
   442     
       
   443 // ---------------------------------------------------------
       
   444 // CXdmXmlContentHandler::OnStartPrefixMappingL
       
   445 //
       
   446 // ---------------------------------------------------------
       
   447 //
       
   448 void CXdmXmlContentHandler::OnStartPrefixMappingL( const RString& aPrefix,
       
   449                                                    const RString& aUri,
       
   450                                                    TInt aErrorCode )
       
   451     {
       
   452     #ifdef _DEBUG
       
   453         iParserMain.WriteToLog( _L8( "CXdmXmlContentHandler::OnStartPrefixMappingL() - Error: %d" ), aErrorCode );
       
   454     #endif
       
   455     AppendNameSpaceL( aPrefix.DesC(), aUri.DesC() );
       
   456     }
       
   457     
       
   458 // ---------------------------------------------------------
       
   459 // CXdmXmlContentHandler::IsWhiteSpace
       
   460 //
       
   461 // ---------------------------------------------------------
       
   462 //
       
   463 TBool CXdmXmlContentHandler::IsWhiteSpace( const TDesC8 &aBytes ) const
       
   464     {
       
   465     #ifdef _DEBUG
       
   466         //iParserMain.WriteToLog( _L8( "CXdmXmlContentHandler::IsWhiteSpace()") );
       
   467     #endif
       
   468     TBool ret = ETrue;
       
   469     if( aBytes.Length() > 0 )
       
   470         {
       
   471         TChar ch = aBytes[0];
       
   472         ret = ch == 32 || ch == 10 || ch == 13 || ch == 9;
       
   473         }
       
   474     return ret;
       
   475     }
       
   476 
       
   477 // ---------------------------------------------------------
       
   478 // CXdmXmlContentHandler::AppendNameSpaceL
       
   479 //
       
   480 // ---------------------------------------------------------
       
   481 //
       
   482 void CXdmXmlContentHandler::AppendNameSpaceL( const TDesC8& aPrefix, const TDesC8& aUri )
       
   483     {
       
   484     #ifdef _DEBUG
       
   485         //iParserMain.WriteToLog( _L8( "CXdmXmlContentHandler::SetNameSpaceInformationL()") );
       
   486     #endif
       
   487     if( iTargetDocument != NULL )
       
   488         {
       
   489         MXdmNamespaceContainer* container = iTargetDocument;
       
   490         container->AppendNamespaceL( aUri, aPrefix );
       
   491         }
       
   492     }
       
   493        
       
   494 // ---------------------------------------------------------
       
   495 // CXdmXmlContentHandler::SetNameSpaceInformationL
       
   496 //
       
   497 // ---------------------------------------------------------
       
   498 //
       
   499 void CXdmXmlContentHandler::SetNamespaceInformationL( const RTagInfo& aElement,
       
   500                                                       MXdmNodeInterface* aXcapNode )
       
   501     {
       
   502     #ifdef _DEBUG
       
   503         //iParserMain.WriteToLog( _L8( "CXdmXmlContentHandler::SetNameSpaceInformationL()") );
       
   504     #endif
       
   505     TPtrC8 prefix( aElement.Prefix().DesC() );
       
   506     if( prefix.Length() > 0 )
       
   507         {
       
   508         aXcapNode->SetPrefixL( prefix );
       
   509         }
       
   510     }
       
   511             
       
   512 // End of File