diff options
author | Richard Hughes <richard@hughsie.com> | 2014-06-24 10:41:55 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2014-06-24 10:41:55 +0100 |
commit | 778f968d2827fd53dd2f2bbe21b287be161f929e (patch) | |
tree | 3311db47715911d763407eacc570d0e556e7f0fc | |
parent | a5446f0b94ccaa556774df558e4af1a6f2735425 (diff) | |
download | appstream-glib-778f968d2827fd53dd2f2bbe21b287be161f929e.tar.gz |
Add as_node_find_with_attribute()
This allows us to find a tag with a specific attribute.
-rw-r--r-- | libappstream-glib/as-node.c | 56 | ||||
-rw-r--r-- | libappstream-glib/as-node.h | 5 | ||||
-rw-r--r-- | libappstream-glib/as-self-test.c | 5 |
3 files changed, 62 insertions, 4 deletions
diff --git a/libappstream-glib/as-node.c b/libappstream-glib/as-node.c index b1a4f41..25ce864 100644 --- a/libappstream-glib/as-node.c +++ b/libappstream-glib/as-node.c @@ -823,7 +823,8 @@ as_node_from_file (GFile *file, * as_node_get_child_node: **/ static GNode * -as_node_get_child_node (const GNode *root, const gchar *name) +as_node_get_child_node (const GNode *root, const gchar *name, + const gchar *attr_key, const gchar *attr_value) { AsNodeData *data; GNode *node; @@ -837,8 +838,15 @@ as_node_get_child_node (const GNode *root, const gchar *name) data = node->data; if (data == NULL) return NULL; - if (g_strcmp0 (as_tag_data_get_name (data), name) == 0) + if (g_strcmp0 (as_tag_data_get_name (data), name) == 0) { + if (attr_key != NULL) { + if (g_strcmp0 (as_node_get_attribute (node, attr_key), + attr_value) != 0) { + continue; + } + } return node; + } } return NULL; } @@ -1200,7 +1208,7 @@ as_node_find (GNode *root, const gchar *path) split = g_strsplit (path, "/", -1); for (i = 0; split[i] != NULL; i++) { - node = as_node_get_child_node (node, split[i]); + node = as_node_get_child_node (node, split[i], NULL, NULL); if (node == NULL) return NULL; } @@ -1208,6 +1216,46 @@ as_node_find (GNode *root, const gchar *path) } /** + * as_node_find_with_attribute: (skip) + * @root: a root node, or %NULL + * @path: a path in the DOM, e.g. "html/body" + * @attr_key: the attribute key + * @attr_value: the attribute value + * + * Gets a node from the DOM tree with a specified attribute. + * + * Return value: A #GNode, or %NULL if not found + * + * Since: 0.1.0 + **/ +GNode * +as_node_find_with_attribute (GNode *root, const gchar *path, + const gchar *attr_key, const gchar *attr_value) +{ + GNode *node = root; + guint i; + _cleanup_strv_free_ gchar **split = NULL; + + g_return_val_if_fail (path != NULL, NULL); + + split = g_strsplit (path, "/", -1); + for (i = 0; split[i] != NULL; i++) { + /* only check the last element */ + if (split[i + 1] == NULL) { + node = as_node_get_child_node (node, split[i], + attr_key, attr_value); + if (node == NULL) + return NULL; + } else { + node = as_node_get_child_node (node, split[i], NULL, NULL); + if (node == NULL) + return NULL; + } + } + return node; +} + +/** * as_node_insert: (skip) * @parent: a parent #GNode. * @name: the tag name, e.g. "id". @@ -1402,7 +1450,7 @@ as_node_get_localized (const GNode *node, const gchar *key) GNode *tmp; /* does it exist? */ - tmp = as_node_get_child_node (node, key); + tmp = as_node_get_child_node (node, key, NULL, NULL); if (tmp == NULL) return NULL; data_unlocalized = as_node_get_data (tmp); diff --git a/libappstream-glib/as-node.h b/libappstream-glib/as-node.h index c4ced19..2074a46 100644 --- a/libappstream-glib/as-node.h +++ b/libappstream-glib/as-node.h @@ -163,6 +163,11 @@ gboolean as_node_to_file (const GNode *root, GNode *as_node_find (GNode *root, const gchar *path) G_GNUC_WARN_UNUSED_RESULT; +GNode *as_node_find_with_attribute (GNode *root, + const gchar *path, + const gchar *attr_key, + const gchar *attr_value) + G_GNUC_WARN_UNUSED_RESULT; GNode *as_node_insert (GNode *parent, const gchar *name, diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index f8f949a..57f25a2 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -1148,6 +1148,11 @@ as_test_node_localized_wrap2_func (void) "<p>Hi</p><ul><li>First</li><li>Second</li></ul>"); g_assert_cmpstr (g_hash_table_lookup (hash, "pl"), ==, "<p>Czesc</p><ul><li>Pierwszy</li><li>Secondski</li></ul>"); + + /* find the Polish first paragraph */ + n1 = as_node_find_with_attribute (root, "description/p", "xml:lang", "pl"); + g_assert (n1 != NULL); + g_assert_cmpstr (as_node_get_data (n1), ==, "Czesc"); } static void |