MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
XmlWriter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "CompositeWriter.hpp"
4#include "XmlReader.hpp"
5
6namespace MayaFlux::IO {
7
8/**
9 * @class XmlWriter
10 * @brief Write preorder XML Composite rows as one document.
11 *
12 * Rows use xml_composite_layout(). Parent indices refer to the complete row
13 * sequence across write_rows() calls. A parent must be an open ancestor;
14 * attributes must precede child content. The writer builds a DOM and saves
15 * it on flush() or close(). It preserves supported node order and values,
16 * but does not promise byte-identical output. Output is UTF-8, and a source
17 * declaration's encoding attribute is updated to UTF-8 when present.
18 *
19 * Usage:
20 * @code
21 * XmlWriter writer;
22 * bool saved = writer.write("controls.xml", nodes);
23 * @endcode
24 *
25 * @note open() creates or truncates the output path. A failed write prevents
26 * finalization. After a successful flush, later rows can still be appended
27 * and the file is updated by the next flush or close.
28 */
29class MAYAFLUX_API XmlWriter : public CompositeWriter {
30public:
32 ~XmlWriter() override;
33
34 /** @brief Accept paths with the .xml extension. */
35 [[nodiscard]] bool can_write(const std::string& filepath) const override;
36
37 /** @brief Start a document using xml_composite_layout(). */
38 bool open(const std::string& filepath,
39 const Kakshya::CompositeLayout& layout) override;
40
41 /**
42 * @brief Append preorder rows whose parent indices are global.
43 * @return False on invalid structure or a field value that cannot be
44 * represented as XML; earlier accepted rows remain in the DOM.
45 */
46 bool write_rows(const Kakshya::CompositeSlice& rows) override;
47
48 /** @brief Save the current document, which must contain one root. */
49 bool flush() override;
50
51 /** @brief Save and release the document, or return false after failure. */
52 bool close() override;
53
54 /** @brief Whether a document is being assembled. */
55 [[nodiscard]] bool is_open() const override { return m_open; }
56
57 /** @brief Supported extension without a dot. */
58 [[nodiscard]] std::vector<std::string> get_supported_extensions() const override { return { "xml" }; }
59
60 /** @brief Last structure or output error. */
61 [[nodiscard]] std::string get_last_error() const override { return m_last_error; }
62
63private:
64 struct Impl;
65 bool write_row(const Kakshya::Composite& row);
66
67 std::unique_ptr<Impl> m_xml;
68 std::vector<bool> m_content_started;
69 std::vector<uint64_t> m_ancestors;
70 std::string m_filepath;
71 std::string m_last_error;
72 bool m_open {};
73 bool m_failed {};
76 size_t m_root_count {};
77};
78
79}
Typed writer contract for schema-bearing Composite files.
bool is_open() const override
Whether a document is being assembled.
Definition XmlWriter.hpp:55
std::vector< bool > m_content_started
Definition XmlWriter.hpp:68
std::string get_last_error() const override
Last structure or output error.
Definition XmlWriter.hpp:61
bool close() override
Save and release the document, or return false after failure.
std::unique_ptr< Impl > m_xml
Definition XmlWriter.hpp:67
bool can_write(const std::string &filepath) const override
Accept paths with the .xml extension.
Definition XmlWriter.cpp:85
bool write_rows(const Kakshya::CompositeSlice &rows) override
Append preorder rows whose parent indices are global.
bool open(const std::string &filepath, const Kakshya::CompositeLayout &layout) override
Start a document using xml_composite_layout().
Definition XmlWriter.cpp:90
std::vector< uint64_t > m_ancestors
Definition XmlWriter.hpp:69
std::vector< std::string > get_supported_extensions() const override
Supported extension without a dot.
Definition XmlWriter.hpp:58
bool flush() override
Save the current document, which must contain one root.
Shared field description and byte layout for a CompositeArray.
Definition Composite.hpp:60
Borrowed consecutive elements, optionally selected by a Region.
Read view of one value in a CompositeArray.