xdmprotocols/XdmXmlParser/src/XdmXmlContentHandler.cpp
branchRCL_3
changeset 34 2669f8761a99
parent 31 2580314736af
child 35 fbd2e7cec7ef
equal deleted inserted replaced
31:2580314736af 34: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:   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 if ( iCurrentNode ) 
       
   258         {
       
   259         iCurrentNode = iCurrentNode->Parent();
       
   260         }
       
   261     else
       
   262         {
       
   263         // For PC-lint note
       
   264         }
       
   265     }
       
   266     
       
   267 // ---------------------------------------------------------
       
   268 // CXdmXmlContentHandler::OnEndPrefixMappingL
       
   269 //
       
   270 // ---------------------------------------------------------
       
   271 //
       
   272 void CXdmXmlContentHandler::OnEndPrefixMappingL( const RString& /*aPrefix*/,
       
   273                                                  TInt aErrorCode )
       
   274     {
       
   275     #ifdef _DEBUG
       
   276         iParserMain.WriteToLog( _L8( "CXmlNode::OnEndPrefixMappingL() - Error: %d "), aErrorCode );
       
   277     #endif
       
   278     }
       
   279     
       
   280 // ---------------------------------------------------------
       
   281 // CXdmXmlContentHandler::OnError
       
   282 //
       
   283 // ---------------------------------------------------------
       
   284 //
       
   285 void CXdmXmlContentHandler::OnError( TInt aErrorCode )
       
   286     {
       
   287     #ifdef _DEBUG
       
   288         iParserMain.WriteToLog( _L8( "CXmlNode::OnError() - Error: %d" ), aErrorCode );
       
   289     #endif
       
   290     }
       
   291     
       
   292 // ---------------------------------------------------------
       
   293 // CXdmXmlContentHandler::OnIgnorableWhiteSpaceL
       
   294 //
       
   295 // ---------------------------------------------------------
       
   296 //
       
   297 void CXdmXmlContentHandler::OnIgnorableWhiteSpaceL( const TDesC8& /*aBytes*/,
       
   298                                                     TInt /*aErrorCode*/ )
       
   299     {
       
   300     #ifdef _DEBUG
       
   301         //iParserMain.WriteToLog( _L8( "CXmlNode::OnIgnorableWhiteSpaceL() - Error: %d" ), aErrorCode );
       
   302     #endif
       
   303     }
       
   304 
       
   305 // ---------------------------------------------------------
       
   306 // CXdmXmlContentHandler::OnProcessingInstructionL
       
   307 //
       
   308 // ---------------------------------------------------------
       
   309 //
       
   310 void CXdmXmlContentHandler::OnProcessingInstructionL( const TDesC8& /*aTarget*/,
       
   311                                                       const TDesC8& /*aData*/,
       
   312                                                       TInt /*aErrorCode*/ )
       
   313     {
       
   314     #ifdef _DEBUG
       
   315        // iParserMain.WriteToLog( _L8( "CXmlNode::OnProcessingInstructionL() - Error: %d" ), aErrorCode );
       
   316     #endif
       
   317     }
       
   318     
       
   319 // ---------------------------------------------------------
       
   320 // CXdmXmlContentHandler::OnSkippedEntityL
       
   321 //
       
   322 // ---------------------------------------------------------
       
   323 //
       
   324 void CXdmXmlContentHandler::OnSkippedEntityL( const RString& /*aName*/,
       
   325                                               TInt /*aErrorCode*/ )
       
   326     {
       
   327     #ifdef _DEBUG
       
   328         //iParserMain.WriteToLog( _L8( "CXmlNode::OnSkippedEntityL() - Error: %d" ), aErrorCode );
       
   329     #endif
       
   330     }
       
   331 
       
   332 // ---------------------------------------------------------
       
   333 // CXdmXmlContentHandler::OnStartDocumentL
       
   334 //
       
   335 // ---------------------------------------------------------
       
   336 //
       
   337 void CXdmXmlContentHandler::OnStartDocumentL( const RDocumentParameters& /*aDocParam*/, TInt aErrorCode )
       
   338     {
       
   339     #ifdef _DEBUG
       
   340         iParserMain.WriteToLog( _L8( "CXmlNode::OnStartDocumentL() - Error: %d "), aErrorCode );
       
   341     #endif
       
   342     }
       
   343 
       
   344 // ---------------------------------------------------------
       
   345 // CXdmXmlContentHandler::OnStartElementL
       
   346 //
       
   347 // ---------------------------------------------------------
       
   348 //
       
   349 void CXdmXmlContentHandler::OnStartElementL( const RTagInfo& aElement,
       
   350                                              const RAttributeArray& aAttributes,
       
   351                                              TInt aErrorCode )
       
   352     {
       
   353     TPtrC8 name( aElement.LocalName().DesC() );
       
   354     TPtrC8 uri( aElement.Uri().DesC() );
       
   355     TPtrC8 prefix( aElement.Prefix().DesC() );
       
   356     #ifdef _DEBUG
       
   357         iParserMain.WriteToLog( _L8( "CXmlNode::OnStartElementL(): Error: %d" ), aErrorCode );
       
   358         iParserMain.WriteToLog( _L8( "  URI:     %S" ), &uri );
       
   359         iParserMain.WriteToLog( _L8( "  Name:    %S" ), &name );
       
   360         iParserMain.WriteToLog( _L8( "  Prefix:  %S" ), &prefix );
       
   361     #endif
       
   362     if( iPartialDocument )
       
   363         {   
       
   364         if( !iFinished )
       
   365             {
       
   366             if( iTargetFound )
       
   367                 {
       
   368                 iCurrentNode = iCurrentNode->CreateChileNodeL();
       
   369                 HandleNextElementL( aElement, aAttributes );
       
   370                 }
       
   371             else
       
   372                 {
       
   373                 if( iNodePath->CheckNextNodeL( name, aAttributes ) )
       
   374                     {
       
   375                     if( iElementType == EXdmElementAttribute )
       
   376                         AppendAttributeValueL( aAttributes );
       
   377                     else
       
   378                         {
       
   379                         iTargetFound = ETrue;
       
   380                         iCurrentNode = iDocumentSubset;
       
   381                         }
       
   382                     }
       
   383                 }
       
   384             }
       
   385         }
       
   386     else
       
   387         {
       
   388         iCurrentNode = iIsRootNode ? iRootNode : iCurrentNode->CreateChileNodeL();
       
   389         HandleNextElementL( aElement, aAttributes );
       
   390         iIsRootNode = EFalse;
       
   391         }    
       
   392     }
       
   393 
       
   394 // ---------------------------------------------------------
       
   395 // CXdmXmlContentHandler::HandleNextElementL
       
   396 //
       
   397 // ---------------------------------------------------------
       
   398 //
       
   399 void CXdmXmlContentHandler::HandleNextElementL( const RTagInfo& aElement,
       
   400                                                 const RAttributeArray& aAttributes )
       
   401     {
       
   402     #ifdef _DEBUG
       
   403         //iParserMain.WriteToLog( _L8( "CXdmXmlContentHandler::HandleNextElementL()") );
       
   404     #endif
       
   405     iCurrentNode->SetNameL( aElement.LocalName().DesC() );
       
   406     SetNamespaceInformationL( aElement, iCurrentNode );
       
   407     TInt count = aAttributes.Count();
       
   408     if( count > 0 )
       
   409         {
       
   410         CXdmNodeAttribute* attribute = NULL;
       
   411         for( TInt i = 0;i < count;i++ )
       
   412             {
       
   413             attribute = iCurrentNode->CreateAttributeL();
       
   414             CleanupStack::PushL( attribute );
       
   415             attribute->SetNameL( aAttributes[i].Attribute().LocalName().DesC() );
       
   416             attribute->SetAttributeValueL( aAttributes[i].Value().DesC() );
       
   417             CleanupStack::Pop();  //attribute
       
   418             }
       
   419         }
       
   420     }
       
   421                         
       
   422 // ---------------------------------------------------------
       
   423 // CXdmXmlContentHandler::CompileAttributesL
       
   424 //
       
   425 // ---------------------------------------------------------
       
   426 //
       
   427 void CXdmXmlContentHandler::AppendAttributeValueL( const RAttributeArray& aAttributes )
       
   428     {
       
   429     #ifdef _DEBUG
       
   430         //iParserMain.WriteToLog( _L8( "CXdmXmlContentHandler::AppendAttributeValueL()") );
       
   431     #endif
       
   432     TInt count = aAttributes.Count();
       
   433     TPtrC8 targetName( iTargetAttribute->EightBitNodeNameLC()->Des() );
       
   434     for( TInt i = 0;!iFinished && i < count;i++ )
       
   435         {
       
   436         TPtrC8 desc = aAttributes[i].Attribute().LocalName().DesC();
       
   437         #ifdef _DEBUG
       
   438             iParserMain.WriteToLog( _L8( "  Name of attribute %d: %S"), i, &desc );
       
   439         #endif
       
   440         if( targetName.Compare( desc ) == 0 )
       
   441             {
       
   442             iFinished = ETrue;
       
   443             TPtrC8 value( aAttributes[i].Value().DesC() );
       
   444             iTargetAttribute->SetAttributeValueL( value );
       
   445             }
       
   446         }
       
   447     CleanupStack::PopAndDestroy();  //desc
       
   448     }
       
   449     
       
   450 // ---------------------------------------------------------
       
   451 // CXdmXmlContentHandler::OnStartPrefixMappingL
       
   452 //
       
   453 // ---------------------------------------------------------
       
   454 //
       
   455 void CXdmXmlContentHandler::OnStartPrefixMappingL( const RString& aPrefix,
       
   456                                                    const RString& aUri,
       
   457                                                    TInt aErrorCode )
       
   458     {
       
   459     #ifdef _DEBUG
       
   460         iParserMain.WriteToLog( _L8( "CXdmXmlContentHandler::OnStartPrefixMappingL() - Error: %d" ), aErrorCode );
       
   461     #endif
       
   462     AppendNameSpaceL( aPrefix.DesC(), aUri.DesC() );
       
   463     }
       
   464     
       
   465 // ---------------------------------------------------------
       
   466 // CXdmXmlContentHandler::IsWhiteSpace
       
   467 //
       
   468 // ---------------------------------------------------------
       
   469 //
       
   470 TBool CXdmXmlContentHandler::IsWhiteSpace( const TDesC8 &aBytes ) const
       
   471     {
       
   472     #ifdef _DEBUG
       
   473         //iParserMain.WriteToLog( _L8( "CXdmXmlContentHandler::IsWhiteSpace()") );
       
   474     #endif
       
   475     TBool ret = ETrue;
       
   476     if( aBytes.Length() > 0 )
       
   477         {
       
   478         TChar ch = aBytes[0];
       
   479         ret = ch == 32 || ch == 10 || ch == 13 || ch == 9;
       
   480         }
       
   481     return ret;
       
   482     }
       
   483 
       
   484 // ---------------------------------------------------------
       
   485 // CXdmXmlContentHandler::AppendNameSpaceL
       
   486 //
       
   487 // ---------------------------------------------------------
       
   488 //
       
   489 void CXdmXmlContentHandler::AppendNameSpaceL( const TDesC8& aPrefix, const TDesC8& aUri )
       
   490     {
       
   491     #ifdef _DEBUG
       
   492         //iParserMain.WriteToLog( _L8( "CXdmXmlContentHandler::SetNameSpaceInformationL()") );
       
   493     #endif
       
   494     if( iTargetDocument != NULL )
       
   495         {
       
   496         MXdmNamespaceContainer* container = iTargetDocument;
       
   497         container->AppendNamespaceL( aUri, aPrefix );
       
   498         }
       
   499     }
       
   500        
       
   501 // ---------------------------------------------------------
       
   502 // CXdmXmlContentHandler::SetNameSpaceInformationL
       
   503 //
       
   504 // ---------------------------------------------------------
       
   505 //
       
   506 void CXdmXmlContentHandler::SetNamespaceInformationL( const RTagInfo& aElement,
       
   507                                                       MXdmNodeInterface* aXcapNode )
       
   508     {
       
   509     #ifdef _DEBUG
       
   510         //iParserMain.WriteToLog( _L8( "CXdmXmlContentHandler::SetNameSpaceInformationL()") );
       
   511     #endif
       
   512     TPtrC8 prefix( aElement.Prefix().DesC() );
       
   513     if( prefix.Length() > 0 )
       
   514         {
       
   515         aXcapNode->SetPrefixL( prefix );
       
   516         }
       
   517     }
       
   518             
       
   519 // End of File