From 655de6ecfa71d341e8782004f352de06fb6e3bca Mon Sep 17 00:00:00 2001 From: Jens Georg Date: Sat, 14 Dec 2013 14:21:08 +0100 Subject: Add utility function to lazy-create ns https://bugzilla.gnome.org/show_bug.cgi?id=705564 --- libgupnp-av/xml-util.c | 31 +++++++++++++++++++++++++++++++ libgupnp-av/xml-util.h | 6 ++++++ 2 files changed, 37 insertions(+) diff --git a/libgupnp-av/xml-util.c b/libgupnp-av/xml-util.c index dd32089..cf79a02 100644 --- a/libgupnp-av/xml-util.c +++ b/libgupnp-av/xml-util.c @@ -494,3 +494,34 @@ xml_util_lookup_namespace (xmlDocPtr doc, GUPnPXMLNamespace ns) return retval; } + +/** + * xml_util_get_ns: + * @doc: A #xmlDoc. + * @ns: A #GUPnPXMLNamespace. + * @ns_out: (out) (allow-none): return location for the namespace or %NULL. + * + * Lazy-create a XML namespace on @doc. + * + * If @ns_out is non-%NULL, the function will return @ns_out immediately. + * @returns: either the existing #xmlNsPtr or a newly created one. + */ +xmlNsPtr +xml_util_get_ns (xmlDocPtr doc, GUPnPXMLNamespace ns, xmlNsPtr *ns_out) +{ + xmlNsPtr tmp_ns; + + /* User supplied namespace, just return that */ + if (ns_out != NULL && *ns_out != NULL) + return *ns_out; + + tmp_ns = xml_util_lookup_namespace (doc, ns); + if (!tmp_ns) + tmp_ns = xml_util_create_namespace (xmlDocGetRootElement (doc), + ns); + + if (ns_out != NULL) + *ns_out = tmp_ns; + + return tmp_ns; +} diff --git a/libgupnp-av/xml-util.h b/libgupnp-av/xml-util.h index e08fa19..064dfe8 100644 --- a/libgupnp-av/xml-util.h +++ b/libgupnp-av/xml-util.h @@ -134,6 +134,12 @@ xml_util_create_namespace (xmlNodePtr root, G_GNUC_INTERNAL xmlNsPtr xml_util_lookup_namespace (xmlDocPtr doc, GUPnPXMLNamespace ns); + +G_GNUC_INTERNAL xmlNsPtr +xml_util_get_ns (xmlDocPtr doc, + GUPnPXMLNamespace ns, + xmlNsPtr *ns_out); + G_END_DECLS #endif /* __XML_UTIL_H__ */ -- cgit v1.2.1