/** @file basetypexmlparser.cpp
 *  Functionality for parsing XML into EABaseTypes
 * 	@headerfile basetypexmlparser.h "EABaseTypesXml/basetypexmlparser.h"
 *	@author Chris Burns (cburns@ea.com)
 *
 *	CTUI
 *	(c) 2013 Electronic Arts Inc. 
 */

#include "EABaseTypesXml/basetypexmlparser.h"

#include "EABaseTypes/factory.h"
#include "EABaseTypes/basetypeencoder.h"

#include "EABaseTypesXml/xmlencoder.h"

#include "UTFXml/XmlCallbackReader.h"

namespace EA
{
	namespace Types
	{
		BaseTypeReturn BaseTypeXmlParser::Parse(Factory* factory,
												 void* buffer, 
												 unsigned int size)
		{
			EA::RawString error(factory->GetAllocator());
			
			BaseTypeRef result = Parse(factory, buffer, size, error);
			
			EA_ASSERT_MESSAGE(result != NULL, error.c_str());
			
			return result;
		}
	
		BaseTypeReturn BaseTypeXmlParser::Parse(Factory* factory,
												 void* buffer, 
												 unsigned int size,
												 EA::RawString& error)
		{
			ArrayRef results = factory->Array();

			BaseTypeArrayEncoder encoder(results.GetUnsafePointer());

			EA::XML::XmlCallbackReader callbackReader(factory->GetAllocator());
			callbackReader.PushInputBuffer(static_cast<char*>(buffer), size);

			XmlDecoderParams decoderParams(factory, &callbackReader);
			XmlDecoder<BaseTypeArrayEncoder>::Decode(&encoder, &decoderParams);

			BaseTypeRef data = NULL;

			data = (*results)[0];
			error.assign((*results)[1]->AsCStr());

			return data;
		}
	}
}
