2 * Copyright (C) 2009 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
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.
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.
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.
20 #include <atompub/content.h>
21 #include <atompub/content-xml.h>
24 #include <libxml/tree.h>
33 xmlNodePtr xmlcontent;
37 atom_content_new (char *type, char *buffer, size_t len)
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;
49 atom_content_new_src (char *type, char *src)
52 content = g_slice_new0 (AtomContent);
53 content->type = g_strdup (type);
54 content->src = g_strdup (src);
59 atom_content_new_data_len (char *buffer, size_t len)
65 doc = xmlReadMemory (buffer, len, NULL, NULL,
66 XML_PARSE_RECOVER | XML_PARSE_NOERROR);
67 if (doc == NULL || (root = xmlDocGetRootElement (doc)) == NULL)
69 if (xmlStrcmp (root->name, "content"))
74 content = g_slice_new0 (AtomContent);
75 if ((content->type = xmlGetProp (root, "type")) == NULL)
77 g_slice_free (AtomContent, content);
81 content->src = xmlGetProp (root, "src");
82 if (content->src == NULL)
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
88 if (root->children && root->children->type == XML_ELEMENT_NODE)
90 content->xmlcontent = xmlCopyNodeList (root->children);
94 content->content = xmlNodeGetContent (root);
95 content->content_len = xmlStrlen (content->content);
103 atom_content_delete (AtomContent *content)
106 g_free (content->type);
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);
117 atom_content_type (AtomContent *content)
119 return content->type;
123 atom_content_type_set (AtomContent *content, char *type)
126 g_free (content->type);
127 content->type = g_strdup (type);
131 atom_content_src (AtomContent *content)
137 atom_content_src_set (AtomContent *content, char *src)
140 g_free (content->src);
141 if (content->content)
143 g_free (content->content);
144 content->content = NULL;
146 if (content->xmlcontent)
148 xmlFreeNode (content->xmlcontent);
149 content->xmlcontent = NULL;
151 content->src = g_strdup (src);
155 atom_content_content (AtomContent *content, char **buffer, size_t *len)
157 if (content->content == NULL)
168 *buffer = g_malloc (content->content_len);
169 memcpy (*buffer, content->content, content->content_len);
172 *len = content->content_len;
177 atom_content_content_set (AtomContent *content, char *buffer, size_t len)
179 if (content->content)
180 g_free (content->content);
183 g_free (content->src);
186 if (content->xmlcontent)
188 xmlFreeNode (content->xmlcontent);
189 content->xmlcontent = NULL;
191 content->content = g_malloc (len);
192 memcpy (content->content, buffer, len);
193 content->content_len = len;
197 atom_content_string (AtomContent *content, char **buffer, size_t *len)
201 node = atom_content_to_xmlnode (content);
210 doc = xmlNewDoc ("1.0");
211 xmlDocSetRootElement (doc, node);
212 xmlDocDumpMemory (doc, buffer, len);
217 atom_content_new_xmlnode (char *type, xmlNodePtr node)
219 AtomContent *content;
220 content = g_slice_new0 (AtomContent);
221 content->type = g_strdup (type);
222 content->xmlcontent = xmlCopyNodeList (node);
227 atom_content_to_xmlnode (AtomContent *content)
230 node = xmlNewNode (NULL, "content");
231 xmlNewProp (node, "type", content->type);
234 xmlNewProp (node, "src", content->src);
237 else if (content->content)
239 xmlNodeSetContentLen (node, content->content, content->content_len);
242 else if (content->xmlcontent)
244 xmlAddChild (node, xmlCopyNodeList (content->xmlcontent));
251 atom_content_content_xmlnode (AtomContent *content)
253 return content->xmlcontent;
257 atom_content_content_set_xmlnode (AtomContent *content, xmlNodePtr node)
259 if (content->xmlcontent)
260 xmlFreeNode (content->xmlcontent);
261 if (content->content)
263 g_free (content->content);
264 content->content = NULL;
268 g_free (content->src);
271 content->xmlcontent = xmlCopyNodeList (node);