xdmprotocols/XdmXmlParser/src/XmlFormatter.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:   CXmlFormatter
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDES
       
    22 #include <XdmDocument.h>
       
    23 #include <XdmDocumentNode.h>
       
    24 #include <XdmNodeAttribute.h>
       
    25 #include "XmlFormatter.h"
       
    26 #include "XmlParserDefines.h"
       
    27 #include "XmlParserNodePath.h"
       
    28 
       
    29 // ----------------------------------------------------------
       
    30 // CXmlFormatter::CXmlFormatter
       
    31 // 
       
    32 // ----------------------------------------------------------
       
    33 //
       
    34 CXmlFormatter::CXmlFormatter( CXdmXmlParser& aParserMain ) :
       
    35                               iTargetFound( EFalse ),
       
    36                               iIsAppended( EFalse ),
       
    37                               iParserMain( aParserMain )
       
    38     {
       
    39     }
       
    40 
       
    41 // ----------------------------------------------------------
       
    42 // CXmlFormatter::NewL
       
    43 // 
       
    44 // ----------------------------------------------------------
       
    45 //    
       
    46 CXmlFormatter* CXmlFormatter::NewL( CXdmXmlParser& aParserMain )
       
    47     {
       
    48     CXmlFormatter* self = new ( ELeave ) CXmlFormatter( aParserMain );
       
    49     CleanupStack::PushL( self );
       
    50     self->ConstructL();
       
    51     CleanupStack::Pop();
       
    52     return self;
       
    53     }
       
    54 
       
    55 // ----------------------------------------------------------
       
    56 // CXmlFormatter::~CXmlFormatter
       
    57 // 
       
    58 // ----------------------------------------------------------
       
    59 //
       
    60 CXmlFormatter::~CXmlFormatter()
       
    61     {        
       
    62     }
       
    63 
       
    64 // ----------------------------------------------------------
       
    65 // CXmlFormatter::ConstructL
       
    66 // 
       
    67 // ----------------------------------------------------------
       
    68 //        
       
    69 void CXmlFormatter::ConstructL()
       
    70     {
       
    71     }
       
    72 
       
    73 // ---------------------------------------------------------
       
    74 // CXmlFormatter::Prefix
       
    75 //
       
    76 // ---------------------------------------------------------
       
    77 //
       
    78 TPtrC8 CXmlFormatter::Prefix( const MXdmNodeInterface& aInterface )
       
    79     {
       
    80     return aInterface.Prefix();
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------
       
    84 // CXdmXmlContentHandler::Reset
       
    85 //
       
    86 // ---------------------------------------------------------
       
    87 //
       
    88 CXdmNodeAttribute* CXmlFormatter::TargetAttribute() const
       
    89     {
       
    90     return iTargetAttribute;
       
    91     }
       
    92     
       
    93 // ---------------------------------------------------------
       
    94 // CXmlFormatter::FindElementName
       
    95 //
       
    96 // ---------------------------------------------------------
       
    97 //
       
    98 TPtrC8 CXmlFormatter::FindElementName( const TDesC8& aElementString )
       
    99     {
       
   100     TInt index = aElementString.LocateF( 32 );
       
   101     if( index > 0 )
       
   102         return aElementString.Left( index );
       
   103     else return aElementString;
       
   104     }
       
   105 
       
   106 // ---------------------------------------------------------
       
   107 // CXmlFormatter::ResetFormatBuffer
       
   108 //
       
   109 // ---------------------------------------------------------
       
   110 //
       
   111 void CXmlFormatter::ResetFormatBuffer()
       
   112     {
       
   113     iFormBufferPos = 0;
       
   114     delete iFormatBuffer;
       
   115     iFormatBuffer = NULL;
       
   116     delete iNodePath;
       
   117     iNodePath = NULL;
       
   118     }
       
   119 
       
   120 
       
   121 // ---------------------------------------------------------
       
   122 // CXmlFormatter::BufferToStringLC
       
   123 //
       
   124 // ---------------------------------------------------------
       
   125 //
       
   126 HBufC8* CXmlFormatter::BufferToStringLC()
       
   127     {
       
   128     HBufC8* ret = HBufC8::NewLC( iFormatBuffer->Size() );
       
   129     TPtr8 pointer( ret->Des() );
       
   130     iFormatBuffer->Read( 0, pointer, iFormatBuffer->Size() );
       
   131     ResetFormatBuffer();
       
   132     return ret;
       
   133     }
       
   134       
       
   135 // ---------------------------------------------------------
       
   136 // CXmlFormatter::CheckSubsetPath
       
   137 //
       
   138 // ---------------------------------------------------------
       
   139 //
       
   140 void CXmlFormatter::CheckSubsetPath( const CXdmDocumentNode& aNode )
       
   141     {
       
   142     iTargetFound = iNodePath->CheckNextNode( aNode );
       
   143     }
       
   144 
       
   145 // ----------------------------------------------------------
       
   146 // CXmlFormatter::FormatLC
       
   147 // 
       
   148 // ----------------------------------------------------------
       
   149 //
       
   150 HBufC8* CXmlFormatter::FormatLC( const TDesC8& aXmlFragment,
       
   151                                  const CXdmDocument* aDocument,
       
   152                                  const CXdmDocumentNode* aTargetNode )
       
   153     {
       
   154     iIsAppended = EFalse;
       
   155     iTargetFound = EFalse;
       
   156     TBool addToRoot = EFalse;
       
   157     iFormatBuffer = CBufSeg::NewL( 128 );
       
   158     iElementType = aTargetNode->ElementType();
       
   159     iDocumentFragment.Set( aXmlFragment );
       
   160     iNodePath = CXmlParserNodePath::NewL( iParserMain, iElementType, *aTargetNode, this );
       
   161     CXdmDocumentNode* element = aDocument->DocumentRoot();
       
   162     CXdmDocumentNode* root = element;
       
   163     CheckSubsetPath( *root );
       
   164     if( iTargetFound )
       
   165         {
       
   166         addToRoot = ETrue;
       
   167         iIsAppended = ETrue;
       
   168         }
       
   169     iFormatBuffer->InsertL( iFormBufferPos, KXmldocumentStart );
       
   170     iFormBufferPos = iFormBufferPos + KXmldocumentStart().Length();
       
   171     FormatNamespaceDeclarationsL( *root, *aDocument );
       
   172     TInt count = element->NodeCount();
       
   173     if( count > 0 )
       
   174         {
       
   175         for( TInt i = 0;i < count;i++ )
       
   176             {
       
   177             element = element->ChileNode( i );
       
   178             FormatElementsL( element );
       
   179             }
       
   180         }
       
   181     else FormatElementsL( element );
       
   182     if( addToRoot )
       
   183         AppendXmlFragmentL();
       
   184     TPtrC8 name( root->EightBitNodeNameLC()->Des() );
       
   185     TPtrC8 prefix = root->Prefix();
       
   186     FormatElementEndL( prefix, name );
       
   187     CleanupStack::PopAndDestroy();  //EightBitNodeNameLC()
       
   188     HBufC8* ret = BufferToStringLC();
       
   189     return ret;
       
   190     }
       
   191 
       
   192 // ---------------------------------------------------------
       
   193 // CXmlFormatter::FormatElementsL
       
   194 //
       
   195 // ---------------------------------------------------------
       
   196 //
       
   197 void CXmlFormatter::FormatElementsL( CXdmDocumentNode* aCurrentNode )
       
   198     {
       
   199     TBool ready = EFalse;
       
   200     TBool addNow = EFalse;
       
   201     if( !iTargetFound && !iIsAppended )
       
   202         {
       
   203         CheckSubsetPath( *aCurrentNode );
       
   204         addNow = iTargetFound && !iIsAppended;
       
   205         }
       
   206     TPtrC8 prefix = Prefix( *aCurrentNode );
       
   207     TPtr8 pointer( aCurrentNode->ElementDataLC()->Des() );
       
   208     FormatElementStartL( prefix, pointer );
       
   209     while( !ready )
       
   210         {
       
   211         if( aCurrentNode->IsLeafNode() )
       
   212             FormatLeafElementL( aCurrentNode );
       
   213         TInt nodeCount = aCurrentNode->NodeCount();
       
   214         if( nodeCount > 0 )
       
   215             {
       
   216             CXdmDocumentNode* child = NULL;
       
   217             for( TInt i = 0;i < nodeCount;i++ )
       
   218                 {
       
   219                 child = aCurrentNode->ChileNode( i ); 
       
   220                 child->IsEmptyNode() && !child->IsLeafNode() ?
       
   221                                      FormatEmptyElementL( child ):
       
   222                                      FormatElementsL( child );
       
   223                 }
       
   224             ready = ETrue;
       
   225             }
       
   226         else ready = ETrue;
       
   227         }
       
   228     if( addNow )
       
   229         AppendXmlFragmentL();
       
   230     FormatElementEndL( prefix, FindElementName( pointer ) );        
       
   231     CleanupStack::PopAndDestroy(); //ElementDataLC()
       
   232     }
       
   233 
       
   234 // ---------------------------------------------------------
       
   235 // CXmlFormatter::AppendXmlFragmentL
       
   236 //
       
   237 // ---------------------------------------------------------
       
   238 //
       
   239 void CXmlFormatter::AppendXmlFragmentL()
       
   240     {
       
   241     iFormatBuffer->InsertL( iFormBufferPos, iDocumentFragment );
       
   242     iFormBufferPos = iFormBufferPos + iDocumentFragment.Length();
       
   243     iIsAppended = ETrue;        
       
   244     }
       
   245                              
       
   246 // ----------------------------------------------------------
       
   247 // CXmlFormatter::FormatLC
       
   248 // 
       
   249 // ----------------------------------------------------------
       
   250 //
       
   251 HBufC8* CXmlFormatter::FormatLC( TBool aIsWholeDocument,
       
   252                                  const CXdmDocument* aDocument,
       
   253                                  const CXdmDocumentNode* aRootNode )
       
   254     {
       
   255     iIsAppended = ETrue; 
       
   256     iFormatBuffer = CBufSeg::NewL( 128 );
       
   257     CXdmDocumentNode* root = CONST_CAST( CXdmDocumentNode*, aRootNode );
       
   258     if( aIsWholeDocument )
       
   259         {
       
   260         CXdmDocumentNode* element = NULL;
       
   261         iFormatBuffer->InsertL( iFormBufferPos, KXmldocumentStart );
       
   262         iFormBufferPos = iFormBufferPos + KXmldocumentStart().Length();
       
   263         FormatNamespaceDeclarationsL( *root, *aDocument );
       
   264         TInt count = root->NodeCount();
       
   265         if( count > 0 )
       
   266             {
       
   267             for( TInt i = 0;i < count;i++ )
       
   268                 {
       
   269                 element = root->ChileNode( i );
       
   270                 FormatElementsL( element );
       
   271                 }
       
   272             }
       
   273         }
       
   274     else FormatElementsL( root );
       
   275     if( aIsWholeDocument )
       
   276         {
       
   277         TPtrC8 name( aRootNode->EightBitNodeNameLC()->Des() );
       
   278         TPtrC8 prefix = aRootNode->Prefix();
       
   279         FormatElementEndL( prefix, name );
       
   280         CleanupStack::PopAndDestroy();  //EightBitNodeNameLC()
       
   281         }
       
   282     HBufC8* ret = BufferToStringLC();
       
   283     return ret;
       
   284     }
       
   285 
       
   286 // ----------------------------------------------------------
       
   287 // CXmlFormatter::FormatNamespaceDeclarationsL
       
   288 // 
       
   289 // ----------------------------------------------------------
       
   290 //
       
   291 void CXmlFormatter::FormatNamespaceDeclarationsL( const CXdmDocumentNode& aRootNode,
       
   292                                                   const MXdmNamespaceContainer& aContainer )
       
   293     {
       
   294     TPtrC8 name( aRootNode.EightBitNodeNameLC()->Des() );
       
   295     TPtrC8 prefix = Prefix( aRootNode );
       
   296     iFormatBuffer->InsertL( iFormBufferPos, KStartBracketString );
       
   297     iFormBufferPos = iFormBufferPos + KStartBracketString().Length();
       
   298     if( prefix.Length() > 0 )
       
   299         {
       
   300         iFormatBuffer->InsertL( iFormBufferPos, prefix );
       
   301         iFormBufferPos = iFormBufferPos + prefix.Length();
       
   302         iFormatBuffer->InsertL( iFormBufferPos, KPrefixSeparatorString );
       
   303         iFormBufferPos = iFormBufferPos + KPrefixSeparatorString().Length();
       
   304         }
       
   305     iFormatBuffer->InsertL( iFormBufferPos, name );
       
   306     iFormBufferPos = iFormBufferPos + name.Length();
       
   307     CleanupStack::PopAndDestroy();  //EightBitNodeNameLC()
       
   308     if( aContainer.Count() > 0 )
       
   309         {
       
   310         TPtrC8 uri( _L8( "" ) );
       
   311         TPtrC8 prefix( _L8( "" ) );
       
   312         for( TInt i = 0;i < aContainer.Count();i++ )
       
   313             {
       
   314             uri.Set( aContainer.Uri( i ) );
       
   315             prefix.Set( aContainer.Prefix( i ) );
       
   316             AppendNamespaceL( uri, prefix );
       
   317             }
       
   318         }
       
   319     iFormatBuffer->InsertL( iFormBufferPos, KEndBracketString );
       
   320     iFormBufferPos = iFormBufferPos + KEndBracketString().Length();
       
   321     iFormatBuffer->InsertL( iFormBufferPos, KNewlineString );
       
   322     iFormBufferPos = iFormBufferPos + KNewlineString().Length();
       
   323     }
       
   324 
       
   325 // ---------------------------------------------------------
       
   326 // CXmlFormatter::FormatLeafElementL
       
   327 //
       
   328 // ---------------------------------------------------------
       
   329 //
       
   330 void CXmlFormatter::FormatLeafElementL( CXdmDocumentNode* aCurrentNode )
       
   331     {
       
   332     TPtrC8 escape( aCurrentNode->EscapeLeafNodeContentLC()->Des() );
       
   333     iFormatBuffer->InsertL( iFormBufferPos, escape );
       
   334     iFormBufferPos = iFormBufferPos + escape.Length();
       
   335     CleanupStack::PopAndDestroy();  //EscapeLeafNodeContentLC()
       
   336     }
       
   337 
       
   338 // ---------------------------------------------------------
       
   339 // CXmlFormatter::FormatEmptyElementL
       
   340 //
       
   341 // ---------------------------------------------------------
       
   342 //
       
   343 void CXmlFormatter::FormatEmptyElementL( CXdmDocumentNode* aEmptyNode ) 
       
   344     {
       
   345     TPtr8 empty( aEmptyNode->ElementDataLC()->Des() );
       
   346     iFormatBuffer->InsertL( iFormBufferPos, KStartBracketString );
       
   347     iFormBufferPos = iFormBufferPos + KStartBracketString().Length();
       
   348     TPtrC8 prefix = Prefix( *aEmptyNode );
       
   349     if( prefix.Length() > 0 )
       
   350         {
       
   351         iFormatBuffer->InsertL( iFormBufferPos, prefix );
       
   352         iFormBufferPos = iFormBufferPos + prefix.Length();
       
   353         iFormatBuffer->InsertL( iFormBufferPos, KPrefixSeparatorString );
       
   354         iFormBufferPos = iFormBufferPos + KPrefixSeparatorString().Length();
       
   355         }
       
   356     iFormatBuffer->InsertL( iFormBufferPos, empty );
       
   357     iFormBufferPos = iFormBufferPos + empty.Length();
       
   358     CleanupStack::PopAndDestroy();  //ElementDataLC()
       
   359     iFormatBuffer->InsertL( iFormBufferPos, KEmptyElementString );
       
   360     iFormBufferPos = iFormBufferPos + TPtrC8( KEmptyElementString ).Length();
       
   361     }
       
   362     
       
   363 // ---------------------------------------------------------
       
   364 // CXmlFormatter::FormatElementStartL
       
   365 //
       
   366 // ---------------------------------------------------------
       
   367 //
       
   368 void CXmlFormatter::FormatElementStartL( const TDesC8& aPrefix,
       
   369                                          const TDesC8& aElementData ) 
       
   370     {
       
   371     iFormatBuffer->InsertL( iFormBufferPos, KStartBracketString );
       
   372     iFormBufferPos = iFormBufferPos + KStartBracketString().Length();
       
   373     if( aPrefix.Length() > 0 )
       
   374         {
       
   375         iFormatBuffer->InsertL( iFormBufferPos, aPrefix );
       
   376         iFormBufferPos = iFormBufferPos + aPrefix.Length();
       
   377         iFormatBuffer->InsertL( iFormBufferPos, KPrefixSeparatorString );
       
   378         iFormBufferPos = iFormBufferPos + KPrefixSeparatorString().Length();
       
   379         }
       
   380     iFormatBuffer->InsertL( iFormBufferPos, aElementData );
       
   381     iFormBufferPos = iFormBufferPos + aElementData.Length();
       
   382     iFormatBuffer->InsertL( iFormBufferPos, KEndBracketString );
       
   383     iFormBufferPos = iFormBufferPos + KEndBracketString().Length();
       
   384     }
       
   385 
       
   386 // ---------------------------------------------------------
       
   387 // CXmlFormatter::FormatElementEndL
       
   388 //
       
   389 // ---------------------------------------------------------
       
   390 //
       
   391 void CXmlFormatter::FormatElementEndL( const TDesC8& aPrefix,
       
   392                                        const TDesC8& aElementName ) 
       
   393     {
       
   394     iFormatBuffer->InsertL( iFormBufferPos, KStartBracketString );
       
   395     iFormBufferPos = iFormBufferPos + KStartBracketString().Length();
       
   396     iFormatBuffer->InsertL( iFormBufferPos, KSlashString );
       
   397     iFormBufferPos = iFormBufferPos + KSlashString().Length();
       
   398     if( aPrefix.Length() > 0 )
       
   399         {
       
   400         iFormatBuffer->InsertL( iFormBufferPos, aPrefix );
       
   401         iFormBufferPos = iFormBufferPos + aPrefix.Length();
       
   402         iFormatBuffer->InsertL( iFormBufferPos, KPrefixSeparatorString );
       
   403         iFormBufferPos = iFormBufferPos + KPrefixSeparatorString().Length();
       
   404         }
       
   405     iFormatBuffer->InsertL( iFormBufferPos, aElementName );
       
   406     iFormBufferPos = iFormBufferPos + aElementName.Length();
       
   407     iFormatBuffer->InsertL( iFormBufferPos, KEndBracketString );
       
   408     iFormBufferPos = iFormBufferPos + KEndBracketString().Length();
       
   409     }
       
   410 
       
   411 // ---------------------------------------------------------
       
   412 // CXmlFormatter::AppendNamespaceL
       
   413 //
       
   414 // ---------------------------------------------------------
       
   415 //
       
   416 void CXmlFormatter::AppendNamespaceL( const TDesC8& aUri, const TDesC8& aPrefix )
       
   417     {
       
   418     iFormatBuffer->InsertL( iFormBufferPos, KXmlNamespaceString );
       
   419     iFormBufferPos = iFormBufferPos + KXmlNamespaceString().Length();
       
   420     if( aPrefix.Length() > 0 )  //Not the default namespace
       
   421         {   
       
   422         iFormatBuffer->InsertL( iFormBufferPos, KPrefixSeparatorString );
       
   423         iFormBufferPos = iFormBufferPos + KPrefixSeparatorString().Length();
       
   424         iFormatBuffer->InsertL( iFormBufferPos, aPrefix );
       
   425         iFormBufferPos = iFormBufferPos + aPrefix.Length();
       
   426         }
       
   427     iFormatBuffer->InsertL( iFormBufferPos, KEquelsSignString );
       
   428     iFormBufferPos = iFormBufferPos + KEquelsSignString().Length();
       
   429     iFormatBuffer->InsertL( iFormBufferPos, KQuotationSignString );
       
   430     iFormBufferPos = iFormBufferPos + KQuotationSignString().Length();
       
   431     iFormatBuffer->InsertL( iFormBufferPos, aUri );
       
   432     iFormBufferPos = iFormBufferPos + aUri.Length();
       
   433     iFormatBuffer->InsertL( iFormBufferPos, KQuotationSignString );
       
   434     iFormBufferPos = iFormBufferPos + KQuotationSignString().Length();
       
   435     }
       
   436     
       
   437 
       
   438 // End of File