xml/xmlfw/src/xmlframework/stringdictionarycollectionimpl.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 <ecom/ecom.h>
       
    17 #include <stringpool.h>
       
    18 
       
    19 #include <xml/stringdictionarycollection.h>
       
    20 #include <xml/xmlframeworkconstants.h>
       
    21 #include <xml/xmlframeworkerrors.h>
       
    22 #include "XmlFrameworkPanics.h"
       
    23 #include <xml/plugins/stringdictionary.h>
       
    24 
       
    25 #include "stringdictionarycollectionimpl.h"
       
    26 
       
    27 using namespace Xml;
       
    28 
       
    29 void RStringDictionaryCollectionImpl::DestroyMapping(TAny* aPtr)
       
    30 /**
       
    31 Tidy memory associated with this object.
       
    32 
       
    33 @param				aPtr The StringDictionary pointer.
       
    34 
       
    35 */
       
    36 	{
       
    37 	TDtorMapping* ptr = static_cast<TDtorMapping*>(aPtr);
       
    38 	 
       
    39 	if (ptr->iDtorKey != KNullUid && ptr->iStringDictionary)
       
    40 		{
       
    41 		// Tell ECom to destroy its plugin details
       
    42 		REComSession::DestroyedImplementation(ptr->iDtorKey);
       
    43 
       
    44 		// Free memory
       
    45 		ptr->iStringDictionary->Release();
       
    46 		}
       
    47 
       
    48 	delete (ptr);
       
    49 	}
       
    50 
       
    51 
       
    52 
       
    53 RStringDictionaryCollectionImpl::RStringDictionaryCollectionImpl()
       
    54 /**
       
    55 Default constructor
       
    56 
       
    57 @post				This object is properly constructed.
       
    58 
       
    59 */
       
    60 :	iRefCount(0),
       
    61 	iStringDictionary(NULL)
       
    62 	{
       
    63 	// do nothing;
       
    64 	}
       
    65 
       
    66 
       
    67 
       
    68 void RStringDictionaryCollectionImpl::OpenL()
       
    69 /**
       
    70 This method opens this resource incrementing the reference count.
       
    71 It must be the first method called after construction.
       
    72 
       
    73 @post				The object is ready to be used.
       
    74 
       
    75 */
       
    76 	{
       
    77 	++iRefCount;
       
    78 
       
    79 	if (iRefCount == 1)
       
    80 		{
       
    81 		// Need to inc before just in case the Open leaves.
       
    82 		// In this case a subsequent Close will panic if the count is dec below zero.
       
    83 		iStringPool.OpenL();
       
    84 		}
       
    85 	}
       
    86 
       
    87 
       
    88 TInt RStringDictionaryCollectionImpl::Close()
       
    89 /**
       
    90 This method cleans up the object before destruction. It releases all resources in
       
    91 accordance to the R Class pattern.
       
    92 
       
    93 @post				This object may be allowed to go out of scope.
       
    94 
       
    95 */
       
    96 	{
       
    97 	if (iRefCount==1)
       
    98 		{
       
    99 		TInt count = iDtorKeyAndDictionaryList.Count();
       
   100 
       
   101 		while (count)
       
   102 			{
       
   103 			DestroyMapping(iDtorKeyAndDictionaryList[--count]);
       
   104 			}
       
   105 
       
   106 		iDtorKeyAndDictionaryList.Reset();
       
   107 
       
   108 		iStringPool.Close();
       
   109 
       
   110 		REComSession::FinalClose();
       
   111 		}
       
   112 
       
   113 	--iRefCount;
       
   114 	__ASSERT_ALWAYS(iRefCount >= 0, Panic(EXmlFrameworkPanicReferenceCountNegative));
       
   115 	
       
   116 	return iRefCount;
       
   117 	}
       
   118 
       
   119 
       
   120 void RStringDictionaryCollectionImpl::OpenDictionaryL(const TDesC8& aDictionaryDescription)
       
   121 /**
       
   122 This method loads the Dictionary.
       
   123 
       
   124 @pre				Connect has been called.
       
   125 @post				The Dictionary has been loaded.
       
   126 
       
   127 @param				aDictionaryDescription The Namepspace MIME type.
       
   128 */
       
   129 	{
       
   130 	if (aDictionaryDescription.Size() == 0)
       
   131 		{
       
   132 		// No Mime type provided.
       
   133 		// Dictionary not required.
       
   134 		iStringDictionary = NULL;
       
   135 		return;
       
   136 		}
       
   137 
       
   138 	// Implies a Dictionary exists
       
   139 
       
   140 	RString nsUri = iStringPool.OpenStringL(aDictionaryDescription);
       
   141 	CleanupClosePushL(nsUri);
       
   142 
       
   143 	if (LocateDictionary(nsUri, iStringDictionary) == EFalse)
       
   144 		{
       
   145 		// Locate the plugin
       
   146 		iStringDictionary = ConstructDictionaryL(aDictionaryDescription, iStringPool);
       
   147 		}
       
   148 
       
   149 	CleanupStack::PopAndDestroy(&nsUri);
       
   150 	}
       
   151 
       
   152 
       
   153 
       
   154 TBool RStringDictionaryCollectionImpl::LocateDictionary(const RString& aNsUri, MStringDictionary*& aStringDictionary)
       
   155 /**
       
   156 This method locates an existing loaded Dictionary by it uri name.
       
   157 
       
   158 @return				EFalse if the Dictionary cannot be located, ETrue if it has.
       
   159 
       
   160 @param				aNsUri The Dictionary uri.
       
   161 @param				aStringDictionary On return, points to the requested Dictionary.
       
   162 */
       
   163 	{
       
   164 	TBool found = EFalse;
       
   165 	TUint count = iDtorKeyAndDictionaryList.Count();
       
   166 
       
   167 	while (!found && count>0)
       
   168 		{
       
   169 		// Check if its been loaded already
       
   170 		if (iDtorKeyAndDictionaryList[--count]->iStringDictionary->CompareThisDictionary(aNsUri))
       
   171 			{
       
   172 			aStringDictionary = iDtorKeyAndDictionaryList[count]->iStringDictionary;
       
   173 			found = ETrue;
       
   174 			}
       
   175 		}
       
   176 	return found;
       
   177 	}
       
   178 
       
   179 
       
   180 
       
   181 RStringPool& RStringDictionaryCollectionImpl::StringPool()
       
   182 /**
       
   183 This method obtains a handle to the RStringPool
       
   184 
       
   185 @return				the RStringPool
       
   186 
       
   187 */
       
   188 	{
       
   189 	return iStringPool;
       
   190 	}
       
   191 
       
   192 
       
   193 MStringDictionary& RStringDictionaryCollectionImpl::CurrentDictionaryL() const
       
   194 /**
       
   195 This method obtains the current string dictionary in use.
       
   196 Also, serves as a way to test if any dictionaries have been loaded as none needs be.
       
   197 
       
   198 @return				the current Dictionary in use, if any.
       
   199 @leave 				KErrXmlMissingStringDictionary if there is no dictionary
       
   200 
       
   201 */
       
   202 	{
       
   203 	if (!iStringDictionary)
       
   204 		{
       
   205 		User::Leave(KErrXmlMissingStringDictionary);
       
   206 		}
       
   207 
       
   208 	return *iStringDictionary;
       
   209 	}
       
   210 
       
   211 
       
   212 
       
   213 MStringDictionary* RStringDictionaryCollectionImpl::ConstructDictionaryL(const TDesC8& aDictionaryUri, 
       
   214 															RStringPool& aStringPool)
       
   215 /**
       
   216 This method constructs a StringDictionary.
       
   217 
       
   218 @return				A pointer to the String Dictionary plugin found.
       
   219 @leave				KErrXmlDictionaryPluginNotFound If ECom fails to find 
       
   220 					the object a leave occurs.
       
   221 
       
   222 @param				aDictionaryUri The uri of this Dictionary.
       
   223 @param				aStringPool Contains the RTable for this Dictionary
       
   224 */
       
   225 	{
       
   226 	TDtorMapping* mapping = new(ELeave) TDtorMapping;
       
   227 	mapping->iDtorKey = KNullUid;
       
   228 	mapping->iStringDictionary = NULL;
       
   229 	
       
   230 	CleanupStack::PushL(TCleanupItem(DestroyMapping, mapping));
       
   231 
       
   232 	// Set resolving parameters to find a plug-in with a matching Dictionary URI
       
   233 	TEComResolverParams resolverParams;
       
   234 	resolverParams.SetDataType(aDictionaryUri);
       
   235 	resolverParams.SetWildcardMatch(ETrue);
       
   236 
       
   237 	TAny* any = NULL;
       
   238 	TRAPD(err, any = REComSession::CreateImplementationL(KStringDictionaryInterfaceUid, 
       
   239 														 mapping->iDtorKey, 
       
   240 														 &aStringPool, 
       
   241 														 resolverParams));
       
   242 													
       
   243 	if (err != KErrNone)
       
   244 		{
       
   245 		if (err == KErrNotFound)
       
   246 			{
       
   247 			User::Leave(KErrXmlStringDictionaryPluginNotFound);
       
   248 			}
       
   249 
       
   250 		User::Leave(err);
       
   251 		}
       
   252 
       
   253 	mapping->iStringDictionary = static_cast<MStringDictionary*>(any);
       
   254 	
       
   255 	User::LeaveIfError(iDtorKeyAndDictionaryList.Append(mapping));
       
   256 
       
   257 	CleanupStack::Pop(mapping);
       
   258 	return (mapping->iStringDictionary); 
       
   259 	}