mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-03 03:25:20 +00:00
virxml: Introduce parsing APIs that keep indentation
When parsing an XML it may be important to keep indentation to produce a better looking result when formatting the XML back. Just look at all those xmlKeepBlanksDefault() calls just before virXMLParse() is called. Anyway, as of libxml2 commit v2.12.0~108 xmlKeepBlanksDefault() is deprecated. Therefore, introduce virXMLParse...WithIndent() variants which would do exactly xmlKeepBlanksDefault() did but with non-deprecated APIs. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
1beb69df87
commit
69958ba310
@ -1129,14 +1129,15 @@ virXMLParseHelper(int domcode,
|
|||||||
const char *rootelement,
|
const char *rootelement,
|
||||||
xmlXPathContextPtr *ctxt,
|
xmlXPathContextPtr *ctxt,
|
||||||
const char *schemafile,
|
const char *schemafile,
|
||||||
bool validate)
|
bool validate,
|
||||||
|
bool keepindent)
|
||||||
{
|
{
|
||||||
struct virParserData private;
|
struct virParserData private;
|
||||||
g_autoptr(xmlParserCtxt) pctxt = NULL;
|
g_autoptr(xmlParserCtxt) pctxt = NULL;
|
||||||
g_autoptr(xmlDoc) xml = NULL;
|
g_autoptr(xmlDoc) xml = NULL;
|
||||||
xmlNodePtr rootnode;
|
xmlNodePtr rootnode;
|
||||||
const char *docname;
|
const char *docname;
|
||||||
const int parseFlags = XML_PARSE_NONET | XML_PARSE_NOWARNING;
|
int parseFlags = XML_PARSE_NONET | XML_PARSE_NOWARNING;
|
||||||
|
|
||||||
if (filename)
|
if (filename)
|
||||||
docname = filename;
|
docname = filename;
|
||||||
@ -1154,6 +1155,10 @@ virXMLParseHelper(int domcode,
|
|||||||
pctxt->_private = &private;
|
pctxt->_private = &private;
|
||||||
pctxt->sax->error = catchXMLError;
|
pctxt->sax->error = catchXMLError;
|
||||||
|
|
||||||
|
if (keepindent) {
|
||||||
|
parseFlags |= XML_PARSE_NOBLANKS;
|
||||||
|
}
|
||||||
|
|
||||||
if (filename) {
|
if (filename) {
|
||||||
xml = xmlCtxtReadFile(pctxt, filename, NULL, parseFlags);
|
xml = xmlCtxtReadFile(pctxt, filename, NULL, parseFlags);
|
||||||
} else {
|
} else {
|
||||||
|
@ -199,7 +199,8 @@ virXMLParseHelper(int domcode,
|
|||||||
const char *rootelement,
|
const char *rootelement,
|
||||||
xmlXPathContextPtr *ctxt,
|
xmlXPathContextPtr *ctxt,
|
||||||
const char *schemafile,
|
const char *schemafile,
|
||||||
bool validate);
|
bool validate,
|
||||||
|
bool keepindent);
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
virXMLPickShellSafeComment(const char *str1,
|
virXMLPickShellSafeComment(const char *str1,
|
||||||
@ -219,7 +220,17 @@ virXMLPickShellSafeComment(const char *str1,
|
|||||||
* Return the parsed document object, or NULL on failure.
|
* Return the parsed document object, or NULL on failure.
|
||||||
*/
|
*/
|
||||||
#define virXMLParse(filename, xmlStr, url, rootelement, ctxt, schemafile, validate) \
|
#define virXMLParse(filename, xmlStr, url, rootelement, ctxt, schemafile, validate) \
|
||||||
virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, rootelement, ctxt, schemafile, validate)
|
virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, rootelement, ctxt, schemafile, validate, false)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* virXMLParseWithIndent:
|
||||||
|
*
|
||||||
|
* Just like virXMLParse, except indentation is preserved. Should be used when
|
||||||
|
* facing an user provided XML which may be formatted back and keeping verbatim
|
||||||
|
* spacing is necessary (e.g. due to <metadata/>).
|
||||||
|
*/
|
||||||
|
#define virXMLParseWithIndent(filename, xmlStr, url, rootelement, ctxt, schemafile, validate) \
|
||||||
|
virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, rootelement, ctxt, schemafile, validate, true)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virXMLParseStringCtxt:
|
* virXMLParseStringCtxt:
|
||||||
@ -233,7 +244,17 @@ virXMLPickShellSafeComment(const char *str1,
|
|||||||
* Return the parsed document object, or NULL on failure.
|
* Return the parsed document object, or NULL on failure.
|
||||||
*/
|
*/
|
||||||
#define virXMLParseStringCtxt(xmlStr, url, pctxt) \
|
#define virXMLParseStringCtxt(xmlStr, url, pctxt) \
|
||||||
virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL, pctxt, NULL, false)
|
virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL, pctxt, NULL, false, false)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* virXMLParseStringCtxtWithIndent:
|
||||||
|
*
|
||||||
|
* Just like virXMLParseStringCtxt, except indentation is preserved. Should be
|
||||||
|
* used when facing an user provided XML which may be formatted back and
|
||||||
|
* keeping verbatim spacing is necessary (e.g. due to <metadata/>).
|
||||||
|
*/
|
||||||
|
#define virXMLParseStringCtxtWithIndent(xmlStr, url, pctxt) \
|
||||||
|
virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL, pctxt, NULL, false, true)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virXMLParseFileCtxt:
|
* virXMLParseFileCtxt:
|
||||||
@ -246,7 +267,7 @@ virXMLPickShellSafeComment(const char *str1,
|
|||||||
* Return the parsed document object, or NULL on failure.
|
* Return the parsed document object, or NULL on failure.
|
||||||
*/
|
*/
|
||||||
#define virXMLParseFileCtxt(filename, pctxt) \
|
#define virXMLParseFileCtxt(filename, pctxt) \
|
||||||
virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, NULL, pctxt, NULL, false)
|
virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, NULL, pctxt, NULL, false, false)
|
||||||
|
|
||||||
int
|
int
|
||||||
virXMLSaveFile(const char *path,
|
virXMLSaveFile(const char *path,
|
||||||
|
Loading…
Reference in New Issue
Block a user