#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cx/printf.h>
#include <cx/hash_map.h>
#include <cx/buffer.h>
#include <libidav/xml.h>
#include "xml.h"
void property_xml2str(DavXmlNode *content,
const char *namespace, cxmutstr *out_xmlstr, cxmutstr *out_nsdef) {
CxBuffer buf;
cxBufferInit(&buf,
NULL,
2048, cxDefaultAllocator,
CX_BUFFER_AUTO_EXTEND);
CxMap *nsmap = cxHashMapCreate(cxDefaultAllocator,
CX_STORE_POINTERS,
16);
cxMapPut(nsmap, namespace, strdup(
"x0"));
dav_print_node(&buf, (cx_write_func)cxBufferWrite, nsmap, content);
cxmutstr ret = cx_mutstrn(buf.space, buf.size);
cxBufferPut(&buf,
0);
CxBuffer nsbuf;
cxBufferInit(&nsbuf,
NULL,
2048, cxDefaultAllocator,
CX_BUFFER_AUTO_EXTEND);
CxIterator i = cxMapIterator(nsmap);
int addSpace =
0;
cx_foreach(CxMapEntry *, entry, i) {
const char *ns = entry->key->data;
const char *pre = entry->value;
if(addSpace) {
cxBufferPut(&nsbuf,
' ');
}
else {
addSpace =
1;
}
cx_bprintf(&nsbuf,
"xmlns:%s=\"%s\"", pre, ns);
}
cxmutstr nsdef = cx_mutstrn(nsbuf.space, nsbuf.size);
cxBufferPut(&nsbuf,
0);
cxMapDestroy(nsmap);
*out_xmlstr = ret;
*out_nsdef = nsdef;
}