summaryrefslogtreecommitdiff
path: root/libappstream-glib/as-node.c
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2014-06-24 10:41:55 +0100
committerRichard Hughes <richard@hughsie.com>2014-06-24 10:41:55 +0100
commit778f968d2827fd53dd2f2bbe21b287be161f929e (patch)
tree3311db47715911d763407eacc570d0e556e7f0fc /libappstream-glib/as-node.c
parenta5446f0b94ccaa556774df558e4af1a6f2735425 (diff)
downloadappstream-glib-778f968d2827fd53dd2f2bbe21b287be161f929e.tar.gz
Add as_node_find_with_attribute()
This allows us to find a tag with a specific attribute.
Diffstat (limited to 'libappstream-glib/as-node.c')
-rw-r--r--libappstream-glib/as-node.c56
1 files changed, 52 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);