# define VIR_WARNINGS_NO_CAST_ALIGN \
_Pragma ("GCC diagnostic push") \
_Pragma ("GCC diagnostic ignored \"-Wcast-align\"")
+# define VIR_WARNINGS_NO_PRINTF \
+ _Pragma ("GCC diagnostic push") \
+ _Pragma ("GCC diagnostic ignored \"-Wsuggest-attribute=format\"")
# define VIR_WARNINGS_RESET \
_Pragma ("GCC diagnostic pop")
# else
# define VIR_WARNINGS_NO_CAST_ALIGN
+# define VIR_WARNINGS_NO_PRINTF
# define VIR_WARNINGS_RESET
# endif
return 0;
}
+
+static void catchRNGError(void *ctx,
+ const char *msg,
+ ...)
+{
+ virBufferPtr buf = ctx;
+ va_list args;
+
+ va_start(args, msg);
+ VIR_WARNINGS_NO_PRINTF;
+ virBufferVasprintf(buf, msg, args);
+ VIR_WARNINGS_RESET;
+ va_end(args);
+}
+
+
+static void ignoreRNGError(void *ctx ATTRIBUTE_UNUSED,
+ const char *msg ATTRIBUTE_UNUSED,
+ ...)
+{}
+
+
+int
+virXMLValidateAgainstSchema(const char *schemafile,
+ xmlDocPtr doc)
+{
+ xmlRelaxNGParserCtxtPtr rngParser = NULL;
+ xmlRelaxNGPtr rng = NULL;
+ xmlRelaxNGValidCtxtPtr rngValid = NULL;
+ virBuffer buf = VIR_BUFFER_INITIALIZER;
+ int ret = -1;
+
+ if (!(rngParser = xmlRelaxNGNewParserCtxt(schemafile))) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unable to create RNG parser for %s"),
+ schemafile);
+ goto cleanup;
+ }
+
+ xmlRelaxNGSetParserErrors(rngParser,
+ catchRNGError,
+ ignoreRNGError,
+ &buf);
+
+ if (!(rng = xmlRelaxNGParse(rngParser))) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unable to parse RNG %s: %s"),
+ schemafile, virBufferCurrentContent(&buf));
+ goto cleanup;
+ }
+
+ if (!(rngValid = xmlRelaxNGNewValidCtxt(rng))) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unable to create RNG validation context %s"),
+ schemafile);
+ goto cleanup;
+ }
+
+ xmlRelaxNGSetValidErrors(rngValid,
+ catchRNGError,
+ ignoreRNGError,
+ &buf);
+
+ if (xmlRelaxNGValidateDoc(rngValid, doc) != 0) {
+ virReportError(VIR_ERR_XML_INVALID_SCHEMA,
+ _("Unable to validate doc against %s\n%s"),
+ schemafile, virBufferCurrentContent(&buf));
+ goto cleanup;
+ }
+
+ ret = 0;
+
+ cleanup:
+ virBufferFreeAndReset(&buf);
+ xmlRelaxNGFreeParserCtxt(rngParser);
+ xmlRelaxNGFreeValidCtxt(rngValid);
+ xmlRelaxNGFree(rng);
+ return ret;
+}
# include <libxml/parser.h>
# include <libxml/tree.h>
# include <libxml/xpath.h>
+# include <libxml/relaxng.h>
int virXPathBoolean(const char *xpath,
xmlXPathContextPtr ctxt);
const char *uri,
const char *key);
+int
+virXMLValidateAgainstSchema(const char *schemafile,
+ xmlDocPtr xml);
+
#endif /* __VIR_XML_H__ */