d9d1f2a9a05529a80a6a2309c10ca23154773096
[cascardo/atompub.git] / atom / entry.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 #include <libxml/parser.h>
25
26 #define ATOM_NAMESPACE "http://www.w3.org/2005/Atom"
27
28 struct _atom_entry
29 {
30   xmlDocPtr doc;
31   char *id;
32   char *title;
33   AtomPerson *author;
34   char *summary;
35 };
36
37 AtomEntry *
38 atom_entry_new (char *title, AtomPerson *author)
39 {
40   AtomEntry *entry;
41   entry = g_slice_new (AtomEntry);
42   entry->doc = NULL;
43   entry->id = NULL;
44   entry->title = g_strdup (title);
45   entry->author = author;
46   entry->summary = NULL;
47   return entry;
48 }
49
50 AtomEntry *
51 atom_entry_new_data_len (char *data, size_t len)
52 {
53   AtomEntry *entry;
54   xmlNodePtr root;
55   xmlNodePtr child;
56   entry = g_slice_new0 (AtomEntry);
57   entry->doc = xmlReadMemory (data, len, NULL, NULL, XML_PARSE_RECOVER);
58   root = xmlDocGetRootElement (entry->doc);
59   if (xmlStrcmp (root->name, "entry"))
60     {
61       xmlFreeDoc (entry->doc);
62       g_slice_free (AtomEntry, entry);
63       return NULL;
64     }
65   for (child = root->xmlChildrenNode; child != NULL; child = child->next)
66     {
67       char * content;
68       content = xmlNodeGetContent (child->xmlChildrenNode);
69       if (!xmlStrcmp (child->name, "id"))
70         entry->id = content;
71       else if (!xmlStrcmp (child->name, "title"))
72         entry->title = content;
73       else if (!xmlStrcmp (child->name, "summary"))
74         entry->summary = content;
75       else if (!xmlStrcmp (child->name, "author"))
76         entry->author = atom_person_new_from_xmlnode (child);
77       else
78         xmlFree (content);
79     }
80   return entry;
81 }
82
83 void
84 atom_entry_delete (AtomEntry *entry)
85 {
86   if (entry->doc)
87     xmlFreeDoc (entry->doc);
88   if (entry->id)
89     g_free (entry->id);
90   if (entry->title)
91     g_free (entry->title);
92   if (entry->author)
93     atom_person_delete (entry->author);
94   if (entry->summary)
95     g_free (entry->summary);
96   g_slice_free (AtomEntry, entry);
97 }
98
99 char *
100 atom_entry_id (AtomEntry *entry)
101 {
102   return entry->id;
103 }
104
105 void
106 atom_entry_id_set (AtomEntry *entry, char *id)
107 {
108   if (entry->id)
109     g_free (entry->id);
110   entry->id = g_strdup (id);
111 }
112
113 char *
114 atom_entry_title (AtomEntry *entry)
115 {
116   return entry->title;
117 }
118
119 void
120 atom_entry_title_set (AtomEntry *entry, char *title)
121 {
122   if (entry->title)
123     g_free (title);
124   entry->title = g_strdup (entry);
125 }
126
127 AtomPerson *
128 atom_entry_author (AtomEntry *entry)
129 {
130   return entry->author;
131 }
132
133 void
134 atom_entry_author_set (AtomEntry *entry, AtomPerson *author)
135 {
136   if (entry->author)
137     atom_person_delete (entry->author);
138   entry->author = author;
139 }
140
141 char *
142 atom_entry_summary (AtomEntry *entry)
143 {
144   return entry->summary;
145 }
146
147 void
148 atom_entry_summary_set (AtomEntry *entry, char *summary)
149 {
150   if (entry->summary)
151     g_free (entry->summary);
152   entry->summary = g_strdup (summary);
153 }
154
155 char *
156 atom_entry_string (AtomEntry *entry)
157 {
158   char *buffer;
159   int size;
160   xmlDocDumpMemory (entry->doc, &buffer, &size);
161   return buffer;
162 }
163
164 size_t
165 atom_entry_len (AtomEntry *entry)
166 {
167   char *buffer;
168   int size;
169   xmlDocDumpMemory (entry->doc, &buffer, &size);
170   xmlFree (buffer);
171   return size;
172 }