[gupnp-av/wip/lazy-namespaces: 5/7] Lazy-create XML namespaces



commit f8cd8c20f1608d326cb62327fc480a0144869ba6
Author: Jens Georg <mail jensge org>
Date:   Sat Dec 14 15:31:08 2013 +0100

    Lazy-create XML namespaces

 libgupnp-av/gupnp-didl-lite-container.c |    6 ++-
 libgupnp-av/gupnp-didl-lite-item.c      |    7 ++++-
 libgupnp-av/gupnp-didl-lite-object.c    |   51 +++++++++++++++++++++----------
 libgupnp-av/gupnp-didl-lite-resource.c  |   21 +++++++++++-
 libgupnp-av/gupnp-didl-lite-writer.c    |   12 -------
 libgupnp-av/xml-util.c                  |   11 +++++--
 libgupnp-av/xml-util.h                  |   11 ++++---
 tests/gtest/test-media-collection.c     |   19 ++++-------
 8 files changed, 85 insertions(+), 53 deletions(-)
---
diff --git a/libgupnp-av/gupnp-didl-lite-container.c b/libgupnp-av/gupnp-didl-lite-container.c
index b817863..ede5a4a 100644
--- a/libgupnp-av/gupnp-didl-lite-container.c
+++ b/libgupnp-av/gupnp-didl-lite-container.c
@@ -647,7 +647,8 @@ gupnp_didl_lite_container_set_container_update_id
 
         str = g_strdup_printf ("%u", update_id);
         xml_util_set_child (xml_node,
-                            upnp_ns,
+                            GUPNP_XML_NAMESPACE_UPNP,
+                            &upnp_ns,
                             xml_doc->doc,
                             "containerUpdateID",
                             str);
@@ -706,7 +707,8 @@ gupnp_didl_lite_container_set_total_deleted_child_count
 
         str = g_strdup_printf ("%u", count);
         xml_util_set_child (xml_node,
-                            upnp_ns,
+                            GUPNP_XML_NAMESPACE_UPNP,
+                            &upnp_ns,
                             xml_doc->doc,
                             "totalDeletedChildCount",
                             str);
diff --git a/libgupnp-av/gupnp-didl-lite-item.c b/libgupnp-av/gupnp-didl-lite-item.c
index f007f9b..68b6332 100644
--- a/libgupnp-av/gupnp-didl-lite-item.c
+++ b/libgupnp-av/gupnp-didl-lite-item.c
@@ -223,7 +223,12 @@ gupnp_didl_lite_item_set_lifetime (GUPnPDIDLLiteItem *item,
                 char *str;
 
                 str = seconds_to_time (lifetime);
-                xml_util_set_child (node, ns, doc->doc, "lifetime", str);
+                xml_util_set_child (node,
+                                    GUPNP_XML_NAMESPACE_DLNA,
+                                    &ns,
+                                    doc->doc,
+                                    "lifetime",
+                                    str);
                 g_free (str);
         }
 
diff --git a/libgupnp-av/gupnp-didl-lite-object.c b/libgupnp-av/gupnp-didl-lite-object.c
index 7b0e75f..b6f7bd2 100644
--- a/libgupnp-av/gupnp-didl-lite-object.c
+++ b/libgupnp-av/gupnp-didl-lite-object.c
@@ -960,9 +960,6 @@ gupnp_didl_lite_object_new_from_xml (xmlNode     *xml_node,
 {
         g_return_val_if_fail (xml_node != NULL, NULL);
         g_return_val_if_fail (xml_node->name != NULL, NULL);
-        g_return_val_if_fail (upnp_ns != NULL, NULL);
-        g_return_val_if_fail (dc_ns != NULL, NULL);
-        g_return_val_if_fail (dlna_ns != NULL, NULL);
 
         if (g_ascii_strcasecmp ((char *) xml_node->name, "container") == 0)
                 return g_object_new (GUPNP_TYPE_DIDL_LITE_CONTAINER,
@@ -1673,7 +1670,8 @@ gupnp_didl_lite_object_set_upnp_class (GUPnPDIDLLiteObject *object,
         g_return_if_fail (GUPNP_IS_DIDL_LITE_OBJECT (object));
 
         xml_util_set_child (object->priv->xml_node,
-                            object->priv->upnp_ns,
+                            GUPNP_XML_NAMESPACE_UPNP,
+                            &(object->priv->upnp_ns),
                             object->priv->xml_doc->doc,
                             "class",
                             upnp_class);
@@ -1765,7 +1763,8 @@ gupnp_didl_lite_object_set_title (GUPnPDIDLLiteObject *object,
         g_return_if_fail (GUPNP_IS_DIDL_LITE_OBJECT (object));
 
         xml_util_set_child (object->priv->xml_node,
-                            object->priv->dc_ns,
+                            GUPNP_XML_NAMESPACE_DC,
+                            &(object->priv->dc_ns),
                             object->priv->xml_doc->doc,
                             "title",
                             title);
@@ -1788,7 +1787,8 @@ gupnp_didl_lite_object_set_creator (GUPnPDIDLLiteObject *object,
         g_return_if_fail (GUPNP_IS_DIDL_LITE_OBJECT (object));
 
         xml_util_set_child (object->priv->xml_node,
-                            object->priv->dc_ns,
+                            GUPNP_XML_NAMESPACE_DC,
+                            &(object->priv->dc_ns),
                             object->priv->xml_doc->doc,
                             "creator",
                             creator);
@@ -1840,7 +1840,8 @@ gupnp_didl_lite_object_set_artist (GUPnPDIDLLiteObject *object,
         g_return_if_fail (GUPNP_IS_DIDL_LITE_OBJECT (object));
 
         xml_util_set_child (object->priv->xml_node,
-                            object->priv->upnp_ns,
+                            GUPNP_XML_NAMESPACE_UPNP,
+                            &(object->priv->upnp_ns),
                             object->priv->xml_doc->doc,
                             "artist",
                             artist);
@@ -1891,7 +1892,8 @@ gupnp_didl_lite_object_set_author (GUPnPDIDLLiteObject *object,
         g_return_if_fail (GUPNP_IS_DIDL_LITE_OBJECT (object));
 
         xml_util_set_child (object->priv->xml_node,
-                            object->priv->upnp_ns,
+                            GUPNP_XML_NAMESPACE_DC,
+                            &(object->priv->upnp_ns),
                             object->priv->xml_doc->doc,
                             "author",
                             author);
@@ -1940,7 +1942,8 @@ gupnp_didl_lite_object_set_genre (GUPnPDIDLLiteObject *object,
         g_return_if_fail (GUPNP_IS_DIDL_LITE_OBJECT (object));
 
         xml_util_set_child (object->priv->xml_node,
-                            object->priv->upnp_ns,
+                            GUPNP_XML_NAMESPACE_UPNP,
+                            &(object->priv->upnp_ns),
                             object->priv->xml_doc->doc,
                             "genre",
                             genre);
@@ -1963,7 +1966,8 @@ gupnp_didl_lite_object_set_write_status (GUPnPDIDLLiteObject *object,
         g_return_if_fail (GUPNP_IS_DIDL_LITE_OBJECT (object));
 
         xml_util_set_child (object->priv->xml_node,
-                            object->priv->dc_ns,
+                            GUPNP_XML_NAMESPACE_DC,
+                            &(object->priv->dc_ns),
                             object->priv->xml_doc->doc,
                             "writeStatus",
                             write_status);
@@ -1986,7 +1990,8 @@ gupnp_didl_lite_object_set_album (GUPnPDIDLLiteObject *object,
         g_return_if_fail (GUPNP_IS_DIDL_LITE_OBJECT (object));
 
         xml_util_set_child (object->priv->xml_node,
-                            object->priv->upnp_ns,
+                            GUPNP_XML_NAMESPACE_UPNP,
+                            &(object->priv->upnp_ns),
                             object->priv->xml_doc->doc,
                             "album",
                             album);
@@ -2011,10 +2016,16 @@ gupnp_didl_lite_object_set_album_art (GUPnPDIDLLiteObject *object,
         g_return_if_fail (GUPNP_IS_DIDL_LITE_OBJECT (object));
 
         node = xml_util_set_child (object->priv->xml_node,
-                                   object->priv->upnp_ns,
+                                   GUPNP_XML_NAMESPACE_UPNP,
+                                   &(object->priv->upnp_ns),
                                    object->priv->xml_doc->doc,
                                    "albumArtURI",
                                    album_art);
+
+        xml_util_get_ns (object->priv->xml_doc->doc,
+                         GUPNP_XML_NAMESPACE_DLNA,
+                         &(object->priv->dlna_ns));
+
         xmlSetNsProp (node,
                       object->priv->dlna_ns,
                       (const unsigned char *) "profileID",
@@ -2038,7 +2049,8 @@ gupnp_didl_lite_object_set_description (GUPnPDIDLLiteObject *object,
         g_return_if_fail (GUPNP_IS_DIDL_LITE_OBJECT (object));
 
         xml_util_set_child (object->priv->xml_node,
-                            object->priv->dc_ns,
+                            GUPNP_XML_NAMESPACE_DC,
+                            &(object->priv->dc_ns),
                             object->priv->xml_doc->doc,
                             "description",
                             description);
@@ -2061,7 +2073,8 @@ gupnp_didl_lite_object_set_date (GUPnPDIDLLiteObject *object,
         g_return_if_fail (GUPNP_IS_DIDL_LITE_OBJECT (object));
 
         xml_util_set_child (object->priv->xml_node,
-                            object->priv->dc_ns,
+                            GUPNP_XML_NAMESPACE_DC,
+                            &(object->priv->dc_ns),
                             object->priv->xml_doc->doc,
                             "date",
                             date);
@@ -2087,7 +2100,8 @@ gupnp_didl_lite_object_set_track_number (GUPnPDIDLLiteObject *object,
 
         str = g_strdup_printf ("%d", track_number);
         xml_util_set_child (object->priv->xml_node,
-                            object->priv->upnp_ns,
+                            GUPNP_XML_NAMESPACE_UPNP,
+                            &(object->priv->upnp_ns),
                             object->priv->xml_doc->doc,
                             "originalTrackNumber",
                             str);
@@ -2112,6 +2126,10 @@ gupnp_didl_lite_object_set_dlna_managed (GUPnPDIDLLiteObject *object,
         g_return_if_fail (object != NULL);
         g_return_if_fail (GUPNP_IS_DIDL_LITE_OBJECT (object));
 
+        xml_util_get_ns (object->priv->xml_doc->doc,
+                         GUPNP_XML_NAMESPACE_DLNA,
+                         &(object->priv->dlna_ns));
+
         str = g_strdup_printf ("%08x", dlna_managed);
         xmlSetNsProp (object->priv->xml_node,
                       object->priv->dlna_ns,
@@ -2140,7 +2158,8 @@ gupnp_didl_lite_object_set_update_id (GUPnPDIDLLiteObject *object,
 
         str = g_strdup_printf ("%u", update_id);
         xml_util_set_child (object->priv->xml_node,
-                            object->priv->upnp_ns,
+                            GUPNP_XML_NAMESPACE_UPNP,
+                            &(object->priv->upnp_ns),
                             object->priv->xml_doc->doc,
                             "objectUpdateID",
                             str);
diff --git a/libgupnp-av/gupnp-didl-lite-resource.c b/libgupnp-av/gupnp-didl-lite-resource.c
index 5ff54e3..e11f204 100644
--- a/libgupnp-av/gupnp-didl-lite-resource.c
+++ b/libgupnp-av/gupnp-didl-lite-resource.c
@@ -1462,6 +1462,10 @@ gupnp_didl_lite_resource_set_cleartext_size
         else {
                 char *str;
                 str = g_strdup_printf ("%" G_GINT64_FORMAT, cleartext_size);
+                xml_util_get_ns (resource->priv->xml_doc->doc,
+                                 GUPNP_XML_NAMESPACE_DLNA,
+                                 &(resource->priv->dlna_ns));
+
                 xmlSetNsProp (resource->priv->xml_node,
                               resource->priv->dlna_ns,
                               (unsigned char *) "cleartextSize",
@@ -1802,6 +1806,10 @@ gupnp_didl_lite_resource_set_track_total (GUPnPDIDLLiteResource *resource,
 
         g_return_if_fail (GUPNP_IS_DIDL_LITE_RESOURCE (resource));
 
+        xml_util_get_ns (resource->priv->xml_doc->doc,
+                         GUPNP_XML_NAMESPACE_DLNA,
+                         &(resource->priv->dlna_ns));
+
         str = g_strdup_printf ("%u", track_total);
         xmlSetNsProp (resource->priv->xml_node,
                       resource->priv->dlna_ns,
@@ -1909,11 +1917,15 @@ gupnp_didl_lite_resource_set_subtitle_file_uri
                 xmlUnsetNsProp (resource->priv->xml_node,
                                 resource->priv->pv_ns,
                                 (unsigned char *) "subtitleFileUri");
-        else
+        else {
+                xml_util_get_ns (resource->priv->xml_doc->doc,
+                                 GUPNP_XML_NAMESPACE_PV,
+                                 &(resource->priv->pv_ns));
                 xmlSetNsProp (resource->priv->xml_node,
                               resource->priv->pv_ns,
                               (unsigned char *) "subtitleFileUri",
                               uri);
+        }
 
         g_object_notify (G_OBJECT (resource), "subtitle-file-uri");
 }
@@ -1941,11 +1953,16 @@ gupnp_didl_lite_resource_set_subtitle_file_type
                 xmlUnsetNsProp (resource->priv->xml_node,
                                 resource->priv->pv_ns,
                                 (unsigned char *) "subtitleFileUri");
-        else
+        else {
+                xml_util_get_ns (resource->priv->xml_doc->doc,
+                                 GUPNP_XML_NAMESPACE_PV,
+                                 &(resource->priv->pv_ns));
+
                 xmlSetNsProp (resource->priv->xml_node,
                               resource->priv->pv_ns,
                               (unsigned char *) "subtitleFileType",
                               type);
+        }
 
         g_object_notify (G_OBJECT (resource), "subtitle-file-type");
 }
diff --git a/libgupnp-av/gupnp-didl-lite-writer.c b/libgupnp-av/gupnp-didl-lite-writer.c
index 1ced042..b61011b 100644
--- a/libgupnp-av/gupnp-didl-lite-writer.c
+++ b/libgupnp-av/gupnp-didl-lite-writer.c
@@ -399,18 +399,6 @@ gupnp_didl_lite_writer_constructed (GObject *object)
                                         (unsigned char *) "DIDL-Lite",
                                         NULL);
         xmlDocSetRootElement (priv->xml_doc->doc, priv->xml_node);
-        priv->dc_ns = xml_util_create_namespace (priv->xml_node,
-                                                 GUPNP_XML_NAMESPACE_DC);
-
-        priv->upnp_ns = xml_util_create_namespace (priv->xml_node,
-                                                   GUPNP_XML_NAMESPACE_UPNP);
-        /* Not adding dlna namespace declaration to any node yet.
-           Add the namespace to Didl-Lite element only if any of the child
-           nodes have dlna namespace prefix attributes */
-        priv->dlna_ns = xml_util_create_namespace (NULL,
-                                                   GUPNP_XML_NAMESPACE_DLNA);
-        priv->pv_ns = xml_util_create_namespace (priv->xml_node,
-                                                 GUPNP_XML_NAMESPACE_PV);
 
         xml_util_create_namespace (priv->xml_node,
                                    GUPNP_XML_NAMESPACE_DIDL_LITE);
diff --git a/libgupnp-av/xml-util.c b/libgupnp-av/xml-util.c
index 91d14f4..41ede36 100644
--- a/libgupnp-av/xml-util.c
+++ b/libgupnp-av/xml-util.c
@@ -236,7 +236,8 @@ xml_util_get_int64_attribute (xmlNode    *node,
 
 xmlNode *
 xml_util_set_child (xmlNode    *parent_node,
-                    xmlNs      *namespace,
+                    GUPnPXMLNamespace ns,
+                    xmlNsPtr   *xmlns,
                     xmlDoc     *doc,
                     const char *name,
                     const char *value)
@@ -245,11 +246,15 @@ xml_util_set_child (xmlNode    *parent_node,
         xmlChar *escaped;
 
         node = xml_util_get_element (parent_node, name, NULL);
-        if (node == NULL)
+        if (node == NULL) {
+                xmlNsPtr ns_ptr = NULL;
+
+                ns_ptr = xml_util_get_ns (doc, ns, xmlns);
                 node = xmlNewChild (parent_node,
-                                    namespace,
+                                    ns_ptr,
                                     (unsigned char *) name,
                                     NULL);
+        }
 
         escaped = xmlEncodeSpecialChars (doc, (const unsigned char *) value);
         xmlNodeSetContent (node, escaped);
diff --git a/libgupnp-av/xml-util.h b/libgupnp-av/xml-util.h
index 064dfe8..811a516 100644
--- a/libgupnp-av/xml-util.h
+++ b/libgupnp-av/xml-util.h
@@ -94,11 +94,12 @@ xml_util_get_int64_attribute            (xmlNode    *node,
                                          gint64      default_value);
 
 G_GNUC_INTERNAL xmlNode *
-xml_util_set_child                      (xmlNode    *parent_node,
-                                         xmlNs      *namespace,
-                                         xmlDoc     *doc,
-                                         const char *name,
-                                         const char *value);
+xml_util_set_child                      (xmlNode          *parent_node,
+                                         GUPnPXMLNamespace ns,
+                                         xmlNsPtr         *namespace,
+                                         xmlDoc           *doc,
+                                         const char       *name,
+                                         const char       *value);
 
 G_GNUC_INTERNAL void
 xml_util_unset_child                    (xmlNode    *parent_node,
diff --git a/tests/gtest/test-media-collection.c b/tests/gtest/test-media-collection.c
index c7d98c7..930d872 100644
--- a/tests/gtest/test-media-collection.c
+++ b/tests/gtest/test-media-collection.c
@@ -32,7 +32,6 @@
 "    xmlns:dc=\"http://purl.org/dc/elements/1.1/\""; \
 "    xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\"" \
 "    xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\"" \
-"    xmlns:pv=\"http://www.pv.com/pvns/\""; \
 "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""; \
 "    xsi:schemaLocation=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" \
 "        http://www.upnp.org/schemas/av/didl-lite-v2-20060531.xsd"; \
@@ -60,10 +59,9 @@
 #define TEST_PARSE_COLLECTION_2 \
 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" \
 "<DIDL-Lite" \
-"    xmlns:dc=\"http://purl.org/dc/elements/1.1/\""; \
 "    xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\"" \
+"    xmlns:dc=\"http://purl.org/dc/elements/1.1/\""; \
 "    xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\"" \
-"    xmlns:pv=\"http://www.pv.com/pvns/\""; \
 "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""; \
 "    xsi:schemaLocation=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" \
 "        http://www.upnp.org/schemas/av/didl-lite-v2-20060531.xsd"; \
@@ -93,10 +91,9 @@
 
 #define TEST_CREATE_FLAT \
 "<DIDL-Lite " \
+        "xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\" " \
         "xmlns:dc=\"http://purl.org/dc/elements/1.1/\"; " \
-        "xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\" " \
-        "xmlns:pv=\"http://www.pv.com/pvns/\"; " \
-        "xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\">" \
+        "xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\">" \
     "<item restricted=\"1\">" \
       "<dc:title>Song1</dc:title>" \
       "<upnp:class>object.item.audioItem</upnp:class>" \
@@ -116,10 +113,9 @@
 
 #define TEST_CREATE_FULL \
 "<DIDL-Lite " \
+        "xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\" " \
         "xmlns:dc=\"http://purl.org/dc/elements/1.1/\"; " \
-        "xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\" " \
-        "xmlns:pv=\"http://www.pv.com/pvns/\"; " \
-        "xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\">" \
+        "xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\">" \
     "<container>" \
       "<dc:title>TestCollection1</dc:title>" \
       "<dc:creator>TestCollection1Author</dc:creator>" \
@@ -143,10 +139,9 @@
 
 #define TEST_CREATE_FULL_REPARENT \
 "<DIDL-Lite " \
+        "xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\" " \
         "xmlns:dc=\"http://purl.org/dc/elements/1.1/\"; " \
-        "xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\" " \
-        "xmlns:pv=\"http://www.pv.com/pvns/\"; " \
-        "xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\">" \
+        "xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\">" \
     "<container>" \
       "<item restricted=\"1\">" \
         "<dc:title>Song1</dc:title>" \


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