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