Commit f2afbaff authored by Alexandre Savard's avatar Alexandre Savard

[#3648] Full composition engine

parent 24f4cdd8
......@@ -46,7 +46,7 @@ namespace Conf
// ctor
ConfigTree::ConfigTree() :_sections()
{
/*
YamlParser *parser;
try {
parser = new YamlParser("sequence.yml");
......@@ -61,8 +61,8 @@ ConfigTree::ConfigTree() :_sections()
}
catch(YamlParserException &e) {
_error("ConfigTree: %s", e.what());
}
}
try {
parser->composeEvents();
}
......@@ -77,6 +77,7 @@ ConfigTree::ConfigTree() :_sections()
catch (YamlParserException &e) {
_error("ConfigTree: %s", e.what());
}
*/
}
// dtor
......
......@@ -29,6 +29,8 @@
*/
#include "yamlengine.h"
#include "../global.h"
namespace Conf {
......@@ -36,9 +38,45 @@ YamlEngine::YamlEngine() {}
YamlEngine::~YamlEngine() {}
void YamlEngine::open() {}
void YamlEngine::openConfigFile()
{
Conf::YamlParser *parser;
try {
parser = new Conf::YamlParser("sequence.yml");
}
catch (Conf::YamlParserException &e) {
_error("ConfigTree: %s", e.what());
}
try {
parser->serializeEvents();
}
catch(Conf::YamlParserException &e) {
_error("ConfigTree: %s", e.what());
}
void YamlEngine::close() {}
try {
document = parser->composeEvents();
}
catch(Conf::YamlParserException &e) {
_error("ConfigTree: %s", e.what());
}
try {
delete parser;
parser = NULL;
}
catch (Conf::YamlParserException &e) {
_error("ConfigTree: %s", e.what());
}
}
void YamlEngine::closeConfigFile()
{
}
void YamlEngine::read() {}
......
......@@ -32,6 +32,7 @@
#define __YAMLENGINE_H__
#include "engine.h"
#include "yamlnode.h"
#include "yamlparser.h"
#include "yamlemitter.h"
#include <exception>
......@@ -53,9 +54,9 @@ class YamlEngine : public Engine {
~YamlEngine();
virtual void open();
virtual void openConfigFile();
virtual void close();
virtual void closeConfigFile();
virtual void write();
......@@ -67,6 +68,8 @@ class YamlEngine : public Engine {
YamlEmitter *emitter;
YamlDocument *document;
};
}
......
......@@ -71,16 +71,20 @@ class YamlNode {
public:
YamlNode(NodeType t) : type(t) {}
YamlNode(NodeType t, YamlNode *top=NULL) : type(t), topNode(top) {}
~YamlNode() {}
NodeType getType() { return type; }
YamlNode *getTopNode() { return topNode; }
private:
NodeType type;
YamlNode *topNode;
};
......@@ -88,7 +92,7 @@ class YamlDocument : YamlNode {
public:
YamlDocument() : YamlNode(DOCUMENT) {}
YamlDocument(YamlNode* top=NULL) : YamlNode(DOCUMENT, top) {}
~YamlDocument() {}
......@@ -106,7 +110,7 @@ class SequenceNode : public YamlNode {
public:
SequenceNode() : YamlNode(SEQUENCE) {}
SequenceNode(YamlNode *top) : YamlNode(SEQUENCE, top) {}
~SequenceNode() {}
......@@ -125,13 +129,13 @@ class MappingNode : public YamlNode {
public:
MappingNode() : YamlNode(MAPPING) {}
MappingNode(YamlNode *top) : YamlNode(MAPPING, top) {}
~MappingNode() {}
Mapping *getMapping() { return &map; }
void setKeyValue(Key key, YamlNode *value);
void setKeyValue(Key key, YamlNode *value);
void removeKeyValue(Key key);
......@@ -148,7 +152,7 @@ class ScalarNode : public YamlNode {
public:
ScalarNode(Value v="") : YamlNode(SCALAR), val(v) {}
ScalarNode(Value v="", YamlNode *top=NULL) : YamlNode(SCALAR, top), val(v) {}
~ScalarNode() {}
......
......@@ -177,11 +177,14 @@ int YamlParser::copyEvent(yaml_event_t *event_to, yaml_event_t *event_from)
}
void YamlParser::composeEvents() {
YamlDocument *YamlParser::composeEvents() {
if(eventNumber == 0)
throw YamlParserException("No event available");
doc = NULL;
topNode = NULL;
for (int i = 0; i < eventNumber;) {
switch(events[i].type) {
......@@ -191,44 +194,209 @@ void YamlParser::composeEvents() {
case YAML_STREAM_END_EVENT:
_debug("YAML_STREAM_END_EVENT");
break;
case YAML_DOCUMENT_START_EVENT:
case YAML_DOCUMENT_START_EVENT: {
_debug("YAML_DOCUMENT_START_EVENT");
// startDocument();
break;
case YAML_DOCUMENT_END_EVENT:
}
case YAML_DOCUMENT_END_EVENT: {
_debug("YAML_DOCUMENT_END_EVENT");
// topNode = NULL;
break;
}
case YAML_ALIAS_EVENT:
_debug("YAML_ALIAS_EVENT");
break;
case YAML_SCALAR_EVENT: {
case YAML_SCALAR_EVENT: {
_debug("YAML_SCALAR_EVENT: anchor %s, tag %s, value %s", events[i].data.scalar.anchor, events[i].data.scalar.tag, events[i].data.scalar.value);
char buffer[1000];
snprintf(buffer, 1000, "%s", events[i].data.scalar.value);
_debug("----------------------------- THE BUFFER: %s", buffer);
ScalarNode *sclr = new ScalarNode(buffer);
_debug("----------------------------- THE VALUE: %s", (sclr->getValue()).c_str());
// ScalarNode *sclr = new ScalarNode("ok");
}
composeScalarEvent(topNode, buffer);
break;
case YAML_SEQUENCE_START_EVENT:
}
case YAML_SEQUENCE_START_EVENT: {
_debug("YAML_SEQUENCE_START_EVENT: anchor %s, tag %s", events[i].data.sequence_start.anchor, events[i].data.sequence_start.tag);
// startSequence();
break;
case YAML_SEQUENCE_END_EVENT:
}
case YAML_SEQUENCE_END_EVENT: {
_debug("YAML_SEQUENCE_END_EVENT");
// endSequence();
break;
case YAML_MAPPING_START_EVENT:
}
case YAML_MAPPING_START_EVENT: {
_debug("YAML_MAPPING_START_EVENT: anchor %s, tag %s", events[i].data.mapping_start.anchor, events[i].data.sequence_start.tag);
Key mapkey;
YamlNode *mapvalue;
MappingNode *map = new MappingNode(topNode);
if(events[i+1].type == YAML_SCALAR_EVENT) {
char buffer[1000];
snprintf(buffer, 1000, "%s", events[i+1].data.scalar.value);
mapkey = Key(buffer);
i++;
}
else
throw YamlParserException("Mapping event not followed by scalar");
if(events[i+1].type == YAML_SCALAR_EVENT) {
char buffer[1000];
snprintf(buffer, 1000, "%s", events[i+1].data.scalar.value);
ScalarNode *sclr = new ScalarNode(buffer, topNode);
mapvalue = (YamlNode *)sclr;
composeMappingEvent(map, mapkey, mapvalue);
}
else if(events[i+1].type == YAML_SEQUENCE_START_EVENT) {
SequenceNode *seq = new SequenceNode(topNode);
mapvalue = (YamlNode *)seq;
composeMappingEvent(map, mapkey, mapvalue);
topNode = (YamlNode *)seq;
}
else
throw YamlParserException("Not acceptable value for mapping");
break;
case YAML_MAPPING_END_EVENT:
}
case YAML_MAPPING_END_EVENT: {
_debug("YAML_MAPPING_END_EVENT");
// endMapping();
break;
}
default:
throw YamlParserException("Unknown Event");
}
i++;
}
return doc;
}
void YamlParser::startDocument()
{
doc = new YamlDocument();
if(!doc)
throw YamlParserException("Not able to create new document");
topNode = (YamlNode *)(doc);
}
void YamlParser::endDocument()
{
if(!doc || !topNode)
throw YamlParserException("No document to end");
if(topNode->getType() != DOCUMENT)
throw YamlParserException("Top node is not a document");
topNode = NULL;
}
void YamlParser::startSequence()
{
if(!topNode)
throw YamlParserException("No container for sequence");
SequenceNode *seq = new SequenceNode(topNode);
composeSequence();
topNode = (YamlNode *)seq;
}
void YamlParser::endSequence()
{
if(!topNode)
throw YamlParserException("No sequence to stop");
if(topNode->getType() != SEQUENCE)
throw YamlParserException("Top node is not a sequence");
topNode = topNode->getTopNode();
}
void YamlParser::endMapping() {
if(!topNode)
throw YamlParserException("No mapping to stop");
if(topNode->getType() != MAPPING)
throw YamlParserException("Top node is not a mapping");
topNode = topNode->getTopNode();
}
void YamlParser::composeScalarEvent(YamlNode *topNode, char *data)
{
if(!topNode)
throw YamlParserException("No container for scalar");
ScalarNode *sclr = new ScalarNode(data, topNode);
switch(topNode->getType()) {
case DOCUMENT:
((YamlDocument *)(topNode))->addNode(sclr);
break;
case SEQUENCE:
((SequenceNode *)(topNode))->addNode(sclr);
break;
case SCALAR:
case MAPPING:
default:
break;
}
}
void YamlParser::composeMappingEvent(MappingNode *map, Key key, YamlNode *val)
{
YamlNode *topNode = map->getTopNode();
if(!topNode)
throw YamlParserException("No container for mapping");
map->setKeyValue(key, val);
switch(topNode->getType()) {
case DOCUMENT:
((YamlDocument *)(topNode))->addNode(map);
break;
case SEQUENCE:
((SequenceNode *)(topNode))->addNode(map);
break;
case SCALAR:
case MAPPING:
default:
break;
}
}
void YamlParser::composeSequenceEvent(YamlNode *topNode, YamlNode *node)
{
if(!topNode)
throw YamlParserException("No container for sequence");
switch(topNode->getType()) {
case DOCUMENT:
((YamlDocument *)(topNode))->addNode(node);
break;
case SEQUENCE:
((SequenceNode *)(topNode))->addNode(node);
break;
case SCALAR:
case MAPPING:
default:
break;
}
}
}
......@@ -31,6 +31,7 @@
#ifndef __YAMLPARSER_H__
#define __YAMLPARSER_H__
#include "yamlnode.h"
#include <yaml.h>
#include <stdio.h>
#include <exception>
......@@ -73,7 +74,7 @@ class YamlParser {
void serializeEvents();
void composeEvents();
YamlDocument *composeEvents();
private:
......@@ -82,6 +83,22 @@ class YamlParser {
*/
int copyEvent(yaml_event_t *event_to, yaml_event_t *event_from);
void startDocument(void);
void endDocument(void);
void startSequence(void);
void endSequence(void);
void endMapping(void);
void composeScalarEvent(YamlNode *topNode, char *data);
void composeMappingEvent(MappingNode *map, Key key, YamlNode *node);
void composeSequenceEvent(YamlNode *topNode, YamlNode *node);
std::string filename;
FILE *fd;
......@@ -106,6 +123,10 @@ class YamlParser {
*/
int eventNumber;
YamlNode *topNode;
YamlDocument *doc;
};
}
......
......@@ -157,30 +157,16 @@ void ConfigurationTest::testYamlParser()
Conf::YamlParser *parser;
try {
parser = new Conf::YamlParser("sequence.yml");
}
catch (Conf::YamlParserException &e) {
_error("ConfigTree: %s", e.what());
}
try {
parser = new Conf::YamlParser("sequence.yml");
parser->serializeEvents();
}
catch(Conf::YamlParserException &e) {
_error("ConfigTree: %s", e.what());
}
try {
parser->composeEvents();
}
catch(Conf::YamlParserException &e) {
_error("ConfigTree: %s", e.what());
}
try {
delete parser;
parser = NULL;
}
catch (Conf::YamlParserException &e) {
_error("ConfigTree: %s", e.what());
......@@ -192,7 +178,10 @@ void ConfigurationTest::testYamlParser()
void ConfigurationTest::testYamlComposition()
{
Conf::SequenceNode *seq = new Conf::SequenceNode();
/*
Conf::YamlDocument *doc = new Conf::YamlDocument();
Conf::SequenceNode *seq = new Conf::SequenceNode(doc);
Conf::MappingNode *map = new Conf::MappingNode();
Conf::ScalarNode *sclr = new Conf::ScalarNode();
......@@ -217,7 +206,7 @@ void ConfigurationTest::testYamlComposition()
delete seq;
delete map;
delete sclr;
*/
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment