libidav/xml.c

Sat, 12 Oct 2019 13:42:02 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 12 Oct 2019 13:42:02 +0200
changeset 655
4d33b672c33a
parent 588
0cfe006fcad2
child 666
97061483d06e
permissions
-rw-r--r--

add edit-user command

331
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
404
5c08b8e14df8 updates copyright notice
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 373
diff changeset
4 * Copyright 2018 Olaf Wintermann. All rights reserved.
331
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
33 #include <ucx/utils.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
34
331
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "xml.h"
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 static DavXmlNodeType convert_type(xmlElementType type) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 DavXmlNodeType ct;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 switch(type) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 default: ct = DAV_XML_NONE; break;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 case XML_ELEMENT_NODE: ct = DAV_XML_ELEMENT; break;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 case XML_TEXT_NODE: ct = DAV_XML_TEXT;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 return ct;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 typedef struct {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 xmlNode *node;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 DavXmlNode *parent;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 } ConvXmlElm;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 DavXmlNode* dav_convert_xml(DavSession *sn, xmlNode *node) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 if(!node) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 return NULL;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 DavXmlNodeType newnt = convert_type(node->type);
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 if(newnt == DAV_XML_NONE) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 return NULL;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 UcxMempool *mp = sn->mp;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 ConvXmlElm *ce = malloc(sizeof(ConvXmlElm));
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 ce->node = node;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 ce->parent = NULL;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 UcxList *stack = ucx_list_prepend(NULL, ce);
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 DavXmlNode *ret = NULL;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 while(stack) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 ConvXmlElm *c = stack->data;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 stack = ucx_list_remove(stack, stack);
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 xmlNode *n = c->node;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 DavXmlNode *prev = NULL;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 while(n) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 DavXmlNode *newxn = ucx_mempool_calloc(mp, 1, sizeof(DavXmlNode));
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 if(!ret) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 ret = newxn;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 newxn->type = convert_type(n->type);
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 newxn->parent = c->parent;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 if(c->parent && !c->parent->children) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 c->parent->children = newxn;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 newxn->prev = prev;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 if(prev) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 prev->next = newxn;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 if(newxn->type == DAV_XML_ELEMENT) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 newxn->name = dav_session_strdup(sn, (char*)n->name);
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 if(n->ns && n->ns->href) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 newxn->namespace = dav_session_strdup(sn, (char*)n->ns->href);
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96
520
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
97 xmlAttr *attr = n->properties;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
98 DavXmlAttr *newattr = NULL;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
99 DavXmlAttr *newattr_last = NULL;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
100 while(attr) {
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
101 DavXmlAttr *na = ucx_mempool_calloc(mp, 1, sizeof(DavXmlAttr));
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
102 na->name = dav_session_strdup(sn, (char*)attr->name);
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
103 if(attr->children && attr->children->type == XML_TEXT_NODE) {
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
104 na->value = dav_session_strdup(sn, (char*)attr->children->content);
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
105 }
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
106 if(!newattr) {
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
107 newattr = na;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
108 } else {
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
109 newattr_last->next = na;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
110 }
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
111 newattr_last = na;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
112
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
113 attr = attr->next;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
114 }
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
115 newxn->attributes = newattr;
331
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 if(n->children) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 ConvXmlElm *convc = malloc(sizeof(ConvXmlElm));
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 convc->node = n->children;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 convc->parent = newxn;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 stack = ucx_list_prepend(stack, convc);
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 } else if(newxn->type == DAV_XML_TEXT) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 sstr_t content = sstrdup_a(mp->allocator, sstr((char*)n->content));
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 newxn->content = content.ptr;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 newxn->contentlength = content.length;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 prev = newxn;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 n = n->next;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 free(c);
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 return ret;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 void dav_print_xml(DavXmlNode *node) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 if(node->type == DAV_XML_ELEMENT) {
520
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
141 printf("<%s", node->name);
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
142 DavXmlAttr *attr = node->attributes;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
143 while(attr) {
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
144 printf(" %s=\"%s\"", attr->name, attr->value);
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
145 attr = attr->next;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
146 }
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
147 putchar('>');
331
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 DavXmlNode *child = node->children;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 if(child) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 dav_print_xml(child);
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 printf("</%s>", node->name);
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 } else {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 fwrite(node->content, 1, node->contentlength, stdout);
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 fflush(stdout);
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 if(node->next) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 dav_print_xml(node->next);
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
164 void dav_print_node(void *stream, write_func writef, UcxMap *nsmap, DavXmlNode *node) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
165 while(node) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
166 if(node->type == DAV_XML_ELEMENT) {
520
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
167 char *tagend = node->children ? ">" : " />";
371
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
168 char *prefix = NULL;
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
169 if(node->namespace) {
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
170 prefix = ucx_map_cstr_get(nsmap, node->namespace);
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
171 if(!prefix) {
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
172 sstr_t newpre = ucx_sprintf("x%d", (int)nsmap->count);
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
173 ucx_map_cstr_put(nsmap, node->namespace, newpre.ptr);
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
174 prefix = newpre.ptr;
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
175 ucx_fprintf(
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
176 stream,
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
177 writef,
588
0cfe006fcad2 fix dav_print_node
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 532
diff changeset
178 "<%s:%s xmlns:%s=\"%s\"",
371
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
179 prefix,
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
180 node->name,
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
181 prefix,
588
0cfe006fcad2 fix dav_print_node
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 532
diff changeset
182 node->namespace);
371
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
183 } else {
520
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
184 ucx_fprintf(stream, writef, "<%s:%s", prefix, node->name);
371
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
185 }
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
186 } else {
520
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
187 ucx_fprintf(stream, writef, "<%s", node->name);
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
188 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
189
520
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
190 DavXmlAttr *attr = node->attributes;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
191 while(attr) {
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
192 ucx_fprintf(stream, writef, " %s=\"%s\"", attr->name, attr->value);
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
193 attr = attr->next;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
194 }
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
195 writef(tagend, 1, strlen(tagend), stream); // end xml tag
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
196
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
197 if(node->children) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
198 dav_print_node(stream, writef, nsmap, node->children);
371
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
199 if(prefix) {
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
200 ucx_fprintf(stream, writef, "</%s:%s>", prefix, node->name);
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
201 } else {
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
202 ucx_fprintf(stream, writef, "</%s>", node->name);
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
203 }
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
204 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
205 } else if(node->type == DAV_XML_TEXT) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
206 writef(node->content, 1, node->contentlength, stream);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
207 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
208
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
209 node = node->next;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
210 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
211 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
212
331
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 /* ------------------------- public API ------------------------- */
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 char* dav_xml_getstring(DavXmlNode *node) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 if(node && node->type == DAV_XML_TEXT) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 return node->content;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 } else {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 return NULL;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222
338
c7f3fe4abdb2 dav get-property supports xml property output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 331
diff changeset
223 DavBool dav_xml_isstring(DavXmlNode *node) {
c7f3fe4abdb2 dav get-property supports xml property output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 331
diff changeset
224 if(node && node->type == DAV_XML_TEXT && !node->next) {
c7f3fe4abdb2 dav get-property supports xml property output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 331
diff changeset
225 return TRUE;
c7f3fe4abdb2 dav get-property supports xml property output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 331
diff changeset
226 } else {
c7f3fe4abdb2 dav get-property supports xml property output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 331
diff changeset
227 return FALSE;
c7f3fe4abdb2 dav get-property supports xml property output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 331
diff changeset
228 }
c7f3fe4abdb2 dav get-property supports xml property output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 331
diff changeset
229 }
c7f3fe4abdb2 dav get-property supports xml property output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 331
diff changeset
230
373
dcc03142eb5f dav info prints simplified xml values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 371
diff changeset
231 DavXmlNode* dav_xml_nextelm(DavXmlNode *node) {
dcc03142eb5f dav info prints simplified xml values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 371
diff changeset
232 node = node->next;
dcc03142eb5f dav info prints simplified xml values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 371
diff changeset
233 while(node) {
dcc03142eb5f dav info prints simplified xml values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 371
diff changeset
234 if(node->type == DAV_XML_ELEMENT) {
dcc03142eb5f dav info prints simplified xml values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 371
diff changeset
235 return node;
dcc03142eb5f dav info prints simplified xml values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 371
diff changeset
236 }
dcc03142eb5f dav info prints simplified xml values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 371
diff changeset
237 node = node->next;
dcc03142eb5f dav info prints simplified xml values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 371
diff changeset
238 }
dcc03142eb5f dav info prints simplified xml values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 371
diff changeset
239 return NULL;
dcc03142eb5f dav info prints simplified xml values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 371
diff changeset
240 }
dcc03142eb5f dav info prints simplified xml values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 371
diff changeset
241
331
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 DavXmlNode* dav_text_node(DavSession *sn, char *text) {
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 UcxMempool *mp = sn->mp;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 DavXmlNode *newxn = ucx_mempool_calloc(mp, 1, sizeof(DavXmlNode));
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 newxn->type = DAV_XML_TEXT;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 sstr_t content = sstrdup_a(mp->allocator, sstr(text));
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 newxn->content = content.ptr;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 newxn->contentlength = content.length;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 return newxn;
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 }
9ca1e4706acc adds initial support for xml properties in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
252
532
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
253 DavXmlAttr* dav_copy_xml_attr(DavXmlAttr *attr) {
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
254 if(!attr) {
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
255 return NULL;
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
256 }
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
257 DavXmlAttr *newattr = NULL;
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
258 DavXmlAttr *prev = NULL;
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
259 while(attr) {
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
260 DavXmlAttr *n = calloc(1, sizeof(DavXmlAttr));
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
261 n->name = strdup(attr->name);
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
262 n->value = strdup(attr->value);
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
263 if(prev) {
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
264 prev->next = n;
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
265 } else {
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
266 newattr = n;
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
267 }
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
268 prev = n;
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
269 attr = attr->next;
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
270 }
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
271 return newattr;
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
272 }
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
273
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
274 DavXmlNode* dav_copy_node(DavXmlNode *node) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
275 DavXmlNode *ret = NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
276 DavXmlNode *prev = NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
277 while(node) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
278 DavXmlNode *copy = calloc(1, sizeof(DavXmlNode));
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
279 copy->type = node->type;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
280 if(node->type == DAV_XML_ELEMENT) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
281 copy->namespace = strdup(node->namespace);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
282 copy->name = strdup(node->name);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
283 copy->children = dav_copy_node(node->children);
532
aeda47714978 fixes dav_copy_node and improves dav-sync restore
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 526
diff changeset
284 copy->attributes = dav_copy_xml_attr(node->attributes);
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
285 } else {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
286 copy->contentlength = node->contentlength;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
287 copy->content = malloc(node->contentlength+1);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
288 memcpy(copy->content, node->content, node->contentlength);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
289 copy->content[copy->contentlength] = 0;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
290 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
291 if(!ret) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
292 ret = copy;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
293 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
294 if(prev) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
295 prev->next = copy;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
296 copy->prev = prev;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
297 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
298 prev = copy;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
299 node = node->next;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
300 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
301 return ret;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
302 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
303
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
304
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
305 DavXmlNode* dav_xml_createnode(const char *ns, const char *name) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
306 DavXmlNode *node = calloc(1, sizeof(DavXmlNode));
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
307 node->type = DAV_XML_ELEMENT;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
308 node->namespace = strdup(ns);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
309 node->name = strdup(name);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
310 return node;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
311 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
312
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
313 DavXmlNode* dav_xml_createnode_with_text(const char *ns, const char *name, const char *text) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
314 DavXmlNode *node = calloc(1, sizeof(DavXmlNode));
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
315 node->type = DAV_XML_ELEMENT;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
316 node->namespace = strdup(ns);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
317 node->name = strdup(name);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
318
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
319 DavXmlNode *textnode = dav_xml_createtextnode(text);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
320 node->children = textnode;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
321
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
322 return node;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
323 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
324
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
325 DavXmlNode* dav_xml_createtextnode(const char *text) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
326 DavXmlNode *node = calloc(1, sizeof(DavXmlNode));
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
327 node->type = DAV_XML_TEXT;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
328 sstr_t content = sstrdup(sstr((char*)text));
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
329 node->content = content.ptr;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
330 node->contentlength = content.length;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
331 return node;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
332 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
333
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
334 void dav_xml_add_child(DavXmlNode *node, DavXmlNode *child) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
335 DavXmlNode *last_child = NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
336 DavXmlNode *c = node->children;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
337 while(c) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
338 last_child = c;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
339 c = c->next;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
340 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
341 if(last_child) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
342 last_child->next = child;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
343 child->prev = last_child;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
344 } else {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
345 node->children = child;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
346 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 338
diff changeset
347 }
371
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
348
520
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
349 void dav_xml_add_attr(DavXmlNode *node, const char *name, const char *value) {
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
350 DavXmlAttr *attr = calloc(1, sizeof(DavXmlAttr));
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
351 attr->name = strdup(name);
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
352 attr->value = strdup(value);
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
353
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
354 if(node->attributes) {
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
355 DavXmlAttr *last;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
356 DavXmlAttr *end = node->attributes;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
357 while(end) {
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
358 last = end;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
359 end = end->next;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
360 }
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
361 last->next = attr;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
362 } else {
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
363 node->attributes = attr;
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
364 }
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
365 }
da2b0cc44e4f adds xml attribute support and xattr property
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
366
526
e3c0440bd599 adds metadata support to dav-sync pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 520
diff changeset
367 char* dav_xml_get_attr(DavXmlNode *node, const char *name) {
e3c0440bd599 adds metadata support to dav-sync pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 520
diff changeset
368 DavXmlAttr *attr = node->attributes;
e3c0440bd599 adds metadata support to dav-sync pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 520
diff changeset
369 while(attr) {
e3c0440bd599 adds metadata support to dav-sync pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 520
diff changeset
370 if(!strcmp(attr->name, name)) {
e3c0440bd599 adds metadata support to dav-sync pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 520
diff changeset
371 return attr->value;
e3c0440bd599 adds metadata support to dav-sync pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 520
diff changeset
372 }
e3c0440bd599 adds metadata support to dav-sync pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 520
diff changeset
373
e3c0440bd599 adds metadata support to dav-sync pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 520
diff changeset
374 attr = attr->next;
e3c0440bd599 adds metadata support to dav-sync pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 520
diff changeset
375 }
e3c0440bd599 adds metadata support to dav-sync pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 520
diff changeset
376 return NULL;
e3c0440bd599 adds metadata support to dav-sync pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 520
diff changeset
377 }
e3c0440bd599 adds metadata support to dav-sync pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 520
diff changeset
378
371
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
379 DavXmlNode* dav_parse_xml(DavSession *sn, const char *str, size_t len) {
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
380 xmlDoc *doc = xmlReadMemory(str, len, NULL, NULL, 0);
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
381 if(!doc) {
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
382 return NULL;
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
383 }
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
384 xmlNode *xml_root = xmlDocGetRootElement(doc);
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
385 if(!xml_root) {
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
386 xmlFreeDoc(doc);
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
387 return NULL;
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
388 }
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
389 DavXmlNode *x = dav_convert_xml(sn, xml_root);
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
390 xmlFreeDoc(doc);
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
391 return x;
604e7e335b3b adds support for xml properties in set-property command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
392 }

mercurial