spreadsheet_drawing.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (c) 2018
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <xlnt/drawing/spreadsheet_drawing.hpp>
  24. #include <detail/constants.hpp>
  25. #include <detail/external/include_libstudxml.hpp>
  26. namespace {
  27. // copy elements to the serializer provided and extract the embed ids
  28. // from blip elements
  29. std::vector<std::string> copy_and_extract(xml::parser &p, xml::serializer &s)
  30. {
  31. std::vector<std::string> embed_ids;
  32. int nest_level = 0;
  33. while (nest_level > 0 || (p.peek() != xml::parser::event_type::end_element && p.peek() != xml::parser::event_type::eof))
  34. {
  35. switch (p.next())
  36. {
  37. case xml::parser::start_element: {
  38. ++nest_level;
  39. auto attribs = p.attribute_map();
  40. auto current_ns = p.namespace_();
  41. s.start_element(p.qname());
  42. s.namespace_decl(current_ns, p.prefix());
  43. if (p.qname().name() == "blip")
  44. {
  45. embed_ids.push_back(attribs.at(xml::qname(xlnt::constants::ns("r"), "embed")).value);
  46. }
  47. p.peek();
  48. auto new_ns = p.namespace_();
  49. if (new_ns != current_ns)
  50. {
  51. auto pref = p.prefix();
  52. s.namespace_decl(new_ns, pref);
  53. }
  54. for (auto &ele : attribs)
  55. {
  56. s.attribute(ele.first, ele.second.value);
  57. }
  58. break;
  59. }
  60. case xml::parser::end_element: {
  61. --nest_level;
  62. s.end_element();
  63. break;
  64. }
  65. case xml::parser::start_namespace_decl: {
  66. s.namespace_decl(p.namespace_(), p.prefix());
  67. break;
  68. }
  69. case xml::parser::end_namespace_decl: { // nothing required here
  70. break;
  71. }
  72. case xml::parser::characters: {
  73. s.characters(p.value());
  74. break;
  75. }
  76. case xml::parser::eof:
  77. return embed_ids;
  78. case xml::parser::start_attribute:
  79. case xml::parser::end_attribute:
  80. default:
  81. break;
  82. }
  83. }
  84. return embed_ids;
  85. }
  86. } // namespace
  87. namespace xlnt {
  88. namespace drawing {
  89. spreadsheet_drawing::spreadsheet_drawing(xml::parser &parser)
  90. {
  91. std::ostringstream serialization_stream;
  92. xml::serializer s(serialization_stream, "", 0);
  93. embed_ids_ = copy_and_extract(parser, s);
  94. serialized_value_ = serialization_stream.str();
  95. }
  96. // void spreadsheet_drawing::serialize(xml::serializer &serializer, const std::string& ns)
  97. void spreadsheet_drawing::serialize(xml::serializer &serializer)
  98. {
  99. std::istringstream ser(serialized_value_);
  100. xml::parser p(ser, "", xml::parser::receive_default);
  101. copy_and_extract(p, serializer);
  102. }
  103. std::vector<std::string> spreadsheet_drawing::get_embed_ids()
  104. {
  105. return embed_ids_;
  106. }
  107. } // namespace drawing
  108. } // namespace xlnt