/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/drivers/common/expat/expat.cpp

*/

#include "sgx/core/core.hpp"

#include "libraries/expat/xmlparse/xmlparse.h"
#include "libraries/expat/expat.hpp"

struct expat_state {
	sgxXMLNode				*pCurrentRoot;
	sgxVector<sgxXMLNode *>	NodeStack;
};


CXMLParser *sgxXMLExpatParser::create()
{
	if (!ms_pSingleton) {
		ms_pSingleton = new sgxXMLExpatParser();
	}
	return ms_pSingleton;
}

sgxXMLExpatParser::sgxXMLExpatParser()
{
}

sgxXMLExpatParser::~sgxXMLExpatParser()
{
}

// ---------------

void startElement(void *userData, const char *name, const char **atts)
{
	expat_state *pState = (expat_state *)userData;

	sgxXMLNode	*pTag = new sgxXMLNode();

	pTag->setElement(sgxXMLElement(name));

	const char **p = atts;
	while(*p) {
		pTag->addAttribute(sgxString(*p), sgxString(*(p+1)));
		p+=2;
	}

	pState->pCurrentRoot = pState->pCurrentRoot->addNode(pTag);
	pState->NodeStack.push_back(pState->pCurrentRoot);
}

void endElement(void *userData, const char *name)
{
	SGX_UNUSED(name);
	
	expat_state *pState = (expat_state *)userData;

	pState->NodeStack.pop_back();
	pState->pCurrentRoot = pState->NodeStack.back();
}

void dataHandler(void *userData, const XML_Char *s, int len)
{
	expat_state *pState = (expat_state *)userData;

	pState->pCurrentRoot->appendData(sgxString(s, len));
}

tBOOL 
sgxXMLExpatParser::parseMemory(sgxXMLTree &tree, const tBYTE *pBuffer, const tUINT32 bufferSize) {
expat_state state;

	state.pCurrentRoot = tree.getRootNode();
	state.NodeStack.push_back(state.pCurrentRoot);

	XML_Parser parser = XML_ParserCreate(NULL);
	XML_SetUserData(parser, &state);
	XML_SetElementHandler(parser, startElement, endElement);
	XML_SetCharacterDataHandler(parser, dataHandler);

	if (!XML_Parse(parser, (const char *)pBuffer, bufferSize, TRUE)) {
		sgxTrace(
		"%s at line %d\n",
		XML_ErrorString(XML_GetErrorCode(parser)),
		XML_GetCurrentLineNumber(parser));
		return FALSE;
	}

	XML_ParserFree(parser);
	return TRUE;
}

tBOOL sgxXMLExpatParser::parseStream(sgxXMLTree &tree, sgxXMLStreamCallbackHandler *pHandler)
{
tBYTE buf[1024];	// this is arbitrary
int done;
expat_state state;

	state.pCurrentRoot = tree.getRootNode();
	state.NodeStack.push_back(state.pCurrentRoot);

	XML_Parser parser = XML_ParserCreate(NULL);
	XML_SetUserData(parser, &state);
	XML_SetElementHandler(parser, startElement, endElement);
	XML_SetCharacterDataHandler(parser, dataHandler);

	do {
		tUINT32 len = pHandler->fillBuffer(buf, sizeof(buf));

		done = len < sizeof(buf);

		if (!XML_Parse(parser, (const char *)buf, len, done)) {
			sgxTrace(
			"%s at line %d\n",
			XML_ErrorString(XML_GetErrorCode(parser)),
			XML_GetCurrentLineNumber(parser));
			return FALSE;
		}
	} while (!done);

	XML_ParserFree(parser);
	return TRUE;
}
