1 // Copyright (c) 2014-2020 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
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, WRISING 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 <algorithm>
26 #include <unordered_set>
27
28 #include <xlnt/packaging/manifest.hpp>
29 #include <xlnt/utils/exceptions.hpp>
30
31 namespace xlnt {
32
clear()33 void manifest::clear()
34 {
35 default_content_types_.clear();
36 override_content_types_.clear();
37 relationships_.clear();
38 }
39
canonicalize(const std::vector<xlnt::relationship> & rels) const40 path manifest::canonicalize(const std::vector<xlnt::relationship> &rels) const
41 {
42 xlnt::path relative;
43
44 for (auto component : rels)
45 {
46 if (component == rels.back())
47 {
48 relative = relative.append(component.target().path());
49 }
50 else
51 {
52 relative = relative.append(component.target().path().parent());
53 }
54 }
55
56 std::vector<std::string> absolute_parts;
57
58 for (const auto &component : relative.split())
59 {
60 if (component == ".") continue;
61
62 if (component == "..")
63 {
64 absolute_parts.pop_back();
65 continue;
66 }
67
68 absolute_parts.push_back(component);
69 }
70
71 xlnt::path result;
72
73 for (const auto &component : absolute_parts)
74 {
75 result = result.append(component);
76 }
77
78 return result;
79 }
80
has_relationship(const path & path,relationship_type type) const81 bool manifest::has_relationship(const path &path, relationship_type type) const
82 {
83 auto rels = relationships_.find(path);
84 if (rels == relationships_.end())
85 {
86 return false;
87 }
88 return rels->second.end() != std::find_if(rels->second.begin(), rels->second.end(), [type](const std::pair<std::string, xlnt::relationship> &rel) { return rel.second.type() == type; });
89 }
90
has_relationship(const path & path,const std::string & rel_id) const91 bool manifest::has_relationship(const path &path, const std::string &rel_id) const
92 {
93 auto rels = relationships_.find(path);
94 if (rels == relationships_.end())
95 {
96 return false;
97 }
98 return rels->second.find(rel_id) != rels->second.end();
99 }
100
relationship(const path & part,relationship_type type) const101 relationship manifest::relationship(const path &part, relationship_type type) const
102 {
103 if (relationships_.find(part) == relationships_.end()) throw key_not_found();
104
105 for (const auto &rel : relationships_.at(part))
106 {
107 if (rel.second.type() == type) return rel.second;
108 }
109
110 throw key_not_found();
111 }
112
relationships(const path & part,relationship_type type) const113 std::vector<xlnt::relationship> manifest::relationships(const path &part, relationship_type type) const
114 {
115 std::vector<xlnt::relationship> matches;
116
117 if (has_relationship(part, type))
118 {
119 for (const auto &rel : relationships_.at(part))
120 {
121 if (rel.second.type() == type)
122 {
123 matches.push_back(rel.second);
124 }
125 }
126 }
127
128 return matches;
129 }
130
content_type(const path & part) const131 std::string manifest::content_type(const path &part) const
132 {
133 auto absolute = part.resolve(path("/"));
134
135 if (has_override_type(absolute))
136 {
137 return override_type(absolute);
138 }
139
140 if (has_default_type(part.extension()))
141 {
142 return default_type(part.extension());
143 }
144
145 throw key_not_found();
146 }
147
register_override_type(const path & part,const std::string & content_type)148 void manifest::register_override_type(const path &part, const std::string &content_type)
149 {
150 override_content_types_[part] = content_type;
151 }
152
unregister_override_type(const path & part)153 void manifest::unregister_override_type(const path &part)
154 {
155 override_content_types_.erase(part);
156 }
157
parts_with_overriden_types() const158 std::vector<path> manifest::parts_with_overriden_types() const
159 {
160 std::vector<path> overriden;
161
162 for (const auto &part : override_content_types_)
163 {
164 overriden.push_back(part.first);
165 }
166
167 return overriden;
168 }
169
relationships(const path & part) const170 std::vector<relationship> manifest::relationships(const path &part) const
171 {
172 if (relationships_.find(part) == relationships_.end())
173 {
174 return {};
175 }
176
177 std::vector<xlnt::relationship> relationships;
178
179 for (const auto &rel : relationships_.at(part))
180 {
181 relationships.push_back(rel.second);
182 }
183
184 return relationships;
185 }
186
relationship(const path & part,const std::string & rel_id) const187 relationship manifest::relationship(const path &part, const std::string &rel_id) const
188 {
189 if (relationships_.find(part) == relationships_.end())
190 {
191 throw key_not_found();
192 }
193
194 for (const auto &rel : relationships_.at(part))
195 {
196 if (rel.second.id() == rel_id)
197 {
198 return rel.second;
199 }
200 }
201
202 throw key_not_found();
203 }
204
parts() const205 std::vector<path> manifest::parts() const
206 {
207 std::unordered_set<path> parts;
208
209 for (const auto &part_rels : relationships_)
210 {
211 parts.insert(part_rels.first);
212
213 for (const auto &rel : part_rels.second)
214 {
215 if (rel.second.target_mode() == target_mode::internal)
216 {
217 parts.insert(rel.second.target().path());
218 }
219 }
220 }
221
222 return std::vector<path>(parts.begin(), parts.end());
223 }
224
register_relationship(const uri & source,relationship_type type,const uri & target,target_mode mode)225 std::string manifest::register_relationship(const uri &source,
226 relationship_type type, const uri &target, target_mode mode)
227 {
228 xlnt::relationship rel(next_relationship_id(source.path()), type, source, target, mode);
229 return register_relationship(rel);
230 }
231
register_relationship(const class relationship & rel)232 std::string manifest::register_relationship(const class relationship &rel)
233 {
234 relationships_[rel.source().path()][rel.id()] = rel;
235 return rel.id();
236 }
237
unregister_relationship(const uri & source,const std::string & rel_id)238 std::unordered_map<std::string, std::string> manifest::unregister_relationship(const uri &source, const std::string &rel_id)
239 {
240 // This shouldn't happen, but just in case...
241 if (rel_id.substr(0, 3) != "rId" || rel_id.size() < 4)
242 {
243 throw xlnt::invalid_parameter();
244 }
245
246 std::unordered_map<std::string, std::string> id_map;
247 auto rel_index = static_cast<std::size_t>(std::stoull(rel_id.substr(3)));
248 auto &part_rels = relationships_.at(source.path());
249
250 for (auto i = rel_index; i <= part_rels.size() + 1; ++i)
251 {
252 auto old_id = "rId" + std::to_string(i);
253
254 // Don't re-add the relationship to be deleted
255 if (i > rel_index)
256 {
257 // Shift all relationships with IDs greater than the deleted one
258 // down by one (e.g. rId7->rId6).
259 auto new_id = "rId" + std::to_string(i - 1);
260 const auto &old_rel = part_rels.at(old_id);
261 register_relationship(xlnt::relationship(new_id, old_rel.type(),
262 old_rel.source(), old_rel.target(), old_rel.target_mode()));
263 id_map[old_id] = new_id;
264 }
265
266 part_rels.erase(old_id);
267 }
268
269 return id_map;
270 }
271
has_default_type(const std::string & extension) const272 bool manifest::has_default_type(const std::string &extension) const
273 {
274 return default_content_types_.find(extension) != default_content_types_.end();
275 }
276
extensions_with_default_types() const277 std::vector<std::string> manifest::extensions_with_default_types() const
278 {
279 std::vector<std::string> extensions;
280
281 for (const auto &extension_type_pair : default_content_types_)
282 {
283 extensions.push_back(extension_type_pair.first);
284 }
285
286 return extensions;
287 }
288
default_type(const std::string & extension) const289 std::string manifest::default_type(const std::string &extension) const
290 {
291 if (default_content_types_.find(extension) == default_content_types_.end())
292 {
293 throw key_not_found();
294 }
295
296 return default_content_types_.at(extension);
297 }
298
register_default_type(const std::string & extension,const std::string & content_type)299 void manifest::register_default_type(const std::string &extension, const std::string &content_type)
300 {
301 default_content_types_[extension] = content_type;
302 }
303
unregister_default_type(const std::string & extension)304 void manifest::unregister_default_type(const std::string &extension)
305 {
306 default_content_types_.erase(extension);
307 }
308
next_relationship_id(const path & part) const309 std::string manifest::next_relationship_id(const path &part) const
310 {
311 if (relationships_.find(part) == relationships_.end()) return "rId1";
312
313 std::size_t index = 1;
314 const auto &part_rels = relationships_.at(part);
315
316 while (part_rels.find("rId" + std::to_string(index)) != part_rels.end())
317 {
318 ++index;
319 }
320
321 return "rId" + std::to_string(index);
322 }
323
has_override_type(const xlnt::path & part) const324 bool manifest::has_override_type(const xlnt::path &part) const
325 {
326 return override_content_types_.find(part) != override_content_types_.end();
327 }
328
override_type(const xlnt::path & part) const329 std::string manifest::override_type(const xlnt::path &part) const
330 {
331 if (!has_override_type(part))
332 {
333 throw key_not_found();
334 }
335
336 return override_content_types_.at(part);
337 }
338
operator ==(const manifest & other) const339 bool manifest::operator==(const manifest &other) const
340 {
341 return default_content_types_ == other.default_content_types_
342 && override_content_types_ == other.override_content_types_
343 && relationships_ == other.relationships_;
344 }
345
346 } // namespace xlnt
347