util: xml: Extract XPath evaluation for strings

Extract the internals of virXPathString which evaluate the XPath and
validate that the returned object is a string into a new helper named
'virXPathEvalString'.

The function will be later reused in the number XPath evaluation
functions.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2022-10-05 09:46:11 +02:00
parent 8525ac4b83
commit 713ec72222

View File

@ -60,6 +60,34 @@ virXMLXPathContextNew(xmlDocPtr xml)
}
static xmlXPathObject *
virXPathEvalString(const char *xpath,
xmlXPathContextPtr ctxt)
{
g_autoptr(xmlXPathObject) obj = NULL;
if (!xpath) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing XPath expression"));
return NULL;
}
if (!ctxt) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing XPath context"));
return NULL;
}
if (!(obj = xmlXPathEval(BAD_CAST xpath, ctxt)))
return NULL;
if (obj->type != XPATH_STRING ||
!obj->stringval ||
obj->stringval[0] == '\0')
return NULL;
return g_steal_pointer(&obj);
}
/**
* virXPathString:
* @xpath: the XPath string to evaluate
@ -76,16 +104,9 @@ virXPathString(const char *xpath,
{
g_autoptr(xmlXPathObject) obj = NULL;
if ((ctxt == NULL) || (xpath == NULL)) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Invalid parameter to virXPathString()"));
if (!(obj = virXPathEvalString(xpath, ctxt)))
return NULL;
}
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
if ((obj == NULL) || (obj->type != XPATH_STRING) ||
(obj->stringval == NULL) || (obj->stringval[0] == 0)) {
return NULL;
}
return g_strdup((char *)obj->stringval);
}