aboutsummaryrefslogtreecommitdiff
path: root/ParserFactory.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ParserFactory.cpp')
-rw-r--r--ParserFactory.cpp35
1 files changed, 18 insertions, 17 deletions
diff --git a/ParserFactory.cpp b/ParserFactory.cpp
index 49c4d00..53bfbc5 100644
--- a/ParserFactory.cpp
+++ b/ParserFactory.cpp
@@ -1,32 +1,33 @@
-#include <algorithm>
-
#include "ParserFactory.h"
#include "Exception.h"
-void ParserFactory::parse(FileReader & file, MuseumDeserializer & deserializer) {
- auto & col = ParserFactory::get_collection();
- if (col.size() < 1)
- throw Exception("no parsers registered");
+#include "XMLParser.h"
+#include "TXTParser.h"
+#include "CSVParser.h"
+
+using namespace std;
+
+ParserFactory::ParserFactory() {
+ this->parsers.push_back(unique_ptr<Parser>(new XMLParser()));
+ this->parsers.push_back(unique_ptr<Parser>(new TXTParser()));
+ this->parsers.push_back(unique_ptr<Parser>(new CSVParser()));
+}
+Parser & ParserFactory::get_parser(FileReader & file) {
unsigned int best_score = 0;
Parser * best_strategy = nullptr;
- for (Parser * strategy : col) {
- unsigned int score = strategy->heuristic(file);
+ for (auto & parser : this->parsers) {
+ parser->set_file(file);
+ unsigned int score = parser->heuristic();
if (score <= best_score) continue;
best_score = score;
- best_strategy = strategy;
+ best_strategy = parser.get();
}
if (best_strategy == nullptr)
- throw Exception("unknown file type");
-
- best_strategy->parse(file, deserializer);
-}
+ throw Exception("ParserFactory: unknown file type");
-void ParserFactory::register_strategy(Parser * ps) {
- auto & col = ParserFactory::get_collection();
- if (std::find(col.begin(), col.end(), ps) != col.end()) return;
- col.push_back(ps);
+ return *best_strategy;
}