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