MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ open()

bool MayaFlux::IO::XmlReader::open ( const std::string & filepath,
FileReadOptions options = FileReadOptions::ALL )
overridevirtual

Parse an XML file and establish the fixed node layout.

Returns
True for a parsed document with one root element.

Implements MayaFlux::IO::FileReader.

Definition at line 100 of file XmlReader.cpp.

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
137 Kakshya::CompositeArray rows(xml_composite_layout());
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}
static std::string resolve_path(const std::string &filepath)
Resolve a filepath against the project source root if not found as-is.
bool can_read(const std::string &filepath) const override
Accept paths with the .xml extension.
Definition XmlReader.cpp:95
void close() override
Release parsed rows and reset the read position.
std::optional< Kakshya::CompositeArray > m_rows
std::optional< Kakshya::ForestIndex > m_forest
Kakshya::CompositeLayout xml_composite_layout()
Composite row layout used by the XML reader and writer.
Definition XmlReader.cpp:13
void error(Component component, Context context, std::source_location location, std::string_view message)
Log an error message and optionally throw an exception.
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.

References MayaFlux::Kakshya::CompositeInsertion::append(), can_read(), close(), MayaFlux::Kakshya::index_composite_forest(), m_filepath, m_forest, MayaFlux::IO::CompositeReader::m_last_error, m_position, m_rows, MayaFlux::IO::FileReader::resolve_path(), MayaFlux::Kakshya::CompositeInsertion::set(), MayaFlux::Kakshya::CompositeInsertion::set_text(), MayaFlux::Kakshya::CompositeArray::size(), and MayaFlux::IO::xml_composite_layout().

Referenced by MayaFlux::IO::XmlDocument::load().

Here is the call graph for this function:
Here is the caller graph for this function: