Change lookup method to find if content is of XML type.
[cascardo/atompub.git] / atom / content.c
index 8dfbdd0..4dea453 100644 (file)
@@ -56,26 +56,18 @@ atom_content_new_src (char *type, char *src)
 }
 
 AtomContent *
 }
 
 AtomContent *
-atom_content_new_data_len (char *buffer, size_t len)
+atom_content_new_from_xmlnode (xmlNodePtr root)
 {
   AtomContent *content;
 {
   AtomContent *content;
-  xmlDocPtr doc;
-  xmlNodePtr root;
   xmlNodePtr child;
   xmlNodePtr child;
-  doc = xmlReadMemory (buffer, len, NULL, NULL,
-                      XML_PARSE_RECOVER | XML_PARSE_NOERROR);
-  if (doc == NULL || (root = xmlDocGetRootElement (doc)) == NULL)
-    return NULL;
   if (xmlStrcmp (root->name, "content"))
     {
   if (xmlStrcmp (root->name, "content"))
     {
-      xmlFreeDoc (doc);
       return NULL;
     }
   content = g_slice_new0 (AtomContent);
   if ((content->type = xmlGetProp (root, "type")) == NULL)
     {
       g_slice_free (AtomContent, content);
       return NULL;
     }
   content = g_slice_new0 (AtomContent);
   if ((content->type = xmlGetProp (root, "type")) == NULL)
     {
       g_slice_free (AtomContent, content);
-      xmlFreeDoc (doc);
       return NULL;
     }
   content->src = xmlGetProp (root, "src");
       return NULL;
     }
   content->src = xmlGetProp (root, "src");
@@ -85,7 +77,10 @@ atom_content_new_data_len (char *buffer, size_t len)
         XML child or a content child. Using the type attribute might
         be a good idead. Let's just ask that the current method
         works. */
         XML child or a content child. Using the type attribute might
         be a good idead. Let's just ask that the current method
         works. */
-      if (root->children && root->children->type == XML_ELEMENT_NODE)
+      child = root->children;
+      while (child && child->type != XML_ELEMENT_NODE)
+       child = child->next;
+      if (child)
        {
          content->xmlcontent = xmlCopyNodeList (root->children);
        }
        {
          content->xmlcontent = xmlCopyNodeList (root->children);
        }
@@ -95,6 +90,20 @@ atom_content_new_data_len (char *buffer, size_t len)
          content->content_len = xmlStrlen (content->content);
        }
     }
          content->content_len = xmlStrlen (content->content);
        }
     }
+  return content;
+}
+
+AtomContent *
+atom_content_new_data_len (char * buffer, size_t len)
+{
+  AtomContent *content;
+  xmlDocPtr doc;
+  xmlNodePtr root;
+  doc = xmlReadMemory (buffer, len, NULL, NULL,
+                      XML_PARSE_RECOVER | XML_PARSE_NOERROR);
+  if (doc == NULL || (root = xmlDocGetRootElement (doc)) == NULL)
+    return NULL;
+  content = atom_content_new_from_xmlnode (root);
   xmlFreeDoc (doc);
   return content;
 }
   xmlFreeDoc (doc);
   return content;
 }
@@ -139,9 +148,15 @@ atom_content_src_set (AtomContent *content, char *src)
   if (content->src)
     g_free (content->src);
   if (content->content)
   if (content->src)
     g_free (content->src);
   if (content->content)
-    g_free (content->content);
+    {
+      g_free (content->content);
+      content->content = NULL;
+    }
   if (content->xmlcontent)
   if (content->xmlcontent)
-    xmlFreeNode (content->xmlcontent);
+    {
+      xmlFreeNode (content->xmlcontent);
+      content->xmlcontent = NULL;
+    }
   content->src = g_strdup (src);
 }
 
   content->src = g_strdup (src);
 }
 
@@ -173,11 +188,18 @@ atom_content_content_set (AtomContent *content, char *buffer, size_t len)
   if (content->content)
     g_free (content->content);
   if (content->src)
   if (content->content)
     g_free (content->content);
   if (content->src)
-    g_free (content->src);
+    {
+      g_free (content->src);
+      content->src = NULL;
+    }
   if (content->xmlcontent)
   if (content->xmlcontent)
-    xmlFreeNode (content->xmlcontent);
+    {
+      xmlFreeNode (content->xmlcontent);
+      content->xmlcontent = NULL;
+    }
   content->content = g_malloc (len);
   memcpy (content->content, buffer, len);
   content->content = g_malloc (len);
   memcpy (content->content, buffer, len);
+  content->content_len = len;
 }
 
 void
 }
 
 void
@@ -235,19 +257,25 @@ atom_content_to_xmlnode (AtomContent *content)
 }
 
 xmlNodePtr
 }
 
 xmlNodePtr
-atom_content_content_xmlnode (AtomContent *content)
+atom_content_xmlcontent (AtomContent *content)
 {
   return content->xmlcontent;
 }
 
 void
 {
   return content->xmlcontent;
 }
 
 void
-atom_content_content_set_xmlnode (AtomContent *content, xmlNodePtr node)
+atom_content_xmlcontent_set (AtomContent *content, xmlNodePtr node)
 {
   if (content->xmlcontent)
     xmlFreeNode (content->xmlcontent);
   if (content->content)
 {
   if (content->xmlcontent)
     xmlFreeNode (content->xmlcontent);
   if (content->content)
-    g_free (content->content);
+    {
+      g_free (content->content);
+      content->content = NULL;
+    }
   if (content->src)
   if (content->src)
-    g_free (content->src);
+    {
+      g_free (content->src);
+      content->src = NULL;
+    }
   content->xmlcontent = xmlCopyNodeList (node);
 }
   content->xmlcontent = xmlCopyNodeList (node);
 }