virNWFilterBindingDefParse: Properly use virXMLParse

Fetch the XPath context and validate the node by using virXMLParse's
features.

This allows to completely remove virNWFilterBindingDefParseNode as
all callers now properly validate the root element name and have a XPath
context handy.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2022-09-22 15:47:40 +02:00
parent 3761a67625
commit 3eb151ddfd
4 changed files with 11 additions and 32 deletions

View File

@ -71,7 +71,7 @@ virNWFilterBindingDefCopy(virNWFilterBindingDef *src)
}
static virNWFilterBindingDef *
virNWFilterBindingDef *
virNWFilterBindingDefParseXML(xmlXPathContextPtr ctxt)
{
virNWFilterBindingDef *ret;
@ -154,42 +154,20 @@ virNWFilterBindingDefParseXML(xmlXPathContextPtr ctxt)
}
virNWFilterBindingDef *
virNWFilterBindingDefParseNode(xmlDocPtr xml,
xmlNodePtr root)
{
g_autoptr(xmlXPathContext) ctxt = NULL;
if (STRNEQ((const char *)root->name, "filterbinding")) {
virReportError(VIR_ERR_XML_ERROR,
"%s",
_("unknown root element for nwfilter binding"));
return NULL;
}
if (!(ctxt = virXMLXPathContextNew(xml)))
return NULL;
ctxt->node = root;
return virNWFilterBindingDefParseXML(ctxt);
}
static virNWFilterBindingDef *
virNWFilterBindingDefParse(const char *xmlStr,
const char *filename,
unsigned int flags)
{
virNWFilterBindingDef *def = NULL;
g_autoptr(xmlDoc) xml = NULL;
g_autoptr(xmlXPathContext) ctxt = NULL;
bool validate = flags & VIR_NWFILTER_BINDING_CREATE_VALIDATE;
if ((xml = virXMLParse(filename, xmlStr, _("(nwfilterbinding_definition)"),
NULL, NULL, "nwfilterbinding.rng", validate))) {
def = virNWFilterBindingDefParseNode(xml, xmlDocGetRootElement(xml));
}
if (!(xml = virXMLParse(filename, xmlStr, _("(nwfilterbinding_definition)"),
"filterbinding", &ctxt, "nwfilterbinding.rng", validate)))
return NULL;
return def;
return virNWFilterBindingDefParseXML(ctxt);
}

View File

@ -47,8 +47,7 @@ virNWFilterBindingDef *
virNWFilterBindingDefCopy(virNWFilterBindingDef *src);
virNWFilterBindingDef *
virNWFilterBindingDefParseNode(xmlDocPtr xml,
xmlNodePtr root);
virNWFilterBindingDefParseXML(xmlXPathContextPtr ctxt);
virNWFilterBindingDef *
virNWFilterBindingDefParseString(const char *xml,

View File

@ -223,7 +223,9 @@ virNWFilterBindingObjParse(const char *filename)
return NULL;
}
if (!(ret->def = virNWFilterBindingDefParseNode(xml, node)))
ctxt->node = node;
if (!(ret->def = virNWFilterBindingDefParseXML(ctxt)))
return NULL;
return g_steal_pointer(&ret);

View File

@ -1365,8 +1365,8 @@ virNWFilterBindingDefFormat;
virNWFilterBindingDefFormatBuf;
virNWFilterBindingDefFree;
virNWFilterBindingDefParseFile;
virNWFilterBindingDefParseNode;
virNWFilterBindingDefParseString;
virNWFilterBindingDefParseXML;
# conf/virnwfilterbindingobj.h