xml/xmlfw/test/rtest/tsrc/contenthandlers.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 <e32test.h>
       
    17 #include <f32file.h>
       
    18 #include <stringpool.h>
       
    19 
       
    20 #include <xml/documentparameters.h>
       
    21 #include <xml/taginfo.h>
       
    22 #include <xml/attribute.h>
       
    23 
       
    24 #include "contenthandlers.h"
       
    25 
       
    26 using namespace Xml;
       
    27 
       
    28 GLREF_D RTest test;
       
    29 
       
    30 
       
    31 //
       
    32 // TRebuildingContentHandler
       
    33 //
       
    34 TRebuildingContentHandler::TRebuildingContentHandler(RFile& aOut)
       
    35 	: iError(KErrNone), iOutFile(aOut)
       
    36 	{
       
    37 	}
       
    38 
       
    39 void TRebuildingContentHandler::OnStartDocumentL(const RDocumentParameters& aDocParam, TInt aErrorCode)
       
    40 	{
       
    41 	User::LeaveIfError(aErrorCode);
       
    42 
       
    43 	iOutFile.Write(_L8("<?xml encoding=\""));
       
    44 	iOutFile.Write(aDocParam.CharacterSetName().DesC());
       
    45 	iOutFile.Write(_L8("\""));
       
    46 	iOutFile.Write(_L8("?>\n"));
       
    47 	}
       
    48 
       
    49 void TRebuildingContentHandler::OnEndDocumentL(TInt aErrorCode)
       
    50 	{
       
    51 	User::LeaveIfError(aErrorCode);
       
    52 	}
       
    53 	
       
    54 void TRebuildingContentHandler::OnStartElementL(const RTagInfo& aElement, const RAttributeArray& aAttributes, TInt aErrorCode)
       
    55 	{
       
    56 	User::LeaveIfError(aErrorCode);
       
    57 
       
    58 	const TDesC8& localPart8 = aElement.LocalName().DesC();
       
    59 	const TDesC8& prefix8 = aElement.Prefix().DesC();
       
    60 
       
    61 	if(prefix8.Length())
       
    62 		{
       
    63 		iOutFile.Write(_L8("<"));
       
    64 		iOutFile.Write(prefix8);
       
    65 		iOutFile.Write(_L8(":"));
       
    66 		iOutFile.Write(localPart8);
       
    67 		}
       
    68 	else
       
    69 		{
       
    70 		iOutFile.Write(_L8("<"));
       
    71 		iOutFile.Write(localPart8);
       
    72 		}	
       
    73 
       
    74 	TInt nAttributes = aAttributes.Count();
       
    75 	for(TInt i=0; i<nAttributes; i++)
       
    76 		{
       
    77 		const RAttribute& attribute = aAttributes[i];
       
    78 		const RTagInfo& nameInfo = attribute.Attribute();
       
    79 
       
    80 		const TDesC8& localPart8 = nameInfo.LocalName().DesC();
       
    81 		const TDesC8& prefix8 = nameInfo.Prefix().DesC();
       
    82 		const TDesC8& value8 = attribute.Value().DesC();
       
    83 
       
    84 		if(prefix8.Length())
       
    85 			{
       
    86 			iOutFile.Write(_L8(" "));
       
    87 			iOutFile.Write(prefix8);
       
    88 			iOutFile.Write(_L8(":"));
       
    89 			iOutFile.Write(localPart8);
       
    90 			}
       
    91 		else
       
    92 			{
       
    93 			iOutFile.Write(_L8(" "));
       
    94 			iOutFile.Write(localPart8);
       
    95 			}	
       
    96 
       
    97 		iOutFile.Write(_L8("=\""));
       
    98 		iOutFile.Write(value8);
       
    99 		iOutFile.Write(_L8("\""));
       
   100 		}
       
   101 
       
   102 	iOutFile.Write(_L8(">"));
       
   103 	}
       
   104 
       
   105 void TRebuildingContentHandler::OnEndElementL(const RTagInfo& aElement, TInt aErrorCode)
       
   106 	{
       
   107 	User::LeaveIfError(aErrorCode);
       
   108 
       
   109 	const TDesC8& localPart8 = aElement.LocalName().DesC();
       
   110 	const TDesC8& prefix8 = aElement.Prefix().DesC();
       
   111 
       
   112 	iOutFile.Write(_L8("</"));
       
   113 
       
   114 	if(prefix8.Length())
       
   115 		{
       
   116 		iOutFile.Write(prefix8);
       
   117 		iOutFile.Write(_L8(":"));
       
   118 		}
       
   119 	iOutFile.Write(localPart8);
       
   120 	iOutFile.Write(_L8(">"));
       
   121 	}
       
   122 
       
   123 void TRebuildingContentHandler::OnContentL(const TDesC8& aData8, TInt aErrorCode)
       
   124 	{
       
   125 	User::LeaveIfError(aErrorCode);
       
   126 	
       
   127 	_LIT8(K_gt8, "&gt;");
       
   128 	_LIT8(K_lt8, "&lt;");
       
   129 	_LIT8(K_amp8, "&amp;");
       
   130 	_LIT8(K_apos8, "&apos;");
       
   131 	_LIT8(K_quot8, "&quot;");
       
   132 
       
   133 	TInt newLength = 0;
       
   134 	TInt length = aData8.Length();
       
   135 
       
   136 	TInt i;
       
   137 	for(i=0; i<length; i++)
       
   138 		switch(aData8[i])
       
   139 			{
       
   140 			case '>':
       
   141 				newLength += K_gt8().Length();
       
   142 				break;
       
   143 			case '<':
       
   144 				newLength += K_lt8().Length();
       
   145 				break;
       
   146 			case '&':
       
   147 				newLength += K_amp8().Length();
       
   148 				break;
       
   149 			case '\"':
       
   150 				newLength += K_quot8().Length();
       
   151 				break;
       
   152 			case '\'':
       
   153 				newLength += K_apos8().Length();
       
   154 				break;
       
   155 			default:
       
   156 				newLength++;
       
   157 			}
       
   158 
       
   159 	TPtr8 out = HBufC8::NewLC(newLength)->Des();
       
   160 
       
   161 	TChar c;
       
   162 	for(i=0; i<length; i++)
       
   163 		switch(c=aData8[i])
       
   164 			{
       
   165 			case '>':
       
   166 				out.Append(K_gt8);
       
   167 				break;
       
   168 			case '<':
       
   169 				out.Append(K_lt8);
       
   170 				break;
       
   171 			case '&':
       
   172 				out.Append(K_amp8);
       
   173 				break;
       
   174 			case '\"':
       
   175 				out.Append(K_quot8);
       
   176 				break;
       
   177 			case '\'':
       
   178 				out.Append(K_apos8);
       
   179 				break;
       
   180 			default:
       
   181 				out.Append(c);
       
   182 			}
       
   183 
       
   184 	iOutFile.Write(out);
       
   185 
       
   186 	CleanupStack::PopAndDestroy(); // out
       
   187 	}
       
   188 
       
   189 void TRebuildingContentHandler::OnStartPrefixMappingL(const RString& /*aPrefix*/, const RString& /*aUri*/, TInt aErrorCode)
       
   190 	{
       
   191 	User::LeaveIfError(aErrorCode);
       
   192 	}
       
   193 
       
   194 void TRebuildingContentHandler::OnEndPrefixMappingL(const RString& /*aPrefix*/, TInt aErrorCode)
       
   195 	{
       
   196 	User::LeaveIfError(aErrorCode);
       
   197 	}
       
   198 
       
   199 void TRebuildingContentHandler::OnIgnorableWhiteSpaceL(const TDesC8& /*aBytes*/, TInt /*aErrorCode*/)
       
   200 	{
       
   201 	}
       
   202 
       
   203 void TRebuildingContentHandler::OnSkippedEntityL(const RString& /*aName*/, TInt /*aErrorCode*/)
       
   204 	{
       
   205 	}
       
   206 
       
   207 void TRebuildingContentHandler::OnProcessingInstructionL(const TDesC8& aTarget8, const TDesC8& aData8, TInt aErrorCode)
       
   208 	{
       
   209 	User::LeaveIfError(aErrorCode);
       
   210 
       
   211 	iOutFile.Write(_L8("<?"));
       
   212 	iOutFile.Write(aTarget8);
       
   213 	iOutFile.Write(_L8(" "));
       
   214 	iOutFile.Write(aData8);
       
   215 	iOutFile.Write(_L8("?>\n"));
       
   216 	}
       
   217 
       
   218 void TRebuildingContentHandler::OnExtensionL(const RString& /*aData*/, TInt /*aToken*/, TInt /*aErrorCode*/)
       
   219 	{
       
   220 	// Meaningless in XML
       
   221 	_LIT(KUnsupportedFeature,"Illegal call to TRebuildingContentHandler::OnExtensionL - aborting\n");
       
   222 	test.Panic(KErrNotSupported, KUnsupportedFeature);
       
   223 	}
       
   224 
       
   225 void TRebuildingContentHandler::OnError(TInt aError)
       
   226 	{
       
   227 	iError = aError;
       
   228 	test.Printf(_L("TRebuildingContentHandler::OnError - ERROR: code=%d - Aborting parsing process\n"), aError);
       
   229 	}
       
   230 
       
   231 TAny* TRebuildingContentHandler::GetExtendedInterface(const TInt32)
       
   232 	{
       
   233 	// do nothing.
       
   234 	return NULL;
       
   235 	}
       
   236 
       
   237 //
       
   238 // TSimpleContentHandler
       
   239 //
       
   240 const TInt TSimpleContentHandler::KExpectedLeaveCode = 1234;
       
   241 
       
   242 TSimpleContentHandler::TSimpleContentHandler()
       
   243 	: iLeaveOnStartElement(EFalse), iNumElements(0), iNumSkippedEntities(0), iNumPrefixMappings(0), iNumPrefixUnmappings(0), 
       
   244 		iError(KErrNone)
       
   245 	{
       
   246 	}
       
   247 
       
   248 void TSimpleContentHandler::OnStartDocumentL(const RDocumentParameters&, TInt)
       
   249 	{
       
   250 	//test.Printf(_L("<!-- TSimpleContentHandler::OnStartDocumentL -->\n"));
       
   251 
       
   252 	iNumElements = 0;
       
   253 	iNumSkippedEntities = 0;
       
   254 	iNumPrefixMappings = 0;
       
   255 	iNumPrefixUnmappings = 0;
       
   256 	}
       
   257 
       
   258 void TSimpleContentHandler::OnEndDocumentL(TInt)
       
   259 	{
       
   260 	//test.Printf(_L("\n<!-- TSimpleContentHandler::OnEndDocumentL -->\n"));
       
   261 
       
   262 	test(!iLeaveOnStartElement);
       
   263 	}
       
   264 	
       
   265 void TSimpleContentHandler::OnStartElementL(const RTagInfo&, const RAttributeArray&, TInt)
       
   266 	{
       
   267 	// test.Printf(_L("<!-- TSimpleContentHandler::OnStartElementL -->\n"));
       
   268 
       
   269 	if(iLeaveOnStartElement)
       
   270 		if(iNumElements++ == 0)
       
   271 			{
       
   272 			//test.Printf(_L("TSimpleContentHandler::OnStartElementL: Simulating Leave with code %d\n"), KExpectedLeaveCode);
       
   273 			User::Leave(KExpectedLeaveCode);
       
   274 			}
       
   275 		else
       
   276 			test(0);
       
   277 
       
   278 	iNumElements++;
       
   279 	}
       
   280 
       
   281 void TSimpleContentHandler::OnEndElementL(const RTagInfo&, TInt)
       
   282 	{
       
   283 	// test.Printf(_L("\n<!-- TSimpleContentHandler::OnEndElementL -->"));
       
   284 
       
   285 	test(!iLeaveOnStartElement);
       
   286 	}
       
   287 
       
   288 void TSimpleContentHandler::OnContentL(const TDesC8&, TInt)
       
   289 	{
       
   290 	// test.Printf(_L("\n<!-- TSimpleContentHandler::OnContentL -->"));
       
   291 
       
   292 	test(!iLeaveOnStartElement);
       
   293 	}
       
   294 
       
   295 void TSimpleContentHandler::OnProcessingInstructionL(const TDesC8&, const TDesC8&, TInt)
       
   296 	{
       
   297 	// test.Printf(_L("\n<!-- TSimpleContentHandler::OnProcessingInstructionL -->"));
       
   298 
       
   299 	test(!iLeaveOnStartElement || iNumElements==0);
       
   300 	}
       
   301 
       
   302 void TSimpleContentHandler::OnOutOfData()
       
   303 	{
       
   304 	_LIT(KUnexpectedOutOfData,"Unexpected call to TSimpleContentHandler::OnOutOfData - aborting\n");
       
   305 	test.Panic(KErrAbort, KUnexpectedOutOfData);
       
   306 	}
       
   307 
       
   308 void TSimpleContentHandler::OnError(TInt aError)
       
   309 	{
       
   310 	iError = aError;
       
   311 	//test.Printf(_L("TSimpleContentHandler::OnError - ERROR: code=%d\n"), aError);
       
   312 	}
       
   313 
       
   314 TAny* TSimpleContentHandler::GetExtendedInterface(const TInt32)
       
   315 	{
       
   316 	// do nothing.
       
   317 	return NULL;
       
   318 	}
       
   319 	
       
   320 void TSimpleContentHandler::OnStartPrefixMappingL(const RString&, const RString&, TInt)
       
   321 	{
       
   322 	iNumPrefixMappings++;
       
   323 	}
       
   324 
       
   325 void TSimpleContentHandler::OnEndPrefixMappingL(const RString&, TInt)
       
   326 	{
       
   327 	test(!iLeaveOnStartElement);
       
   328 	iNumPrefixUnmappings++;
       
   329 	}
       
   330 
       
   331 void TSimpleContentHandler::OnIgnorableWhiteSpaceL(const TDesC8&, TInt)
       
   332 	{
       
   333 	}
       
   334 
       
   335 void TSimpleContentHandler::OnSkippedEntityL(const RString&, TInt)
       
   336 	{
       
   337 	iNumSkippedEntities++;
       
   338 	}
       
   339 
       
   340 void TSimpleContentHandler::OnExtensionL(const RString&, TInt, TInt)
       
   341 	{
       
   342 	// Meaningless in XML
       
   343 	_LIT(KUnsupportedFeature,"Illegal call to TSimpleContentHandler::OnExtensionL - aborting\n");
       
   344 	test.Panic(KErrNotSupported, KUnsupportedFeature);
       
   345 	}
       
   346 
       
   347 
       
   348 //
       
   349 // TNamespaceContentHandler
       
   350 //
       
   351 TNamespaceContentHandler::TNamespaceContentHandler(const TDesC8& aDefaultUri, const TDesC8& aElementPrefix,
       
   352 		const TDesC8& aElementUri, const TDesC8& aAttributePrefix, const TDesC8& aAttributeUri)
       
   353 	: TSimpleContentHandler(), iDefaultUri(aDefaultUri), iElementPrefix(aElementPrefix), iElementUri(aElementUri),
       
   354 		iAttributePrefix(aAttributePrefix), iAttributeUri(aAttributeUri), iState(KNothingMapped)
       
   355 	{
       
   356 	}
       
   357 
       
   358 void TNamespaceContentHandler::OnStartElementL(const RTagInfo& aElement, const RAttributeArray& aAttributes, TInt aError)
       
   359 	{
       
   360 	TSimpleContentHandler::OnStartElementL(aElement, aAttributes, aError);
       
   361 
       
   362 	if(iState==KNothingMapped)
       
   363 		test(aElement.Prefix().DesC().Length()==0 && aElement.Uri().DesC().Length()==0);
       
   364 	else
       
   365 		 if(aElement.Prefix().DesC().Length()==0)
       
   366 			test(aElement.Uri().DesC()==iDefaultUri);
       
   367 		else
       
   368 			test(aElement.Prefix().DesC()==iElementPrefix && aElement.Uri().DesC()==iElementUri);
       
   369 
       
   370 	if(aAttributes.Count())
       
   371 		{
       
   372 		const RTagInfo& attribute = aAttributes[0].Attribute();
       
   373 
       
   374 		if(attribute.Prefix().DesC().Length()==0)
       
   375 			test(attribute.Uri().DesC().Length()==0);
       
   376 		else
       
   377 			{
       
   378 			test(iState!=KNothingMapped);
       
   379 			test(attribute.Prefix().DesC()==iAttributePrefix && attribute.Uri().DesC()==iAttributeUri);
       
   380 			}
       
   381 		}
       
   382 	}
       
   383 
       
   384 void TNamespaceContentHandler::OnEndElementL(const RTagInfo& aElement, TInt aError)
       
   385 	{
       
   386 	TSimpleContentHandler::OnEndElementL(aElement, aError);
       
   387 
       
   388 	if(iState==KNothingMapped)
       
   389 		test(aElement.Prefix().DesC().Length()==0 && aElement.Uri().DesC().Length()==0);
       
   390 	else
       
   391 		if(aElement.Prefix().DesC().Length()==0)
       
   392 			test(aElement.Uri().DesC()==iDefaultUri);
       
   393 		else
       
   394 			test(aElement.Prefix().DesC()==iElementPrefix && aElement.Uri().DesC()==iElementUri);
       
   395 	}
       
   396 
       
   397 void TNamespaceContentHandler::OnStartPrefixMappingL(const RString& aPrefix, const RString& aUri, TInt aError)
       
   398 	{
       
   399 	TSimpleContentHandler::OnStartPrefixMappingL(aPrefix, aUri, aError);
       
   400 
       
   401 	TPtrC8 prefix = aPrefix.DesC();
       
   402 	TPtrC8 uri = aUri.DesC();
       
   403 
       
   404 	switch(iState)
       
   405 		{
       
   406 		case KNothingMapped:
       
   407 			test(prefix.Length()==0 && uri==iDefaultUri);
       
   408 			iState = KDefaultNsMapped;
       
   409 			break;
       
   410 
       
   411 		case KDefaultNsMapped:
       
   412 			test(prefix==iElementPrefix && uri==iElementUri);
       
   413 			iState = KElementNsMapped;
       
   414 			break;
       
   415 
       
   416 		case KElementNsMapped:
       
   417 			test(prefix==iAttributePrefix && uri==iAttributeUri);
       
   418 			iState = KAttributeNsMapped;
       
   419 			break;
       
   420 
       
   421 		case KAttributeNsMapped:
       
   422 			test(0);
       
   423 		}
       
   424 	}
       
   425 
       
   426 void TNamespaceContentHandler::OnEndPrefixMappingL(const RString& aPrefix, TInt aError)
       
   427 	{
       
   428 	TSimpleContentHandler::OnEndPrefixMappingL(aPrefix, aError);
       
   429 
       
   430 	TPtrC8 prefix = aPrefix.DesC();
       
   431 
       
   432 	switch(iState)
       
   433 		{
       
   434 		case KAttributeNsMapped:
       
   435 			test(prefix==iAttributePrefix);
       
   436 			iState = KElementNsMapped;
       
   437 			break;
       
   438 
       
   439 		case KElementNsMapped:
       
   440 			test(prefix==iElementPrefix);
       
   441 			iState = KDefaultNsMapped;
       
   442 			break;
       
   443 
       
   444 		case KDefaultNsMapped:
       
   445 			test(prefix.Length()==0);
       
   446 			iState = KNothingMapped;
       
   447 			break;
       
   448 
       
   449 		case KNothingMapped:
       
   450 			test(0);
       
   451 		}
       
   452 	}
       
   453 
       
   454 
       
   455 
       
   456 void TCapsContentHandler::OnStartElementL(const RTagInfo& aElement, const RAttributeArray& aAttributes, TInt aError)
       
   457 	{
       
   458 	TSimpleContentHandler::OnStartElementL(aElement, aAttributes, aError);
       
   459 
       
   460 	// Test file should contain the following:
       
   461 	//		<PREFIX:NAME xlmns:PREFIX="HTTP://UPPERCASE.COM" 
       
   462 	//		xlmns:ATTPREFIX="HTTP://STILLUPPERCASE.COM" ATTPREFIX:ATTNAME="VALUE"/>
       
   463 
       
   464 	// Expat should deliver it to us as:
       
   465 	//		prefix, name, "HTTP://UPPERCASE.COM"
       
   466 	//		attprefix, attname, "HTTP://STILLUPPERCASE.COM", "VALUE"
       
   467 
       
   468 	_LIT8(KPrefix, "prefix");
       
   469 	_LIT8(KName, "name");
       
   470 	_LIT8(KUri, "HTTP://UPPERCASE.COM");
       
   471 	_LIT8(KAttprefix, "attprefix");
       
   472 	_LIT8(KAttname, "attname");
       
   473 	_LIT8(KAtturi, "HTTP://STILLUPPERCASE.COM");
       
   474 	_LIT8(KValue, "VALUE");
       
   475 
       
   476 	test(aElement.Prefix().DesC() == KPrefix);
       
   477 	test(aElement.LocalName().DesC() == KName);
       
   478 	test(aElement.Uri().DesC() == KUri);
       
   479 
       
   480 	test(aAttributes.Count() == 1);
       
   481 	const RAttribute& attribute = aAttributes[0];
       
   482 	const RTagInfo& nameInfo = attribute.Attribute();
       
   483 
       
   484 	test(nameInfo.Prefix().DesC() == KAttprefix);
       
   485 	test(nameInfo.LocalName().DesC() == KAttname);
       
   486 	test(nameInfo.Uri().DesC() == KAtturi);
       
   487 	test(attribute.Value().DesC() == KValue);
       
   488 	}
       
   489