[gupnp-av/wip/didl-lite-fragments] Rework XSDData.



commit 01b955f2098c64a433a2616cd3702b55a728a3d8
Author: Krzesimir Nowak <krnowak openismus com>
Date:   Wed Oct 24 14:59:04 2012 +0200

    Rework XSDData.

 libgupnp-av/xsd-data.c |   93 ++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 71 insertions(+), 22 deletions(-)
---
diff --git a/libgupnp-av/xsd-data.c b/libgupnp-av/xsd-data.c
index 950f70a..f74e549 100644
--- a/libgupnp-av/xsd-data.c
+++ b/libgupnp-av/xsd-data.c
@@ -22,8 +22,6 @@
 #include "xsd-data.h"
 
 struct _XSDData {
-        xmlDocPtr schema_doc;
-        xmlSchemaParserCtxtPtr parser_context;
         xmlSchemaPtr schema;
         xmlSchemaValidCtxtPtr valid_context;
 };
@@ -33,18 +31,12 @@ xsd_data_new (const gchar *xsd_file)
 {
         XSDData *xsd_data = g_slice_new0 (XSDData);
         gboolean failed = TRUE;
+        xmlSchemaParserCtxtPtr context = xmlSchemaNewParserCtxt (xsd_file);
 
-        return xsd_data;
-
-        xsd_data->schema_doc = xmlReadFile (xsd_file, NULL, XML_PARSE_NONET);
-        if (xsd_data->schema_doc == NULL)
-                /* the schema cannot be loaded or is not well-formed */
-                goto out;
-        xsd_data->parser_context = xmlSchemaNewDocParserCtxt (xsd_data->schema_doc);
-        if (xsd_data->parser_context == NULL)
+        if (context == NULL)
                 /* unable to create a parser context for the schema */
                 goto out;
-        xsd_data->schema = xmlSchemaParse (xsd_data->parser_context);
+        xsd_data->schema = xmlSchemaParse (context);
         if (xsd_data->schema == NULL)
                 /* the schema itself is not valid */
                 goto out;
@@ -54,11 +46,13 @@ xsd_data_new (const gchar *xsd_file)
                 goto out;
         failed = FALSE;
  out:
+        if (context != NULL)
+                xmlSchemaFreeParserCtxt (context);
         if (failed) {
                 xsd_data_free (xsd_data);
                 xsd_data = NULL;
         }
-        
+
         return xsd_data;
 }
 
@@ -71,10 +65,6 @@ xsd_data_free (XSDData *xsd_data)
                 xmlSchemaFreeValidCtxt (xsd_data->valid_context);
         if (xsd_data->schema != NULL)
                 xmlSchemaFree (xsd_data->schema);
-        if (xsd_data->parser_context != NULL)
-                xmlSchemaFreeParserCtxt (xsd_data->parser_context);
-        if (xsd_data->schema_doc != NULL)
-                xmlFreeDoc (xsd_data->schema_doc);
         g_slice_free (XSDData, xsd_data);
 }
 
@@ -82,13 +72,72 @@ gboolean
 xsd_data_validate_doc (XSDData *xsd_data,
                        xmlDoc  *doc)
 {
+        static xmlSAXHandler empty_handler = {
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                NULL,
+                XML_SAX2_MAGIC,
+                NULL,
+                NULL,
+                NULL,
+                NULL
+        };
         xmlChar *dump = NULL;
+        int size = 0;
+        xmlParserInputBufferPtr buffer = NULL;
+        gboolean result = FALSE;
 
-        xmlDocDumpMemory (doc, &dump, NULL);
-        g_debug ("Doc dump:\n%s", dump);
-        xmlFree (dump);
-
-        return TRUE;
+        if (xsd_data == NULL)
+                return TRUE;
 
-        return (xmlSchemaValidateDoc (xsd_data->valid_context, doc) == 0);
+        xmlDocDumpMemory (doc, &dump, &size);
+        if (dump == NULL)
+                goto out;
+        g_debug ("Doc dump:\n%s", dump);
+        buffer = xmlParserInputBufferCreateMem ((char *) dump,
+                                                   size,
+                                                   XML_CHAR_ENCODING_NONE);
+        if (buffer == NULL)
+                goto out;
+        if (!xmlSchemaValidateStream (xsd_data->valid_context,
+                                      buffer,
+                                      XML_CHAR_ENCODING_NONE,
+                                      &empty_handler,
+                                      NULL))
+                result = TRUE;
+ out:
+        /* Commented out, because it crashes because of double free. I
+         * suppose that it is freed by xmlSchemaValidateStream.
+         */
+        /*
+        if (buffer)
+                xmlFreeParserInputBuffer (buffer);
+        */
+        if (dump != NULL)
+                xmlFree (dump);
+        return result;
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]