[Xfce4-commits] <xfce4-weather-plugin:master> Refactor parsing of XML documents.

Harald Judt noreply at xfce.org
Tue Nov 20 20:18:03 CET 2012


Updating branch refs/heads/master
         to 1445bcd83c4eaf795447f30c16379d439d31796e (commit)
       from e4d8aa09938564c25832128b7a1f34ffc9ad6074 (commit)

commit 1445bcd83c4eaf795447f30c16379d439d31796e
Author: Harald Judt <h.judt at gmx.at>
Date:   Tue Nov 20 20:00:49 2012 +0100

    Refactor parsing of XML documents.

 panel-plugin/weather-parsers.c |   28 ++++++++++++++++++++++++++++
 panel-plugin/weather-parsers.h |    6 ++++++
 2 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/panel-plugin/weather-parsers.c b/panel-plugin/weather-parsers.c
index 8206ce2..610d935 100644
--- a/panel-plugin/weather-parsers.c
+++ b/panel-plugin/weather-parsers.c
@@ -439,6 +439,34 @@ parse_place(xmlNode *cur_node)
 }
 
 
+gpointer
+parse_xml_document(SoupMessage *msg,
+                   XmlParseFunc parse_func)
+{
+    xmlDoc *doc;
+    xmlNode *root_node;
+    gpointer user_data = NULL;
+
+    if (G_LIKELY(msg && msg->response_body && msg->response_body->data)) {
+        if (g_utf8_validate(msg->response_body->data, -1, NULL)) {
+            /* force parsing as UTF-8, the XML encoding header may lie */
+            doc = xmlReadMemory(msg->response_body->data,
+                                strlen(msg->response_body->data),
+                                NULL, "UTF-8", 0);
+        } else
+            doc = xmlParseMemory(msg->response_body->data,
+                                 strlen(msg->response_body->data));
+        if (G_LIKELY(doc)) {
+            root_node = xmlDocGetRootElement(doc);
+            if (G_LIKELY(root_node))
+                user_data = parse_func(root_node);
+            xmlFreeDoc(doc);
+        }
+    }
+    return user_data;
+}
+
+
 static void
 xml_location_free(xml_location *loc)
 {
diff --git a/panel-plugin/weather-parsers.h b/panel-plugin/weather-parsers.h
index 2c58e72..bc4bfad 100644
--- a/panel-plugin/weather-parsers.h
+++ b/panel-plugin/weather-parsers.h
@@ -22,6 +22,7 @@
 #include <glib.h>
 #include <gtk/gtk.h>
 #include <libxml/parser.h>
+#include <libsoup/soup.h>
 
 G_BEGIN_DECLS
 
@@ -35,6 +36,8 @@ enum {
     CLOUDS_PERC_NUM
 };
 
+typedef gpointer (*XmlParseFunc) (xmlNode *node);
+
 typedef struct {
     gchar *altitude;
     gchar *latitude;
@@ -119,6 +122,9 @@ xml_time *get_timeslice(xml_weather *data,
                         const time_t start_t,
                         const time_t end_t);
 
+gpointer parse_xml_document(SoupMessage *msg,
+                            XmlParseFunc parse_func);
+
 void xml_time_free(xml_time *timeslice);
 
 void xml_weather_free(xml_weather *data);


More information about the Xfce4-commits mailing list