[gupnp-av/wip/lazy-namespaces: 3/7] Add utility function to find namespaces



commit 527fdbfbd477508db0702c116ca0fc930ee772aa
Author: Jens Georg <mail jensge org>
Date:   Sat Dec 14 14:02:50 2013 +0100

    Add utility function to find namespaces

 libgupnp-av/gupnp-didl-lite-parser.c |   38 +++++---------------------
 libgupnp-av/xml-util.c               |   50 ++++++++++++++++++++++++++++++++++
 libgupnp-av/xml-util.h               |    3 ++
 3 files changed, 60 insertions(+), 31 deletions(-)
---
diff --git a/libgupnp-av/gupnp-didl-lite-parser.c b/libgupnp-av/gupnp-didl-lite-parser.c
index 8f31605..c11340e 100644
--- a/libgupnp-av/gupnp-didl-lite-parser.c
+++ b/libgupnp-av/gupnp-didl-lite-parser.c
@@ -272,46 +272,22 @@ gupnp_didl_lite_parser_parse_didl_recursive (GUPnPDIDLLiteParser *parser,
                 return FALSE;
         }
 
-        /* Lookup UPnP and DC namespaces */
-        ns_list = xmlGetNsList (doc,
-                                xmlDocGetRootElement (doc));
-
-        if (ns_list) {
-                short i;
-
-                for (i = 0; ns_list[i] != NULL; i++) {
-                        const char *prefix = (const char *) ns_list[i]->prefix;
-
-                        if (prefix == NULL)
-                                continue;
-
-                        if (! upnp_ns &&
-                            g_ascii_strcasecmp (prefix, "upnp") == 0)
-                                upnp_ns = ns_list[i];
-                        else if (! dc_ns &&
-                                 g_ascii_strcasecmp (prefix, "dc") == 0)
-                                dc_ns = ns_list[i];
-                        else if (! dlna_ns &&
-                                 g_ascii_strcasecmp (prefix, "dlna") == 0)
-                                dlna_ns = ns_list[i];
-                        else if (! pv_ns &&
-                                g_ascii_strcasecmp (prefix, "pv") == 0)
-                                pv_ns = ns_list[i];
-                }
-
-                xmlFree (ns_list);
-        }
-
-        /* Create UPnP and DC namespaces if they don't exist */
+        /* Create namespaces if they don't exist */
+        upnp_ns = xml_util_lookup_namespace (doc, GUPNP_XML_NAMESPACE_UPNP);
         if (! upnp_ns)
                 upnp_ns = xml_util_create_namespace (xmlDocGetRootElement (doc),
                                                      GUPNP_XML_NAMESPACE_UPNP);
+
+        dc_ns = xml_util_lookup_namespace (doc, GUPNP_XML_NAMESPACE_DC);
         if (! dc_ns)
                 dc_ns = xml_util_create_namespace (xmlDocGetRootElement (doc),
                                                    GUPNP_XML_NAMESPACE_DC);
+        dlna_ns = xml_util_lookup_namespace (doc, GUPNP_XML_NAMESPACE_DLNA);
         if (! dlna_ns)
                 dlna_ns = xml_util_create_namespace (xmlDocGetRootElement (doc),
                                                    GUPNP_XML_NAMESPACE_DLNA);
+
+        pv_ns = xml_util_lookup_namespace (doc, GUPNP_XML_NAMESPACE_PV);
         if (! pv_ns)
                 pv_ns = xml_util_create_namespace (xmlDocGetRootElement (doc),
                                                    GUPNP_XML_NAMESPACE_PV);
diff --git a/libgupnp-av/xml-util.c b/libgupnp-av/xml-util.c
index a8de2c8..dd32089 100644
--- a/libgupnp-av/xml-util.c
+++ b/libgupnp-av/xml-util.c
@@ -444,3 +444,53 @@ xml_util_create_namespace (xmlNodePtr root, GUPnPXMLNamespace ns)
                          (const xmlChar *) gupnp_xml_namespaces[ns].uri,
                          (const xmlChar *) gupnp_xml_namespaces[ns].prefix);
 }
+
+/**
+ * xml_util_lookup_namespace:
+ * @doc: #xmlDoc
+ * @ns: namespace to look up (except DIDL-Lite, which doesn't have a prefix)
+ * @returns: %NULL if namespace does not exist, a pointer to the namespace
+ * otherwise.
+ */
+xmlNsPtr
+xml_util_lookup_namespace (xmlDocPtr doc, GUPnPXMLNamespace ns)
+{
+        xmlNsPtr *ns_list, *it, retval = NULL;
+        const char *ns_prefix = NULL;
+        const char *ns_uri = NULL;
+
+        g_return_val_if_fail (ns < GUPNP_XML_NAMESPACE_COUNT, NULL);
+
+        ns_prefix = gupnp_xml_namespaces[ns].prefix;
+        ns_uri = gupnp_xml_namespaces[ns].uri;
+
+        ns_list = xmlGetNsList (doc, xmlDocGetRootElement (doc));
+        if (ns_list == NULL)
+                return NULL;
+
+        for (it = ns_list; *it != NULL; it++) {
+                const char *it_prefix = (const char *) (*it)->prefix;
+                const char *it_uri = (const char *) (*it)->href;
+
+                if (it_prefix == NULL) {
+                        if (ns_prefix != NULL)
+                                continue;
+
+                        if (g_ascii_strcasecmp (it_uri, ns_uri) == 0) {
+                                retval = *it;
+                                break;
+                        }
+
+                        continue;
+                }
+
+                if (g_ascii_strcasecmp (it_prefix, ns_prefix) == 0) {
+                        retval = *it;
+                        break;
+                }
+        }
+
+        xmlFree (ns_list);
+
+        return retval;
+}
diff --git a/libgupnp-av/xml-util.h b/libgupnp-av/xml-util.h
index 8d34b4b..e08fa19 100644
--- a/libgupnp-av/xml-util.h
+++ b/libgupnp-av/xml-util.h
@@ -131,6 +131,9 @@ G_GNUC_INTERNAL xmlNsPtr
 xml_util_create_namespace               (xmlNodePtr root,
                                          GUPnPXMLNamespace ns);
 
+G_GNUC_INTERNAL xmlNsPtr
+xml_util_lookup_namespace               (xmlDocPtr doc,
+                                         GUPnPXMLNamespace ns);
 G_END_DECLS
 
 #endif /* __XML_UTIL_H__ */


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