Added support for category element
[cascardo/atompub.git] / atom / category.c
diff --git a/atom/category.c b/atom/category.c
new file mode 100644 (file)
index 0000000..6b18679
--- /dev/null
@@ -0,0 +1,129 @@
+/*
+ *  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
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#include <atompub/atom.h>
+
+#include <glib.h>
+#include <libxml/tree.h>
+
+struct _atom_category
+{
+  char *term;
+  IRI *scheme;
+  char *label;
+};
+
+AtomCategory *
+atom_category_new (char *term)
+{
+  AtomCategory *category;
+  category = g_slice_new (AtomCategory);
+  category->term = g_strdup (term);
+  category->scheme = NULL;
+  category->label = NULL;
+  return category;
+}
+
+void
+atom_category_delete (AtomCategory *category)
+{
+  if (category->term)
+    g_free (category->term);
+  if (category->scheme)
+    iri_delete (category->scheme);
+  if (category->label)
+    g_free (category->label);
+  g_slice_free (AtomCategory, category);
+}
+
+char *
+atom_category_term (AtomCategory *category)
+{
+  return category->term;
+}
+
+void
+atom_category_term_set (AtomCategory *category, char *term)
+{
+  if (category->term)
+    g_free (category->term);
+  category->term = g_strdup (term);
+}
+
+IRI *
+atom_category_scheme (AtomCategory *category)
+{
+  return category->scheme;
+}
+
+void
+atom_category_scheme_set (AtomCategory *category, IRI *scheme)
+{
+  if (category->scheme)
+    iri_delete (category->scheme);
+  category->scheme = iri_copy (scheme);
+}
+
+char *
+atom_category_label (AtomCategory *category)
+{
+  return category->label;
+}
+
+void
+atom_category_label_set (AtomCategory *category, char *label)
+{
+  if (category->label)
+    g_free (category->label);
+  category->label = g_strdup (label);
+}
+
+xmlNodePtr
+atom_category_to_xmlnode (AtomCategory *category, char *elname)
+{
+  xmlNodePtr node;
+  node = xmlNewNode (NULL, elname);
+  xmlNewTextChild (node, NULL, "term", category->term);
+  if (category->scheme)
+    xmlNewTextChild (node, NULL, "scheme", iri_get_string (category->scheme));
+  if (category->label)
+    xmlNewTextChild (node, NULL, "label", category->label);
+  return node;
+}
+
+AtomPerson *
+atom_category_new_from_xmlnode (xmlNodePtr node)
+{
+  xmlNodePtr child;
+  AtomCategory *category;
+  category = g_slice_new0 (AtomCategory);
+  for (child = node->xmlChildrenNode; child != NULL; child = child->next)
+    {
+      char *content = xmlNodeGetContent (child->xmlChildrenNode);
+      if (!xmlStrcmp (child->name, "term"))
+       category->term = content;
+      else if (!xmlStrcmp (child->name, "scheme"))
+       category->scheme = iri_new_from_string (content);
+      else if (!xmlStrcmp (child->name, "label"))
+       category->label = content;
+      else
+       xmlFree (content);
+    }
+  return category;
+}