| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "profilexmlparser.h" |
| 5 | |
| 6 | #include "iprofile.h" |
| 7 | #include "isyscomponentprofilepart.h" |
| 8 | #include <algorithm> |
| 9 | #include <cctype> |
| 10 | #include <pugixml.hpp> |
| 11 | #include <spdlog/spdlog.h> |
| 12 | #include <utility> |
| 13 | |
| 14 | ProfileXMLParser::Factory::Factory( |
| 15 | IProfilePartXMLParserProvider const &profilePartParserProvider, |
| 16 | ProfileXMLParser &outer) noexcept |
| 17 | : ProfilePartXMLParser::Factory(profilePartParserProvider) |
| 18 | , outer_(outer) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | void ProfileXMLParser::Factory::takePartParser( |
| 23 | Item const &i, std::unique_ptr<IProfilePartXMLParser> &&part) |
| 24 | { |
| 25 | auto &key = dynamic_cast<ISysComponentProfilePart const &>(i).key(); |
| 26 | outer_.parsers_.emplace(key, std::move(part)); |
| 27 | } |
| 28 | |
| 29 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 30 | ProfileXMLParser::Factory::provideExporter(Item const &i) |
| 31 | { |
| 32 | if (i.ID() == IProfile::ItemID) |
| 33 | return *this; |
| 34 | else |
| 35 | return factory(i); |
| 36 | } |
| 37 | |
| 38 | class ProfileXMLParser::Initializer final : public IProfile::Exporter |
| 39 | { |
| 40 | public: |
| 41 | Initializer(ProfileXMLParser &outer) noexcept |
| 42 | : outer_(outer) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 47 | provideExporter(Item const &i) override; |
| 48 | |
| 49 | void takeActive(bool active) override; |
| 50 | void takeInfo(IProfile::Info const &info) override; |
| 51 | |
| 52 | private: |
| 53 | ProfileXMLParser &outer_; |
| 54 | std::unordered_map<std::string, std::unique_ptr<Exportable::Exporter>> initializers_; |
| 55 | }; |
| 56 | |
| 57 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 58 | ProfileXMLParser::Initializer::provideExporter(Item const &i) |
| 59 | { |
| 60 | auto &id = i.ID(); |
| 61 | if (id == IProfile::ItemID) |
| 62 | return *this; |
| 63 | else { |
| 64 | auto &key = dynamic_cast<ISysComponentProfilePart const &>(i).key(); |
| 65 | if (initializers_.count(key) > 0) |
| 66 | return *initializers_.at(key); |
| 67 | else if (outer_.parsers_.count(key) > 0) { |
| 68 | auto initializer = outer_.parsers_.at(key)->initializer(); |
| 69 | if (initializer != nullptr) { |
| 70 | initializers_.emplace(key, std::move(initializer)); |
| 71 | return *initializers_.at(key); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return {}; |
| 77 | } |
| 78 | |
| 79 | void ProfileXMLParser::Initializer::takeActive(bool active) |
| 80 | { |
| 81 | outer_.active_ = outer_.activeDefault_ = active; |
| 82 | } |
| 83 | |
| 84 | void ProfileXMLParser::Initializer::takeInfo(IProfile::Info const &info) |
| 85 | { |
| 86 | outer_.info_ = outer_.infoDefault_ = info; |
| 87 | } |
| 88 | |
| 89 | ProfileXMLParser::ProfileXMLParser() noexcept |
| 90 | : format_("xml") |
| 91 | { |
| 92 | profileNodeName_ = IProfile::ItemID; |
| 93 | std::transform(profileNodeName_.cbegin(), profileNodeName_.cend(), |
| 94 | profileNodeName_.begin(), ::toupper); |
| 95 | } |
| 96 | |
| 97 | std::string const &ProfileXMLParser::format() |
| 98 | { |
| 99 | return format_; |
| 100 | } |
| 101 | |
| 102 | std::unique_ptr<Exportable::Exporter> ProfileXMLParser::initializer() |
| 103 | { |
| 104 | return std::make_unique<ProfileXMLParser::Initializer>(*this); |
| 105 | } |
| 106 | |
| 107 | bool ProfileXMLParser::load(std::vector<char> const &data, IProfile &profile) |
| 108 | { |
| 109 | pugi::xml_document doc; |
| 110 | auto status = doc.load_buffer(data.data(), data.size()); |
| 111 | if (status) { |
| 112 | auto profileNode = doc.child(profileNodeName_.c_str()); |
| 113 | if (!profileNode.empty()) { |
| 114 | |
| 115 | auto active = profileNode.attribute("active"); |
| 116 | auto name = profileNode.attribute("name"); |
| 117 | auto exe = profileNode.attribute("exe"); |
| 118 | |
| 119 | active_ = active.as_bool(activeDefault_); |
| 120 | info_.name = name.as_string(infoDefault_.name.c_str()); |
| 121 | info_.exe = exe.as_string(infoDefault_.exe.c_str()); |
| 122 | |
| 123 | for (auto &[key, component] : parsers_) |
| 124 | component->loadFrom(profileNode); |
| 125 | |
| 126 | profile.importWith(*this); |
| 127 | return true; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | SPDLOG_DEBUG("Cannot parse xml data for profile {}.\nError: {}", |
| 132 | profile.info().name, status.description()); |
| 133 | |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | class PugiXMLWriter final : public pugi::xml_writer |
| 138 | { |
| 139 | public: |
| 140 | PugiXMLWriter(std::vector<char> &data) |
| 141 | : data_(data) |
| 142 | { |
| 143 | data.clear(); |
| 144 | } |
| 145 | void write(const void *data, size_t size) override |
| 146 | { |
| 147 | auto start = data_.size(); |
| 148 | data_.resize(start + size); |
| 149 | memcpy(&data_[start], data, size * sizeof(char)); |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | std::vector<char> &data_; |
| 154 | }; |
| 155 | |
| 156 | bool ProfileXMLParser::save(std::vector<char> &data, IProfile const &profile) |
| 157 | { |
| 158 | profile.exportWith(*this); |
| 159 | |
| 160 | pugi::xml_document doc; |
| 161 | auto profileNode = doc.append_child(profileNodeName_.c_str()); |
| 162 | profileNode.append_attribute("active") = active_; |
| 163 | profileNode.append_attribute("name") = info_.name.c_str(); |
| 164 | profileNode.append_attribute("exe") = info_.exe.c_str(); |
| 165 | |
| 166 | for (auto &[key, component] : parsers_) |
| 167 | component->appendTo(profileNode); |
| 168 | |
| 169 | PugiXMLWriter writer(data); |
| 170 | doc.save(writer); |
| 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 176 | ProfileXMLParser::provideExporter(Item const &i) |
| 177 | { |
| 178 | if (i.ID() == IProfile::ItemID) |
| 179 | return *this; |
| 180 | |
| 181 | auto &key = dynamic_cast<ISysComponentProfilePart const &>(i).key(); |
| 182 | auto const iter = parsers_.find(key); |
| 183 | if (iter != parsers_.cend()) |
| 184 | return iter->second->profilePartExporter(); |
| 185 | |
| 186 | return {}; |
| 187 | } |
| 188 | |
| 189 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 190 | ProfileXMLParser::provideImporter(Item const &i) |
| 191 | { |
| 192 | if (i.ID() == IProfile::ItemID) |
| 193 | return *this; |
| 194 | |
| 195 | auto &key = dynamic_cast<ISysComponentProfilePart const &>(i).key(); |
| 196 | auto const iter = parsers_.find(key); |
| 197 | if (iter != parsers_.cend()) |
| 198 | return iter->second->profilePartImporter(); |
| 199 | |
| 200 | return {}; |
| 201 | } |
| 202 | |
| 203 | void ProfileXMLParser::takeActive(bool active) |
| 204 | { |
| 205 | active_ = active; |
| 206 | } |
| 207 | |
| 208 | void ProfileXMLParser::takeInfo(IProfile::Info const &info) |
| 209 | { |
| 210 | info_ = info; |
| 211 | } |
| 212 | |
| 213 | bool ProfileXMLParser::provideActive() const |
| 214 | { |
| 215 | return active_; |
| 216 | } |
| 217 | |
| 218 | IProfile::Info const &ProfileXMLParser::provideInfo() const |
| 219 | { |
| 220 | return info_; |
| 221 | } |