Sat, 30 Apr 2022 20:44:38 +0200
add basic pg proppatch implementation
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> |
318
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
35 | #include <ucx/buffer.h> |
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
|
36 | |
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 | #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
|
38 | |
211
2160585200ac
add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | #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
|
40 | |
232 | 41 | /***************************************************************************** |
42 | * Utility functions | |
43 | *****************************************************************************/ | |
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
|
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 | /* |
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 | * 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
|
47 | * 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
|
48 | */ |
232 | 49 | 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
|
50 | 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
|
51 | 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
|
52 | 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
|
53 | 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
|
54 | 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
|
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 | |
e4f3e1433098
add function for getting all namespace definitions that are required for an element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
224
diff
changeset
|
57 | |
232 | 58 | /***************************************************************************** |
59 | * Public functions | |
60 | *****************************************************************************/ | |
61 | ||
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
|
62 | /* ------------------------ 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
|
63 | |
223
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
64 | typedef struct StackElm { |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
65 | WSXmlNode *node; // list of nodes |
224
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
66 | //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
|
67 | int endonly; |
223
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
68 | struct StackElm *next; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
69 | } StackElm; |
211
2160585200ac
add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | |
223
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
71 | #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
|
72 | |
223
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
73 | int wsxml_iterator( |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
74 | pool_handle_t *pool, |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
75 | WSXmlNode *node, |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
76 | wsxml_func begincb, |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
77 | wsxml_func endcb, |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
78 | void *udata) |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
79 | { |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
80 | if(!node) { |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
81 | return 0; |
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 | |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
84 | StackElm *stack = pool_malloc(pool, sizeof(StackElm)); |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
85 | if(!stack) { |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
86 | return 1; // OOM |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
87 | } |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
88 | stack->next = NULL; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
89 | stack->node = node; |
224
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
90 | stack->endonly = 0; |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
91 | //stack->parent = NULL; |
223
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
92 | |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
93 | int ret = 0; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
94 | int br = 0; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
95 | while(stack) { |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
96 | StackElm *cur = stack; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
97 | WSXmlNode *xmlnode = cur->node; // get top stack element |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
98 | stack = cur->next; // and remove it |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
99 | cur->next = NULL; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
100 | |
224
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
101 | while(xmlnode && !cur->endonly) { |
223
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
102 | // element begin callback |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
103 | if(begincb(xmlnode, udata)) { |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
104 | br = 1; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
105 | 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
|
106 | } |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
107 | |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
108 | if(xmlnode->children) { |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
109 | // put the children on the stack |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
110 | // the next stack iteration will process the children |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
111 | StackElm *newelm = pool_malloc(pool, sizeof(StackElm)); |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
112 | if(!newelm) { |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
113 | ret = 1; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
114 | br = 1; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
115 | break; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
116 | } |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
117 | newelm->next = NULL; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
118 | newelm->node = xmlnode->children; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
119 | // 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
|
120 | // 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
|
121 | //newelm->parent = xmlnode; |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
122 | newelm->endonly = 0; |
223
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
123 | |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
124 | // 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
|
125 | // 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
|
126 | // stack |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
127 | // 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
|
128 | // children and the end tag are processed |
223
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
129 | if(xmlnode->next) { |
224
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
130 | StackElm *nextelm = pool_malloc(pool, sizeof(StackElm)); |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
131 | if(!nextelm) { |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
132 | ret = 1; |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
133 | br = 1; |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
134 | break; |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
135 | } |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
136 | nextelm->node = xmlnode->next; |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
137 | nextelm->next = NULL; |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
138 | nextelm->endonly = 0; |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
139 | STACK_PUSH(stack, nextelm); |
223
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
140 | } |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
141 | |
224
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
142 | // 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
|
143 | // 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
|
144 | // element, after all children are processed |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
145 | // reuse cur |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
146 | cur->node = xmlnode; |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
147 | cur->endonly = 1; |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
148 | STACK_PUSH(stack, cur); |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
149 | |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
150 | cur = NULL; |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
151 | |
223
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
152 | // now we can put the children on the stack |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
153 | STACK_PUSH(stack, newelm); |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
154 | // 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
|
155 | break; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
156 | } else { |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
157 | // 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
|
158 | // after the begin callback (no intermediate nodes) |
224
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
159 | cur->node = NULL; |
223
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
160 | if(endcb(xmlnode, udata)) { |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
161 | br = 1; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
162 | break; |
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 | |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
166 | // continue with next node at this level |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
167 | xmlnode = xmlnode->next; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
168 | } |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
169 | if(br) { |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
170 | break; // break because of an error |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
171 | } |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
172 | |
224
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
173 | if(cur && cur->node) { |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
174 | //xmlNode *endNode = cur->parent ? cur->parent : cur->node; |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
175 | xmlNode *endNode = cur->node; |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
176 | if(endcb(endNode, udata)) { |
0de1ec82628e
add test for xml iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
223
diff
changeset
|
177 | break; |
223
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
178 | } |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
179 | pool_free(pool, cur); |
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 | |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
183 | // free all remaining elements |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
184 | StackElm *elm = stack; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
185 | while(elm) { |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
186 | StackElm *next = elm->next; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
187 | pool_free(pool, elm); |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
188 | elm = next; |
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 | |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
191 | return ret; |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
192 | } |
bbaec8415c10
add xml tree iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
211
diff
changeset
|
193 | |
232 | 194 | /* ------------------- 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
|
195 | |
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 | 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
|
197 | 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
|
198 | 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
|
199 | 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
|
200 | 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
|
201 | } 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
|
202 | |
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 | 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
|
204 | 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
|
205 | // 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
|
206 | 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
|
207 | // 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
|
208 | // all namespaces in a map |
232 | 209 | 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
|
210 | 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
|
211 | 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
|
212 | 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
|
213 | } |
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 | 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
|
215 | 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
|
216 | 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
|
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 | |
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 | // 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
|
220 | // 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
|
221 | 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
|
222 | 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
|
223 | 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
|
224 | 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
|
225 | 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
|
226 | 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
|
227 | 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
|
228 | } |
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->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
|
230 | 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
|
231 | 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
|
232 | // 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
|
233 | 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
|
234 | 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
|
235 | 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
|
236 | } |
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 | 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
|
238 | |
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 | // 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
|
240 | 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
|
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 | } |
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 | 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
|
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 | |
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 | 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
|
247 | 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
|
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 | |
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 | 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
|
251 | 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
|
252 | 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
|
253 | 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
|
254 | { |
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 | 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
|
256 | |
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 | 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
|
258 | 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
|
259 | 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
|
260 | 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
|
261 | 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
|
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 | |
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 | 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
|
265 | 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
|
266 | 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
|
267 | 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
|
268 | |
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 | // 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
|
270 | // 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
|
271 | // 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
|
272 | 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
|
273 | 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
|
274 | 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
|
275 | 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
|
276 | } 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
|
277 | // 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
|
278 | // 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
|
279 | 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
|
280 | while(def) { |
232 | 281 | 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
|
282 | 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
|
283 | 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
|
284 | 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
|
285 | } |
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 | 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
|
287 | 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
|
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 | |
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 | // 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
|
291 | 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
|
292 | 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
|
293 | 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
|
294 | 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
|
295 | 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
|
296 | 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
|
297 | 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
|
298 | 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
|
299 | } |
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->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
|
301 | 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
|
302 | 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
|
303 | 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
|
304 | 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
|
305 | } 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
|
306 | 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
|
307 | } |
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 | 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
|
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 | |
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 | 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
|
313 | 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
|
314 | } |
232 | 315 | |
316 | ||
318
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
317 | static ssize_t buf_writefunc(void *buf, const void *s, size_t len) { |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
318 | int w = ucx_buffer_write(s, 1, len, buf); |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
319 | return w == 0 ? IO_ERROR : w; |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
320 | } |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
321 | |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
322 | WSXmlData* wsxml_node2data( |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
323 | pool_handle_t *pool, |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
324 | WSXmlNode *node) |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
325 | { |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
326 | UcxBuffer *buf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
327 | if(!buf) { |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
328 | return NULL; |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
329 | } |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
330 | |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
331 | int error = 0; |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
332 | WebdavNSList *nslist = wsxml_get_required_namespaces(pool, node, &error); |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
333 | if(error) { |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
334 | return NULL; |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
335 | } |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
336 | |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
337 | Writer writer; |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
338 | char buffer[512]; |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
339 | writer_init_with_stream(&writer, buf, buf_writefunc, buffer, 512); |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
340 | |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
341 | WSXmlData *data = NULL; |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
342 | if(!wsxml_write_nodes_without_nsdef(pool, &writer, node) && !writer_flush(&writer)) { |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
343 | data = pool_malloc(pool, sizeof(WSXmlData)); |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
344 | if(data) { |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
345 | data->data = pool_malloc(pool, buf->size + 1); |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
346 | if(data->data) { |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
347 | memcpy(data->data, buf->space, buf->size); |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
348 | data->data[buf->size] = '\0'; |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
349 | data->length = buf->size; |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
350 | data->namespaces = nslist; |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
351 | } |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
352 | } |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
353 | } |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
354 | |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
355 | ucx_buffer_free(buf); |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
356 | |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
357 | return data; |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
358 | } |
60870dbac94f
add basic pg proppatch implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
233
diff
changeset
|
359 | |
232 | 360 | /***************************************************************************** |
361 | * Non public functions | |
362 | *****************************************************************************/ | |
363 | ||
364 | typedef struct XmlWriter { | |
365 | /* | |
366 | * Memory pool for temp memory allocations | |
367 | */ | |
368 | pool_handle_t *pool; | |
369 | ||
370 | /* | |
371 | * Buffered output stream | |
372 | */ | |
373 | Writer *out; | |
374 | ||
375 | /* | |
376 | * Map for all previously defined namespaces | |
377 | * key: (char*) namespace prefix | |
378 | * value: WSNamespace* | |
379 | */ | |
380 | UcxMap *namespaces; | |
381 | ||
382 | /* | |
383 | * Should namespace definitions be created | |
384 | */ | |
385 | WSBool define_namespaces; | |
386 | } XmlWriter; | |
387 | ||
388 | /* | |
389 | * Serialize an XML text node | |
390 | * 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
|
391 | * type: 0 = element text, 1 = attribute text |
232 | 392 | */ |
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
|
393 | static void xml_ser_text(Writer *out, int type, const char *text) { |
232 | 394 | size_t start = 0; |
395 | size_t i; | |
396 | sstr_t entityref = { NULL, 0 }; | |
397 | 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
|
398 | 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
|
399 | 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
|
400 | entityref = S("&"); |
c5985d2fc19a
add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
232
diff
changeset
|
401 | } 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
|
402 | 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
|
403 | entityref = S("<"); |
c5985d2fc19a
add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
232
diff
changeset
|
404 | } 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
|
405 | entityref = S(">"); |
c5985d2fc19a
add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
232
diff
changeset
|
406 | } |
c5985d2fc19a
add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
232
diff
changeset
|
407 | } 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
|
408 | 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
|
409 | entityref = S("""); |
c5985d2fc19a
add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
232
diff
changeset
|
410 | } 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
|
411 | entityref = S("'"); |
c5985d2fc19a
add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
232
diff
changeset
|
412 | } |
232 | 413 | } |
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
|
414 | |
232 | 415 | if(entityref.ptr) { |
416 | size_t len = i-start; | |
417 | if(len > 0) { | |
418 | writer_put(out, text+start, len); | |
419 | } | |
420 | writer_puts(out, entityref); | |
421 | entityref.ptr = NULL; | |
422 | entityref.length = 0; | |
423 | start = i+1; | |
424 | } | |
425 | } | |
426 | size_t len = i-start; | |
427 | if(len > 0) { | |
428 | writer_put(out, text+start, len); | |
429 | } | |
430 | } | |
431 | ||
432 | /* | |
433 | * Serialize an XML element node | |
434 | */ | |
435 | static void xml_ser_element(XmlWriter *xw, xmlNode *node) { | |
436 | Writer *out = xw->out; | |
437 | writer_putc(out, '<'); | |
438 | ||
439 | // write prefix and ':' | |
440 | if(node->ns && node->ns->prefix) { | |
441 | writer_puts(out, sstr((char*)node->ns->prefix)); | |
442 | writer_putc(out, ':'); | |
443 | } | |
444 | ||
445 | // node name | |
446 | writer_puts(out, sstr((char*)node->name)); | |
447 | ||
448 | // namespace definitions | |
449 | if(xw->define_namespaces) { | |
450 | xmlNs *nsdef = node->nsDef; | |
451 | while(nsdef) { | |
452 | // we define only namespaces without prefix or namespaces | |
453 | // with prefix, that are not already defined | |
454 | // xw->namespaces contains all namespace, that were defined | |
455 | // before xml serialization | |
456 | if(!nsdef->prefix) { | |
457 | writer_puts(out, S(" xmlns=\"")); | |
458 | writer_puts(out, sstr((char*)nsdef->href)); | |
459 | writer_putc(out, '"'); | |
460 | } else { | |
461 | WSNamespace *n = xw->namespaces ? | |
462 | ucx_map_cstr_get(xw->namespaces, (char*)nsdef->prefix) : | |
463 | NULL; | |
464 | if(!n) { | |
465 | writer_puts(out, S(" xmlns:")); | |
466 | writer_puts(out, sstr((char*)nsdef->prefix)); | |
467 | writer_puts(out, S("=\"")); | |
468 | writer_puts(out, sstr((char*)nsdef->href)); | |
469 | writer_putc(out, '"'); | |
470 | } | |
471 | } | |
472 | ||
473 | nsdef = nsdef->next; | |
474 | } | |
475 | } | |
476 | ||
477 | // attributes | |
478 | xmlAttr *attr = node->properties; | |
479 | while(attr) { | |
480 | // format: ' [<prefix>:]<name>="<value>"' | |
481 | writer_putc(out, ' '); | |
482 | // optional namespace | |
483 | if(attr->ns && attr->ns->prefix) { | |
484 | writer_puts(out, sstr((char*)attr->ns->prefix)); | |
485 | writer_putc(out, ':'); | |
486 | } | |
487 | // <name>=" | |
488 | writer_puts(out, sstr((char*)attr->name)); | |
489 | writer_puts(out, S("=\"")); | |
490 | // value | |
491 | xmlNode *value = attr->children; | |
492 | while(value) { | |
493 | 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
|
494 | xml_ser_text(out, 1, (const char*)value->content); |
232 | 495 | } |
496 | value = value->next; | |
497 | } | |
498 | // trailing quote | |
499 | writer_putc(out, '"'); | |
500 | ||
501 | attr = attr->next; | |
502 | } | |
503 | ||
504 | if(node->children) { | |
505 | writer_putc(out, '>'); | |
506 | } else { | |
507 | writer_puts(out, S("/>")); | |
508 | } | |
509 | } | |
510 | ||
511 | static int xml_ser_node_begin(xmlNode *node, void *userdata) { | |
512 | XmlWriter *xw = userdata; | |
513 | switch(node->type) { | |
514 | case XML_ELEMENT_NODE: xml_ser_element(xw, node); break; | |
515 | case XML_ATTRIBUTE_NODE: break; | |
516 | 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
|
517 | xml_ser_text(xw->out, 0, (const char*)node->content); |
232 | 518 | break; |
519 | } | |
520 | case XML_CDATA_SECTION_NODE: { | |
521 | break; | |
522 | } | |
523 | case XML_ENTITY_REF_NODE: break; | |
524 | case XML_ENTITY_NODE: break; | |
525 | case XML_PI_NODE: break; | |
526 | case XML_COMMENT_NODE: break; | |
527 | case XML_DOCUMENT_NODE: break; | |
528 | case XML_DOCUMENT_TYPE_NODE: break; | |
529 | case XML_DOCUMENT_FRAG_NODE: break; | |
530 | case XML_NOTATION_NODE: break; | |
531 | case XML_HTML_DOCUMENT_NODE: break; | |
532 | case XML_DTD_NODE: break; | |
533 | case XML_ELEMENT_DECL: break; | |
534 | case XML_ATTRIBUTE_DECL: break; | |
535 | case XML_ENTITY_DECL: break; | |
536 | case XML_NAMESPACE_DECL: break; | |
537 | case XML_XINCLUDE_START: break; | |
538 | case XML_XINCLUDE_END: break; | |
539 | default: break; | |
540 | } | |
541 | return 0; | |
542 | } | |
543 | ||
544 | static int xml_ser_node_end(xmlNode *node, void *userdata) { | |
545 | XmlWriter *xw = userdata; | |
546 | Writer *out = xw->out; | |
547 | if(node->type == XML_ELEMENT_NODE) { | |
548 | if(node->children) { | |
549 | writer_puts(xw->out, S("</")); | |
550 | // write prefix and ':' | |
551 | if(node->ns && node->ns->prefix) { | |
552 | writer_puts(out, sstr((char*)node->ns->prefix)); | |
553 | writer_putc(out, ':'); | |
554 | } | |
555 | // name and close tag | |
556 | writer_puts(out, sstr((char*)node->name)); | |
557 | writer_putc(out, '>'); | |
558 | ||
559 | } // element was already closed in xml_ser_node_begin | |
560 | } | |
561 | return 0; | |
562 | } | |
563 | ||
564 | ||
565 | static int xml_write_nodes( | |
566 | pool_handle_t *pool, | |
567 | Writer *out, | |
568 | UcxMap *nsdefs, | |
569 | WSBool createdefs, | |
570 | xmlNode *node) | |
571 | { | |
572 | XmlWriter xmlwriter; | |
573 | xmlwriter.pool = pool; | |
574 | xmlwriter.out = out; | |
575 | xmlwriter.namespaces = nsdefs; | |
576 | xmlwriter.define_namespaces = createdefs; | |
577 | ||
578 | // iterate over xml nodes | |
579 | // this includes node->children and node->next | |
580 | int err = wsxml_iterator( | |
581 | pool, | |
582 | node, | |
583 | xml_ser_node_begin, | |
584 | xml_ser_node_end, | |
585 | &xmlwriter); | |
586 | if(err) { | |
587 | return -1; | |
588 | } | |
589 | ||
590 | return out->error; | |
591 | } | |
592 | ||
593 | int wsxml_write_nodes( | |
594 | pool_handle_t *pool, | |
595 | Writer *out, | |
596 | UcxMap *nsdefs, | |
597 | xmlNode *node) | |
598 | { | |
599 | return xml_write_nodes(pool, out, nsdefs, TRUE, node); | |
600 | } | |
601 | ||
602 | int wsxml_write_nodes_without_nsdef( | |
603 | pool_handle_t *pool, | |
604 | Writer *out, | |
605 | xmlNode *node) | |
606 | { | |
607 | return xml_write_nodes(pool, out, NULL, FALSE, node); | |
608 | } |