syncmlfw/common/wbxml/src/WBXMLGenerator.cpp
changeset 0 b497e44ab2fc
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002 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:  Generic WBXML generator class implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // ------------------------------------------------------------------------------------------------
       
    20 // Includes
       
    21 // ------------------------------------------------------------------------------------------------
       
    22 #include "WBXMLGenerator.h"
       
    23 #include "WBXMLDefs.h"
       
    24 #include "XMLStaticWorkspace.h"
       
    25 #include "XMLDynamicWorkspace.h"
       
    26 #include "WBXMLGeneratorError.h"
       
    27 
       
    28 // ------------------------------------------------------------------------------------------------
       
    29 // CWBXMLGenerator
       
    30 // ------------------------------------------------------------------------------------------------
       
    31 EXPORT_C TPtrC8 CWBXMLGenerator::Document()
       
    32 	{
       
    33 	return Workspace()->Buffer();
       
    34 	}
       
    35 
       
    36 // ------------------------------------------------------------------------------------------------
       
    37 EXPORT_C void CWBXMLGenerator::Reset()
       
    38 	{
       
    39 	Workspace()->Reset();
       
    40 	}
       
    41 
       
    42 // ------------------------------------------------------------------------------------------------
       
    43 EXPORT_C void CWBXMLGenerator::CreateStaticWorkspaceL( TInt aBufferSize )
       
    44 	{
       
    45 	FreeWorkspace();
       
    46 	iWorkspace = CXMLStaticWorkspace::NewL(aBufferSize);
       
    47 	iOwnsWorkspace = ETrue;
       
    48 	}
       
    49 
       
    50 // ------------------------------------------------------------------------------------------------
       
    51 EXPORT_C void CWBXMLGenerator::CreateDynamicWorkspaceL()
       
    52 	{
       
    53 	FreeWorkspace();
       
    54 	iWorkspace = CXMLDynamicWorkspace::NewL();
       
    55 	iOwnsWorkspace = ETrue;
       
    56 	}
       
    57 
       
    58 // ------------------------------------------------------------------------------------------------
       
    59 EXPORT_C CXMLWorkspace* CWBXMLGenerator::Workspace() const
       
    60 	{
       
    61 	return iWorkspace;
       
    62 	}
       
    63 
       
    64 // ------------------------------------------------------------------------------------------------
       
    65 EXPORT_C void CWBXMLGenerator::SetWorkspace( CXMLWorkspace* aBuffer, TBool aTakeOwnership )
       
    66 	{	
       
    67 	FreeWorkspace();
       
    68 	iOwnsWorkspace = aTakeOwnership;
       
    69 	iWorkspace = aBuffer;
       
    70 	}
       
    71 
       
    72 // ------------------------------------------------------------------------------------------------
       
    73 void CWBXMLGenerator::FreeWorkspace()
       
    74 	{
       
    75 	if( iOwnsWorkspace )
       
    76 		{
       
    77 		delete iWorkspace;
       
    78 		iWorkspace = 0;
       
    79 		}
       
    80 	}
       
    81 
       
    82 // ------------------------------------------------------------------------------------------------
       
    83 EXPORT_C CWBXMLGenerator::CWBXMLGenerator()
       
    84 	{
       
    85 	}
       
    86 
       
    87 // ------------------------------------------------------------------------------------------------
       
    88 EXPORT_C CWBXMLGenerator::~CWBXMLGenerator()
       
    89 	{
       
    90 	FreeWorkspace();
       
    91 	}
       
    92 
       
    93 // ------------------------------------------------------------------------------------------------
       
    94 EXPORT_C void CWBXMLGenerator::BeginDocumentL( TUint8 aVersion, TInt32 aPublicId, TUint32 aCharset, const TDesC8& aStringTbl )
       
    95 	{
       
    96 	Workspace()->WriteL(aVersion);
       
    97 
       
    98 	if( aPublicId <= 0 )
       
    99 		{
       
   100 		Workspace()->WriteL(0);
       
   101 		WriteMUint32L(-aPublicId);
       
   102 		}
       
   103 	else
       
   104 		{
       
   105 		WriteMUint32L(aPublicId);
       
   106 		}
       
   107 	WriteMUint32L(aCharset);
       
   108 	WriteMUint32L(aStringTbl.Size());
       
   109 	Workspace()->WriteL(aStringTbl);
       
   110 	}
       
   111 
       
   112 // ------------------------------------------------------------------------------------------------
       
   113 EXPORT_C void CWBXMLGenerator::BeginElementL( TUint8 aElement, TBool aHasContent, TBool aHasAttributes )
       
   114 	{
       
   115 	if( aHasAttributes )
       
   116 		{
       
   117 		aElement |= KWBXMLHasAttributes;
       
   118 		}
       
   119 	if( aHasContent )
       
   120 		{
       
   121 		aElement |= KWBXMLHasContent;
       
   122 		}
       
   123 	WriteMUint32L(aElement);
       
   124 	}
       
   125 
       
   126 // ------------------------------------------------------------------------------------------------
       
   127 EXPORT_C void CWBXMLGenerator::EndElementL()
       
   128 	{
       
   129 	Workspace()->WriteL(END);
       
   130 	}
       
   131 
       
   132 // ------------------------------------------------------------------------------------------------
       
   133 EXPORT_C void CWBXMLGenerator::AddElementL( TUint8 aElement, const TDesC8& aContent, const TWBXMLContentFormat aFormat )
       
   134 	{
       
   135 	BeginElementL(aElement, ETrue);
       
   136 	if( aFormat == EWBXMLContentFormatOpaque )
       
   137 		{
       
   138 		WriteOpaqueDataL(aContent);
       
   139 		}
       
   140 	else
       
   141 		{
       
   142 		WriteInlineStringL(aContent);
       
   143 		}
       
   144 	EndElementL();
       
   145 	}
       
   146 
       
   147 // ------------------------------------------------------------------------------------------------
       
   148 EXPORT_C void CWBXMLGenerator::AddElementL( TUint8 /*aElement*/, const TDesC8& /*aContent*/, const CWBXMLAttributes& /*aAttributes*/, const TWBXMLContentFormat /*aFormat*/ )
       
   149 	{
       
   150 	}
       
   151 
       
   152 // ------------------------------------------------------------------------------------------------
       
   153 EXPORT_C void CWBXMLGenerator::AddEntityL( TUint32 aEntity )
       
   154 	{
       
   155 	Workspace()->WriteL(ENTITY);
       
   156 	WriteMUint32L(aEntity);
       
   157 	}
       
   158 
       
   159 // ------------------------------------------------------------------------------------------------
       
   160 EXPORT_C void CWBXMLGenerator::AddExt_IL( TInt aExtNum, const TDesC8& aContent )
       
   161 	{
       
   162 	Workspace()->WriteL(TUint8(EXT_I_0 + aExtNum));
       
   163 	WriteInlineStringL(aContent);
       
   164 	}
       
   165 
       
   166 // ------------------------------------------------------------------------------------------------
       
   167 EXPORT_C void CWBXMLGenerator::AddExt_TL( TInt aExtNum, TUint32 aContent )
       
   168 	{
       
   169 	Workspace()->WriteL(TUint8(EXT_T_0 + aExtNum));
       
   170 	WriteMUint32L(aContent);
       
   171 	}
       
   172 
       
   173 // ------------------------------------------------------------------------------------------------
       
   174 EXPORT_C void CWBXMLGenerator::AddExtL( TInt aExtNum )
       
   175 	{
       
   176 	Workspace()->WriteL(TUint8(EXT_0 + aExtNum));
       
   177 	}
       
   178 
       
   179 // ------------------------------------------------------------------------------------------------
       
   180 EXPORT_C void CWBXMLGenerator::SwitchCodePageL( TUint8 aNewCodePage )
       
   181 	{
       
   182 	Workspace()->WriteL(SWITCH_PAGE);
       
   183 	Workspace()->WriteL(aNewCodePage);
       
   184 	}
       
   185 
       
   186 // ------------------------------------------------------------------------------------------------
       
   187 EXPORT_C TInt CWBXMLGenerator::HandleResult( TInt aResult, TInt aTreshold )
       
   188 	{
       
   189 	switch( aResult )
       
   190 		{
       
   191 		case KErrNone:
       
   192 			if( Workspace()->FreeSize() < aTreshold )
       
   193 				{				
       
   194 				Workspace()->Rollback();
       
   195 				return KWBXMLGeneratorBufferFull;
       
   196 				}
       
   197 			Workspace()->Commit();
       
   198 			return KWBXMLGeneratorOk;
       
   199 
       
   200 		case KErrTooBig:
       
   201 			Workspace()->Rollback();
       
   202 			return KWBXMLGeneratorBufferFull;
       
   203 
       
   204 		default:
       
   205 			return KErrGeneral;
       
   206 		}
       
   207 	}
       
   208 
       
   209 // ------------------------------------------------------------------------------------------------
       
   210 void CWBXMLGenerator::WriteMUint32L( TUint32 aValue )
       
   211 	{
       
   212 	TUint8 temp[5];
       
   213 	TInt i(4);
       
   214 	
       
   215 	temp[i--] = TUint8(aValue & 0x7F);
       
   216 	aValue >>= 7;
       
   217 	while( aValue > 0 )
       
   218 		{
       
   219 		temp[i--] = TUint8((aValue & 0x7F) | 0x80);
       
   220 		aValue >>= 7;
       
   221 		}
       
   222 			
       
   223 	while( i < 4 )
       
   224 		{
       
   225 		Workspace()->WriteL(temp[++i]);
       
   226 		}
       
   227 	}
       
   228 
       
   229 // ------------------------------------------------------------------------------------------------
       
   230 void CWBXMLGenerator::WriteOpaqueDataL( const TDesC8& aData )
       
   231 	{
       
   232 	Workspace()->WriteL(OPAQUE);
       
   233 	WriteMUint32L(aData.Size());
       
   234 	Workspace()->WriteL(aData);
       
   235 	}
       
   236 
       
   237 // ------------------------------------------------------------------------------------------------
       
   238 void CWBXMLGenerator::WriteInlineStringL( const TDesC8& aData )
       
   239 	{
       
   240 	Workspace()->WriteL(STR_I);
       
   241 	Workspace()->WriteL(aData);
       
   242 	Workspace()->WriteL(0);
       
   243 	}
       
   244