From 840d1bcefcf849303dcb28a622ee03cc1a4522e8 Mon Sep 17 00:00:00 2001 From: Crzyrndm Date: Sun, 24 Jun 2018 23:08:23 +1200 Subject: [PATCH] Serialisation of extLst without namespace support -- roundtripping namespaces requires modifications of the parser used for the entire worksheet as there does not appear to be a way to track the namespace changes without listening/registering for the event. This (ofcourse) breaks lots of other things... --- include/xlnt/packaging/ext_list.hpp | 4 ++-- source/detail/serialization/xlsx_producer.cpp | 5 +++++ source/packaging/ext_list.cpp | 13 +++++-------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/xlnt/packaging/ext_list.hpp b/include/xlnt/packaging/ext_list.hpp index 3e7e710e..fdd3b83e 100644 --- a/include/xlnt/packaging/ext_list.hpp +++ b/include/xlnt/packaging/ext_list.hpp @@ -49,14 +49,14 @@ public: public: ext(xml::parser &parser); ext(const uri& ID, const std::string& serialised); - void serialise(xml::serializer &serialiser); + void serialise(xml::serializer &serialiser, const std::string& ns); uri extension_ID_; std::string serialised_value_; }; ext_list() = default; // default ctor required by xlnt::optional explicit ext_list(xml::parser &parser); - void serialize(xml::serializer &serialiser, std::string nmsp); + void serialize(xml::serializer &serialiser, const std::string& ns); void add_extension(const uri &ID, const std::string &element); diff --git a/source/detail/serialization/xlsx_producer.cpp b/source/detail/serialization/xlsx_producer.cpp index b435bd9d..0717d408 100644 --- a/source/detail/serialization/xlsx_producer.cpp +++ b/source/detail/serialization/xlsx_producer.cpp @@ -2998,6 +2998,11 @@ void xlsx_producer::write_worksheet(const relationship &rel) } } + if (ws.d_->extension_list_.is_set()) + { + ws.d_->extension_list_.get().serialize(*current_part_serializer_, xmlns); + } + write_end_element(xmlns, "worksheet"); if (!worksheet_rels.empty()) diff --git a/source/packaging/ext_list.cpp b/source/packaging/ext_list.cpp index c5eefde4..7bf91274 100644 --- a/source/packaging/ext_list.cpp +++ b/source/packaging/ext_list.cpp @@ -9,7 +9,7 @@ namespace { xlnt::uri roundtrip(xml::parser& p, xml::serializer &s) { xlnt::uri ext_uri; - int nest_level = 0; // ext element already opened on entry + int nest_level = 0; while (nest_level > 0 || (p.peek() != xml::parser::event_type::end_element && p.peek() != xml::parser::event_type::eof)) { switch (p.next()) @@ -64,8 +64,6 @@ namespace xlnt { ext_list::ext::ext(xml::parser &parser) { - //parser.next_expect(xml::parser::start_element, "ext"); - //extension_ID_ = uri(parser.attribute("uri")); std::ostringstream serialisation_stream; xml::serializer s(serialisation_stream, "", 0); extension_ID_ = roundtrip(parser, s); @@ -76,7 +74,7 @@ ext_list::ext::ext(const uri &ID, const std::string &serialised) : extension_ID_(ID), serialised_value_(serialised) {} -void ext_list::ext::serialise(xml::serializer &serialiser) +void ext_list::ext::serialise(xml::serializer &serialiser, const std::string& ns) { std::istringstream ser(serialised_value_); xml::parser p(ser, "", xml::parser::receive_default); @@ -85,7 +83,6 @@ void ext_list::ext::serialise(xml::serializer &serialiser) ext_list::ext_list(xml::parser &parser) { - parser.content(xml::parser::content_type::mixed); // begin with the start element already parsed while (parser.peek() == xml::parser::start_element) { @@ -94,12 +91,12 @@ ext_list::ext_list(xml::parser &parser) // end without parsing the end element } -void ext_list::serialize(xml::serializer &serialiser, std::string namesp) +void ext_list::serialize(xml::serializer &serialiser, const std::string& namesp) { - serialiser.start_element("extLst", namesp); + serialiser.start_element(namesp, "extLst"); for (auto &ext : extensions_) { - ext.serialise(serialiser); + ext.serialise(serialiser, namesp); } serialiser.end_element(); }