ParserResultFileWriter.cc 2.28 KB
/*
 * ParserResultFileWriter.cc
 *
 *  Created on: 29 apr. 2019
 *      Author: AKKA
 */

#include "ParserResultFileWriter.hh"

namespace AMDA {
namespace parser {

bool ParserResultFileWriter::write(const char* filePath, std::vector<ParserResultFileWriter::ParserResult>& parserResults) {
	LIBXML_TEST_VERSION

	xmlTextWriterPtr writer = xmlNewTextWriterFilename(filePath, 0);
	if (writer == NULL) {
		return false;
	}

	int rc;
	rc = xmlTextWriterStartDocument(writer, "1.0", "UTF-8", NULL);

	if (rc < 0) {
		xmlFreeTextWriter(writer);
		return false;
	}

	rc = xmlTextWriterStartElement(writer, BAD_CAST "expressions");
        if (rc < 0) {
		xmlFreeTextWriter(writer);
                return false;
        }

	for (auto parserRes : parserResults) {
		rc = xmlTextWriterStartElement(writer, BAD_CAST "expression");
		if (rc < 0) {
			xmlFreeTextWriter(writer);
			return false;
		}

		rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "success", BAD_CAST (parserRes.parseSuccess ? "true" : "false"));
		if (rc < 0) {
			xmlFreeTextWriter(writer);
			return false;
		}

		rc = xmlTextWriterWriteElement(writer, BAD_CAST "ihm", BAD_CAST parserRes.ihmExpression.c_str());
		if (rc < 0) {
			xmlFreeTextWriter(writer);
			return false;
		}

		rc = xmlTextWriterWriteElement(writer, BAD_CAST "kernel", BAD_CAST parserRes.kernelExpression.c_str());
		if (rc < 0) {
			xmlFreeTextWriter(writer);
			return false;
		}

		rc = xmlTextWriterStartElement(writer, BAD_CAST "params");
		if (rc < 0) {
			xmlFreeTextWriter(writer);
			return false;
		}

		for (auto param : parserRes.paramsList) {
			rc = xmlTextWriterWriteElement(writer, BAD_CAST "param", BAD_CAST param.c_str());
		}

		// close params node
		rc = xmlTextWriterEndElement(writer);
		if (rc < 0) {
			xmlFreeTextWriter(writer);
			return false;
		}

		// close expression node
		rc = xmlTextWriterEndElement(writer);
		if (rc < 0) {
			xmlFreeTextWriter(writer);
			return false;
		}
	}

	// close expressions node
	rc = xmlTextWriterEndElement(writer);
        if (rc < 0) {
		xmlFreeTextWriter(writer);
                return false;
        }

	rc = xmlTextWriterEndDocument(writer);
        if (rc < 0) {
		xmlFreeTextWriter(writer);
                return false;
	}

        xmlFreeTextWriter(writer);

	return true;
}

} /* namespace parser */
} /* namespace AMDA */