47fd693d652edcc4475da28d4ff1787c7b2b6a94
[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_data_len (char *buffer, size_t len)
60 {
61   AtomContent *content;
62   xmlDocPtr doc;
63   xmlNodePtr root;
64   xmlNodePtr child;
65   doc = xmlReadMemory (buffer, len, NULL, NULL,
66                        XML_PARSE_RECOVER | XML_PARSE_NOERROR);
67   if (doc == NULL || (root = xmlDocGetRootElement (doc)) == NULL)
68     return NULL;
69   if (xmlStrcmp (root->name, "content"))
70     {
71       xmlFreeDoc (doc);
72       return NULL;
73     }
74   content = g_slice_new0 (AtomContent);
75   if ((content->type = xmlGetProp (root, "type")) == NULL)
76     {
77       g_slice_free (AtomContent, content);
78       xmlFreeDoc (doc);
79       return NULL;
80     }
81   content->src = xmlGetProp (root, "src");
82   if (content->src == NULL)
83     {
84       /* libxml2 does not help a lot here. We must decide between a
85          XML child or a content child. Using the type attribute might
86          be a good idead. Let's just ask that the current method
87          works. */
88       if (root->children && root->children->type == XML_ELEMENT_NODE)
89         {
90           content->xmlcontent = xmlCopyNodeList (root->children);
91         }
92       else
93         {
94           content->content = xmlNodeGetContent (root);
95           content->content_len = xmlStrlen (content->content);
96         }
97     }
98   xmlFreeDoc (doc);
99   return content;
100 }
101
102 void
103 atom_content_delete (AtomContent *content)
104 {
105   if (content->type)
106     g_free (content->type);
107   if (content->src)
108     g_free (content->src);
109   if (content->content)
110     g_free (content->content);
111   if (content->xmlcontent)
112     xmlFreeNode (content->xmlcontent);
113   g_slice_free (AtomContent, content);
114 }
115
116 char *
117 atom_content_type (AtomContent *content)
118 {
119   return content->type;
120 }
121
122 void
123 atom_content_type_set (AtomContent *content, char *type)
124 {
125   if (content->type)
126     g_free (content->type);
127   content->type = g_strdup (type);
128 }
129
130 char *
131 atom_content_src (AtomContent *content)
132 {
133   return content->src;
134 }
135
136 void
137 atom_content_src_set (AtomContent *content, char *src)
138 {
139   if (content->src)
140     g_free (content->src);
141   if (content->content)
142     {
143       g_free (content->content);
144       content->content = NULL;
145     }
146   if (content->xmlcontent)
147     {
148       xmlFreeNode (content->xmlcontent);
149       content->xmlcontent = NULL;
150     }
151   content->src = g_strdup (src);
152 }
153
154 void
155 atom_content_content (AtomContent *content, char **buffer, size_t *len)
156 {
157   if (content->content == NULL)
158     {
159       if (buffer)
160         *buffer = NULL;
161       if (len)
162         *len = 0;
163     }
164   else
165     {
166       if (buffer)
167         {
168           *buffer = g_malloc (content->content_len);
169           memcpy (*buffer, content->content, content->content_len);
170         }
171       if (len)
172         *len = content->content_len;
173     }
174 }
175
176 void
177 atom_content_content_set (AtomContent *content, char *buffer, size_t len)
178 {
179   if (content->content)
180     g_free (content->content);
181   if (content->src)
182     {
183       g_free (content->src);
184       content->src = NULL;
185     }
186   if (content->xmlcontent)
187     {
188       xmlFreeNode (content->xmlcontent);
189       content->xmlcontent = NULL;
190     }
191   content->content = g_malloc (len);
192   memcpy (content->content, buffer, len);
193 }
194
195 void
196 atom_content_string (AtomContent *content, char **buffer, size_t *len)
197 {
198   xmlDocPtr doc;
199   xmlNodePtr node;
200   node = atom_content_to_xmlnode (content);
201   if (node == NULL)
202     {
203       if (buffer)
204         *buffer = NULL;
205       if (len)
206         *len = 0;
207       return;
208     }
209   doc = xmlNewDoc ("1.0");
210   xmlDocSetRootElement (doc, node);
211   xmlDocDumpMemory (doc, buffer, len);
212   xmlFreeDoc (doc);
213 }
214
215 AtomContent *
216 atom_content_new_xmlnode (char *type, xmlNodePtr node)
217 {
218   AtomContent *content;
219   content = g_slice_new0 (AtomContent);
220   content->type = g_strdup (type);
221   content->xmlcontent = xmlCopyNodeList (node);
222   return content;
223 }
224
225 xmlNodePtr
226 atom_content_to_xmlnode (AtomContent *content)
227 {
228   xmlNodePtr node;
229   node = xmlNewNode (NULL, "content");
230   xmlNewProp (node, "type", content->type);
231   if (content->src)
232     {
233       xmlNewProp (node, "src", content->src);
234       return node;
235     }
236   else if (content->content)
237     {
238       xmlNodeSetContentLen (node, content->content, content->content_len);
239       return node;
240     }
241   else if (content->xmlcontent)
242     {
243       xmlAddChild (node, xmlCopyNodeList (content->xmlcontent));
244       return node;
245     }
246   return node;
247 }
248
249 xmlNodePtr
250 atom_content_content_xmlnode (AtomContent *content)
251 {
252   return content->xmlcontent;
253 }
254
255 void
256 atom_content_content_set_xmlnode (AtomContent *content, xmlNodePtr node)
257 {
258   if (content->xmlcontent)
259     xmlFreeNode (content->xmlcontent);
260   if (content->content)
261     {
262       g_free (content->content);
263       content->content = NULL;
264     }
265   if (content->src)
266     {
267       g_free (content->src);
268       content->src = NULL;
269     }
270   content->xmlcontent = xmlCopyNodeList (node);
271 }