[libvirt] [PATCHv2 1/5] Introduce virXMLValidator structure

Ján Tomko jtomko at redhat.com
Tue Jun 7 18:07:28 UTC 2016


Store all the data related to RNG validation in one structure to
allow splitting virXMLValidateAgainstSchema.
---
 src/util/virxml.c | 47 +++++++++++++++++++++++++++--------------------
 src/util/virxml.h | 10 ++++++++++
 2 files changed, 37 insertions(+), 20 deletions(-)

diff --git a/src/util/virxml.c b/src/util/virxml.c
index 489bad8..b3e4184 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -1108,56 +1108,63 @@ 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);
     return ret;
 }
diff --git a/src/util/virxml.h b/src/util/virxml.h
index b94de74..b75b109 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -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,14 @@ int virXMLInjectNamespace(xmlNodePtr node,
                           const char *uri,
                           const char *key);
 
+typedef struct _virXMLValidator {
+    xmlRelaxNGParserCtxtPtr rngParser;
+    xmlRelaxNGPtr rng;
+    xmlRelaxNGValidCtxtPtr rngValid;
+    virBuffer buf;
+    char *schemafile;
+} virXMLValidator, *virXMLValidatorPtr;
+
 int
 virXMLValidateAgainstSchema(const char *schemafile,
                             xmlDocPtr xml);
-- 
2.7.3




More information about the libvir-list mailing list