locationmanager/ReverseGeocode/src/xmlhandler.cpp
branchRCL_3
changeset 19 b73252188534
equal deleted inserted replaced
18:63c982fb92f2 19:b73252188534
       
     1 /*
       
     2 * Copyright (c) 2006-2009 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: Implementation of the Parser 
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 #include <utf.h>
       
    21 #include "internaladdressinfo.h"
       
    22 #include "xmlhandler.h"
       
    23 #include "locationmanagerdebug.h"
       
    24 
       
    25 using namespace xmlhandler;
       
    26 
       
    27 // CONSTANTS
       
    28 _LIT8( KXmlMimeType, "text/xml" );
       
    29 _LIT8( KCountry, "country" );
       
    30 _LIT8( KState, "state" );
       
    31 _LIT8( KDistrict, "district" );
       
    32 _LIT8( KCity, "city" );
       
    33 _LIT8( KPostalCode, "postCode" );
       
    34 _LIT8( KThoroughfare, "thoroughfare" );
       
    35 _LIT8( KNameTag, "name" );
       
    36 _LIT8( KNumberTag, "number" );
       
    37 
       
    38 // METHODS DEFINITION
       
    39 
       
    40 // --------------------------------------------------------------------------
       
    41 // CXmlHandler::NewL
       
    42 // --------------------------------------------------------------------------
       
    43 CXmlHandler* CXmlHandler::NewL( MXmlHandlerObserver& aObserver,  CInternalAddressInfo *aAddressInfo )
       
    44     {
       
    45     LOG("CXmlHandler::NewL ,begin");
       
    46     CXmlHandler* self = CXmlHandler::NewLC( aObserver, aAddressInfo );
       
    47     CleanupStack::Pop(); //self
       
    48     return self;
       
    49     }
       
    50 
       
    51 // --------------------------------------------------------------------------
       
    52 // CXmlHandler::NewLC
       
    53 // --------------------------------------------------------------------------
       
    54 
       
    55 CXmlHandler* CXmlHandler::NewLC( MXmlHandlerObserver& aObserver,  CInternalAddressInfo *aAddressInfo  )
       
    56     {
       
    57     LOG("CXmlHandler::NewLC ,begin");
       
    58     CXmlHandler* self = new ( ELeave ) CXmlHandler( aObserver, aAddressInfo );
       
    59     CleanupStack::PushL( self );
       
    60     self->ConstructL();
       
    61     return self;
       
    62     }
       
    63 
       
    64 // --------------------------------------------------------------------------
       
    65 // CXmlHandler::~CXmlHandler
       
    66 // --------------------------------------------------------------------------
       
    67 CXmlHandler::~CXmlHandler()
       
    68     {
       
    69     LOG("CXmlHandler::~CXmlHandler");
       
    70     delete iParser;
       
    71     iParser = NULL;
       
    72     delete iBuffer;
       
    73     iBuffer = NULL;
       
    74     }
       
    75 
       
    76 // --------------------------------------------------------------------------
       
    77 // CXmlHandler::CXmlHandler
       
    78 // --------------------------------------------------------------------------
       
    79 CXmlHandler::CXmlHandler( MXmlHandlerObserver& aObserver, CInternalAddressInfo *aAddressInfo  ):
       
    80     iObserver( aObserver ),
       
    81     iParser( NULL ),
       
    82     iBuffer( NULL ),
       
    83     iAddressInfo( aAddressInfo ),
       
    84     iThoroughfare( EFalse )
       
    85     {
       
    86     }
       
    87 
       
    88 // --------------------------------------------------------------------------
       
    89 // CXmlHandler::ConstructL
       
    90 // --------------------------------------------------------------------------
       
    91 void CXmlHandler::ConstructL()
       
    92     {
       
    93     LOG("CXmlHandler::ConstructL ,begin");
       
    94     iParser = CParser::NewL( KXmlMimeType, *this );
       
    95  
       
    96     }
       
    97 
       
    98 // --------------------------------------------------------------------------
       
    99 // CXmlHandler::StartParsingL()
       
   100 // --------------------------------------------------------------------------   
       
   101 void CXmlHandler::StartParsingL( HBufC8 *aBuf )
       
   102     {
       
   103 	LOG("CXmlHandler::StartParsingL ,begin");
       
   104      //Reset the address values before starting new content parsing
       
   105      iAddressInfo->ResetAddressInfoL();
       
   106       
       
   107     if( iBuffer )
       
   108         {
       
   109         delete iBuffer;
       
   110         iBuffer = NULL;
       
   111         }
       
   112 
       
   113     iBuffer = HBufC8::NewL( aBuf->Size() );
       
   114     TPtr8 ptr = iBuffer->Des();
       
   115     ptr.Copy( aBuf->Ptr() ,  aBuf->Size() );
       
   116     
       
   117     // Now, we have the whole file content in iBuffer.
       
   118     // We are ready to parse the XML content.
       
   119     iParser->ParseBeginL();
       
   120     iParser->ParseL( *iBuffer );
       
   121     
       
   122     // Since we read the whole file contents within one-shot,
       
   123     // we can call ParseEndL() right after calling ParseL().
       
   124     iParser->ParseEndL();
       
   125 	LOG("CXmlHandler::StartParsingL ,end");
       
   126     }
       
   127 
       
   128 // --------------------------------------------------------------------------
       
   129 // CXmlHandler::OnStartDocumentL()
       
   130 // --------------------------------------------------------------------------
       
   131 void CXmlHandler::OnStartDocumentL( const RDocumentParameters& /*aDocParam*/,
       
   132                                     TInt aErrorCode )
       
   133     {
       
   134     LOG1("CXmlHandler::OnStartDocumentL ,Error code - %d", aErrorCode);
       
   135     if( KErrNone != aErrorCode )
       
   136         {
       
   137         iObserver.OnParseCompletedL( aErrorCode, *iAddressInfo );
       
   138         }
       
   139     else
       
   140         {
       
   141         // Do nothing
       
   142         }
       
   143 	LOG("CXmlHandler::OnStartDocumentL ,end");
       
   144     }
       
   145 
       
   146 // --------------------------------------------------------------------------
       
   147 // CXmlHandler::OnEndDocumentL()
       
   148 // --------------------------------------------------------------------------    
       
   149 void CXmlHandler::OnEndDocumentL( TInt aErrorCode )
       
   150     {
       
   151     LOG1("CXmlHandler::OnEndDocumentL ,Errcode - %d", aErrorCode);
       
   152     iObserver.OnParseCompletedL( aErrorCode, *iAddressInfo );
       
   153     }
       
   154 
       
   155 // --------------------------------------------------------------------------
       
   156 // CXmlHandler::OnStartElementL()
       
   157 // --------------------------------------------------------------------------
       
   158 void CXmlHandler::OnStartElementL( const RTagInfo& aElement,
       
   159                                    const RAttributeArray& /*aAttributes*/, TInt aErrorCode )
       
   160     {
       
   161     LOG1("CXmlHandler::OnStartElementL , Errorcode - %d", aErrorCode);
       
   162     if ( KErrNone == aErrorCode )
       
   163         {
       
   164         // If we find the start of an element, we write to the screen,
       
   165         // for example: "<tag>"
       
   166         
       
   167         if( !aElement.LocalName().DesC().Compare( KCountry ) )
       
   168             {
       
   169             iCurrentElement = ECountryName;
       
   170             }
       
   171         else if( !aElement.LocalName().DesC().Compare( KState ) )
       
   172             {
       
   173             iCurrentElement = EState;
       
   174             }
       
   175         else if( !aElement.LocalName().DesC().Compare( KCity ) )
       
   176             {
       
   177             iCurrentElement = ECity;
       
   178             }
       
   179         else if( !aElement.LocalName().DesC().Compare( KDistrict ) )
       
   180             {
       
   181             iCurrentElement = EDistrict;
       
   182             }
       
   183         else if( !aElement.LocalName().DesC().Compare( KPostalCode )  )
       
   184             {
       
   185             iCurrentElement = EPostalCode;
       
   186             }
       
   187         else if( !aElement.LocalName().DesC().Compare( KThoroughfare )  )
       
   188             {
       
   189             iThoroughfare = ETrue;
       
   190             }
       
   191         else if( !aElement.LocalName().DesC().Compare( KNameTag ) && iThoroughfare )
       
   192             {
       
   193             iCurrentElement = EThoroughfareName;
       
   194             }
       
   195         else if( !aElement.LocalName().DesC().Compare( KNumberTag ) && iThoroughfare )
       
   196             {
       
   197             iCurrentElement = EThoroughfareNumber;
       
   198             }
       
   199         else
       
   200             {
       
   201             ///Do something
       
   202             }
       
   203         }
       
   204     else
       
   205         {
       
   206         iObserver.OnParseCompletedL( aErrorCode, *iAddressInfo );
       
   207         }
       
   208 	LOG("CXmlHandler::OnStartElementL ,end");
       
   209     }
       
   210         
       
   211 // --------------------------------------------------------------------------
       
   212 // CXmlHandler::OnEndElementL()
       
   213 // --------------------------------------------------------------------------
       
   214 void CXmlHandler::OnEndElementL( const RTagInfo& /*aElement*/, TInt aErrorCode )
       
   215     {
       
   216     LOG1("CXmlHandler::OnEndElementL ,Error code - %d", aErrorCode);
       
   217     if( KErrNone == aErrorCode )
       
   218         {
       
   219         // at the end of the tag </tag>
       
   220         //Set it to ENone
       
   221         iCurrentElement = ENone;
       
   222         iThoroughfare = EFalse;
       
   223         }
       
   224     else
       
   225         {
       
   226         iObserver.OnParseCompletedL( aErrorCode, *iAddressInfo );
       
   227         }
       
   228     }
       
   229     
       
   230 // --------------------------------------------------------------------------
       
   231 // CXmlHandler::OnContentL()
       
   232 // --------------------------------------------------------------------------
       
   233 void CXmlHandler::OnContentL( const TDesC8 &aBytes, TInt aErrorCode )
       
   234     {
       
   235     LOG1("CXmlHandler::OnContentL ,Errorcode - %d", aErrorCode);
       
   236     if( KErrNone == aErrorCode )
       
   237         {
       
   238 
       
   239         if( iCurrentElement == ENone )
       
   240             {
       
   241             //ignore if the current element is not there
       
   242             return;
       
   243             }
       
   244 
       
   245         // convert the content to UCS-2
       
   246         // from UTF-8        
       
   247         RBuf buffer;
       
   248         buffer.CreateL( aBytes.Length() );
       
   249         CleanupClosePushL(buffer);
       
   250         CnvUtfConverter::ConvertToUnicodeFromUtf8( buffer , aBytes );
       
   251         
       
   252         if( iCurrentElement == ECountryName )
       
   253             {
       
   254             iAddressInfo->SetCountryName( buffer );
       
   255             }
       
   256         else if( iCurrentElement == EState )
       
   257             {
       
   258             iAddressInfo->SetState( buffer );
       
   259             }
       
   260         else if( iCurrentElement == EDistrict )
       
   261             {
       
   262             iAddressInfo->SetDistrict( buffer );
       
   263             }
       
   264         else if( iCurrentElement == ECity )
       
   265             {
       
   266             iAddressInfo->SetCity( buffer );
       
   267             }
       
   268         else if( iCurrentElement == EPostalCode )
       
   269             {
       
   270             iAddressInfo->SetPincode( buffer );
       
   271             }
       
   272         else if( iCurrentElement == EThoroughfareName )
       
   273             {
       
   274             iAddressInfo->SetThoroughfareName( buffer );
       
   275             }
       
   276         else if( iCurrentElement == EThoroughfareNumber )
       
   277             {
       
   278             iAddressInfo->SetThoroughfareNumber( buffer );
       
   279             }
       
   280         else
       
   281             {
       
   282                  ///Do something
       
   283             }
       
   284         CleanupStack::PopAndDestroy(); // buffer
       
   285         }
       
   286     else
       
   287         {
       
   288         iObserver.OnParseCompletedL( aErrorCode, *iAddressInfo );
       
   289         }
       
   290 	LOG("CXmlHandler::OnContentL ,end");
       
   291     }
       
   292     
       
   293 // --------------------------------------------------------------------------
       
   294 // CXmlHandler::OnStartPrefixMappingL()
       
   295 // --------------------------------------------------------------------------
       
   296 void CXmlHandler::OnStartPrefixMappingL( const RString& /*aPrefix*/,
       
   297                                          const RString& /*aUri*/, TInt aErrorCode )
       
   298     {
       
   299     LOG1("CXmlHandler::OnStartPrefixMappingL ,Error code - %d", aErrorCode);
       
   300     if( KErrNone != aErrorCode )
       
   301         {
       
   302         iObserver.OnParseCompletedL( aErrorCode, *iAddressInfo );
       
   303         }
       
   304     else
       
   305         {
       
   306         // Do nothing
       
   307         }
       
   308     }
       
   309         
       
   310 // --------------------------------------------------------------------------
       
   311 // CXmlHandler::OnEndPrefixMappingL()
       
   312 // --------------------------------------------------------------------------
       
   313 void CXmlHandler::OnEndPrefixMappingL( const RString& /*aPrefix*/, TInt aErrorCode )
       
   314     {
       
   315     LOG1("CXmlHandler::OnEndPrefixMappingL ,Error code - %d", aErrorCode);
       
   316     if( KErrNone != aErrorCode )
       
   317         {
       
   318         iObserver.OnParseCompletedL( aErrorCode, *iAddressInfo );
       
   319         }
       
   320     else
       
   321         {
       
   322         // Do nothing
       
   323         }
       
   324     }
       
   325     
       
   326 // --------------------------------------------------------------------------
       
   327 // CXmlHandler::OnIgnorableWhiteSpaceL()
       
   328 // --------------------------------------------------------------------------
       
   329 void CXmlHandler::OnIgnorableWhiteSpaceL( const TDesC8& /*aBytes*/,TInt aErrorCode )
       
   330     {
       
   331     LOG1("CXmlHandler::OnIgnorableWhiteSpaceL ,Error code - %d", aErrorCode);
       
   332     if( KErrNone != aErrorCode )
       
   333         {
       
   334         iObserver.OnParseCompletedL( aErrorCode, *iAddressInfo );
       
   335         }
       
   336     else
       
   337         {
       
   338         // Do nothing
       
   339         }
       
   340     }
       
   341     
       
   342 // --------------------------------------------------------------------------
       
   343 // CXmlHandler::OnSkippedEntityL()
       
   344 // --------------------------------------------------------------------------
       
   345 void CXmlHandler::OnSkippedEntityL( const RString& /*aName*/, TInt aErrorCode )
       
   346     {
       
   347     LOG1("CXmlHandler::OnSkippedEntityL ,Error code - %d", aErrorCode);
       
   348     if( KErrNone != aErrorCode )
       
   349         {
       
   350         iObserver.OnParseCompletedL( aErrorCode, *iAddressInfo );
       
   351         }
       
   352     else
       
   353         {
       
   354         // Do nothing
       
   355         }
       
   356     }
       
   357 
       
   358 // --------------------------------------------------------------------------
       
   359 // CXmlHandler::OnProcessingInstructionL()
       
   360 // --------------------------------------------------------------------------
       
   361 void CXmlHandler::OnProcessingInstructionL( const TDesC8& /*aTarget*/, const TDesC8& /*aData*/, TInt aErrorCode )
       
   362     {
       
   363     LOG1("CXmlHandler::OnProcessingInstructionL ,Error code - %d", aErrorCode);
       
   364     if( KErrNone != aErrorCode )
       
   365         {
       
   366         iObserver.OnParseCompletedL( aErrorCode, *iAddressInfo );
       
   367         }
       
   368     else
       
   369         {
       
   370         // Do nothing
       
   371         }
       
   372     }
       
   373 
       
   374 // --------------------------------------------------------------------------
       
   375 // CXmlHandler::OnError()
       
   376 // --------------------------------------------------------------------------
       
   377 void CXmlHandler::OnError( TInt aErrorCode )
       
   378     {
       
   379     LOG1("CXmlHandler::OnError ,Error code - %d", aErrorCode);
       
   380     TRAP_IGNORE( iObserver.OnParseCompletedL( aErrorCode, *iAddressInfo ) );
       
   381     }
       
   382 
       
   383 // --------------------------------------------------------------------------
       
   384 // CXmlHandler::GetExtendedInterface()
       
   385 // --------------------------------------------------------------------------
       
   386 TAny* CXmlHandler::GetExtendedInterface( const TInt32 /*aUid*/ )
       
   387     {
       
   388     return 0;
       
   389     }
       
   390 
       
   391 // End of File