xml/xmlfw/src/xmlframework/dictionarycodepage.cpp
changeset 0 e35f40988205
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32base.h>
       
    17 #include <e32std.h>
       
    18 
       
    19 #include <xml/xmlframeworkerrors.h>
       
    20 #include "XmlFrameworkPanics.h"
       
    21 #include <xml/plugins/dictionarycodepage.h>
       
    22 
       
    23 using namespace Xml;
       
    24 
       
    25 const TInt KTokenGranularity=30;
       
    26 
       
    27 EXPORT_C CDictionaryCodePage* CDictionaryCodePage::NewL(const TStringTable* aElementTable, 
       
    28 											  			const TStringTable* aAttributeTable, 
       
    29 											  			const TStringTable* aValueTable, 
       
    30 											  			TUint8 aCodePage)
       
    31 
       
    32 	{
       
    33 	return new(ELeave) CDictionaryCodePage(aElementTable, 
       
    34 										   aAttributeTable, 
       
    35 										   aValueTable, 
       
    36 										   aCodePage);
       
    37 	}
       
    38 	
       
    39 	
       
    40 EXPORT_C CDictionaryCodePage::~CDictionaryCodePage()
       
    41 /**
       
    42 Destructor.
       
    43 
       
    44 @post				This object may be allowed to go out of scope.
       
    45 
       
    46 */
       
    47 	{
       
    48 	iElementStringPoolIndexToToken.Close();
       
    49 	iElementTokenToStringPoolIndex.Close();	
       
    50 	iAttributeStringPoolIndexToToken.Close();
       
    51 	iAttributeTokenToStringPoolIndex.Close();	
       
    52 	iValueStringPoolIndexToToken.Close();
       
    53 	iValueTokenToStringPoolIndex.Close();	
       
    54 	}
       
    55 
       
    56 
       
    57 CDictionaryCodePage::CDictionaryCodePage(const TStringTable* aElementTable, 
       
    58 										const TStringTable* aAttributeTable, 
       
    59 										const TStringTable* aValueTable, 
       
    60 										TUint8 aCodePage) 
       
    61 /**
       
    62 Constructor.
       
    63 
       
    64 @post				This object is properly constructed.
       
    65 
       
    66 @param				aElementTable the element string table associated with this code page.
       
    67 @param				aAttributeTable the attribute string table associated with this code page.
       
    68 @param				aValueTable the attributevalue string table associated with this code page.
       
    69 @param				aCodePage the codepage number for this object.
       
    70 
       
    71 */
       
    72 :	iElementTable(aElementTable), // we do not own these StringTable
       
    73 	iAttributeTable(aAttributeTable),
       
    74 	iValueTable(aValueTable),
       
    75 	iElementStringPoolIndexToToken(KTokenGranularity), 
       
    76 	iElementTokenToStringPoolIndex(KTokenGranularity,
       
    77 								   _FOFF(TStringPoolTokenMapping, iTokenValue)), // key offset
       
    78 	iAttributeStringPoolIndexToToken(KTokenGranularity), 
       
    79 	iAttributeTokenToStringPoolIndex(KTokenGranularity, 
       
    80 									 _FOFF(TStringPoolTokenMapping, iTokenValue)), // key offset
       
    81 	iValueStringPoolIndexToToken(KTokenGranularity), 
       
    82 	iValueTokenToStringPoolIndex(KTokenGranularity, 
       
    83 								 _FOFF(TStringPoolTokenMapping, iTokenValue)), // key offset
       
    84 	iCodePage(aCodePage)
       
    85 	{
       
    86 	// do nothing;
       
    87 	}
       
    88 
       
    89 
       
    90 EXPORT_C const TStringTable* CDictionaryCodePage::StringTable(TStringType aType) const
       
    91 /**
       
    92 This method obtains the correct string table based on the type passed.
       
    93 
       
    94 @return				the correct string table for that type.
       
    95 
       
    96 @param				aType the type of string table required, e.g. element.
       
    97 @panic				EXmlFrameworkPanicUnexpectedLogic If the type cannot be recognised.
       
    98 
       
    99 */
       
   100 	{
       
   101 	const TStringTable* table = 0;
       
   102 
       
   103 	switch (aType)
       
   104 		{
       
   105 		case EStringTypeElement:
       
   106 			{
       
   107 			table = iElementTable;
       
   108 			break;
       
   109 			}
       
   110 		case EStringTypeAttribute:
       
   111 			{
       
   112 			table = iAttributeTable;
       
   113 			break;
       
   114 			}
       
   115 		case EStringTypeAttributeValue:
       
   116 			{
       
   117 			table = iValueTable;
       
   118 			break;
       
   119 			}
       
   120 		default:
       
   121 			{
       
   122 			__ASSERT_ALWAYS(EFalse, Panic(EXmlFrameworkPanicUnexpectedLogic));
       
   123 			}
       
   124 		};
       
   125 	return table;
       
   126 	}
       
   127 
       
   128 
       
   129 
       
   130 EXPORT_C TUint8 CDictionaryCodePage::CodePage() const
       
   131 /**
       
   132 This method returns the codepage number for this page.
       
   133 
       
   134 @return				the codepage number.
       
   135 
       
   136 */
       
   137 	{
       
   138 	return iCodePage;
       
   139 	}
       
   140 
       
   141 
       
   142 
       
   143 EXPORT_C TInt CDictionaryCodePage::StringPoolIndexFromToken(TInt aToken, TStringType aType) const
       
   144 /**
       
   145 This method obtains a String Pool index from a token value
       
   146 
       
   147 @return				the String Pool index
       
   148 @return				KErrXmlStringPoolTableNotFound
       
   149 
       
   150 @param				aToken is the token value.
       
   151 @param				aType is the type of the token, e.g. element.
       
   152 
       
   153 @panic				EXmlFrameworkPanicUnexpectedLogic If the type cannot be recognised.
       
   154 
       
   155 */
       
   156 	{
       
   157 	const RArray<TStringPoolTokenMapping>* toIndex = 0;
       
   158 
       
   159 	switch (aType)
       
   160 		{
       
   161 		case EStringTypeElement:
       
   162 			{
       
   163 			toIndex = &iElementTokenToStringPoolIndex;
       
   164 			break;
       
   165 			}
       
   166 		case EStringTypeAttribute:
       
   167 			{
       
   168 			toIndex = &iAttributeTokenToStringPoolIndex;
       
   169 			break;
       
   170 			}
       
   171 		case EStringTypeAttributeValue:
       
   172 			{
       
   173 			toIndex = &iValueTokenToStringPoolIndex;
       
   174 			break;
       
   175 			}
       
   176 		default:
       
   177 			{
       
   178 			__ASSERT_ALWAYS(EFalse, Panic(EXmlFrameworkPanicUnexpectedLogic));
       
   179 			}
       
   180 		};
       
   181 
       
   182 	TStringPoolTokenMapping map;
       
   183 	map.iTokenValue = aToken;
       
   184 	TInt index;
       
   185 
       
   186 	// Since tokens are not numerically adjacent numbers a search is carried out:
       
   187 	if (toIndex->FindInSignedKeyOrder(map, index) == KErrNone)
       
   188 		{
       
   189 		return ((*toIndex)[index]).iTableIndex;
       
   190 		}
       
   191 	return KErrXmlStringPoolTableNotFound;
       
   192 	}
       
   193 
       
   194 
       
   195 
       
   196 EXPORT_C TInt CDictionaryCodePage::TokenFromStringPoolIndex(TInt aIndex, TStringType aType) const
       
   197 /**
       
   198 This method obtains a Token value from a String Pool index.
       
   199 
       
   200 @return				the Token value.
       
   201 
       
   202 @param				aIndex is the String Pool index
       
   203 @param				aType is the type of the token, e.g. element.
       
   204 
       
   205 @panic				EXmlFrameworkPanicUnexpectedLogic If the type cannot be recognised.
       
   206 
       
   207 */
       
   208 	{
       
   209 	const RArray<TInt>* toToken = 0;
       
   210 
       
   211 	switch (aType)
       
   212 		{
       
   213 		case EStringTypeElement:
       
   214 			{
       
   215 			toToken = &iElementStringPoolIndexToToken;
       
   216 			break;
       
   217 			}
       
   218 		case EStringTypeAttribute:
       
   219 			{
       
   220 			toToken = &iAttributeStringPoolIndexToToken;
       
   221 			break;
       
   222 			}
       
   223 		case EStringTypeAttributeValue:
       
   224 			{
       
   225 			toToken = &iValueStringPoolIndexToToken;
       
   226 			break;
       
   227 			}
       
   228 		default:
       
   229 			{
       
   230 			__ASSERT_ALWAYS(EFalse, Panic(EXmlFrameworkPanicUnexpectedLogic));
       
   231 			}
       
   232 		};
       
   233 
       
   234 	return ((*toToken)[aIndex]);
       
   235 	}
       
   236 
       
   237 
       
   238 
       
   239 EXPORT_C void CDictionaryCodePage::ConstructIndexMappingL(const TInt* aStringPoolToTokenMapping, 
       
   240 													 TStringType aType)
       
   241 /**
       
   242 This method constructs the internal correlation between indices and tokens.
       
   243 
       
   244 @param				aStringPoolToTokenMapping array of token values, NULL terminated.
       
   245 @param				aType is the type of the token, e.g. element.
       
   246 
       
   247 @panic				EXmlFrameworkPanicUnexpectedLogic If the type cannot be recognised.
       
   248 
       
   249 */
       
   250 	{
       
   251 	RArray<TInt>* toToken = 0;
       
   252 	RArray<TStringPoolTokenMapping>* toIndex = 0;
       
   253 
       
   254 	switch (aType)
       
   255 		{
       
   256 		case EStringTypeElement:
       
   257 			{
       
   258 			toToken = &iElementStringPoolIndexToToken;
       
   259 			toIndex = &iElementTokenToStringPoolIndex;
       
   260 			break;
       
   261 			}
       
   262 		case EStringTypeAttribute:
       
   263 			{
       
   264 			toToken = &iAttributeStringPoolIndexToToken;
       
   265 			toIndex = &iAttributeTokenToStringPoolIndex;
       
   266 			break;
       
   267 			}
       
   268 		case EStringTypeAttributeValue:
       
   269 			{
       
   270 			toToken = &iValueStringPoolIndexToToken;
       
   271 			toIndex = &iValueTokenToStringPoolIndex;
       
   272 			break;
       
   273 			}
       
   274 		default:
       
   275 			{
       
   276 			__ASSERT_ALWAYS(EFalse, Panic(EXmlFrameworkPanicUnexpectedLogic));
       
   277 			}
       
   278 		};
       
   279 
       
   280 	TInt element = 0;
       
   281 	for (TInt count=0; (element = aStringPoolToTokenMapping[count]) != 0; ++count)
       
   282 		{
       
   283 		// The index to token mapping is straight forward.
       
   284 		// We simply add a table index. 
       
   285 		// The table index is the same index into the RArray saving space.
       
   286 		User::LeaveIfError(toToken->Append(element));
       
   287 
       
   288 		// The token to index mapping is slightly more complicated,
       
   289 		// and we use a compare function to extract it.
       
   290 		TStringPoolTokenMapping mapping;
       
   291 		mapping.iTokenValue = element;		// This is the primary key (Token)
       
   292 		mapping.iTableIndex = count;								// This is the Table index
       
   293 		User::LeaveIfError(toIndex->InsertInOrder(
       
   294 								mapping, 
       
   295 								TLinearOrder<TStringPoolTokenMapping>(
       
   296 									CDictionaryCodePage::CompareStringPoolTokenMappingTable)));
       
   297 		}	
       
   298 	}
       
   299 
       
   300 
       
   301 
       
   302 TInt CDictionaryCodePage::CompareStringPoolTokenMappingTable(const TStringPoolTokenMapping& aFirst, 
       
   303 														const TStringPoolTokenMapping& aSecond)
       
   304 /**
       
   305 This method compares two codepages and determines the order of two objects of a given class type.
       
   306 
       
   307 @return				zero, if the two objects are equal
       
   308 @return				a negative value, if the first object is less than the second.
       
   309 @return				a positive value, if the first object is greater than the second.
       
   310 
       
   311 @param				aFirst the first object.
       
   312 @param				aSecond the second object.
       
   313 */
       
   314 	{
       
   315 	return aFirst.iTokenValue - aSecond.iTokenValue;
       
   316 	}