2015-12-25 04:51:11 +08:00
|
|
|
// Copyright (c) 2014-2015 Thomas Fussell
|
|
|
|
// 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
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
|
|
|
|
// 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
|
2015-11-02 01:31:29 +08:00
|
|
|
#include <xlnt/serialization/relationship_serializer.hpp>
|
2015-11-11 07:58:54 +08:00
|
|
|
|
2015-11-03 21:38:09 +08:00
|
|
|
#include <xlnt/packaging/relationship.hpp>
|
|
|
|
#include <xlnt/packaging/zip_file.hpp>
|
2015-11-02 01:31:29 +08:00
|
|
|
#include <xlnt/serialization/xml_document.hpp>
|
|
|
|
#include <xlnt/serialization/xml_node.hpp>
|
2015-10-30 01:46:56 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
#include "detail/constants.hpp"
|
2015-10-30 01:46:56 +08:00
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
namespace {
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string make_rels_name(const std::string &target)
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
|
|
|
const char sep = '/';
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
if (target.empty() || target.back() == sep)
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
|
|
|
return target + "_rels/.rels";
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
auto sep_pos = target.find_last_of(sep);
|
|
|
|
auto first_part = target.substr(0, sep_pos + 1);
|
|
|
|
auto last_part = target.substr(sep_pos + 1);
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
return first_part + "_rels/" + last_part + ".rels";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 01:46:56 +08:00
|
|
|
namespace xlnt {
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-04 07:26:33 +08:00
|
|
|
relationship_serializer::relationship_serializer(zip_file &archive) : archive_(archive)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::vector<relationship> relationship_serializer::read_relationships(const std::string &target)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2015-10-31 06:54:04 +08:00
|
|
|
xml_document xml;
|
2015-11-04 07:26:33 +08:00
|
|
|
xml.from_string(archive_.read(make_rels_name(target)));
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
auto root_node = xml.get_child("Relationships");
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
std::vector<relationship> relationships;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
for (auto relationship_node : root_node.get_children())
|
2015-10-30 07:37:07 +08:00
|
|
|
{
|
2015-11-01 22:43:01 +08:00
|
|
|
if (relationship_node.get_name() != "Relationship")
|
2015-10-30 07:37:07 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string id = relationship_node.get_attribute("Id");
|
|
|
|
std::string type = relationship_node.get_attribute("Type");
|
|
|
|
std::string rel_target = relationship_node.get_attribute("Target");
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-03 05:45:05 +08:00
|
|
|
relationships.push_back(xlnt::relationship(type, id, rel_target));
|
2015-10-30 07:37:07 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
return relationships;
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
bool relationship_serializer::write_relationships(const std::vector<relationship> &relationships,
|
2015-11-11 07:58:54 +08:00
|
|
|
const std::string &target)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2015-10-31 06:54:04 +08:00
|
|
|
xml_document xml;
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
auto root_node = xml.add_child("Relationships");
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-03 05:45:05 +08:00
|
|
|
xml.add_namespace("", constants::Namespace("relationships"));
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
for (const auto &relationship : relationships)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2015-10-30 07:37:07 +08:00
|
|
|
auto relationship_node = root_node.add_child("Relationship");
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-30 07:37:07 +08:00
|
|
|
relationship_node.add_attribute("Id", relationship.get_id());
|
2015-11-03 05:45:05 +08:00
|
|
|
relationship_node.add_attribute("Target", relationship.get_target_uri());
|
2015-10-30 07:37:07 +08:00
|
|
|
relationship_node.add_attribute("Type", relationship.get_type_string());
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
if (relationship.get_target_mode() == target_mode::external)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2015-10-30 07:37:07 +08:00
|
|
|
relationship_node.add_attribute("TargetMode", "External");
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-04 07:26:33 +08:00
|
|
|
archive_.writestr(make_rels_name(target), xml.to_string());
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-30 01:46:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|