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