
#include "spsXMLParser.hxx"

//////////////////////////////	PUBLIC ////////////////////////////////////

//============================  LIFECYCLE =================================

XMLParser::XMLParser()
{
}

XMLParser::XMLParser(const SPS_CHAR* lpEventRoot, const SPS_CHAR* lpXmlEvent, const SPS_UINT2 iIsFile)
{
    mlpEventRoot = (char*) lpEventRoot;

    mpTheLiaison = new XalanSourceTreeParserLiaison(mTheDOMSupport);
    mTheDOMSupport.setParserLiaison(mpTheLiaison);

    if(SPS_XML_EVENT == iIsFile)
    {
	// Create an Input source that represents the event string
    	MemBufInputSource* pMemBufIS = new MemBufInputSource((const XMLByte*)lpXmlEvent,
                            strlen(lpXmlEvent), "xEvent", false);
    	// Parse the document...
    	mpTheDocumentPtr = mpTheLiaison->parseXMLStream(*pMemBufIS);
	delete(pMemBufIS);
    } else {
	const LocalFileInputSource theInputSource(c_wstr(XalanDOMString(lpXmlEvent)));
    	// Parse the document...
    	mpTheDocumentPtr = mpTheLiaison->parseXMLStream(theInputSource);
    }
    assert(mpTheDocumentPtr != 0);
}

XMLParser::~XMLParser()
{
    delete mpTheLiaison;
}

//============================  OPERATIONS ================================

/***************************************************************************
** FUNCTION:
**	queryValue()
****************************************************************************
** DESCRIPTION:
**	Provides interface for creating DOM tree and extracting data
**	via XPaths
** IN PARAM:
**	lpXmlEvent: XML Event to be parsed
**	lpXPath: XPath to be applied on XML Event
** OUT PARAM:
** RETURN:
***************************************************************************/

SPS_CHAR* XMLParser::queryValue(SPS_CHAR* lpXPath)
{
    // The XPath Evaluator
    XPathEvaluator theEvaluator;

    // Find the context node...
    XalanNode* const pTheContextNode = theEvaluator.selectSingleNode( mTheDOMSupport, mpTheDocumentPtr,
		    XalanDOMString(mlpEventRoot).c_str(), 0);
    if (pTheContextNode == 0)
    {
	cerr << "Warning -- No nodes matched the location path \"" << "event" << "\"." << endl
		<< "Execution cannot continue..." << endl << endl;
    }
    else
    {
	XObjectPtr theResult( theEvaluator.evaluate(mTheDOMSupport, pTheContextNode, XalanDOMString(lpXPath).c_str(), 0));
	assert(theResult.null() == false);
	XalanDOMString domStr = theResult->str();
	SPS_UINT4 iStrSize = domStr.size();
	SPS_CHAR* lpResultStr = new SPS_CHAR[(iStrSize + 1) * sizeof(SPS_CHAR)]; // DO Remeber to dealloc this after the call
	if (lpResultStr)
	{
	    SPS_UINT4 i = 0;
	    for ( ; i < iStrSize ; i++)
		lpResultStr[i] = domStr[i];
	    lpResultStr[i] = '\0';
	    return lpResultStr;
	}
    }
}

void XMLParser::initXMLLibs()
{
    cout << "initXml():Start...\n";
    XMLPlatformUtils::Initialize();
    cout << "initXml(): Initialized XMLPlatformUtils...\n";
    XPathEvaluator::initialize();
    cout << "initXml(): Initialized XPathEvaluator...\n";
}

void XMLParser::termXMLLibs()
{
    XPathEvaluator::terminate();
    XMLPlatformUtils::Terminate();
}
//////////////////////////////	PROTECTED /////////////////////////////////

//////////////////////////////	PRIVATE /////////////////////////////////

