applayerpluginsandutils/uripermissionservices/server/src/tldlistinitializer.cpp
changeset 0 b16258d2340f
child 49 b91bcc4b38e4
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2008-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 #include "tldlistinitializer.h"
       
    16 #include "tldschema.h"
       
    17 #include "urilistinterface.h"
       
    18 #include "tldproperties.h"
       
    19 
       
    20 using namespace Xml;
       
    21 
       
    22 CTldListInitializer* CTldListInitializer::NewL ( CUriListInterface& aListInterface )
       
    23 	{
       
    24 	CTldListInitializer* self = new ( ELeave ) CTldListInitializer ( aListInterface );
       
    25 	CleanupStack::PushL ( self );
       
    26 	self->ConstructL ();
       
    27 	CleanupStack::Pop ();
       
    28 	return self;
       
    29 	}
       
    30 
       
    31 CTldListInitializer::~CTldListInitializer ()
       
    32 	{
       
    33 	iContent.Close ();
       
    34 	iTldName.Close ();
       
    35 	iStringPool.Close ();
       
    36 	delete iTldUriImpl;
       
    37 	}
       
    38 
       
    39 
       
    40 CTldListInitializer::CTldListInitializer ( CUriListInterface& aListInterface )
       
    41 : iListInterface ( aListInterface ),
       
    42 iParserState ( EInitial )
       
    43 	{
       
    44 		
       
    45 	}
       
    46 
       
    47 void CTldListInitializer::ConstructL ()
       
    48 	{
       
    49 	const TInt KMaxContentSize = 4 * 1024;
       
    50 	iContent.CreateL (KMaxContentSize);
       
    51 	iStringPool.OpenL ( TLDXMLTAGS::Table ); // Open the schema table
       
    52 	iTldUriImpl = CTldProperties::NewL (InetUriList::EBlackList); // Create an empty object
       
    53 	}
       
    54 
       
    55 void CTldListInitializer::ParseDocumentL ( const TDesC& aFile )
       
    56 	{
       
    57 	_LIT8( KXmlParserDataType, "text/xml" );
       
    58 	
       
    59 	RFs myFs;
       
    60 	CleanupClosePushL ( myFs );
       
    61 	User::LeaveIfError ( myFs.Connect () );
       
    62 	
       
    63 	CParser* myParser = CParser::NewLC ( KXmlParserDataType(), *this );
       
    64 	ParseL ( *myParser, myFs, aFile );
       
    65 	CleanupStack::PopAndDestroy ( 2 ); // myParser, myFs
       
    66 	}
       
    67 
       
    68 // Virtual functions from MContentHandler
       
    69 void CTldListInitializer::OnStartDocumentL ( const Xml::RDocumentParameters& /* aDocParam */, TInt aErrorCode )
       
    70 	{
       
    71 	User::LeaveIfError ( aErrorCode );
       
    72 	}
       
    73 	
       
    74 void CTldListInitializer::OnEndDocumentL ( TInt aErrorCode )
       
    75 	{
       
    76 	User::LeaveIfError ( aErrorCode );		
       
    77 	}
       
    78 
       
    79 void CTldListInitializer::OnStartElementL ( const Xml::RTagInfo& aElement, const Xml::RAttributeArray&  aAttributes, TInt aErrorCode )
       
    80 	{	
       
    81 	User::LeaveIfError ( aErrorCode );		
       
    82 	SetParserState ( ParserStateL( aElement.LocalName() ) );
       
    83 	SaveTldName(aAttributes); 
       
    84 	SetTldNameL ( );
       
    85 	}
       
    86 
       
    87 void CTldListInitializer::OnEndElementL ( const Xml::RTagInfo& aElement, TInt aErrorCode )
       
    88 	{
       
    89 	User::LeaveIfError ( aErrorCode );
       
    90 	SetParserState ( ParserStateL( aElement.LocalName(), ETrue ) );		
       
    91 	SetValueL ( KNullDesC8() );
       
    92 	if ( iParserState == EEndOfListtype )
       
    93 		{
       
    94 		// Insert the record and then clear
       
    95 		iListInterface.InsertTldPolicyDataL( *iTldUriImpl );
       
    96 		iTldUriImpl->Clear();
       
    97 		}
       
    98 	}
       
    99 
       
   100 void CTldListInitializer::OnContentL ( const TDesC8& aBytes, TInt aErrorCode )
       
   101 	{
       
   102 	User::LeaveIfError ( aErrorCode );	
       
   103 	SetValueL ( aBytes );
       
   104 	}
       
   105 
       
   106 void CTldListInitializer::SetParserState ( TParserState aNewState )
       
   107 	{
       
   108 	switch ( aNewState )
       
   109 		{
       
   110 		case EInitial:
       
   111 		iParserState = EInitial;
       
   112 		break;
       
   113 		
       
   114 		case EStartOfPolicyData:
       
   115 		__ASSERT_DEBUG ( iParserState == EInitial, User::Invariant() );
       
   116 		iParserState = aNewState;
       
   117 		break;
       
   118 
       
   119 		case EEndOfPolicyData:
       
   120 		__ASSERT_DEBUG ( iParserState == EEndOfTLD, User::Invariant() );
       
   121 		iParserState = aNewState;
       
   122 		break;
       
   123 		
       
   124 		case EStartOfTLD:
       
   125 		__ASSERT_DEBUG ( (iParserState == EStartOfPolicyData) || (iParserState == EEndOfTLD ), User::Invariant() );
       
   126 		iParserState = aNewState;				
       
   127 		break;
       
   128 		
       
   129 		case EEndOfTLD:
       
   130 		__ASSERT_DEBUG ( iParserState == EEndOfListtype, User::Invariant() );
       
   131 		iParserState = aNewState;				
       
   132 		break;
       
   133 		
       
   134 		case EStartOfListtype:
       
   135 		__ASSERT_DEBUG ( (iParserState ==  EStartOfTLD ) || (iParserState == EEndOfListtype), User::Invariant() );
       
   136 		iParserState = aNewState;
       
   137 		break;
       
   138 		
       
   139 		case EEndOfListtype:
       
   140 		__ASSERT_DEBUG ( iParserState == EStartOfListtype, User::Invariant() );
       
   141 		iParserState = aNewState;		
       
   142 		break;
       
   143 		
       
   144 		default:
       
   145 
       
   146 		// Do nothing
       
   147 		break;
       
   148 		}	
       
   149 	}
       
   150 
       
   151 CTldListInitializer::TParserState CTldListInitializer::ParserStateL ( const RString& aTag, TBool aEndElement /* =EFalse */ )
       
   152 	{	
       
   153 	// Open a RString from the string pool
       
   154 	RString myTagString = iStringPool.OpenStringL ( aTag.DesC() );
       
   155 	TInt tblIndex = myTagString.Index ( TLDXMLTAGS::Table );
       
   156 	TParserState parserState( EInitial );
       
   157 	switch ( tblIndex )
       
   158 		{
       
   159 		case TLDXMLTAGS::EPolicyData:
       
   160 			{
       
   161 			parserState = aEndElement ? EEndOfPolicyData : EStartOfPolicyData;
       
   162 			break;				
       
   163 			}
       
   164 
       
   165 		case TLDXMLTAGS::EWhitelist:
       
   166 		case TLDXMLTAGS::EBlacklist:
       
   167 			{
       
   168 			parserState = aEndElement ? EEndOfListtype : EStartOfListtype;
       
   169 			SetListType ( tblIndex );
       
   170 			break;				
       
   171 			}
       
   172 		
       
   173 		case TLDXMLTAGS::ETLD:
       
   174 			{
       
   175 			parserState = aEndElement ? EEndOfTLD : EStartOfTLD;
       
   176 			break;	
       
   177 			}
       
   178 
       
   179 		default:
       
   180 			{
       
   181 			// Do nothing
       
   182 			break;
       
   183 			}
       
   184 		}	
       
   185 	myTagString.Close ();
       
   186 	return parserState;	
       
   187 	}
       
   188 
       
   189 void CTldListInitializer::SetListType ( TInt aIndex )
       
   190 	{
       
   191 	switch ( aIndex )	
       
   192 		{
       
   193 		case TLDXMLTAGS::EWhitelist: 
       
   194 			{
       
   195 			iTldUriImpl->SetListType(InetUriList::EWhiteList);
       
   196 			break;		
       
   197 			}
       
   198 		case TLDXMLTAGS::EBlacklist:
       
   199 			{
       
   200 			iTldUriImpl->SetListType(InetUriList::EBlackList);
       
   201 			break;	
       
   202 			}
       
   203 		default:
       
   204 			{
       
   205 			User::Invariant ();	
       
   206 			}
       
   207 		}
       
   208 	}
       
   209 
       
   210 void CTldListInitializer::SetTldNameL ( )
       
   211 	{
       
   212 	switch ( iParserState )	
       
   213 		{
       
   214 		case EStartOfListtype:
       
   215 			{
       
   216 			iTldUriImpl->SetTldNameL(iTldName);
       
   217 			break;
       
   218 			}
       
   219 		default:
       
   220 			{
       
   221 			//Do Nothing
       
   222 			break;
       
   223 			}
       
   224 		}
       
   225 	}	
       
   226 
       
   227 void CTldListInitializer::SetValueL ( const TDesC8& aValue )
       
   228 	{	
       
   229 	switch ( iParserState )		
       
   230 		{
       
   231 		case EStartOfListtype:
       
   232 			{
       
   233 			iContent.Append ( aValue );
       
   234 			break;	
       
   235 			}
       
   236 		case EEndOfListtype:
       
   237 			{
       
   238 			if ( iContent.Length() > 0 )
       
   239 				{
       
   240 				//Set initialize database(Blobs)
       
   241 				iTldUriImpl->SetCharacterSetL(iContent);
       
   242 				iContent.Zero ();
       
   243 				}
       
   244 			break;
       
   245 			}
       
   246 		default: 
       
   247 			{
       
   248 			// do nothing
       
   249 			break;	
       
   250 			}
       
   251 		}
       
   252 	}
       
   253 
       
   254 //Store the Tld name for later use
       
   255 void CTldListInitializer::SaveTldName(const Xml::RAttributeArray&  aAttributes)
       
   256 	{
       
   257 	_LIT8( KName, "name" );
       
   258 	TInt nAttributes = aAttributes.Count();
       
   259 	for(TInt index(0); index < nAttributes; index++)
       
   260 		{
       
   261 		const RAttribute& KAttribute = aAttributes[index];
       
   262 		const RTagInfo& KNameInfo = KAttribute.Attribute();
       
   263 		const TDesC8& KLocalName8 = KNameInfo.LocalName().DesC();
       
   264 		if ( iParserState == EStartOfTLD )
       
   265 			{
       
   266 			// Store the TLD name for later use
       
   267 			__ASSERT_ALWAYS(KLocalName8.CompareF( KName ) == 0, User::Invariant() );
       
   268 			iTldName.Close();
       
   269 			iTldName.Create( KAttribute.Value().DesC() );
       
   270 			break;	
       
   271 			}
       
   272 		}
       
   273 	}
       
   274 	
       
   275