2018-06-23 15:08:39 +08:00
|
|
|
#include <xlnt/packaging/ext_list.hpp>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <detail/external/include_libstudxml.hpp>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// send elements straight from parser to serialiser without modification
|
|
|
|
// runs until the end of the current open element
|
2018-06-24 17:25:58 +08:00
|
|
|
xlnt::uri roundtrip(xml::parser& p, xml::serializer &s)
|
2018-06-23 15:08:39 +08:00
|
|
|
{
|
2018-06-24 17:25:58 +08:00
|
|
|
xlnt::uri ext_uri;
|
2018-06-24 19:08:23 +08:00
|
|
|
int nest_level = 0;
|
2018-06-23 15:08:39 +08:00
|
|
|
while (nest_level > 0 || (p.peek() != xml::parser::event_type::end_element && p.peek() != xml::parser::event_type::eof))
|
|
|
|
{
|
|
|
|
switch (p.next())
|
|
|
|
{
|
|
|
|
case xml::parser::start_element:
|
|
|
|
{
|
|
|
|
++nest_level;
|
2018-06-25 19:12:35 +08:00
|
|
|
auto attribs = p.attribute_map();
|
2018-06-25 19:17:21 +08:00
|
|
|
s.start_element(p.qname());
|
2018-06-24 17:25:58 +08:00
|
|
|
if (nest_level == 1)
|
|
|
|
{
|
2018-06-25 19:12:35 +08:00
|
|
|
ext_uri = xlnt::uri(attribs.at(xml::qname("uri")).value);
|
2018-06-24 17:25:58 +08:00
|
|
|
}
|
2018-06-25 19:12:35 +08:00
|
|
|
auto current_ns = p.namespace_();
|
|
|
|
p.peek(); // to look into the new namespace
|
|
|
|
auto new_ns = p.namespace_(); // only before attributes?
|
|
|
|
if (new_ns != current_ns)
|
|
|
|
{
|
|
|
|
auto pref = p.prefix();
|
|
|
|
s.namespace_decl(new_ns, pref);
|
|
|
|
}
|
|
|
|
for (auto &ele : attribs)
|
2018-06-23 15:08:39 +08:00
|
|
|
{
|
2018-06-24 17:25:58 +08:00
|
|
|
s.attribute(ele.first.string(), ele.second.value);
|
2018-06-23 15:08:39 +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;
|
|
|
|
}
|
2018-06-23 18:43:11 +08:00
|
|
|
case xml::parser::eof:
|
2018-06-24 17:25:58 +08:00
|
|
|
return ext_uri;
|
2018-06-23 18:43:11 +08:00
|
|
|
case xml::parser::start_attribute:
|
|
|
|
case xml::parser::end_attribute:
|
|
|
|
default:
|
|
|
|
break;
|
2018-06-23 15:08:39 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-24 17:25:58 +08:00
|
|
|
return ext_uri;
|
2018-06-23 15:08:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace xlnt {
|
|
|
|
|
2018-06-25 19:12:35 +08:00
|
|
|
ext_list::ext::ext(xml::parser &parser, const std::string& ns)
|
2018-06-23 15:08:39 +08:00
|
|
|
{
|
|
|
|
std::ostringstream serialisation_stream;
|
|
|
|
xml::serializer s(serialisation_stream, "", 0);
|
2018-06-25 19:12:35 +08:00
|
|
|
s.start_element(xml::qname(ns, "wrap")); // wrapper for the xmlns declaration
|
|
|
|
s.namespace_decl(ns, "");
|
2018-06-24 17:25:58 +08:00
|
|
|
extension_ID_ = roundtrip(parser, s);
|
2018-06-25 19:12:35 +08:00
|
|
|
s.end_element(xml::qname(ns, "wrap"));
|
2018-06-23 15:08:39 +08:00
|
|
|
serialised_value_ = serialisation_stream.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
ext_list::ext::ext(const uri &ID, const std::string &serialised)
|
|
|
|
: extension_ID_(ID), serialised_value_(serialised)
|
|
|
|
{}
|
|
|
|
|
2018-06-25 19:12:35 +08:00
|
|
|
void ext_list::ext::serialise(xml::serializer &serialiser, const std::string& ns)
|
2018-06-23 15:08:39 +08:00
|
|
|
{
|
|
|
|
std::istringstream ser(serialised_value_);
|
|
|
|
xml::parser p(ser, "", xml::parser::receive_default);
|
2018-06-25 19:12:35 +08:00
|
|
|
p.next_expect(xml::parser::event_type::start_element, xml::qname(ns, "wrap"));
|
2018-06-23 15:08:39 +08:00
|
|
|
roundtrip(p, serialiser);
|
2018-06-25 19:12:35 +08:00
|
|
|
p.next_expect(xml::parser::event_type::end_element, xml::qname(ns, "wrap"));
|
2018-06-23 15:08:39 +08:00
|
|
|
}
|
|
|
|
|
2018-06-25 19:12:35 +08:00
|
|
|
ext_list::ext_list(xml::parser &parser, const std::string& ns)
|
2018-06-23 15:08:39 +08:00
|
|
|
{
|
|
|
|
// begin with the start element already parsed
|
|
|
|
while (parser.peek() == xml::parser::start_element)
|
|
|
|
{
|
2018-06-25 19:12:35 +08:00
|
|
|
extensions_.push_back(ext(parser, ns));
|
2018-06-23 15:08:39 +08:00
|
|
|
}
|
|
|
|
// end without parsing the end element
|
|
|
|
}
|
|
|
|
|
2018-06-25 19:12:35 +08:00
|
|
|
void ext_list::serialize(xml::serializer &serialiser, const std::string& ns)
|
2018-06-23 15:08:39 +08:00
|
|
|
{
|
2018-06-25 19:12:35 +08:00
|
|
|
serialiser.start_element(ns, "extLst");
|
2018-06-23 15:08:39 +08:00
|
|
|
for (auto &ext : extensions_)
|
|
|
|
{
|
2018-06-25 19:12:35 +08:00
|
|
|
ext.serialise(serialiser, ns);
|
2018-06-23 15:08:39 +08:00
|
|
|
}
|
|
|
|
serialiser.end_element();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ext_list::add_extension(const uri &ID, const std::string &element)
|
|
|
|
{
|
|
|
|
extensions_.push_back(ext{ID, element});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ext_list::has_extension(const uri &extension_uri) const
|
|
|
|
{
|
|
|
|
return extensions_.end() != std::find_if(extensions_.begin(), extensions_.end(),
|
|
|
|
[&extension_uri](const ext &ext) { return extension_uri == ext.extension_ID_; });
|
|
|
|
}
|
|
|
|
|
|
|
|
const ext_list::ext &ext_list::extension(const uri &extension_uri) const
|
|
|
|
{
|
|
|
|
return *std::find_if(extensions_.begin(), extensions_.end(),
|
|
|
|
[&extension_uri](const ext &ext) { return extension_uri == ext.extension_ID_; });
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<ext_list::ext> &ext_list::extensions() const
|
|
|
|
{
|
|
|
|
return extensions_;
|
|
|
|
}
|
|
|
|
|
2018-07-03 10:05:07 +08:00
|
|
|
bool ext_list::operator==(const ext_list &rhs) const
|
|
|
|
{
|
2018-07-03 10:37:24 +08:00
|
|
|
return extensions_ == rhs.extensions_;
|
2018-07-03 10:05:07 +08:00
|
|
|
}
|
|
|
|
|
2018-06-23 15:08:39 +08:00
|
|
|
}
|