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