7131
|
1 /**
|
|
2 * @file xmlnode.c XML DOM functions
|
|
3 *
|
|
4 * gaim
|
|
5 *
|
8046
|
6 * Gaim is the legal property of its developers, whose names are too numerous
|
|
7 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
8 * source distribution.
|
7131
|
9 *
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; either version 2 of the License, or
|
|
13 * (at your option) any later version.
|
|
14 *
|
|
15 * This program is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this program; if not, write to the Free Software
|
|
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
23 */
|
|
24
|
|
25 /* A lot of this code at least resembles the code in libxode, but since
|
|
26 * libxode uses memory pools that we simply have no need for, I decided to
|
|
27 * write my own stuff. Also, re-writing this lets me be as lightweight
|
|
28 * as I want to be. Thank you libxode for giving me a good starting point */
|
|
29
|
|
30 #include "internal.h"
|
|
31
|
|
32 #include <string.h>
|
|
33 #include <glib.h>
|
|
34
|
|
35 #include "xmlnode.h"
|
|
36
|
|
37 static xmlnode*
|
8135
|
38 new_node(const char *name, XMLNodeType type)
|
7131
|
39 {
|
|
40 xmlnode *node = g_new0(xmlnode, 1);
|
|
41 if(name)
|
|
42 node->name = g_strdup(name);
|
|
43 node->type = type;
|
|
44
|
|
45 return node;
|
|
46 }
|
|
47
|
|
48 xmlnode*
|
|
49 xmlnode_new(const char *name)
|
|
50 {
|
|
51 g_return_val_if_fail(name != NULL, NULL);
|
|
52
|
8135
|
53 return new_node(name, XMLNODE_TYPE_TAG);
|
7131
|
54 }
|
|
55
|
|
56 xmlnode *xmlnode_new_child(xmlnode *parent, const char *name)
|
|
57 {
|
|
58 xmlnode *node;
|
|
59
|
|
60 g_return_val_if_fail(parent != NULL, NULL);
|
|
61 g_return_val_if_fail(name != NULL, NULL);
|
|
62
|
8135
|
63 node = new_node(name, XMLNODE_TYPE_TAG);
|
7131
|
64
|
|
65 xmlnode_insert_child(parent, node);
|
|
66
|
|
67 return node;
|
|
68 }
|
|
69
|
|
70 void
|
|
71 xmlnode_insert_child(xmlnode *parent, xmlnode *child)
|
|
72 {
|
|
73 g_return_if_fail(parent != NULL);
|
|
74 g_return_if_fail(child != NULL);
|
|
75
|
|
76 child->parent = parent;
|
|
77
|
|
78 if(parent->child) {
|
|
79 xmlnode *x;
|
|
80 for(x = parent->child; x->next; x = x->next);
|
|
81 x->next = child;
|
|
82 } else {
|
|
83 parent->child = child;
|
|
84 }
|
|
85 }
|
|
86
|
|
87 void
|
|
88 xmlnode_insert_data(xmlnode *parent, const char *data, size_t size)
|
|
89 {
|
|
90 xmlnode *node;
|
|
91 size_t real_size;
|
|
92
|
|
93 g_return_if_fail(parent != NULL);
|
|
94 g_return_if_fail(data != NULL);
|
|
95 g_return_if_fail(size != 0);
|
|
96
|
|
97 real_size = size == -1 ? strlen(data) : size;
|
|
98
|
8135
|
99 node = new_node(NULL, XMLNODE_TYPE_DATA);
|
7131
|
100
|
|
101 node->data = g_memdup(data, real_size);
|
|
102 node->data_sz = real_size;
|
|
103
|
|
104 xmlnode_insert_child(parent, node);
|
|
105 }
|
|
106
|
|
107 void
|
|
108 xmlnode_remove_attrib(xmlnode *node, const char *attr)
|
|
109 {
|
|
110 xmlnode *attr_node, *sibling = NULL;
|
|
111
|
|
112 g_return_if_fail(node != NULL);
|
|
113 g_return_if_fail(attr != NULL);
|
|
114
|
|
115 for(attr_node = node->child; attr_node; attr_node = attr_node->next)
|
|
116 {
|
8135
|
117 if(attr_node->type == XMLNODE_TYPE_ATTRIB &&
|
7131
|
118 !strcmp(attr_node->name, attr)) {
|
|
119 if(node->child == attr_node) {
|
|
120 node->child = attr_node->next;
|
|
121 } else {
|
|
122 sibling->next = attr_node->next;
|
|
123 }
|
|
124 xmlnode_free(attr_node);
|
|
125 return;
|
|
126 }
|
|
127 sibling = attr_node;
|
|
128 }
|
|
129 }
|
|
130
|
|
131 void
|
|
132 xmlnode_set_attrib(xmlnode *node, const char *attr, const char *value)
|
|
133 {
|
|
134 xmlnode *attrib_node;
|
|
135
|
|
136 g_return_if_fail(node != NULL);
|
|
137 g_return_if_fail(attr != NULL);
|
|
138 g_return_if_fail(value != NULL);
|
|
139
|
|
140 xmlnode_remove_attrib(node, attr);
|
|
141
|
8135
|
142 attrib_node = new_node(attr, XMLNODE_TYPE_ATTRIB);
|
7131
|
143
|
|
144 attrib_node->data = g_strdup(value);
|
|
145
|
|
146 xmlnode_insert_child(node, attrib_node);
|
|
147 }
|
|
148
|
|
149 const char*
|
|
150 xmlnode_get_attrib(xmlnode *node, const char *attr)
|
|
151 {
|
|
152 xmlnode *x;
|
|
153
|
|
154 g_return_val_if_fail(node != NULL, NULL);
|
|
155
|
|
156 for(x = node->child; x; x = x->next) {
|
8135
|
157 if(x->type == XMLNODE_TYPE_ATTRIB && !strcmp(attr, x->name)) {
|
7131
|
158 return x->data;
|
|
159 }
|
|
160 }
|
|
161
|
|
162 return NULL;
|
|
163 }
|
|
164
|
|
165 void xmlnode_free(xmlnode *node)
|
|
166 {
|
|
167 xmlnode *x, *y;
|
|
168
|
|
169 g_return_if_fail(node != NULL);
|
|
170
|
|
171 x = node->child;
|
|
172 while(x) {
|
|
173 y = x->next;
|
|
174 xmlnode_free(x);
|
|
175 x = y;
|
|
176 }
|
|
177
|
|
178 if(node->name)
|
|
179 g_free(node->name);
|
|
180 if(node->data)
|
|
181 g_free(node->data);
|
|
182 g_free(node);
|
|
183 }
|
|
184
|
|
185 xmlnode*
|
8262
|
186 xmlnode_get_child_with_namespace(xmlnode *parent, const char *name, const char *ns)
|
7131
|
187 {
|
|
188 xmlnode *x, *ret = NULL;
|
|
189 char **names;
|
|
190 char *parent_name, *child_name;
|
|
191
|
|
192 g_return_val_if_fail(parent != NULL, NULL);
|
|
193
|
|
194 names = g_strsplit(name, "/", 2);
|
|
195 parent_name = names[0];
|
|
196 child_name = names[1];
|
|
197
|
|
198 for(x = parent->child; x; x = x->next) {
|
8262
|
199 const char *xmlns = NULL;
|
|
200 if(ns)
|
|
201 xmlns = xmlnode_get_attrib(x, "xmlns");
|
|
202
|
|
203 if(x->type == XMLNODE_TYPE_TAG && name && !strcmp(parent_name, x->name)
|
|
204 && (!ns || (xmlns && !strcmp(ns, xmlns)))) {
|
7131
|
205 ret = x;
|
|
206 break;
|
|
207 }
|
|
208 }
|
|
209
|
|
210 if(child_name && ret)
|
8262
|
211 ret = xmlnode_get_child(ret, child_name);
|
7131
|
212
|
|
213 g_strfreev(names);
|
|
214 return ret;
|
|
215 }
|
|
216
|
8262
|
217 xmlnode*
|
|
218 xmlnode_get_child(xmlnode *parent, const char *name)
|
|
219 {
|
|
220 return xmlnode_get_child_with_namespace(parent, name, NULL);
|
|
221 }
|
|
222
|
7131
|
223 char *
|
|
224 xmlnode_get_data(xmlnode *node)
|
|
225 {
|
|
226 GString *str = NULL;
|
|
227 char *ret = NULL;
|
|
228 xmlnode *c;
|
|
229
|
|
230 g_return_val_if_fail(node != NULL, NULL);
|
|
231
|
|
232
|
|
233 for(c = node->child; c; c = c->next) {
|
8135
|
234 if(c->type == XMLNODE_TYPE_DATA) {
|
7131
|
235 if(!str)
|
|
236 str = g_string_new("");
|
|
237 str = g_string_append_len(str, c->data, c->data_sz);
|
|
238 }
|
|
239 }
|
|
240
|
|
241 if(str) {
|
|
242 ret = str->str;
|
|
243 g_string_free(str, FALSE);
|
|
244 }
|
|
245
|
|
246 return ret;
|
|
247 }
|
|
248
|
9838
|
249 static char *xmlnode_to_str_helper(xmlnode *node, int *len, gboolean formatting, int depth)
|
7131
|
250 {
|
|
251 char *ret;
|
|
252 GString *text = g_string_new("");
|
|
253 xmlnode *c;
|
9837
|
254 char *node_name, *esc, *esc2, *tab = NULL;
|
9838
|
255 gboolean need_end = FALSE, pretty = formatting;
|
9837
|
256 #ifdef _WIN32
|
|
257 static const char *newline = "\r\n";
|
|
258 #else
|
|
259 static const char *newline = "\n";
|
|
260 #endif
|
|
261
|
|
262 if(pretty && depth) {
|
|
263 tab = g_strnfill(depth, '\t');
|
|
264 text = g_string_append(text, tab);
|
|
265 }
|
7131
|
266
|
|
267 node_name = g_markup_escape_text(node->name, -1);
|
|
268 g_string_append_printf(text, "<%s", node_name);
|
|
269
|
|
270 for(c = node->child; c; c = c->next)
|
|
271 {
|
8135
|
272 if(c->type == XMLNODE_TYPE_ATTRIB) {
|
7131
|
273 esc = g_markup_escape_text(c->name, -1);
|
|
274 esc2 = g_markup_escape_text(c->data, -1);
|
|
275 g_string_append_printf(text, " %s='%s'", esc, esc2);
|
|
276 g_free(esc);
|
|
277 g_free(esc2);
|
8135
|
278 } else if(c->type == XMLNODE_TYPE_TAG || c->type == XMLNODE_TYPE_DATA) {
|
9837
|
279 if(c->type == XMLNODE_TYPE_DATA)
|
9838
|
280 pretty = FALSE;
|
7131
|
281 need_end = TRUE;
|
|
282 }
|
|
283 }
|
|
284
|
|
285 if(need_end) {
|
9838
|
286 g_string_append_printf(text, ">%s", pretty ? newline : "");
|
7131
|
287
|
|
288 for(c = node->child; c; c = c->next)
|
|
289 {
|
8135
|
290 if(c->type == XMLNODE_TYPE_TAG) {
|
7642
|
291 int esc_len;
|
9838
|
292 esc = xmlnode_to_str_helper(c, &esc_len, pretty, depth+1);
|
7642
|
293 text = g_string_append_len(text, esc, esc_len);
|
7131
|
294 g_free(esc);
|
8135
|
295 } else if(c->type == XMLNODE_TYPE_DATA) {
|
7131
|
296 esc = g_markup_escape_text(c->data, c->data_sz);
|
7642
|
297 text = g_string_append(text, esc);
|
7131
|
298 g_free(esc);
|
|
299 }
|
|
300 }
|
|
301
|
9838
|
302 if(tab && pretty)
|
9837
|
303 text = g_string_append(text, tab);
|
|
304 g_string_append_printf(text, "</%s>%s", node_name, pretty ? newline : "");
|
7131
|
305 } else {
|
9837
|
306 g_string_append_printf(text, "/>%s", pretty ? newline : "");
|
7131
|
307 }
|
|
308
|
|
309 g_free(node_name);
|
|
310
|
9837
|
311 if(tab)
|
|
312 g_free(tab);
|
|
313
|
7131
|
314 ret = text->str;
|
7642
|
315 if(len)
|
|
316 *len = text->len;
|
7131
|
317 g_string_free(text, FALSE);
|
|
318 return ret;
|
|
319 }
|
|
320
|
9837
|
321 char *xmlnode_to_str(xmlnode *node, int *len) {
|
|
322 return xmlnode_to_str_helper(node, len, FALSE, 0);
|
|
323 }
|
|
324
|
|
325 char *xmlnode_to_formatted_str(xmlnode *node, int *len) {
|
|
326 return xmlnode_to_str_helper(node, len, TRUE, 0);
|
|
327 }
|
|
328
|
7131
|
329 struct _xmlnode_parser_data {
|
|
330 xmlnode *current;
|
|
331 };
|
|
332
|
|
333 static void
|
|
334 xmlnode_parser_element_start(GMarkupParseContext *context,
|
|
335 const char *element_name, const char **attrib_names,
|
|
336 const char **attrib_values, gpointer user_data, GError **error)
|
|
337 {
|
|
338 struct _xmlnode_parser_data *xpd = user_data;
|
|
339 xmlnode *node;
|
|
340 int i;
|
|
341
|
|
342 if(!element_name) {
|
|
343 return;
|
|
344 } else {
|
|
345 if(xpd->current)
|
|
346 node = xmlnode_new_child(xpd->current, element_name);
|
|
347 else
|
|
348 node = xmlnode_new(element_name);
|
|
349
|
|
350 for(i=0; attrib_names[i]; i++)
|
|
351 xmlnode_set_attrib(node, attrib_names[i], attrib_values[i]);
|
|
352
|
|
353 xpd->current = node;
|
|
354 }
|
|
355 }
|
|
356
|
|
357 static void
|
|
358 xmlnode_parser_element_end(GMarkupParseContext *context,
|
|
359 const char *element_name, gpointer user_data, GError **error)
|
|
360 {
|
|
361 struct _xmlnode_parser_data *xpd = user_data;
|
|
362
|
|
363 if(!element_name || !xpd->current)
|
|
364 return;
|
|
365
|
|
366 if(xpd->current->parent) {
|
|
367 if(!strcmp(xpd->current->name, element_name))
|
|
368 xpd->current = xpd->current->parent;
|
|
369 }
|
|
370 }
|
|
371
|
|
372 static void
|
|
373 xmlnode_parser_element_text(GMarkupParseContext *context, const char *text,
|
|
374 gsize text_len, gpointer user_data, GError **error)
|
|
375 {
|
|
376 struct _xmlnode_parser_data *xpd = user_data;
|
|
377
|
|
378 if(!xpd->current)
|
|
379 return;
|
|
380
|
|
381 if(!text || !text_len)
|
|
382 return;
|
|
383
|
|
384 xmlnode_insert_data(xpd->current, text, text_len);
|
|
385 }
|
|
386
|
|
387 static GMarkupParser xmlnode_parser = {
|
|
388 xmlnode_parser_element_start,
|
|
389 xmlnode_parser_element_end,
|
|
390 xmlnode_parser_element_text,
|
|
391 NULL,
|
|
392 NULL
|
|
393 };
|
|
394
|
|
395
|
|
396 xmlnode *xmlnode_from_str(const char *str, size_t size)
|
|
397 {
|
|
398 struct _xmlnode_parser_data *xpd = g_new0(struct _xmlnode_parser_data, 1);
|
|
399 xmlnode *ret;
|
|
400 GMarkupParseContext *context;
|
|
401 size_t real_size = size == -1 ? strlen(str) : size;
|
|
402
|
|
403 context = g_markup_parse_context_new(&xmlnode_parser, 0, xpd, NULL);
|
|
404
|
|
405 if(!g_markup_parse_context_parse(context, str, real_size, NULL)) {
|
|
406 while(xpd->current && xpd->current->parent)
|
|
407 xpd->current = xpd->current->parent;
|
|
408 if(xpd->current)
|
|
409 xmlnode_free(xpd->current);
|
|
410 xpd->current = NULL;
|
|
411 }
|
|
412 g_markup_parse_context_free(context);
|
|
413
|
|
414 ret = xpd->current;
|
|
415 g_free(xpd);
|
|
416 return ret;
|
|
417 }
|
8135
|
418
|
|
419 xmlnode *xmlnode_copy(xmlnode *src)
|
|
420 {
|
|
421 xmlnode *ret;
|
|
422 xmlnode *child;
|
|
423 xmlnode *sibling = NULL;
|
|
424
|
|
425 if(!src)
|
|
426 return NULL;
|
|
427
|
|
428 ret = new_node(src->name, src->type);
|
|
429 if(src->data) {
|
8167
|
430 if(src->data_sz) {
|
|
431 ret->data = g_memdup(src->data, src->data_sz);
|
|
432 ret->data_sz = src->data_sz;
|
|
433 } else {
|
|
434 ret->data = g_strdup(src->data);
|
|
435 }
|
8135
|
436 }
|
|
437
|
|
438 for(child = src->child; child; child = child->next) {
|
|
439 if(sibling) {
|
|
440 sibling->next = xmlnode_copy(child);
|
|
441 sibling = sibling->next;
|
|
442 } else {
|
|
443 ret->child = xmlnode_copy(child);
|
|
444 sibling = ret->child;
|
|
445 }
|
|
446 sibling->parent = ret;
|
|
447 }
|
|
448
|
|
449 return ret;
|
|
450 }
|
|
451
|
|
452 xmlnode *xmlnode_get_next_twin(xmlnode *node) {
|
|
453 xmlnode *sibling;
|
8262
|
454 const char *ns = xmlnode_get_attrib(node, "xmlns");
|
8135
|
455
|
|
456 g_return_val_if_fail(node != NULL, NULL);
|
|
457 g_return_val_if_fail(node->type == XMLNODE_TYPE_TAG, NULL);
|
|
458
|
|
459 for(sibling = node->next; sibling; sibling = sibling->next) {
|
8283
|
460 const char *xmlns = NULL;
|
8262
|
461 if(ns)
|
|
462 xmlns = xmlnode_get_attrib(sibling, "xmlns");
|
|
463
|
|
464 if(sibling->type == XMLNODE_TYPE_TAG && !strcmp(node->name, sibling->name) &&
|
|
465 (!ns || (xmlns && !strcmp(ns, xmlns))))
|
8135
|
466 return sibling;
|
|
467 }
|
|
468
|
|
469 return NULL;
|
|
470 }
|
|
471
|