contentctrl_plat/ds_folder_util_api/inc/nsmlfolderparser.h
changeset 1 95fdac6ccb5c
equal deleted inserted replaced
0:dab8a81a92de 1:95fdac6ccb5c
       
     1 /*
       
     2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Folder XML parser
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // 1.2 Changes: nsmlxmlparser module added
       
    20 
       
    21 
       
    22 #ifndef __NSMLFOLDERPARSER_H__
       
    23 #define __NSMLFOLDERPARSER_H__
       
    24 
       
    25 // ------------------------------------------------------------------------------------------------
       
    26 // Includes
       
    27 // ------------------------------------------------------------------------------------------------
       
    28 #include <e32base.h>
       
    29 #include <s32strm.h>
       
    30 
       
    31 // ------------------------------------------------------------------------------------------------
       
    32 // Class forward declarations
       
    33 // ------------------------------------------------------------------------------------------------
       
    34 class CNSmlXmlParser;
       
    35 class CNSmlExtData;
       
    36 
       
    37 
       
    38 //Constants
       
    39 
       
    40 // the maximum length of an integer in characters
       
    41 const TInt KIntegerMaxLength = 12;
       
    42 
       
    43 // the length of a datetime in characters
       
    44 const TInt KDateTimeLength = 16;
       
    45 
       
    46 // ------------------------------------------------------------------------------------------------
       
    47 // Typedefs
       
    48 // ------------------------------------------------------------------------------------------------
       
    49 // the array used for xval-values
       
    50 typedef CArrayPtrFlat<HBufC8> CNSmlXValArray;
       
    51 // the array used for extension data
       
    52 typedef CArrayPtrFlat<CNSmlExtData> CNSmlExtDataArray;
       
    53 
       
    54 // enumeration for boolean values
       
    55 enum TNSmlBoolean
       
    56     {
       
    57     EBooleanMissing = 0, // not included in the xml
       
    58     EBooleanTrue,
       
    59     EBooleanFalse
       
    60     };
       
    61 
       
    62 // The error values returned from parser and generator functions
       
    63 enum TNSmlParserGeneratorError
       
    64     {
       
    65     EErrorNone,             // no error
       
    66     EOutOfMemory,           // out of memory
       
    67     EMandatoryFieldNotFound,// XNam inside Ext missing
       
    68     EInvalidBooleanValue,   // Invalid value inside boolean elements
       
    69     EInvalidIntegerValue,   // Invalid value inside integer elements
       
    70     EInvalidDatetimeValue,  // Invalid value inside datetime elements
       
    71     EInvalidCDataStructure, // CDATA error
       
    72     EInvalidXmlError,       // Xml includes invalid data, i.e. xml generally against dtd
       
    73     EInvalidFilename,       // file was not found or the filename was otherwise erroneous
       
    74     EUnknownError           // Unknown error
       
    75     };
       
    76 
       
    77 // extension data (found in and folder)
       
    78 class CNSmlExtData : public CBase
       
    79 	{
       
    80 public:
       
    81 	IMPORT_C static CNSmlExtData* NewL();
       
    82 	IMPORT_C static CNSmlExtData* NewLC();
       
    83 	IMPORT_C virtual ~CNSmlExtData();
       
    84 
       
    85 	IMPORT_C void AddXValL( HBufC8* aXVal );
       
    86 
       
    87 	IMPORT_C void GenerateXmlL( TPtr8& aXml, const CNSmlXmlParser* aParser ) const;
       
    88 	IMPORT_C TInt CountXmlSize( const CNSmlXmlParser* aParser ) const;
       
    89 	IMPORT_C void ConvertIntoEntitiesL( const CNSmlXmlParser* aParser );
       
    90 
       
    91 private:
       
    92 	CNSmlExtData();
       
    93 	void ConstructL();
       
    94 
       
    95 public:
       
    96 	HBufC8* iXNam;
       
    97 	CNSmlXValArray* iXVals;
       
    98 	};
       
    99 
       
   100 
       
   101 // folder attributes
       
   102 struct TNSmlFolderAttributeData
       
   103 	{
       
   104 	IMPORT_C TNSmlFolderAttributeData();
       
   105 
       
   106 	/*
       
   107 	* Returns the count of attributes that are not EBooleanMissing.
       
   108 	*/
       
   109 	IMPORT_C TInt AttributeCount() const;
       
   110 
       
   111 	IMPORT_C void GenerateXml( TPtr8& aXml, const CNSmlXmlParser* aParser ) const;
       
   112 	IMPORT_C TInt CountXmlSize( const CNSmlXmlParser* aParser ) const;
       
   113 
       
   114 	TNSmlBoolean iHidden;
       
   115 	TNSmlBoolean iSystem;
       
   116 	TNSmlBoolean iArchived;
       
   117 	TNSmlBoolean iDelete;
       
   118 	TNSmlBoolean iWritable;
       
   119 	TNSmlBoolean iReadable;
       
   120 	TNSmlBoolean iExecutable;
       
   121 	};
       
   122 
       
   123 // Base class for actual parsergenerators. 
       
   124 // Implements basic utilities for parsing and generating the xml.
       
   125 class CNSmlXmlParser : public CBase  
       
   126 	{
       
   127 public:
       
   128 	/*
       
   129 	* The constructor.
       
   130 	*/
       
   131 	IMPORT_C CNSmlXmlParser();
       
   132 
       
   133 	/*
       
   134 	* The destructor.
       
   135 	*/
       
   136 	IMPORT_C virtual ~CNSmlXmlParser();
       
   137 
       
   138 	/*
       
   139 	* Parses through the given xml and places the data it contains to 
       
   140 	* member variables.	Removes all the comments from the original string.
       
   141 	* @param aXml The xml to be parsed.
       
   142 	* @return 
       
   143 	*/
       
   144 	virtual TNSmlParserGeneratorError ParseXml( HBufC8* aXml ) =0;
       
   145 
       
   146 	/*
       
   147 	* Generates xml using the data in member variables of the child class. 
       
   148 	* aXml contains the generated xml when method returns successfully. 
       
   149 	* Caller should not instantiate 
       
   150 	* the buffer, since this method counts the size of the xml and 
       
   151 	* instantiates the buffer using the size as its maximum size.
       
   152 	* Caller gets the control of the buffer when method returns.
       
   153 	* @param aXml A pointer to a buffer, which is instantiated in this 
       
   154 	* method and contains the xml when method returns succesfully.
       
   155 	* @return 
       
   156 	*/
       
   157 	virtual TNSmlParserGeneratorError GenerateXml( HBufC8*& aXml ) =0;
       
   158 
       
   159 	/*
       
   160 	* Counts the size of the generated xml, when the xml would be generated 
       
   161 	* using the data in child class' member variables.
       
   162 	*/
       
   163 	virtual TInt CountXmlSizeL() =0;
       
   164 
       
   165 	/* utility functions */
       
   166 
       
   167 	void PreProcessL( HBufC8* aXml ) const;
       
   168 
       
   169 	TPtrC8 BooleanToString( const TNSmlBoolean aValue ) const;
       
   170 	TBuf8<KDateTimeLength> DateTimeToStringL( const TTime& aValue ) const;
       
   171 	TBuf8<KIntegerMaxLength> IntegerToString( const TInt aValue ) const;
       
   172 
       
   173 	TNSmlBoolean StringToBooleanL( const TPtrC8& aValue ) const;
       
   174 	TInt StringToIntegerL( const TPtrC8& aValue ) const;
       
   175 	// Note: Ignores UTC-times (Z-ending) and treats them as local time!
       
   176 	TTime StringToTTimeL( TPtrC8& aValue ) const;
       
   177 
       
   178 	void AppendElement( TPtr8& aPtr, const TDesC8& aElementName, const TDesC8& aValue ) const;
       
   179 	void AppendElement( TPtr8& aPtr, const TDesC8& aElementName ) const;
       
   180 	void AppendEndElement( TPtr8& aPtr, const TDesC8& aElementName ) const;
       
   181 
       
   182 	TInt SizeOfElements( const TDesC8& aElementName ) const;
       
   183 	TInt SizeOfBoolean( const TNSmlBoolean aValue, const TDesC8& aElementName ) const;
       
   184 	TInt SizeOfDatetime( const TDesC8& aElementName ) const;
       
   185 	TInt SizeOfInteger( const TDesC8& aElementName ) const;
       
   186 	TInt SizeOfString( const HBufC8* aValue, const TDesC8& aElementName ) const;
       
   187 
       
   188 	TInt EntitiesToCharactersL( HBufC8*& aXml, TInt aStartPos, TInt aEndPos ) const;
       
   189 	TInt CharactersToEntitiesL( HBufC8*& aXml, TInt aStartPos, TInt aEndPos ) const;
       
   190 
       
   191 	TBool IsWhitespace( const TDesC8& aText ) const;
       
   192 	void LeaveIfNotWhiteSpaceL( const TDesC8& aText ) const;
       
   193 
       
   194 	TNSmlParserGeneratorError CheckError( const TInt error ) const;
       
   195 
       
   196 protected:
       
   197 	// parsing methods
       
   198 	void ParseL( TPtrC8& aXml );
       
   199 	void AddToCompleteL( const TPtrC8 aStr );
       
   200 	void ResetBufferL( HBufC8*& aBuf ) const;
       
   201 	void AddToBufferL( const TText c, HBufC8*& aBuf ) const;
       
   202 
       
   203 	virtual void NextElementL( TPtrC8 aElement ) =0;
       
   204 	virtual void NextDataL( TPtrC8 aData ) =0;
       
   205 
       
   206 private:
       
   207 	TInt ReplaceL( HBufC8*& aText, const TDesC8& aTarget, const TDesC8& aItem, TInt aStartPos, TInt aEndPos ) const;
       
   208 	void CheckDatetimeErrorL( const TInt error ) const;
       
   209 
       
   210 protected:
       
   211 	HBufC8* iBuffer;
       
   212 	HBufC8* iCompleteBuffer;
       
   213 	};
       
   214 
       
   215 // Folder parser. Implements parsing and generating of Folder xml.
       
   216 class CNSmlFolderParser : public CNSmlXmlParser
       
   217 	{
       
   218 public:
       
   219 	IMPORT_C static CNSmlFolderParser* NewL();
       
   220 	IMPORT_C static CNSmlFolderParser* NewLC();
       
   221 	IMPORT_C virtual ~CNSmlFolderParser();
       
   222 
       
   223 	IMPORT_C virtual TNSmlParserGeneratorError ParseXml( HBufC8* aXml );
       
   224 	IMPORT_C virtual TNSmlParserGeneratorError GenerateXml( HBufC8*& aXml );
       
   225 	IMPORT_C virtual TInt CountXmlSizeL() ;
       
   226 
       
   227 	IMPORT_C void AddExtL( CNSmlExtData* aExt );
       
   228 
       
   229 protected:
       
   230 	virtual void NextElementL( TPtrC8 aElement );
       
   231 	virtual void NextDataL( TPtrC8 aData );
       
   232 
       
   233 protected:
       
   234 	
       
   235 	// the folder elements (states) in the order they appear in DTD
       
   236 	enum TNSmlCurrentFolderElement
       
   237 		{
       
   238 		EFolderNone = 0,
       
   239 		EFolder,
       
   240 		EFolderName, 
       
   241 		EFolderCreated,
       
   242 		EFolderModified,
       
   243 		EFolderAccessed,
       
   244 		EAttributes,
       
   245 		EAttributesH,
       
   246 		EAttributesS,
       
   247 		EAttributesA,
       
   248 		EAttributesD,
       
   249 		EAttributesW,
       
   250 		EAttributesR,
       
   251 		EAttributesX,
       
   252 		EFolderRole,
       
   253 		EFolderExt,
       
   254 		EFolderExtXNam,
       
   255 		EFolderExtXVal
       
   256 		};
       
   257 	
       
   258 	// struct used in folder parsing (which members are already set)
       
   259 	struct TNSmlSetFolderValues
       
   260 		{
       
   261 		TNSmlSetFolderValues();
       
   262 		void Reset();
       
   263 		
       
   264 		TBool iFolder;
       
   265 		TBool iName;
       
   266 		TBool iCreated;
       
   267 		TBool iModified;
       
   268 		TBool iAccessed;
       
   269 		TBool iAttributes;
       
   270 		TBool iAttributesH;
       
   271 		TBool iAttributesS;
       
   272 		TBool iAttributesA;
       
   273 		TBool iAttributesD;
       
   274 		TBool iAttributesW;
       
   275 		TBool iAttributesR;
       
   276 		TBool iAttributesX;
       
   277 		TBool iRole;
       
   278 		TBool iXNam;
       
   279 		};
       
   280 	
       
   281 private:
       
   282 	void StartElementStateChangeL( TNSmlCurrentFolderElement aCurrentState, TNSmlCurrentFolderElement aNextState, TBool aIsSet = EFalse );
       
   283 	void EndElementStateChangeL( TNSmlCurrentFolderElement aCurrentState, TNSmlCurrentFolderElement aNextState );
       
   284 
       
   285 	void GenerateFolderXmlL( HBufC8*& aXml );
       
   286 	void ConvertIntoEntitiesL();
       
   287 
       
   288 	CNSmlFolderParser();
       
   289 	void ConstructL();
       
   290 
       
   291 public:
       
   292 	HBufC8* iName;
       
   293 	TTime iCreated;
       
   294 	TTime iModified;
       
   295 	TTime iAccessed;
       
   296 	TNSmlFolderAttributeData iAttributes;
       
   297 	HBufC8* iRole;
       
   298 	CNSmlExtDataArray* iExt;
       
   299 
       
   300 private:
       
   301 	TNSmlCurrentFolderElement iCurrentState;
       
   302 	TNSmlCurrentFolderElement iLastState;
       
   303 	TNSmlSetFolderValues iSetValues;
       
   304 
       
   305 	CNSmlExtData* iExtData;
       
   306 	};
       
   307 
       
   308 
       
   309 
       
   310 #endif // __NSMLFOLDERPARSER_H__