messagingfw/wappushfw/ROAPTContentHandler/src/MessageComposer.cpp
changeset 62 db3f5fa34ec7
parent 0 8e480a14352b
equal deleted inserted replaced
60:9f5ae1728557 62:db3f5fa34ec7
       
     1 // Copyright (c) 2006-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 <f32file.h>
       
    17 #include <xml/documentparameters.h>
       
    18 #include <xml/taginfo.h>
       
    19 #include <xml/attribute.h>
       
    20 
       
    21 using namespace Xml;
       
    22 
       
    23 // System Include
       
    24 //For base64 encoding
       
    25 #include <tconvbase64.h>
       
    26 #include <xml/xmlparsererrors.h>
       
    27 #include <xml/xmlframeworkerrors.h>
       
    28 #include <xml/parserfeature.h>
       
    29 
       
    30 // User Include
       
    31 #include "MessageComposer.h"
       
    32 #include "errorcodes.h"
       
    33 
       
    34 const TInt KXmlBufferlength = 128;
       
    35 //text DRM MIME type
       
    36 _LIT(KDRMTextContentType, "application/vnd.oma.drm.rights+xml");
       
    37 
       
    38 //prefix and suffix to be appended.
       
    39 _LIT8(KXmlParserDataType, "text/xml");
       
    40 _LIT8(KWbxmlParserDataType, "text/wbxml");
       
    41 _LIT8(KOpeningBracket, "<");
       
    42 _LIT8(KClosingBracket, ">");
       
    43 _LIT8(KOpeningEndBracket, "</");
       
    44 _LIT8(KOpeningQuote, "=\"");
       
    45 _LIT8(KClosingQuote, "\"");
       
    46 _LIT8(KSpace," ");
       
    47 /** 
       
    48  * Constructor
       
    49  * Copy CPushMessage pointer for reference during parse operations 
       
    50  */
       
    51 CMessageComposer::CMessageComposer ( CPushMessage& aPushMessage )
       
    52 				: iPushMessage ( aPushMessage )
       
    53 	{
       
    54 	iIsKeyValue = EFalse;
       
    55 	}
       
    56 
       
    57 /** 
       
    58 * Destructor
       
    59 * Delete the parser object and close the buffer which store DRM message.
       
    60 */
       
    61 CMessageComposer::~CMessageComposer()
       
    62 	{
       
    63 	delete iParser;
       
    64 	iXmlBuf.Close();
       
    65 	}
       
    66 
       
    67 /** 
       
    68 */
       
    69 CMessageComposer* CMessageComposer::NewL( CPushMessage& aPushMessage)
       
    70 	{
       
    71 	CMessageComposer* self = new ( ELeave ) CMessageComposer ( aPushMessage );
       
    72 	CleanupStack::PushL ( self );
       
    73 	self->ConstructL ();
       
    74 	CleanupStack::Pop ( self );
       
    75 	return ( self );
       
    76 	}
       
    77 
       
    78 /** 
       
    79 */
       
    80 void CMessageComposer::ConstructL()
       
    81 	{
       
    82 	iXmlBuf.CreateL(KXmlBufferlength);
       
    83 	LoadPluginL ();
       
    84 	}
       
    85 
       
    86 /*
       
    87 * Load the plugin based on incoming message (XML or WBXML plugin)
       
    88 */
       
    89 void CMessageComposer::LoadPluginL ()
       
    90 	{
       
    91 	TPtrC contentType;
       
    92 	iPushMessage.GetContentType ( contentType );
       
    93 	iParser = CParser::NewL ( ( contentType.CompareF ( KDRMTextContentType ) ) ?
       
    94 				KWbxmlParserDataType() : KXmlParserDataType(), *this );		
       
    95 	}
       
    96 	
       
    97 /** 
       
    98 
       
    99 */
       
   100 const TDesC8& CMessageComposer::ParseMessageL ()
       
   101 	{
       
   102 	// Begin parsing
       
   103 	iParser->ParseBeginL ();
       
   104 	
       
   105 	// Get the message body and do parsing.
       
   106 	TPtrC8 msgBody;
       
   107 	if(iPushMessage.GetMessageBody ( msgBody ))
       
   108 		{
       
   109 		iParser->EnableFeature ( ERawContent );
       
   110 		iParser->ParseL ( msgBody );
       
   111 		}
       
   112 	else
       
   113 		{
       
   114 		//Leave with KErrNotFound error code as message body not found
       
   115 		User::Leave ( KErrNotFound );
       
   116 		}	
       
   117 	
       
   118 	// End parsing
       
   119 	iParser->ParseEndL ();
       
   120 	
       
   121 	return iXmlBuf;
       
   122 	}
       
   123 
       
   124 RStringPool& CMessageComposer::StringPool ()
       
   125 	{
       
   126 	return iParser->StringPool ();
       
   127 	}
       
   128 
       
   129 void CMessageComposer::OnStartDocumentL ( const Xml::RDocumentParameters&  /*aDocParam*/ , TInt aErrorCode )
       
   130 	{
       
   131 	User::LeaveIfError ( MapToWapXMLError ( aErrorCode ) );
       
   132 	}
       
   133 
       
   134 void CMessageComposer::OnEndDocumentL ( TInt aErrorCode )
       
   135 	{
       
   136 	User::LeaveIfError ( MapToWapXMLError ( aErrorCode ) );
       
   137 	}
       
   138 
       
   139 void CMessageComposer::OnStartElementL ( const Xml::RTagInfo& aElement, const Xml::RAttributeArray& aAttributes, TInt aErrorCode )
       
   140 	{	
       
   141 	User::LeaveIfError( MapToWapXMLError ( aErrorCode ) );
       
   142 	
       
   143 	if(aElement.LocalName().DesC().Compare(_L8("ds:KeyValue")) == 0)
       
   144 		{
       
   145 		iIsKeyValue = ETrue;
       
   146 		}
       
   147 
       
   148 	//Append '<' before the element
       
   149 	AppendDataL ( KOpeningBracket );
       
   150 	//Append the element
       
   151 	AppendDataL ( aElement.LocalName().DesC() );
       
   152 	
       
   153 	//Now append all the attributes and attribute values in loop
       
   154 	TInt nAttributes = aAttributes.Count();
       
   155 	for ( TInt i = 0; i < nAttributes; ++i )
       
   156 		{
       
   157 		const RAttribute& attribute = aAttributes[i];
       
   158 		const RTagInfo& nameInfo = attribute.Attribute();		
       
   159 		//Append a blanck space before appending attribute name
       
   160 		AppendDataL ( KSpace );
       
   161 		//Append the attribute name
       
   162 		AppendDataL ( nameInfo.LocalName().DesC() );
       
   163 		//Append '="' before appending attribute value
       
   164 		AppendDataL ( KOpeningQuote	);
       
   165 		//Append attribue Value
       
   166 		AppendDataL ( attribute.Value().DesC() );		
       
   167 		//Append '"' after the attribute value
       
   168 		AppendDataL ( KClosingQuote );
       
   169 		}//End of for loop, appended all attribures of this element		
       
   170 		//Append '>' after the element
       
   171 		AppendDataL ( KClosingBracket );
       
   172 	}
       
   173 
       
   174 void CMessageComposer::OnEndElementL ( const Xml::RTagInfo&  aElement , TInt aErrorCode )
       
   175 	{
       
   176 	User::LeaveIfError ( MapToWapXMLError ( aErrorCode ) );	
       
   177 
       
   178 	AppendDataL ( KOpeningEndBracket );
       
   179 	AppendDataL ( aElement.LocalName().DesC() );
       
   180 	AppendDataL ( KClosingBracket );
       
   181 	}
       
   182 
       
   183 void CMessageComposer::OnContentL ( const TDesC8& aBytes, TInt aErrorCode )
       
   184 	{
       
   185 	User::LeaveIfError ( MapToWapXMLError ( aErrorCode ) );
       
   186 
       
   187 	if(iIsKeyValue)
       
   188 		{
       
   189 		// Got to base64 encode the 128-bit encryption key.
       
   190 		iIsKeyValue = EFalse;
       
   191 		TBase64 codec;
       
   192 		TInt length = aBytes.Length();
       
   193 		// Calculate the length of the base64 encoded key and create a buffer of that length.
       
   194 		HBufC8* encodedBytes = HBufC8::NewLC(((length%3)>0)?((length/3)+length+3):((length/3)+length+2));
       
   195 		TPtr8 encodedBytesPtr(encodedBytes->Des());
       
   196 		codec.Encode(aBytes, encodedBytesPtr);
       
   197 		AppendDataL(*encodedBytes);
       
   198 		CleanupStack::PopAndDestroy(encodedBytes);		
       
   199 		}
       
   200 	else
       
   201 		{
       
   202 		//Append the content.
       
   203 		AppendDataL ( aBytes );
       
   204 		}		
       
   205 	}
       
   206 
       
   207 void CMessageComposer::OnStartPrefixMappingL ( const RString& /* aPrefix */, const RString& /* aUri */, TInt aErrorCode )
       
   208 	{
       
   209 	User::LeaveIfError ( MapToWapXMLError ( aErrorCode ) );
       
   210 	}
       
   211 
       
   212 void CMessageComposer::OnEndPrefixMappingL ( const RString& /* aPrefix */, TInt aErrorCode )
       
   213 	{
       
   214 	User::LeaveIfError ( MapToWapXMLError ( aErrorCode ) );
       
   215 	}
       
   216 
       
   217 void CMessageComposer::OnIgnorableWhiteSpaceL ( const TDesC8& /* aBytes */, TInt aErrorCode )
       
   218 	{
       
   219 	User::LeaveIfError ( MapToWapXMLError ( aErrorCode ) );	
       
   220 	}
       
   221 
       
   222 void CMessageComposer::OnSkippedEntityL ( const RString& /* aName */, TInt aErrorCode )
       
   223 	{
       
   224 	User::LeaveIfError ( MapToWapXMLError ( aErrorCode ) );
       
   225 	}
       
   226 
       
   227 void CMessageComposer::OnProcessingInstructionL ( const TDesC8& /* aTarget */, const TDesC8& /* aData */, TInt aErrorCode )
       
   228 	{
       
   229 	User::LeaveIfError ( MapToWapXMLError ( aErrorCode ) );
       
   230 	}
       
   231 
       
   232 void CMessageComposer::OnError ( TInt aErrorCode )
       
   233 	{
       
   234 	iLastError = MapToWapXMLError ( aErrorCode );
       
   235 	}
       
   236 
       
   237 TAny* CMessageComposer::GetExtendedInterface ( const TInt32 /* aUid */ )
       
   238 	{
       
   239 	return NULL;
       
   240 	}
       
   241 
       
   242 TInt CMessageComposer::LastError ()
       
   243 	{
       
   244 	return iLastError;
       
   245 	}
       
   246 
       
   247 void CMessageComposer::AppendDataL ( const TDesC8& aData )
       
   248 	{
       
   249 	TInt xmlBufMaxLen = iXmlBuf.MaxLength ();
       
   250 	TInt xmlBufLen = iXmlBuf.Length ();
       
   251 	TInt dataLen = aData.Length ();
       
   252 	if ( xmlBufMaxLen < ( xmlBufLen + dataLen ) )
       
   253 		{
       
   254 		// Need to allocate more space.
       
   255 		TInt lenToAllocate = (dataLen > KXmlBufferlength) ? dataLen : KXmlBufferlength;
       
   256 		iXmlBuf.ReAllocL ( xmlBufMaxLen + lenToAllocate );
       
   257 		}	
       
   258 	iXmlBuf.Append ( aData );
       
   259 	}
       
   260 	
       
   261 // Map the generic XML parser code to WAP XML errors. Description of the error code
       
   262 // has taken from expat.
       
   263 TInt CMessageComposer::MapToWapXMLError ( TInt aXmlErrorCode )
       
   264 	{
       
   265 	
       
   266 	TInt errorCode = aXmlErrorCode;
       
   267 	
       
   268 	switch ( aXmlErrorCode )
       
   269 		{
       
   270 		case EXmlParserError:	
       
   271 		case EXmlSyntax:			
       
   272 		errorCode = EWapErrGeneral;
       
   273 		break;
       
   274 		// The document contains no elements 
       
   275 		// (XML requires all documents to contain exactly one top-level element)..
       
   276 		case EXmlNoElements:
       
   277 		errorCode = EWapErrXmlLibMissingDocumentRootNode;
       
   278 		break;
       
   279 		
       
   280 		// Raised when an input byte could not properly be assigned to a character; 
       
   281 		// for example, a NUL byte (value 0) in a UTF-8 input stream.
       
   282 		case EXmlInvalidToken:
       
   283 		errorCode = EWapErrXmlLibIllegalTagName;
       
   284 		break;
       
   285 		
       
   286 		// Some token (such as a start tag) was not closed before the end of the stream or 
       
   287 		// the next token was encountered.
       
   288 		case EXmlUnclosedToken:
       
   289 		// An end tag did not match the innermost open start tag.
       
   290 		case EXmlTagMismatch:
       
   291 		errorCode = EWapErrXmlLibEndTagMismatch;
       
   292 		break;
       
   293 		
       
   294 		// An incomplete character was found in the input.
       
   295 		case EXmlPartialChar:
       
   296 		// A character reference referred to a character which is illegal in XML 
       
   297 		// (for example, character 0, or `&#0;').
       
   298 		case EXmlBadCharRef:
       
   299 		errorCode = EWapErrXmlLibInvalidCharacterReference;
       
   300 		break;		
       
   301 		
       
   302 		// An attribute was used more than once in a start tag.
       
   303 		case EXmlDuplicateAttribute:
       
   304 		errorCode = EWapErrXmlLibInvalidAttributeDeclaration;
       
   305 		break;
       
   306 		
       
   307 		// Something other than whitespace occurred after the document element.
       
   308 		case EXmlJunkAfterDocElement:		
       
   309 		// A parameter entity reference was found where it was not allowed.
       
   310 		case EXmlPeRef:
       
   311 		errorCode = EWapErrDocumentCorrupted;
       
   312 		break;
       
   313 		
       
   314 		// A reference was made to a entity which was not defined.
       
   315 		case EXmlUndefinedEntity:
       
   316 		// An entity reference contained another reference to the same entity; possibly via a 
       
   317 		// different name, and possibly indirectly.
       
   318 		case EXmlRecursiveEntity:		
       
   319 		case EXmlAsyncEntity:
       
   320 		// An entity reference in an attribute value referred to an external entity instead of an internal entity.
       
   321 		case EXmlAttributeExternalEntityRef:
       
   322 		case EXmlExternalEntityHandling:
       
   323 		errorCode = EWapErrXmlLibUnknownEntityReference;
       
   324 		break;
       
   325 
       
   326 		// An entity reference referred to an entity which was declared with a notation, 
       
   327 		// so cannot be parsed.
       
   328 		case EXmlBinaryEntityRef:
       
   329 		errorCode = EWapErrDocumentCorrupted;
       
   330 		break;
       
   331 		
       
   332 		// An XML declaration was found somewhere other than the start of the input data.				
       
   333 		case EXmlMisplacedPi:
       
   334 		case EXmlIncorrectEncoding:
       
   335 		errorCode = EWapErrXmlLibInvalidDocument;
       
   336 		break;
       
   337 		// The document encoding is not supported by Expat.				
       
   338 		case EXmlUnknownEncoding:
       
   339 		errorCode = EWapErrUnknownDocument; 
       
   340 		break;		
       
   341 		
       
   342 		// A CDATA marked section was not closed.
       
   343 		case EXmlUnclosedCdata:
       
   344 		errorCode = EWapErrXmlLibMissingCDATASectionEndTag;
       
   345 		break;
       
   346 
       
   347 		// The parser determined that the document was not ``standalone'' 
       
   348 		// though it declared itself to be in the XML declaration, and the NotStandaloneHandler 
       
   349 		// was set and returned 0.
       
   350 		case EXmlNotStandalone:
       
   351 		case EXmlUnexpectedState:
       
   352 		case EXmlEntityDeclInPe:
       
   353 		errorCode = EWapErrUnknownDocument;
       
   354 		break;
       
   355 		
       
   356 		case EXmlDtdRequired:
       
   357 		errorCode = EWapErrDTDUnavailable;
       
   358 		break;
       
   359 		
       
   360 		// A behavioral change was requested after parsing started that can only be changed 
       
   361 		// before parsing has started. This is (currently) only raised by UseForeignDTD().
       
   362 		case EXmlFeatureLockedWhileParsing:
       
   363 		errorCode = EWapErrGeneral;
       
   364 		break;
       
   365 		
       
   366 		// Xml framework errors
       
   367 		case KErrXmlStringDictionaryPluginNotFound:
       
   368 		case KErrXmlParserPluginNotFound:
       
   369 		case KErrXmlGeneratorPluginNotFound:
       
   370 		case KErrXmlPluginNotFound:
       
   371 		errorCode = EWapErrPluginNotFound;
       
   372 		break;
       
   373 
       
   374 		case KErrXmlBadCharacterConversion:
       
   375 		case KErrXmlUnsupportedCharacterSet:
       
   376 		case KErrXmlUnavailableCharacterSet:
       
   377 		errorCode = EWapErrXmlLibInvalidCharacterReference;
       
   378 		break;
       
   379 		
       
   380 		// MStringDictionary errors ( mainly wbxml errors )
       
   381 		case KErrXmlUnsupportedElement:
       
   382 		errorCode = EWapErrXmlLibIllegalTagName;
       
   383 		break;
       
   384 		
       
   385 		case KErrXmlUnsupportedAttribute:
       
   386 		errorCode = EWapErrXmlLibMissingRequiredAttribute;
       
   387 		break;
       
   388 		
       
   389 		case KErrXmlUnsupportedAttributeValue:
       
   390 		errorCode = EWapErrXmlLibIllegalAttributeValue;
       
   391 		break;
       
   392 
       
   393 		case KErrXmlMissingStringDictionary:
       
   394 		errorCode = EWapErrXmlLibMissingDocument;
       
   395 		break;
       
   396 
       
   397 		case KErrXmlUnsupportedDocumentVersion:
       
   398 		errorCode = EWapErrXmlLibInvalidDocumentStructure;
       
   399 		break;
       
   400 		
       
   401 		case KErrXmlDocumentCorrupt:
       
   402 		errorCode = EWapErrDocumentCorrupted;
       
   403 		break;
       
   404 		
       
   405 		case KErrXmlStringPoolTableNotFound:
       
   406 		case KErrXmlBadIndex:
       
   407 		case KErrXmlUnsupportedExtInterface:
       
   408 		case KErrXmlLast:
       
   409 		errorCode = EWapErrGeneral;
       
   410 		break;
       
   411 		
       
   412 		default:
       
   413 		// Do nothing. any other kind of error.
       
   414 		break;
       
   415 		}
       
   416 	return errorCode;
       
   417 	}