Added support for category element
[cascardo/atompub.git] / atom / category.c
1 /*
2  *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #include <atompub/atom.h>
21
22 #include <glib.h>
23 #include <libxml/tree.h>
24
25 struct _atom_category
26 {
27   char *term;
28   IRI *scheme;
29   char *label;
30 };
31
32 AtomCategory *
33 atom_category_new (char *term)
34 {
35   AtomCategory *category;
36   category = g_slice_new (AtomCategory);
37   category->term = g_strdup (term);
38   category->scheme = NULL;
39   category->label = NULL;
40   return category;
41 }
42
43 void
44 atom_category_delete (AtomCategory *category)
45 {
46   if (category->term)
47     g_free (category->term);
48   if (category->scheme)
49     iri_delete (category->scheme);
50   if (category->label)
51     g_free (category->label);
52   g_slice_free (AtomCategory, category);
53 }
54
55 char *
56 atom_category_term (AtomCategory *category)
57 {
58   return category->term;
59 }
60
61 void
62 atom_category_term_set (AtomCategory *category, char *term)
63 {
64   if (category->term)
65     g_free (category->term);
66   category->term = g_strdup (term);
67 }
68
69 IRI *
70 atom_category_scheme (AtomCategory *category)
71 {
72   return category->scheme;
73 }
74
75 void
76 atom_category_scheme_set (AtomCategory *category, IRI *scheme)
77 {
78   if (category->scheme)
79     iri_delete (category->scheme);
80   category->scheme = iri_copy (scheme);
81 }
82
83 char *
84 atom_category_label (AtomCategory *category)
85 {
86   return category->label;
87 }
88
89 void
90 atom_category_label_set (AtomCategory *category, char *label)
91 {
92   if (category->label)
93     g_free (category->label);
94   category->label = g_strdup (label);
95 }
96
97 xmlNodePtr
98 atom_category_to_xmlnode (AtomCategory *category, char *elname)
99 {
100   xmlNodePtr node;
101   node = xmlNewNode (NULL, elname);
102   xmlNewTextChild (node, NULL, "term", category->term);
103   if (category->scheme)
104     xmlNewTextChild (node, NULL, "scheme", iri_get_string (category->scheme));
105   if (category->label)
106     xmlNewTextChild (node, NULL, "label", category->label);
107   return node;
108 }
109
110 AtomPerson *
111 atom_category_new_from_xmlnode (xmlNodePtr node)
112 {
113   xmlNodePtr child;
114   AtomCategory *category;
115   category = g_slice_new0 (AtomCategory);
116   for (child = node->xmlChildrenNode; child != NULL; child = child->next)
117     {
118       char *content = xmlNodeGetContent (child->xmlChildrenNode);
119       if (!xmlStrcmp (child->name, "term"))
120         category->term = content;
121       else if (!xmlStrcmp (child->name, "scheme"))
122         category->scheme = iri_new_from_string (content);
123       else if (!xmlStrcmp (child->name, "label"))
124         category->label = content;
125       else
126         xmlFree (content);
127     }
128   return category;
129 }