libidav/xml.c

changeset 520
da2b0cc44e4f
parent 404
5c08b8e14df8
child 526
e3c0440bd599
--- a/libidav/xml.c	Tue Mar 12 09:35:04 2019 +0100
+++ b/libidav/xml.c	Wed Mar 13 12:52:24 2019 +0100
@@ -94,7 +94,25 @@
                     newxn->namespace = dav_session_strdup(sn, (char*)n->ns->href);
                 }
                 
-                // TODO: copy attributes
+                xmlAttr *attr = n->properties;
+                DavXmlAttr *newattr = NULL;
+                DavXmlAttr *newattr_last = NULL;
+                while(attr) {
+                    DavXmlAttr *na = ucx_mempool_calloc(mp, 1, sizeof(DavXmlAttr));
+                    na->name = dav_session_strdup(sn, (char*)attr->name);
+                    if(attr->children && attr->children->type == XML_TEXT_NODE) {
+                        na->value = dav_session_strdup(sn, (char*)attr->children->content);
+                    }
+                    if(!newattr) {
+                        newattr = na;
+                    } else {
+                        newattr_last->next = na;
+                    }
+                    newattr_last = na;
+                    
+                    attr = attr->next;
+                }
+                newxn->attributes = newattr;
                 
                 if(n->children) {
                     ConvXmlElm *convc = malloc(sizeof(ConvXmlElm));
@@ -120,7 +138,13 @@
 
 void dav_print_xml(DavXmlNode *node) {
     if(node->type == DAV_XML_ELEMENT) {
-        printf("<%s>", node->name);
+        printf("<%s", node->name);
+        DavXmlAttr *attr = node->attributes;
+        while(attr) {
+            printf(" %s=\"%s\"", attr->name, attr->value);
+            attr = attr->next;
+        }
+        putchar('>');
         
         DavXmlNode *child = node->children;
         if(child) {
@@ -140,7 +164,7 @@
 void dav_print_node(void *stream, write_func writef, UcxMap *nsmap, DavXmlNode *node) {
     while(node) {
         if(node->type == DAV_XML_ELEMENT) {
-            char *tagend = node->children ? ">" : " />";
+            char *tagend = node->children ? ">" : " />";          
             char *prefix = NULL;
             if(node->namespace) {
                 prefix = ucx_map_cstr_get(nsmap, node->namespace);
@@ -158,12 +182,19 @@
                             node->namespace,
                             tagend);
                 } else {
-                    ucx_fprintf(stream, writef, "<%s:%s%s", prefix, node->name, tagend);
+                    ucx_fprintf(stream, writef, "<%s:%s", prefix, node->name);
                 }
             } else {
-                ucx_fprintf(stream, writef, "<%s%s", node->name, tagend);
+                ucx_fprintf(stream, writef, "<%s", node->name);
             }
             
+            DavXmlAttr *attr = node->attributes;
+            while(attr) {
+                ucx_fprintf(stream, writef, " %s=\"%s\"", attr->name, attr->value);
+                attr = attr->next;
+            }
+            writef(tagend, 1, strlen(tagend), stream); // end xml tag
+            
             if(node->children) {
                 dav_print_node(stream, writef, nsmap, node->children);
                 if(prefix) {
@@ -296,6 +327,24 @@
     }
 }
 
+void dav_xml_add_attr(DavXmlNode *node, const char *name, const char *value) {
+    DavXmlAttr *attr = calloc(1, sizeof(DavXmlAttr));
+    attr->name = strdup(name);
+    attr->value = strdup(value);
+    
+    if(node->attributes) {
+        DavXmlAttr *last;
+        DavXmlAttr *end = node->attributes;
+        while(end) {
+            last = end;
+            end = end->next;
+        }
+        last->next = attr;
+    } else {
+        node->attributes = attr;
+    }
+}
+
 DavXmlNode* dav_parse_xml(DavSession *sn, const char *str, size_t len) {
     xmlDoc *doc = xmlReadMemory(str, len, NULL, NULL, 0);
     if(!doc) {

mercurial