

#include <util/PlatformUtils.hpp>
#include <sax2/SAX2XMLReader.hpp>
#include <sax2/XMLReaderFactory.hpp>
#include <framework/MemBufInputSource.hpp>
#include "XmlFunctionHandler.h"
#include "MessageFormat.h"

#include <string>

using namespace std;

#pragma warning(disable:4786)

static const char*  memBufId = "fileInfo";

int main (int argC, char* argV[]) {

	
	FILE* xmlFile,*wxmlFile;
	
	MessageFormat msgFormat;
	MessageBody   msgBody;
	MessageHeader msgHeader;

	msgHeader.setMessageId("ABC1234");
	msgHeader.setMessageSize(1024);
	msgBody.setMessageType("login");
	msgBody.setLoginName("anonymous");
	msgBody.setPassword("anonymous");
	msgBody.setToAddress("192.168.1.9");
	msgBody.setText("hi how r u");

//	delay(2);	
	msgFormat.setMessageHeader(msgHeader);
	msgFormat.setMessageBody(msgBody);

	xmlFile = fopen("C:\\xerces\\xerces-c-src1_5_0\\doc\\my.xml","r");
	wxmlFile = fopen("C:\\xerces\\xerces-c-src1_5_0\\doc\\aboutr.xml","w");	
	if (!xmlFile) {
		cout << "unable to open file" <<endl;
		exit(1);
	}
	if (!wxmlFile) {
		cout << "unable to open file" <<endl;
		exit(1);
	}

	int noOfChars;
	char strBuf[2418];
	while(!feof(xmlFile))
		noOfChars = fread(strBuf,1,sizeof(strBuf),xmlFile);
	
	
	

	//LocalFileInputSource myfile(const XMLCh* const xmlFile);
	//const XMLCh* temp = myfile.getSystemId();
	//myfile.makeStream();

	/*char* tempBuf = strBuf;

	tempBuf = (char*) msgFormat.getXmlData().c_str();
	strcat(tempBuf,"\0");

	const char* tBuf = tempBuf;

//	cout<<tempBuf;
	fwrite(tBuf, sizeof(char),msgFormat.getXmlData().length(),wxmlFile);
	*///cout<<msgFormat.getXmlData().c_str();
	//tempBuf++;
	//strcat(tempBuf,"\0");
	//*tempBuf="\0";
	try {
        XMLPlatformUtils::Initialize();
    }
    catch (const XMLException& toCatch) {
        cout << "Error during initialization! :\n"
             << toCatch.getMessage() << "\n";
        return 1;
    }
    
	SAXParser::ValSchemes    valScheme = SAXParser::Val_Auto;
    bool doNamespaces    = false;
    bool doSchema        = false;
    if (argC > 1)
    {
        // See if non validating dom parser configuration is requested.
        if ((argC == 2) && !strcmp(argV[1], "-?"))
        {
            //usage();
            return 2;
        }

        int argInd;
        for (argInd = 1; argInd < argC; argInd++)
        {
            if (!strncmp(argV[argInd], "-v=", 3)
            ||  !strncmp(argV[argInd], "-V=", 3))
            {
                const char* const parm = &argV[argInd][3];

                if (!strcmp(parm, "never"))
                    valScheme = SAXParser::Val_Never;
                else if (!strcmp(parm, "auto"))
                    valScheme = SAXParser::Val_Auto;
                else if (!strcmp(parm, "always"))
                    valScheme = SAXParser::Val_Always;
                else
                {
                    cerr << "Unknown -v= value: " << parm << endl;
                    return 2;
                }
            }
             else if (!strcmp(argV[argInd], "-n")
                  ||  !strcmp(argV[argInd], "-N"))
            {
                doNamespaces = true;
            }
             else if (!strcmp(argV[argInd], "-s")
                  ||  !strcmp(argV[argInd], "-S"))
            {
                doSchema = true;
            }
             else
            {
                cerr << "Unknown option '" << argV[argInd]
                     << "', ignoring it\n" << endl;
            }
        }
    }

    SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
	//XmlFunctionHandler handler((XMLCh*)"C:\\xerces\\xerces-c-src1_5_0\\doc\\sax2count.xml");
//	SAXParser parser;
	
//    parser->setValidationScheme(valScheme);
//    parser->setDoNamespaces(doNamespaces);
//    parser->setDoSchema(doSchema);

	XmlFunctionHandler handler;
	parser->setContentHandler(&handler);
	parser->setLexicalHandler(&handler);
    parser->setErrorHandler(&handler);
//    LocalFileInputSource* FileInputSource = new LocalFileInputSource(;
	char* tempBuf = strBuf;

	string strTmpBuf = msgFormat.getXmlData();
	strcat(tempBuf,"\0");

	const char* tBuf = strTmpBuf.c_str();

//	cout<<tempBuf;
	fwrite(tBuf, sizeof(char),msgFormat.getXmlData().length(),wxmlFile);

	MemBufInputSource* memBuf = new MemBufInputSource
    (
        /*(const XMLByte*) msgFormat.getXmlData().c_str()*/
		(const XMLByte*)tBuf
        ,/*strlen(msgFormat.getXmlData().c_str())*/
		strlen(tBuf)
        ,memBufId
        ,true
    );


   // try {
       //  parser->parse("C:\\xerces\\xerces-c-src1_5_0\\doc\\about.xml");
//		sleep(1);  
//		getch();
		parser->parse(*memBuf);

    //}
   /* catch (const XMLException& toCatch) {
        cout << "\nFile not found: '" << xmlFile << "'\n"
             << "Exception message is: \n"
             << toCatch.getMessage() << "\n" ;
        return -1;
    }
/*	 catch (...)
    {
        cerr << "\nUnexpected exception during parsing: '" << xmlFile << "'\n";
        XMLPlatformUtils::Terminate();
        return 4;
    }*/
	return 0;
}

