If it is not possible to parse document, return NULL
[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   if (entry->doc == NULL)
59     {
60       g_slice_free (AtomEntry, entry);
61       return NULL;
62     }
63   root = xmlDocGetRootElement (entry->doc);
64   if (xmlStrcmp (root->name, "entry"))
65     {
66       xmlFreeDoc (entry->doc);
67       g_slice_free (AtomEntry, entry);
68       return NULL;
69     }
70   for (child = root->xmlChildrenNode; child != NULL; child = child->next)
71     {
72       char * content;
73       content = xmlNodeGetContent (child->xmlChildrenNode);
74       if (!xmlStrcmp (child->name, "id"))
75         entry->id = content;
76       else if (!xmlStrcmp (child->name, "title"))
77         entry->title = content;
78       else if (!xmlStrcmp (child->name, "summary"))
79         entry->summary = content;
80       else if (!xmlStrcmp (child->name, "author"))
81         entry->author = atom_person_new_from_xmlnode (child);
82       else
83         xmlFree (content);
84     }
85   return entry;
86 }
87
88 void
89 atom_entry_delete (AtomEntry *entry)
90 {
91   if (entry->doc)
92     xmlFreeDoc (entry->doc);
93   if (entry->id)
94     g_free (entry->id);
95   if (entry->title)
96     g_free (entry->title);
97   if (entry->author)
98     atom_person_delete (entry->author);
99   if (entry->summary)
100     g_free (entry->summary);
101   g_slice_free (AtomEntry, entry);
102 }
103
104 char *
105 atom_entry_id (AtomEntry *entry)
106 {
107   return entry->id;
108 }
109
110 void
111 atom_entry_id_set (AtomEntry *entry, char *id)
112 {
113   if (entry->id)
114     g_free (entry->id);
115   entry->id = g_strdup (id);
116 }
117
118 char *
119 atom_entry_title (AtomEntry *entry)
120 {
121   return entry->title;
122 }
123
124 void
125 atom_entry_title_set (AtomEntry *entry, char *title)
126 {
127   if (entry->title)
128     g_free (title);
129   entry->title = g_strdup (entry);
130 }
131
132 AtomPerson *
133 atom_entry_author (AtomEntry *entry)
134 {
135   return entry->author;
136 }
137
138 void
139 atom_entry_author_set (AtomEntry *entry, AtomPerson *author)
140 {
141   if (entry->author)
142     atom_person_delete (entry->author);
143   entry->author = author;
144 }
145
146 char *
147 atom_entry_summary (AtomEntry *entry)
148 {
149   return entry->summary;
150 }
151
152 void
153 atom_entry_summary_set (AtomEntry *entry, char *summary)
154 {
155   if (entry->summary)
156     g_free (entry->summary);
157   entry->summary = g_strdup (summary);
158 }
159
160 char *
161 atom_entry_string (AtomEntry *entry)
162 {
163   char *buffer;
164   int size;
165   xmlDocDumpMemory (entry->doc, &buffer, &size);
166   return buffer;
167 }
168
169 size_t
170 atom_entry_len (AtomEntry *entry)
171 {
172   char *buffer;
173   int size;
174   xmlDocDumpMemory (entry->doc, &buffer, &size);
175   xmlFree (buffer);
176   return size;
177 }