vpnengine/utlxml/src/XmlWriter.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2003 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 CXmlWriter.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <eikenv.h>
       
    21 #include <e32std.h>
       
    22 #include <e32des8.h>
       
    23 #include "XwImpl.h"
       
    24 
       
    25 const TInt KExtraPunctuation = 13;  // CDATA section has 12 extra characters
       
    26 const TInt KBufSizeIncrement = 1024;
       
    27 const TInt KBufInitSize = 2048;
       
    28 
       
    29 //
       
    30 // Common literal text
       
    31 //
       
    32 _LIT8(KPrologStart, "<?xml");
       
    33 _LIT8(KPrologEnd, "?>");
       
    34 _LIT8(KVersion, "version");
       
    35 _LIT8(K10, "1.0");
       
    36 _LIT8(KEncoding, "encoding");
       
    37 _LIT8(KIso88591, "ISO-8859-1");
       
    38 _LIT8(KOpenBracket, "<");
       
    39 _LIT8(KCloseBracket, ">");
       
    40 _LIT8(KStartEndTag, "</");
       
    41 _LIT8(KCloseTag, "/>");
       
    42 _LIT8(KSpace, " ");
       
    43 _LIT8(KEquals, "=");
       
    44 _LIT8(KSingleQuote, "\'");
       
    45 //_LIT8(KDoubleQuote, "\"");
       
    46 _LIT8(KCDStart, "<![CDATA[");
       
    47 _LIT8(KCDEnd, "]]>");
       
    48 
       
    49 _LIT8(KEntityLowerThan, "&lt;");           // <
       
    50 _LIT8(KEntityGreaterThan, "&gt;");         // >
       
    51 _LIT8(KEntityAmpersand, "&amp;");          // &
       
    52 _LIT8(KEntityApostrophe, "&apos;");        // '
       
    53 _LIT8(KEntityQuotation, "&quot;");         // "
       
    54 
       
    55 
       
    56 ////////////////////////////////////////////////////////////////////////////////////////
       
    57 // 
       
    58 ////////////////////////////////////////////////////////////////////////////////////////
       
    59 
       
    60 CXmlWriter::CXmlWriter() 
       
    61 : iBufferPtr(NULL, 0), iTempBufferPtr(NULL, 0)
       
    62     {
       
    63     }
       
    64 
       
    65 
       
    66 ////////////////////////////////////////////////////////////////////////////////////////
       
    67 // 
       
    68 ////////////////////////////////////////////////////////////////////////////////////////
       
    69 // NewLC with two stage construction 
       
    70 EXPORT_C CXmlWriter* CXmlWriter::NewLC()
       
    71     {
       
    72     // get new, leave if can't
       
    73     CXmlWriter* self = new (ELeave) CXmlWriter();
       
    74     // push onto cleanup stack in case self->ConstructL leaves 
       
    75     CleanupStack::PushL(self);
       
    76     // complete construction with second phase constructor 
       
    77     self->ConstructL();
       
    78     return self;
       
    79     }
       
    80 
       
    81 ////////////////////////////////////////////////////////////////////////////////////////
       
    82 // 
       
    83 ////////////////////////////////////////////////////////////////////////////////////////
       
    84 EXPORT_C CXmlWriter* CXmlWriter::NewL() 
       
    85     {
       
    86     CXmlWriter* self=NewLC();
       
    87     CleanupStack::Pop();
       
    88     return self;
       
    89     }
       
    90 
       
    91 ////////////////////////////////////////////////////////////////////////////////////////
       
    92 // 
       
    93 ////////////////////////////////////////////////////////////////////////////////////////
       
    94 void CXmlWriter::ConstructL() 
       
    95     {
       
    96     iBuffer = HBufC8::NewMaxL(KBufInitSize);
       
    97     iBufferPtr.Set(iBuffer->Des());
       
    98     iBufferPtr.SetLength(0);  // Otherwise the Length is set to KBufInitSize
       
    99     iTempBuffer = HBufC8::NewMaxL(KBufInitSize);
       
   100     iTempBufferPtr.Set(iTempBuffer->Des());
       
   101     iTempBufferPtr.SetLength(0);  // Otherwise the Length is set to KBufInitSize
       
   102     }
       
   103 
       
   104 ////////////////////////////////////////////////////////////////////////////////////////
       
   105 // 
       
   106 ////////////////////////////////////////////////////////////////////////////////////////
       
   107 CXmlWriter::~CXmlWriter()
       
   108     {
       
   109     delete (iBuffer);
       
   110     delete (iTempBuffer);
       
   111     }
       
   112 
       
   113 ////////////////////////////////////////////////////////////////////////////////////////
       
   114 // 
       
   115 ////////////////////////////////////////////////////////////////////////////////////////
       
   116 void CXmlWriter::CheckSpaceL(const TInt aSpace)
       
   117     {
       
   118     // Make sure that we have enough space for the new text
       
   119     TInt spaceLeft = iBufferPtr.MaxLength() - iBufferPtr.Length()
       
   120                     - KExtraPunctuation;
       
   121     
       
   122     if (aSpace > spaceLeft)
       
   123         {
       
   124         // Allocate enough space for the new text + some additional 
       
   125         // free space so that allocations are not too frequent
       
   126         
       
   127         TInt newMaxLength = iBufferPtr.MaxLength() + aSpace + KBufSizeIncrement;
       
   128         
       
   129         iBuffer = iBuffer->ReAllocL(newMaxLength);
       
   130         iBufferPtr.Set(iBuffer->Des());
       
   131         }
       
   132     }
       
   133 
       
   134 ////////////////////////////////////////////////////////////////////////////////////////
       
   135 // 
       
   136 ////////////////////////////////////////////////////////////////////////////////////////
       
   137 void CXmlWriter::CheckSpaceL(const TDesC8& aText)
       
   138     {
       
   139     // Make sure that we have enough space for the new text
       
   140     TInt spaceLeft = iBufferPtr.MaxLength() - iBufferPtr.Length()
       
   141                     - KExtraPunctuation;
       
   142     
       
   143     if (aText.Length() > spaceLeft)
       
   144         {
       
   145         // Allocate enough space for the new text + some additional 
       
   146         // free space so that allocations are not too frequent
       
   147 
       
   148         TInt newMaxLength = iBufferPtr.MaxLength() + aText.Length() 
       
   149                         + KBufSizeIncrement;
       
   150 
       
   151         iBuffer = iBuffer->ReAllocL(newMaxLength);
       
   152         iBufferPtr.Set(iBuffer->Des());
       
   153         }
       
   154     }
       
   155 
       
   156 ////////////////////////////////////////////////////////////////////////////////////////
       
   157 // 
       
   158 ////////////////////////////////////////////////////////////////////////////////////////
       
   159 void CXmlWriter::CheckSpaceL(const TDesC8& aText1, const TDesC8& aText2)
       
   160     {
       
   161     // Make sure that we have enough space for the new text
       
   162     TInt spaceLeft = iBufferPtr.MaxLength() - iBufferPtr.Length()
       
   163                     - KExtraPunctuation;
       
   164     
       
   165     if ((aText1.Length() + aText2.Length()) > spaceLeft)
       
   166         {
       
   167         // Allocate enough space for the new text + some additional 
       
   168         // free space so that allocations are not too frequent
       
   169         
       
   170         TInt newMaxLength = iBufferPtr.MaxLength() + aText1.Length() 
       
   171                             + aText2.Length() + KBufSizeIncrement;
       
   172         
       
   173         iBuffer = iBuffer->ReAllocL(newMaxLength);
       
   174         iBufferPtr.Set(iBuffer->Des());
       
   175         }
       
   176     }
       
   177 
       
   178 ////////////////////////////////////////////////////////////////////////////////////////
       
   179 // 
       
   180 ////////////////////////////////////////////////////////////////////////////////////////
       
   181 void CXmlWriter::CheckTempBufSpaceL(const TInt aSpace)
       
   182     {
       
   183     // Make sure that we have enough space for the new text
       
   184     TInt spaceLeft = iTempBufferPtr.MaxLength() - iTempBufferPtr.Length();
       
   185     
       
   186     if (aSpace > spaceLeft)
       
   187         {
       
   188         // Allocate enough space for the new text + some additional 
       
   189         // free space so that allocations are not too frequent
       
   190         
       
   191         TInt newMaxLength = iTempBufferPtr.MaxLength() + aSpace + KBufSizeIncrement;
       
   192         
       
   193         iTempBuffer = iTempBuffer->ReAllocL(newMaxLength);
       
   194         iTempBufferPtr.Set(iTempBuffer->Des());
       
   195         }
       
   196     }
       
   197 
       
   198 
       
   199 ////////////////////////////////////////////////////////////////////////////////////////
       
   200 // 
       
   201 ////////////////////////////////////////////////////////////////////////////////////////
       
   202 void CXmlWriter::InsertEntitiesL()
       
   203     {
       
   204     TInt i = 0;
       
   205     
       
   206     while ( i < iTempBufferPtr.Length() )
       
   207         {
       
   208         if ( iTempBufferPtr[i] == '<' )
       
   209             {
       
   210             CheckTempBufSpaceL(KEntityLowerThan().Length());
       
   211             iTempBufferPtr.Replace(i, 1, KEntityLowerThan);
       
   212             i += KEntityLowerThan().Length();
       
   213             }
       
   214         else if ( iTempBufferPtr[i] == '>' )
       
   215             {
       
   216             CheckTempBufSpaceL(KEntityGreaterThan().Length());
       
   217             iTempBufferPtr.Replace(i, 1, KEntityGreaterThan);
       
   218             i += KEntityGreaterThan().Length();
       
   219             }
       
   220         else if ( iTempBufferPtr[i] == '&' )
       
   221             {
       
   222             CheckTempBufSpaceL(KEntityAmpersand().Length());
       
   223             iTempBufferPtr.Replace(i, 1, KEntityAmpersand);
       
   224             i += KEntityAmpersand().Length();
       
   225             }
       
   226         else if ( iTempBufferPtr[i] == '\'' )
       
   227             {
       
   228             CheckTempBufSpaceL(KEntityApostrophe().Length());
       
   229             iTempBufferPtr.Replace(i, 1, KEntityApostrophe);
       
   230             i += KEntityApostrophe().Length();
       
   231             }
       
   232         else if ( iTempBufferPtr[i] == '\"' )
       
   233             {
       
   234             CheckTempBufSpaceL(KEntityQuotation().Length());
       
   235             iTempBufferPtr.Replace(i, 1, KEntityQuotation);
       
   236             i += KEntityQuotation().Length();
       
   237             }
       
   238         else
       
   239             {
       
   240             i++;
       
   241             }
       
   242         }
       
   243     }
       
   244 
       
   245 ////////////////////////////////////////////////////////////////////////////////////////
       
   246 // 
       
   247 ////////////////////////////////////////////////////////////////////////////////////////
       
   248 EXPORT_C void CXmlWriter::AddPrologL()
       
   249     {
       
   250     CheckSpaceL(KPrologStart().Length() + 2*KSpace().Length() + KVersion().Length() + 
       
   251                2*KEquals().Length() + 4*KSingleQuote().Length() + K10().Length() +
       
   252                KEncoding().Length() + KIso88591().Length() + KPrologEnd().Length());
       
   253 
       
   254     iBufferPtr.Append(KPrologStart);  // <?xml
       
   255     iBufferPtr.Append(KSpace);
       
   256     iBufferPtr.Append(KVersion);  // version
       
   257     iBufferPtr.Append(KEquals);   // =
       
   258     iBufferPtr.Append(KSingleQuote);   // '
       
   259     iBufferPtr.Append(K10);  // 1.0
       
   260     iBufferPtr.Append(KSingleQuote);   // '
       
   261     iBufferPtr.Append(KSpace);
       
   262     iBufferPtr.Append(KEncoding);  // encoding
       
   263     iBufferPtr.Append(KEquals);  // =
       
   264     iBufferPtr.Append(KSingleQuote);  // '
       
   265     iBufferPtr.Append(KIso88591);  // ISO-8859-1
       
   266     iBufferPtr.Append(KSingleQuote);  // '
       
   267     iBufferPtr.Append(KPrologEnd);  // ?>
       
   268     }
       
   269 
       
   270 
       
   271 ////////////////////////////////////////////////////////////////////////////////////////
       
   272 // 
       
   273 ////////////////////////////////////////////////////////////////////////////////////////
       
   274 EXPORT_C void CXmlWriter::AddPrologL(const TDesC8& aVersionNum)
       
   275     {
       
   276     CheckSpaceL(KPrologStart().Length() + 2*KSpace().Length() + KVersion().Length() + 
       
   277                2*KEquals().Length() + 4*KSingleQuote().Length() + aVersionNum.Length() +
       
   278                KEncoding().Length() + KIso88591().Length() + KPrologEnd().Length());
       
   279 
       
   280     iBufferPtr.Append(KPrologStart);  // <?xml
       
   281     iBufferPtr.Append(KSpace);
       
   282     iBufferPtr.Append(KVersion);  // version
       
   283     iBufferPtr.Append(KEquals);   // =
       
   284     iBufferPtr.Append(KSingleQuote);   // '
       
   285     iBufferPtr.Append(aVersionNum);
       
   286     iBufferPtr.Append(KSingleQuote);   // '
       
   287     iBufferPtr.Append(KSpace);
       
   288     iBufferPtr.Append(KEncoding);  // encoding
       
   289     iBufferPtr.Append(KEquals);  // =
       
   290     iBufferPtr.Append(KSingleQuote);  // '
       
   291     iBufferPtr.Append(KIso88591);  // ISO-8859-1
       
   292     iBufferPtr.Append(KSingleQuote);  // '
       
   293     iBufferPtr.Append(KPrologEnd);  // ?>
       
   294     }
       
   295 
       
   296 ////////////////////////////////////////////////////////////////////////////////////////
       
   297 // 
       
   298 ////////////////////////////////////////////////////////////////////////////////////////
       
   299 EXPORT_C void CXmlWriter::AddStartTagL(const TDesC8& aName)
       
   300     {
       
   301     CheckSpaceL(aName);
       
   302 
       
   303     iBufferPtr.Append(KOpenBracket);  // <
       
   304     iBufferPtr.Append(aName);
       
   305     iBufferPtr.Append(KCloseBracket);  // >
       
   306     }
       
   307 
       
   308 ////////////////////////////////////////////////////////////////////////////////////////
       
   309 // 
       
   310 ////////////////////////////////////////////////////////////////////////////////////////
       
   311 EXPORT_C void CXmlWriter::OpenStartTagL(const TDesC8& aName)
       
   312     {
       
   313     CheckSpaceL(aName);
       
   314 
       
   315     iBufferPtr.Append(KOpenBracket);  // <
       
   316     iBufferPtr.Append(aName);
       
   317     }
       
   318 
       
   319 ////////////////////////////////////////////////////////////////////////////////////////
       
   320 // 
       
   321 ////////////////////////////////////////////////////////////////////////////////////////
       
   322 EXPORT_C void CXmlWriter::AddAttributeL(const TDesC8& aName, const TDesC8& aValue)
       
   323     {
       
   324     CheckTempBufSpaceL(aName.Length() + aValue.Length());
       
   325 
       
   326     iTempBufferPtr = aValue;
       
   327 
       
   328     InsertEntitiesL();
       
   329 
       
   330     CheckSpaceL(aName, iTempBufferPtr);
       
   331 
       
   332     iBufferPtr.Append(KSpace);  // 
       
   333     iBufferPtr.Append(aName);
       
   334     iBufferPtr.Append(KEquals);  // =
       
   335     iBufferPtr.Append(KSingleQuote);  // '
       
   336     iBufferPtr.Append(iTempBufferPtr);
       
   337     iBufferPtr.Append(KSingleQuote);  // '
       
   338     }
       
   339 
       
   340 ////////////////////////////////////////////////////////////////////////////////////////
       
   341 // 
       
   342 ////////////////////////////////////////////////////////////////////////////////////////
       
   343 EXPORT_C void CXmlWriter::CloseStartTagL(TBool aEndElement)
       
   344     {
       
   345     CheckSpaceL(KCloseTag);
       
   346     
       
   347     if(aEndElement)
       
   348         {
       
   349         iBufferPtr.Append(KCloseTag);  // />
       
   350         }
       
   351     else
       
   352         {
       
   353         iBufferPtr.Append(KCloseBracket);
       
   354         }
       
   355     }
       
   356 
       
   357 ////////////////////////////////////////////////////////////////////////////////////////
       
   358 // 
       
   359 ////////////////////////////////////////////////////////////////////////////////////////
       
   360 EXPORT_C void CXmlWriter::AddTextL(const TDesC8& aText)
       
   361     {
       
   362     CheckTempBufSpaceL(aText.Length());
       
   363 
       
   364     iTempBufferPtr = aText;
       
   365 
       
   366     InsertEntitiesL();
       
   367 
       
   368     CheckSpaceL(iTempBufferPtr);
       
   369 
       
   370     iBufferPtr.Append(iTempBufferPtr);
       
   371     }
       
   372 
       
   373 ////////////////////////////////////////////////////////////////////////////////////////
       
   374 // 
       
   375 ////////////////////////////////////////////////////////////////////////////////////////
       
   376 EXPORT_C void CXmlWriter::AddXmlTextL(const TDesC8& aText)
       
   377     {
       
   378     CheckSpaceL(aText);
       
   379 
       
   380     iBufferPtr.Append(aText);
       
   381     }
       
   382 
       
   383 ////////////////////////////////////////////////////////////////////////////////////////
       
   384 // 
       
   385 ////////////////////////////////////////////////////////////////////////////////////////
       
   386 EXPORT_C void CXmlWriter::AddEndTagL(const TDesC8& aName)
       
   387     {
       
   388     CheckSpaceL(aName);
       
   389     
       
   390     iBufferPtr.Append(KStartEndTag);  // </
       
   391     iBufferPtr.Append(aName);
       
   392     iBufferPtr.Append(KCloseBracket);  // >
       
   393     }
       
   394 
       
   395 ////////////////////////////////////////////////////////////////////////////////////////
       
   396 // 
       
   397 ////////////////////////////////////////////////////////////////////////////////////////
       
   398 EXPORT_C void CXmlWriter::AddCdataSectL(const TDesC8& aText)
       
   399     {
       
   400     CheckSpaceL(aText);
       
   401     
       
   402     iBufferPtr.Append(KCDStart);  // <![CDATA[
       
   403     iBufferPtr.Append(aText);
       
   404     iBufferPtr.Append(KCDEnd);  // ]]>
       
   405     }
       
   406 
       
   407 ////////////////////////////////////////////////////////////////////////////////////////
       
   408 // 
       
   409 ////////////////////////////////////////////////////////////////////////////////////////
       
   410 EXPORT_C void CXmlWriter::InsertTextL(TInt aPos, const TDesC8& aText)
       
   411     {
       
   412     iTempBufferPtr = aText;
       
   413 
       
   414     InsertEntitiesL();
       
   415 
       
   416     CheckSpaceL(iTempBufferPtr);
       
   417 
       
   418     iBufferPtr.Insert(aPos, iTempBufferPtr);  // aPos is an offset; a zero value 
       
   419                                               // refers to the leftmost data 
       
   420                                               // position
       
   421     }
       
   422 
       
   423 ////////////////////////////////////////////////////////////////////////////////////////
       
   424 // 
       
   425 ////////////////////////////////////////////////////////////////////////////////////////
       
   426 EXPORT_C void CXmlWriter::InsertXmlTextL(TInt aPos, const TDesC8& aText)
       
   427     {
       
   428     CheckSpaceL(aText);
       
   429     
       
   430     iBufferPtr.Insert(aPos, aText);  // aPos is an offset; a zero value 
       
   431                                      // refers to the leftmost data 
       
   432                                      // position
       
   433     }
       
   434 
       
   435 ////////////////////////////////////////////////////////////////////////////////////////
       
   436 // 
       
   437 ////////////////////////////////////////////////////////////////////////////////////////
       
   438 EXPORT_C TInt CXmlWriter::Length()
       
   439     {
       
   440     return iBufferPtr.Length();
       
   441     }
       
   442 
       
   443 ////////////////////////////////////////////////////////////////////////////////////////
       
   444 // 
       
   445 ////////////////////////////////////////////////////////////////////////////////////////
       
   446 EXPORT_C TPtrC8 CXmlWriter::DocPart(TInt aStartPos, TInt aEndPos)
       
   447     {
       
   448     iDoc.Set(iBufferPtr.Mid(aStartPos, (aEndPos - aStartPos + 1)));
       
   449     return iDoc;
       
   450     }
       
   451 
       
   452 ////////////////////////////////////////////////////////////////////////////////////////
       
   453 // 
       
   454 ////////////////////////////////////////////////////////////////////////////////////////
       
   455 EXPORT_C void CXmlWriter::Reset()
       
   456     {
       
   457     iBufferPtr.Zero();
       
   458     iDoc.Set(KNullDesC8);
       
   459     }
       
   460 
       
   461 ////////////////////////////////////////////////////////////////////////////////////////
       
   462 // Dll entry point
       
   463 ////////////////////////////////////////////////////////////////////////////////////////