ContextFileWriter.cc 1.72 KB
/*
 * ContextFileWriter.cc
 *
 *  Created on: 12 aug. 2015
 *      Author: AKKA
 */

#include "ContextFileWriter.hh"

namespace plot
{

ContextFileWriter::ContextFileWriter() : _writer(NULL) {

}

ContextFileWriter::~ContextFileWriter() {
	closeWriter();
}

bool ContextFileWriter::initWriter(const char* filePath) {
	if (_writer != NULL)
		return false;

	LIBXML_TEST_VERSION

	_writer = xmlNewTextWriterFilename(filePath, 0);
	if (_writer == NULL) {
		LOG4CXX_WARN(gLogger, "Error to init the context writer");
		return false;
	}

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

	if (rc < 0) {
		LOG4CXX_WARN(gLogger, "Error to init the context writer");
		return false;
	}

	return startElement("context");
}

bool ContextFileWriter::closeWriter() {
	if (_writer == NULL)
		return false;

	int rc = xmlTextWriterEndDocument(_writer);
	if (rc < 0)
		return false;

	xmlFreeTextWriter(_writer);

	_writer = NULL;

	return true;
}

bool ContextFileWriter::startElement(const char* name) {
	if (_writer == NULL)
		return false;

	int rc = xmlTextWriterStartElement(_writer, BAD_CAST name);
	if (rc < 0) {
		LOG4CXX_WARN(gLogger, "Cannot start element");
		return false;
	}

	return true;
}

bool ContextFileWriter::endElement() {
	if (_writer == NULL)
		return false;

	int rc = xmlTextWriterEndElement(_writer);
	if (rc < 0) {
		LOG4CXX_WARN(gLogger, "Cannot end element");
		return false;
	}

	return true;
}

bool ContextFileWriter::addAttribute(const char* name, const char* value) {
	if (_writer == NULL)
		return false;

	int rc = xmlTextWriterWriteAttribute(_writer, BAD_CAST name, BAD_CAST value);
	if (rc < 0) {
		LOG4CXX_WARN(gLogger, "Cannot add attribute");
		return false;
	}

	return false;
}

} /* namespace Plot */