2006-02-16 22:50:52 +00:00
|
|
|
/*
|
2014-03-07 13:38:51 +00:00
|
|
|
* virxml.h: helper APIs for dealing with XML documents
|
2012-12-13 18:13:21 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2005, 2007-2012 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
2006-02-16 22:50:52 +00:00
|
|
|
*/
|
|
|
|
|
2019-06-18 16:13:06 +00:00
|
|
|
#pragma once
|
2006-02-16 22:50:52 +00:00
|
|
|
|
2019-06-18 16:13:06 +00:00
|
|
|
#include "internal.h"
|
2006-02-16 22:50:52 +00:00
|
|
|
|
2019-06-18 16:13:06 +00:00
|
|
|
#include <libxml/parser.h>
|
|
|
|
#include <libxml/tree.h>
|
|
|
|
#include <libxml/xpath.h>
|
|
|
|
#include <libxml/relaxng.h>
|
2006-02-16 22:50:52 +00:00
|
|
|
|
2019-06-18 16:13:06 +00:00
|
|
|
#include "virbuffer.h"
|
2016-06-07 16:55:28 +00:00
|
|
|
|
2019-09-09 06:33:58 +00:00
|
|
|
xmlXPathContextPtr virXMLXPathContextNew(xmlDocPtr xml)
|
2019-10-14 12:25:14 +00:00
|
|
|
G_GNUC_WARN_UNUSED_RESULT;
|
2019-09-09 06:33:58 +00:00
|
|
|
|
2010-02-04 21:52:34 +00:00
|
|
|
int virXPathBoolean(const char *xpath,
|
2008-04-10 16:54:54 +00:00
|
|
|
xmlXPathContextPtr ctxt);
|
2010-02-04 21:52:34 +00:00
|
|
|
char * virXPathString(const char *xpath,
|
2008-04-10 16:54:54 +00:00
|
|
|
xmlXPathContextPtr ctxt);
|
2010-02-04 21:52:34 +00:00
|
|
|
char * virXPathStringLimit(const char *xpath,
|
|
|
|
size_t maxlen,
|
|
|
|
xmlXPathContextPtr ctxt);
|
|
|
|
int virXPathNumber(const char *xpath,
|
2008-04-10 16:54:54 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
double *value);
|
2011-01-13 22:09:18 +00:00
|
|
|
int virXPathInt(const char *xpath,
|
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
int *value);
|
|
|
|
int virXPathUInt(const char *xpath,
|
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
unsigned int *value);
|
2010-02-04 21:52:34 +00:00
|
|
|
int virXPathLong(const char *xpath,
|
2008-04-10 16:54:54 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
long *value);
|
2011-01-13 22:09:18 +00:00
|
|
|
int virXPathULong(const char *xpath,
|
2008-07-11 10:48:34 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
unsigned long *value);
|
2010-02-04 21:52:34 +00:00
|
|
|
int virXPathULongLong(const char *xpath,
|
2009-02-24 14:54:30 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
unsigned long long *value);
|
2011-01-13 22:09:18 +00:00
|
|
|
int virXPathLongLong(const char *xpath,
|
2010-02-02 17:49:09 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
long long *value);
|
2011-01-13 22:09:18 +00:00
|
|
|
int virXPathLongHex(const char *xpath,
|
2009-02-24 14:53:30 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
long *value);
|
2010-02-04 21:52:34 +00:00
|
|
|
int virXPathULongHex(const char *xpath,
|
2009-02-24 14:53:30 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
unsigned long *value);
|
2010-02-04 21:52:34 +00:00
|
|
|
xmlNodePtr virXPathNode(const char *xpath,
|
2008-04-10 16:54:54 +00:00
|
|
|
xmlXPathContextPtr ctxt);
|
2010-02-04 21:52:34 +00:00
|
|
|
int virXPathNodeSet(const char *xpath,
|
2008-04-10 16:54:54 +00:00
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
xmlNodePtr **list);
|
2008-07-11 16:23:36 +00:00
|
|
|
char * virXMLPropString(xmlNodePtr node,
|
|
|
|
const char *name);
|
2017-08-15 13:20:55 +00:00
|
|
|
char * virXMLPropStringLimit(xmlNodePtr node,
|
|
|
|
const char *name,
|
|
|
|
size_t maxlen);
|
2017-08-16 08:26:09 +00:00
|
|
|
char * virXMLNodeContentString(xmlNodePtr node);
|
2011-12-07 22:06:18 +00:00
|
|
|
long virXMLChildElementCount(xmlNodePtr node);
|
2008-07-11 16:23:36 +00:00
|
|
|
|
2011-08-18 21:01:36 +00:00
|
|
|
/* Internal function; prefer the macros below. */
|
2010-02-24 20:44:47 +00:00
|
|
|
xmlDocPtr virXMLParseHelper(int domcode,
|
|
|
|
const char *filename,
|
|
|
|
const char *xmlStr,
|
2011-08-18 21:01:36 +00:00
|
|
|
const char *url,
|
|
|
|
xmlXPathContextPtr *pctxt);
|
2010-02-24 20:44:47 +00:00
|
|
|
|
2012-10-29 12:15:55 +00:00
|
|
|
const char *virXMLPickShellSafeComment(const char *str1, const char *str2);
|
2011-08-18 21:01:36 +00:00
|
|
|
/**
|
|
|
|
* virXMLParse:
|
|
|
|
* @filename: file to parse, or NULL for string parsing
|
|
|
|
* @xmlStr: if @filename is NULL, a string to parse
|
|
|
|
* @url: if @filename is NULL, an optional filename to attribute the parse to
|
|
|
|
*
|
|
|
|
* Parse xml from either a file or a string.
|
|
|
|
*
|
|
|
|
* Return the parsed document object, or NULL on failure.
|
|
|
|
*/
|
2019-06-18 16:13:06 +00:00
|
|
|
#define virXMLParse(filename, xmlStr, url) \
|
2011-08-18 21:01:36 +00:00
|
|
|
virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, NULL)
|
2010-02-24 20:44:47 +00:00
|
|
|
|
2011-08-18 21:01:36 +00:00
|
|
|
/**
|
|
|
|
* virXMLParseString:
|
|
|
|
* @xmlStr: a string to parse
|
|
|
|
* @url: an optional filename to attribute the parse to
|
|
|
|
*
|
|
|
|
* Parse xml from a string.
|
|
|
|
*
|
|
|
|
* Return the parsed document object, or NULL on failure.
|
|
|
|
*/
|
2019-06-18 16:13:06 +00:00
|
|
|
#define virXMLParseString(xmlStr, url) \
|
2011-08-18 21:01:36 +00:00
|
|
|
virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL)
|
2010-02-24 20:44:47 +00:00
|
|
|
|
2011-08-18 21:01:36 +00:00
|
|
|
/**
|
|
|
|
* virXMLParseFile:
|
|
|
|
* @filename: file to parse
|
|
|
|
*
|
|
|
|
* Parse xml from a file.
|
|
|
|
*
|
|
|
|
* Return the parsed document object, or NULL on failure.
|
|
|
|
*/
|
2019-06-18 16:13:06 +00:00
|
|
|
#define virXMLParseFile(filename) \
|
2011-08-18 21:01:36 +00:00
|
|
|
virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, NULL)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virXMLParseCtxt:
|
|
|
|
* @filename: file to parse, or NULL for string parsing
|
|
|
|
* @xmlStr: if @filename is NULL, a string to parse
|
|
|
|
* @url: if @filename is NULL, an optional filename to attribute the parse to
|
|
|
|
* @pctxt: if non-NULL, populate with a new context object on success,
|
|
|
|
* with (*pctxt)->node pre-set to the root node
|
|
|
|
*
|
|
|
|
* Parse xml from either a file or a string.
|
|
|
|
*
|
|
|
|
* Return the parsed document object, or NULL on failure.
|
|
|
|
*/
|
2019-06-18 16:13:06 +00:00
|
|
|
#define virXMLParseCtxt(filename, xmlStr, url, pctxt) \
|
2011-08-18 21:01:36 +00:00
|
|
|
virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, pctxt)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virXMLParseStringCtxt:
|
|
|
|
* @xmlStr: a string to parse
|
|
|
|
* @url: an optional filename to attribute the parse to
|
|
|
|
* @pctxt: if non-NULL, populate with a new context object on success,
|
|
|
|
* with (*pctxt)->node pre-set to the root node
|
|
|
|
*
|
|
|
|
* Parse xml from a string.
|
|
|
|
*
|
|
|
|
* Return the parsed document object, or NULL on failure.
|
|
|
|
*/
|
2019-06-18 16:13:06 +00:00
|
|
|
#define virXMLParseStringCtxt(xmlStr, url, pctxt) \
|
2011-08-18 21:01:36 +00:00
|
|
|
virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, pctxt)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virXMLParseFileCtxt:
|
|
|
|
* @filename: file to parse
|
|
|
|
* @pctxt: if non-NULL, populate with a new context object on success,
|
|
|
|
* with (*pctxt)->node pre-set to the root node
|
|
|
|
*
|
|
|
|
* Parse xml from a file.
|
|
|
|
*
|
|
|
|
* Return the parsed document object, or NULL on failure.
|
|
|
|
*/
|
2019-06-18 16:13:06 +00:00
|
|
|
#define virXMLParseFileCtxt(filename, pctxt) \
|
2011-08-18 21:01:36 +00:00
|
|
|
virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, pctxt)
|
2010-02-24 20:44:47 +00:00
|
|
|
|
2011-10-06 09:57:06 +00:00
|
|
|
int virXMLSaveFile(const char *path,
|
|
|
|
const char *warnName,
|
|
|
|
const char *warnCommand,
|
|
|
|
const char *xml);
|
|
|
|
|
2013-09-04 14:49:40 +00:00
|
|
|
char *virXMLNodeToString(xmlDocPtr doc, xmlNodePtr node);
|
|
|
|
|
2017-08-14 12:31:52 +00:00
|
|
|
bool virXMLNodeNameEqual(xmlNodePtr node,
|
|
|
|
const char *name);
|
|
|
|
|
2013-09-06 15:34:43 +00:00
|
|
|
xmlNodePtr virXMLFindChildNodeByNs(xmlNodePtr root,
|
|
|
|
const char *uri);
|
|
|
|
|
|
|
|
int virXMLExtractNamespaceXML(xmlNodePtr root,
|
|
|
|
const char *uri,
|
|
|
|
char **doc);
|
|
|
|
|
2013-09-09 09:49:11 +00:00
|
|
|
int virXMLInjectNamespace(xmlNodePtr node,
|
|
|
|
const char *uri,
|
|
|
|
const char *key);
|
|
|
|
|
2016-06-24 15:19:35 +00:00
|
|
|
void virXMLNodeSanitizeNamespaces(xmlNodePtr node);
|
|
|
|
|
2016-10-19 20:57:46 +00:00
|
|
|
int virXMLCheckIllegalChars(const char *nodeName,
|
|
|
|
const char *str,
|
|
|
|
const char *illegal);
|
|
|
|
|
2016-06-07 16:55:28 +00:00
|
|
|
struct _virXMLValidator {
|
|
|
|
xmlRelaxNGParserCtxtPtr rngParser;
|
|
|
|
xmlRelaxNGPtr rng;
|
|
|
|
xmlRelaxNGValidCtxtPtr rngValid;
|
|
|
|
virBuffer buf;
|
|
|
|
char *schemafile;
|
|
|
|
};
|
|
|
|
typedef struct _virXMLValidator virXMLValidator;
|
|
|
|
typedef virXMLValidator *virXMLValidatorPtr;
|
|
|
|
|
2016-06-07 16:56:23 +00:00
|
|
|
virXMLValidatorPtr
|
|
|
|
virXMLValidatorInit(const char *schemafile);
|
|
|
|
|
2016-06-07 16:58:02 +00:00
|
|
|
int
|
|
|
|
virXMLValidatorValidate(virXMLValidatorPtr validator,
|
|
|
|
xmlDocPtr doc);
|
|
|
|
|
2014-11-18 14:50:05 +00:00
|
|
|
int
|
|
|
|
virXMLValidateAgainstSchema(const char *schemafile,
|
|
|
|
xmlDocPtr xml);
|
2016-06-07 16:57:29 +00:00
|
|
|
void
|
|
|
|
virXMLValidatorFree(virXMLValidatorPtr validator);
|
2014-11-18 14:50:05 +00:00
|
|
|
|
2019-10-24 13:50:50 +00:00
|
|
|
void
|
2017-08-24 13:08:23 +00:00
|
|
|
virXMLFormatElement(virBufferPtr buf,
|
|
|
|
const char *name,
|
|
|
|
virBufferPtr attrBuf,
|
2019-10-24 13:50:50 +00:00
|
|
|
virBufferPtr childBuf);
|
2017-08-24 13:08:23 +00:00
|
|
|
|
2019-02-26 16:34:57 +00:00
|
|
|
struct _virXPathContextNodeSave {
|
|
|
|
xmlXPathContextPtr ctxt;
|
|
|
|
xmlNodePtr node;
|
|
|
|
};
|
|
|
|
typedef struct _virXPathContextNodeSave virXPathContextNodeSave;
|
|
|
|
typedef virXPathContextNodeSave *virXPathContextNodeSavePtr;
|
|
|
|
|
|
|
|
void
|
|
|
|
virXPathContextNodeRestore(virXPathContextNodeSavePtr save);
|
|
|
|
|
2019-10-15 12:47:50 +00:00
|
|
|
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(virXPathContextNodeSave, virXPathContextNodeRestore);
|
2019-02-26 16:34:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* VIR_XPATH_NODE_AUTORESTORE:
|
|
|
|
* @ctxt: XML XPath context pointer
|
|
|
|
*
|
|
|
|
* This macro ensures that when the scope where it's used ends, @ctxt's current
|
|
|
|
* node pointer is reset to the original value when this macro was used.
|
|
|
|
*/
|
2019-06-18 16:13:06 +00:00
|
|
|
#define VIR_XPATH_NODE_AUTORESTORE(_ctxt) \
|
2019-10-15 12:47:50 +00:00
|
|
|
g_auto(virXPathContextNodeSave) _ctxt ## CtxtSave = { .ctxt = _ctxt,\
|
|
|
|
.node = _ctxt->node}; \
|
2019-02-26 16:34:57 +00:00
|
|
|
ignore_value(&_ctxt ## CtxtSave)
|
|
|
|
|
2019-10-15 12:47:50 +00:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(xmlDoc, xmlFreeDoc);
|
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(xmlXPathContext, xmlXPathFreeContext);
|
2020-06-10 03:43:56 +00:00
|
|
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(xmlBuffer, xmlBufferFree);
|
2019-08-20 15:45:10 +00:00
|
|
|
|
|
|
|
typedef int (*virXMLNamespaceParse)(xmlXPathContextPtr ctxt, void **nsdata);
|
|
|
|
typedef void (*virXMLNamespaceFree)(void *nsdata);
|
|
|
|
typedef int (*virXMLNamespaceFormat)(virBufferPtr buf, void *nsdata);
|
|
|
|
typedef const char *(*virXMLNamespaceHref)(void);
|
|
|
|
|
|
|
|
struct _virXMLNamespace {
|
|
|
|
virXMLNamespaceParse parse;
|
|
|
|
virXMLNamespaceFree free;
|
|
|
|
virXMLNamespaceFormat format;
|
2019-08-20 22:03:15 +00:00
|
|
|
const char *prefix;
|
2019-08-21 07:48:47 +00:00
|
|
|
const char *uri;
|
2019-08-20 15:45:10 +00:00
|
|
|
};
|
|
|
|
typedef struct _virXMLNamespace virXMLNamespace;
|
|
|
|
typedef virXMLNamespace *virXMLNamespacePtr;
|
2019-08-20 20:50:10 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
virXMLNamespaceFormatNS(virBufferPtr buf,
|
|
|
|
virXMLNamespace const *ns);
|
2019-08-20 20:14:13 +00:00
|
|
|
int
|
|
|
|
virXMLNamespaceRegister(xmlXPathContextPtr ctxt,
|
|
|
|
virXMLNamespace const *ns);
|
2020-05-28 10:38:43 +00:00
|
|
|
|
|
|
|
int virParseScaledValue(const char *xpath,
|
|
|
|
const char *units_xpath,
|
|
|
|
xmlXPathContextPtr ctxt,
|
|
|
|
unsigned long long *val,
|
|
|
|
unsigned long long scale,
|
|
|
|
unsigned long long max,
|
|
|
|
bool required);
|