Add debug output for a relationship mismatch

This commit is contained in:
Crzyrndm 2018-07-09 15:24:03 +12:00
parent 53c193433f
commit ec02121c15

View File

@ -3,17 +3,17 @@
#include <sstream> #include <sstream>
#include <unordered_set> #include <unordered_set>
#include <xlnt/packaging/manifest.hpp>
#include <xlnt/workbook/workbook.hpp>
#include <detail/external/include_libstudxml.hpp> #include <detail/external/include_libstudxml.hpp>
#include <detail/serialization/vector_streambuf.hpp> #include <detail/serialization/vector_streambuf.hpp>
#include <detail/serialization/zstream.hpp> #include <detail/serialization/zstream.hpp>
#include <xlnt/packaging/manifest.hpp>
#include <xlnt/workbook/workbook.hpp>
class xml_helper class xml_helper
{ {
public: public:
static bool compare_files(const std::string &left, static bool compare_files(const std::string &left,
const std::string &right, const std::string &content_type) const std::string &right, const std::string &content_type)
{ {
// content types are stored in unordered maps, too complicated to compare // content types are stored in unordered maps, too complicated to compare
if (content_type == "[Content_Types].xml") if (content_type == "[Content_Types].xml")
@ -34,7 +34,7 @@ public:
} }
auto is_xml = (content_type.substr(0, 12) == "application/" auto is_xml = (content_type.substr(0, 12) == "application/"
&& content_type.substr(content_type.size() - 4) == "+xml") && content_type.substr(content_type.size() - 4) == "+xml")
|| content_type == "application/xml" || content_type == "application/xml"
|| content_type == "[Content_Types].xml" || content_type == "[Content_Types].xml"
|| content_type == "application/vnd.openxmlformats-officedocument.vmlDrawing"; || content_type == "application/vnd.openxmlformats-officedocument.vmlDrawing";
@ -63,8 +63,7 @@ public:
bool difference = false; bool difference = false;
auto right_iter = right_parser.begin(); auto right_iter = right_parser.begin();
auto is_whitespace = [](const std::string &v) auto is_whitespace = [](const std::string &v) {
{
return v.find_first_not_of("\n\r\t ") == std::string::npos; return v.find_first_not_of("\n\r\t ") == std::string::npos;
}; };
@ -198,26 +197,26 @@ public:
++right_iter; ++right_iter;
} }
if (difference && !suppress_debug_info) if (difference && !suppress_debug_info)
{ {
std::cout << "documents don't match" << std::endl; std::cout << "documents don't match" << std::endl;
std::cout << "left:" << std::endl; std::cout << "left:" << std::endl;
for (auto c : left) for (auto c : left)
{ {
std::cout << c << std::flush; std::cout << c << std::flush;
} }
std::cout << std::endl; std::cout << std::endl;
std::cout << "right:" << std::endl; std::cout << "right:" << std::endl;
for (auto c : right) for (auto c : right)
{ {
std::cout << c << std::flush; std::cout << c << std::flush;
} }
std::cout << std::endl; std::cout << std::endl;
} }
return !difference; return !difference;
} }
static bool compare_relationships(const xlnt::manifest &left, static bool compare_relationships(const xlnt::manifest &left,
@ -271,27 +270,26 @@ public:
return true; return true;
} }
static bool xlsx_archives_match(const std::vector<std::uint8_t> &left, static bool xlsx_archives_match(const std::vector<std::uint8_t> &left,
const std::vector<std::uint8_t> &right) const std::vector<std::uint8_t> &right)
{ {
xlnt::detail::vector_istreambuf left_buffer(left); xlnt::detail::vector_istreambuf left_buffer(left);
std::istream left_stream(&left_buffer); std::istream left_stream(&left_buffer);
xlnt::detail::izstream left_archive(left_stream); xlnt::detail::izstream left_archive(left_stream);
const auto left_info = left_archive.files(); const auto left_info = left_archive.files();
xlnt::detail::vector_istreambuf right_buffer(right); xlnt::detail::vector_istreambuf right_buffer(right);
std::istream right_stream(&right_buffer); std::istream right_stream(&right_buffer);
xlnt::detail::izstream right_archive(right_stream); xlnt::detail::izstream right_archive(right_stream);
const auto right_info = right_archive.files(); const auto right_info = right_archive.files();
auto difference_is_missing_calc_chain = false; auto difference_is_missing_calc_chain = false;
if (std::abs(int(left_info.size()) - int(right_info.size())) == 1) if (std::abs(int(left_info.size()) - int(right_info.size())) == 1)
{ {
auto is_calc_chain = [](const xlnt::path &p) auto is_calc_chain = [](const xlnt::path &p) {
{
return p.filename() == "calcChain.xml"; return p.filename() == "calcChain.xml";
}; };
@ -306,7 +304,7 @@ public:
} }
} }
if (left_info.size() != right_info.size() && ! difference_is_missing_calc_chain) if (left_info.size() != right_info.size() && !difference_is_missing_calc_chain)
{ {
std::cout << "left has a different number of files than right" << std::endl; std::cout << "left has a different number of files than right" << std::endl;
@ -338,12 +336,41 @@ public:
if (!compare_relationships(left_manifest, right_manifest)) if (!compare_relationships(left_manifest, right_manifest))
{ {
std::cout << "relationship mismatch\n"
<< "Left:\n";
for (const auto &part : left_manifest.parts())
{
std::cout << "-part: " << part.string() << '\n';
auto rels = left_manifest.relationships(part);
for (auto &rel : rels)
{
std::cout << rel.id() << ':'
<< static_cast<int>(rel.type())
<< ':' << static_cast<int>(rel.target_mode())
<< ':' << rel.source().path().string()
<< ':' << rel.target().path().string() << '\n';
}
}
std::cout << "\nRight:\n";
for (const auto &part : right_manifest.parts())
{
std::cout << "-part: " << part.string() << '\n';
auto rels = right_manifest.relationships(part);
for (auto &rel : rels)
{
std::cout << rel.id()
<< ':' << static_cast<int>(rel.type())
<< ':' << static_cast<int>(rel.target_mode())
<< ':' << rel.source().path().string()
<< ':' << rel.target().path().string() << '\n';
}
}
return false; return false;
} }
for (auto left_member : left_info) for (auto left_member : left_info)
{ {
if (!right_archive.has_file(left_member)) if (!right_archive.has_file(left_member))
{ {
if (difference_is_missing_calc_chain) if (difference_is_missing_calc_chain)
{ {
@ -357,32 +384,34 @@ public:
} }
auto left_content_type = left_member.string() == "[Content_Types].xml" auto left_content_type = left_member.string() == "[Content_Types].xml"
? "[Content_Types].xml" : left_manifest.content_type(left_member); ? "[Content_Types].xml"
: left_manifest.content_type(left_member);
auto right_content_type = left_member.string() == "[Content_Types].xml" auto right_content_type = left_member.string() == "[Content_Types].xml"
? "[Content_Types].xml" : right_manifest.content_type(left_member); ? "[Content_Types].xml"
: right_manifest.content_type(left_member);
if (left_content_type != right_content_type) if (left_content_type != right_content_type)
{ {
std::cout << "content types differ: " std::cout << "content types differ: "
<< left_member.string() << left_member.string()
<< " " << " "
<< left_content_type << left_content_type
<< " " << " "
<< right_content_type << right_content_type
<< std::endl; << std::endl;
match = false; match = false;
break; break;
} }
if (!compare_files(left_archive.read(left_member), if (!compare_files(left_archive.read(left_member),
right_archive.read(left_member), left_content_type)) right_archive.read(left_member), left_content_type))
{ {
std::cout << left_member.string() << std::endl; std::cout << left_member.string() << std::endl;
match = false; match = false;
break; break;
} }
} }
return match; return match;
} }
}; };