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) {
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)
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())) {
210 return false;
211 }
212
213 m_xml->nodes.emplace_back();
215
216 return true;
217 }
218
219 const auto type = type_of(*kind);
220 if (!type) {
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
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];
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 {
249 if (*type == pugi::node_element) {
251 m_last_error =
"XML document may contain only one root element";
252 return false;
253 }
254
256 } else if (*type == pugi::node_declaration) {
258 m_last_error =
"XML declaration must be the first document node";
259 return false;
260 }
261
263 } else if (*type == pugi::node_doctype) {
265 m_last_error =
"XML doctype must occur once before the root";
266 return false;
267 }
268
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()))) {
301 return false;
302 }
303
304 m_xml->nodes.push_back(node);
306
307 if (*type == pugi::node_element || *type == pugi::node_declaration)
309
310 return true;
311}
std::vector< bool > m_content_started
std::unique_ptr< Impl > m_xml
std::vector< uint64_t > m_ancestors