MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
XmlReader.cpp
Go to the documentation of this file.
1#include "XmlReader.hpp"
2
5
6#include <cstddef>
7#include <pugixml.hpp>
8
9#include <cctype>
10
11namespace MayaFlux::IO {
12
14{
16 layout.add_field<uint64_t>("parent");
17 layout.add_field<std::string>("kind");
18 layout.add_field<std::string>("name");
19 layout.add_field<std::string>("value");
20
21 return layout;
22}
23
24std::string_view XmlElementView::name() const noexcept
25{
26 const auto source = m_rows->at(m_row);
27 return source ? source->text("name").value_or(std::string_view {}) : std::string_view {};
28}
29
30std::optional<std::string_view> XmlElementView::attribute(std::string_view name) const noexcept
31{
32 const size_t end = m_row + m_forest->subtree_size(m_row);
33 for (size_t index = m_row + 1; index < end; ++index) {
34 const auto source = m_rows->at(index);
35 if (!source || source->text("kind") != std::string_view("attribute"))
36 break;
37 if (source->text("name") == name)
38 return source->text("value");
39 }
40 return std::nullopt;
41}
42
43namespace {
44
45 constexpr uintmax_t max_xml_file_bytes = static_cast<const uintmax_t>(64 * 1024 * 1024);
46 constexpr size_t max_xml_nodes = 1'000'000;
47 constexpr size_t max_xml_depth = 256;
48
49 std::string extension_of(const std::string& filepath)
50 {
51 auto extension = std::filesystem::path(filepath).extension().string();
52 std::ranges::transform(extension, extension.begin(),
53 [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
54
55 return extension;
56 }
57
58 std::string_view node_kind(pugi::xml_node_type type)
59 {
60 switch (type) {
61 case pugi::node_element:
62 return "element";
63 case pugi::node_pcdata:
64 return "text";
65 case pugi::node_cdata:
66 return "cdata";
67 case pugi::node_comment:
68 return "comment";
69 case pugi::node_pi:
70 return "pi";
71 case pugi::node_declaration:
72 return "declaration";
73 case pugi::node_doctype:
74 return "doctype";
75 default:
76 return {};
77 }
78 }
79
80 struct PendingNode {
81 pugi::xml_node node;
82 std::optional<uint64_t> parent;
83 size_t depth;
84 };
85
86 void push_children(std::vector<PendingNode>& pending,
87 const pugi::xml_node& node, std::optional<uint64_t> parent, size_t depth)
88 {
89 for (auto child = node.last_child(); child; child = child.previous_sibling())
90 pending.push_back({ .node = child, .parent = parent, .depth = depth });
91 }
92
93}
94
95bool XmlReader::can_read(const std::string& filepath) const
96{
97 return extension_of(filepath) == ".xml";
98}
99
100bool XmlReader::open(const std::string& filepath, FileReadOptions)
101{
102 close();
103 m_last_error.clear();
104
105 if (!can_read(filepath)) {
106 m_last_error = "Expected an .xml file";
107 return false;
108 }
109
110 m_filepath = resolve_path(filepath);
111 std::error_code error;
112 const auto file_size = std::filesystem::file_size(m_filepath, error);
113 if (error) {
114 m_last_error = "Cannot open XML file";
115 return false;
116 }
117
118 if (file_size > max_xml_file_bytes) {
119 m_last_error = "XML file exceeds the 64 MiB input limit";
120 return false;
121 }
122
123 pugi::xml_document document;
124 const auto parsed = document.load_file(m_filepath.c_str(),
125 pugi::parse_full | pugi::parse_ws_pcdata);
126 if (!parsed) {
127 m_last_error = "XML parse failed at byte "
128 + std::to_string(parsed.offset) + ": " + parsed.description();
129 return false;
130 }
131
132 if (!document.document_element()) {
133 m_last_error = "XML document has no root element";
134 return false;
135 }
136
138 Kakshya::CompositeInsertion insertion(rows);
139 std::vector<PendingNode> pending;
140 push_children(pending, document, std::nullopt, 0);
141
142 while (!pending.empty()) {
143 auto entry = pending.back();
144 pending.pop_back();
145
146 if (!entry.parent && entry.node.type() == pugi::node_pcdata) {
147 const std::string_view value(entry.node.value());
148
149 if (std::ranges::all_of(value, [](unsigned char c) {
150 return std::isspace(c) != 0;
151 }))
152 continue;
153 }
154
155 if (entry.depth > max_xml_depth || rows.size() >= max_xml_nodes) {
156 m_last_error = "XML document exceeds the node or depth limit";
157 return false;
158 }
159
160 const auto kind = node_kind(entry.node.type());
161 if (kind.empty()) {
162 m_last_error = "Unsupported XML node type";
163 return false;
164 }
165
166 const auto row = insertion.append();
167 if (!insertion.set_text(row, "kind", kind)
168 || (entry.parent && !insertion.set(row, "parent", *entry.parent))) {
169 m_last_error = "Could not store XML node";
170 return false;
171 }
172
173 const std::string_view name(entry.node.name());
174 const std::string_view value(entry.node.value());
175 const bool has_value = entry.node.type() == pugi::node_pcdata
176 || entry.node.type() == pugi::node_cdata
177 || entry.node.type() == pugi::node_comment
178 || entry.node.type() == pugi::node_pi
179 || entry.node.type() == pugi::node_doctype;
180
181 if ((!name.empty() && !insertion.set_text(row, "name", name))
182 || (has_value && !insertion.set_text(row, "value", value))) {
183 m_last_error = "Could not store XML node content";
184 return false;
185 }
186
187 for (auto attribute : entry.node.attributes()) {
188 if (rows.size() >= max_xml_nodes) {
189 m_last_error = "XML document exceeds the node limit";
190 return false;
191 }
192
193 const auto attribute_row = insertion.append();
194 if (!insertion.set(attribute_row, "parent", static_cast<uint64_t>(row))
195 || !insertion.set_text(attribute_row, "kind", "attribute")
196 || !insertion.set_text(attribute_row, "name", attribute.name())
197 || !insertion.set_text(attribute_row, "value", attribute.value())) {
198 m_last_error = "Could not store XML attribute";
199 return false;
200 }
201 }
202
203 push_children(pending, entry.node, static_cast<uint64_t>(row), entry.depth + 1);
204 }
205
206 std::string forest_error;
207 auto forest = Kakshya::index_composite_forest(rows, "parent", forest_error);
208 if (!forest || !forest->preorder()) {
209 m_last_error = "Could not index the XML node hierarchy: " + forest_error;
210 return false;
211 }
212
213 m_forest = std::move(*forest);
214 m_rows.emplace(std::move(rows));
215 m_position = 0;
216
217 return true;
218}
219
221{
222 m_rows.reset();
223 m_forest.reset();
224 m_filepath.clear();
225 m_position = 0;
226}
227
228std::optional<Kakshya::CompositeArray> XmlReader::take_rows()
229{
230 std::optional<Kakshya::CompositeArray> rows = std::move(m_rows);
231 close();
232 return rows;
233}
234
235std::optional<FileMetadata> XmlReader::get_metadata() const
236{
237 if (!m_rows)
238 return std::nullopt;
239
240 FileMetadata metadata;
241 metadata.format = "xml";
242 metadata.mime_type = "application/xml";
243
244 std::error_code error;
245 metadata.file_size = std::filesystem::file_size(m_filepath, error);
246 if (error)
247 metadata.file_size = 0;
248
249 return metadata;
250}
251
252std::optional<Kakshya::CompositeArray> XmlReader::read_composite()
253{
254 return read_next(std::numeric_limits<size_t>::max());
255}
256
257std::optional<Kakshya::CompositeArray>
258XmlReader::read_next(size_t max_elements)
259{
260 if (!m_rows) {
261 m_last_error = "No XML document is open";
262 return std::nullopt;
263 }
264
265 Kakshya::CompositeArray result(m_rows->layout());
266 Kakshya::CompositeInsertion insertion(result);
267 const auto count = std::min(max_elements,
268 m_rows->size() - static_cast<size_t>(m_position));
269
270 for (size_t i = 0; i < count; ++i) {
271 const auto source = m_rows->at(static_cast<size_t>(m_position) + i);
272 if (!source || !insertion.append(*source)) {
273 m_last_error = "Could not copy XML node";
274 return std::nullopt;
275 }
276 }
277
278 m_position += count;
279 m_last_error.clear();
280 return result;
281}
282
283std::optional<Kakshya::CompositeLayout> XmlReader::get_layout() const
284{
285 if (!m_rows)
286 return std::nullopt;
287
288 return m_rows->layout();
289}
290
291std::optional<XmlElementView> XmlReader::root_element() const noexcept
292{
293 if (!m_rows)
294 return std::nullopt;
295
296 for (size_t index = 0; index < m_rows->size(); index += m_forest->subtree_size(index)) {
297 const auto source = m_rows->at(index);
298 if (source && source->text("kind") == std::string_view("element"))
299 return XmlElementView(*m_rows, *m_forest, index);
300 }
301 return std::nullopt;
302}
303
304std::optional<XmlElementView> XmlReader::element_at(uint64_t row) const noexcept
305{
306 if (!m_rows || row >= m_rows->size())
307 return std::nullopt;
308
309 const auto source = m_rows->at(static_cast<size_t>(row));
310 if (!source || source->text("kind") != std::string_view("element"))
311 return std::nullopt;
312
313 return XmlElementView(*m_rows, *m_forest, static_cast<size_t>(row));
314}
315
316bool XmlReader::seek(const std::vector<uint64_t>& position)
317{
318 if (!m_rows || position.size() != 1 || position[0] > m_rows->size()) {
319 m_last_error = "Seek requires a valid XML node position";
320 return false;
321 }
322
323 m_position = position[0];
324 m_last_error.clear();
325 return true;
326}
327
328std::vector<uint64_t> XmlReader::get_dimension_sizes() const
329{
330 return m_rows ? std::vector<uint64_t> { m_rows->size() }
331 : std::vector<uint64_t> {};
332}
333
334}
static std::string resolve_path(const std::string &filepath)
Resolve a filepath against the project source root if not found as-is.
const Kakshya::CompositeArray * m_rows
Definition XmlReader.hpp:55
std::string_view name() const noexcept
Definition XmlReader.cpp:24
const Kakshya::ForestIndex * m_forest
Definition XmlReader.hpp:56
std::optional< std::string_view > attribute(std::string_view name) const noexcept
Definition XmlReader.cpp:30
Borrowed XML element with named attributes and immediate child elements.
Definition XmlReader.hpp:30
std::vector< uint64_t > get_dimension_sizes() const override
Total number of rows while open.
std::optional< Kakshya::CompositeArray > take_rows()
Move the parsed rows out and close the reader.
std::optional< XmlElementView > root_element() const noexcept
Borrow the document root element, independent of the row read position.
std::optional< FileMetadata > get_metadata() const override
XML format, MIME type, and source file size.
bool seek(const std::vector< uint64_t > &position) override
Seek to one row index, including the end position.
bool can_read(const std::string &filepath) const override
Accept paths with the .xml extension.
Definition XmlReader.cpp:95
std::optional< Kakshya::CompositeArray > read_next(size_t max_elements) override
Copy at most max_elements rows and advance the position.
std::optional< XmlElementView > element_at(uint64_t row) const noexcept
Borrow an element by its global XML row index.
bool open(const std::string &filepath, FileReadOptions options=FileReadOptions::ALL) override
Parse an XML file and establish the fixed node layout.
void close() override
Release parsed rows and reset the read position.
std::optional< Kakshya::CompositeArray > m_rows
std::optional< Kakshya::ForestIndex > m_forest
std::optional< Kakshya::CompositeArray > read_composite() override
Copy all rows remaining after the current position.
std::optional< Kakshya::CompositeLayout > get_layout() const override
Copy the finalized node layout while open.
size_t size() const noexcept
Number of packed elements.
Owns a sequence of values that share a CompositeLayout.
bool set(size_t index, std::string_view field_name, T value)
Write an arithmetic or GLM field of an existing element.
bool set_text(size_t index, std::string_view field_name, std::string_view value)
Write UTF-8 bytes to a std::string field.
size_t append()
Append an empty element.
Mutable insertion adapter for a CompositeArray.
bool add_field(std::string name)
Declare a named arithmetic, GLM, text, or binary field.
Definition Composite.hpp:74
Shared field description and byte layout for a CompositeArray.
Definition Composite.hpp:60
FileReadOptions
Generic options for file reading behavior.
Kakshya::CompositeLayout xml_composite_layout()
Composite row layout used by the XML reader and writer.
Definition XmlReader.cpp:13
std::optional< ForestIndex > index_composite_forest(const CompositeArray &rows, std::string_view parent_field, std::string &error, uint64_t *error_row)
Index the parent links of a Composite array.
uint64_t file_size
Size in bytes.
std::string format
File format identifier (e.g., "wav", "mp3", "hdf5").
std::string mime_type
MIME type if applicable (e.g., "audio/wav").
Generic metadata structure for any file type.