-
Notifications
You must be signed in to change notification settings - Fork 0
/
konoco_sax_namespace_handler.c
288 lines (243 loc) · 8.51 KB
/
konoco_sax_namespace_handler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* konoco_sax_namespace_handler.c
* KonocoXML
*
* Created by Tobias Kräntzer <[email protected]> on 29.03.09.
* Copyright 2009 Konoco. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY Tobias Kräntzer ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Tobias Kräntzer BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "konoco_sax_namespace_handler.h"
#include "konoco_sax_handle.h"
#include "konoco_sax.h"
#include "konoco_namespace_resolver.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#pragma mark Split Prefix
typedef struct _qname {
konoco_buffer prefix;
konoco_buffer name;
} qname;
int
split_prefix(qname * result, konoco_buffer * input)
{
void * split_position = memchr(input->data, ':', input->length);
if (split_position == 0) {
result->prefix.length = 0;
result->name.data = input->data;
result->name.length = input->length;
return (0);
} else {
result->prefix.data = input->data;
result->prefix.length = (unsigned char *)split_position - input->data;
result->name.data = (unsigned char *)split_position + 1;
result->name.length = input->length - 1 - ((unsigned char *)split_position - input->data);
return (1);
};
}
void
append_attribute(parser_handle * parser, konoco_buffer * prefix, konoco_buffer * name, konoco_buffer * value)
{
attribute_list * attr = malloc(sizeof(attribute_list));
assert(attr);
konoco_buffer_init_cpy(&attr->prefix, prefix);
konoco_buffer_init_cpy(&attr->name, name);
konoco_buffer_init_cpy(&attr->value, value);
attr->next = parser->ns_delegate_data.attributes;
parser->ns_delegate_data.attributes = attr;
}
void
free_attribute_list(attribute_list * item)
{
if (item->next)
free_attribute_list(item->next);
konoco_buffer_free(&item->prefix);
konoco_buffer_free(&item->name);
konoco_buffer_free(&item->value);
free(item);
}
void
free_attributes(parser_handle * parser)
{
if (parser->ns_delegate_data.attributes == 0)
return;
free_attribute_list(parser->ns_delegate_data.attributes);
parser->ns_delegate_data.attributes = 0;
}
void
call_cb_for_attr_item(parser_handle * parser, attribute_list * item, konoco_sax_delegate * delegate)
{
konoco_buffer * ns = konoco_namespace_resolver_get(parser->ns_resolver, &item->prefix);
(delegate->attribute)(parser,
delegate->data,
&item->name,
ns,
&item->value);
if (item->next)
call_cb_for_attr_item(parser, item->next, delegate);
}
#pragma mark Callbacks for the NS Delegate
void
ns_delegate_start_document(void * parser, void * data)
{
ns_delegate_data * ns_data = (ns_delegate_data *)data;
if (ns_data->orig_delegate->start_document) {
(ns_data->orig_delegate->start_document)(parser,
ns_data->orig_delegate->data);
};
}
void
ns_delegate_end_document(void * parser, void * data)
{
ns_delegate_data * ns_data = (ns_delegate_data *)data;
if (ns_data->orig_delegate->end_document) {
(ns_data->orig_delegate->end_document)(parser,
ns_data->orig_delegate->data);
};
}
void
ns_delegate_characters(void * parser, void * data, konoco_buffer * text)
{
ns_delegate_data * ns_data = (ns_delegate_data *)data;
if (ns_data->orig_delegate->characters) {
(ns_data->orig_delegate->characters)(parser,
ns_data->orig_delegate->data,
text);
};
}
void
ns_delegate_error(void * parser, void * data, const char * msg)
{
ns_delegate_data * ns_data = (ns_delegate_data *)data;
if (ns_data->orig_delegate->error) {
(ns_data->orig_delegate->error)(parser,
ns_data->orig_delegate->data,
msg);
};
}
#pragma mark Special Callback Function for NS Handling
void
ns_delegate_start_element(void * p, void * data, konoco_buffer * name, konoco_buffer * namespace)
{
parser_handle * parser = (parser_handle *)p;
ns_delegate_data * ns_data = (ns_delegate_data *)data;
parser->ns_resolver = konoco_namespace_resolver_push(parser->ns_resolver);
qname qname;
split_prefix(&qname, name);
konoco_buffer_cpy(&ns_data->element_prefix, &qname.prefix);
konoco_buffer_cpy(&ns_data->element_name, &qname.name);
// split qname
// set prefix and name to ns_delegate_data
}
void
ns_delegate_end_element(void * p, void * data, konoco_buffer * name, konoco_buffer * namespace)
{
parser_handle * parser = (parser_handle *)p;
ns_delegate_data * ns_data = (ns_delegate_data *)data;
// call delegate
if (ns_data->orig_delegate->end_element) {
// split qname
qname qname;
split_prefix(&qname, name);
// lookup ns
konoco_buffer * ns = konoco_namespace_resolver_get(parser->ns_resolver, &qname.prefix);
(ns_data->orig_delegate->end_element)(parser,
ns_data->orig_delegate->data,
&qname.name,
ns);
};
// pop ns resolver
parser->ns_resolver = konoco_namespace_resolver_pop(parser->ns_resolver);
}
void
ns_delegate_attribute(void * p, void * data, konoco_buffer * name, konoco_buffer * namespace, konoco_buffer * value)
{
parser_handle * parser = (parser_handle *)p;
ns_delegate_data * ns_data = (ns_delegate_data *)data;
qname qname;
if (split_prefix(&qname, name)){
if (konoco_buffer_eq_str(&qname.prefix, "xmlns")) {
assert(parser->ns_resolver);
konoco_namespace_resolver_set(parser->ns_resolver, &qname.name, value);
if (ns_data->orig_delegate->xmlns) {
(ns_data->orig_delegate->xmlns)(parser,
ns_data->orig_delegate->data,
&qname.name,
value);
};
} else {
append_attribute(parser, &qname.prefix, &qname.name, value);
}
} else {
if (konoco_buffer_eq_str(&qname.name, "xmlns")) {
assert(parser->ns_resolver);
konoco_namespace_resolver_set(parser->ns_resolver, &qname.prefix, value);
if (ns_data->orig_delegate->xmlns) {
(ns_data->orig_delegate->xmlns)(parser,
ns_data->orig_delegate->data,
&qname.prefix,
value);
};
} else {
append_attribute(parser, &qname.prefix, &qname.name, value);
};
};
// split qname
// if prefix is not 'xmlns', append attribute at the list
// else set prefix/namespace pair
}
void
ns_delegate_flush(void * p, void * data)
{
parser_handle * parser = (parser_handle *)p;
ns_delegate_data * ns_data = (ns_delegate_data *)data;
if (ns_data->orig_delegate->start_element) {
konoco_buffer * ns = konoco_namespace_resolver_get(parser->ns_resolver, &ns_data->element_prefix);
(ns_data->orig_delegate->start_element)(parser,
ns_data->orig_delegate->data,
&ns_data->element_name,
ns);
};
if (ns_data->orig_delegate->attribute && ns_data->attributes) {
call_cb_for_attr_item(parser, ns_data->attributes, ns_data->orig_delegate);
};
free_attributes(parser);
// lookup ns for element and call element callback
// lookup ns for attributes and call callback
// cleanup
}
#pragma mark Inject the NS Delegate
void
inject_namespace_delegate(parser_handle * parser)
{
parser->ns_delegate_data.orig_delegate = parser->delegate;
parser->ns_delegate.data = &parser->ns_delegate_data;
parser->ns_delegate.start_document = &ns_delegate_start_document;
parser->ns_delegate.end_document = &ns_delegate_end_document;
parser->ns_delegate.start_element = &ns_delegate_start_element;
parser->ns_delegate.end_element = &ns_delegate_end_element;
parser->ns_delegate.attribute = &ns_delegate_attribute;
parser->ns_delegate.characters = &ns_delegate_characters;
parser->ns_delegate.error = &ns_delegate_error;
parser->ns_delegate._flush = &ns_delegate_flush;
parser->delegate = &parser->ns_delegate;
}