2021-08-22 20:23:18 +08:00
|
|
|
// Copyright (c) 2014-2021 Thomas Fussell
|
2015-12-25 04:51:11 +08:00
|
|
|
// Copyright (c) 2010-2015 openpyxl
|
|
|
|
//
|
|
|
|
// 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,
|
2015-12-25 04:51:11 +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
|
2016-08-12 12:22:14 +08:00
|
|
|
|
2015-11-01 22:48:51 +08:00
|
|
|
#include <algorithm>
|
2016-08-12 12:22:14 +08:00
|
|
|
#include <unordered_set>
|
2015-11-01 22:48:51 +08:00
|
|
|
|
2015-11-03 21:46:40 +08:00
|
|
|
#include <xlnt/packaging/manifest.hpp>
|
2016-08-05 13:52:05 +08:00
|
|
|
#include <xlnt/utils/exceptions.hpp>
|
|
|
|
|
2015-10-30 01:46:56 +08:00
|
|
|
namespace xlnt {
|
|
|
|
|
2016-07-30 05:50:33 +08:00
|
|
|
void manifest::clear()
|
|
|
|
{
|
2016-08-12 12:22:14 +08:00
|
|
|
default_content_types_.clear();
|
|
|
|
override_content_types_.clear();
|
|
|
|
relationships_.clear();
|
2016-07-30 05:50:33 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
path manifest::canonicalize(const std::vector<xlnt::relationship> &rels) const
|
2016-11-14 00:52:39 +08:00
|
|
|
{
|
|
|
|
xlnt::path relative;
|
|
|
|
|
|
|
|
for (auto component : rels)
|
|
|
|
{
|
|
|
|
if (component == rels.back())
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
relative = relative.append(component.target().path());
|
2016-11-14 00:52:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
relative = relative.append(component.target().path().parent());
|
2016-11-14 00:52:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> absolute_parts;
|
|
|
|
|
|
|
|
for (const auto &component : relative.split())
|
|
|
|
{
|
|
|
|
if (component == ".") continue;
|
|
|
|
|
|
|
|
if (component == "..")
|
|
|
|
{
|
|
|
|
absolute_parts.pop_back();
|
|
|
|
continue;
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-14 00:52:39 +08:00
|
|
|
absolute_parts.push_back(component);
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-14 00:52:39 +08:00
|
|
|
xlnt::path result;
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-14 00:52:39 +08:00
|
|
|
for (const auto &component : absolute_parts)
|
|
|
|
{
|
|
|
|
result = result.append(component);
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-11-14 00:52:39 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-06-23 09:19:34 +08:00
|
|
|
bool manifest::has_relationship(const path &path, relationship_type type) const
|
2016-08-05 13:52:05 +08:00
|
|
|
{
|
2018-06-23 09:19:34 +08:00
|
|
|
auto rels = relationships_.find(path);
|
|
|
|
if (rels == relationships_.end())
|
2016-08-12 12:22:14 +08:00
|
|
|
{
|
2018-06-23 09:19:34 +08:00
|
|
|
return false;
|
2016-08-12 12:22:14 +08:00
|
|
|
}
|
2019-12-27 01:03:12 +08:00
|
|
|
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; });
|
2018-06-23 09:19:34 +08:00
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2018-06-23 09:19:34 +08:00
|
|
|
bool manifest::has_relationship(const path &path, const std::string &rel_id) const
|
|
|
|
{
|
|
|
|
auto rels = relationships_.find(path);
|
2019-12-27 01:03:12 +08:00
|
|
|
if (rels == relationships_.end())
|
2018-06-23 09:19:34 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return rels->second.find(rel_id) != rels->second.end();
|
2016-08-05 13:52:05 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
relationship manifest::relationship(const path &part, relationship_type type) const
|
2016-08-05 13:52:05 +08:00
|
|
|
{
|
2016-08-12 12:22:14 +08:00
|
|
|
if (relationships_.find(part) == relationships_.end()) throw key_not_found();
|
2016-08-05 13:52:05 +08:00
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
for (const auto &rel : relationships_.at(part))
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
if (rel.second.type() == type) return rel.second;
|
2016-08-12 12:22:14 +08:00
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
|
|
|
throw key_not_found();
|
2016-08-05 13:52:05 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
std::vector<xlnt::relationship> manifest::relationships(const path &part, relationship_type type) const
|
2016-08-05 13:52:05 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
std::vector<xlnt::relationship> matches;
|
2016-08-05 13:52:05 +08:00
|
|
|
|
2016-11-30 08:05:47 +08:00
|
|
|
if (has_relationship(part, type))
|
2016-08-12 12:22:14 +08:00
|
|
|
{
|
2016-11-30 08:05:47 +08:00
|
|
|
for (const auto &rel : relationships_.at(part))
|
2016-08-12 12:22:14 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
if (rel.second.type() == type)
|
2016-11-30 08:05:47 +08:00
|
|
|
{
|
|
|
|
matches.push_back(rel.second);
|
|
|
|
}
|
2016-08-12 12:22:14 +08:00
|
|
|
}
|
|
|
|
}
|
2016-08-05 13:52:05 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return matches;
|
2016-08-05 13:52:05 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
std::string manifest::content_type(const path &part) const
|
2016-08-05 13:52:05 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
auto absolute = part.resolve(path("/"));
|
2016-08-13 11:06:25 +08:00
|
|
|
|
|
|
|
if (has_override_type(absolute))
|
2016-08-12 12:22:14 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
return override_type(absolute);
|
2016-08-12 12:22:14 +08:00
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
if (has_default_type(part.extension()))
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
return default_type(part.extension());
|
2016-08-12 12:22:14 +08:00
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
throw key_not_found();
|
2016-08-05 13:52:05 +08:00
|
|
|
}
|
|
|
|
|
2016-08-06 22:40:17 +08:00
|
|
|
void manifest::register_override_type(const path &part, const std::string &content_type)
|
2016-08-05 13:52:05 +08:00
|
|
|
{
|
2016-08-12 12:22:14 +08:00
|
|
|
override_content_types_[part] = content_type;
|
2016-08-05 13:52:05 +08:00
|
|
|
}
|
|
|
|
|
2016-10-23 01:53:44 +08:00
|
|
|
void manifest::unregister_override_type(const path &part)
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
override_content_types_.erase(part);
|
2016-10-23 01:53:44 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
std::vector<path> manifest::parts_with_overriden_types() const
|
2016-08-05 13:52:05 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
std::vector<path> overriden;
|
2016-08-05 13:52:05 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
for (const auto &part : override_content_types_)
|
|
|
|
{
|
2016-08-12 12:22:14 +08:00
|
|
|
overriden.push_back(part.first);
|
2016-12-24 23:04:57 +08:00
|
|
|
}
|
2016-08-05 13:52:05 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return overriden;
|
2016-08-05 13:52:05 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
std::vector<relationship> manifest::relationships(const path &part) const
|
2016-08-05 13:52:05 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
if (relationships_.find(part) == relationships_.end())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2016-08-05 13:52:05 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
std::vector<xlnt::relationship> relationships;
|
2016-08-05 13:52:05 +08:00
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
for (const auto &rel : relationships_.at(part))
|
|
|
|
{
|
|
|
|
relationships.push_back(rel.second);
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
|
|
|
return relationships;
|
2016-08-05 13:52:05 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
relationship manifest::relationship(const path &part, const std::string &rel_id) const
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
if (relationships_.find(part) == relationships_.end())
|
|
|
|
{
|
|
|
|
throw key_not_found();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &rel : relationships_.at(part))
|
|
|
|
{
|
|
|
|
if (rel.second.id() == rel_id)
|
|
|
|
{
|
|
|
|
return rel.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw key_not_found();
|
2016-08-05 13:52:05 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
std::vector<path> manifest::parts() const
|
2016-08-05 13:52:05 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
std::unordered_set<path> parts;
|
2015-10-30 01:46:56 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
for (const auto &part_rels : relationships_)
|
|
|
|
{
|
2016-08-12 12:22:14 +08:00
|
|
|
parts.insert(part_rels.first);
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
for (const auto &rel : part_rels.second)
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
if (rel.second.target_mode() == target_mode::internal)
|
2016-08-12 12:22:14 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
parts.insert(rel.second.target().path());
|
2016-08-12 12:22:14 +08:00
|
|
|
}
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
}
|
2016-08-06 22:40:17 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return std::vector<path>(parts.begin(), parts.end());
|
2015-10-31 06:54:04 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:08:57 +08:00
|
|
|
std::string manifest::register_relationship(const uri &source,
|
|
|
|
relationship_type type, const uri &target, target_mode mode)
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2016-12-04 20:29:10 +08:00
|
|
|
xlnt::relationship rel(next_relationship_id(source.path()), type, source, target, mode);
|
2016-12-24 23:04:57 +08:00
|
|
|
return register_relationship(rel);
|
2015-10-31 06:54:04 +08:00
|
|
|
}
|
|
|
|
|
2016-12-04 20:29:10 +08:00
|
|
|
std::string manifest::register_relationship(const class relationship &rel)
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2016-12-04 20:29:10 +08:00
|
|
|
relationships_[rel.source().path()][rel.id()] = rel;
|
2016-12-24 23:04:57 +08:00
|
|
|
return rel.id();
|
2016-08-05 13:52:05 +08:00
|
|
|
}
|
|
|
|
|
2017-01-09 03:12:55 +08:00
|
|
|
std::unordered_map<std::string, std::string> manifest::unregister_relationship(const uri &source, const std::string &rel_id)
|
2016-10-23 01:53:44 +08:00
|
|
|
{
|
2017-01-09 03:12:55 +08:00
|
|
|
// This shouldn't happen, but just in case...
|
|
|
|
if (rel_id.substr(0, 3) != "rId" || rel_id.size() < 4)
|
|
|
|
{
|
|
|
|
throw xlnt::invalid_parameter();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_map<std::string, std::string> id_map;
|
|
|
|
auto rel_index = static_cast<std::size_t>(std::stoull(rel_id.substr(3)));
|
|
|
|
auto &part_rels = relationships_.at(source.path());
|
|
|
|
|
|
|
|
for (auto i = rel_index; i <= part_rels.size() + 1; ++i)
|
|
|
|
{
|
|
|
|
auto old_id = "rId" + std::to_string(i);
|
|
|
|
|
|
|
|
// Don't re-add the relationship to be deleted
|
|
|
|
if (i > rel_index)
|
|
|
|
{
|
|
|
|
// Shift all relationships with IDs greater than the deleted one
|
|
|
|
// down by one (e.g. rId7->rId6).
|
|
|
|
auto new_id = "rId" + std::to_string(i - 1);
|
|
|
|
const auto &old_rel = part_rels.at(old_id);
|
|
|
|
register_relationship(xlnt::relationship(new_id, old_rel.type(),
|
|
|
|
old_rel.source(), old_rel.target(), old_rel.target_mode()));
|
|
|
|
id_map[old_id] = new_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
part_rels.erase(old_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return id_map;
|
2016-10-23 01:53:44 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 13:52:05 +08:00
|
|
|
bool manifest::has_default_type(const std::string &extension) const
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
return default_content_types_.find(extension) != default_content_types_.end();
|
2016-08-05 13:52:05 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
std::vector<std::string> manifest::extensions_with_default_types() const
|
2016-08-05 13:52:05 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
std::vector<std::string> extensions;
|
2016-08-05 13:52:05 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
for (const auto &extension_type_pair : default_content_types_)
|
|
|
|
{
|
|
|
|
extensions.push_back(extension_type_pair.first);
|
|
|
|
}
|
2016-08-05 13:52:05 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return extensions;
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
std::string manifest::default_type(const std::string &extension) const
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
if (default_content_types_.find(extension) == default_content_types_.end())
|
|
|
|
{
|
|
|
|
throw key_not_found();
|
|
|
|
}
|
2016-08-05 13:52:05 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return default_content_types_.at(extension);
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 13:52:05 +08:00
|
|
|
void manifest::register_default_type(const std::string &extension, const std::string &content_type)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
default_content_types_[extension] = content_type;
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 13:52:05 +08:00
|
|
|
void manifest::unregister_default_type(const std::string &extension)
|
2015-11-20 11:54:54 +08:00
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
default_content_types_.erase(extension);
|
2015-11-20 11:54:54 +08:00
|
|
|
}
|
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
std::string manifest::next_relationship_id(const path &part) const
|
2015-11-20 11:54:54 +08:00
|
|
|
{
|
2016-08-12 12:22:14 +08:00
|
|
|
if (relationships_.find(part) == relationships_.end()) return "rId1";
|
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
std::size_t index = 1;
|
|
|
|
const auto &part_rels = relationships_.at(part);
|
2016-08-05 13:52:05 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
while (part_rels.find("rId" + std::to_string(index)) != part_rels.end())
|
|
|
|
{
|
|
|
|
++index;
|
|
|
|
}
|
2016-08-05 13:52:05 +08:00
|
|
|
|
2016-12-24 23:04:57 +08:00
|
|
|
return "rId" + std::to_string(index);
|
2016-08-05 13:52:05 +08:00
|
|
|
}
|
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
bool manifest::has_override_type(const xlnt::path &part) const
|
2016-08-05 13:52:05 +08:00
|
|
|
{
|
2016-08-12 12:22:14 +08:00
|
|
|
return override_content_types_.find(part) != override_content_types_.end();
|
|
|
|
}
|
2016-08-05 13:52:05 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
std::string manifest::override_type(const xlnt::path &part) const
|
2016-08-12 12:22:14 +08:00
|
|
|
{
|
|
|
|
if (!has_override_type(part))
|
|
|
|
{
|
|
|
|
throw key_not_found();
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2016-08-12 12:22:14 +08:00
|
|
|
return override_content_types_.at(part);
|
2015-11-20 11:54:54 +08:00
|
|
|
}
|
|
|
|
|
2018-07-03 10:05:07 +08:00
|
|
|
bool manifest::operator==(const manifest &other) const
|
|
|
|
{
|
|
|
|
return default_content_types_ == other.default_content_types_
|
|
|
|
&& override_content_types_ == other.override_content_types_
|
|
|
|
&& relationships_ == other.relationships_;
|
|
|
|
}
|
|
|
|
|
2015-10-30 01:46:56 +08:00
|
|
|
} // namespace xlnt
|