xml/xmldomandxpath/src/xmlengineserializer/xmlengdeserializer.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 // XML Deserializer implementation
       
    15 //
       
    16 
       
    17 #include <xml/dom/xmlengdeserializer.h>
       
    18 #include <xml/dom/xmlengerrors.h>
       
    19 #include "xmlengdeserializerxop.h"
       
    20 #include "xmlengdeserializergzip.h"
       
    21 #include <xml/utils/xmlengxestd.h>
       
    22 #include <xml/utils/xmlengxestrings.h>
       
    23 #include <xml/dom/xmlengdatacontainer.h>
       
    24 #include <xml/dom/xmlengparsingoptions.h>
       
    25 #include <xml/dom/xmlengserializeerrors.h>
       
    26 
       
    27 #include <xml/parser.h>
       
    28 #include <xml/parserfeature.h>
       
    29 #include <xml/matchdata.h>
       
    30 #include <f32file.h>
       
    31 
       
    32 _LIT8(KXmlMimeType, "text/xml");
       
    33 _LIT8(KParserType,"libxml2");
       
    34 
       
    35 using namespace Xml;
       
    36 
       
    37 // --------------------------------------------------------------------------------------
       
    38 // Creates an instance of CXmlEngDeserializer
       
    39 // --------------------------------------------------------------------------------------
       
    40 //
       
    41 EXPORT_C CXmlEngDeserializer* CXmlEngDeserializer::NewL( MContentHandler& aContentHandler,	TXmlEngDeserializerType aType ) 															
       
    42     {
       
    43 	switch(aType)
       
    44 		{
       
    45 		case EDeserializerXOP:
       
    46 			{
       
    47 			return CXmlEngDeserializerXOP::NewL( aContentHandler, EFalse );			
       
    48 			}
       
    49 		case EDeserializerXOPInfoset:
       
    50 			{
       
    51 			return CXmlEngDeserializerXOP::NewL( aContentHandler, ETrue );			
       
    52 			}			
       
    53 		case EDeserializerGZip:
       
    54 			{
       
    55 			return CXmlEngDeserializerGZIP::NewL( aContentHandler );
       
    56 			}
       
    57 		case EDeserializerDefault:		
       
    58 		default:
       
    59 		    {
       
    60 	        CXmlEngDeserializer* self = new (ELeave) CXmlEngDeserializer; 
       
    61 	        CleanupStack::PushL(self);
       
    62 	        self->ConstructL( aContentHandler );
       
    63 	        CleanupStack::Pop(self);
       
    64 	        return self;
       
    65 		    }
       
    66         }
       
    67     }
       
    68 
       
    69 
       
    70 // --------------------------------------------------------------------------------------
       
    71 // Set input type as file
       
    72 // --------------------------------------------------------------------------------------
       
    73 //
       
    74 EXPORT_C void CXmlEngDeserializer::SetInputFileL(const TDesC& aFileName)
       
    75 	{
       
    76 	if(iInputFileName)
       
    77 	    {
       
    78 	    delete iInputFileName;
       
    79 	    iInputFileName=NULL;
       
    80 	    }
       
    81 	iInputFileName = aFileName.AllocL();
       
    82 	iSerializationOutput = EDeserializeFromFile;
       
    83 	}
       
    84 
       
    85 // --------------------------------------------------------------------------------------
       
    86 // Set input type as buffer
       
    87 // --------------------------------------------------------------------------------------
       
    88 //
       
    89 EXPORT_C void CXmlEngDeserializer::SetInputBuffer(const TDesC8& aBuffer)
       
    90 	{
       
    91 	iBuffer.Set(aBuffer);
       
    92 	iSerializationOutput = EDeserializeFromBuffer;
       
    93 	}
       
    94 
       
    95 // --------------------------------------------------------------------------------------
       
    96 // Set content handler
       
    97 // --------------------------------------------------------------------------------------
       
    98 //
       
    99 EXPORT_C void CXmlEngDeserializer::SetContentHandlerL(MContentHandler& aContentHandler)
       
   100 	{
       
   101 	TInt i;
       
   102    	delete iParser;
       
   103    	iParser = NULL;
       
   104    	ConstructL( aContentHandler );
       
   105    	for(i=1; i<=ELastFeature; i*=2)
       
   106    		{
       
   107    		if(i & iFeatures)
       
   108    			{
       
   109    			iParser->EnableFeature(i);
       
   110    			}   			
       
   111    		}
       
   112 	}	
       
   113 
       
   114 // --------------------------------------------------------------------------------------
       
   115 // Set parsing options
       
   116 // May be used to alter deserializer output
       
   117 // --------------------------------------------------------------------------------------
       
   118 //	
       
   119 EXPORT_C void CXmlEngDeserializer::SetParsingOptions(TXmlEngParsingOptions& aOptions)
       
   120 	{
       
   121 	iParsingOptions = aOptions;
       
   122 	}
       
   123 
       
   124 // --------------------------------------------------------------------------------------
       
   125 // Set parsing options
       
   126 // May be used to alter deserializer output
       
   127 // --------------------------------------------------------------------------------------
       
   128 //	
       
   129 EXPORT_C const TXmlEngParsingOptions& CXmlEngDeserializer::ParsingOptions()
       
   130 	{
       
   131 	return iParsingOptions;
       
   132 	}
       
   133 	
       
   134 // --------------------------------------------------------------------------------------
       
   135 // Set external data array
       
   136 // --------------------------------------------------------------------------------------
       
   137 //
       
   138 EXPORT_C void CXmlEngDeserializer::UseExternalDataL(RArray<TXmlEngDataContainer>& aList)
       
   139 	{
       
   140 	iDataList = &aList;
       
   141 	}
       
   142 
       
   143 // --------------------------------------------------------------------------------------
       
   144 // Fetches external data registered in this deserializer
       
   145 // --------------------------------------------------------------------------------------
       
   146 //
       
   147 EXPORT_C RArray<TXmlEngDataContainer>* CXmlEngDeserializer::ExternalData()
       
   148 	{
       
   149 	return iDataList;
       
   150 	}
       
   151 
       
   152 // --------------------------------------------------------------------------------------
       
   153 // Enables CParser feature
       
   154 // --------------------------------------------------------------------------------------
       
   155 //
       
   156 EXPORT_C TInt CXmlEngDeserializer::EnableFeature(TInt aParserFeature)
       
   157 	{
       
   158 	TInt res = iParser->EnableFeature(aParserFeature);
       
   159 	if(!res)
       
   160 		{
       
   161 		iFeatures |= aParserFeature;	
       
   162 		}
       
   163 	return res; 
       
   164 	}
       
   165 	
       
   166 // --------------------------------------------------------------------------------------
       
   167 // Disables CParser feature
       
   168 // --------------------------------------------------------------------------------------
       
   169 //
       
   170 EXPORT_C TInt CXmlEngDeserializer::DisableFeature(TInt aParserFeature)
       
   171 	{
       
   172 	TInt res = iParser->DisableFeature(aParserFeature);
       
   173 	if(!res)
       
   174 		{
       
   175 		iFeatures &= ~aParserFeature;	
       
   176 		}
       
   177 	return res;	
       
   178 	}
       
   179 
       
   180 // --------------------------------------------------------------------------------------
       
   181 // Checks if a feature is enabled
       
   182 // --------------------------------------------------------------------------------------
       
   183 //	
       
   184 EXPORT_C TBool CXmlEngDeserializer::IsFeatureEnabled(TInt aParserFeature) const
       
   185 	{
       
   186 	return iParser->IsFeatureEnabled(aParserFeature);
       
   187 	}
       
   188   
       
   189 // --------------------------------------------------------------------------------------
       
   190 // Serializes TXmlEngNode to file
       
   191 // --------------------------------------------------------------------------------------
       
   192 //
       
   193 EXPORT_C void CXmlEngDeserializer::DeserializeL()
       
   194     {
       
   195 	switch(iSerializationOutput)
       
   196 		{
       
   197 		case EDeserializeFromFile:
       
   198 			{
       
   199 			if(!iInputFileName)
       
   200 				{
       
   201 				User::Leave(KXmlEngErrNoParameters);
       
   202 				}
       
   203 		    RFs fs; 
       
   204 		    CleanupClosePushL(fs);
       
   205 		    User::LeaveIfError(fs.Connect());    				
       
   206 		    DeserializeL( fs, iInputFileName->Des() );			
       
   207 			CleanupStack::PopAndDestroy(&fs);
       
   208 			break;
       
   209 			}
       
   210 		case EDeserializeFromBuffer:
       
   211 			{
       
   212 			if(iBuffer.Length() == 0)
       
   213 				{
       
   214 				User::Leave(KXmlEngErrNoParameters);
       
   215 				}
       
   216 			DeserializeL(iBuffer);			
       
   217 			break;
       
   218 			}
       
   219 		case EDeserializeFromStream:
       
   220 			{
       
   221 			// Currently this feature is not supported. Implement when need arises or remove.
       
   222 			User::Leave(KErrNotSupported);
       
   223 			}			
       
   224 		default:
       
   225 			{
       
   226 			User::Leave(KErrNotSupported);
       
   227 			break;
       
   228 			}						
       
   229 		}   
       
   230     }
       
   231 
       
   232 // --------------------------------------------------------------------------------------
       
   233 // Deserialize document
       
   234 // --------------------------------------------------------------------------------------
       
   235 //
       
   236 EXPORT_C void CXmlEngDeserializer::DeserializeL( const TDesC& aFileName,
       
   237 											const TXmlEngParsingOptions& /*aOptions*/) 
       
   238     {
       
   239 	RFs fs; 
       
   240 	CleanupClosePushL(fs);
       
   241 	User::LeaveIfError(fs.Connect());    				    
       
   242 	ParseL(*iParser,fs,aFileName);			
       
   243 	CleanupStack::PopAndDestroy(&fs);	
       
   244 	}
       
   245 
       
   246 // --------------------------------------------------------------------------------------
       
   247 // Deserialize document
       
   248 // --------------------------------------------------------------------------------------
       
   249 //
       
   250 EXPORT_C void CXmlEngDeserializer::DeserializeL( RFs& aRFs, 
       
   251 											const TDesC& aFileName,
       
   252 											const TXmlEngParsingOptions& /*aOptions*/) 
       
   253     {
       
   254 	ParseL(*iParser,aRFs,aFileName);	
       
   255 	}
       
   256 
       
   257 // --------------------------------------------------------------------------------------
       
   258 // Deserialize document
       
   259 // --------------------------------------------------------------------------------------
       
   260 //
       
   261 EXPORT_C void CXmlEngDeserializer::DeserializeL( const TDesC8& aBuffer,
       
   262 											const TXmlEngParsingOptions& /*aOptions*/) 
       
   263     {
       
   264   	ParseL(*iParser,aBuffer);
       
   265     }        
       
   266 // --------------------------------------------------------------------------------------
       
   267 // Default Constructor
       
   268 // --------------------------------------------------------------------------------------
       
   269 //
       
   270 CXmlEngDeserializer::CXmlEngDeserializer()
       
   271     { 
       
   272 	iParsingOptions = TXmlEngParsingOptions();           
       
   273     }
       
   274     
       
   275 // --------------------------------------------------------------------------------------
       
   276 // Destructor
       
   277 // --------------------------------------------------------------------------------------
       
   278 //
       
   279 CXmlEngDeserializer::~CXmlEngDeserializer()
       
   280     {
       
   281    	delete iParser; 
       
   282    	delete iInputFileName;  
       
   283     }
       
   284 
       
   285 // --------------------------------------------------------------------------------------
       
   286 // 2nd phase constructor
       
   287 // --------------------------------------------------------------------------------------
       
   288 //
       
   289 void CXmlEngDeserializer::ConstructL( MContentHandler& aContentHandler )
       
   290     {
       
   291     if(iParser)
       
   292     	{
       
   293     	delete iParser;
       
   294     	iParser = NULL;
       
   295     	}
       
   296 
       
   297     CMatchData* matchData = CMatchData::NewLC();
       
   298     matchData->SetMimeTypeL(KXmlMimeType);
       
   299     matchData->SetVariantL(KParserType); 
       
   300     iParser = CParser::NewL(*matchData, aContentHandler);
       
   301     CleanupStack::PopAndDestroy(&(*matchData));
       
   302     }
       
   303 
       
   304 
       
   305 // --------------------------------------------------------------------------------------
       
   306 // Retrieves pointer to data container referenced by CID 
       
   307 // --------------------------------------------------------------------------------------
       
   308 //
       
   309 TXmlEngDataContainer* CXmlEngDeserializer::GetDataContainer(const TDesC8& aCid)
       
   310 	{
       
   311 	for(TInt i = 0; i < iDataList->Count(); i++)
       
   312 		{
       
   313 		if( (*iDataList)[i].Cid() == aCid )
       
   314 			{
       
   315 			return &(*iDataList)[i];
       
   316 			}
       
   317 		}	
       
   318 	return NULL;
       
   319 	}
       
   320