2018-10-05 21:30:54 +08:00
|
|
|
// Copyright (c) 2018
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
2021-08-22 20:23:18 +08:00
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
2018-10-05 21:30:54 +08:00
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE
|
|
|
|
//
|
|
|
|
// @license: http://www.opensource.org/licenses/mit-license.php
|
|
|
|
// @author: see AUTHORS file
|
|
|
|
|
|
|
|
#include <xlnt/drawing/spreadsheet_drawing.hpp>
|
|
|
|
#include <detail/constants.hpp>
|
|
|
|
|
|
|
|
#include <detail/external/include_libstudxml.hpp>
|
|
|
|
|
|
|
|
namespace {
|
2019-12-27 01:03:12 +08:00
|
|
|
// copy elements to the serializer provided and extract the embed ids
|
|
|
|
// from blip elements
|
|
|
|
std::vector<std::string> copy_and_extract(xml::parser &p, xml::serializer &s)
|
|
|
|
{
|
|
|
|
std::vector<std::string> embed_ids;
|
|
|
|
int nest_level = 0;
|
|
|
|
while (nest_level > 0 || (p.peek() != xml::parser::event_type::end_element && p.peek() != xml::parser::event_type::eof))
|
2018-10-05 21:30:54 +08:00
|
|
|
{
|
2019-12-27 01:03:12 +08:00
|
|
|
switch (p.next())
|
2018-10-05 21:30:54 +08:00
|
|
|
{
|
2019-12-27 01:03:12 +08:00
|
|
|
case xml::parser::start_element: {
|
|
|
|
++nest_level;
|
|
|
|
auto attribs = p.attribute_map();
|
|
|
|
auto current_ns = p.namespace_();
|
|
|
|
s.start_element(p.qname());
|
|
|
|
s.namespace_decl(current_ns, p.prefix());
|
|
|
|
if (p.qname().name() == "blip")
|
2018-10-05 21:30:54 +08:00
|
|
|
{
|
2019-12-27 01:03:12 +08:00
|
|
|
embed_ids.push_back(attribs.at(xml::qname(xlnt::constants::ns("r"), "embed")).value);
|
2018-10-05 21:30:54 +08:00
|
|
|
}
|
2019-12-27 01:03:12 +08:00
|
|
|
p.peek();
|
|
|
|
auto new_ns = p.namespace_();
|
|
|
|
if (new_ns != current_ns)
|
2018-10-05 21:30:54 +08:00
|
|
|
{
|
2019-12-27 01:03:12 +08:00
|
|
|
auto pref = p.prefix();
|
|
|
|
s.namespace_decl(new_ns, pref);
|
2018-10-05 21:30:54 +08:00
|
|
|
}
|
2019-12-27 01:03:12 +08:00
|
|
|
for (auto &ele : attribs)
|
2018-10-05 21:30:54 +08:00
|
|
|
{
|
2019-12-27 01:03:12 +08:00
|
|
|
s.attribute(ele.first, ele.second.value);
|
2018-10-05 21:30:54 +08:00
|
|
|
}
|
2019-12-27 01:03:12 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case xml::parser::end_element: {
|
|
|
|
--nest_level;
|
|
|
|
s.end_element();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case xml::parser::start_namespace_decl: {
|
|
|
|
s.namespace_decl(p.namespace_(), p.prefix());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case xml::parser::end_namespace_decl: { // nothing required here
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case xml::parser::characters: {
|
|
|
|
s.characters(p.value());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case xml::parser::eof:
|
|
|
|
return embed_ids;
|
|
|
|
case xml::parser::start_attribute:
|
|
|
|
case xml::parser::end_attribute:
|
|
|
|
default:
|
|
|
|
break;
|
2018-10-05 21:30:54 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-27 01:03:12 +08:00
|
|
|
return embed_ids;
|
2018-10-05 21:30:54 +08:00
|
|
|
}
|
2019-12-27 01:03:12 +08:00
|
|
|
} // namespace
|
2018-10-05 21:30:54 +08:00
|
|
|
|
|
|
|
namespace xlnt {
|
|
|
|
namespace drawing {
|
|
|
|
|
|
|
|
spreadsheet_drawing::spreadsheet_drawing(xml::parser &parser)
|
|
|
|
{
|
|
|
|
std::ostringstream serialization_stream;
|
|
|
|
xml::serializer s(serialization_stream, "", 0);
|
|
|
|
embed_ids_ = copy_and_extract(parser, s);
|
|
|
|
serialized_value_ = serialization_stream.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// void spreadsheet_drawing::serialize(xml::serializer &serializer, const std::string& ns)
|
|
|
|
void spreadsheet_drawing::serialize(xml::serializer &serializer)
|
|
|
|
{
|
|
|
|
std::istringstream ser(serialized_value_);
|
|
|
|
xml::parser p(ser, "", xml::parser::receive_default);
|
|
|
|
copy_and_extract(p, serializer);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> spreadsheet_drawing::get_embed_ids()
|
|
|
|
{
|
|
|
|
return embed_ids_;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace drawing
|
|
|
|
} // namespace xlnt
|