williamr@2: // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). williamr@2: // All rights reserved. williamr@2: // This component and the accompanying materials are made available williamr@2: // under the terms of the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members williamr@2: // which accompanies this distribution, and is available williamr@2: // at the URL "http://www.symbianfoundation.org/legal/licencesv10.html". williamr@2: // williamr@2: // Initial Contributors: williamr@2: // Nokia Corporation - initial contribution. williamr@2: // williamr@2: // Contributors: williamr@2: // williamr@2: // Description: williamr@2: // This file contains the declaration of the generic CMDXMLParser class williamr@2: // which is responsible for creating a DOM structure williamr@2: // from a given XML file. williamr@2: // williamr@2: // williamr@2: williamr@2: williamr@2: williamr@2: /** williamr@2: @file williamr@2: */ williamr@2: williamr@2: #ifndef __GMXMLPARSER_H__ williamr@2: #define __GMXMLPARSER_H__ williamr@2: williamr@2: #include williamr@2: #include williamr@2: #include williamr@2: williamr@2: //forward reference williamr@2: class CMDXMLDocument; williamr@2: class CMDXMLEntityConverter; williamr@2: class CMDXMLElement; williamr@2: class MXMLDtd; williamr@2: williamr@2: williamr@2: williamr@2: class MMDXMLParserObserver williamr@2: /** Abstract observer interface for notification when XML parsing is complete. williamr@2: williamr@2: It should be implemented by users of CMDXMLParser williamr@2: @publishedAll williamr@2: @released*/ williamr@2: { williamr@2: public: williamr@2: /** williamr@2: Call back function used to inform a client of the Parser when a parsing operation completes. williamr@2: */ williamr@2: virtual void ParseFileCompleteL() = 0; williamr@2: }; williamr@2: williamr@2: class MMDXMLParserDataProvider williamr@2: /** Abstract data source interface for XML data source. williamr@2: williamr@2: The user of CMDXMLParser must build one of these to encapsulate the data source williamr@2: that they wish to parse. CMDXMLParser implements a file-based data source to williamr@2: implement the functionality of the ParseFile function. williamr@2: williamr@2: @publishedAll williamr@2: @released*/ williamr@2: { williamr@2: public: williamr@2: /** Status codes returned by GetData() implementations. */ williamr@2: enum TDataProviderResults williamr@2: { williamr@2: KMoreData, ///< Returned by the interface implementation when it is returning more data. williamr@2: KDataStreamError, ///< Returned by the interface when an unrecoverable error prevents obtaining more data. A recoverable error should be represented by KDataNotReady. williamr@2: KDataStreamEnd ///< Returned by the interface when there is no more data to come. williamr@2: }; williamr@2: williamr@2: public: williamr@2: /** williamr@2: The XML Parser calls this on a specific data provider to get more data williamr@2: when required. williamr@2: williamr@2: Note that the TPtrC supplied may be used by the parser at any time williamr@2: between the return of this call and the next call that the parser williamr@2: makes out. williamr@2: williamr@2: Your data provider must not move the data pointed to until the williamr@2: parser has indicated that it's done with that block by asking for williamr@2: another. williamr@2: williamr@2: Ownership of the data pointed to remains with the data provider. williamr@2: williamr@2: williamr@2: General comments on efficiency williamr@2: ------------------------------ williamr@2: williamr@2: The parser is designed such that it processes the whole data block williamr@2: provided in one go. It will automatically become asynchronous when williamr@2: another block is required - the data provider only needs to supply williamr@2: data. williamr@2: williamr@2: Because of this design, it allows the data provider to indirectly williamr@2: control the amount of processing time that will be needed williamr@2: in a single block. williamr@2: williamr@2: It is a good idea to balance the need for the fastest possible williamr@2: processing with the need for client application responsiveness by williamr@2: ensuring that the amount of data passed in a single block is not williamr@2: too large. However, it is worth bearing in mind that the parser williamr@2: will convert UTF8 data streams in blocks of 32 characters, and williamr@2: supplying blocks of smaller length than this will result in a williamr@2: slight loss of efficiency. williamr@2: williamr@2: @param aPtr On return, the data provided williamr@2: @param aStatus Asynchronous status to be completed by the function with a williamr@2: TDataProviderResults value williamr@2: */ williamr@2: virtual void GetData(TPtrC8 &aPtr, TRequestStatus &aStatus) = 0; williamr@2: /** williamr@2: Called to indicate that use of the data source is complete. williamr@2: */ williamr@2: virtual void Disconnect() = 0; williamr@2: }; williamr@2: williamr@2: class CMDXMLParserFileDataSource; williamr@2: williamr@2: class CMDXMLParser: public CActive williamr@2: /** Creates a DOM structure from a given XML file. williamr@2: williamr@2: The parsing operation is asynchronous and is initiated by a call to ParseFile(). williamr@2: On completion, the created DOM document can be retrieved through DetachXMLDoc(). williamr@2: williamr@2: Note the following ownership rules for the DOM document: williamr@2: williamr@2: 1. calling DetachXMLDoc() transfers ownership of the document to the client williamr@2: williamr@2: 2. if the parser is asked to parse a new file while it still owns an existing williamr@2: DOM document, it will delete the old document. williamr@2: williamr@2: @publishedAll williamr@2: @released williamr@2: */ williamr@2: { williamr@2: public: williamr@2: /** Allocates and constructs a new XML parser, specifying a DTD. williamr@2: williamr@2: @param aParserObserver XML parser observer williamr@2: @leave KErrNoMemory Out of memory williamr@2: @return New XML parser */ williamr@2: IMPORT_C static CMDXMLParser* NewL(MMDXMLParserObserver* aParserObserver); williamr@2: williamr@2: /** Allocates and constructs a new XML parser, specifying a DTD. williamr@2: williamr@2: @param aParserObserver XML parser observer williamr@2: @param aDtdRepresentation DTD validator williamr@2: @leave KErrNoMemory Out of memory williamr@2: @return New XML parser */ williamr@2: IMPORT_C static CMDXMLParser* NewL(MMDXMLParserObserver* aParserObserver, MXMLDtd* aDtdRepresentation); williamr@2: williamr@2: /** Allocates and constructs a new XML parser, leaving the object on the cleanup williamr@2: stack. williamr@2: williamr@2: @param aParserObserver XML parser observer williamr@2: @leave KErrNoMemory Out of memory williamr@2: @return New XML parser */ williamr@2: IMPORT_C static CMDXMLParser* NewLC(MMDXMLParserObserver* aParserObserver); williamr@2: williamr@2: /** Allocates and constructs a new XML parser, leaving the object on the cleanup williamr@2: stack. williamr@2: williamr@2: @param aParserObserver XML parser observer williamr@2: @param aDtdRepresentation DTD validator williamr@2: @leave KErrNoMemory Out of memory williamr@2: @return New XML parser */ williamr@2: IMPORT_C static CMDXMLParser* NewLC(MMDXMLParserObserver* aParserObserver, MXMLDtd* aDtdRepresentation); williamr@2: williamr@2: williamr@2: /** Destructor. */ williamr@2: IMPORT_C ~CMDXMLParser(); williamr@2: williamr@2: /** Gets the last error found by the parser. williamr@2: williamr@2: @return Error code williamr@2: */ williamr@2: IMPORT_C TInt Error() const; williamr@2: williamr@2: /** williamr@2: Get the severity of the most severe error found. williamr@2: @return the maximum error severity williamr@2: */ williamr@2: IMPORT_C TXMLErrorCodeSeverity ErrorSeverity() const; williamr@2: williamr@2: /** Gets the created DOM. williamr@2: williamr@2: This should be called after the conclusion of the parser process. williamr@2: williamr@2: Note that the function sets the internal variable pointing to the document williamr@2: to NULL, so this function can only be called once per file parse. The caller williamr@2: takes ownership of the document, and must delete it when its use is complete. williamr@2: williamr@2: @return The created DOM */ williamr@2: IMPORT_C CMDXMLDocument* DetachXMLDoc(); williamr@2: williamr@2: /** Parses a specified XML file into a DOM object tree. williamr@2: williamr@2: @param aRFs File server session williamr@2: @param aFileToParse The file name to parse williamr@2: @return KErrNone if success or a file read error code */ williamr@2: IMPORT_C TInt ParseFile(RFs aRFs, const TDesC& aFileToParse); williamr@2: williamr@2: IMPORT_C TInt ParseFile(RFile& aFileHandleToParse); williamr@2: williamr@2: /** Parses a specified XML Data Source into a DOM object tree. williamr@2: Use ParseSourceL() function in preference to ParseSource() williamr@2: @param aSource MMDXMLParserDataProvider pointer williamr@2: */ williamr@2: inline void ParseSource(MMDXMLParserDataProvider *aSource) williamr@2: { williamr@2: TRAP_IGNORE(ParseSourceL(aSource)); williamr@2: } williamr@2: williamr@2: /** Parses a specified XML Data Source into a DOM object tree. williamr@2: @param aSource MMDXMLParserDataProvider pointer williamr@2: */ williamr@2: IMPORT_C void ParseSourceL(MMDXMLParserDataProvider *aSource); williamr@2: williamr@2: /** Defines input stream character widths. */ williamr@2: enum TMDXMLParserInputCharWidth williamr@2: { williamr@2: EAscii = 0x01, ///< ASCII williamr@2: EUnicode = 0x02 /// iUnicodeConversion; // buffer to temporarily hold the results of conversion from UTF8 to Unicode williamr@2: TInt iUnicodeConversionLen; // number of characters stored in our intermediate buffer williamr@2: TInt iUnicodeReadPos; // next character to send from our intermediate buffer williamr@2: TBuf<1> iSpareChar; williamr@2: williamr@2: /* member variables used when parsing a local file */ williamr@2: TDesC *iFileToParse; williamr@2: RFs iRFs; williamr@2: RFile iFileHandleToParse; williamr@2: williamr@2: TBool iEndOfTag; williamr@2: williamr@2: /* member variables used in DoParseLoopL() */ williamr@2: TBool iOpened; williamr@2: TBool iClosed; williamr@2: CMDXMLElement* iNewElement; williamr@2: CMDXMLElement* iParentElement; williamr@2: HBufC* iText; williamr@2: enum EParserStates williamr@2: { williamr@2: KInitFromFile, williamr@2: KDetermineCharset, williamr@2: KWaitingForData, williamr@2: KParseData, williamr@2: KSpanDataGap, williamr@2: KFinished williamr@2: }; williamr@2: williamr@2: EParserStates iState; williamr@2: EParserStates iPreviousState; williamr@2: TInt iSuspiciousCharacter; williamr@2: TBool iStoreInvalid; // controls whether invalid elements and attributes are stored in the DOM. williamr@2: TBool iPreserve; williamr@2: williamr@2: }; williamr@2: williamr@2: #endif