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

◆ write_row()

bool MayaFlux::IO::XmlWriter::write_row ( const Kakshya::Composite & row)
private

Definition at line 166 of file XmlWriter.cpp.

167{
168 const auto kind = row.text("kind");
169 const auto name = row.text("name");
170 const auto value = row.text("value");
171 const auto parent = row.get<uint64_t>("parent");
172 const auto index = static_cast<uint64_t>(m_xml->nodes.size());
173
174 if (!kind) {
175 m_last_error = "XML row is missing its kind";
176 return false;
177 }
178
179 if (*kind == "attribute") {
180 if (!parent || *parent >= index
181 || !name || !valid_name(*name)
182 || !value || !valid_xml_value(*value)
183 || m_ancestors.empty()
184 || m_ancestors.back() != *parent
185 || m_content_started[*parent]) {
186 m_last_error = "XML attribute requires an open parent and must precede its content";
187 return false;
188 }
189
190 auto owner = m_xml->nodes[*parent];
191 if (owner.type() != pugi::node_element
192 && owner.type() != pugi::node_declaration) {
193 m_last_error = "XML attribute parent must be an element or declaration";
194 return false;
195 }
196
197 if (owner.attribute(std::string(*name).c_str())) {
198 m_last_error = "XML attribute names must be unique within an element";
199 return false;
200 }
201
202 auto attribute = owner.append_attribute(std::string(*name).c_str());
203 const std::string output_value =
204 owner.type() == pugi::node_declaration && *name == "encoding"
205 ? "UTF-8"
206 : std::string(*value);
207
208 if (!attribute || !attribute.set_value(output_value.c_str())) {
209 m_last_error = "Could not append XML attribute";
210 return false;
211 }
212
213 m_xml->nodes.emplace_back();
214 m_content_started.push_back(false);
215
216 return true;
217 }
218
219 const auto type = type_of(*kind);
220 if (!type) {
221 m_last_error = "Unsupported XML node kind";
222 return false;
223 }
224
225 pugi::xml_node owner = m_xml->document.root();
226 if (parent) {
227 if (*parent >= index) {
228 m_last_error = "XML parent index must precede its child";
229 return false;
230 }
231
232 while (!m_ancestors.empty() && m_ancestors.back() != *parent)
233 m_ancestors.pop_back();
234
235 if (m_ancestors.empty() || m_xml->nodes[*parent].type() != pugi::node_element) {
236 m_last_error = "XML parent must be an open ancestor element";
237 return false;
238 }
239
240 owner = m_xml->nodes[*parent];
241 m_content_started[*parent] = true;
242
243 if (*type == pugi::node_declaration || *type == pugi::node_doctype) {
244 m_last_error = "XML declaration and doctype must be top-level";
245 return false;
246 }
247 } else {
248 m_ancestors.clear();
249 if (*type == pugi::node_element) {
250 if (m_root_count != 0) {
251 m_last_error = "XML document may contain only one root element";
252 return false;
253 }
254
255 ++m_root_count;
256 } else if (*type == pugi::node_declaration) {
257 if (m_declaration_seen || !m_xml->nodes.empty()) {
258 m_last_error = "XML declaration must be the first document node";
259 return false;
260 }
261
262 m_declaration_seen = true;
263 } else if (*type == pugi::node_doctype) {
264 if (m_doctype_seen || m_root_count != 0) {
265 m_last_error = "XML doctype must occur once before the root";
266 return false;
267 }
268
269 m_doctype_seen = true;
270 } else if (*type != pugi::node_comment && *type != pugi::node_pi) {
271 m_last_error = "XML text and CDATA require a parent element";
272 return false;
273 }
274 }
275
276 const bool needs_name = *type == pugi::node_element
277 || *type == pugi::node_pi || *type == pugi::node_declaration;
278
279 const bool needs_value = *type == pugi::node_pcdata
280 || *type == pugi::node_cdata || *type == pugi::node_comment
281 || *type == pugi::node_pi || *type == pugi::node_doctype;
282
283 if ((needs_name && (!name || !valid_name(*name)))
284 || (*type == pugi::node_declaration && (!name || *name != "xml"))
285 || (*type == pugi::node_pi && name && *name == "xml")
286 || (!needs_name && name)
287 || (needs_value && (!value || !valid_xml_value(*value)))
288 || (!needs_value && value)
289 || (*type == pugi::node_comment
290 && (value->find("--") != std::string_view::npos || value->ends_with('-')))
291 || (*type == pugi::node_cdata && value->find("]]>") != std::string_view::npos)
292 || (*type == pugi::node_pi && value->find("?>") != std::string_view::npos)) {
293 m_last_error = "XML row has invalid name or value fields";
294 return false;
295 }
296
297 auto node = owner.append_child(*type);
298 if (!node || (name && !node.set_name(std::string(*name).c_str()))
299 || (value && !node.set_value(std::string(*value).c_str()))) {
300 m_last_error = "Could not append XML node";
301 return false;
302 }
303
304 m_xml->nodes.push_back(node);
305 m_content_started.push_back(false);
306
307 if (*type == pugi::node_element || *type == pugi::node_declaration)
308 m_ancestors.push_back(index);
309
310 return true;
311}
std::vector< bool > m_content_started
Definition XmlWriter.hpp:68
std::unique_ptr< Impl > m_xml
Definition XmlWriter.hpp:67
std::vector< uint64_t > m_ancestors
Definition XmlWriter.hpp:69

References MayaFlux::Kakshya::Composite::get(), m_ancestors, m_content_started, m_declaration_seen, m_doctype_seen, m_last_error, m_root_count, m_xml, and MayaFlux::Kakshya::Composite::text().

Referenced by write_rows().

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