[gupnp-av/wip/lazy-namespaces: 2/7] Add utility functions for ns creation



commit da457eccdffc9b26a4e9e2a463815168f97186e9
Author: Jens Georg <mail jensge org>
Date:   Sat Dec 14 13:16:17 2013 +0100

    Add utility functions for ns creation

 libgupnp-av/gupnp-didl-lite-parser.c |   28 +++++++------------------
 libgupnp-av/gupnp-didl-lite-writer.c |   36 +++++++++++----------------------
 libgupnp-av/xml-util.c               |   33 +++++++++++++++++++++++++++++++
 libgupnp-av/xml-util.h               |   14 +++++++++++++
 4 files changed, 67 insertions(+), 44 deletions(-)
---
diff --git a/libgupnp-av/gupnp-didl-lite-parser.c b/libgupnp-av/gupnp-didl-lite-parser.c
index c5b8500..8f31605 100644
--- a/libgupnp-av/gupnp-didl-lite-parser.c
+++ b/libgupnp-av/gupnp-didl-lite-parser.c
@@ -304,29 +304,17 @@ gupnp_didl_lite_parser_parse_didl_recursive (GUPnPDIDLLiteParser *parser,
 
         /* Create UPnP and DC namespaces if they don't exist */
         if (! upnp_ns)
-                upnp_ns = xmlNewNs (xmlDocGetRootElement (doc),
-                                    (unsigned char *)
-                                    "urn:schemas-upnp-org:metadata-1-0/upnp/",
-                                    (unsigned char *)
-                                    GUPNP_DIDL_LITE_WRITER_NAMESPACE_UPNP);
+                upnp_ns = xml_util_create_namespace (xmlDocGetRootElement (doc),
+                                                     GUPNP_XML_NAMESPACE_UPNP);
         if (! dc_ns)
-                dc_ns = xmlNewNs (xmlDocGetRootElement (doc),
-                                  (unsigned char *)
-                                  "http://purl.org/dc/elements/1.1/";,
-                                  (unsigned char *)
-                                  GUPNP_DIDL_LITE_WRITER_NAMESPACE_DC);
+                dc_ns = xml_util_create_namespace (xmlDocGetRootElement (doc),
+                                                   GUPNP_XML_NAMESPACE_DC);
         if (! dlna_ns)
-                dlna_ns = xmlNewNs (xmlDocGetRootElement (doc),
-                                    (unsigned char *)
-                                    "urn:schemas-dlna-org:metadata-2-0/",
-                                    (unsigned char *)
-                                    GUPNP_DIDL_LITE_WRITER_NAMESPACE_DLNA);
+                dlna_ns = xml_util_create_namespace (xmlDocGetRootElement (doc),
+                                                   GUPNP_XML_NAMESPACE_DLNA);
         if (! pv_ns)
-                pv_ns = xmlNewNs (xmlDocGetRootElement (doc),
-                                    (unsigned char *)
-                                    "http://www.pv.com/pvns/";,
-                                    (unsigned char *)
-                                    GUPNP_DIDL_LITE_WRITER_NAMESPACE_PV);
+                pv_ns = xml_util_create_namespace (xmlDocGetRootElement (doc),
+                                                   GUPNP_XML_NAMESPACE_PV);
 
         xml_doc = gupnp_xml_doc_new (doc);
 
diff --git a/libgupnp-av/gupnp-didl-lite-writer.c b/libgupnp-av/gupnp-didl-lite-writer.c
index 558eb47..1ced042 100644
--- a/libgupnp-av/gupnp-didl-lite-writer.c
+++ b/libgupnp-av/gupnp-didl-lite-writer.c
@@ -399,33 +399,21 @@ gupnp_didl_lite_writer_constructed (GObject *object)
                                         (unsigned char *) "DIDL-Lite",
                                         NULL);
         xmlDocSetRootElement (priv->xml_doc->doc, priv->xml_node);
-        priv->dc_ns = xmlNewNs (priv->xml_node,
-                                (unsigned char *)
-                                "http://purl.org/dc/elements/1.1/";,
-                                (unsigned char *)
-                                GUPNP_DIDL_LITE_WRITER_NAMESPACE_DC);
-        priv->upnp_ns = xmlNewNs (priv->xml_node,
-                                  (unsigned char *)
-                                  "urn:schemas-upnp-org:metadata-1-0/upnp/",
-                                  (unsigned char *)
-                                  GUPNP_DIDL_LITE_WRITER_NAMESPACE_UPNP);
+        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 = xmlNewNs (NULL,
-                                  (unsigned char *)
-                                  "urn:schemas-dlna-org:metadata-1-0/",
-                                  (unsigned char *)
-                                  GUPNP_DIDL_LITE_WRITER_NAMESPACE_DLNA);
-        priv->pv_ns = xmlNewNs (priv->xml_node,
-                                 (unsigned char *)
-                                 "http://www.pv.com/pvns/";,
-                                 (unsigned char *)
-                                 GUPNP_DIDL_LITE_WRITER_NAMESPACE_PV);
-        xmlNewNs (priv->xml_node,
-                  (unsigned char *)
-                  "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/",
-                  NULL);
+        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);
 
         if (priv->language)
                 xmlSetProp (priv->xml_node,
diff --git a/libgupnp-av/xml-util.c b/libgupnp-av/xml-util.c
index 4543c74..a8de2c8 100644
--- a/libgupnp-av/xml-util.c
+++ b/libgupnp-av/xml-util.c
@@ -27,6 +27,23 @@
 
 #include "xml-util.h"
 
+typedef struct _GUPnPXMLNamespaceDescription
+{
+        char *uri;
+        char *prefix;
+} GUPnPXMLNamespaceDescription;
+
+
+static GUPnPXMLNamespaceDescription gupnp_xml_namespaces[] =
+{
+        { "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/", NULL },
+        { "http://purl.org/dc/elements/1.1/";, "dc" },
+        { "urn:schemas-dlna-org:metadata-1-0/", "dlna" },
+        { "http://www.pv.com/pvns/";, "pv" },
+        { "urn:schemas-upnp-org:metadata-1-0/upnp/", "upnp" },
+        NULL
+};
+
 xmlNode *
 xml_util_get_element (xmlNode *node,
                       ...)
@@ -411,3 +428,19 @@ xml_util_get_attributes_map (xmlNode *node)
 
         return attributes_map;
 }
+
+/**
+ * xml_util_create_namespace:
+ * @root: (allow-none): Document root node or %NULL for anonymous ns.
+ * @ns: Namespace
+ * @returns: Newly created namespace on root node
+ */
+xmlNsPtr
+xml_util_create_namespace (xmlNodePtr root, GUPnPXMLNamespace ns)
+{
+        g_return_val_if_fail (ns < GUPNP_XML_NAMESPACE_COUNT, NULL);
+
+        return xmlNewNs (root,
+                         (const xmlChar *) gupnp_xml_namespaces[ns].uri,
+                         (const xmlChar *) gupnp_xml_namespaces[ns].prefix);
+}
diff --git a/libgupnp-av/xml-util.h b/libgupnp-av/xml-util.h
index ffa5e50..8d34b4b 100644
--- a/libgupnp-av/xml-util.h
+++ b/libgupnp-av/xml-util.h
@@ -30,6 +30,16 @@
 #include <libxml/tree.h>
 #include <stdarg.h>
 
+typedef enum _GUPnPXMLNamespace
+{
+        GUPNP_XML_NAMESPACE_DIDL_LITE,
+        GUPNP_XML_NAMESPACE_DC,
+        GUPNP_XML_NAMESPACE_DLNA,
+        GUPNP_XML_NAMESPACE_PV,
+        GUPNP_XML_NAMESPACE_UPNP,
+        GUPNP_XML_NAMESPACE_COUNT
+} GUPnPXMLNamespace;
+
 G_BEGIN_DECLS
 
 /* Misc utilities for inspecting xmlNodes */
@@ -117,6 +127,10 @@ xml_util_copy_node                      (xmlNode *node);
 G_GNUC_INTERNAL GHashTable *
 xml_util_get_attributes_map             (xmlNode *node);
 
+G_GNUC_INTERNAL xmlNsPtr
+xml_util_create_namespace               (xmlNodePtr root,
+                                         GUPnPXMLNamespace ns);
+
 G_END_DECLS
 
 #endif /* __XML_UTIL_H__ */


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