TimeTableCatalogFactory.cc 4.48 KB
/*
 * TimeTableCatalogFactory.cc
 *
 *  Created on: 8 août 2013
 *      Author: CS
 */

#include "TimeTableCatalogFactory.hh"
#include "AsciiReader.hh"
#include "SpaceReader.hh"
#include "InternalXMLReader.hh"
#include "VOTableReader.hh"
#include "AsciiWriter.hh"
#include "InternalXMLWriter.hh"
#include "VOTableWriter.hh"
#include <boost/algorithm/string.hpp>

namespace TimeTableCatalog {

std::unique_ptr<TimeTableCatalogFactory> TimeTableCatalogFactory::_instance =
		nullptr;
std::once_flag TimeTableCatalogFactory::_onceFlag;

std::map<const std::string, AbstractReader*> TimeTableCatalogFactory::_readers =
		std::map<const std::string, AbstractReader*>();
std::map<const std::string, AbstractWriter*> TimeTableCatalogFactory::_writers =
		std::map<const std::string, AbstractWriter*>();

TimeTableCatalogFactory::TimeTableCatalogFactory() {
	// register readers
	registerReader(AsciiReader::FORMAT,
			new AsciiReader(std::string()));
	registerReader(InternalXMLReader::FORMAT,
			new InternalXMLReader(std::string()));
	registerReader(VOTableReader::FORMAT,
			new VOTableReader(std::string()));
                    registerReader(SpaceReader::FORMAT,
			new SpaceReader(std::string()));

	// register writers
	registerWriter(AsciiWriter::FORMAT,
			new AsciiWriter(std::string()));
	registerWriter(InternalXMLWriter::FORMAT,
			new InternalXMLWriter(std::string()));
	registerWriter(VOTableWriter::FORMAT,
			new VOTableWriter(std::string()));

}

TimeTableCatalogFactory::~TimeTableCatalogFactory() {
	std::map<const std::string, AbstractReader*>::const_iterator itr;
	for (itr = _readers.begin(); itr != _readers.end(); ++itr) {
		delete (*itr).second;
	}

	std::map<const std::string, AbstractWriter*>::const_iterator itw;
	for (itw = _writers.begin(); itw != _writers.end(); ++itw) {
		delete (*itw).second;
	}
}

void TimeTableCatalogFactory::registerReader(const std::string& pKey,
		AbstractReader* pReader) {
	// register if not already in
	if (_readers.find(pKey) == _readers.end()) {
		_readers[pKey] = pReader;
	}
}

void TimeTableCatalogFactory::registerWriter(const std::string& pKey,
		AbstractWriter* pWriter) {
	// register if not already in
	if (_writers.find(pKey) == _writers.end()) {
		_writers[pKey] = pWriter;
	}
}

std::string TimeTableCatalogFactory::getReaderType(
		const std::string& pPath) {
	for (std::map<const std::string, AbstractReader*>::const_iterator iterator =
			TimeTableCatalogFactory::_readers.begin();
			iterator != TimeTableCatalogFactory::_readers.end();
			++iterator) {
		if (iterator->second->canRead(pPath)) {
			return iterator->first;
		}
	}
	return std::string();
}

/**
 * Checks a reader exists for this type.
 */
bool TimeTableCatalogFactory::canRead(const std::string& pType) const {
	for (std::map<const std::string, AbstractReader*>::const_iterator iterator =
			TimeTableCatalogFactory::_readers.begin();
			iterator != TimeTableCatalogFactory::_readers.end();
			++iterator) {
		if (pType == iterator->first) {
			return true;
		}
	}
	return false;
}

std::unique_ptr<AbstractReader> TimeTableCatalogFactory::createReader(
		const std::string& pKey, const std::string& pPath) const {
	std::map<const std::string, AbstractReader*>::const_iterator it =
			_readers.find(pKey);

	if (it != _readers.end()) {
		return ((*it).second)->createInstance(pPath);
	}

	return 0;
}

std::unique_ptr<AbstractWriter> TimeTableCatalogFactory::createWriter(
		const std::string& pKey, const std::string& pPath,
		const std::string& pName) const {
	std::map<const std::string, AbstractWriter*>::const_iterator it =
			_writers.find(pKey);

	if (it != _writers.end()) {
		return ((*it).second)->createInstance(pPath, pName);
	}

	return 0;
}

/**
 * Reads a time table.
 */
void TimeTable::read(const std::string& ppath, const std::string& ptype) {
	std::unique_ptr<AbstractReader> reader =
			TimeTableCatalogFactory::getInstance().createReader(ptype,
					ppath);
	reader->read(*this);

}

/**
 * Writes a time table.
 */
std::string TimeTable::write(const std::string& ppath, const std::string& ptype,
		const std::string& pName) const {
	std::unique_ptr<AbstractWriter> writer =
			TimeTableCatalogFactory::getInstance().createWriter(ptype,
					ppath, pName);
	return writer->write(*this);
}

/**
 * Reads a time table and writes it into another format.
 */
void TimeTable::convert(const std::string& pInPath, const std::string& pInType,
		const std::string& pOutPath, const std::string& pName,
		const std::string& pOutType) {
	read(pInPath, pInType);
	write(pOutPath, pOutType, pName);
}

} /* namespace TimeTableCatalog */