xml/xmldomandxpath/src/xmlengineserializer/xmlengserializerxop.h
changeset 0 e35f40988205
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     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 // Implementation of XOP serializer
       
    15 //
       
    16 
       
    17 
       
    18 
       
    19 /**
       
    20  @file
       
    21  @internalComponent
       
    22  @released
       
    23 */
       
    24 #ifndef XMLENGSERIALIZERXOP_H
       
    25 #define XMLENGSERIALIZERXOP_H
       
    26 
       
    27 #include <xml/dom/xmlengserializer.h>
       
    28 #include <xml/dom/xmlengdataserializer.h>
       
    29 #include <f32file.h>
       
    30 
       
    31 class CDesC8ArrayFlat;
       
    32 class TXmlEngNode;
       
    33 class TXmlEngDataContainer;
       
    34 class MXmlEngSXOPOutputStream;
       
    35 
       
    36 const TInt KInitBufferSize = 4096; //initial output buffer size
       
    37 const TInt KXopStubMaxSize = 256; // href='cid:' + cid length
       
    38 
       
    39 /**
       
    40  * XOP serializer
       
    41  */
       
    42 class CXmlEngSerializerXOP: public CXmlEngSerializer, public MXmlEngDataSerializer
       
    43     {
       
    44  public:
       
    45     /**
       
    46      * Creates an instance of CXmlEngSerializerXOP class
       
    47      *
       
    48 	 * @param aCleanXOPInfoset Indicates that MIME headers 
       
    49 	 *		  should be skipped upon serialization
       
    50 	 * @return Pointer to instance of CXmlEngSerializerXOP class
       
    51      *
       
    52      * @leave KErrNoMemory, KErrGeneral, KXmlEngErrWrongEncoding.
       
    53      */
       
    54 	IMPORT_C static CXmlEngSerializerXOP* NewL( TBool aCleanXOPInfoset  );
       
    55  
       
    56     /**
       
    57      * Serializes TXmlEngNode
       
    58      *
       
    59 	 * @param aRoot Root node of a DOM tree to be serialized
       
    60 	 * @return Number of bytes written
       
    61      *
       
    62      * @leave KErrNoMemory, KErrGeneral, KXmlEngErrWrongEncoding.
       
    63      */
       
    64 	TInt SerializeL(const TXmlEngNode aRoot);
       
    65 
       
    66     /**
       
    67      * Serializes TXmlEngNode to file in XOP format
       
    68      *
       
    69 	 * @param aFileName File to which DOM tree will be serialized
       
    70 	 * @param aRoot Root node of a DOM tree to be serialized	 
       
    71 	 * @param aOptions Serialization options
       
    72 	 * @return Number of bytes written
       
    73      *
       
    74      * @leave KErrNoMemory, KErrGeneral, KXmlEngErrWrongEncoding.
       
    75      */
       
    76 	TInt SerializeL(const TDesC& aFileName,
       
    77 	                const TXmlEngNode aRoot,
       
    78 					const TXmlEngSerializationOptions& aOptions);
       
    79 
       
    80     /**
       
    81      * Serializes TXmlEngNode to file in XOP format
       
    82      *
       
    83 	 * @param aRFs File server handle     
       
    84 	 * @param aFileName File to which DOM tree will be serialized
       
    85 	 * @param aRoot Root node of a DOM tree to be serialized
       
    86 	 * @param aOptions Serialization options	 	 
       
    87 	 * @return Number of bytes written
       
    88      *
       
    89      * @leave KErrNoMemory, KErrGeneral, KXmlEngErrWrongEncoding.
       
    90      */
       
    91 	TInt SerializeL(RFs& aRFs,
       
    92 					const TDesC& aFileName,
       
    93 					const TXmlEngNode aRoot,
       
    94 					const TXmlEngSerializationOptions& aOptions);
       
    95 
       
    96     /**
       
    97      * Serializes TXmlEngNode to buffer in XOP format
       
    98      *
       
    99 	 * @param aBuffer Buffer to which DOM tree will be serialized
       
   100 	 * @param aRoot Root node of a DOM tree to be serialized
       
   101 	 * @param aOptions Serialization options
       
   102 	 * @return Number of bytes written
       
   103      *
       
   104      * @leave KErrNoMemory, KErrGeneral, KXmlEngErrWrongEncoding.
       
   105      */
       
   106 	TInt SerializeL( RBuf8& aBuffer, 
       
   107                      const TXmlEngNode aRoot, 
       
   108 					 const TXmlEngSerializationOptions& aOptions );	 						  
       
   109 	 						  
       
   110 	/**
       
   111      * Destructor.
       
   112      */
       
   113     ~CXmlEngSerializerXOP();
       
   114 
       
   115   	/**
       
   116      * Determines how data node should be serialized
       
   117 	 * @param aRoot Root node of a DOM tree to be serialized     	
       
   118      *
       
   119      */    
       
   120     TPtrC8 SerializeDataL(TXmlEngNode aNode);
       
   121  
       
   122 protected: //functions
       
   123 
       
   124     
       
   125 private: //functions
       
   126     /**
       
   127      * Constructor
       
   128      *
       
   129 	 * @param aCleanXOPInfoset Indicates that MIME headers 
       
   130 	 *		  should be skipped upon serialization     
       
   131      */
       
   132     CXmlEngSerializerXOP( TBool aCleanXOPInfoset );
       
   133 
       
   134     /**
       
   135      * Second phase constructor
       
   136      */
       
   137     void ConstructL();
       
   138 
       
   139     /**
       
   140      * Serializes DOM tree to a stream
       
   141      */
       
   142 	TInt StreamSerializeL(MXmlEngSXOPOutputStream& aOutputStream, 
       
   143 						  const TXmlEngNode aRoot,
       
   144 						  const TXmlEngSerializationOptions& aOptions);
       
   145 
       
   146     /**
       
   147      * Generates random CID
       
   148      *
       
   149 	 * @param aCid randomly generated CID string
       
   150      */
       
   151 	 void GenerateRandomCid(TDes8& aCid);
       
   152 
       
   153     /**
       
   154      * Opens already existing or creates a new file
       
   155      *
       
   156      * @param aFileName file name
       
   157      * @param aFile Instance of class RFile
       
   158      */
       
   159 	 void OpenOrCreateL(TDesC& aFileName, RFile& aFile);
       
   160 
       
   161     /**
       
   162      * Sets callback for text nodes in serialization options
       
   163      *
       
   164      * @param aOptions Serialization options
       
   165      */
       
   166 	 void SetCallbackL(TXmlEngSerializationOptions& aOptions);
       
   167 
       
   168 	/**
       
   169      * Leaves if a DOM tree contains 'Include' elements from XOP 
       
   170      * namespace 'http://www.w3.org/2004/08/xop/include'
       
   171      *
       
   172      * @param aRoot Root node of a DOM tree to be serialized
       
   173      */
       
   174 	 void LeaveIfXopIncludeL(TXmlEngNode aRoot);
       
   175 	 	 
       
   176 	/**
       
   177      * Verifies if a DOM tree contains 'Include' elements from XOP 
       
   178      * namespace 'http://www.w3.org/2004/08/xop/include'
       
   179      *
       
   180      * @param aRoot Root node of a DOM tree to be serialized
       
   181      * @return ETrue if a DOM tree contains Include element from XOP namespace,
       
   182      *		   EFalse otherwise.
       
   183      */
       
   184 	 TBool HasXopInclude(TXmlEngNode aRoot);
       
   185 
       
   186 	/**
       
   187      * Verifies if container's parent node has xmlmime:contentType 
       
   188      * attribute information item. If found, the function returns ETrue 
       
   189      * and xmlmime:contentType attribute value. Otherwise, function
       
   190      * returns EFalse and aContentType is undefined.
       
   191      *
       
   192      * @param aContainer Data container whose parent node is to be searched
       
   193      * @return ETrue if xmlmime:contentType attribute is found,
       
   194      *		   EFalse otherwise.
       
   195      */	 
       
   196 	 TBool GetContentTypeValue(TXmlEngDataContainer aContainer, TPtrC8& aContentType);
       
   197 
       
   198 	/**
       
   199      * Decodes input data from base64 to binary octets and creates
       
   200      * heap buffer with the decoded data. The buffer is pushed on the cleanup stack.
       
   201      *
       
   202      * @param aEncodedData Base64 encoded buffer
       
   203      * @return Buffer with decoded data in binary octets
       
   204      */	 
       
   205 	 HBufC8* CreateDecodedBufLC(TPtrC8 aEncodedData);
       
   206 	 
       
   207 	/**
       
   208      * Appends given string to output buffer. If the serializer's 
       
   209      * output type is file, the output buffer is released to the output stream 
       
   210      * after reaching a specified threshold and the output buffer is cleared.
       
   211      *
       
   212      * @param aString String to write
       
   213      * @param aFlush Indicates that buffer is written immediately  
       
   214      */		 
       
   215 	 void BufferedWriteL(const TDesC8& aString, TBool aFlush = EFalse);
       
   216 
       
   217 	/**
       
   218      * Flushes the output buffer and closes the output file.
       
   219      */		 
       
   220 	 void CommitWriteL();	 
       
   221 	     
       
   222 public: //data
       
   223 
       
   224 private: //data
       
   225 	/** array that stores containers to be serialized **/
       
   226 	RArray<TXmlEngDataContainer> iDataContainerArray;
       
   227 
       
   228 	/** output buffer **/
       
   229 	HBufC8* iOutputBuffer;	
       
   230 
       
   231 	/** output buffer pointer **/
       
   232 	TPtr8* iOutputBufferPtr;
       
   233 	
       
   234 	/** size of data written **/
       
   235 	TInt iDataWritten;
       
   236 
       
   237 	/**	Indicates that MIME headers should be skipped upon serialization **/
       
   238 	TBool iCleanXOPInfoset;	
       
   239 
       
   240 	/** file server handle **/
       
   241 	RFs iRFs;
       
   242 	
       
   243 	/** output file handle **/
       
   244 	RFile iOutputFile;
       
   245 
       
   246 	/** buffer for storing xop stub that replaces binary data in xop document **/
       
   247 	HBufC8* iXopStub;
       
   248 
       
   249 	/** pointer to stub buffer **/
       
   250 	TPtr8* iXopStubPtr;	// pointer to TPtr8 to avoid initialization at construction	
       
   251 	
       
   252 	/** pointer to current output stream **/
       
   253 	MXmlEngSXOPOutputStream* iTmpOutputStream;
       
   254     };
       
   255 
       
   256 
       
   257 
       
   258 #endif /* XMLENGSERIALIZERXOP_H */