MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
XmlDocument.cpp
Go to the documentation of this file.
1#include "XmlDocument.hpp"
2
3namespace MayaFlux::IO {
4
5std::shared_ptr<const XmlDocument> XmlDocument::load(const std::string& path, std::string& error)
6{
7 XmlReader reader;
8 if (!reader.open(path)) {
9 error = reader.get_last_error();
10 return nullptr;
11 }
12 auto rows = reader.take_rows();
13 if (!rows) {
14 error = "No XML rows were parsed";
15 return nullptr;
16 }
17 return from_rows(std::move(*rows), error);
18}
19
20std::shared_ptr<const XmlDocument> XmlDocument::from_rows(
21 Kakshya::CompositeArray rows, std::string& error)
22{
24 error = "Rows do not use the XML node layout";
25 return nullptr;
26 }
27 auto forest = Kakshya::CompositeForest::build(std::move(rows), "parent", error);
28 if (!forest)
29 return nullptr;
30 if (!forest->index().preorder()) {
31 error = "XML rows are not in document order";
32 return nullptr;
33 }
34 return std::shared_ptr<const XmlDocument>(new XmlDocument(std::move(forest)));
35}
36
37std::optional<XmlElementView> XmlDocument::root_element() const noexcept
38{
39 const auto& rows = m_forest->rows();
40 const auto& index = m_forest->index();
41 for (size_t row = 0; row < rows.size(); row += index.subtree_size(row)) {
42 const auto source = rows.at(row);
43 if (source && source->text("kind") == std::string_view("element"))
44 return XmlElementView(rows, index, row);
45 }
46 return std::nullopt;
47}
48
49std::optional<XmlElementView> XmlDocument::element_at(uint64_t row) const noexcept
50{
51 const auto& rows = m_forest->rows();
52 if (row >= rows.size())
53 return std::nullopt;
54 const auto source = rows.at(static_cast<size_t>(row));
55 if (!source || source->text("kind") != std::string_view("element"))
56 return std::nullopt;
57 return XmlElementView(rows, m_forest->index(), static_cast<size_t>(row));
58}
59
60}
std::string get_last_error() const override
Get the last error message.
std::shared_ptr< const Kakshya::CompositeForest > m_forest
std::optional< XmlElementView > root_element() const noexcept
Borrow the document root element.
std::optional< XmlElementView > element_at(uint64_t row) const noexcept
Borrow an element by its row index.
static std::shared_ptr< const XmlDocument > from_rows(Kakshya::CompositeArray rows, std::string &error)
Take ownership of rows in the XML node layout.
XmlDocument(std::shared_ptr< const Kakshya::CompositeForest > forest) noexcept
const Kakshya::CompositeForest & forest() const noexcept
The rows together with their parent index.
static std::shared_ptr< const XmlDocument > load(const std::string &path, std::string &error)
Parse a file into an owning document.
Borrowed XML element with named attributes and immediate child elements.
Definition XmlReader.hpp:30
std::optional< Kakshya::CompositeArray > take_rows()
Move the parsed rows out and close the reader.
bool open(const std::string &filepath, FileReadOptions options=FileReadOptions::ALL) override
Parse an XML file and establish the fixed node layout.
Read an XML document into preorder Composite rows.
const CompositeLayout & layout() const noexcept
Finalized layout shared by all elements.
Owns a sequence of values that share a CompositeLayout.
static std::shared_ptr< const CompositeForest > build(CompositeArray rows, std::string_view parent_field, std::string &error, uint64_t *error_row=nullptr)
Index rows and take ownership of them.
bool same_structure(const CompositeLayout &other) const noexcept
Compare declared field names, kinds, and nested layouts.
Definition Composite.cpp:71
Kakshya::CompositeLayout xml_composite_layout()
Composite row layout used by the XML reader and writer.
Definition XmlReader.cpp:13