Sun, 19 Jan 2020 10:03:31 +0100
handle missing properties in multistatus.c
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 | 40 | /***************************************************************************** |
41 | * Utility functions | |
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 | 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 | 57 | /***************************************************************************** |
58 | * Public functions | |
59 | *****************************************************************************/ | |
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 | 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 | 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 | 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 | 314 | |
315 | ||
316 | /***************************************************************************** | |
317 | * Non public functions | |
318 | *****************************************************************************/ | |
319 | ||
320 | typedef struct XmlWriter { | |
321 | /* | |
322 | * Memory pool for temp memory allocations | |
323 | */ | |
324 | pool_handle_t *pool; | |
325 | ||
326 | /* | |
327 | * Buffered output stream | |
328 | */ | |
329 | Writer *out; | |
330 | ||
331 | /* | |
332 | * Map for all previously defined namespaces | |
333 | * key: (char*) namespace prefix | |
334 | * value: WSNamespace* | |
335 | */ | |
336 | UcxMap *namespaces; | |
337 | ||
338 | /* | |
339 | * Should namespace definitions be created | |
340 | */ | |
341 | WSBool define_namespaces; | |
342 | } XmlWriter; | |
343 | ||
344 | /* | |
345 | * Serialize an XML text node | |
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 | 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 | 350 | size_t start = 0; |
351 | size_t i; | |
352 | sstr_t entityref = { NULL, 0 }; | |
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("&"); |
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("<"); |
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(">"); |
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("""); |
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("'"); |
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 | 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 | 371 | if(entityref.ptr) { |
372 | size_t len = i-start; | |
373 | if(len > 0) { | |
374 | writer_put(out, text+start, len); | |
375 | } | |
376 | writer_puts(out, entityref); | |
377 | entityref.ptr = NULL; | |
378 | entityref.length = 0; | |
379 | start = i+1; | |
380 | } | |
381 | } | |
382 | size_t len = i-start; | |
383 | if(len > 0) { | |
384 | writer_put(out, text+start, len); | |
385 | } | |
386 | } | |
387 | ||
388 | /* | |
389 | * Serialize an XML element node | |
390 | */ | |
391 | static void xml_ser_element(XmlWriter *xw, xmlNode *node) { | |
392 | Writer *out = xw->out; | |
393 | writer_putc(out, '<'); | |
394 | ||
395 | // write prefix and ':' | |
396 | if(node->ns && node->ns->prefix) { | |
397 | writer_puts(out, sstr((char*)node->ns->prefix)); | |
398 | writer_putc(out, ':'); | |
399 | } | |
400 | ||
401 | // node name | |
402 | writer_puts(out, sstr((char*)node->name)); | |
403 | ||
404 | // namespace definitions | |
405 | if(xw->define_namespaces) { | |
406 | xmlNs *nsdef = node->nsDef; | |
407 | while(nsdef) { | |
408 | // we define only namespaces without prefix or namespaces | |
409 | // with prefix, that are not already defined | |
410 | // xw->namespaces contains all namespace, that were defined | |
411 | // before xml serialization | |
412 | if(!nsdef->prefix) { | |
413 | writer_puts(out, S(" xmlns=\"")); | |
414 | writer_puts(out, sstr((char*)nsdef->href)); | |
415 | writer_putc(out, '"'); | |
416 | } else { | |
417 | WSNamespace *n = xw->namespaces ? | |
418 | ucx_map_cstr_get(xw->namespaces, (char*)nsdef->prefix) : | |
419 | NULL; | |
420 | if(!n) { | |
421 | writer_puts(out, S(" xmlns:")); | |
422 | writer_puts(out, sstr((char*)nsdef->prefix)); | |
423 | writer_puts(out, S("=\"")); | |
424 | writer_puts(out, sstr((char*)nsdef->href)); | |
425 | writer_putc(out, '"'); | |
426 | } | |
427 | } | |
428 | ||
429 | nsdef = nsdef->next; | |
430 | } | |
431 | } | |
432 | ||
433 | // attributes | |
434 | xmlAttr *attr = node->properties; | |
435 | while(attr) { | |
436 | // format: ' [<prefix>:]<name>="<value>"' | |
437 | writer_putc(out, ' '); | |
438 | // optional namespace | |
439 | if(attr->ns && attr->ns->prefix) { | |
440 | writer_puts(out, sstr((char*)attr->ns->prefix)); | |
441 | writer_putc(out, ':'); | |
442 | } | |
443 | // <name>=" | |
444 | writer_puts(out, sstr((char*)attr->name)); | |
445 | writer_puts(out, S("=\"")); | |
446 | // value | |
447 | xmlNode *value = attr->children; | |
448 | while(value) { | |
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 | 451 | } |
452 | value = value->next; | |
453 | } | |
454 | // trailing quote | |
455 | writer_putc(out, '"'); | |
456 | ||
457 | attr = attr->next; | |
458 | } | |
459 | ||
460 | if(node->children) { | |
461 | writer_putc(out, '>'); | |
462 | } else { | |
463 | writer_puts(out, S("/>")); | |
464 | } | |
465 | } | |
466 | ||
467 | static int xml_ser_node_begin(xmlNode *node, void *userdata) { | |
468 | XmlWriter *xw = userdata; | |
469 | switch(node->type) { | |
470 | case XML_ELEMENT_NODE: xml_ser_element(xw, node); break; | |
471 | case XML_ATTRIBUTE_NODE: break; | |
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 | 474 | break; |
475 | } | |
476 | case XML_CDATA_SECTION_NODE: { | |
477 | break; | |
478 | } | |
479 | case XML_ENTITY_REF_NODE: break; | |
480 | case XML_ENTITY_NODE: break; | |
481 | case XML_PI_NODE: break; | |
482 | case XML_COMMENT_NODE: break; | |
483 | case XML_DOCUMENT_NODE: break; | |
484 | case XML_DOCUMENT_TYPE_NODE: break; | |
485 | case XML_DOCUMENT_FRAG_NODE: break; | |
486 | case XML_NOTATION_NODE: break; | |
487 | case XML_HTML_DOCUMENT_NODE: break; | |
488 | case XML_DTD_NODE: break; | |
489 | case XML_ELEMENT_DECL: break; | |
490 | case XML_ATTRIBUTE_DECL: break; | |
491 | case XML_ENTITY_DECL: break; | |
492 | case XML_NAMESPACE_DECL: break; | |
493 | case XML_XINCLUDE_START: break; | |
494 | case XML_XINCLUDE_END: break; | |
495 | default: break; | |
496 | } | |
497 | return 0; | |
498 | } | |
499 | ||
500 | static int xml_ser_node_end(xmlNode *node, void *userdata) { | |
501 | XmlWriter *xw = userdata; | |
502 | Writer *out = xw->out; | |
503 | if(node->type == XML_ELEMENT_NODE) { | |
504 | if(node->children) { | |
505 | writer_puts(xw->out, S("</")); | |
506 | // write prefix and ':' | |
507 | if(node->ns && node->ns->prefix) { | |
508 | writer_puts(out, sstr((char*)node->ns->prefix)); | |
509 | writer_putc(out, ':'); | |
510 | } | |
511 | // name and close tag | |
512 | writer_puts(out, sstr((char*)node->name)); | |
513 | writer_putc(out, '>'); | |
514 | ||
515 | } // element was already closed in xml_ser_node_begin | |
516 | } | |
517 | return 0; | |
518 | } | |
519 | ||
520 | ||
521 | static int xml_write_nodes( | |
522 | pool_handle_t *pool, | |
523 | Writer *out, | |
524 | UcxMap *nsdefs, | |
525 | WSBool createdefs, | |
526 | xmlNode *node) | |
527 | { | |
528 | XmlWriter xmlwriter; | |
529 | xmlwriter.pool = pool; | |
530 | xmlwriter.out = out; | |
531 | xmlwriter.namespaces = nsdefs; | |
532 | xmlwriter.define_namespaces = createdefs; | |
533 | ||
534 | // iterate over xml nodes | |
535 | // this includes node->children and node->next | |
536 | int err = wsxml_iterator( | |
537 | pool, | |
538 | node, | |
539 | xml_ser_node_begin, | |
540 | xml_ser_node_end, | |
541 | &xmlwriter); | |
542 | if(err) { | |
543 | return -1; | |
544 | } | |
545 | ||
546 | return out->error; | |
547 | } | |
548 | ||
549 | int wsxml_write_nodes( | |
550 | pool_handle_t *pool, | |
551 | Writer *out, | |
552 | UcxMap *nsdefs, | |
553 | xmlNode *node) | |
554 | { | |
555 | return xml_write_nodes(pool, out, nsdefs, TRUE, node); | |
556 | } | |
557 | ||
558 | int wsxml_write_nodes_without_nsdef( | |
559 | pool_handle_t *pool, | |
560 | Writer *out, | |
561 | xmlNode *node) | |
562 | { | |
563 | return xml_write_nodes(pool, out, NULL, FALSE, node); | |
564 | } |