Ejemplo n.º 1
0
static gboolean
ide_doap_parse_maintainer (IdeDoap   *self,
                           XmlReader *reader)
{
  g_assert (IDE_IS_DOAP (self));
  g_assert (XML_IS_READER (reader));

  if (!xml_reader_read (reader))
    return FALSE;

  do
    {
      if (xml_reader_is_a_local (reader, "Person") && xml_reader_read (reader))
        {
          g_autoptr(IdeDoapPerson) person = ide_doap_person_new ();

          do
            {
              if (xml_reader_is_a_local (reader, "name"))
                {
                  ide_doap_person_set_name (person, xml_reader_read_string (reader));
                }
              else if (xml_reader_is_a_local (reader, "mbox"))
                {
                  gchar *str;

                  str = xml_reader_get_attribute (reader, "rdf:resource");
                  if (str != NULL && str[0] != '\0' && g_str_has_prefix (str, "mailto:"))
                    ide_doap_person_set_email (person, str + strlen ("mailto:"));
                  g_free (str);
                }
            }
          while (xml_reader_read_to_next (reader));

          if (ide_doap_person_get_name (person) || ide_doap_person_get_email (person))
            self->maintainers = g_list_append (self->maintainers, g_object_ref (person));
        }
    }
  while (xml_reader_read_to_next (reader));

  return TRUE;
}
Ejemplo n.º 2
0
static gboolean
load_doap (IdeDoap       *self,
           XmlReader     *reader,
           GError       **error)
{
  if (!xml_reader_read_start_element (reader, "Project"))
    {
      g_set_error (error,
                   IDE_DOAP_ERROR,
                   IDE_DOAP_ERROR_INVALID_FORMAT,
                   "Project element is missing from doap.");
      return FALSE;
    }

  g_object_freeze_notify (G_OBJECT (self));

  xml_reader_read (reader);

  do
    {
      const gchar *element_name;

      element_name = xml_reader_get_local_name (reader);

      if (g_strcmp0 (element_name, "name") == 0 ||
          g_strcmp0 (element_name, "shortdesc") == 0 ||
          g_strcmp0 (element_name, "description") == 0)
        {
          gchar *str;

          str = xml_reader_read_string (reader);
          if (str != NULL)
            g_object_set (self, element_name, g_strstrip (str), NULL);
          g_free (str);
        }
      else if (g_strcmp0 (element_name, "category") == 0 ||
               g_strcmp0 (element_name, "homepage") == 0 ||
               g_strcmp0 (element_name, "download-page") == 0 ||
               g_strcmp0 (element_name, "bug-database") == 0)
        {
          gchar *str;

          str = xml_reader_get_attribute (reader, "rdf:resource");
          if (str != NULL)
            g_object_set (self, element_name, g_strstrip (str), NULL);
          g_free (str);
        }
      else if (g_strcmp0 (element_name, "programming-language") == 0)
        {
          gchar *str;

          str = xml_reader_read_string (reader);
          if (str != NULL && str[0] != '\0')
            ide_doap_add_language (self, g_strstrip (str));
          g_free (str);
        }
      else if (g_strcmp0 (element_name, "maintainer") == 0)
        {
          if (!ide_doap_parse_maintainer (self, reader))
            break;
        }
    }
  while (xml_reader_read_to_next (reader));

  g_object_thaw_notify (G_OBJECT (self));

  return TRUE;
}
Ejemplo n.º 3
0
gboolean
ide_doap_load_from_file (IdeDoap       *self,
                         GFile         *file,
                         GCancellable  *cancellable,
                         GError       **error)
{
  g_autoptr(XmlReader) reader = NULL;

  g_return_val_if_fail (IDE_IS_DOAP (self), FALSE);
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
  g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);

  reader = xml_reader_new ();

  if (!xml_reader_load_from_file (reader, file, cancellable, error))
    return FALSE;

  if (!xml_reader_read_start_element (reader, "Project"))
    {
      g_set_error (error,
                   IDE_DOAP_ERROR,
                   IDE_DOAP_ERROR_INVALID_FORMAT,
                   "Project element is missing from doap.");
      return FALSE;
    }

  g_object_freeze_notify (G_OBJECT (self));

  xml_reader_read (reader);

  do
    {
      const gchar *element_name;

      element_name = xml_reader_get_local_name (reader);

      if (ide_str_equal0 (element_name, "name") ||
          ide_str_equal0 (element_name, "shortdesc") ||
          ide_str_equal0 (element_name, "description"))
        {
          gchar *str;

          str = xml_reader_read_string (reader);
          if (str != NULL)
            g_object_set (self, element_name, g_strstrip (str), NULL);
          g_free (str);
        }
      else if (ide_str_equal0 (element_name, "category") ||
               ide_str_equal0 (element_name, "homepage") ||
               ide_str_equal0 (element_name, "download-page") ||
               ide_str_equal0 (element_name, "bug-database"))
        {
          gchar *str;

          str = xml_reader_get_attribute (reader, "rdf:resource");
          if (str != NULL)
            g_object_set (self, element_name, g_strstrip (str), NULL);
          g_free (str);
        }
      else if (ide_str_equal0 (element_name, "programming-language"))
        {
          gchar *str;

          str = xml_reader_read_string (reader);
          if (!ide_str_empty0 (str))
            ide_doap_add_language (self, g_strstrip (str));
          g_free (str);
        }
      else if (ide_str_equal0 (element_name, "maintainer"))
        {
          if (!ide_doap_parse_maintainer (self, reader))
            break;
        }
    }
  while (xml_reader_read_to_next (reader));

  g_object_thaw_notify (G_OBJECT (self));

  return TRUE;
}
Ejemplo n.º 4
0
static NewsPerson *
news_format_rss_read_person (NewsFormatRss *rss,
                             XmlReader      *reader,
                             GHashTable     *persons)
{
   GomAdapter *adapter;
   NewsPerson *person;
   gboolean valid = FALSE;
   gchar *str;

   ENTRY;

   g_assert(NEWS_IS_FORMAT_RSS(rss));
   g_assert(XML_IS_READER(reader));

   adapter = news_format_get_adapter(NEWS_FORMAT(rss));
   person = g_object_new(NEWS_TYPE_PERSON,
                         "adapter", adapter,
                         "is-new", TRUE,
                         NULL);

   if (!xml_reader_is_empty_element(reader)) {
      xml_reader_read(reader);

      for (;
           xml_reader_get_node_type(reader) != XML_READER_TYPE_END_ELEMENT;
           xml_reader_read_to_next(reader)) {
         if (rss->priv->error) {
            break;
         }
         if (xml_reader_get_node_type(reader) == XML_READER_TYPE_ELEMENT) {
            if (xml_reader_is_a_local(reader, "email")) {
               str = xml_reader_read_string(reader);
               valid = valid || (str && str[0]);
               g_object_set(person, "email", str, NULL);
               g_free(str);
            }
         }
      }
   }

   if (!valid) {
      g_object_unref(person);
      RETURN(NULL);
   }

   {
      GomResource *resource;
      gchar *email;
      gchar *name;
      gchar *key;

      g_object_get(person,
                   "name", &name,
                "email", &email,
                NULL);
      key = g_strdup_printf("%s|%s", name, email);
      if ((resource = g_hash_table_lookup(persons, key))) {
         g_object_unref(person);
         person = g_object_ref(resource);
      } else {
         g_hash_table_insert(persons, g_strdup(key),
                             g_object_ref(person));
      }
      g_free(name);
      g_free(email);
      g_free(key);
   }

   RETURN(person);
}
Ejemplo n.º 5
0
static gboolean
news_format_rss_read_from_xml (NewsFormatRss  *rss,
                        XmlReader      *reader,
                        NewsFeed       *feed,
                        GError        **error)
{
   GHashTable *categories = NULL;
   GHashTable *persons = NULL;
   GHashTable *links = NULL;
   GomAdapter *adapter;
   gchar *str;

   ENTRY;

   g_return_val_if_fail(NEWS_IS_FORMAT_RSS(rss), FALSE);
   g_return_val_if_fail(XML_IS_READER(reader), FALSE);
   g_return_val_if_fail(NEWS_IS_FEED(feed), FALSE);

   adapter = news_format_get_adapter(NEWS_FORMAT(rss));
   g_assert(adapter);

skip:
   if (!xml_reader_read_to_next(reader) ||
       !xml_reader_is_a(reader, "rss")) {
      if (xml_reader_is_a(reader, "xml-stylesheet") ||
          xml_reader_is_a(reader, "#comment")) {
         goto skip;
      }
      RETURN_ERROR;
   }

   if (!xml_reader_is_a_local(reader, "rss")) {
      RETURN_ERROR;
   }

   if ((str = xml_reader_get_attribute(reader, "version"))) {
      if (!!g_strcmp0(str, "2.0")) {
         g_set_error(error, XML_READER_ERROR, XML_READER_ERROR_INVALID,
                  _("RSS Version \"%s\" is not supported."), str);
         g_free(str);
         RETURN(FALSE);
      }
      g_free(str);
   }

   if (!xml_reader_read_start_element(reader, "channel")) {
      RETURN_ERROR;
   }

   persons = g_hash_table_new_full(g_str_hash, g_str_equal,
                                   g_free, g_object_unref);
   categories = g_hash_table_new_full(g_str_hash, g_str_equal,
                                      g_free, g_object_unref);
   links = g_hash_table_new_full(g_str_hash, g_str_equal,
                                 g_free, g_object_unref);

   xml_reader_read(reader);

   for (xml_reader_read_to_next(reader);
        xml_reader_get_node_type(reader) != XML_READER_TYPE_END_ELEMENT;
        xml_reader_read_to_next(reader)) {

      if ((xml_reader_get_node_type(reader) != XML_READER_TYPE_ELEMENT) || rss->priv->error) {
         g_hash_table_destroy(categories);
         g_hash_table_destroy(persons);
         g_hash_table_destroy(links);
         RETURN_ERROR;
      }

      if (xml_reader_is_a_local(reader, "title")) {
         str = xml_reader_read_string(reader);
         g_object_set(feed, "title", str, NULL);
         g_free(str);
      } else if (xml_reader_is_a_local(reader, "link")) {
         GomCollection *collection;
         NewsLink *link_;

         g_object_get(feed, "links", &collection, NULL);
         if ((link_ = news_format_rss_read_link(rss, reader, links))) {
            gom_collection_add_resource(collection, GOM_RESOURCE(link_));
            g_object_unref(link_);
         }
         g_object_unref(collection);
      } else if (xml_reader_is_a_local(reader, "description")) {
         str = xml_reader_read_string(reader);
         g_object_set(feed, "description", str, NULL);
         g_free(str);
         xml_reader_read_end_element(reader);
      } else if (xml_reader_is_a_local(reader, "language")) {
         str = xml_reader_read_string(reader);
         g_object_set(feed, "language", str, NULL);
         g_free(str);
      } else if (xml_reader_is_a_local(reader, "copyright")) {
         str = xml_reader_read_string(reader);
         g_object_set(feed, "copyright", str, NULL);
         g_free(str);
      } else if (xml_reader_is_a_local(reader, "managingEditor")) {
         GomCollection *collection;
         NewsPerson *person;

         g_object_get(feed, "authors", &collection, NULL);
         if ((person = news_format_rss_read_person(rss, reader, persons))) {
            gom_collection_add_resource(collection, GOM_RESOURCE(person));
            g_object_unref(person);
         }
         g_object_unref(collection);
      } else if (xml_reader_is_a_local(reader, "pubDate") ||
                 xml_reader_is_a_local(reader, "lastBuildDate")) {
         GDateTime *dt;
         GTimeVal tv;

         if ((str = xml_reader_read_string(reader))) {
            if (g_time_val_from_iso8601(str, &tv)) {
               if ((dt = g_date_time_new_from_timeval_utc(&tv))) {
                  g_object_set(feed,
                            "last-updated-at", dt,
                            NULL);
                  g_date_time_unref(dt);
               }
            }
            g_free(str);
         }
      } else if (xml_reader_is_a_local(reader, "category")) {
         GomCollection *collection;
         NewsCategory *category;

         g_object_get(feed, "categories", &collection, NULL);
         if ((category = news_format_rss_read_category(rss, reader, categories))) {
            gom_collection_add_resource(collection, GOM_RESOURCE(category));
            g_object_unref(category);
         }
         g_object_unref(collection);
      } else if (xml_reader_is_a_local(reader, "generator")) {
         str = xml_reader_read_string(reader);
         g_object_set(feed, "generator", str, NULL);
         g_free(str);
      } else if (xml_reader_is_a_local(reader, "item")) {
         GomCollection *collection;
         NewsItem *item;

         g_object_get(feed, "items", &collection, NULL);
         item = g_object_new(NEWS_TYPE_ITEM,
                        "adapter", adapter,
                        "is-new", TRUE,
                        NULL);
         if (news_format_rss_read_item(rss, reader, item, persons, categories, links)) {
            gom_collection_add_resource(collection,
                                 GOM_RESOURCE(item));
         }
         g_object_unref(item);
         g_object_unref(collection);
      }
   }

   g_hash_table_destroy(categories);
   g_hash_table_destroy(links);
   g_hash_table_destroy(persons);

   RETURN(TRUE);
}
Ejemplo n.º 6
0
static gboolean
news_format_rss_read_item (NewsFormatRss *rss,
                           XmlReader     *reader,
                           NewsItem      *item,
                           GHashTable    *persons,
                           GHashTable    *categories,
                           GHashTable    *links)
{
   gchar *str;

   xml_reader_read(reader);

   for (;
        xml_reader_get_node_type(reader) != XML_READER_TYPE_END_ELEMENT;
        xml_reader_read_to_next(reader)) {
      if (rss->priv->error) {
         break;
      }

      if (xml_reader_is_a_local(reader, "author")) {
         GomCollection *collection;
         NewsPerson *person;

         g_object_get(item, "authors", &collection, NULL);
         if ((person = news_format_rss_read_person(rss, reader, persons))) {
            gom_collection_add_resource(collection, GOM_RESOURCE(person));
            g_object_unref(person);
         }
         g_object_unref(collection);
      } else if (xml_reader_is_a_local(reader, "category")) {
         GomCollection *collection;
         NewsCategory *category;

         g_object_get(item, "categories", &collection, NULL);
         if ((category = news_format_rss_read_category(rss, reader, categories))) {
            gom_collection_add_resource(collection, GOM_RESOURCE(category));
            g_object_unref(category);
         }
         g_object_unref(collection);
      } else if (xml_reader_is_a_local(reader, "link")) {
         GomCollection *collection;
         NewsLink *link_;

         g_object_get(item, "links", &collection, NULL);
         if ((link_ = news_format_rss_read_link(rss, reader, links))) {
            gom_collection_add_resource(collection, GOM_RESOURCE(link_));
            g_object_unref(link_);
         }
         g_object_unref(collection);
      } else if (xml_reader_is_a_local(reader, "guid")) {
         str = xml_reader_read_string(reader);
         g_object_set(item, "item-id", str, NULL);
         g_free(str);
      } else if (xml_reader_is_a_local(reader, "description")) {
         str = xml_reader_read_inner_xml(reader);
         g_object_set(item, "summary", str, NULL);
         g_free(str);
         xml_reader_read_end_element(reader);
      } else if (xml_reader_is_a_local(reader, "title")) {
         str = xml_reader_read_string(reader);
         g_object_set(item, "title", str, NULL);
         g_free(str);
      } else if (xml_reader_is_a_local(reader, "pubDate")) {
         GDateTime *dt;
         GTimeVal tv;

         if ((str = xml_reader_read_string(reader))) {
            if (g_time_val_from_iso8601(str, &tv)) {
               if ((dt = g_date_time_new_from_timeval_utc(&tv))) {
                  g_object_set(item,
                               "last-updated-at", dt,
                               NULL);
                  g_date_time_unref(dt);
               }
            }
            g_free(str);
         }
      } else if (xml_reader_is_a_local(reader, "source")) {
         /*
          * TODO:
          */
      }
   }

   xml_reader_read_end_element(reader);

   return TRUE;
}