util: xml: Introduce virXMLFormatElementEmpty

Add a helper which will format an XML element with attributes and
children, but compared to virXMLFormatElement it also formats an empty
element if both buffers are empty.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
Tested-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2021-04-13 16:12:17 +02:00
parent 6c7faa2720
commit 1ac21ab7ea
3 changed files with 35 additions and 15 deletions

View File

@ -3541,6 +3541,7 @@ virXMLBufferCreate;
virXMLCheckIllegalChars;
virXMLExtractNamespaceXML;
virXMLFormatElement;
virXMLFormatElementEmpty;
virXMLNewNode;
virXMLNodeContentString;
virXMLNodeNameEqual;

View File

@ -1631,6 +1631,32 @@ virXMLValidatorFree(virXMLValidator *validator)
}
/* same as virXMLFormatElement but outputs an empty element if @attrBuf and
* @childBuf are both empty */
void
virXMLFormatElementEmpty(virBuffer *buf,
const char *name,
virBuffer *attrBuf,
virBuffer *childBuf)
{
virBufferAsprintf(buf, "<%s", name);
if (attrBuf && virBufferUse(attrBuf) > 0)
virBufferAddBuffer(buf, attrBuf);
if (childBuf && virBufferUse(childBuf) > 0) {
virBufferAddLit(buf, ">\n");
virBufferAddBuffer(buf, childBuf);
virBufferAsprintf(buf, "</%s>\n", name);
} else {
virBufferAddLit(buf, "/>\n");
}
virBufferFreeAndReset(attrBuf);
virBufferFreeAndReset(childBuf);
}
/**
* virXMLFormatElement
* @buf: the parent buffer where the element will be placed
@ -1655,21 +1681,7 @@ virXMLFormatElement(virBuffer *buf,
(!childBuf || virBufferUse(childBuf) == 0))
return;
virBufferAsprintf(buf, "<%s", name);
if (attrBuf && virBufferUse(attrBuf) > 0)
virBufferAddBuffer(buf, attrBuf);
if (childBuf && virBufferUse(childBuf) > 0) {
virBufferAddLit(buf, ">\n");
virBufferAddBuffer(buf, childBuf);
virBufferAsprintf(buf, "</%s>\n", name);
} else {
virBufferAddLit(buf, "/>\n");
}
virBufferFreeAndReset(attrBuf);
virBufferFreeAndReset(childBuf);
virXMLFormatElementEmpty(buf, name, attrBuf, childBuf);
}

View File

@ -315,6 +315,13 @@ virXMLFormatElement(virBuffer *buf,
virBuffer *attrBuf,
virBuffer *childBuf);
void
virXMLFormatElementEmpty(virBuffer *buf,
const char *name,
virBuffer *attrBuf,
virBuffer *childBuf);
struct _virXPathContextNodeSave {
xmlXPathContextPtr ctxt;
xmlNodePtr node;