From 87b8d3d415be5a53eaf18318d6ac83758784fcdf Mon Sep 17 00:00:00 2001 From: Guilhem Bonnefille Date: Fri, 1 Jan 2010 22:13:23 +0100 Subject: [PATCH] Add XPATH related attribute support It is now possible to set both path and attribute name in a single, self explanatory, string: /path/to/element@attribute --- src/vikgotoxmltool.c | 48 ++++++++++++++++++++++++++++++++++++++++------ test/test_vikgotoxmltool.c | 7 +++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/vikgotoxmltool.c b/src/vikgotoxmltool.c index 07800a46..64178c19 100644 --- a/src/vikgotoxmltool.c +++ b/src/vikgotoxmltool.c @@ -107,6 +107,7 @@ _goto_xml_tool_set_property (GObject *object, { VikGotoXmlTool *self = VIK_GOTO_XML_TOOL (object); VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self); + gchar **splitted = NULL; switch (property_id) { @@ -116,23 +117,53 @@ _goto_xml_tool_set_property (GObject *object, break; case PROP_LAT_PATH: + splitted = g_strsplit (g_value_get_string (value), "@", 2); g_free (priv->lat_path); - priv->lat_path = g_value_dup_string (value); + priv->lat_path = splitted[0]; + if (splitted[1]) + { + g_object_set (object, "lat-attr", splitted[1], NULL); + g_free (splitted[1]); + } + /* only free the tab, not the strings */ + g_free (splitted); + splitted = NULL; break; case PROP_LAT_ATTR: - g_free (priv->lat_attr); - priv->lat_attr = g_value_dup_string (value); + /* Avoid to overwrite XPATH value */ + /* NB: This disable future overwriting, + but as property is CONSTRUCT_ONLY there is no matter */ + if (!priv->lat_attr || g_value_get_string (value)) + { + g_free (priv->lat_attr); + priv->lat_attr = g_value_dup_string (value); + } break; case PROP_LON_PATH: + splitted = g_strsplit (g_value_get_string (value), "@", 2); g_free (priv->lon_path); - priv->lon_path = g_value_dup_string (value); + priv->lon_path = splitted[0]; + if (splitted[1]) + { + g_object_set (object, "lon-attr", splitted[1], NULL); + g_free (splitted[1]); + } + /* only free the tab, not the strings */ + g_free (splitted); + splitted = NULL; break; case PROP_LON_ATTR: - g_free (priv->lon_attr); - priv->lon_attr = g_value_dup_string (value); + /* Avoid to overwrite XPATH value */ + /* NB: This disable future overwriting, + but as property is CONSTRUCT_ONLY there is no matter */ + if (!priv->lon_attr || g_value_get_string (value)) + { + g_free (priv->lon_attr); + priv->lon_attr = g_value_dup_string (value); + } break; default: @@ -372,6 +403,11 @@ _goto_xml_tool_parse_file_for_latlon(VikGotoTool *self, gchar *filename, struct VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self); g_return_val_if_fail(priv != NULL, FALSE); + g_debug ("%s: %s@%s, %s@%s", + __FUNCTION__, + priv->lat_path, priv->lat_attr, + priv->lon_path, priv->lon_attr); + FILE *file = g_fopen (filename, "r"); if (file == NULL) /* TODO emit warning */ diff --git a/test/test_vikgotoxmltool.c b/test/test_vikgotoxmltool.c index 0673eb3f..ab050357 100644 --- a/test/test_vikgotoxmltool.c +++ b/test/test_vikgotoxmltool.c @@ -29,11 +29,18 @@ int main(int argc, char *argv[]) "lon-path", "/geonames/geoname", "lon-attr", "lng", NULL ) ); + + VikGotoXmlTool *with_xpath = VIK_GOTO_XML_TOOL ( g_object_new ( VIK_GOTO_XML_TOOL_TYPE, "label", "OSM", + "url-format", "http://ws.geonames.org/search?q=%s&maxRows=1&lang=es&style=short", + "lat-path", "/geonames/geoname@lat", + "lon-path", "/geonames/geoname@lng", + NULL ) ); int i; for (i = 1; i