virxml.h 9.3 KB
Newer Older
1
/*
2
 * virxml.h: helper APIs for dealing with XML documents
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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/>.
19 20
 */

21
#pragma once
22

23
#include "internal.h"
24

25 26 27 28
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/relaxng.h>
29

30 31
#include "virbuffer.h"
#include "virautoclean.h"
J
Ján Tomko 已提交
32

33
int              virXPathBoolean(const char *xpath,
34
                                 xmlXPathContextPtr ctxt);
35
char *            virXPathString(const char *xpath,
36
                                 xmlXPathContextPtr ctxt);
37 38 39 40
char *       virXPathStringLimit(const char *xpath,
                                 size_t maxlen,
                                 xmlXPathContextPtr ctxt);
int               virXPathNumber(const char *xpath,
41 42
                                 xmlXPathContextPtr ctxt,
                                 double *value);
43 44 45 46 47 48
int                  virXPathInt(const char *xpath,
                                 xmlXPathContextPtr ctxt,
                                 int *value);
int                 virXPathUInt(const char *xpath,
                                 xmlXPathContextPtr ctxt,
                                 unsigned int *value);
49
int                 virXPathLong(const char *xpath,
50 51
                                 xmlXPathContextPtr ctxt,
                                 long *value);
52
int                virXPathULong(const char *xpath,
53 54
                                 xmlXPathContextPtr ctxt,
                                 unsigned long *value);
55
int            virXPathULongLong(const char *xpath,
M
Mark McLoughlin 已提交
56 57
                                 xmlXPathContextPtr ctxt,
                                 unsigned long long *value);
58
int             virXPathLongLong(const char *xpath,
59 60
                                 xmlXPathContextPtr ctxt,
                                 long long *value);
61
int              virXPathLongHex(const char *xpath,
62 63
                                 xmlXPathContextPtr ctxt,
                                 long *value);
64
int             virXPathULongHex(const char *xpath,
65 66
                                 xmlXPathContextPtr ctxt,
                                 unsigned long *value);
67
xmlNodePtr          virXPathNode(const char *xpath,
68
                                 xmlXPathContextPtr ctxt);
69
int              virXPathNodeSet(const char *xpath,
70 71
                                 xmlXPathContextPtr ctxt,
                                 xmlNodePtr **list);
72 73
char *          virXMLPropString(xmlNodePtr node,
                                 const char *name);
74 75 76
char *     virXMLPropStringLimit(xmlNodePtr node,
                                 const char *name,
                                 size_t maxlen);
77
char *   virXMLNodeContentString(xmlNodePtr node);
E
Eric Blake 已提交
78
long     virXMLChildElementCount(xmlNodePtr node);
79

80
/* Internal function; prefer the macros below.  */
81 82 83
xmlDocPtr      virXMLParseHelper(int domcode,
                                 const char *filename,
                                 const char *xmlStr,
84 85
                                 const char *url,
                                 xmlXPathContextPtr *pctxt);
86

J
Ján Tomko 已提交
87
const char *virXMLPickShellSafeComment(const char *str1, const char *str2);
88 89 90 91 92 93 94 95 96 97
/**
 * 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.
 */
98
#define virXMLParse(filename, xmlStr, url) \
99
    virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, NULL)
100

101 102 103 104 105 106 107 108 109
/**
 * 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.
 */
110
#define virXMLParseString(xmlStr, url) \
111
    virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL)
112

113 114 115 116 117 118 119 120
/**
 * virXMLParseFile:
 * @filename: file to parse
 *
 * Parse xml from a file.
 *
 * Return the parsed document object, or NULL on failure.
 */
121
#define virXMLParseFile(filename) \
122 123 124 125 126 127 128 129 130 131 132 133 134 135
    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.
 */
136
#define virXMLParseCtxt(filename, xmlStr, url, pctxt) \
137 138 139 140 141 142 143 144 145 146 147 148 149
    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.
 */
150
#define virXMLParseStringCtxt(xmlStr, url, pctxt) \
151 152 153 154 155 156 157 158 159 160 161 162
    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.
 */
163
#define virXMLParseFileCtxt(filename, pctxt) \
164
    virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, pctxt)
165

166 167 168 169 170
int virXMLSaveFile(const char *path,
                   const char *warnName,
                   const char *warnCommand,
                   const char *xml);

171 172
char *virXMLNodeToString(xmlDocPtr doc, xmlNodePtr node);

173 174 175
bool virXMLNodeNameEqual(xmlNodePtr node,
                         const char *name);

176 177 178 179 180 181 182
xmlNodePtr virXMLFindChildNodeByNs(xmlNodePtr root,
                                   const char *uri);

int virXMLExtractNamespaceXML(xmlNodePtr root,
                              const char *uri,
                              char **doc);

183 184 185 186
int virXMLInjectNamespace(xmlNodePtr node,
                          const char *uri,
                          const char *key);

187 188
void virXMLNodeSanitizeNamespaces(xmlNodePtr node);

189 190 191 192
int virXMLCheckIllegalChars(const char *nodeName,
                            const char *str,
                            const char *illegal);

J
Ján Tomko 已提交
193 194 195 196 197 198 199 200 201 202
struct _virXMLValidator {
    xmlRelaxNGParserCtxtPtr rngParser;
    xmlRelaxNGPtr rng;
    xmlRelaxNGValidCtxtPtr rngValid;
    virBuffer buf;
    char *schemafile;
};
typedef struct _virXMLValidator virXMLValidator;
typedef virXMLValidator *virXMLValidatorPtr;

J
Ján Tomko 已提交
203 204 205
virXMLValidatorPtr
virXMLValidatorInit(const char *schemafile);

J
Ján Tomko 已提交
206 207 208 209
int
virXMLValidatorValidate(virXMLValidatorPtr validator,
                        xmlDocPtr doc);

210 211 212
int
virXMLValidateAgainstSchema(const char *schemafile,
                            xmlDocPtr xml);
J
Ján Tomko 已提交
213 214
void
virXMLValidatorFree(virXMLValidatorPtr validator);
215

216 217 218 219
int
virXMLFormatElement(virBufferPtr buf,
                    const char *name,
                    virBufferPtr attrBuf,
220 221
                    virBufferPtr childBuf)
    ATTRIBUTE_RETURN_CHECK;
222

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
struct _virXPathContextNodeSave {
    xmlXPathContextPtr ctxt;
    xmlNodePtr node;
};
typedef struct _virXPathContextNodeSave virXPathContextNodeSave;
typedef virXPathContextNodeSave *virXPathContextNodeSavePtr;

void
virXPathContextNodeRestore(virXPathContextNodeSavePtr save);

VIR_DEFINE_AUTOCLEAN_FUNC(virXPathContextNodeSave, virXPathContextNodeRestore);

/**
 * 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.
 */
242
#define VIR_XPATH_NODE_AUTORESTORE(_ctxt) \
243 244 245 246
    VIR_AUTOCLEAN(virXPathContextNodeSave) _ctxt ## CtxtSave = { .ctxt = _ctxt,\
                                                                 .node = _ctxt->node}; \
    ignore_value(&_ctxt ## CtxtSave)

247 248
VIR_DEFINE_AUTOPTR_FUNC(xmlDoc, xmlFreeDoc);
VIR_DEFINE_AUTOPTR_FUNC(xmlXPathContext, xmlXPathFreeContext);
J
Ján Tomko 已提交
249 250 251 252 253 254 255 256 257 258

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;
J
Ján Tomko 已提交
259
    const char *prefix;
260
    const char *uri;
J
Ján Tomko 已提交
261 262 263
};
typedef struct _virXMLNamespace virXMLNamespace;
typedef virXMLNamespace *virXMLNamespacePtr;
264 265 266 267

void
virXMLNamespaceFormatNS(virBufferPtr buf,
                        virXMLNamespace const *ns);
268 269 270
int
virXMLNamespaceRegister(xmlXPathContextPtr ctxt,
                        virXMLNamespace const *ns);