xml/xmldomandxpath/src/xmlengineserializer/xmlengserializer.cpp
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 serializer
       
    15 //
       
    16 
       
    17 #include <xml/dom/xmlengserializer.h>
       
    18 #include <xml/dom/xmlengerrors.h>
       
    19 #include "xmlengserializerxop.h"
       
    20 #include "xmlengserializergzip.h"
       
    21 #include <xml/dom/xmlengdocument.h>
       
    22 #include <xml/dom/xmlengserializeerrors.h>
       
    23 
       
    24 
       
    25 // --------------------------------------------------------------------------------------
       
    26 // Creates an instance of CXmlEngSerializer
       
    27 // --------------------------------------------------------------------------------------
       
    28 //
       
    29 EXPORT_C CXmlEngSerializer* CXmlEngSerializer::NewL(TXmlEngSerializerType aType) 
       
    30     {
       
    31     switch(aType)
       
    32         {
       
    33         case ESerializerXOP:
       
    34             {
       
    35             return CXmlEngSerializerXOP::NewL( EFalse );
       
    36             }
       
    37         case ESerializerXOPInfoset:
       
    38             {
       
    39             return CXmlEngSerializerXOP::NewL( ETrue );
       
    40             }			
       
    41         case ESerializerGZip:
       
    42             {
       
    43             return CXmlEngSerializerGZIP::NewL();
       
    44             }			
       
    45         case ESerializerDefault:		
       
    46         default:
       
    47             {
       
    48             CXmlEngSerializer* self = new (ELeave) CXmlEngSerializer();
       
    49             //2nd phase construction not needed currently
       
    50             //CleanupStack::PushL(self);
       
    51             //self->ConstructL();
       
    52             //CleanupStack::Pop(self);            
       
    53             return self;
       
    54             }
       
    55         }
       
    56     }
       
    57 
       
    58 // --------------------------------------------------------------------------------------
       
    59 // Set output type as file
       
    60 // --------------------------------------------------------------------------------------
       
    61 //
       
    62 EXPORT_C void CXmlEngSerializer::SetOutputL(const TDesC& aFileName)
       
    63     {
       
    64     if(iOutFileName)
       
    65         {
       
    66         delete iOutFileName;
       
    67         iOutFileName=NULL;
       
    68         }
       
    69     iOutFileName = aFileName.AllocL();
       
    70     iSerializationOutput = ESerializeToFile;
       
    71     }
       
    72 
       
    73 // --------------------------------------------------------------------------------------
       
    74 // Set output type as buffer
       
    75 // --------------------------------------------------------------------------------------
       
    76 //
       
    77 EXPORT_C void CXmlEngSerializer::SetOutput(RBuf8& aBuffer)
       
    78     {
       
    79     iBuffer = &aBuffer;
       
    80     iSerializationOutput = ESerializeToBuffer;
       
    81     }
       
    82 
       
    83 // --------------------------------------------------------------------------------------
       
    84 // Set output type as output stream
       
    85 // --------------------------------------------------------------------------------------
       
    86 //
       
    87 EXPORT_C void CXmlEngSerializer::SetOutput(MXmlEngOutputStream& aStream)
       
    88     {
       
    89     iOutputStream = &aStream;
       
    90     iSerializationOutput = ESerializeToStream;
       
    91     }	
       
    92 
       
    93 // --------------------------------------------------------------------------------------
       
    94 // Set serialization options
       
    95 // --------------------------------------------------------------------------------------
       
    96 //
       
    97 EXPORT_C void CXmlEngSerializer::SetSerializationOptions(TXmlEngSerializationOptions& aOptions)
       
    98     {
       
    99     iSerializationOptions = &aOptions;
       
   100     }
       
   101 
       
   102 // --------------------------------------------------------------------------------------
       
   103 // Serializes TXmlEngNode to file
       
   104 // --------------------------------------------------------------------------------------
       
   105 //
       
   106 EXPORT_C TInt CXmlEngSerializer::SerializeL( TXmlEngNode aRoot)
       
   107     {
       
   108     TXmlEngSerializationOptions* defPtr = NULL;
       
   109     TXmlEngSerializationOptions def = TXmlEngSerializationOptions();
       
   110     if(iSerializationOptions )
       
   111     	{
       
   112     	defPtr = iSerializationOptions;
       
   113     	}
       
   114     else
       
   115         {
       
   116         defPtr = &def;
       
   117         }
       
   118     
       
   119     RXmlEngDocument doc = aRoot.OwnerDocument(); //CXmlEngSerializer isn't owner of doc - in case
       
   120         										   //of leave, cleanup done by the caller
       
   121     switch(iSerializationOutput)
       
   122         {
       
   123         case ESerializeToFile:
       
   124             {
       
   125             return SerializeL(iOutFileName->Des(), aRoot, *defPtr);
       
   126             }
       
   127         case ESerializeToBuffer:
       
   128             {
       
   129             return SerializeL(*iBuffer, aRoot, *defPtr);
       
   130             }
       
   131         case ESerializeToStream:
       
   132             {
       
   133             if(!iOutputStream)
       
   134                 {
       
   135                 User::Leave(KXmlEngErrNoParameters);
       
   136                 }
       
   137             return doc.SaveL(*iOutputStream, aRoot, *defPtr );			
       
   138             }
       
   139             default:
       
   140                 {
       
   141                 User::Leave(KErrNotSupported);
       
   142                 }						
       
   143         }
       
   144         return KErrGeneral; //should never happen
       
   145     }    
       
   146 
       
   147 // --------------------------------------------------------------------------------------
       
   148 // Serializes TXmlEngNode to file
       
   149 // --------------------------------------------------------------------------------------
       
   150 //
       
   151 EXPORT_C TInt CXmlEngSerializer::SerializeL( RFs& aRFs, 
       
   152 										  const TDesC& aFileName, 
       
   153 										  const TXmlEngNode aRoot,										  
       
   154 										  const TXmlEngSerializationOptions& aOptions )
       
   155     {
       
   156     return aRoot.OwnerDocument().SaveL(aRFs, aFileName, aRoot, aOptions);
       
   157     }
       
   158 
       
   159 // --------------------------------------------------------------------------------------
       
   160 // Serializes TXmlEngNode to file
       
   161 // --------------------------------------------------------------------------------------
       
   162 //
       
   163 EXPORT_C TInt CXmlEngSerializer::SerializeL( const TDesC& aFileName, 
       
   164                                             const TXmlEngNode aRoot,
       
   165                                             const TXmlEngSerializationOptions& aOptions )
       
   166     {
       
   167     TInt size;
       
   168     RFs rfs;
       
   169     User::LeaveIfError(rfs.Connect());
       
   170     CleanupClosePushL(rfs);
       
   171     size = aRoot.OwnerDocument().SaveL(rfs, aFileName, aRoot, aOptions);
       
   172     CleanupStack::PopAndDestroy(&rfs);
       
   173     return size;
       
   174     }
       
   175 
       
   176 // --------------------------------------------------------------------------------------
       
   177 // Serializes TXmlEngNode to buffer
       
   178 // --------------------------------------------------------------------------------------
       
   179 //
       
   180 EXPORT_C TInt CXmlEngSerializer::SerializeL( RBuf8& aBuffer, 
       
   181                                         const TXmlEngNode aRoot, 
       
   182                                         const TXmlEngSerializationOptions& aOptions)
       
   183     {
       
   184     return aRoot.OwnerDocument().SaveL(aBuffer, aRoot, aOptions);
       
   185     } 
       
   186    
       
   187 // --------------------------------------------------------------------------------------
       
   188 // Default Constructor
       
   189 // --------------------------------------------------------------------------------------
       
   190 //
       
   191 CXmlEngSerializer::CXmlEngSerializer()
       
   192 	: iBuffer(NULL),
       
   193 	  iOutputStream(NULL),
       
   194 	  iSerializationOptions(NULL)
       
   195     {
       
   196     }
       
   197 
       
   198 void CXmlEngSerializer::ConstructL()
       
   199     {
       
   200     }
       
   201     
       
   202 // --------------------------------------------------------------------------------------
       
   203 // Destructor
       
   204 // --------------------------------------------------------------------------------------
       
   205 //
       
   206 CXmlEngSerializer::~CXmlEngSerializer()
       
   207     {
       
   208     delete iOutFileName;
       
   209     }
       
   210     
       
   211