improve manifest

This commit is contained in:
Thomas Fussell 2016-07-22 22:24:45 -04:00
parent d3486bb986
commit c1364199ee
4 changed files with 25 additions and 61 deletions

View File

@ -24,7 +24,7 @@
#pragma once
#include <string>
#include <vector>
#include <unordered_map>
#include <xlnt/xlnt_config.hpp>
#include <xlnt/packaging/default_type.hpp>
@ -41,17 +41,17 @@ class XLNT_CLASS manifest
public:
bool has_default_type(const std::string &extension) const;
std::string get_default_type(const std::string &extension) const;
const std::vector<default_type> &get_default_types() const;
const std::unordered_map<std::string, default_type> &get_default_types() const;
void add_default_type(const std::string &extension, const std::string &content_type);
bool has_override_type(const std::string &part_name) const;
std::string get_override_type(const std::string &part_name) const;
const std::vector<override_type> &get_override_types() const;
const std::unordered_map<std::string, override_type> &get_override_types() const;
void add_override_type(const std::string &part_name, const std::string &content_type);
private:
std::vector<default_type> default_types_;
std::vector<override_type> override_types_;
std::unordered_map<std::string, default_type> default_types_;
std::unordered_map<std::string, override_type> override_types_;
};
} // namespace xlnt

View File

@ -59,15 +59,15 @@ void manifest_serializer::write_manifest(pugi::xml_document &xml) const
for (const auto default_type : manifest_.get_default_types())
{
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());
type_node.append_attribute("Extension").set_value(default_type.second.get_extension().c_str());
type_node.append_attribute("ContentType").set_value(default_type.second.get_content_type().c_str());
}
for (const auto override_type : manifest_.get_override_types())
{
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());
type_node.append_attribute("PartName").set_value(override_type.second.get_part_name().c_str());
type_node.append_attribute("ContentType").set_value(override_type.second.get_content_type().c_str());
}
}

View File

@ -26,88 +26,47 @@
#include <xlnt/packaging/manifest.hpp>
namespace {
bool match_path(const std::string &path, const std::string &comparand)
{
if (path == comparand)
{
return true;
}
if (path[0] != '/' && path[0] != '.' && comparand[0] == '/')
{
return match_path("/" + path, comparand);
}
else if (comparand[0] != '/' && comparand[0] != '.' && path[0] == '/')
{
return match_path(path, "/" + comparand);
}
return false;
}
} // namespace
namespace xlnt {
bool manifest::has_default_type(const std::string &extension) const
{
return std::find_if(default_types_.begin(), default_types_.end(),
[&](const default_type &d) { return d.get_extension() == extension; }) != default_types_.end();
return default_types_.find(extension) != default_types_.end();
}
bool manifest::has_override_type(const std::string &part_name) const
{
return std::find_if(override_types_.begin(), override_types_.end(), [&](const override_type &d) {
return match_path(d.get_part_name(), part_name);
}) != override_types_.end();
auto absolute = part_name.front() == '/' ? part_name : ("/" + part_name);
return override_types_.find(absolute) != override_types_.end();
}
void manifest::add_default_type(const std::string &extension, const std::string &content_type)
{
if(has_default_type(extension)) return;
default_types_.push_back(default_type(extension, content_type));
default_types_[extension] = default_type(extension, content_type);
}
void manifest::add_override_type(const std::string &part_name, const std::string &content_type)
{
if(has_override_type(part_name)) return;
override_types_.push_back(override_type(part_name, content_type));
auto absolute = part_name.front() == '/' ? part_name : ("/" + part_name);
override_types_[absolute] = override_type(absolute, content_type);
}
std::string manifest::get_default_type(const std::string &extension) const
{
auto match = std::find_if(default_types_.begin(), default_types_.end(),
[&](const default_type &d) { return d.get_extension() == extension; });
if (match == default_types_.end())
{
throw std::runtime_error("no default type found for extension: " + extension);
}
return match->get_content_type();
return default_types_.at(extension).get_content_type();
}
std::string manifest::get_override_type(const std::string &part_name) const
{
auto match = std::find_if(override_types_.begin(), override_types_.end(),
[&](const override_type &d) { return match_path(d.get_part_name(), part_name); });
if (match == override_types_.end())
{
throw std::runtime_error("no default type found for part name: " + part_name);
}
return match->get_content_type();
auto absolute = part_name.front() == '/' ? part_name : ("/" + part_name);
return override_types_.at(absolute).get_content_type();
}
const std::vector<default_type> &manifest::get_default_types() const
const std::unordered_map<std::string, default_type> &manifest::get_default_types() const
{
return default_types_;
}
const std::vector<override_type> &manifest::get_override_types() const
const std::unordered_map<std::string, override_type> &manifest::get_override_types() const
{
return override_types_;
}

View File

@ -53,6 +53,11 @@ public:
wb.add_style(s);
TS_ASSERT_EQUALS(e.get_stylesheet().styles.size(), 2);
xlnt::style copy;
copy = s;
TS_ASSERT_EQUALS(s, copy);
}
/*