xml/libxml2libs/test/xmlengtester/xmlengtester_emulator/src/testcontenthandler.cpp
changeset 0 e35f40988205
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 // Copyright (c) 2005-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 "TestContentHandler.h"
       
    17 
       
    18 
       
    19 CTestHandler* CTestHandler::NewL(const RFile& aFileName)
       
    20 {
       
    21 	CTestHandler* tester = new (ELeave) CTestHandler();
       
    22 	CleanupStack::PushL(tester);
       
    23 	tester->ConstructL(aFileName);
       
    24 	CleanupStack::Pop(tester);
       
    25 	return tester;
       
    26 }
       
    27 
       
    28 
       
    29 CTestHandler* CTestHandler::NewLC(const RFile& aFileName)
       
    30 {
       
    31 	CTestHandler* tester = new (ELeave) CTestHandler();
       
    32 	CleanupStack::PushL(tester);
       
    33 	tester->ConstructL(aFileName);
       
    34 	return tester;
       
    35 }
       
    36 
       
    37 CTestHandler::~CTestHandler()
       
    38 {
       
    39 }
       
    40 
       
    41 CTestHandler::CTestHandler()
       
    42 {
       
    43 }
       
    44 
       
    45 void CTestHandler::ConstructL(const RFile & aFileName)
       
    46 {
       
    47 	iLog = aFileName;
       
    48 }
       
    49 
       
    50 /**
       
    51 This method is a callback to indicate the start of the document.
       
    52 @param				aDocParam Specifies the various parameters of the document.
       
    53 					aDocParam.iCharacterSetName The character encoding of the document.
       
    54 @param				aErrorCode is the error code. 
       
    55 					If this is not KErrNone then special action may be required.
       
    56 */
       
    57 void CTestHandler::OnStartDocumentL(const RDocumentParameters& aDocParam, TInt aErrorCode)
       
    58 {
       
    59 	_LIT8(KOnStartDocumentFuncName,"OnStartDocument()\r\n");
       
    60 	_LIT8(KInfoOnStartDocP,"\tDocument start \tparameters: %S \r\n");
       
    61 	_LIT8(KInfoOnError,"Error occurs %d \r\n");
       
    62 		
       
    63 	iLog.Write(KOnStartDocumentFuncName);
       
    64 
       
    65 	if (aErrorCode == KErrNone)
       
    66 	{
       
    67 		TBuf8<KShortInfoSize> info;
       
    68 		TBuf8<KShortInfoSize> info2;
       
    69 		
       
    70 		info2.Copy(aDocParam.CharacterSetName().DesC());		
       
    71 
       
    72 		info.Format(KInfoOnStartDocP,&info2);
       
    73 		iLog.Write(info);
       
    74 	}
       
    75 	else
       
    76 	{
       
    77 		TBuf8<KShortInfoSize> info;
       
    78 		info.Format(KInfoOnError,aErrorCode);
       
    79 		iLog.Write(info);
       
    80 	}
       
    81 }
       
    82 
       
    83 
       
    84 /**
       
    85 This method is a callback to indicate the end of the document.
       
    86 @param				aErrorCode is the error code. 
       
    87 					If this is not KErrNone then special action may be required.
       
    88 */
       
    89 void CTestHandler::OnEndDocumentL(TInt aErrorCode)
       
    90 {
       
    91 	_LIT8(KOnEndDocumentFuncName,"OnEndDocument()\r\n");
       
    92 	_LIT8(KInfoOnEndDoc,"\tDocument end \r\n");
       
    93 	_LIT8(KInfoOnError,"Error occurs %d \r\n");
       
    94 
       
    95 	iLog.Write(KOnEndDocumentFuncName);
       
    96 	
       
    97 	if (aErrorCode == KErrNone)
       
    98 	{
       
    99 		iLog.Write(KInfoOnEndDoc);
       
   100 	}
       
   101 	else
       
   102 	{
       
   103 		TBuf8<KShortInfoSize> info;
       
   104 		info.Format(KInfoOnError,aErrorCode);
       
   105 		iLog.Write(info);
       
   106 	}
       
   107 }
       
   108 
       
   109 
       
   110 /**
       
   111 This method is a callback to indicate an element has been parsed.
       
   112 @param				aElement is a handle to the element's details.
       
   113 @param				aAttributes contains the attributes for the element.
       
   114 @param				aErrorCode is the error code.
       
   115 					If this is not KErrNone then special action may be required.
       
   116 */
       
   117 void CTestHandler::OnStartElementL(const RTagInfo& aElement, const RAttributeArray& aAttributes, 
       
   118 								 TInt aErrorCode)
       
   119 {
       
   120 	_LIT8(KOnStartElementFuncName,"OnStartElement()\r\n");
       
   121 	_LIT8(KInfoOnStartElePU,"\tElement start namespace: %S \t prefix: %S \tname: %S\r\n");
       
   122 	_LIT8(KInfoOnAttributePU,"\t\tAttribute namaspace: %S \t prefix: %S \tname: %S \t value: %S\r\n");
       
   123 	_LIT8(KInfoOnError,"Error occurs %d \r\n");
       
   124 
       
   125 	iLog.Write(KOnStartElementFuncName);
       
   126 	
       
   127 	if (aErrorCode == KErrNone)
       
   128 	{
       
   129 		
       
   130 		TBuf8<KShortInfoSize> info;
       
   131 		TBuf8<KShortInfoSize> info2;
       
   132 		TBuf8<KShortInfoSize> info3;
       
   133 		TBuf8<KShortInfoSize> info4;
       
   134 		TBuf8<KShortInfoSize> info5;
       
   135 
       
   136 		info2.Copy(aElement.LocalName().DesC());		
       
   137 		info3.Copy(aElement.Uri().DesC());		
       
   138 		info4.Copy(aElement.Prefix().DesC());
       
   139 		
       
   140 		info.Format(KInfoOnStartElePU,&info3,&info4,&info2);	
       
   141 		iLog.Write(info);
       
   142 		
       
   143 		RArray <RAttribute> array = aAttributes;
       
   144 		TInt size = array.Count();
       
   145 		
       
   146 		RAttribute attr;
       
   147 		
       
   148 		if ( size > 0 )
       
   149 		{
       
   150 			for ( TInt i = 0; i < size; i++)
       
   151 			{
       
   152 				attr = array[i];
       
   153 				
       
   154 				info2.Copy(attr.Attribute().LocalName().DesC());
       
   155 				info3.Copy(attr.Attribute().Uri().DesC());		
       
   156 				info4.Copy(attr.Attribute().Prefix().DesC());		
       
   157 				info5.Copy(attr.Value().DesC());		
       
   158 				
       
   159 				info.Format(KInfoOnAttributePU,&info3,&info4,&info2,&info5);
       
   160 				iLog.Write(info);
       
   161 			}
       
   162 		}
       
   163 	}
       
   164 	else
       
   165 	{
       
   166 		TBuf8<KShortInfoSize> info;
       
   167 		info.Format(KInfoOnError,aErrorCode);
       
   168 		iLog.Write(info);
       
   169 	}
       
   170 }
       
   171 
       
   172 	
       
   173 /**
       
   174 This method is a callback to indicate the end of the element has been reached.
       
   175 @param				aElement is a handle to the element's details.
       
   176 @param				aErrorCode is the error code.
       
   177 					If this is not KErrNone then special action may be required.
       
   178 */
       
   179 void CTestHandler::OnEndElementL(const RTagInfo& aElement, TInt aErrorCode)
       
   180 {
       
   181 	_LIT8(KOnEndElementFuncName,"OnEndElement()\r\n");
       
   182 	_LIT8(KInfoOnEndElePU,"\tElement end namespace: %S \t prefix: %S \tname: %S\r\n");
       
   183 	_LIT8(KInfoOnError,"Error occurs %d \r\n");
       
   184 
       
   185 	TBuf8<KShortInfoSize> info;
       
   186 	TBuf8<KShortInfoSize> info2;
       
   187 	TBuf8<KShortInfoSize> info3;
       
   188 	TBuf8<KShortInfoSize> info4;
       
   189 	
       
   190 	iLog.Write(KOnEndElementFuncName);
       
   191 	if (aErrorCode == KErrNone)
       
   192 	{
       
   193 		info2.Copy(aElement.LocalName().DesC());		
       
   194 		info3.Copy(aElement.Uri().DesC());		
       
   195 		info4.Copy(aElement.Prefix().DesC());
       
   196 		
       
   197 		info.Format(KInfoOnEndElePU,&info3,&info4,&info2);	
       
   198 		iLog.Write(info);
       
   199 	}
       
   200 	else
       
   201 	{
       
   202 		info.Format(KInfoOnError,aErrorCode);
       
   203 		iLog.Write(info);
       
   204 	}	
       
   205 }
       
   206 
       
   207 /**
       
   208 This method is a callback that sends the content of the element.
       
   209 Not all the content may be returned in one go. The data may be sent in chunks.
       
   210 When an OnEndElementL is received this means there is no more content to be sent.
       
   211 @param				aBytes is the raw content data for the element. 
       
   212 					The client is responsible for converting the data to the 
       
   213 					required character set if necessary.
       
   214 					In some instances the content may be binary and must not be converted.
       
   215 @param				aErrorCode is the error code.
       
   216 					If this is not KErrNone then special action may be required.
       
   217 */
       
   218 void CTestHandler::OnContentL(const TDesC8& aBytes, TInt aErrorCode)
       
   219 	 {
       
   220 	_LIT8(KOnContentFuncName,"OnContent()\r\n");
       
   221 	_LIT8(KInfoOnContent,"\tContent of element: %S \r\n");
       
   222 	_LIT8(KInfoOnError,"Error occurs %d \r\n");
       
   223 	TBuf8<KShortInfoSize> info;
       
   224 	TBuf8<KShortInfoSize> info2;
       
   225 	iLog.Write(KOnContentFuncName);
       
   226 	if (aErrorCode == KErrNone)
       
   227 		{
       
   228 		if(aBytes.Length() >= KShortInfoSize)
       
   229 			{ 
       
   230 			TPtrC8 bytes = aBytes.Ptr();
       
   231 			HBufC8* buf1 = HBufC8::NewL(aBytes.Length() + 100);
       
   232 			buf1->Des().Format(KInfoOnContent,&bytes);
       
   233 			iLog.Write(*buf1);
       
   234 			}
       
   235 		info2.Copy(aBytes);
       
   236 		info2.Trim();
       
   237 		if (info2.Length())
       
   238 			{
       
   239 			info.Format(KInfoOnContent,&info2);
       
   240 			iLog.Write(info);
       
   241 			}
       
   242 		}
       
   243 		else
       
   244 			{
       
   245 			TBuf8<KShortInfoSize> info;
       
   246 			info.Format(KInfoOnError,aErrorCode);
       
   247 			iLog.Write(info);
       
   248 			}
       
   249 	  }
       
   250 
       
   251 
       
   252 /**
       
   253 This method is a callback that sends the binary content of the element.
       
   254 @param				aBytes is the raw binary content data for the element. 
       
   255 @param				aErrorCode is the error code.
       
   256 					If this is not KErrNone then special action may be required.
       
   257 */
       
   258 void CTestHandler::OnBinaryContentL(const TDesC8& aBytes, const TDesC8& aCid, TInt aErrorCode)
       
   259 {
       
   260 /*
       
   261 	//use this part of the code if you want to save binary data to MSWord format
       
   262 	RFs fs;
       
   263 	CleanupClosePushL(fs);
       
   264 	User::LeaveIfError(fs.Connect());
       
   265 	RFile f;
       
   266 	CleanupClosePushL(f);
       
   267 	TInt err = f.Replace(fs, _L("c:\\xmlengine\\deserializer\\output\\bin.doc"), EFileWrite);
       
   268 	err = f.Write(aBytes);
       
   269 	CleanupStack::PopAndDestroy(2); //f fs
       
   270 */
       
   271 	aCid.Size(); // eliminate compilation warning
       
   272 	_LIT8(KOnContentFuncName,"OnBinaryContent()\r\n");
       
   273 	_LIT8(KInfoOnContent,"\tBinary content of element: %S \r\n");
       
   274 	_LIT8(KInfoOnError,"Error occurs %d \r\n");
       
   275 	
       
   276 	HBufC8* info = HBufC8::NewL(aBytes.Size() + 50);
       
   277 	TPtr8 infoPtr = info->Des();
       
   278 
       
   279 	iLog.Write(KOnContentFuncName);
       
   280 
       
   281 	if (aErrorCode == KErrNone)
       
   282 	{
       
   283 		infoPtr.Format(KInfoOnContent, &aBytes);
       
   284 		iLog.Write(infoPtr);
       
   285 	}
       
   286 	else
       
   287 	{
       
   288 		TBuf8<KShortInfoSize> info;
       
   289 		info.Format(KInfoOnError,aErrorCode);
       
   290 		iLog.Write(info);
       
   291 	}
       
   292 	delete info;
       
   293 }
       
   294 
       
   295 /**
       
   296 This method is a callback that sends data container reference
       
   297 @param				aContainer Container encapsulating binary data
       
   298 @param				aErrorCode is the error code.
       
   299 					If this is not KErrNone then special action may be required.
       
   300 */
       
   301 void CTestHandler::OnDataContainerL(const TXmlEngDataContainer& aContainer, TInt aErrorCode)
       
   302 {
       
   303 	_LIT8(KOnContentFuncName,"OnDataContainer()\r\n");
       
   304 	_LIT8(KInfoOnContent,"\tData container CID: %S \r\n");
       
   305 	_LIT8(KInfoOnError,"Error occurs %d \r\n");
       
   306 	
       
   307 	HBufC8* info = HBufC8::NewL(aContainer.Cid().Size() + 50);
       
   308 	TPtr8 infoPtr = info->Des();
       
   309 
       
   310 	iLog.Write(KOnContentFuncName);
       
   311 
       
   312 	if (aErrorCode == KErrNone)
       
   313 	{   TPtrC8 cid = aContainer.Cid();
       
   314 		infoPtr.Format(KInfoOnContent, &cid);
       
   315 		iLog.Write(infoPtr);
       
   316 	}
       
   317 	else
       
   318 	{
       
   319 		TBuf8<KShortInfoSize> info;
       
   320 		info.Format(KInfoOnError,aErrorCode);
       
   321 		iLog.Write(info);
       
   322 	}
       
   323 	delete info;	
       
   324 }
       
   325 	
       
   326 /**
       
   327 This method is a notification of the beginning of the scope of a prefix-URI Namespace mapping.
       
   328 This method is always called before the corresponding OnStartElementL method.
       
   329 @param				aPrefix is the Namespace prefix being declared.
       
   330 @param				aUri is the Namespace URI the prefix is mapped to.
       
   331 @param				aErrorCode is the error code.
       
   332 					If this is not KErrNone then special action may be required.
       
   333 */
       
   334 void CTestHandler::OnStartPrefixMappingL(const RString& aPrefix, const RString& aUri, 
       
   335 									   TInt aErrorCode)
       
   336 {
       
   337 	_LIT8(KOnStartPrefMapFuncName,"OnStartPrefixMapping()\r\n");
       
   338 	_LIT8(KInfoOnStartPref,"\tPrefix mapping start prefix: %S \t uri: %S \r\n");
       
   339 	_LIT8(KInfoOnError,"Error occurs %d \r\n");
       
   340 
       
   341 	iLog.Write(KOnStartPrefMapFuncName);
       
   342 	
       
   343 	if (aErrorCode == KErrNone)
       
   344 	{
       
   345 		TBuf8<KShortInfoSize> info;
       
   346 		TBuf8<KShortInfoSize> info2;
       
   347 		TBuf8<KShortInfoSize> info3;
       
   348 		
       
   349 		info2.Copy(aPrefix.DesC());		
       
   350 		info3.Copy(aUri.DesC());		
       
   351 		
       
   352 		info.Format(KInfoOnStartPref,&info2,&info3);
       
   353 	
       
   354 		iLog.Write(info);
       
   355 	}
       
   356 	else
       
   357 	{
       
   358 		TBuf8<KShortInfoSize> info;
       
   359 		info.Format(KInfoOnError,aErrorCode);
       
   360 		iLog.Write(info);
       
   361 	}
       
   362 }
       
   363 
       
   364 /**
       
   365 This method is a notification of the end of the scope of a prefix-URI mapping.
       
   366 This method is called after the corresponding DoEndElementL method.
       
   367 @param				aPrefix is the Namespace prefix that was mapped.
       
   368 @param				aErrorCode is the error code.
       
   369 					If this is not KErrNone then special action may be required.
       
   370 */
       
   371 void CTestHandler::OnEndPrefixMappingL(const RString& aPrefix, TInt aErrorCode)
       
   372 {
       
   373 	_LIT8(KOnEndPrefMapFuncName,"OnEndPrefixMapping()\r\n");
       
   374 	_LIT8(KInfoOnEndPref,"\tPrefix mapping end prefix: %S \r\n");
       
   375 	_LIT8(KInfoOnError,"Error occurs %d \r\n");
       
   376 	
       
   377 	iLog.Write(KOnEndPrefMapFuncName);
       
   378 	
       
   379 	if (aErrorCode == KErrNone)
       
   380 	{
       
   381 		TBuf8<KShortInfoSize> info;
       
   382 		TBuf8<KShortInfoSize> info2;
       
   383 		
       
   384 		info2.Copy(aPrefix.DesC());		
       
   385 		
       
   386 		info.Format(KInfoOnEndPref,&info2);
       
   387 	
       
   388 		iLog.Write(info);
       
   389 	}
       
   390 	else
       
   391 	{
       
   392 		TBuf8<KShortInfoSize> info;
       
   393 		info.Format(KInfoOnError,aErrorCode);
       
   394 		iLog.Write(info);
       
   395 	}
       
   396 }
       
   397 
       
   398 /**
       
   399 This method is a notification of ignorable whitespace in element content.
       
   400 @param				aBytes are the ignored bytes from the document being parsed.
       
   401 @param				aErrorCode is the error code.
       
   402 					If this is not KErrNone then special action may be required.
       
   403 */
       
   404 void CTestHandler::OnIgnorableWhiteSpaceL(const TDesC8& aBytes, TInt aErrorCode)
       
   405 {
       
   406 	_LIT8(KOnIgnorWhiteFuncName,"OnIgnorableWhiteSpace()\r\n");	
       
   407 	_LIT8(KInfoOnIgnorWhiteSpace,"\tIgnoring white space: %S length: %d \r\n");
       
   408 	_LIT8(KInfoOnError,"Error occurs %d \r\n");
       
   409 	
       
   410 	TBuf8<KShortInfoSize> info;
       
   411 	TBuf8<KShortInfoSize> info2;
       
   412 	
       
   413 	iLog.Write(KOnIgnorWhiteFuncName);
       
   414 	
       
   415 	if (aErrorCode == KErrNone)
       
   416 	{
       
   417 		info2.Copy(aBytes);
       
   418 		info2.Trim();
       
   419 		
       
   420 		if (info2.Length())
       
   421 		{
       
   422 			info.Format(KInfoOnIgnorWhiteSpace,&info2,info2.Length());
       
   423 			iLog.Write(info);
       
   424 		}
       
   425 	}
       
   426 	else
       
   427 	{
       
   428 		TBuf8<KShortInfoSize> info;
       
   429 		info.Format(KInfoOnError,aErrorCode);
       
   430 		iLog.Write(info);
       
   431 	}
       
   432 }
       
   433 
       
   434 /**
       
   435 This method is a notification of a skipped entity. If the parser encounters an 
       
   436 external entity it does not need to expand it - it can return the entity as aName 
       
   437 for the client to deal with.
       
   438 @param				aName is the name of the skipped entity.
       
   439 @param				aErrorCode is the error code.
       
   440 					If this is not KErrNone then special action may be required.
       
   441 */
       
   442 void CTestHandler::OnSkippedEntityL(const RString& aName, TInt aErrorCode)
       
   443 {
       
   444 	_LIT8(KOnSkipEntFuncName,"OnSkippedEntity()\r\n");	
       
   445 	_LIT8(KInfoOnEndPref,"\tSkipped entity: %S \r\n");
       
   446 	_LIT8(KInfoOnError,"Error occurs %d \r\n");
       
   447 	
       
   448 	iLog.Write(KOnSkipEntFuncName);
       
   449 
       
   450 	if (aErrorCode == KErrNone)
       
   451 	{
       
   452 		TBuf8<KShortInfoSize> info;
       
   453 		TBuf8<KShortInfoSize> info2;
       
   454 		
       
   455 		info2.Copy(aName.DesC());		
       
   456 		
       
   457 		info.Format(KInfoOnEndPref,&info2);
       
   458 	
       
   459 		iLog.Write(info);
       
   460 	}
       
   461 	else
       
   462 	{
       
   463 		TBuf8<KShortInfoSize> info;
       
   464 		info.Format(KInfoOnError,aErrorCode);
       
   465 		iLog.Write(info);
       
   466 	}	
       
   467 }
       
   468 
       
   469 /**
       
   470 This method is a receive notification of a processing instruction.
       
   471 @param				aTarget is the processing instruction target.
       
   472 @param				aData is the processing instruction data. If empty none was supplied.
       
   473 @param				aErrorCode is the error code.
       
   474 					If this is not KErrNone then special action may be required.
       
   475 */
       
   476 void CTestHandler::OnProcessingInstructionL(const TDesC8& aTarget, const TDesC8& aData, 
       
   477 										  TInt aErrorCode)
       
   478 {
       
   479 	_LIT8(KOnProcInstFuncName,"OnProcessingInstruction()\r\n");
       
   480 	_LIT8(KInfoOnEndEle,"\tProcessing instruction target: %S \t data: %S\r\n");
       
   481 	_LIT8(KInfoOnError,"Error occurs %d \r\n");
       
   482 
       
   483 	TBuf8<KShortInfoSize> info;
       
   484 	
       
   485 	iLog.Write(KOnProcInstFuncName);
       
   486 	
       
   487 	if (aErrorCode == KErrNone)
       
   488 	{
       
   489 		info.Format(KInfoOnEndEle,&aTarget,&aData);
       
   490 		iLog.Write(info);
       
   491 	}
       
   492 	else
       
   493 	{
       
   494 		info.Format(KInfoOnError,aErrorCode);
       
   495 		iLog.Write(info);
       
   496 	}
       
   497 }
       
   498 
       
   499 /**
       
   500 This method indicates an error has occurred.
       
   501 @param				aError is the error code
       
   502 */
       
   503 void CTestHandler::OnError(TInt aErrorCode)
       
   504 {
       
   505 	_LIT8(KOnErrorFuncName,"OnError()\r\n");
       
   506 	_LIT8(KInfoOnErrorFunc,"\tError function - error occurs %d \r\n");
       
   507 	
       
   508 	iLog.Write(KOnErrorFuncName);	
       
   509 	TBuf8<KShortInfoSize> info;
       
   510 	info.Format(KInfoOnErrorFunc,aErrorCode);
       
   511 	iLog.Write(info);
       
   512 }
       
   513 
       
   514 /**
       
   515 This method obtains the interface matching the specified uid.
       
   516 @return				0 if no interface matching the uid is found.
       
   517 					Otherwise, the this pointer cast to that interface.
       
   518 @param				aUid the uid identifying the required interface.
       
   519 */
       
   520 TAny* CTestHandler::GetExtendedInterface(const TInt32 aUid)
       
   521 	{
       
   522 	if (aUid == MXmlEngExtendedHandler::EExtInterfaceUid)
       
   523 		{
       
   524 		return static_cast<MXmlEngExtendedHandler*>(this);
       
   525 		}
       
   526 	else return 0;
       
   527 	}