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)
142 g_free (content->content);
143 if (content->xmlcontent)
144 xmlFreeNode (content->xmlcontent);
145 content->src = g_strdup (src);
149 atom_content_content (AtomContent *content, char **buffer, size_t *len)
151 if (content->content == NULL)
162 *buffer = g_malloc (content->content_len);
163 memcpy (*buffer, content->content, content->content_len);
166 *len = content->content_len;
171 atom_content_content_set (AtomContent *content, char *buffer, size_t len)
173 if (content->content)
174 g_free (content->content);
176 g_free (content->src);
177 if (content->xmlcontent)
178 xmlFreeNode (content->xmlcontent);
179 content->content = g_malloc (len);
180 memcpy (content->content, buffer, len);
184 atom_content_string (AtomContent *content, char **buffer, size_t *len)
188 node = atom_content_to_xmlnode (content);
197 doc = xmlNewDoc ("1.0");
198 xmlDocSetRootElement (doc, node);
199 xmlDocDumpMemory (doc, buffer, len);
204 atom_content_new_xmlnode (char *type, xmlNodePtr node)
206 AtomContent *content;
207 content = g_slice_new0 (AtomContent);
208 content->type = g_strdup (type);
209 content->xmlcontent = xmlCopyNodeList (node);
214 atom_content_to_xmlnode (AtomContent *content)
217 node = xmlNewNode (NULL, "content");
218 xmlNewProp (node, "type", content->type);
221 xmlNewProp (node, "src", content->src);
224 else if (content->content)
226 xmlNodeSetContentLen (node, content->content, content->content_len);
229 else if (content->xmlcontent)
231 xmlAddChild (node, xmlCopyNodeList (content->xmlcontent));
238 atom_content_content_xmlnode (AtomContent *content)
240 return content->xmlcontent;
244 atom_content_content_set_xmlnode (AtomContent *content, xmlNodePtr node)
246 if (content->xmlcontent)
247 xmlFreeNode (content->xmlcontent);
248 if (content->content)
249 g_free (content->content);
251 g_free (content->src);
252 content->xmlcontent = xmlCopyNodeList (node);