[Libosinfo] [RFC PATCH 5/5] osinfo_loader: do not use xpath to read localized strings

Daniel P. Berrange berrange at redhat.com
Tue Jun 3 14:22:05 UTC 2014


On Tue, Jun 03, 2014 at 04:08:12PM +0200, Giuseppe Scrivano wrote:
> Signed-off-by: Giuseppe Scrivano <gscrivan at redhat.com>
> ---
>  osinfo/osinfo_loader.c | 18 +++++-------------
>  1 file changed, 5 insertions(+), 13 deletions(-)
> 
> diff --git a/osinfo/osinfo_loader.c b/osinfo/osinfo_loader.c
> index 2a7d748..bcd1b1d 100644
> --- a/osinfo/osinfo_loader.c
> +++ b/osinfo/osinfo_loader.c
> @@ -297,32 +297,24 @@ static void osinfo_loader_entity(OsinfoLoader *loader,
>                                   GError **err)
>  {
>      int i = 0;
> -    const gchar * const *langs = g_get_language_names ();
>  
>      /* Standard well-known keys first, allow single value only */
>      for (i = 0 ; keys != NULL && keys[i].name != NULL; i++) {
>          gchar *value_str = NULL;
>          gboolean value_bool = FALSE;
>          gchar *xpath = NULL;
> -        int j;
>          xmlXPathCompExprPtr comp;
>  
>          /* We are guaranteed to have at least the default "C" locale and we
>           * want to ignore that, hence the NULL check on index 'j + 1'.
>           */
>          if (keys[i].type == G_TYPE_STRING) {
> -            for (j = 0; langs[j + 1] != NULL; j++) {
> -                xpath = g_strdup_printf("string(./%s[lang('%s')])",
> -                                        keys[i].name, langs[j]);

So given an XML doc

    <foo>eek</foo>
    <foo xml:lang='fr'>bar</foo>
    <foo xml:lang='pl'>wizz</foo>

an expression   './foo[lang('fr')]' will return the contents of
the <foo> element which has  xml:lang == 'fr'.

> -                comp = osinfo_loader_get_comp_xpath(loader, xpath);
> -                value_str = osinfo_loader_string(comp, ctxt, err);
> -                g_free(xpath);
> -                xpath = NULL;
> -                if (error_is_set(err))
> -                    return;
> -
> -                if (value_str != NULL)
> +            xmlNodePtr it;
> +            for (it = root->children; it; it = it->next) {
> +                if (xmlStrEqual(it->name, BAD_CAST keys[i].name)) {
> +                    value_str = g_strdup(gettext((const char *) it->children->content));

This meanwhile is matching on any <foo> ignoring the desired
'langs[j]' value. So you need to check the  'xml:lang' attribute
value too


Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|




More information about the Libosinfo mailing list