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