Change lookup method to find if content is of XML type.
[cascardo/atompub.git] / atom / content.c
1 /*
2  *  Copyright (C) 2009  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/content.h>
21 #include <atompub/content-xml.h>
22
23 #include <glib.h>
24 #include <libxml/tree.h>
25 #include <string.h>
26
27 struct _atom_content
28 {
29   char *type;
30   char *src;
31   char *content;
32   size_t content_len;
33   xmlNodePtr xmlcontent;
34 };
35
36 AtomContent *
37 atom_content_new (char *type, char *buffer, size_t len)
38 {
39   AtomContent *content;
40   content = g_slice_new0 (AtomContent);
41   content->type = g_strdup (type);
42   content->content = g_malloc (len);
43   memcpy (content->content, buffer, len);
44   content->content_len = len;
45   return content;
46 }
47
48 AtomContent *
49 atom_content_new_src (char *type, char *src)
50 {
51   AtomContent *content;
52   content = g_slice_new0 (AtomContent);
53   content->type = g_strdup (type);
54   content->src = g_strdup (src);
55   return content;
56 }
57
58 AtomContent *
59 atom_content_new_from_xmlnode (xmlNodePtr root)
60 {
61   AtomContent *content;
62   xmlNodePtr child;
63   if (xmlStrcmp (root->name, "content"))
64     {
65       return NULL;
66     }
67   content = g_slice_new0 (AtomContent);
68   if ((content->type = xmlGetProp (root, "type")) == NULL)
69     {
70       g_slice_free (AtomContent, content);
71       return NULL;
72     }
73   content->src = xmlGetProp (root, "src");
74   if (content->src == NULL)
75     {
76       /* libxml2 does not help a lot here. We must decide between a
77          XML child or a content child. Using the type attribute might
78          be a good idead. Let's just ask that the current method
79          works. */
80       child = root->children;
81       while (child && child->type != XML_ELEMENT_NODE)
82         child = child->next;
83       if (child)
84         {
85           content->xmlcontent = xmlCopyNodeList (root->children);
86         }
87       else
88         {
89           content->content = xmlNodeGetContent (root);
90           content->content_len = xmlStrlen (content->content);
91         }
92     }
93   return content;
94 }
95
96 AtomContent *
97 atom_content_new_data_len (char * buffer, size_t len)
98 {
99   AtomContent *content;
100   xmlDocPtr doc;
101   xmlNodePtr root;
102   doc = xmlReadMemory (buffer, len, NULL, NULL,
103                        XML_PARSE_RECOVER | XML_PARSE_NOERROR);
104   if (doc == NULL || (root = xmlDocGetRootElement (doc)) == NULL)
105     return NULL;
106   content = atom_content_new_from_xmlnode (root);
107   xmlFreeDoc (doc);
108   return content;
109 }
110
111 void
112 atom_content_delete (AtomContent *content)
113 {
114   if (content->type)
115     g_free (content->type);
116   if (content->src)
117     g_free (content->src);
118   if (content->content)
119     g_free (content->content);
120   if (content->xmlcontent)
121     xmlFreeNode (content->xmlcontent);
122   g_slice_free (AtomContent, content);
123 }
124
125 char *
126 atom_content_type (AtomContent *content)
127 {
128   return content->type;
129 }
130
131 void
132 atom_content_type_set (AtomContent *content, char *type)
133 {
134   if (content->type)
135     g_free (content->type);
136   content->type = g_strdup (type);
137 }
138
139 char *
140 atom_content_src (AtomContent *content)
141 {
142   return content->src;
143 }
144
145 void
146 atom_content_src_set (AtomContent *content, char *src)
147 {
148   if (content->src)
149     g_free (content->src);
150   if (content->content)
151     {
152       g_free (content->content);
153       content->content = NULL;
154     }
155   if (content->xmlcontent)
156     {
157       xmlFreeNode (content->xmlcontent);
158       content->xmlcontent = NULL;
159     }
160   content->src = g_strdup (src);
161 }
162
163 void
164 atom_content_content (AtomContent *content, char **buffer, size_t *len)
165 {
166   if (content->content == NULL)
167     {
168       if (buffer)
169         *buffer = NULL;
170       if (len)
171         *len = 0;
172     }
173   else
174     {
175       if (buffer)
176         {
177           *buffer = g_malloc (content->content_len);
178           memcpy (*buffer, content->content, content->content_len);
179         }
180       if (len)
181         *len = content->content_len;
182     }
183 }
184
185 void
186 atom_content_content_set (AtomContent *content, char *buffer, size_t len)
187 {
188   if (content->content)
189     g_free (content->content);
190   if (content->src)
191     {
192       g_free (content->src);
193       content->src = NULL;
194     }
195   if (content->xmlcontent)
196     {
197       xmlFreeNode (content->xmlcontent);
198       content->xmlcontent = NULL;
199     }
200   content->content = g_malloc (len);
201   memcpy (content->content, buffer, len);
202   content->content_len = len;
203 }
204
205 void
206 atom_content_string (AtomContent *content, char **buffer, size_t *len)
207 {
208   xmlDocPtr doc;
209   xmlNodePtr node;
210   node = atom_content_to_xmlnode (content);
211   if (node == NULL)
212     {
213       if (buffer)
214         *buffer = NULL;
215       if (len)
216         *len = 0;
217       return;
218     }
219   doc = xmlNewDoc ("1.0");
220   xmlDocSetRootElement (doc, node);
221   xmlDocDumpMemory (doc, buffer, len);
222   xmlFreeDoc (doc);
223 }
224
225 AtomContent *
226 atom_content_new_xmlnode (char *type, xmlNodePtr node)
227 {
228   AtomContent *content;
229   content = g_slice_new0 (AtomContent);
230   content->type = g_strdup (type);
231   content->xmlcontent = xmlCopyNodeList (node);
232   return content;
233 }
234
235 xmlNodePtr
236 atom_content_to_xmlnode (AtomContent *content)
237 {
238   xmlNodePtr node;
239   node = xmlNewNode (NULL, "content");
240   xmlNewProp (node, "type", content->type);
241   if (content->src)
242     {
243       xmlNewProp (node, "src", content->src);
244       return node;
245     }
246   else if (content->content)
247     {
248       xmlNodeSetContentLen (node, content->content, content->content_len);
249       return node;
250     }
251   else if (content->xmlcontent)
252     {
253       xmlAddChild (node, xmlCopyNodeList (content->xmlcontent));
254       return node;
255     }
256   return node;
257 }
258
259 xmlNodePtr
260 atom_content_xmlcontent (AtomContent *content)
261 {
262   return content->xmlcontent;
263 }
264
265 void
266 atom_content_xmlcontent_set (AtomContent *content, xmlNodePtr node)
267 {
268   if (content->xmlcontent)
269     xmlFreeNode (content->xmlcontent);
270   if (content->content)
271     {
272       g_free (content->content);
273       content->content = NULL;
274     }
275   if (content->src)
276     {
277       g_free (content->src);
278       content->src = NULL;
279     }
280   content->xmlcontent = xmlCopyNodeList (node);
281 }