xdmprotocols/XdmXmlParser/src/XdmXmlParser.cpp
branchGCC_SURGE
changeset 28 d9861ae9169c
parent 23 77cb48a03620
parent 26 04ca1926b01c
equal deleted inserted replaced
23:77cb48a03620 28:d9861ae9169c
     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:   CXdmXmlParser
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <hal.h>
       
    23 #include <parser.h>
       
    24 #include <f32file.h>
       
    25 #include <flogger.h>
       
    26 #include <parserfeature.h>
       
    27 #include <XdmNodeAttribute.h>
       
    28 #include <XdmDocument.h>
       
    29 #include <xdmlogwriter.h>
       
    30 #include <XdmDocumentNode.h>
       
    31 #include "XdmXmlParser.h"
       
    32 #include "XmlFormatter.h"
       
    33 #include "XmlParserDefines.h"
       
    34 #include "XdmXmlContentHandler.h"
       
    35 
       
    36 // ----------------------------------------------------------
       
    37 // CXdmXmlParser::CXdmXmlParser
       
    38 // 
       
    39 // ----------------------------------------------------------
       
    40 //
       
    41 CXdmXmlParser::CXdmXmlParser() : iDumpIndex( 1 ),
       
    42                                  iXmlDocument( ( TText8* )"", 0, 0 )                                           
       
    43     {
       
    44     }
       
    45 
       
    46 // ----------------------------------------------------------
       
    47 // CXdmXmlParser::NewL
       
    48 // 
       
    49 // ----------------------------------------------------------
       
    50 //
       
    51 EXPORT_C CXdmXmlParser* CXdmXmlParser::NewL()
       
    52     {
       
    53     CXdmXmlParser* self = new ( ELeave ) CXdmXmlParser();
       
    54     CleanupStack::PushL( self );
       
    55     self->ConstructL();
       
    56     CleanupStack::Pop();
       
    57     return self;
       
    58     }
       
    59 
       
    60 // ----------------------------------------------------------
       
    61 // CXdmXmlParser::ConstructL
       
    62 // 
       
    63 // ----------------------------------------------------------
       
    64 //
       
    65 void CXdmXmlParser::ConstructL()
       
    66     {
       
    67     #ifdef _DEBUG
       
    68         iLogWriter = CXdmLogWriter::NewL( KParserLogFile );
       
    69         WriteToLog( _L8( "CXdmXmlParser::ConstructL()" ) );
       
    70     #endif
       
    71     iXmlFormatter = CXmlFormatter::NewL( *this );
       
    72     iContentHandler = CXdmXmlContentHandler::NewL( *this );
       
    73     iXmlParser = CParser::NewL( KDefaultMimeType, *iContentHandler );               
       
    74     }
       
    75 
       
    76 // ----------------------------------------------------
       
    77 // CXdmXmlParser::~CXdmXmlParser
       
    78 // 
       
    79 // ----------------------------------------------------
       
    80 //
       
    81 CXdmXmlParser::~CXdmXmlParser()
       
    82     {
       
    83     #ifdef _DEBUG
       
    84         WriteToLog( _L8( "CXdmXmlParser::~CXdmXmlParser()" ) );
       
    85     #endif
       
    86     delete iXmlParser;
       
    87     delete iLogWriter;
       
    88     delete iXmlFormatter;
       
    89     delete iContentHandler;
       
    90     }
       
    91      
       
    92 // ----------------------------------------------------------
       
    93 // CXdmXmlParser::ParseDocumentL
       
    94 // 
       
    95 // ----------------------------------------------------------
       
    96 //    
       
    97 EXPORT_C void CXdmXmlParser::ParseDocumentL( CXdmDocument* aDocument,
       
    98                                              const TDesC8& aXmlDocument )  
       
    99     {
       
   100     #ifdef _DEBUG
       
   101         TInt start = CXdmXmlParser::TimeL();
       
   102     #endif
       
   103     CXdmDocumentNode* root = aDocument->DocumentRoot();
       
   104     if( root == NULL )
       
   105         aDocument->CreateRootL();
       
   106     User::LeaveIfError( iXmlParser->EnableFeature( EReportNamespaceMapping ) );
       
   107     iContentHandler->SetTarget( *aDocument );
       
   108     Xml::ParseL( *iXmlParser, aXmlDocument );
       
   109     iContentHandler->Reset();
       
   110     //self->ParseL( aDocument, aXmlDocument );
       
   111     #ifdef _DEBUG
       
   112         TInt finish = CXdmXmlParser::TimeL();
       
   113         WriteToLog( _L8( "CXdmXmlParser::ParseDocumentL() - Parsing took %d milliseconds" ), finish - start ); 
       
   114     #endif
       
   115     }
       
   116 
       
   117 // ----------------------------------------------------------
       
   118 // CXdmXmlParser::ParseDocumentL
       
   119 // 
       
   120 // ----------------------------------------------------------
       
   121 // 
       
   122 EXPORT_C void CXdmXmlParser::ParseDocumentL( const TDesC8& aXmlDocument,
       
   123                                              CXdmDocumentNode* aDocumentRoot )  
       
   124     {
       
   125     #ifdef _DEBUG
       
   126         TInt start = CXdmXmlParser::TimeL();
       
   127     #endif
       
   128     User::LeaveIfError( iXmlParser->EnableFeature( EReportNamespaceMapping ) );
       
   129     iContentHandler->SetTarget( *aDocumentRoot );
       
   130     Xml::ParseL( *iXmlParser, aXmlDocument );
       
   131     iContentHandler->Reset();
       
   132     //self->ParseL( aXmlDocument, aDocumentRoot );
       
   133     #ifdef _DEBUG
       
   134         TInt finish = CXdmXmlParser::TimeL();
       
   135         WriteToLog( _L8( "CXdmXmlParser::ParseDocumentL() - Parsing took %d milliseconds" ), finish - start ); 
       
   136     #endif
       
   137     }
       
   138     
       
   139 // ----------------------------------------------------------
       
   140 // CXdmXmlParser::ParseDocumentL
       
   141 // 
       
   142 // ----------------------------------------------------------
       
   143 // 
       
   144 EXPORT_C void CXdmXmlParser::ParseDocumentL( CXdmDocument* aDocument,
       
   145                                              const TDesC8& aXmlDocument,
       
   146                                              CXdmDocumentNode* aDocumentSubset )  
       
   147     {
       
   148     #ifdef _DEBUG
       
   149         TInt start = CXdmXmlParser::TimeL();
       
   150     #endif
       
   151     User::LeaveIfError( iXmlParser->EnableFeature( EReportNamespaces ) );
       
   152     User::LeaveIfError( iXmlParser->EnableFeature( EReportNamespaceMapping ) );
       
   153     User::LeaveIfError( iXmlParser->EnableFeature( EReportNamespacePrefixes ) );
       
   154     iContentHandler->SetTargetL( *aDocument, *aDocumentSubset );
       
   155     Xml::ParseL( *iXmlParser, aXmlDocument );
       
   156     iContentHandler->Reset();
       
   157     //self->ParseL( aDocument, aXmlDocument, aDocumentSubset );
       
   158     #ifdef _DEBUG
       
   159         TInt finish = CXdmXmlParser::TimeL();
       
   160         CXdmXmlParser::WriteToLog( _L8(
       
   161             "CXdmXmlParser::ParseDocumentL() - Parsing took %d milliseconds" ), finish - start ); 
       
   162     #endif
       
   163     }
       
   164     
       
   165 // ----------------------------------------------------------
       
   166 // CXdmXmlParser::FormatToXmlLC
       
   167 // 
       
   168 // ----------------------------------------------------------
       
   169 //       
       
   170 EXPORT_C HBufC8* CXdmXmlParser::FormatToXmlLC( TBool aIsWholeDocument,
       
   171                                                const CXdmDocument* aDocument, 
       
   172                                                const CXdmDocumentNode* aRootNode )
       
   173     {
       
   174     #ifdef _DEBUG
       
   175         TInt start = CXdmXmlParser::TimeL();
       
   176     #endif
       
   177     HBufC8* ret = iXmlFormatter->FormatLC( aIsWholeDocument, aDocument, aRootNode );
       
   178     #ifdef _DEBUG
       
   179         DumpDocumentL( ret );
       
   180         TInt finish = CXdmXmlParser::TimeL();
       
   181         CXdmXmlParser::WriteToLog( _L8(
       
   182             "CXdmXmlParser::FormatToXmlLC() - Formatting took %d milliseconds" ), finish - start ); 
       
   183     #endif
       
   184     return ret;  
       
   185     }
       
   186 
       
   187 // ----------------------------------------------------------
       
   188 // CXdmXmlParser::FormatToXmlLC
       
   189 // 
       
   190 // ----------------------------------------------------------
       
   191 //      
       
   192 EXPORT_C HBufC8* CXdmXmlParser::FormatToXmlLC( const TDesC8& aXmlFragment,
       
   193                                                const CXdmDocument* aDocument,
       
   194                                                const CXdmDocumentNode* aTargetNode )
       
   195     {
       
   196     #ifdef _DEBUG
       
   197         TInt start = CXdmXmlParser::TimeL();
       
   198     #endif
       
   199     //HBufC8* ret = self->FormatLC( aXmlFragment, aDocument, aTargetNode );
       
   200     HBufC8* ret = iXmlFormatter->FormatLC( aXmlFragment, aDocument, aTargetNode );
       
   201     #ifdef _DEBUG
       
   202         DumpDocumentL( ret );
       
   203         TInt finish = CXdmXmlParser::TimeL();
       
   204         CXdmXmlParser::WriteToLog( _L8(
       
   205             "CXdmXmlParser::FormatToXmlLC() - Formatting took %d milliseconds" ), finish - start ); 
       
   206     #endif
       
   207     return ret;  
       
   208     }
       
   209     
       
   210 // ---------------------------------------------------------
       
   211 // CXdmXmlParser::FinishParsing
       
   212 //
       
   213 // ---------------------------------------------------------
       
   214 //
       
   215 void CXdmXmlParser::FinishParsingL()
       
   216     {
       
   217     #ifdef _DEBUG
       
   218         WriteToLog( _L8( "CXdmXmlParser::FinishParsing()" ) );
       
   219     #endif
       
   220     iXmlParser->ParseEndL();
       
   221     }
       
   222 
       
   223 #ifdef _DEBUG 
       
   224 
       
   225 // ---------------------------------------------------------
       
   226 // CXcapPartDocOperation::DumpDocumentL
       
   227 //
       
   228 // ---------------------------------------------------------
       
   229 //
       
   230 void CXdmXmlParser::DumpDocumentL( HBufC8* aDocData ) 
       
   231     {
       
   232     if( aDocData )
       
   233         {
       
   234         RFile file;
       
   235         RFs session;
       
   236         TPtr8 pointer( aDocData->Des() );
       
   237         _LIT( KXmlFileExtension, ".xml" );
       
   238         TBuf<128> nameBuf( _L( "C:\\logs\\XDM\\request" ) );
       
   239         nameBuf.AppendNum( iDumpIndex );
       
   240         nameBuf.Append( KXmlFileExtension );
       
   241         User::LeaveIfError( session.Connect() );
       
   242         TInt error( file.Replace( session, nameBuf, EFileWrite ) );
       
   243         if( error == KErrNone )
       
   244             {
       
   245             file.Write( pointer );
       
   246             file.Close();
       
   247             iDumpIndex++;
       
   248             }
       
   249         session.Close();
       
   250         }
       
   251     }
       
   252     
       
   253 // ----------------------------------------------------------
       
   254 // CXdmXmlParser::TimeL
       
   255 // 
       
   256 // ----------------------------------------------------------
       
   257 //
       
   258 TInt CXdmXmlParser::TimeL()
       
   259     {
       
   260     TInt period = 0;
       
   261     User::LeaveIfError( HAL::Get( HALData::ESystemTickPeriod, period ) );
       
   262     TInt millisecsPerTick = period / 1000;
       
   263     return User::TickCount() * millisecsPerTick;
       
   264     }
       
   265 
       
   266 // ----------------------------------------------------------
       
   267 // CXdmXmlParser::WriteToLog
       
   268 // 
       
   269 // ----------------------------------------------------------
       
   270 //
       
   271 void CXdmXmlParser::WriteToLog( TRefByValue<const TDesC8> aFmt,... ) const                                
       
   272     {
       
   273     VA_LIST list;
       
   274     VA_START( list, aFmt );
       
   275     TBuf8<KLogBufferMaxSize> buf;
       
   276     buf.FormatList( aFmt, list );
       
   277     iLogWriter->WriteToLog( buf );
       
   278     }
       
   279 
       
   280 #endif
       
   281 
       
   282 
       
   283 
       
   284