[gnome-software] Remove the escape chars when unmunging the XML content



commit 700060e81095f292663eca69b94fc02b96563f98
Author: Richard Hughes <richard hughsie com>
Date:   Mon Oct 21 10:33:23 2013 +0100

    Remove the escape chars when unmunging the XML content

 src/plugins/appstream-common.c  |   86 +++++++++++++++++++++++++++++++++++++++
 src/plugins/appstream-common.h  |    2 +
 src/plugins/gs-plugin-appdata.c |   51 +++--------------------
 3 files changed, 95 insertions(+), 44 deletions(-)
---
diff --git a/src/plugins/appstream-common.c b/src/plugins/appstream-common.c
index 6e9f411..18ade72 100644
--- a/src/plugins/appstream-common.c
+++ b/src/plugins/appstream-common.c
@@ -160,3 +160,89 @@ appstream_get_locale_value (const gchar *lang)
 
        return G_MAXUINT;
 }
+
+/**
+ * appstream_string_replace:
+ */
+static guint
+appstream_string_replace (GString *string,
+                         const gchar *search,
+                         const gchar *replace)
+{
+       gchar *tmp;
+       guint count = 0;
+       guint replace_len = strlen (replace);
+       guint search_len = strlen (search);
+
+       do {
+               tmp = g_strstr_len (string->str, -1, search);
+               if (tmp == NULL)
+                       goto out;
+
+               /* reallocate the string if required */
+               if (search_len > replace_len) {
+                       g_string_erase (string,
+                                       tmp - string->str,
+                                       search_len - replace_len);
+               }
+               if (search_len < replace_len) {
+                       g_string_insert_len (string,
+                                           tmp - string->str,
+                                           search,
+                                           replace_len - search_len);
+               }
+
+               /* just memcmp in the new string */
+               memcpy (tmp, replace, replace_len);
+               count++;
+       } while (TRUE);
+out:
+       return count;
+}
+
+/**
+ * appstream_xml_unmunge:
+ */
+gchar *
+appstream_xml_unmunge (const gchar *text, gssize text_length)
+{
+       GString *str;
+       gboolean ignore_whitespace = TRUE;
+       guint i;
+
+       /* unknown length */
+       if (text_length == -1)
+               text_length = strlen (text);
+
+       /* ignore repeated whitespace */
+       str = g_string_sized_new (text_length);
+       for (i = 0; i < text_length; i++) {
+               if (text[i] == ' ' || text[i] == '\n') {
+                       if (!ignore_whitespace)
+                               g_string_append_c (str, ' ');
+                       ignore_whitespace = TRUE;
+               } else {
+                       g_string_append_c (str, text[i]);
+                       ignore_whitespace = FALSE;
+               }
+       }
+
+       /* nothing left */
+       if (str->len == 0) {
+               g_string_free (str, TRUE);
+               return NULL;
+       }
+
+       /* remove trailing space */
+       if (str->str[str->len - 1] == ' ')
+               g_string_truncate (str, str->len - 1);
+
+       /* remove escape chars */
+       appstream_string_replace (str, "&amp;", "&");
+       appstream_string_replace (str, "&lt;", "<");
+       appstream_string_replace (str, "&gt;", ">");
+       appstream_string_replace (str, "&#34;", "\"");
+       appstream_string_replace (str, "&#39;", "'");
+
+       return g_string_free (str, FALSE);
+}
diff --git a/src/plugins/appstream-common.h b/src/plugins/appstream-common.h
index 7dd9b17..51c68b3 100644
--- a/src/plugins/appstream-common.h
+++ b/src/plugins/appstream-common.h
@@ -57,6 +57,8 @@ typedef enum {
 AppstreamTag    appstream_tag_from_string      (const gchar    *element_name);
 const gchar    *appstream_tag_to_string        (AppstreamTag    tag);
 guint           appstream_get_locale_value     (const gchar    *lang);
+gchar          *appstream_xml_unmunge          (const gchar    *text,
+                                                gssize          text_length);
 
 G_END_DECLS
 
diff --git a/src/plugins/gs-plugin-appdata.c b/src/plugins/gs-plugin-appdata.c
index 7496bb6..f6fa380 100644
--- a/src/plugins/gs-plugin-appdata.c
+++ b/src/plugins/gs-plugin-appdata.c
@@ -315,43 +315,6 @@ appdata_parse_end_element_cb (GMarkupParseContext *context,
 }
 
 /**
- * appdata_xml_unmunge:
- */
-static gchar *
-appdata_xml_unmunge (const gchar *text, guint text_length)
-{
-       GString *str;
-       guint i;
-       gboolean ignore_whitespace = TRUE;
-
-       /* ignore repeated whitespace */
-       str = g_string_sized_new (text_length);
-       for (i = 0; i < text_length; i++) {
-               if (text[i] == ' ') {
-                       if (!ignore_whitespace)
-                               g_string_append_c (str, ' ');
-                       ignore_whitespace = TRUE;
-               } else if (text[i] == '\n') {
-                       continue;
-               } else {
-                       g_string_append_c (str, text[i]);
-                       ignore_whitespace = FALSE;
-               }
-       }
-
-       /* nothing left */
-       if (str->len == 0) {
-               g_string_free (str, TRUE);
-               return NULL;
-       }
-
-       /* remove trailing space */
-       if (str->str[str->len - 1] == ' ')
-               g_string_truncate (str, str->len - 1);
-       return g_string_free (str, FALSE);
-}
-
-/**
  * appdata_parse_text_cb:
  */
 static void
@@ -378,7 +341,7 @@ appdata_parse_text_cb (GMarkupParseContext *context,
                /* ignore */
                break;
        case APPSTREAM_TAG_DESCRIPTION:
-               tmp = appdata_xml_unmunge (text, text_len);
+               tmp = appstream_xml_unmunge (text, text_len);
                if (tmp == NULL)
                        break;
                appstream_description_build (helper,
@@ -387,13 +350,13 @@ appdata_parse_text_cb (GMarkupParseContext *context,
                break;
        case APPSTREAM_TAG_SCREENSHOT:
                /* FIXME: actually add to API */
-               //tmp = appdata_xml_unmunge (text, text_len);
+               //tmp = appstream_xml_unmunge (text, text_len);
                //gs_app_add_screenshot (helper->app, tmp);
                break;
        case APPSTREAM_TAG_NAME:
                // FIXME: does not get best language
                if (gs_app_get_name (helper->app) == NULL) {
-                       tmp = appdata_xml_unmunge (text, text_len);
+                       tmp = appstream_xml_unmunge (text, text_len);
                        if (tmp == NULL)
                                break;
                        g_debug ("AppData: Setting name: %s", tmp);
@@ -403,7 +366,7 @@ appdata_parse_text_cb (GMarkupParseContext *context,
        case APPSTREAM_TAG_SUMMARY:
                // FIXME: does not get best language
                if (gs_app_get_summary (helper->app) == NULL) {
-                       tmp = appdata_xml_unmunge (text, text_len);
+                       tmp = appstream_xml_unmunge (text, text_len);
                        if (tmp == NULL)
                                break;
                        g_debug ("AppData: Setting summary: %s", tmp);
@@ -412,7 +375,7 @@ appdata_parse_text_cb (GMarkupParseContext *context,
                break;
        case APPSTREAM_TAG_URL:
                if (gs_app_get_url (helper->app, GS_APP_URL_KIND_HOMEPAGE) == NULL) {
-                       tmp = appdata_xml_unmunge (text, text_len);
+                       tmp = appstream_xml_unmunge (text, text_len);
                        if (tmp == NULL)
                                break;
                        g_debug ("AppData: Setting URL: %s", tmp);
@@ -421,7 +384,7 @@ appdata_parse_text_cb (GMarkupParseContext *context,
                break;
        case APPSTREAM_TAG_PROJECT_GROUP:
                if (gs_app_get_project_group (helper->app) == NULL) {
-                       tmp = appdata_xml_unmunge (text, text_len);
+                       tmp = appstream_xml_unmunge (text, text_len);
                        if (tmp == NULL)
                                break;
                        g_debug ("AppData: Setting project-group: %s", tmp);
@@ -429,7 +392,7 @@ appdata_parse_text_cb (GMarkupParseContext *context,
                }
                break;
        default:
-               tmp = appdata_xml_unmunge (text, text_len);
+               tmp = appstream_xml_unmunge (text, text_len);
                if (tmp == NULL)
                        break;
                g_warning ("AppData: unknown data '%s' is '%s'",


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