| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "profilepartxmlparser.h" |
| 5 | |
| 6 | #include "iprofilepartxmlparserprovider.h" |
| 7 | #include "item.h" |
| 8 | #include <utility> |
| 9 | |
| 10 | ProfilePartXMLParser::ProfilePartXMLParser(std::string_view id, |
| 11 | Importable::Importer &importer, |
| 12 | Exportable::Exporter &exporter) noexcept |
| 13 | : id_(id) |
| 14 | , importer_(importer) |
| 15 | , exporter_(exporter) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | std::string const &ProfilePartXMLParser::ID() const |
| 20 | { |
| 21 | return id_; |
| 22 | } |
| 23 | |
| 24 | void ProfilePartXMLParser::loadFrom(pugi::xml_node const &parentNode) |
| 25 | { |
| 26 | resetAttributes(); |
| 27 | loadPartFrom(parentNode); |
| 28 | } |
| 29 | |
| 30 | Importable::Importer &ProfilePartXMLParser::profilePartImporter() const |
| 31 | { |
| 32 | return importer_; |
| 33 | } |
| 34 | |
| 35 | Exportable::Exporter &ProfilePartXMLParser::profilePartExporter() const |
| 36 | { |
| 37 | return exporter_; |
| 38 | } |
| 39 | |
| 40 | ProfilePartXMLParser::Factory::Factory( |
| 41 | IProfilePartXMLParserProvider const &profilePartParserProvider) noexcept |
| 42 | : profilePartParserProvider_(profilePartParserProvider) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 47 | ProfilePartXMLParser::Factory::factory(Item const &i) |
| 48 | { |
| 49 | auto partParser = createPartParser(i.ID()); |
| 50 | if (partParser != nullptr) { |
| 51 | auto factory = partParser->factory(profilePartParserProvider_); |
| 52 | takePartParser(i, std::move(partParser)); |
| 53 | |
| 54 | if (factory != nullptr) { |
| 55 | factories_.emplace_back(std::move(factory)); |
| 56 | return *factories_.back(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return {}; |
| 61 | } |
| 62 | |
| 63 | std::unique_ptr<IProfilePartXMLParser> |
| 64 | ProfilePartXMLParser::Factory::createPartParser(std::string const &componentID) const |
| 65 | { |
| 66 | auto &partParserProviders = |
| 67 | profilePartParserProvider_.profilePartParserProviders(); |
| 68 | auto const providerIt = partParserProviders.find(componentID); |
| 69 | if (providerIt != partParserProviders.cend()) |
| 70 | return providerIt->second(); |
| 71 | |
| 72 | return nullptr; |
| 73 | } |