Introduce virXMLValidator structure

Store all the data related to RNG validation in one structure to
allow splitting virXMLValidateAgainstSchema.
This commit is contained in:
Ján Tomko 2016-06-07 18:55:28 +02:00
parent 9cda91d6f6
commit 71c68b40df
2 changed files with 40 additions and 20 deletions

View File

@ -1108,56 +1108,64 @@ int
virXMLValidateAgainstSchema(const char *schemafile,
xmlDocPtr doc)
{
xmlRelaxNGParserCtxtPtr rngParser = NULL;
xmlRelaxNGPtr rng = NULL;
xmlRelaxNGValidCtxtPtr rngValid = NULL;
virBuffer buf = VIR_BUFFER_INITIALIZER;
virXMLValidatorPtr validator = NULL;
int ret = -1;
if (!(rngParser = xmlRelaxNGNewParserCtxt(schemafile))) {
if (VIR_ALLOC(validator) < 0)
return -1;
if (VIR_STRDUP(validator->schemafile, schemafile) < 0)
goto cleanup;
if (!(validator->rngParser =
xmlRelaxNGNewParserCtxt(validator->schemafile))) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to create RNG parser for %s"),
schemafile);
validator->schemafile);
goto cleanup;
}
xmlRelaxNGSetParserErrors(rngParser,
xmlRelaxNGSetParserErrors(validator->rngParser,
catchRNGError,
ignoreRNGError,
&buf);
&validator->buf);
if (!(rng = xmlRelaxNGParse(rngParser))) {
if (!(validator->rng = xmlRelaxNGParse(validator->rngParser))) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to parse RNG %s: %s"),
schemafile, virBufferCurrentContent(&buf));
validator->schemafile,
virBufferCurrentContent(&validator->buf));
goto cleanup;
}
if (!(rngValid = xmlRelaxNGNewValidCtxt(rng))) {
if (!(validator->rngValid = xmlRelaxNGNewValidCtxt(validator->rng))) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to create RNG validation context %s"),
schemafile);
validator->schemafile);
goto cleanup;
}
xmlRelaxNGSetValidErrors(rngValid,
xmlRelaxNGSetValidErrors(validator->rngValid,
catchRNGError,
ignoreRNGError,
&buf);
&validator->buf);
if (xmlRelaxNGValidateDoc(rngValid, doc) != 0) {
if (xmlRelaxNGValidateDoc(validator->rngValid, doc) != 0) {
virReportError(VIR_ERR_XML_INVALID_SCHEMA,
_("Unable to validate doc against %s\n%s"),
schemafile, virBufferCurrentContent(&buf));
validator->schemafile,
virBufferCurrentContent(&validator->buf));
goto cleanup;
}
ret = 0;
cleanup:
virBufferFreeAndReset(&buf);
xmlRelaxNGFreeParserCtxt(rngParser);
xmlRelaxNGFreeValidCtxt(rngValid);
xmlRelaxNGFree(rng);
VIR_FREE(validator->schemafile);
virBufferFreeAndReset(&validator->buf);
xmlRelaxNGFreeParserCtxt(validator->rngParser);
xmlRelaxNGFreeValidCtxt(validator->rngValid);
xmlRelaxNGFree(validator->rng);
VIR_FREE(validator);
return ret;
}

View File

@ -30,6 +30,8 @@
# include <libxml/xpath.h>
# include <libxml/relaxng.h>
# include "virbuffer.h"
int virXPathBoolean(const char *xpath,
xmlXPathContextPtr ctxt);
char * virXPathString(const char *xpath,
@ -177,6 +179,16 @@ int virXMLInjectNamespace(xmlNodePtr node,
const char *uri,
const char *key);
struct _virXMLValidator {
xmlRelaxNGParserCtxtPtr rngParser;
xmlRelaxNGPtr rng;
xmlRelaxNGValidCtxtPtr rngValid;
virBuffer buf;
char *schemafile;
};
typedef struct _virXMLValidator virXMLValidator;
typedef virXMLValidator *virXMLValidatorPtr;
int
virXMLValidateAgainstSchema(const char *schemafile,
xmlDocPtr xml);