xmlbuilder: Validate root element of object parsexml

Ensure that for example a DeviceDisk is actually passed <disk> XML
This commit is contained in:
Cole Robinson
2019-05-08 13:02:35 -04:00
parent 201dfdb23e
commit 472cfbc2a7
3 changed files with 24 additions and 0 deletions

View File

@ -107,6 +107,8 @@ class _XMLBase(object):
raise NotImplementedError()
def _node_has_content(self, node):
raise NotImplementedError()
def _node_get_name(self, node):
raise NotImplementedError()
def node_clear(self, xpath):
raise NotImplementedError()
def _sanitize_xml(self, xml):
@ -165,6 +167,14 @@ class _XMLBase(object):
return
self._node_remove_child(parentnode, childnode)
def validate_root_name(self, expected_root_name):
rootname = self._node_get_name(self._find("."))
if rootname == expected_root_name:
return
raise RuntimeError(
_("XML did not have expected root element name '%s', found '%s'") %
(expected_root_name, rootname))
def _node_set_content(self, xpath, node, setval):
xpathobj = _XPath(xpath)
if setval is not None:
@ -342,6 +352,9 @@ class _Libxml2API(_XMLBase):
def _node_has_content(self, node):
return node.type == "element" and (node.children or node.properties)
def _node_get_name(self, node):
return node.name
def _node_remove_child(self, parentnode, childnode):
node = childnode