diff --git a/CMakeLists.txt b/CMakeLists.txt index 6378f122..f3eabfec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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" diff --git a/src/odr/internal/common/document.cpp b/src/odr/internal/common/document.cpp new file mode 100644 index 00000000..0bc5d427 --- /dev/null +++ b/src/odr/internal/common/document.cpp @@ -0,0 +1,25 @@ +#include + +#include +#include + +#include + +namespace odr::internal::common { + +Document::Document(FileType file_type, DocumentType document_type, + std::shared_ptr 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 Document::files() const noexcept { + return m_filesystem; +} + +} // namespace odr::internal::common diff --git a/src/odr/internal/common/document.hpp b/src/odr/internal/common/document.hpp new file mode 100644 index 00000000..79cbb258 --- /dev/null +++ b/src/odr/internal/common/document.hpp @@ -0,0 +1,56 @@ +#ifndef ODR_INTERNAL_COMMON_DOCUMENT_H +#define ODR_INTERNAL_COMMON_DOCUMENT_H + +#include + +#include + +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 files); + + [[nodiscard]] FileType file_type() const noexcept final; + [[nodiscard]] DocumentType document_type() const noexcept final; + + [[nodiscard]] std::shared_ptr + files() const noexcept final; + +protected: + FileType m_file_type; + DocumentType m_document_type; + + std::shared_ptr m_filesystem; + + friend class Element; +}; + +template class TemplateDocument : public Document { +public: + TemplateDocument(FileType file_type, DocumentType document_type, + std::shared_ptr 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) { + m_elements.push_back(std::move(element)); + } + +protected: + std::vector> m_elements; + element_t *m_root_element{}; +}; + +} // namespace odr::internal::common + +#endif // ODR_INTERNAL_COMMON_DOCUMENT_H diff --git a/src/odr/internal/odf/odf_document.cpp b/src/odr/internal/odf/odf_document.cpp index 41980052..7c4bc15a 100644 --- a/src/odr/internal/odf/odf_document.cpp +++ b/src/odr/internal/odf/odf_document.cpp @@ -16,8 +16,8 @@ namespace odr::internal::odf { Document::Document(const FileType file_type, const DocumentType document_type, std::shared_ptr filesystem) - : m_file_type{file_type}, m_document_type{document_type}, - m_filesystem{std::move(filesystem)} { + : common::TemplateDocument(file_type, document_type, + std::move(filesystem)) { m_content_xml = util::xml::parse(*m_filesystem, "content.xml"); if (m_filesystem->exists("styles.xml")) { @@ -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 Document::files() const noexcept { - return m_filesystem; -} - -abstract::Element *Document::root_element() const { return m_root_element; } - } // namespace odr::internal::odf diff --git a/src/odr/internal/odf/odf_document.hpp b/src/odr/internal/odf/odf_document.hpp index 05bc8b15..07cc59a8 100644 --- a/src/odr/internal/odf/odf_document.hpp +++ b/src/odr/internal/odf/odf_document.hpp @@ -3,8 +3,8 @@ #include -#include #include +#include #include #include #include @@ -19,7 +19,7 @@ class ReadableFilesystem; namespace odr::internal::odf { -class Document : public abstract::Document { +class Document : public common::TemplateDocument { public: Document(FileType file_type, DocumentType document_type, std::shared_ptr files); @@ -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 - files() const noexcept final; - - [[nodiscard]] abstract::Element *root_element() const final; - protected: - FileType m_file_type; - DocumentType m_document_type; - - std::shared_ptr m_filesystem; - pugi::xml_document m_content_xml; pugi::xml_document m_styles_xml; - std::vector> m_elements; - Element *m_root_element{}; - StyleRegistry m_style_registry; friend class Element; diff --git a/src/odr/internal/ooxml/presentation/ooxml_presentation_document.cpp b/src/odr/internal/ooxml/presentation/ooxml_presentation_document.cpp index 54a939da..c3cdaae1 100644 --- a/src/odr/internal/ooxml/presentation/ooxml_presentation_document.cpp +++ b/src/odr/internal/ooxml/presentation/ooxml_presentation_document.cpp @@ -11,7 +11,9 @@ namespace odr::internal::ooxml::presentation { Document::Document(std::shared_ptr filesystem) - : m_filesystem{std::move(filesystem)} { + : common::TemplateDocument(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()); @@ -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 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) { - m_elements.push_back(std::move(element)); -} - } // namespace odr::internal::ooxml::presentation diff --git a/src/odr/internal/ooxml/presentation/ooxml_presentation_document.hpp b/src/odr/internal/ooxml/presentation/ooxml_presentation_document.hpp index b2f4aec5..77b9c434 100644 --- a/src/odr/internal/ooxml/presentation/ooxml_presentation_document.hpp +++ b/src/odr/internal/ooxml/presentation/ooxml_presentation_document.hpp @@ -1,7 +1,7 @@ #ifndef ODR_INTERNAL_OOXML_PRESENTATION_H #define ODR_INTERNAL_OOXML_PRESENTATION_H -#include +#include #include #include @@ -11,7 +11,7 @@ namespace odr::internal::ooxml::presentation { -class Document final : public abstract::Document { +class Document final : public common::TemplateDocument { public: explicit Document(std::shared_ptr filesystem); @@ -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 - files() const noexcept final; - - [[nodiscard]] abstract::Element *root_element() const final; - - void register_element_(std::unique_ptr element); - private: std::shared_ptr m_filesystem; pugi::xml_document m_document_xml; std::unordered_map m_slides_xml; - std::vector> m_elements; - Element *m_root_element{}; - friend class Element; }; diff --git a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_document.cpp b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_document.cpp index 23d778c7..43b72897 100644 --- a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_document.cpp +++ b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_document.cpp @@ -12,7 +12,9 @@ namespace odr::internal::ooxml::spreadsheet { Document::Document(std::shared_ptr filesystem) - : m_filesystem{std::move(filesystem)} { + : common::TemplateDocument(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"); @@ -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 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) { - m_elements.push_back(std::move(element)); -} - } // namespace odr::internal::ooxml::spreadsheet diff --git a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_document.hpp b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_document.hpp index 53fcad4d..0f1b0f4b 100644 --- a/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_document.hpp +++ b/src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_document.hpp @@ -3,7 +3,7 @@ #include -#include +#include #include #include #include @@ -14,13 +14,9 @@ #include -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 { public: explicit Document(std::shared_ptr filesystem); @@ -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 - files() const noexcept final; - - [[nodiscard]] abstract::Element *root_element() const final; - - void register_element_(std::unique_ptr element); - private: struct Sheet final { common::Path sheet_path; @@ -48,17 +34,12 @@ class Document final : public abstract::Document { pugi::xml_document drawing_xml; }; - std::shared_ptr m_filesystem; - pugi::xml_document m_workbook_xml; pugi::xml_document m_styles_xml; std::unordered_map m_sheets; std::unordered_map m_drawings_xml; pugi::xml_document m_shared_strings_xml; - std::vector> m_elements; - Element *m_root_element{}; - StyleRegistry m_style_registry; std::vector m_shared_strings; diff --git a/src/odr/internal/ooxml/text/ooxml_text_document.cpp b/src/odr/internal/ooxml/text/ooxml_text_document.cpp index a943b984..ae05ca88 100644 --- a/src/odr/internal/ooxml/text/ooxml_text_document.cpp +++ b/src/odr/internal/ooxml/text/ooxml_text_document.cpp @@ -16,7 +16,9 @@ namespace odr::internal::ooxml::text { Document::Document(std::shared_ptr filesystem) - : m_filesystem{std::move(filesystem)} { + : common::TemplateDocument(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"); @@ -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 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) { - m_elements.push_back(std::move(element)); -} - } // namespace odr::internal::ooxml::text diff --git a/src/odr/internal/ooxml/text/ooxml_text_document.hpp b/src/odr/internal/ooxml/text/ooxml_text_document.hpp index ff2e9ae8..8de3b6e5 100644 --- a/src/odr/internal/ooxml/text/ooxml_text_document.hpp +++ b/src/odr/internal/ooxml/text/ooxml_text_document.hpp @@ -3,7 +3,7 @@ #include -#include +#include #include #include #include @@ -14,13 +14,9 @@ #include -namespace odr::internal::abstract { -class ReadableFilesystem; -} - namespace odr::internal::ooxml::text { -class Document final : public abstract::Document { +class Document final : public common::TemplateDocument { public: explicit Document(std::shared_ptr filesystem); @@ -30,27 +26,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 - files() const noexcept final; - - [[nodiscard]] abstract::Element *root_element() const final; - - void register_element_(std::unique_ptr element); - private: - std::shared_ptr m_filesystem; - pugi::xml_document m_document_xml; pugi::xml_document m_styles_xml; std::unordered_map m_document_relations; - std::vector> m_elements; - Element *m_root_element{}; - StyleRegistry m_style_registry; friend class Element;