2015-12-25 06:10:02 +08:00
|
|
|
// Copyright (c) 2014-2016 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
|
|
|
|
// 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
|
2016-07-04 07:22:08 +08:00
|
|
|
|
|
|
|
#include <pugixml.hpp>
|
2015-10-30 01:46:56 +08:00
|
|
|
|
2015-11-03 22:06:01 +08:00
|
|
|
#include <detail/constants.hpp>
|
2016-07-04 07:22:08 +08:00
|
|
|
#include <detail/manifest_serializer.hpp>
|
|
|
|
#include <xlnt/packaging/manifest.hpp>
|
2015-10-30 01:46:56 +08:00
|
|
|
|
|
|
|
namespace xlnt {
|
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
manifest_serializer::manifest_serializer(manifest &m) : manifest_(m)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2015-10-31 06:54:04 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
void manifest_serializer::read_manifest(const pugi::xml_document &xml)
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
const auto root_node = xml.child("Types");
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
for (const auto child : root_node.children())
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
if (child.name() == std::string("Default"))
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
manifest_.add_default_type(child.attribute("Extension").value(), child.attribute("ContentType").value());
|
2015-10-31 06:54:04 +08:00
|
|
|
}
|
2016-07-04 07:22:08 +08:00
|
|
|
else if (child.name() == std::string("Override"))
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
manifest_.add_override_type(child.attribute("PartName").value(), child.attribute("ContentType").value());
|
2015-10-31 06:54:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
void manifest_serializer::write_manifest(pugi::xml_document &xml) const
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto root_node = xml.append_child("Types");
|
|
|
|
root_node.append_attribute("xmlns").set_value(constants::Namespace("content-types").c_str());
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
for (const auto default_type : manifest_.get_default_types())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto type_node = root_node.append_child("Default");
|
|
|
|
type_node.append_attribute("Extension").set_value(default_type.get_extension().c_str());
|
|
|
|
type_node.append_attribute("ContentType").set_value(default_type.get_content_type().c_str());
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
for (const auto override_type : manifest_.get_override_types())
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2016-07-04 07:22:08 +08:00
|
|
|
auto type_node = root_node.append_child("Override");
|
|
|
|
type_node.append_attribute("PartName").set_value(override_type.get_part_name().c_str());
|
|
|
|
type_node.append_attribute("ContentType").set_value(override_type.get_content_type().c_str());
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
2015-10-31 06:54:04 +08:00
|
|
|
}
|
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string manifest_serializer::determine_document_type() const
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2015-11-03 05:45:05 +08:00
|
|
|
if (!manifest_.has_override_type(constants::ArcWorkbook()))
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
|
|
|
return "unsupported";
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string type = manifest_.get_override_type(constants::ArcWorkbook());
|
2015-11-01 22:43:01 +08:00
|
|
|
|
|
|
|
if (type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
|
|
|
return "excel";
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
else if (type == "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml")
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
|
|
|
return "powerpoint";
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
else if (type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml")
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
|
|
|
return "word";
|
|
|
|
}
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
return "unsupported";
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace xlnt
|