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
|
10415
|
88 xmlnode_insert_data(xmlnode *node, const char *data, size_t size)
|
7131
|
89 {
|
10415
|
90 xmlnode *child;
|
7131
|
91 size_t real_size;
|
|
92
|
10415
|
93 g_return_if_fail(node != NULL);
|
7131
|
94 g_return_if_fail(data != NULL);
|
|
95 g_return_if_fail(size != 0);
|
|
96
|
|
97 real_size = size == -1 ? strlen(data) : size;
|
|
98
|
10415
|
99 child = new_node(NULL, XMLNODE_TYPE_DATA);
|
7131
|
100
|
10415
|
101 child->data = g_memdup(data, real_size);
|
|
102 child->data_sz = real_size;
|
7131
|
103
|
10415
|
104 xmlnode_insert_child(node, child);
|
7131
|
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 xmlnode *c;
|
|
228
|
|
229 g_return_val_if_fail(node != NULL, NULL);
|
|
230
|
|
231
|
|
232 for(c = node->child; c; c = c->next) {
|
8135
|
233 if(c->type == XMLNODE_TYPE_DATA) {
|
7131
|
234 if(!str)
|
|
235 str = g_string_new("");
|
|
236 str = g_string_append_len(str, c->data, c->data_sz);
|
|
237 }
|
|
238 }
|
|
239
|
10331
|
240 if (str == NULL)
|
|
241 return NULL;
|
7131
|
242
|
10331
|
243 return g_string_free(str, FALSE);
|
7131
|
244 }
|
|
245
|
10415
|
246 static gchar *xmlnode_to_str_helper(xmlnode *node, int *len, gboolean formatting, int depth)
|
7131
|
247 {
|
|
248 GString *text = g_string_new("");
|
|
249 xmlnode *c;
|
9837
|
250 char *node_name, *esc, *esc2, *tab = NULL;
|
9838
|
251 gboolean need_end = FALSE, pretty = formatting;
|
9837
|
252 #ifdef _WIN32
|
|
253 static const char *newline = "\r\n";
|
|
254 #else
|
|
255 static const char *newline = "\n";
|
|
256 #endif
|
|
257
|
|
258 if(pretty && depth) {
|
|
259 tab = g_strnfill(depth, '\t');
|
|
260 text = g_string_append(text, tab);
|
|
261 }
|
7131
|
262
|
|
263 node_name = g_markup_escape_text(node->name, -1);
|
|
264 g_string_append_printf(text, "<%s", node_name);
|
|
265
|
|
266 for(c = node->child; c; c = c->next)
|
|
267 {
|
8135
|
268 if(c->type == XMLNODE_TYPE_ATTRIB) {
|
7131
|
269 esc = g_markup_escape_text(c->name, -1);
|
|
270 esc2 = g_markup_escape_text(c->data, -1);
|
|
271 g_string_append_printf(text, " %s='%s'", esc, esc2);
|
|
272 g_free(esc);
|
|
273 g_free(esc2);
|
8135
|
274 } else if(c->type == XMLNODE_TYPE_TAG || c->type == XMLNODE_TYPE_DATA) {
|
9837
|
275 if(c->type == XMLNODE_TYPE_DATA)
|
9838
|
276 pretty = FALSE;
|
7131
|
277 need_end = TRUE;
|
|
278 }
|
|
279 }
|
|
280
|
|
281 if(need_end) {
|
9838
|
282 g_string_append_printf(text, ">%s", pretty ? newline : "");
|
7131
|
283
|
|
284 for(c = node->child; c; c = c->next)
|
|
285 {
|
8135
|
286 if(c->type == XMLNODE_TYPE_TAG) {
|
7642
|
287 int esc_len;
|
9838
|
288 esc = xmlnode_to_str_helper(c, &esc_len, pretty, depth+1);
|
7642
|
289 text = g_string_append_len(text, esc, esc_len);
|
7131
|
290 g_free(esc);
|
8135
|
291 } else if(c->type == XMLNODE_TYPE_DATA) {
|
7131
|
292 esc = g_markup_escape_text(c->data, c->data_sz);
|
7642
|
293 text = g_string_append(text, esc);
|
7131
|
294 g_free(esc);
|
|
295 }
|
|
296 }
|
|
297
|
9838
|
298 if(tab && pretty)
|
9837
|
299 text = g_string_append(text, tab);
|
10415
|
300 g_string_append_printf(text, "</%s>%s", node_name, formatting ? newline : "");
|
7131
|
301 } else {
|
10415
|
302 g_string_append_printf(text, "/>%s", formatting ? newline : "");
|
7131
|
303 }
|
|
304
|
|
305 g_free(node_name);
|
|
306
|
9837
|
307 if(tab)
|
|
308 g_free(tab);
|
|
309
|
7642
|
310 if(len)
|
|
311 *len = text->len;
|
10331
|
312
|
|
313 return g_string_free(text, FALSE);
|
7131
|
314 }
|
|
315
|
10415
|
316 gchar *xmlnode_to_str(xmlnode *node, int *len) {
|
9837
|
317 return xmlnode_to_str_helper(node, len, FALSE, 0);
|
|
318 }
|
|
319
|
10415
|
320 gchar *xmlnode_to_formatted_str(xmlnode *node, int *len) {
|
|
321 gchar *xml, *xml_with_declaration;
|
|
322
|
|
323 xml = xmlnode_to_str_helper(node, len, TRUE, 0);
|
|
324 xml_with_declaration =
|
|
325 g_strdup_printf("<?xml version='1.0' encoding='UTF-8' ?>\n\n%s", xml);
|
|
326 g_free(xml);
|
|
327
|
|
328 return xml_with_declaration;
|
9837
|
329 }
|
|
330
|
7131
|
331 struct _xmlnode_parser_data {
|
|
332 xmlnode *current;
|
|
333 };
|
|
334
|
|
335 static void
|
|
336 xmlnode_parser_element_start(GMarkupParseContext *context,
|
|
337 const char *element_name, const char **attrib_names,
|
|
338 const char **attrib_values, gpointer user_data, GError **error)
|
|
339 {
|
|
340 struct _xmlnode_parser_data *xpd = user_data;
|
|
341 xmlnode *node;
|
|
342 int i;
|
|
343
|
|
344 if(!element_name) {
|
|
345 return;
|
|
346 } else {
|
|
347 if(xpd->current)
|
|
348 node = xmlnode_new_child(xpd->current, element_name);
|
|
349 else
|
|
350 node = xmlnode_new(element_name);
|
|
351
|
|
352 for(i=0; attrib_names[i]; i++)
|
|
353 xmlnode_set_attrib(node, attrib_names[i], attrib_values[i]);
|
|
354
|
|
355 xpd->current = node;
|
|
356 }
|
|
357 }
|
|
358
|
|
359 static void
|
|
360 xmlnode_parser_element_end(GMarkupParseContext *context,
|
|
361 const char *element_name, gpointer user_data, GError **error)
|
|
362 {
|
|
363 struct _xmlnode_parser_data *xpd = user_data;
|
|
364
|
|
365 if(!element_name || !xpd->current)
|
|
366 return;
|
|
367
|
|
368 if(xpd->current->parent) {
|
|
369 if(!strcmp(xpd->current->name, element_name))
|
|
370 xpd->current = xpd->current->parent;
|
|
371 }
|
|
372 }
|
|
373
|
|
374 static void
|
|
375 xmlnode_parser_element_text(GMarkupParseContext *context, const char *text,
|
|
376 gsize text_len, gpointer user_data, GError **error)
|
|
377 {
|
|
378 struct _xmlnode_parser_data *xpd = user_data;
|
|
379
|
|
380 if(!xpd->current)
|
|
381 return;
|
|
382
|
|
383 if(!text || !text_len)
|
|
384 return;
|
|
385
|
|
386 xmlnode_insert_data(xpd->current, text, text_len);
|
|
387 }
|
|
388
|
|
389 static GMarkupParser xmlnode_parser = {
|
|
390 xmlnode_parser_element_start,
|
|
391 xmlnode_parser_element_end,
|
|
392 xmlnode_parser_element_text,
|
|
393 NULL,
|
|
394 NULL
|
|
395 };
|
|
396
|
|
397
|
|
398 xmlnode *xmlnode_from_str(const char *str, size_t size)
|
|
399 {
|
|
400 struct _xmlnode_parser_data *xpd = g_new0(struct _xmlnode_parser_data, 1);
|
|
401 xmlnode *ret;
|
|
402 GMarkupParseContext *context;
|
|
403 size_t real_size = size == -1 ? strlen(str) : size;
|
|
404
|
|
405 context = g_markup_parse_context_new(&xmlnode_parser, 0, xpd, NULL);
|
|
406
|
|
407 if(!g_markup_parse_context_parse(context, str, real_size, NULL)) {
|
|
408 while(xpd->current && xpd->current->parent)
|
|
409 xpd->current = xpd->current->parent;
|
|
410 if(xpd->current)
|
|
411 xmlnode_free(xpd->current);
|
|
412 xpd->current = NULL;
|
|
413 }
|
|
414 g_markup_parse_context_free(context);
|
|
415
|
|
416 ret = xpd->current;
|
|
417 g_free(xpd);
|
|
418 return ret;
|
|
419 }
|
8135
|
420
|
|
421 xmlnode *xmlnode_copy(xmlnode *src)
|
|
422 {
|
|
423 xmlnode *ret;
|
|
424 xmlnode *child;
|
|
425 xmlnode *sibling = NULL;
|
|
426
|
|
427 if(!src)
|
|
428 return NULL;
|
|
429
|
|
430 ret = new_node(src->name, src->type);
|
|
431 if(src->data) {
|
8167
|
432 if(src->data_sz) {
|
|
433 ret->data = g_memdup(src->data, src->data_sz);
|
|
434 ret->data_sz = src->data_sz;
|
|
435 } else {
|
|
436 ret->data = g_strdup(src->data);
|
|
437 }
|
8135
|
438 }
|
|
439
|
|
440 for(child = src->child; child; child = child->next) {
|
|
441 if(sibling) {
|
|
442 sibling->next = xmlnode_copy(child);
|
|
443 sibling = sibling->next;
|
|
444 } else {
|
|
445 ret->child = xmlnode_copy(child);
|
|
446 sibling = ret->child;
|
|
447 }
|
|
448 sibling->parent = ret;
|
|
449 }
|
|
450
|
|
451 return ret;
|
|
452 }
|
|
453
|
|
454 xmlnode *xmlnode_get_next_twin(xmlnode *node) {
|
|
455 xmlnode *sibling;
|
8262
|
456 const char *ns = xmlnode_get_attrib(node, "xmlns");
|
8135
|
457
|
|
458 g_return_val_if_fail(node != NULL, NULL);
|
|
459 g_return_val_if_fail(node->type == XMLNODE_TYPE_TAG, NULL);
|
|
460
|
|
461 for(sibling = node->next; sibling; sibling = sibling->next) {
|
8283
|
462 const char *xmlns = NULL;
|
8262
|
463 if(ns)
|
|
464 xmlns = xmlnode_get_attrib(sibling, "xmlns");
|
|
465
|
|
466 if(sibling->type == XMLNODE_TYPE_TAG && !strcmp(node->name, sibling->name) &&
|
|
467 (!ns || (xmlns && !strcmp(ns, xmlns))))
|
8135
|
468 return sibling;
|
|
469 }
|
|
470
|
|
471 return NULL;
|
|
472 }
|
|
473
|