Do not output error when trying to parse XML data
[cascardo/atompub.git] / atom / entry.c
index c1385d1..069207b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (C) 2007  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 #include <atompub/atom.h>
 
 #include <glib.h>
+#include <libxml/tree.h>
+#include <libxml/parser.h>
 
 struct _atom_entry
 {
-  char *data;
-  size_t len;
+  xmlDocPtr doc;
+  char *id;
+  char *title;
+  AtomPerson *author;
+  char *summary;
 };
 
-Atom *
-atom_new_data_len (char *data, size_t len)
+AtomEntry *
+atom_entry_new (char *title, AtomPerson *author)
 {
-  Atom *atom = g_slice_new (Atom);
-  atom->data = g_malloc (len);
-  memcpy (atom->data, data, len);
-  atom->len = len;
-  return atom;
+  AtomEntry *entry;
+  entry = g_slice_new (AtomEntry);
+  entry->doc = NULL;
+  entry->id = NULL;
+  entry->title = g_strdup (title);
+  entry->author = author;
+  entry->summary = NULL;
+  return entry;
+}
+
+AtomEntry *
+atom_entry_new_data_len (char *data, size_t len)
+{
+  AtomEntry *entry;
+  xmlNodePtr root;
+  xmlNodePtr child;
+  entry = g_slice_new0 (AtomEntry);
+  entry->doc = xmlReadMemory (data, len, NULL, NULL,
+                              XML_PARSE_RECOVER | XML_PARSE_NOERROR);
+  if (entry->doc == NULL ||
+      (root = xmlDocGetRootElement (entry->doc)) == NULL)
+    {
+      g_slice_free (AtomEntry, entry);
+      return NULL;
+    }
+  if (xmlStrcmp (root->name, "entry"))
+    {
+      xmlFreeDoc (entry->doc);
+      g_slice_free (AtomEntry, entry);
+      return NULL;
+    }
+  for (child = root->xmlChildrenNode; child != NULL; child = child->next)
+    {
+      char * content;
+      content = xmlNodeGetContent (child->xmlChildrenNode);
+      if (!xmlStrcmp (child->name, "id"))
+       entry->id = content;
+      else if (!xmlStrcmp (child->name, "title"))
+       entry->title = content;
+      else if (!xmlStrcmp (child->name, "summary"))
+       entry->summary = content;
+      else if (!xmlStrcmp (child->name, "author"))
+       entry->author = atom_person_new_from_xmlnode (child);
+      else
+       xmlFree (content);
+    }
+  return entry;
+}
+
+void
+atom_entry_delete (AtomEntry *entry)
+{
+  if (entry->doc)
+    xmlFreeDoc (entry->doc);
+  if (entry->id)
+    g_free (entry->id);
+  if (entry->title)
+    g_free (entry->title);
+  if (entry->author)
+    atom_person_delete (entry->author);
+  if (entry->summary)
+    g_free (entry->summary);
+  g_slice_free (AtomEntry, entry);
+}
+
+char *
+atom_entry_id (AtomEntry *entry)
+{
+  return entry->id;
+}
+
+void
+atom_entry_id_set (AtomEntry *entry, char *id)
+{
+  if (entry->id)
+    g_free (entry->id);
+  entry->id = g_strdup (id);
+}
+
+char *
+atom_entry_title (AtomEntry *entry)
+{
+  return entry->title;
+}
+
+void
+atom_entry_title_set (AtomEntry *entry, char *title)
+{
+  if (entry->title)
+    g_free (title);
+  entry->title = g_strdup (entry);
+}
+
+AtomPerson *
+atom_entry_author (AtomEntry *entry)
+{
+  return entry->author;
 }
 
 void
-atom_delete (Atom *atom)
+atom_entry_author_set (AtomEntry *entry, AtomPerson *author)
 {
-  if (atom->data)
-    g_free (atom->data);
-  g_slice_free (Atom, atom);
+  if (entry->author)
+    atom_person_delete (entry->author);
+  entry->author = author;
 }
 
 char *
-atom_string (Atom *atom)
+atom_entry_summary (AtomEntry *entry)
+{
+  return entry->summary;
+}
+
+void
+atom_entry_summary_set (AtomEntry *entry, char *summary)
+{
+  if (entry->summary)
+    g_free (entry->summary);
+  entry->summary = g_strdup (summary);
+}
+
+void
+atom_entry_string (AtomEntry *entry, char **buffer, size_t *len)
 {
-  return atom->data;
+  xmlDocDumpMemory (entry->doc, buffer, len);
 }
 
-size_t
-atom_len (Atom *atom)
+xmlNodePtr
+atom_entry_to_xmlnode (AtomEntry *entry)
 {
-  return atom->len;
+  if (entry->doc)
+    return xmlCopyNodeList (xmlDocGetRootElement (entry->doc));
+  return NULL;
 }