Skip to content

Commit

Permalink
dedupliacte
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Dec 3, 2023
1 parent 30a0b21 commit e48bdf5
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 146 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ add_library(odr
"src/odr/internal/cfb/cfb_impl.cpp"
"src/odr/internal/cfb/cfb_util.cpp"

"src/odr/internal/common/document.cpp"
"src/odr/internal/common/document_element.cpp"
"src/odr/internal/common/file.cpp"
"src/odr/internal/common/filesystem.cpp"
Expand Down
25 changes: 25 additions & 0 deletions src/odr/internal/common/document.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <odr/internal/common/document.hpp>

#include <odr/internal/abstract/filesystem.hpp>
#include <odr/internal/common/document_element.hpp>

#include <utility>

namespace odr::internal::common {

Document::Document(FileType file_type, DocumentType document_type,
std::shared_ptr<abstract::ReadableFilesystem> filesystem)
: m_file_type{file_type}, m_document_type{document_type},
m_filesystem{std::move(filesystem)} {}

FileType Document::file_type() const noexcept { return m_file_type; }

DocumentType Document::document_type() const noexcept {
return m_document_type;
}

std::shared_ptr<abstract::ReadableFilesystem> Document::files() const noexcept {
return m_filesystem;
}

} // namespace odr::internal::common
56 changes: 56 additions & 0 deletions src/odr/internal/common/document.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef ODR_INTERNAL_COMMON_DOCUMENT_H
#define ODR_INTERNAL_COMMON_DOCUMENT_H

#include <odr/internal/abstract/document.hpp>

#include <vector>

namespace odr::internal::abstract {
class ReadableFilesystem;
} // namespace odr::internal::abstract

namespace odr::internal::common {
class Element;

class Document : public abstract::Document {
public:
Document(FileType file_type, DocumentType document_type,
std::shared_ptr<abstract::ReadableFilesystem> files);

[[nodiscard]] FileType file_type() const noexcept final;
[[nodiscard]] DocumentType document_type() const noexcept final;

[[nodiscard]] std::shared_ptr<abstract::ReadableFilesystem>
files() const noexcept final;

protected:
FileType m_file_type;
DocumentType m_document_type;

std::shared_ptr<abstract::ReadableFilesystem> m_filesystem;

friend class Element;
};

template <typename element_t> class TemplateDocument : public Document {
public:
TemplateDocument(FileType file_type, DocumentType document_type,
std::shared_ptr<abstract::ReadableFilesystem> files)
: Document(file_type, document_type, std::move(files)) {}

[[nodiscard]] abstract::Element *root_element() const final {
return m_root_element;
}

void register_element_(std::unique_ptr<element_t> element) {
m_elements.push_back(std::move(element));
}

protected:
std::vector<std::unique_ptr<element_t>> m_elements;
element_t *m_root_element{};
};

} // namespace odr::internal::common

#endif // ODR_INTERNAL_COMMON_DOCUMENT_H
16 changes: 2 additions & 14 deletions src/odr/internal/odf/odf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace odr::internal::odf {

Document::Document(const FileType file_type, const DocumentType document_type,
std::shared_ptr<abstract::ReadableFilesystem> filesystem)
: m_file_type{file_type}, m_document_type{document_type},
m_filesystem{std::move(filesystem)} {
: common::TemplateDocument<Element>(file_type, document_type,
std::move(filesystem)) {
m_content_xml = util::xml::parse(*m_filesystem, "content.xml");

if (m_filesystem->exists("styles.xml")) {
Expand Down Expand Up @@ -96,16 +96,4 @@ void Document::save(const common::Path & /*path*/,
throw UnsupportedOperation();
}

FileType Document::file_type() const noexcept { return m_file_type; }

DocumentType Document::document_type() const noexcept {
return m_document_type;
}

std::shared_ptr<abstract::ReadableFilesystem> Document::files() const noexcept {
return m_filesystem;
}

abstract::Element *Document::root_element() const { return m_root_element; }

} // namespace odr::internal::odf
20 changes: 2 additions & 18 deletions src/odr/internal/odf/odf_document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <odr/file.hpp>

#include <odr/internal/abstract/document.hpp>
#include <odr/internal/abstract/document_element.hpp>
#include <odr/internal/common/document.hpp>
#include <odr/internal/common/path.hpp>
#include <odr/internal/odf/odf_element.hpp>
#include <odr/internal/odf/odf_style.hpp>
Expand All @@ -19,7 +19,7 @@ class ReadableFilesystem;

namespace odr::internal::odf {

class Document : public abstract::Document {
class Document : public common::TemplateDocument<Element> {
public:
Document(FileType file_type, DocumentType document_type,
std::shared_ptr<abstract::ReadableFilesystem> files);
Expand All @@ -30,26 +30,10 @@ class Document : public abstract::Document {
void save(const common::Path &path) const final;
void save(const common::Path &path, const char *password) const final;

[[nodiscard]] FileType file_type() const noexcept final;
[[nodiscard]] DocumentType document_type() const noexcept final;

[[nodiscard]] std::shared_ptr<abstract::ReadableFilesystem>
files() const noexcept final;

[[nodiscard]] abstract::Element *root_element() const final;

protected:
FileType m_file_type;
DocumentType m_document_type;

std::shared_ptr<abstract::ReadableFilesystem> m_filesystem;

pugi::xml_document m_content_xml;
pugi::xml_document m_styles_xml;

std::vector<std::unique_ptr<Element>> m_elements;
Element *m_root_element{};

StyleRegistry m_style_registry;

friend class Element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
namespace odr::internal::ooxml::presentation {

Document::Document(std::shared_ptr<abstract::ReadableFilesystem> filesystem)
: m_filesystem{std::move(filesystem)} {
: common::TemplateDocument<Element>(FileType::office_open_xml_presentation,
DocumentType::presentation,
std::move(filesystem)) {
m_document_xml = util::xml::parse(*m_filesystem, "ppt/presentation.xml");

m_root_element = parse_tree(*this, m_document_xml.document_element());
Expand All @@ -38,22 +40,4 @@ void Document::save(const common::Path & /*path*/,
throw UnsupportedOperation();
}

FileType Document::file_type() const noexcept {
return FileType::office_open_xml_presentation;
}

DocumentType Document::document_type() const noexcept {
return DocumentType::presentation;
}

std::shared_ptr<abstract::ReadableFilesystem> Document::files() const noexcept {
return m_filesystem;
}

abstract::Element *Document::root_element() const { return m_root_element; }

void Document::register_element_(std::unique_ptr<Element> element) {
m_elements.push_back(std::move(element));
}

} // namespace odr::internal::ooxml::presentation
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef ODR_INTERNAL_OOXML_PRESENTATION_H
#define ODR_INTERNAL_OOXML_PRESENTATION_H

#include <odr/internal/abstract/document.hpp>
#include <odr/internal/common/document.hpp>
#include <odr/internal/ooxml/presentation/ooxml_presentation_element.hpp>

#include <unordered_map>
Expand All @@ -11,7 +11,7 @@

namespace odr::internal::ooxml::presentation {

class Document final : public abstract::Document {
class Document final : public common::TemplateDocument<Element> {
public:
explicit Document(std::shared_ptr<abstract::ReadableFilesystem> filesystem);

Expand All @@ -21,25 +21,12 @@ class Document final : public abstract::Document {
void save(const common::Path &path) const final;
void save(const common::Path &path, const char *password) const final;

[[nodiscard]] FileType file_type() const noexcept final;
[[nodiscard]] DocumentType document_type() const noexcept final;

[[nodiscard]] std::shared_ptr<abstract::ReadableFilesystem>
files() const noexcept final;

[[nodiscard]] abstract::Element *root_element() const final;

void register_element_(std::unique_ptr<Element> element);

private:
std::shared_ptr<abstract::ReadableFilesystem> m_filesystem;

pugi::xml_document m_document_xml;
std::unordered_map<std::string, pugi::xml_document> m_slides_xml;

std::vector<std::unique_ptr<Element>> m_elements;
Element *m_root_element{};

friend class Element;
};

Expand Down
22 changes: 3 additions & 19 deletions src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace odr::internal::ooxml::spreadsheet {

Document::Document(std::shared_ptr<abstract::ReadableFilesystem> filesystem)
: m_filesystem{std::move(filesystem)} {
: common::TemplateDocument<Element>(FileType::office_open_xml_workbook,
DocumentType::spreadsheet,
std::move(filesystem)) {
m_workbook_xml = util::xml::parse(*m_filesystem, "xl/workbook.xml");
m_styles_xml = util::xml::parse(*m_filesystem, "xl/styles.xml");

Expand Down Expand Up @@ -65,22 +67,4 @@ void Document::save(const common::Path & /*path*/,
throw UnsupportedOperation();
}

FileType Document::file_type() const noexcept {
return FileType::office_open_xml_workbook;
}

DocumentType Document::document_type() const noexcept {
return DocumentType::spreadsheet;
}

std::shared_ptr<abstract::ReadableFilesystem> Document::files() const noexcept {
return m_filesystem;
}

abstract::Element *Document::root_element() const { return m_root_element; }

void Document::register_element_(std::unique_ptr<Element> element) {
m_elements.push_back(std::move(element));
}

} // namespace odr::internal::ooxml::spreadsheet
23 changes: 2 additions & 21 deletions src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <odr/file.hpp>

#include <odr/internal/abstract/document.hpp>
#include <odr/internal/common/document.hpp>
#include <odr/internal/common/path.hpp>
#include <odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_element.hpp>
#include <odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_style.hpp>
Expand All @@ -14,13 +14,9 @@

#include <pugixml.hpp>

namespace odr::internal::abstract {
class ReadableFilesystem;
} // namespace odr::internal::abstract

namespace odr::internal::ooxml::spreadsheet {

class Document final : public abstract::Document {
class Document final : public common::TemplateDocument<Element> {
public:
explicit Document(std::shared_ptr<abstract::ReadableFilesystem> filesystem);

Expand All @@ -30,16 +26,6 @@ class Document final : public abstract::Document {
void save(const common::Path &path) const final;
void save(const common::Path &path, const char *password) const final;

[[nodiscard]] FileType file_type() const noexcept final;
[[nodiscard]] DocumentType document_type() const noexcept final;

[[nodiscard]] std::shared_ptr<abstract::ReadableFilesystem>
files() const noexcept final;

[[nodiscard]] abstract::Element *root_element() const final;

void register_element_(std::unique_ptr<Element> element);

private:
struct Sheet final {
common::Path sheet_path;
Expand All @@ -48,17 +34,12 @@ class Document final : public abstract::Document {
pugi::xml_document drawing_xml;
};

std::shared_ptr<abstract::ReadableFilesystem> m_filesystem;

pugi::xml_document m_workbook_xml;
pugi::xml_document m_styles_xml;
std::unordered_map<std::string, Sheet> m_sheets;
std::unordered_map<std::string, pugi::xml_document> m_drawings_xml;
pugi::xml_document m_shared_strings_xml;

std::vector<std::unique_ptr<Element>> m_elements;
Element *m_root_element{};

StyleRegistry m_style_registry;
std::vector<pugi::xml_node> m_shared_strings;

Expand Down
22 changes: 3 additions & 19 deletions src/odr/internal/ooxml/text/ooxml_text_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
namespace odr::internal::ooxml::text {

Document::Document(std::shared_ptr<abstract::ReadableFilesystem> filesystem)
: m_filesystem{std::move(filesystem)} {
: common::TemplateDocument<Element>(FileType::office_open_xml_document,
DocumentType::text,
std::move(filesystem)) {
m_document_xml = util::xml::parse(*m_filesystem, "word/document.xml");
m_styles_xml = util::xml::parse(*m_filesystem, "word/styles.xml");

Expand Down Expand Up @@ -66,22 +68,4 @@ void Document::save(const common::Path & /*path*/,
throw UnsupportedOperation();
}

FileType Document::file_type() const noexcept {
return FileType::office_open_xml_document;
}

DocumentType Document::document_type() const noexcept {
return DocumentType::text;
}

std::shared_ptr<abstract::ReadableFilesystem> Document::files() const noexcept {
return m_filesystem;
}

abstract::Element *Document::root_element() const { return m_root_element; }

void Document::register_element_(std::unique_ptr<Element> element) {
m_elements.push_back(std::move(element));
}

} // namespace odr::internal::ooxml::text
Loading

0 comments on commit e48bdf5

Please sign in to comment.