src/server/webdav/xml.c

Sat, 16 Apr 2022 14:36:08 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 16 Apr 2022 14:36:08 +0200
branch
webdav
changeset 288
7dd45173f68a
parent 233
c5985d2fc19a
child 318
60870dbac94f
permissions
-rw-r--r--

create pg test data

211
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2019 Olaf Wintermann. All rights reserved.
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
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
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
225
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
33 #include <ucx/string.h>
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
34 #include <ucx/map.h>
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
35
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
36 #include "../util/util.h"
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
37
211
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include "xml.h"
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
40 /*****************************************************************************
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
41 * Utility functions
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
42 *****************************************************************************/
225
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
43
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
44 /*
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
45 * generates a string key for an xml namespace
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
46 * format: prefix '\0' href
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
47 */
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
48 static sstr_t xml_namespace_key(UcxAllocator *a, WSNamespace *ns) {
225
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
49 sstr_t key = sstrcat_a(a, 3,
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
50 ns->prefix ? sstr((char*)ns->prefix) : S("\0"),
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
51 S("\0"),
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
52 sstr((char*)ns->href));
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
53 return key;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
54 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
55
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
56
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
57 /*****************************************************************************
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
58 * Public functions
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
59 *****************************************************************************/
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
60
225
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
61 /* ------------------------ wsxml_iterator ------------------------ */
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
62
223
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
63 typedef struct StackElm {
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
64 WSXmlNode *node; // list of nodes
224
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
65 //WSXmlNode *parent; // if not NULL, call endcb after node->next is NULL
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
66 int endonly;
223
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
67 struct StackElm *next;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
68 } StackElm;
211
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69
223
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
70 #define STACK_PUSH(stack, elm) if(stack) { elm->next = stack; } stack = elm;
211
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71
223
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
72 int wsxml_iterator(
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
73 pool_handle_t *pool,
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
74 WSXmlNode *node,
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
75 wsxml_func begincb,
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
76 wsxml_func endcb,
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
77 void *udata)
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
78 {
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
79 if(!node) {
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
80 return 0;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
81 }
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
82
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
83 StackElm *stack = pool_malloc(pool, sizeof(StackElm));
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
84 if(!stack) {
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
85 return 1; // OOM
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
86 }
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
87 stack->next = NULL;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
88 stack->node = node;
224
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
89 stack->endonly = 0;
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
90 //stack->parent = NULL;
223
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
91
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
92 int ret = 0;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
93 int br = 0;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
94 while(stack) {
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
95 StackElm *cur = stack;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
96 WSXmlNode *xmlnode = cur->node; // get top stack element
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
97 stack = cur->next; // and remove it
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
98 cur->next = NULL;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
99
224
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
100 while(xmlnode && !cur->endonly) {
223
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
101 // element begin callback
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
102 if(begincb(xmlnode, udata)) {
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
103 br = 1;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
104 break; // I don't like break with labels - is this wrong?
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
105 }
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
106
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
107 if(xmlnode->children) {
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
108 // put the children on the stack
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
109 // the next stack iteration will process the children
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
110 StackElm *newelm = pool_malloc(pool, sizeof(StackElm));
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
111 if(!newelm) {
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
112 ret = 1;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
113 br = 1;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
114 break;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
115 }
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
116 newelm->next = NULL;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
117 newelm->node = xmlnode->children;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
118 // setting the parent will make sure endcb will be called
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
119 // for the current xmlnode after all children are processed
224
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
120 //newelm->parent = xmlnode;
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
121 newelm->endonly = 0;
223
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
122
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
123 // if xmlnode->next is not NULL, there are still nodes at
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
124 // this level, therefore we have to put these also on the
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
125 // stack
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
126 // this way, the remaining nodes are processed after all
224
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
127 // children and the end tag are processed
223
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
128 if(xmlnode->next) {
224
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
129 StackElm *nextelm = pool_malloc(pool, sizeof(StackElm));
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
130 if(!nextelm) {
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
131 ret = 1;
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
132 br = 1;
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
133 break;
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
134 }
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
135 nextelm->node = xmlnode->next;
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
136 nextelm->next = NULL;
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
137 nextelm->endonly = 0;
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
138 STACK_PUSH(stack, nextelm);
223
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
139 }
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
140
224
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
141 // we have to put the end tag of the current element
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
142 // on the stack to ensure endcb is called for the current
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
143 // element, after all children are processed
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
144 // reuse cur
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
145 cur->node = xmlnode;
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
146 cur->endonly = 1;
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
147 STACK_PUSH(stack, cur);
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
148
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
149 cur = NULL;
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
150
223
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
151 // now we can put the children on the stack
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
152 STACK_PUSH(stack, newelm);
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
153 // break, because we don't want to process xmlnode->next now
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
154 break;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
155 } else {
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
156 // no children means, the end callback can be called directly
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
157 // after the begin callback (no intermediate nodes)
224
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
158 cur->node = NULL;
223
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
159 if(endcb(xmlnode, udata)) {
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
160 br = 1;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
161 break;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
162 }
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
163 }
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
164
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
165 // continue with next node at this level
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
166 xmlnode = xmlnode->next;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
167 }
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
168 if(br) {
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
169 break; // break because of an error
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
170 }
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
171
224
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
172 if(cur && cur->node) {
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
173 //xmlNode *endNode = cur->parent ? cur->parent : cur->node;
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
174 xmlNode *endNode = cur->node;
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
175 if(endcb(endNode, udata)) {
0de1ec82628e add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 223
diff changeset
176 break;
223
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
177 }
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
178 pool_free(pool, cur);
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
179 }
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
180 }
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
181
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
182 // free all remaining elements
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
183 StackElm *elm = stack;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
184 while(elm) {
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
185 StackElm *next = elm->next;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
186 pool_free(pool, elm);
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
187 elm = next;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
188 }
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
189
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
190 return ret;
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
191 }
bbaec8415c10 add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
192
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
193 /* ------------------- wsxml_get_required_namespaces ------------------- */
225
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
194
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
195 typedef struct WSNsCollector {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
196 UcxAllocator *a;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
197 UcxMap *nsmap;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
198 WebdavNSList *def;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
199 int error;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
200 } WSNsCollector;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
201
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
202 static int nslist_node_begin(xmlNode *node, void *userdata) {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
203 WSNsCollector *col = userdata;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
204 // namespace required for all elements
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
205 if(node->type == XML_ELEMENT_NODE && node->ns) {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
206 // we create a list of unique prefix-href namespaces by putting
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
207 // all namespaces in a map
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
208 sstr_t nskey = xml_namespace_key(col->a, node->ns);
225
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
209 if(!nskey.ptr) {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
210 col->error = 1;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
211 return 1;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
212 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
213 if(ucx_map_sstr_put(col->nsmap, nskey, node->ns)) {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
214 col->error = 1;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
215 return 1;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
216 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
217
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
218 // collect all namespace definitions for removing these namespaces
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
219 // from col->nsmap later
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
220 WSNamespace *def = node->nsDef;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
221 while(def) {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
222 WebdavNSList *newdef = col->a->malloc(
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
223 col->a->pool, sizeof(WebdavNSList));
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
224 if(!newdef) {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
225 col->error = 1;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
226 return 1;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
227 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
228 newdef->namespace = def;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
229 newdef->prev = NULL;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
230 newdef->next = NULL;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
231 // prepend newdef to the list
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
232 if(col->def) {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
233 newdef->next = col->def;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
234 col->def->prev = newdef;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
235 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
236 col->def = newdef;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
237
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
238 // continue with next namespace definition
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
239 def = def->next;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
240 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
241 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
242 return 0;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
243 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
244
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
245 static int nslist_node_end(xmlNode *node, void *userdata) {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
246 return 0;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
247 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
248
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
249 WebdavNSList* wsxml_get_required_namespaces(
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
250 pool_handle_t *pool,
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
251 WSXmlNode *node,
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
252 int *error)
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
253 {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
254 if(error) *error = 0;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
255
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
256 UcxAllocator a = util_pool_allocator(pool);
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
257 UcxMap *nsmap = ucx_map_new_a(&a, 16);
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
258 if(!nsmap) {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
259 if(error) *error = 1;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
260 return NULL;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
261 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
262
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
263 WSNsCollector col;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
264 col.a = &a;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
265 col.nsmap = nsmap;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
266 col.def = NULL;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
267
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
268 // iterate over all xml elements
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
269 // this will fill the hashmap with all namespaces
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
270 // all namespace definitions are added to col.def
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
271 WebdavNSList *list = NULL;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
272 WebdavNSList *end = NULL;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
273 if(wsxml_iterator(pool, node, nslist_node_begin, nslist_node_end, &col)) {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
274 if(error) *error = 1;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
275 } else {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
276 // remove all namespace definitions from the map
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
277 // what we get is a map that contains all missing namespace definitions
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
278 WebdavNSList *def = col.def;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
279 while(def) {
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
280 sstr_t nskey = xml_namespace_key(&a, def->namespace);
225
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
281 if(!nskey.ptr) {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
282 if(error) *error = 1;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
283 break;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
284 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
285 ucx_map_sstr_remove(nsmap, nskey);
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
286 def = def->next;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
287 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
288
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
289 // convert nsmap to a list
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
290 UcxMapIterator i = ucx_map_iterator(nsmap);
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
291 WSNamespace *ns;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
292 UCX_MAP_FOREACH(key, ns, i) {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
293 WebdavNSList *newelm = pool_malloc(pool, sizeof(WebdavNSList));
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
294 if(!newelm) {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
295 if(error) *error = 1;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
296 list = NULL;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
297 break;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
298 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
299 newelm->namespace = ns;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
300 newelm->next = NULL;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
301 newelm->prev = end; // NULL or the end of list
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
302 if(end) {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
303 end->next = newelm; // append new element
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
304 } else {
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
305 list = newelm; // start new list
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
306 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
307 end = newelm;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
308 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
309 }
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
310
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
311 ucx_map_free(nsmap);
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
312 return list;
e4f3e1433098 add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 224
diff changeset
313 }
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
314
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
315
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
316 /*****************************************************************************
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
317 * Non public functions
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
318 *****************************************************************************/
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
319
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
320 typedef struct XmlWriter {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
321 /*
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
322 * Memory pool for temp memory allocations
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
323 */
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
324 pool_handle_t *pool;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
325
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
326 /*
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
327 * Buffered output stream
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
328 */
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
329 Writer *out;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
330
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
331 /*
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
332 * Map for all previously defined namespaces
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
333 * key: (char*) namespace prefix
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
334 * value: WSNamespace*
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
335 */
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
336 UcxMap *namespaces;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
337
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
338 /*
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
339 * Should namespace definitions be created
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
340 */
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
341 WSBool define_namespaces;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
342 } XmlWriter;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
343
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
344 /*
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
345 * Serialize an XML text node
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
346 * This replaces some special characters with entity refs
233
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
347 * type: 0 = element text, 1 = attribute text
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
348 */
233
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
349 static void xml_ser_text(Writer *out, int type, const char *text) {
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
350 size_t start = 0;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
351 size_t i;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
352 sstr_t entityref = { NULL, 0 };
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
353 for(i=0;text[i]!='\0';i++) {
233
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
354 char c = text[i];
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
355 if(c == '&') {
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
356 entityref = S("&amp;");
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
357 } else if(type == 0) {
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
358 if(c == '<') {
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
359 entityref = S("&lt;");
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
360 } else if(c == '>') {
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
361 entityref = S("&gt;");
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
362 }
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
363 } else {
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
364 if(c == '\"') {
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
365 entityref = S("&quot;");
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
366 } else if(c == '\'') {
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
367 entityref = S("&apos;");
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
368 }
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
369 }
233
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
370
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
371 if(entityref.ptr) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
372 size_t len = i-start;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
373 if(len > 0) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
374 writer_put(out, text+start, len);
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
375 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
376 writer_puts(out, entityref);
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
377 entityref.ptr = NULL;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
378 entityref.length = 0;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
379 start = i+1;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
380 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
381 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
382 size_t len = i-start;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
383 if(len > 0) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
384 writer_put(out, text+start, len);
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
385 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
386 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
387
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
388 /*
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
389 * Serialize an XML element node
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
390 */
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
391 static void xml_ser_element(XmlWriter *xw, xmlNode *node) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
392 Writer *out = xw->out;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
393 writer_putc(out, '<');
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
394
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
395 // write prefix and ':'
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
396 if(node->ns && node->ns->prefix) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
397 writer_puts(out, sstr((char*)node->ns->prefix));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
398 writer_putc(out, ':');
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
399 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
400
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
401 // node name
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
402 writer_puts(out, sstr((char*)node->name));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
403
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
404 // namespace definitions
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
405 if(xw->define_namespaces) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
406 xmlNs *nsdef = node->nsDef;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
407 while(nsdef) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
408 // we define only namespaces without prefix or namespaces
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
409 // with prefix, that are not already defined
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
410 // xw->namespaces contains all namespace, that were defined
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
411 // before xml serialization
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
412 if(!nsdef->prefix) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
413 writer_puts(out, S(" xmlns=\""));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
414 writer_puts(out, sstr((char*)nsdef->href));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
415 writer_putc(out, '"');
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
416 } else {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
417 WSNamespace *n = xw->namespaces ?
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
418 ucx_map_cstr_get(xw->namespaces, (char*)nsdef->prefix) :
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
419 NULL;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
420 if(!n) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
421 writer_puts(out, S(" xmlns:"));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
422 writer_puts(out, sstr((char*)nsdef->prefix));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
423 writer_puts(out, S("=\""));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
424 writer_puts(out, sstr((char*)nsdef->href));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
425 writer_putc(out, '"');
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
426 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
427 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
428
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
429 nsdef = nsdef->next;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
430 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
431 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
432
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
433 // attributes
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
434 xmlAttr *attr = node->properties;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
435 while(attr) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
436 // format: ' [<prefix>:]<name>="<value>"'
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
437 writer_putc(out, ' ');
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
438 // optional namespace
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
439 if(attr->ns && attr->ns->prefix) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
440 writer_puts(out, sstr((char*)attr->ns->prefix));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
441 writer_putc(out, ':');
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
442 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
443 // <name>="
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
444 writer_puts(out, sstr((char*)attr->name));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
445 writer_puts(out, S("=\""));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
446 // value
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
447 xmlNode *value = attr->children;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
448 while(value) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
449 if(value->content) {
233
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
450 xml_ser_text(out, 1, (const char*)value->content);
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
451 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
452 value = value->next;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
453 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
454 // trailing quote
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
455 writer_putc(out, '"');
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
456
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
457 attr = attr->next;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
458 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
459
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
460 if(node->children) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
461 writer_putc(out, '>');
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
462 } else {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
463 writer_puts(out, S("/>"));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
464 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
465 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
466
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
467 static int xml_ser_node_begin(xmlNode *node, void *userdata) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
468 XmlWriter *xw = userdata;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
469 switch(node->type) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
470 case XML_ELEMENT_NODE: xml_ser_element(xw, node); break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
471 case XML_ATTRIBUTE_NODE: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
472 case XML_TEXT_NODE: {
233
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
473 xml_ser_text(xw->out, 0, (const char*)node->content);
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
474 break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
475 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
476 case XML_CDATA_SECTION_NODE: {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
477 break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
478 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
479 case XML_ENTITY_REF_NODE: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
480 case XML_ENTITY_NODE: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
481 case XML_PI_NODE: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
482 case XML_COMMENT_NODE: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
483 case XML_DOCUMENT_NODE: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
484 case XML_DOCUMENT_TYPE_NODE: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
485 case XML_DOCUMENT_FRAG_NODE: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
486 case XML_NOTATION_NODE: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
487 case XML_HTML_DOCUMENT_NODE: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
488 case XML_DTD_NODE: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
489 case XML_ELEMENT_DECL: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
490 case XML_ATTRIBUTE_DECL: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
491 case XML_ENTITY_DECL: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
492 case XML_NAMESPACE_DECL: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
493 case XML_XINCLUDE_START: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
494 case XML_XINCLUDE_END: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
495 default: break;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
496 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
497 return 0;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
498 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
499
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
500 static int xml_ser_node_end(xmlNode *node, void *userdata) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
501 XmlWriter *xw = userdata;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
502 Writer *out = xw->out;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
503 if(node->type == XML_ELEMENT_NODE) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
504 if(node->children) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
505 writer_puts(xw->out, S("</"));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
506 // write prefix and ':'
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
507 if(node->ns && node->ns->prefix) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
508 writer_puts(out, sstr((char*)node->ns->prefix));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
509 writer_putc(out, ':');
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
510 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
511 // name and close tag
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
512 writer_puts(out, sstr((char*)node->name));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
513 writer_putc(out, '>');
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
514
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
515 } // element was already closed in xml_ser_node_begin
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
516 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
517 return 0;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
518 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
519
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
520
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
521 static int xml_write_nodes(
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
522 pool_handle_t *pool,
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
523 Writer *out,
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
524 UcxMap *nsdefs,
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
525 WSBool createdefs,
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
526 xmlNode *node)
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
527 {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
528 XmlWriter xmlwriter;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
529 xmlwriter.pool = pool;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
530 xmlwriter.out = out;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
531 xmlwriter.namespaces = nsdefs;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
532 xmlwriter.define_namespaces = createdefs;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
533
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
534 // iterate over xml nodes
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
535 // this includes node->children and node->next
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
536 int err = wsxml_iterator(
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
537 pool,
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
538 node,
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
539 xml_ser_node_begin,
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
540 xml_ser_node_end,
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
541 &xmlwriter);
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
542 if(err) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
543 return -1;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
544 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
545
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
546 return out->error;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
547 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
548
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
549 int wsxml_write_nodes(
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
550 pool_handle_t *pool,
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
551 Writer *out,
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
552 UcxMap *nsdefs,
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
553 xmlNode *node)
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
554 {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
555 return xml_write_nodes(pool, out, nsdefs, TRUE, node);
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
556 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
557
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
558 int wsxml_write_nodes_without_nsdef(
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
559 pool_handle_t *pool,
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
560 Writer *out,
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
561 xmlNode *node)
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
562 {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
563 return xml_write_nodes(pool, out, NULL, FALSE, node);
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 225
diff changeset
564 }

mercurial