xml/xmldomandxpath/src/xmlengineserializer/xmlengdeserializerxop.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 XOP Deserializer implementation
       
    15 //
       
    16 
       
    17 #include "xmlengdeserializerxop.h"
       
    18 #include <xml/dom/xmlengerrors.h>
       
    19 #include "xmlengcontenthandler.h"
       
    20 #include <xml/utils/xmlengxestd.h>
       
    21 #include <xml/utils/xmlengxestrings.h>
       
    22 #include <xml/dom/xmlengserializeerrors.h>
       
    23 
       
    24 #include <xml/parser.h>
       
    25 #include <xml/parserfeature.h>
       
    26 #include <xml/matchdata.h>
       
    27 #include <bafl/multipartparser.h>
       
    28 #include <bafl/bodypart.h>
       
    29 #include <f32file.h>
       
    30 
       
    31 _LIT8(KXmlMimeType, "text/xml");
       
    32 _LIT8(KParserType,"libxml2");
       
    33 
       
    34 // Implemented according to XOP Spec (http://www.w3.org/TR/xop10/)
       
    35 _LIT8(KMultipartMixed, "multipart/mixed");
       
    36 _LIT8(KBoundary1, "MIME_boundary"); 
       
    37 _LIT(KUrlStub, "http://url_stub");  
       
    38 
       
    39 using namespace Xml;
       
    40 
       
    41 // --------------------------------------------------------------------------------------
       
    42 // Creates an instance of CXMLDEDeserializerXOP
       
    43 // --------------------------------------------------------------------------------------
       
    44 //
       
    45 CXmlEngDeserializerXOP* CXmlEngDeserializerXOP::NewL( MContentHandler& aContentHandler,
       
    46 	 											TBool aCleanXOPInfoset ) 
       
    47     {	    
       
    48 	CXmlEngDeserializerXOP* deserializerXop = new (ELeave) CXmlEngDeserializerXOP(aCleanXOPInfoset);
       
    49 	CleanupStack::PushL( deserializerXop ); 	
       
    50 	deserializerXop->ConstructL( aContentHandler );
       
    51 	CleanupStack::Pop(); //deserializerXop
       
    52 	return deserializerXop;
       
    53     }
       
    54 
       
    55 // --------------------------------------------------------------------------------------
       
    56 // Enables CParser feature
       
    57 // --------------------------------------------------------------------------------------
       
    58 //
       
    59 EXPORT_C TInt CXmlEngDeserializerXOP::EnableFeature(TInt aParserFeature)
       
    60 	{
       
    61 	if(aParserFeature == EConvertTagsToLowerCase)
       
    62 		{
       
    63 		iParserMode |= aParserFeature;
       
    64 		return KErrNone;		
       
    65 		}
       
    66 	return CXmlEngDeserializer::EnableFeature(aParserFeature);
       
    67 	}
       
    68 	
       
    69 // --------------------------------------------------------------------------------------
       
    70 // Disables CParser feature
       
    71 // --------------------------------------------------------------------------------------
       
    72 //
       
    73 EXPORT_C TInt CXmlEngDeserializerXOP::DisableFeature(TInt aParserFeature)
       
    74 	{
       
    75 	if(aParserFeature == EConvertTagsToLowerCase)
       
    76 		{
       
    77 		iParserMode &= ~aParserFeature;
       
    78 		return KErrNone;		
       
    79 		}	
       
    80 	return CXmlEngDeserializer::DisableFeature(aParserFeature);
       
    81 	}
       
    82 
       
    83 // --------------------------------------------------------------------------------------
       
    84 // Checks if a feature is enabled
       
    85 // --------------------------------------------------------------------------------------
       
    86 //	
       
    87 EXPORT_C TBool CXmlEngDeserializerXOP::IsFeatureEnabled(TInt aParserFeature) const
       
    88 	{
       
    89 	if(aParserFeature == EConvertTagsToLowerCase)
       
    90 		{
       
    91 		return iParserMode & aParserFeature;		
       
    92 		}	
       
    93 	return iParser->IsFeatureEnabled(aParserFeature);
       
    94 	}
       
    95     
       
    96 // --------------------------------------------------------------------------------------
       
    97 // Deserializes document
       
    98 // --------------------------------------------------------------------------------------
       
    99 //
       
   100 void CXmlEngDeserializerXOP::DeserializeL()
       
   101     {
       
   102 	switch(iSerializationOutput)
       
   103 		{
       
   104 		case EDeserializeFromFile:
       
   105 			{
       
   106 			RFs fs;
       
   107 			User::LeaveIfError(fs.Connect());
       
   108 			CleanupClosePushL(fs); 
       
   109 		    DeserializeL(fs, iInputFileName->Des(), iParsingOptions);
       
   110 		    CleanupStack::PopAndDestroy(&fs); //fs
       
   111 		    break;
       
   112 			}
       
   113 		case EDeserializeFromBuffer:
       
   114 			{
       
   115 			if(iBuffer.Length() == 0)
       
   116 				{
       
   117 				User::Leave(KXmlEngErrNoParameters);
       
   118 				}
       
   119 			DeserializeL(iBuffer, iParsingOptions);
       
   120 			break;
       
   121 			}
       
   122 		case EDeserializeFromStream:
       
   123 			{
       
   124 			// Currently this feature is not supported. Implement when need arises or remove.			
       
   125 			User::Leave(KErrNotSupported);
       
   126 			}
       
   127 		default:
       
   128 			{
       
   129 			User::Leave(KErrNotSupported);
       
   130 			break;
       
   131 			}						
       
   132 		}   
       
   133     }
       
   134 
       
   135 // --------------------------------------------------------------------------------------
       
   136 // Deserialize document
       
   137 // --------------------------------------------------------------------------------------
       
   138 //
       
   139 void CXmlEngDeserializerXOP::DeserializeL( const TDesC& aFileName,
       
   140 										   const TXmlEngParsingOptions& aOptions ) 
       
   141     {
       
   142 	RFs fs;
       
   143 	User::LeaveIfError(fs.Connect());
       
   144 	CleanupClosePushL(fs); 
       
   145 	DeserializeL(fs, aFileName, aOptions);
       
   146 	CleanupStack::PopAndDestroy(&fs); //fs
       
   147 	}
       
   148 
       
   149 // --------------------------------------------------------------------------------------
       
   150 // Deserialize document
       
   151 // --------------------------------------------------------------------------------------
       
   152 //
       
   153 void CXmlEngDeserializerXOP::DeserializeL( RFs& aRFs, 
       
   154 										const TDesC& aFileName,
       
   155 										const TXmlEngParsingOptions& aOptions ) 
       
   156     {
       
   157 	iBodyPartArray.ResetAndDestroy();     
       
   158 	if(iData)
       
   159 	   	{
       
   160 	   	delete iData;
       
   161 	   	iData = NULL;
       
   162 	   	}
       
   163 	iData = ReadFileL( aRFs, aFileName );
       
   164 	TPtrC8 dataPtr = iData->Des();
       
   165 	CXmlEngDeserializerXOP::DeserializeL( dataPtr, aOptions );	
       
   166 	}
       
   167 
       
   168 // --------------------------------------------------------------------------------------
       
   169 // Deserialize document
       
   170 // --------------------------------------------------------------------------------------
       
   171 //
       
   172 void CXmlEngDeserializerXOP::DeserializeL( const TDesC8& aBuffer,
       
   173 										   const TXmlEngParsingOptions& aOptions ) 
       
   174     {
       
   175     TXmlEngParsingOptions originalOpts = iParsingOptions;
       
   176     iParsingOptions = aOptions;
       
   177 
       
   178     if( iCleanXOPInfoset )
       
   179     	{
       
   180 		Xml::ParseL(*iParser, aBuffer);
       
   181 		}
       
   182 	else
       
   183 		{
       
   184 	    if(!aBuffer.Length())
       
   185 	    	{
       
   186 	    	User::Leave(KXmlEngErrNoParameters);
       
   187 	    	}
       
   188 	    if(iBodyPartArray.Count())
       
   189 	        iBodyPartArray.ResetAndDestroy();   	
       
   190 	    
       
   191 	   	MultipartParser::ParseL( aBuffer, KMultipartMixed, KBoundary1, KUrlStub, iBodyPartArray );    	
       
   192 
       
   193 		// get xml document
       
   194 		CBodyPart* bodyPart = NULL;
       
   195 		if(iBodyPartArray.Count() > 0)	
       
   196 			{
       
   197 			bodyPart = iBodyPartArray[0];		
       
   198 			}
       
   199 		else
       
   200 			{
       
   201 			//The multipart document wasn't parsed correctly
       
   202 			User::Leave(KXmlEngErrBadMultipart);
       
   203 			}
       
   204 		Xml::ParseL(*iParser, bodyPart->Body());		
       
   205 		}
       
   206 	iParsingOptions = originalOpts;
       
   207 	iBodyPartArray.ResetAndDestroy();
       
   208     }        
       
   209 
       
   210 // --------------------------------------------------------------------------------------
       
   211 // Retrieves pointer to data referenced by CID from parsed multipart package
       
   212 // --------------------------------------------------------------------------------------
       
   213 //
       
   214 TInt CXmlEngDeserializerXOP::GetData(const TDesC8& aCid, TPtrC8& aData)
       
   215 	{
       
   216 	for(TInt i = 0; i < iBodyPartArray.Count(); i++)
       
   217 		{
       
   218 		TPtrC8 cid = iBodyPartArray[i]->ContentID();
       
   219 		if(iBodyPartArray[i]->ContentID() == aCid)
       
   220 			{
       
   221 			TInt size = iBodyPartArray[i]->Body().Size();
       
   222 			iDataPtr.Set(iBodyPartArray[i]->Body());
       
   223 			aData.Set(iDataPtr);
       
   224 			return KErrNone;
       
   225 			}
       
   226 		}
       
   227 	return KErrNotFound;
       
   228 	}
       
   229   
       
   230 // --------------------------------------------------------------------------------------
       
   231 // Default Constructor
       
   232 // --------------------------------------------------------------------------------------
       
   233 //
       
   234 CXmlEngDeserializerXOP::CXmlEngDeserializerXOP( TBool aCleanXOPInfoset ) 
       
   235 	 : iData(NULL), iCleanXOPInfoset(aCleanXOPInfoset)
       
   236     {
       
   237     }
       
   238     
       
   239 // --------------------------------------------------------------------------------------
       
   240 // Destructor
       
   241 // --------------------------------------------------------------------------------------
       
   242 //
       
   243 CXmlEngDeserializerXOP::~CXmlEngDeserializerXOP()
       
   244     {
       
   245     if(iData)
       
   246     	{
       
   247     	delete iData;
       
   248     	}
       
   249     iBodyPartArray.ResetAndDestroy();
       
   250     delete iInternalHandler;  
       
   251     }
       
   252 
       
   253 // --------------------------------------------------------------------------------------
       
   254 // 2nd phase constructor
       
   255 // --------------------------------------------------------------------------------------
       
   256 //
       
   257 void CXmlEngDeserializerXOP::ConstructL( MContentHandler& aContentHandler )
       
   258     {
       
   259     if(iInternalHandler)
       
   260     	{
       
   261     	delete iInternalHandler;
       
   262     	iInternalHandler = NULL;
       
   263     	}    
       
   264     iInternalHandler = CXmlEngContentHandler::NewL(this, &aContentHandler);
       
   265 
       
   266     if(iParser)
       
   267     	{
       
   268     	delete iParser;
       
   269     	iParser = NULL;
       
   270     	}
       
   271 
       
   272     CMatchData* matchData = CMatchData::NewLC();
       
   273     matchData->SetMimeTypeL(KXmlMimeType);
       
   274     matchData->SetVariantL(KParserType); 
       
   275     iParser = CParser::NewL(*matchData, *iInternalHandler);
       
   276     CleanupStack::PopAndDestroy(matchData);
       
   277     }
       
   278 
       
   279 // --------------------------------------------------------------------------------------
       
   280 // Read file
       
   281 // --------------------------------------------------------------------------------------
       
   282 //
       
   283 HBufC8* CXmlEngDeserializerXOP::ReadFileL( RFs& aRFs, const TDesC& aFileName)
       
   284     {
       
   285     RFile file;
       
   286     User::LeaveIfError(file.Open(aRFs, aFileName, EFileRead));
       
   287     CleanupClosePushL(file);
       
   288     TInt size;
       
   289     User::LeaveIfError(file.Size(size));
       
   290     HBufC8* buf = HBufC8::NewLC(size);
       
   291     TPtr8 bufPtr(buf->Des());
       
   292     User::LeaveIfError(file.Read(bufPtr));
       
   293     CleanupStack::Pop(buf); // buf
       
   294     CleanupStack::PopAndDestroy(&file); // file
       
   295     return buf;
       
   296     }
       
   297 
       
   298 /*
       
   299 // --------------------------------------------------------------------------------------
       
   300 // SerializationOutput
       
   301 // --------------------------------------------------------------------------------------
       
   302 //
       
   303 TXmlEngDeserializationSource CXmlEngDeserializerXOP::SerializationOutput()
       
   304     {
       
   305     return iSerializationOutput;
       
   306     }
       
   307 */
       
   308 
       
   309