92 newxn->name = dav_session_strdup(sn, (char*)n->name); |
92 newxn->name = dav_session_strdup(sn, (char*)n->name); |
93 if(n->ns && n->ns->href) { |
93 if(n->ns && n->ns->href) { |
94 newxn->namespace = dav_session_strdup(sn, (char*)n->ns->href); |
94 newxn->namespace = dav_session_strdup(sn, (char*)n->ns->href); |
95 } |
95 } |
96 |
96 |
97 // TODO: copy attributes |
97 xmlAttr *attr = n->properties; |
|
98 DavXmlAttr *newattr = NULL; |
|
99 DavXmlAttr *newattr_last = NULL; |
|
100 while(attr) { |
|
101 DavXmlAttr *na = ucx_mempool_calloc(mp, 1, sizeof(DavXmlAttr)); |
|
102 na->name = dav_session_strdup(sn, (char*)attr->name); |
|
103 if(attr->children && attr->children->type == XML_TEXT_NODE) { |
|
104 na->value = dav_session_strdup(sn, (char*)attr->children->content); |
|
105 } |
|
106 if(!newattr) { |
|
107 newattr = na; |
|
108 } else { |
|
109 newattr_last->next = na; |
|
110 } |
|
111 newattr_last = na; |
|
112 |
|
113 attr = attr->next; |
|
114 } |
|
115 newxn->attributes = newattr; |
98 |
116 |
99 if(n->children) { |
117 if(n->children) { |
100 ConvXmlElm *convc = malloc(sizeof(ConvXmlElm)); |
118 ConvXmlElm *convc = malloc(sizeof(ConvXmlElm)); |
101 convc->node = n->children; |
119 convc->node = n->children; |
102 convc->parent = newxn; |
120 convc->parent = newxn; |
118 return ret; |
136 return ret; |
119 } |
137 } |
120 |
138 |
121 void dav_print_xml(DavXmlNode *node) { |
139 void dav_print_xml(DavXmlNode *node) { |
122 if(node->type == DAV_XML_ELEMENT) { |
140 if(node->type == DAV_XML_ELEMENT) { |
123 printf("<%s>", node->name); |
141 printf("<%s", node->name); |
|
142 DavXmlAttr *attr = node->attributes; |
|
143 while(attr) { |
|
144 printf(" %s=\"%s\"", attr->name, attr->value); |
|
145 attr = attr->next; |
|
146 } |
|
147 putchar('>'); |
124 |
148 |
125 DavXmlNode *child = node->children; |
149 DavXmlNode *child = node->children; |
126 if(child) { |
150 if(child) { |
127 dav_print_xml(child); |
151 dav_print_xml(child); |
128 } |
152 } |
138 } |
162 } |
139 |
163 |
140 void dav_print_node(void *stream, write_func writef, UcxMap *nsmap, DavXmlNode *node) { |
164 void dav_print_node(void *stream, write_func writef, UcxMap *nsmap, DavXmlNode *node) { |
141 while(node) { |
165 while(node) { |
142 if(node->type == DAV_XML_ELEMENT) { |
166 if(node->type == DAV_XML_ELEMENT) { |
143 char *tagend = node->children ? ">" : " />"; |
167 char *tagend = node->children ? ">" : " />"; |
144 char *prefix = NULL; |
168 char *prefix = NULL; |
145 if(node->namespace) { |
169 if(node->namespace) { |
146 prefix = ucx_map_cstr_get(nsmap, node->namespace); |
170 prefix = ucx_map_cstr_get(nsmap, node->namespace); |
147 if(!prefix) { |
171 if(!prefix) { |
148 sstr_t newpre = ucx_sprintf("x%d", (int)nsmap->count); |
172 sstr_t newpre = ucx_sprintf("x%d", (int)nsmap->count); |
156 node->name, |
180 node->name, |
157 prefix, |
181 prefix, |
158 node->namespace, |
182 node->namespace, |
159 tagend); |
183 tagend); |
160 } else { |
184 } else { |
161 ucx_fprintf(stream, writef, "<%s:%s%s", prefix, node->name, tagend); |
185 ucx_fprintf(stream, writef, "<%s:%s", prefix, node->name); |
162 } |
186 } |
163 } else { |
187 } else { |
164 ucx_fprintf(stream, writef, "<%s%s", node->name, tagend); |
188 ucx_fprintf(stream, writef, "<%s", node->name); |
165 } |
189 } |
|
190 |
|
191 DavXmlAttr *attr = node->attributes; |
|
192 while(attr) { |
|
193 ucx_fprintf(stream, writef, " %s=\"%s\"", attr->name, attr->value); |
|
194 attr = attr->next; |
|
195 } |
|
196 writef(tagend, 1, strlen(tagend), stream); // end xml tag |
166 |
197 |
167 if(node->children) { |
198 if(node->children) { |
168 dav_print_node(stream, writef, nsmap, node->children); |
199 dav_print_node(stream, writef, nsmap, node->children); |
169 if(prefix) { |
200 if(prefix) { |
170 ucx_fprintf(stream, writef, "</%s:%s>", prefix, node->name); |
201 ucx_fprintf(stream, writef, "</%s:%s>", prefix, node->name); |
294 } else { |
325 } else { |
295 node->children = child; |
326 node->children = child; |
296 } |
327 } |
297 } |
328 } |
298 |
329 |
|
330 void dav_xml_add_attr(DavXmlNode *node, const char *name, const char *value) { |
|
331 DavXmlAttr *attr = calloc(1, sizeof(DavXmlAttr)); |
|
332 attr->name = strdup(name); |
|
333 attr->value = strdup(value); |
|
334 |
|
335 if(node->attributes) { |
|
336 DavXmlAttr *last; |
|
337 DavXmlAttr *end = node->attributes; |
|
338 while(end) { |
|
339 last = end; |
|
340 end = end->next; |
|
341 } |
|
342 last->next = attr; |
|
343 } else { |
|
344 node->attributes = attr; |
|
345 } |
|
346 } |
|
347 |
299 DavXmlNode* dav_parse_xml(DavSession *sn, const char *str, size_t len) { |
348 DavXmlNode* dav_parse_xml(DavSession *sn, const char *str, size_t len) { |
300 xmlDoc *doc = xmlReadMemory(str, len, NULL, NULL, 0); |
349 xmlDoc *doc = xmlReadMemory(str, len, NULL, NULL, 0); |
301 if(!doc) { |
350 if(!doc) { |
302 return NULL; |
351 return NULL; |
303 } |
352 } |