xml/xmlfw/src/xmlframework/parser.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 <f32file.h>
       
    17 
       
    18 #include <xml/parser.h>
       
    19 #include <xml/matchdata.h>
       
    20 
       
    21 #include "parserimpl.h"
       
    22 
       
    23 
       
    24 using namespace Xml;
       
    25 
       
    26 
       
    27 CParser::CParser() : CBase() , iImpl(0)
       
    28 	{
       
    29 	}
       
    30 
       
    31 /**  This method creates a parser that is ready to parse documents 
       
    32 of the specified mime type.
       
    33 
       
    34 If there are multiple parser plugins in the system which can parse the 
       
    35 mime type, the XML framework will choose a parser. 
       
    36 
       
    37 The criteria used to choose a parser, from many matching parsers, is as follows:
       
    38 
       
    39 - A Symbian-supplied parser (with variant field set to "Symbian") will be selected by default. 
       
    40 If there are multiple Symbian-supplied parsers, the one with the lowest Uid will be selected. 
       
    41 Otherwise, the non-Symbian parser with the lowest Uid will be selected.
       
    42 
       
    43 @return	A constructed parser
       
    44 
       
    45 @param	aParserMimeType The mime type of the documents to parse (e.g. "txt/xml").
       
    46 @param	aCallback 		The handler for parser generated events.
       
    47 
       
    48 @leave KErrNoMemory If there is not enough memory to create parser or
       
    49 					one of system wide error codes. 
       
    50 					
       
    51 @leave KErrXmlParserPluginNotFound
       
    52 					If Xml framework is unable to find a parser 	
       
    53 					based on data provided in aParserMimeType.
       
    54 					
       
    55 @leave ...      One of the system wide error codes or one of the Xml 
       
    56                 specific ones defined in XmlFrameworkErrors.h
       
    57 @see CParser::NewL(const CMatchData& aCriteria, MContentHandler& aCallback)
       
    58 */
       
    59 EXPORT_C CParser* CParser::NewL(const TDesC8& aParserMimeType, MContentHandler& aCallback)
       
    60 	{
       
    61 	CParser* self=NewLC(aParserMimeType, aCallback);
       
    62 	CleanupStack::Pop(self);
       
    63 	return self;
       
    64 	}
       
    65 
       
    66 /**  This method is similar to NewL, but leaves the created parser on the
       
    67 cleanup stack.
       
    68 
       
    69 @return	A constructed parser
       
    70 
       
    71 @param	aParserMimeType The mime type of the documents to parse (e.g. "txt/xml").
       
    72 @param	aCallback 		The handler for parser generated events.
       
    73 
       
    74 @leave KErrNoMemory If there is not enough memory to create parser or 
       
    75 					one of system wide error codes. 
       
    76 					
       
    77 @leave KErrXmlParserPluginNotFound
       
    78 					If Xml framework is unable to find a parser 	
       
    79 					based on data provided in aParserMimeType.
       
    80 		
       
    81 @leave ...      One of the system wide error codes or one of the Xml 
       
    82                 specific ones defined in XmlFrameworkErrors.h
       
    83 @see CParser::NewL(const TDesC8& aParserMimeType, MContentHandler& aCallback)		
       
    84 @see CParser::NewLC(const CMatchData& aCriteria, MContentHandler& aCallback)					
       
    85 */
       
    86 EXPORT_C CParser* CParser::NewLC(const TDesC8& aParserMimeType, MContentHandler& aCallback)
       
    87 	{
       
    88 	CParser* self = new (ELeave) CParser;
       
    89 	CleanupStack::PushL(self);
       
    90 	self->ConstructL(aParserMimeType, aCallback);
       
    91 	return self;
       
    92 	}
       
    93 		
       
    94 /**  
       
    95 This method constructs the object with default settings
       
    96 
       
    97 @param aParserMimeType Parser Mime type
       
    98 @param aCallback Event handler
       
    99 
       
   100 @leave ...      One of the system wide error codes or one of the Xml 
       
   101                 specific ones defined in XmlFrameworkErrors.h
       
   102 @internalComponent
       
   103 */
       
   104 		
       
   105 void CParser::ConstructL(const TDesC8& aParserMimeType, MContentHandler& aCallback)
       
   106 	{
       
   107 	// create parser implementaion class if or created yet. 
       
   108 	if (!iImpl)
       
   109 		{
       
   110 		iImpl = new (ELeave) TParserImpl;
       
   111 		}
       
   112 		
       
   113 	// create CMatchData object with default parameters and adequate mime type
       
   114 	CMatchData* matchData = CMatchData::NewLC();
       
   115 	matchData->SetMimeTypeL(aParserMimeType);
       
   116 	iImpl->OpenL(*matchData, aCallback);
       
   117 	CleanupStack::PopAndDestroy(matchData);
       
   118 	} 
       
   119 
       
   120 /**  
       
   121 This method constructs the object according to the details specified in CMatchData object
       
   122 
       
   123 @param aMatchData Detailed parser information 
       
   124 @param aCallback Event handler
       
   125 
       
   126 @leave ...      One of the system wide error codes or one of the Xml 
       
   127                 specific ones defined in XmlFrameworkErrors.h
       
   128 @internalComponent
       
   129 */
       
   130 void CParser::ConstructL(const CMatchData& aMatchData, MContentHandler& aCallback)
       
   131 	{
       
   132 	if (!iImpl)
       
   133 		{
       
   134 		iImpl = new (ELeave) TParserImpl;
       
   135 		}
       
   136 	iImpl->OpenL(aMatchData, aCallback);
       
   137 	}
       
   138 	
       
   139 /**  This method is the destructor for the object.*/
       
   140 CParser::~CParser()
       
   141 	{
       
   142 	if (iImpl)
       
   143 		{
       
   144 		iImpl->Close();
       
   145 		delete iImpl;
       
   146 		iImpl = NULL;
       
   147 		}
       
   148 	}
       
   149 
       
   150 
       
   151 const TUint  KDefChunkSize				= 0x100;
       
   152 
       
   153 
       
   154 /** 
       
   155 This convenience Xml function may be used to parse the file named in aFilename 
       
   156 using the supplied parser. It reads the file in chunks and passes these to 
       
   157 CParser::ParseL(). When the end of file is reach CParser::ParseEndL() is called.
       
   158 It assumes the caller has successfully created a valid parser with a valid
       
   159 MContentHandler reference and has called ParserBeginL().
       
   160 
       
   161 @param aParser   A valid CParser object to perform the parsing which has 
       
   162                  already had ParseBeginL() called.
       
   163 @param aFs       The file server session to use
       
   164 @param aFilename The filename of the file to open, read and parse
       
   165 @leave ...       One of the system wide error codes or one of the Xml 
       
   166                  specific ones defined in XmlFrameworkErrors.h
       
   167 */
       
   168 EXPORT_C void Xml::ParseL(CParser& aParser, RFs& aFs, const TDesC& aFilename)
       
   169 	{
       
   170 	RFile file;
       
   171 	
       
   172 	User::LeaveIfError(file.Open(aFs, aFilename, EFileRead|EFileShareReadersOnly));
       
   173 	CleanupClosePushL(file);
       
   174 	
       
   175 	TBuf8<KDefChunkSize> data;
       
   176 	User::LeaveIfError(file.Read(data,:: KDefChunkSize));
       
   177 	TUint length = data.Length();
       
   178 
       
   179 	while (length)
       
   180 		{
       
   181 		aParser.ParseL(data);
       
   182 		User::LeaveIfError(file.Read(data, KDefChunkSize));
       
   183 		length = data.Length();
       
   184 		}
       
   185 	aParser.ParseEndL();
       
   186 	CleanupStack::PopAndDestroy(&file);
       
   187 	}
       
   188 
       
   189 /** 
       
   190 This convenience Xml function may be used to parse the file held in aFile 
       
   191 using the supplied parser. It reads the file in chunks and passes these to 
       
   192 CParser::ParseL(). When the end of file is reach CParser::ParseEndL() is called.
       
   193 It assumes the caller has successfully created a valid parser with a valid
       
   194 MContentHandler reference and has called ParserBeginL().
       
   195 
       
   196 @param aParser   A valid CParser object to perform the parsing which has 
       
   197                  already had ParseBeginL() called.
       
   198 @param aFilename Open file handle to the file to read and parse
       
   199 @leave ...       One of the system wide error codes or one of the Xml 
       
   200                  specific ones defined in XmlFrameworkErrors.h
       
   201 */
       
   202 EXPORT_C void Xml::ParseL(CParser& aParser, RFile& aFile)
       
   203 	{
       
   204 	TBuf8<KDefChunkSize> data;
       
   205 	User::LeaveIfError(aFile.Read(data,:: KDefChunkSize));
       
   206 	TUint length = data.Length();
       
   207 
       
   208 	while (length)
       
   209 		{
       
   210 		aParser.ParseL(data);
       
   211 		User::LeaveIfError(aFile.Read(data, KDefChunkSize));
       
   212 		length = data.Length();
       
   213 		}
       
   214 	aParser.ParseEndL();
       
   215 	} 
       
   216 
       
   217 /** 
       
   218 This convenience Xml function may be used to parse the XML document held in a
       
   219 descriptor in memory using the supplied parser. This variant function does call 
       
   220 ParseBeginL() to reset the parser to the default parser as selected for the 
       
   221 mime type given at creation time. It passes the entire descriptor into  
       
   222 CParser::ParseL(). When this returns it calls CParser::ParseEndL().
       
   223 It assumes the caller has successfully created a valid parser with a valid
       
   224 MContentHandler reference.
       
   225 
       
   226 This API can be used when parsing different kinds of documents with
       
   227 the majority being parsed associated with the default parser/mime type.
       
   228 
       
   229 @param aParser   A valid CParser object to perform the parsing
       
   230 @param aContent  The entire XML document content to parse
       
   231 @leave ...       One of the system wide error codes or one of the Xml 
       
   232                  specific ones defined in XmlFrameworkErrors.h
       
   233 */
       
   234 EXPORT_C void Xml::ParseL(CParser& aParser, const TDesC8& aContent)
       
   235 	{
       
   236 	aParser.ParseBeginL(); // resets to default mime type
       
   237 	aParser.ParseL(aContent);
       
   238 	aParser.ParseEndL();
       
   239 	}
       
   240 
       
   241 
       
   242 /** This method tells the parser that we're going to start parsing a document
       
   243 using the parser associated with this mime type.
       
   244 
       
   245 @param aDocumentMimeType the mime type of the document
       
   246 */
       
   247 EXPORT_C void CParser::ParseBeginL(const TDesC8& aDocumentMimeType)
       
   248 	{
       
   249 	// create CMatchData object with default parameters and adequate mime type
       
   250 	CMatchData* matchData = CMatchData::NewLC();
       
   251 	matchData->SetMimeTypeL(aDocumentMimeType);
       
   252 	iImpl->SetMimeTypeL(*matchData);
       
   253 	CleanupStack::PopAndDestroy();
       
   254 	}
       
   255 
       
   256 /** This method tells the parser that we're going to start parsing a document 
       
   257 using the default mime type specified on construction.
       
   258 
       
   259 The processor chain and features will be cleared if the parser currently 
       
   260 set is not the default, all old features are removed as these generally 
       
   261 have no meaning between parsers.
       
   262 
       
   263 @leave ...       One of the system wide error codes or one of the Xml 
       
   264                  specific ones defined in XmlFrameworkErrors.h
       
   265 */
       
   266 EXPORT_C void CParser::ParseBeginL()
       
   267 	{
       
   268 	iImpl->ResetMimeTypeL();
       
   269 	}
       
   270 
       
   271 
       
   272 /** This method tells the parser to parse a fragment of a document. 
       
   273 Could be the whole document. ParseEndL should be called once the whole document has
       
   274 been parsed. 
       
   275 
       
   276 The parser currently set will be used.
       
   277 
       
   278 @param aFragment the fragment to parse
       
   279 @leave ...       One of the system wide error codes or one of the Xml 
       
   280                  specific ones defined in XmlFrameworkErrors.h
       
   281 */
       
   282 EXPORT_C void CParser::ParseL(const TDesC8& aFragment)
       
   283 	{
       
   284 	iImpl->ParseChunkL(aFragment);
       
   285 	}
       
   286 
       
   287 
       
   288 /** This method tells the parser that we've finished parsing the current document
       
   289 and should be called after calling CParser::ParseL for the final time,
       
   290 as this will initiate error callbacks via MContentHandler, and clean up memory
       
   291 where appropriate, should an error have occured during the parsing process.
       
   292 Such an error could occur when trying to parse a truncated document.
       
   293 @leave ...       One of the system wide error codes or one of the Xml 
       
   294                  specific ones defined in XmlFrameworkErrors.h
       
   295 */
       
   296 EXPORT_C void CParser::ParseEndL()
       
   297 	{
       
   298 	iImpl->ParseLastChunkL(_L8(""));
       
   299 	}
       
   300 	
       
   301 	
       
   302 	
       
   303 /** This method changes the client and plugin chain.
       
   304 
       
   305 @param			aCallback the client at the end of the callback
       
   306 				chain that is to receive the parsed document information.
       
   307 @param			aPlugins a list of plugin implementation uids that
       
   308 				make up the callback chain.
       
   309 @leave ...      One of the system wide error codes or one of the Xml 
       
   310                 specific ones defined in XmlFrameworkErrors.h
       
   311 */
       
   312 EXPORT_C void CParser::SetProcessorChainL(const RContentProcessorUids& aPlugins)
       
   313 	{
       
   314 	iImpl->SetProcessorChainL(aPlugins);
       
   315 	}	
       
   316 	
       
   317 	
       
   318 /** This method enables a specific feature of the parser.
       
   319 
       
   320 @return KErrNone if successful, KErrNotSupported if the parser doesn't support the feature.
       
   321 @param aParserFeature The parser feature that must be enabled.
       
   322 @see TParserFeature
       
   323 */
       
   324 EXPORT_C TInt CParser::EnableFeature(TInt aParserFeature)
       
   325 	{
       
   326 	return iImpl->EnableFeature(aParserFeature);
       
   327 	}
       
   328 
       
   329 /** This method disables a specific feature of the parser.
       
   330 
       
   331 @return	KErrNone if successful, KErrNotSupported if the feature can't be disabled.
       
   332 @param aParserFeature The parser feature that must be disabled.
       
   333 @see TParserFeature 
       
   334 */
       
   335 EXPORT_C TInt CParser::DisableFeature(TInt aParserFeature)
       
   336 	{
       
   337 	return iImpl->DisableFeature(aParserFeature);
       
   338 	}
       
   339 
       
   340 /** This method tell whether a specific feature of the parser is enabled.
       
   341 @return	True if the feature is enabled.
       
   342 @see				TParserFeature 
       
   343 */
       
   344 EXPORT_C TBool CParser::IsFeatureEnabled(TInt aParserFeature) const
       
   345 	{
       
   346 	return iImpl->IsFeatureEnabled(aParserFeature);
       
   347 	}
       
   348 
       
   349 
       
   350 
       
   351 /** This method preloads a string dictionary prior to parsing.
       
   352 
       
   353 @param			aPublicId  the public identifier representing the 
       
   354 				document dtd. 
       
   355 @leave ...      One of the system wide error codes or one of the Xml 
       
   356                 specific ones defined in XmlFrameworkErrors.h
       
   357 */
       
   358 EXPORT_C void CParser::AddPreloadedDictionaryL(const TDesC8& aPublicId)
       
   359 	{
       
   360 	iImpl->AddPreloadedDictionaryL(aPublicId);
       
   361 	}
       
   362 	
       
   363 
       
   364 
       
   365 /** This method obtains a handle to the current string pool.
       
   366 
       
   367 @return				handle to the current string pool.
       
   368 */
       
   369 EXPORT_C RStringPool& CParser::StringPool()
       
   370 	{
       
   371 	return iImpl->StringPool();
       
   372 	}
       
   373 
       
   374 
       
   375 /** This method obtains a handle to the current StringDictionaryCollection.
       
   376 
       
   377 @return				handle to the current StringDictionaryCollection.
       
   378 */
       
   379 EXPORT_C RStringDictionaryCollection& CParser::StringDictionaryCollection()
       
   380 	{
       
   381 	return iImpl->StringDictionaryCollection();
       
   382 	}
       
   383 
       
   384 
       
   385 /**  This method creates the particular parser specified in CMatchData parameter.
       
   386 
       
   387 The parser plugin resolution process is based on mime type and variant field. 
       
   388 Both are provided in CMatchData parameter. Mime Type is a mandatory string 
       
   389 for the resolution process and it is matched against the data field of plugin resource files. 
       
   390 Variant string is optional. If it exists, it is matched against the first entry 
       
   391 of the opaque data field of plugin resource files. 	
       
   392 	
       
   393 If the query is narrowed down to many parsers, the XML framework might either leave with 
       
   394 an error (KErrXmlMoreThanOneParserMatched), or choose a parser. The behaviour is specified 
       
   395 by LeaveOnMany flag. The default value of the flag is FALSE ('choose a parser' behaviour). 
       
   396 
       
   397 The criteria used to choose a parser, from many matching parsers, is as follows:
       
   398 	
       
   399 - If the optional Variant field is specified, the XML framework will choose the parser with
       
   400 the lowest Uid from the list. 
       
   401 
       
   402 - If the optional Variant field is not specified, a Symbian-supplied parser (with variant 
       
   403 field set to "Symbian") will be selected by default. If there are multiple Symbian-supplied 
       
   404 parsers, the one with the lowest Uid will be selected. Otherwise, the non-Symbian parser 
       
   405 with the lowest Uid will be selected.
       
   406 
       
   407 Case sensitivity of the string matching process is applied according to the relevant flag 
       
   408 in CMatchData. 
       
   409 The default value is TRUE (Case Sensitivity enabled). 
       
   410 
       
   411 Only ROM-based parsers are returned if the relevant flag is set in CMatchData.
       
   412 The default value is FALSE (all parsers are considered).
       
   413 	
       
   414 @return	A constructed parser
       
   415 
       
   416 @param	aCriteria 	The specific information about required parser 
       
   417 					(mime type, variant data).
       
   418 @param	aCallback 	The xml/wbxml event handler.
       
   419 
       
   420 @leave KErrNoMemory If there is not enough memory to create parser or 
       
   421 					one of system wide error codes. 
       
   422 
       
   423 @leave KErrXmlParserPluginNotFound 	
       
   424 					If Xml framework is unable to find a parser 	
       
   425 					based on data provided in CMatchData.
       
   426 								
       
   427 @leave KErrXmlMoreThanOneParserMatched 	
       
   428 					If Xml framework narrowed down the query 
       
   429 					to many parsers and a user requested to leave 
       
   430 					in such case (LeaveOnMany flag set). 
       
   431 
       
   432 @leave ...      One of the system wide error codes or one of the Xml 
       
   433                 specific ones defined in XmlFrameworkErrors.h
       
   434 @see CMatchData
       
   435 */
       
   436 EXPORT_C CParser* CParser::NewL(const CMatchData& aCriteria, MContentHandler& aCallback)
       
   437 	{
       
   438 	CParser* me = CParser::NewLC(aCriteria, aCallback);
       
   439 	CleanupStack::Pop(me);
       
   440 	return me;
       
   441 	}
       
   442 	
       
   443 /** This method creates the particular parser specified in CMatchData parameter.
       
   444 It performs the same way as NewL with the exception that it leaves the object 
       
   445 on the cleanup stack. 
       
   446 
       
   447 @return	A constructed parser
       
   448 
       
   449 @param	aCriteria 	The specific information about required parser 
       
   450 					(mime type, version, variant data).
       
   451 @param	aCallback 	The xml/wbxml event handler.
       
   452 
       
   453 @leave KErrNoMemory If there is not enough memory to create parser or 
       
   454 					one of system wide error codes. 
       
   455 
       
   456 @leave KErrXmlParserPluginNotFound 	
       
   457 					If Xml framework is unable to find a parser 	
       
   458 					based on data provided in CMatchData.
       
   459 								
       
   460 @leave KErrXmlMoreThanOneParserMatched 	
       
   461 					If Xml framework narrowed down the query 
       
   462 					to many parsers and a user requested to leave 
       
   463 					in such case (LeaveOnMany flag set). 
       
   464 
       
   465 @leave ...      One of the system wide error codes or one of the Xml 
       
   466                 specific ones defined in XmlFrameworkErrors.h
       
   467 @see CParser::NewL(const CMatchData& aCriteria, MContentHandler& aCallback)
       
   468 @see CMatchData
       
   469 */
       
   470 EXPORT_C CParser* CParser::NewLC(const CMatchData& aCriteria, MContentHandler& aCallback)
       
   471 	{
       
   472 	CParser* me = new (ELeave) CParser();
       
   473 	CleanupStack::PushL(me);
       
   474 	me->ConstructL(aCriteria, aCallback);
       
   475 	return me;
       
   476 	}
       
   477 
       
   478 /** This method tells the parser that we're going to start parsing a document
       
   479 using the parser associated with given CMatchData criteria.
       
   480 
       
   481 @param aCriteria 		The specific information about required parser 
       
   482 					(mime type, version, variant data).
       
   483 
       
   484 
       
   485 @leave KErrNoMemory 	If there is not enough memory to create parser.
       
   486 
       
   487 @leave KErrArgument 	If the data specified in CMatchData are not sufficient.
       
   488 
       
   489 @leave KErrXmlParserPluginNotFound 	
       
   490 					If Xml framework is unable to find a parser 	
       
   491 					based on data provided in CMatchData.
       
   492 								
       
   493 @leave KErrXmlMoreThanOneParserMatched 	
       
   494 					If Xml framework narrowed down the query 
       
   495 					to many parsers and a user requested to leave 
       
   496 					in such case (LeaveOnMany flag set). 
       
   497 					
       
   498 @leave ...      One of the system wide error codes or one of the Xml 
       
   499                 specific ones defined in XmlFrameworkErrors.h
       
   500 @see CMatchData					
       
   501 */
       
   502 EXPORT_C void CParser::ParseBeginL(const CMatchData& aCriteria)
       
   503 	{
       
   504 	iImpl->SetMimeTypeL(aCriteria);
       
   505 	}
       
   506 
       
   507