libidav/xml.c

changeset 361
b6f2462ee055
parent 338
c7f3fe4abdb2
child 371
604e7e335b3b
equal deleted inserted replaced
355:5da2cf15eb44 361:b6f2462ee055
27 */ 27 */
28 28
29 #include <stdio.h> 29 #include <stdio.h>
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <string.h> 31 #include <string.h>
32
33 #include <ucx/utils.h>
32 34
33 #include "xml.h" 35 #include "xml.h"
34 36
35 static DavXmlNodeType convert_type(xmlElementType type) { 37 static DavXmlNodeType convert_type(xmlElementType type) {
36 DavXmlNodeType ct; 38 DavXmlNodeType ct;
133 if(node->next) { 135 if(node->next) {
134 dav_print_xml(node->next); 136 dav_print_xml(node->next);
135 } 137 }
136 } 138 }
137 139
140 void dav_print_node(void *stream, write_func writef, UcxMap *nsmap, DavXmlNode *node) {
141 while(node) {
142 if(node->type == DAV_XML_ELEMENT) {
143 char *prefix = ucx_map_cstr_get(nsmap, node->namespace);
144 char *tagend = node->children ? ">" : " />";
145 if(!prefix) {
146 sstr_t newpre = ucx_sprintf("x%d", (int)nsmap->count);
147 ucx_map_cstr_put(nsmap, node->namespace, newpre.ptr);
148 prefix = newpre.ptr;
149 ucx_fprintf(
150 stream,
151 writef,
152 "<%s:%s xmlns:%s=\"%s\"%s",
153 prefix,
154 node->name,
155 prefix,
156 node->namespace,
157 tagend);
158 } else {
159 ucx_fprintf(stream, writef, "<%s:%s%s", prefix, node->name, tagend);
160 }
161
162 if(node->children) {
163 dav_print_node(stream, writef, nsmap, node->children);
164 ucx_fprintf(stream, writef, "</%s:%s>", prefix, node->name);
165 }
166 } else if(node->type == DAV_XML_TEXT) {
167 writef(node->content, 1, node->contentlength, stream);
168 }
169
170 node = node->next;
171 }
172 }
173
138 /* ------------------------- public API ------------------------- */ 174 /* ------------------------- public API ------------------------- */
139 175
140 char* dav_xml_getstring(DavXmlNode *node) { 176 char* dav_xml_getstring(DavXmlNode *node) {
141 if(node && node->type == DAV_XML_TEXT) { 177 if(node && node->type == DAV_XML_TEXT) {
142 return node->content; 178 return node->content;
161 newxn->content = content.ptr; 197 newxn->content = content.ptr;
162 newxn->contentlength = content.length; 198 newxn->contentlength = content.length;
163 return newxn; 199 return newxn;
164 } 200 }
165 201
202
203
204
205 DavXmlNode* dav_copy_node(DavXmlNode *node) {
206 DavXmlNode *ret = NULL;
207 DavXmlNode *prev = NULL;
208 while(node) {
209 DavXmlNode *copy = calloc(1, sizeof(DavXmlNode));
210 copy->type = node->type;
211 if(node->type == DAV_XML_ELEMENT) {
212 copy->namespace = strdup(node->namespace);
213 copy->name = strdup(node->name);
214 copy->children = dav_copy_node(node->children);
215 } else {
216 copy->contentlength = node->contentlength;
217 copy->content = malloc(node->contentlength+1);
218 memcpy(copy->content, node->content, node->contentlength);
219 copy->content[copy->contentlength] = 0;
220 }
221 if(!ret) {
222 ret = copy;
223 }
224 if(prev) {
225 prev->next = copy;
226 copy->prev = prev;
227 }
228 prev = copy;
229 node = node->next;
230 }
231 return ret;
232 }
233
234
235 DavXmlNode* dav_xml_createnode(const char *ns, const char *name) {
236 DavXmlNode *node = calloc(1, sizeof(DavXmlNode));
237 node->type = DAV_XML_ELEMENT;
238 node->namespace = strdup(ns);
239 node->name = strdup(name);
240 return node;
241 }
242
243 DavXmlNode* dav_xml_createnode_with_text(const char *ns, const char *name, const char *text) {
244 DavXmlNode *node = calloc(1, sizeof(DavXmlNode));
245 node->type = DAV_XML_ELEMENT;
246 node->namespace = strdup(ns);
247 node->name = strdup(name);
248
249 DavXmlNode *textnode = dav_xml_createtextnode(text);
250 node->children = textnode;
251
252 return node;
253 }
254
255 DavXmlNode* dav_xml_createtextnode(const char *text) {
256 DavXmlNode *node = calloc(1, sizeof(DavXmlNode));
257 node->type = DAV_XML_TEXT;
258 sstr_t content = sstrdup(sstr((char*)text));
259 node->content = content.ptr;
260 node->contentlength = content.length;
261 return node;
262 }
263
264 void dav_xml_add_child(DavXmlNode *node, DavXmlNode *child) {
265 DavXmlNode *last_child = NULL;
266 DavXmlNode *c = node->children;
267 while(c) {
268 last_child = c;
269 c = c->next;
270 }
271 if(last_child) {
272 last_child->next = child;
273 child->prev = last_child;
274 } else {
275 node->children = child;
276 }
277 }

mercurial