14192
|
1 /**
|
|
2 * @file xmlnode.c XML DOM functions
|
|
3 *
|
|
4 * gaim
|
|
5 *
|
|
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.
|
|
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 #ifdef HAVE_LIBXML
|
|
33 #include <libxml/parser.h>
|
|
34 #endif
|
|
35 #include <string.h>
|
|
36 #include <glib.h>
|
|
37
|
|
38 #include "util.h"
|
|
39 #include "xmlnode.h"
|
|
40
|
|
41 #ifdef _WIN32
|
|
42 # define NEWLINE_S "\r\n"
|
|
43 #else
|
|
44 # define NEWLINE_S "\n"
|
|
45 #endif
|
|
46
|
|
47 static xmlnode*
|
|
48 new_node(const char *name, XMLNodeType type)
|
|
49 {
|
|
50 xmlnode *node = g_new0(xmlnode, 1);
|
|
51
|
|
52 node->name = g_strdup(name);
|
|
53 node->type = type;
|
|
54
|
|
55 return node;
|
|
56 }
|
|
57
|
|
58 xmlnode*
|
|
59 xmlnode_new(const char *name)
|
|
60 {
|
|
61 g_return_val_if_fail(name != NULL, NULL);
|
|
62
|
|
63 return new_node(name, XMLNODE_TYPE_TAG);
|
|
64 }
|
|
65
|
|
66 xmlnode *
|
|
67 xmlnode_new_child(xmlnode *parent, const char *name)
|
|
68 {
|
|
69 xmlnode *node;
|
|
70
|
|
71 g_return_val_if_fail(parent != NULL, NULL);
|
|
72 g_return_val_if_fail(name != NULL, NULL);
|
|
73
|
|
74 node = new_node(name, XMLNODE_TYPE_TAG);
|
|
75
|
|
76 xmlnode_insert_child(parent, node);
|
|
77
|
|
78 return node;
|
|
79 }
|
|
80
|
|
81 void
|
|
82 xmlnode_insert_child(xmlnode *parent, xmlnode *child)
|
|
83 {
|
|
84 g_return_if_fail(parent != NULL);
|
|
85 g_return_if_fail(child != NULL);
|
|
86
|
|
87 child->parent = parent;
|
|
88
|
|
89 if(parent->lastchild) {
|
|
90 parent->lastchild->next = child;
|
|
91 } else {
|
|
92 parent->child = child;
|
|
93 }
|
|
94
|
|
95 parent->lastchild = child;
|
|
96 }
|
|
97
|
|
98 void
|
|
99 xmlnode_insert_data(xmlnode *node, const char *data, gssize size)
|
|
100 {
|
|
101 xmlnode *child;
|
|
102 gsize real_size;
|
|
103
|
|
104 g_return_if_fail(node != NULL);
|
|
105 g_return_if_fail(data != NULL);
|
|
106 g_return_if_fail(size != 0);
|
|
107
|
|
108 real_size = size == -1 ? strlen(data) : size;
|
|
109
|
|
110 child = new_node(NULL, XMLNODE_TYPE_DATA);
|
|
111
|
|
112 child->data = g_memdup(data, real_size);
|
|
113 child->data_sz = real_size;
|
|
114
|
|
115 xmlnode_insert_child(node, child);
|
|
116 }
|
|
117
|
|
118 void
|
|
119 xmlnode_remove_attrib(xmlnode *node, const char *attr)
|
|
120 {
|
|
121 xmlnode *attr_node, *sibling = NULL;
|
|
122
|
|
123 g_return_if_fail(node != NULL);
|
|
124 g_return_if_fail(attr != NULL);
|
|
125
|
|
126 for(attr_node = node->child; attr_node; attr_node = attr_node->next)
|
|
127 {
|
|
128 if(attr_node->type == XMLNODE_TYPE_ATTRIB &&
|
|
129 !strcmp(attr_node->name, attr)) {
|
|
130 if(node->child == attr_node) {
|
|
131 node->child = attr_node->next;
|
|
132 } else if (node->lastchild == attr_node) {
|
|
133 node->lastchild = sibling;
|
|
134 } else {
|
|
135 sibling->next = attr_node->next;
|
|
136 }
|
|
137 xmlnode_free(attr_node);
|
|
138 return;
|
|
139 }
|
|
140 sibling = attr_node;
|
|
141 }
|
|
142 }
|
|
143
|
|
144 void
|
|
145 xmlnode_set_attrib(xmlnode *node, const char *attr, const char *value)
|
|
146 {
|
|
147 xmlnode *attrib_node;
|
|
148
|
|
149 g_return_if_fail(node != NULL);
|
|
150 g_return_if_fail(attr != NULL);
|
|
151 g_return_if_fail(value != NULL);
|
|
152
|
|
153 xmlnode_remove_attrib(node, attr);
|
|
154
|
|
155 attrib_node = new_node(attr, XMLNODE_TYPE_ATTRIB);
|
|
156
|
|
157 attrib_node->data = g_strdup(value);
|
|
158
|
|
159 xmlnode_insert_child(node, attrib_node);
|
|
160 }
|
|
161
|
|
162 const char *
|
|
163 xmlnode_get_attrib(xmlnode *node, const char *attr)
|
|
164 {
|
|
165 xmlnode *x;
|
|
166
|
|
167 g_return_val_if_fail(node != NULL, NULL);
|
|
168
|
|
169 for(x = node->child; x; x = x->next) {
|
|
170 if(x->type == XMLNODE_TYPE_ATTRIB && !strcmp(attr, x->name)) {
|
|
171 return x->data;
|
|
172 }
|
|
173 }
|
|
174
|
|
175 return NULL;
|
|
176 }
|
|
177
|
|
178
|
|
179 void xmlnode_set_namespace(xmlnode *node, const char *xmlns)
|
|
180 {
|
|
181 #ifdef HAVE_LIBXML
|
|
182 g_return_if_fail(node != NULL);
|
|
183
|
|
184 g_free(node->namespace);
|
|
185 node->namespace = g_strdup(xmlns);
|
|
186 #else
|
|
187 xmlnode_set_attrib(node, "xmlns", xmlns);
|
|
188 #endif
|
|
189 }
|
|
190
|
|
191 const char *xmlnode_get_namespace(xmlnode *node)
|
|
192 {
|
|
193 #ifdef HAVE_LIBXML
|
|
194 g_return_val_if_fail(node != NULL, NULL);
|
|
195
|
|
196 return node->namespace;
|
|
197 #else
|
|
198 return xmlnode_get_attrib(node, "xmlns");
|
|
199 #endif
|
|
200 }
|
|
201
|
|
202 void
|
|
203 xmlnode_free(xmlnode *node)
|
|
204 {
|
|
205 xmlnode *x, *y;
|
|
206
|
|
207 g_return_if_fail(node != NULL);
|
|
208
|
|
209 x = node->child;
|
|
210 while(x) {
|
|
211 y = x->next;
|
|
212 xmlnode_free(x);
|
|
213 x = y;
|
|
214 }
|
|
215
|
|
216 g_free(node->name);
|
|
217 g_free(node->data);
|
|
218 #ifdef HAVE_LIBXML
|
|
219 g_free(node->namespace);
|
|
220 #endif
|
|
221 g_free(node);
|
|
222 }
|
|
223
|
|
224 xmlnode*
|
|
225 xmlnode_get_child(const xmlnode *parent, const char *name)
|
|
226 {
|
|
227 return xmlnode_get_child_with_namespace(parent, name, NULL);
|
|
228 }
|
|
229
|
|
230 xmlnode *
|
|
231 xmlnode_get_child_with_namespace(const xmlnode *parent, const char *name, const char *ns)
|
|
232 {
|
|
233 xmlnode *x, *ret = NULL;
|
|
234 char **names;
|
|
235 char *parent_name, *child_name;
|
|
236
|
|
237 g_return_val_if_fail(parent != NULL, NULL);
|
|
238 g_return_val_if_fail(name != NULL, NULL);
|
|
239
|
|
240 names = g_strsplit(name, "/", 2);
|
|
241 parent_name = names[0];
|
|
242 child_name = names[1];
|
|
243
|
|
244 for(x = parent->child; x; x = x->next) {
|
|
245 const char *xmlns = NULL;
|
|
246 if(ns)
|
|
247 xmlns = xmlnode_get_namespace(x);
|
|
248
|
|
249 if(x->type == XMLNODE_TYPE_TAG && name && !strcmp(parent_name, x->name)
|
|
250 && (!ns || (xmlns && !strcmp(ns, xmlns)))) {
|
|
251 ret = x;
|
|
252 break;
|
|
253 }
|
|
254 }
|
|
255
|
|
256 if(child_name && ret)
|
|
257 ret = xmlnode_get_child(ret, child_name);
|
|
258
|
|
259 g_strfreev(names);
|
|
260 return ret;
|
|
261 }
|
|
262
|
|
263 char *
|
|
264 xmlnode_get_data(xmlnode *node)
|
|
265 {
|
|
266 GString *str = NULL;
|
|
267 xmlnode *c;
|
|
268
|
|
269 g_return_val_if_fail(node != NULL, NULL);
|
|
270
|
|
271 for(c = node->child; c; c = c->next) {
|
|
272 if(c->type == XMLNODE_TYPE_DATA) {
|
|
273 if(!str)
|
|
274 str = g_string_new("");
|
|
275 str = g_string_append_len(str, c->data, c->data_sz);
|
|
276 }
|
|
277 }
|
|
278
|
|
279 if (str == NULL)
|
|
280 return NULL;
|
|
281
|
|
282 return g_string_free(str, FALSE);
|
|
283 }
|
|
284
|
|
285 static char *
|
|
286 xmlnode_to_str_helper(xmlnode *node, int *len, gboolean formatting, int depth)
|
|
287 {
|
|
288 GString *text = g_string_new("");
|
|
289 xmlnode *c;
|
|
290 char *node_name, *esc, *esc2, *tab = NULL;
|
|
291 gboolean need_end = FALSE, pretty = formatting;
|
|
292
|
|
293 g_return_val_if_fail(node != NULL, NULL);
|
|
294
|
|
295 if(pretty && depth) {
|
|
296 tab = g_strnfill(depth, '\t');
|
|
297 text = g_string_append(text, tab);
|
|
298 }
|
|
299
|
|
300 node_name = g_markup_escape_text(node->name, -1);
|
|
301 g_string_append_printf(text, "<%s", node_name);
|
|
302
|
|
303 #ifdef HAVE_LIBXML
|
|
304 if (node->namespace) {
|
|
305 char *namespace = g_markup_escape_text(node->namespace, -1);
|
|
306 g_string_append_printf(text, " xmlns='%s'", namespace);
|
|
307 g_free(namespace);
|
|
308 }
|
|
309 #endif
|
|
310 for(c = node->child; c; c = c->next)
|
|
311 {
|
|
312 if(c->type == XMLNODE_TYPE_ATTRIB) {
|
|
313 esc = g_markup_escape_text(c->name, -1);
|
|
314 esc2 = g_markup_escape_text(c->data, -1);
|
|
315 g_string_append_printf(text, " %s='%s'", esc, esc2);
|
|
316 g_free(esc);
|
|
317 g_free(esc2);
|
|
318 } else if(c->type == XMLNODE_TYPE_TAG || c->type == XMLNODE_TYPE_DATA) {
|
|
319 if(c->type == XMLNODE_TYPE_DATA)
|
|
320 pretty = FALSE;
|
|
321 need_end = TRUE;
|
|
322 }
|
|
323 }
|
|
324
|
|
325 if(need_end) {
|
|
326 g_string_append_printf(text, ">%s", pretty ? NEWLINE_S : "");
|
|
327
|
|
328 for(c = node->child; c; c = c->next)
|
|
329 {
|
|
330 if(c->type == XMLNODE_TYPE_TAG) {
|
|
331 int esc_len;
|
|
332 esc = xmlnode_to_str_helper(c, &esc_len, pretty, depth+1);
|
|
333 text = g_string_append_len(text, esc, esc_len);
|
|
334 g_free(esc);
|
|
335 } else if(c->type == XMLNODE_TYPE_DATA && c->data_sz > 0) {
|
|
336 esc = g_markup_escape_text(c->data, c->data_sz);
|
|
337 text = g_string_append(text, esc);
|
|
338 g_free(esc);
|
|
339 }
|
|
340 }
|
|
341
|
|
342 if(tab && pretty)
|
|
343 text = g_string_append(text, tab);
|
|
344 g_string_append_printf(text, "</%s>%s", node_name, formatting ? NEWLINE_S : "");
|
|
345 } else {
|
|
346 g_string_append_printf(text, "/>%s", formatting ? NEWLINE_S : "");
|
|
347 }
|
|
348
|
|
349 g_free(node_name);
|
|
350
|
|
351 g_free(tab);
|
|
352
|
|
353 if(len)
|
|
354 *len = text->len;
|
|
355
|
|
356 return g_string_free(text, FALSE);
|
|
357 }
|
|
358
|
|
359 char *
|
|
360 xmlnode_to_str(xmlnode *node, int *len)
|
|
361 {
|
|
362 return xmlnode_to_str_helper(node, len, FALSE, 0);
|
|
363 }
|
|
364
|
|
365 char *
|
|
366 xmlnode_to_formatted_str(xmlnode *node, int *len)
|
|
367 {
|
|
368 char *xml, *xml_with_declaration;
|
|
369
|
|
370 g_return_val_if_fail(node != NULL, NULL);
|
|
371
|
|
372 xml = xmlnode_to_str_helper(node, len, TRUE, 0);
|
|
373 xml_with_declaration =
|
|
374 g_strdup_printf("<?xml version='1.0' encoding='UTF-8' ?>" NEWLINE_S NEWLINE_S "%s", xml);
|
|
375 g_free(xml);
|
|
376
|
|
377 return xml_with_declaration;
|
|
378 }
|
|
379
|
|
380 struct _xmlnode_parser_data {
|
|
381 xmlnode *current;
|
|
382 };
|
|
383
|
|
384 #ifdef HAVE_LIBXML
|
|
385 static void
|
|
386 xmlnode_parser_element_start_libxml(void *user_data,
|
|
387 const xmlChar *element_name, const xmlChar *prefix, const xmlChar *namespace,
|
|
388 int nb_namespaces, const xmlChar **namespaces,
|
|
389 int nb_attributes, int nb_defaulted, const xmlChar **attributes)
|
|
390 {
|
|
391 struct _xmlnode_parser_data *xpd = user_data;
|
|
392 xmlnode *node;
|
|
393 int i;
|
|
394
|
|
395 if(!element_name) {
|
|
396 return;
|
|
397 } else {
|
|
398 if(xpd->current)
|
|
399 node = xmlnode_new_child(xpd->current, element_name);
|
|
400 else
|
|
401 node = xmlnode_new(element_name);
|
|
402
|
|
403 xmlnode_set_namespace(node, namespace);
|
|
404
|
|
405 for(i=0; i < nb_attributes * 5; i+=5) {
|
14228
|
406 #ifdef HAVE_LIBXML
|
|
407 char *txt;
|
|
408 #endif
|
14192
|
409 int attrib_len = attributes[i+4] - attributes[i+3];
|
|
410 char *attrib = g_malloc(attrib_len + 1);
|
|
411 memcpy(attrib, attributes[i+3], attrib_len);
|
|
412 attrib[attrib_len] = '\0';
|
|
413 #ifdef HAVE_LIBXML
|
14228
|
414 txt = attrib;
|
14192
|
415 attrib = gaim_unescape_html(txt);
|
|
416 g_free(txt);
|
|
417 #endif
|
|
418 xmlnode_set_attrib(node, attributes[i], attrib);
|
|
419 g_free(attrib);
|
|
420 }
|
|
421
|
|
422 xpd->current = node;
|
|
423 }
|
|
424 }
|
|
425
|
|
426 static void
|
|
427 xmlnode_parser_element_end_libxml(void *user_data, const xmlChar *element_name,
|
|
428 const xmlChar *prefix, const xmlChar *namespace)
|
|
429 {
|
|
430 struct _xmlnode_parser_data *xpd = user_data;
|
|
431
|
|
432 if(!element_name || !xpd->current)
|
|
433 return;
|
|
434
|
|
435 if(xpd->current->parent) {
|
|
436 if(!strcmp(xpd->current->name, element_name))
|
|
437 xpd->current = xpd->current->parent;
|
|
438 }
|
|
439 }
|
|
440
|
|
441 static void
|
|
442 xmlnode_parser_element_text_libxml(void *user_data, const xmlChar *text, int text_len)
|
|
443 {
|
|
444 struct _xmlnode_parser_data *xpd = user_data;
|
|
445
|
|
446 if(!xpd->current)
|
|
447 return;
|
|
448
|
|
449 if(!text || !text_len)
|
|
450 return;
|
|
451
|
|
452 xmlnode_insert_data(xpd->current, text, text_len);
|
|
453 }
|
|
454
|
|
455 #else
|
|
456
|
|
457 static void
|
|
458 xmlnode_parser_element_start(GMarkupParseContext *context,
|
|
459 const char *element_name, const char **attrib_names,
|
|
460 const char **attrib_values, gpointer user_data, GError **error)
|
|
461 {
|
|
462 struct _xmlnode_parser_data *xpd = user_data;
|
|
463 xmlnode *node;
|
|
464 int i;
|
|
465
|
|
466 if(!element_name) {
|
|
467 return;
|
|
468 } else {
|
|
469 if(xpd->current)
|
|
470 node = xmlnode_new_child(xpd->current, element_name);
|
|
471 else
|
|
472 node = xmlnode_new(element_name);
|
|
473
|
|
474 for(i=0; attrib_names[i]; i++)
|
|
475 xmlnode_set_attrib(node, attrib_names[i], attrib_values[i]);
|
|
476
|
|
477 xpd->current = node;
|
|
478 }
|
|
479 }
|
|
480
|
|
481 static void
|
|
482 xmlnode_parser_element_end(GMarkupParseContext *context,
|
|
483 const char *element_name, gpointer user_data, GError **error)
|
|
484 {
|
|
485 struct _xmlnode_parser_data *xpd = user_data;
|
|
486
|
|
487 if(!element_name || !xpd->current)
|
|
488 return;
|
|
489
|
|
490 if(xpd->current->parent) {
|
|
491 if(!strcmp(xpd->current->name, element_name))
|
|
492 xpd->current = xpd->current->parent;
|
|
493 }
|
|
494 }
|
|
495
|
|
496 static void
|
|
497 xmlnode_parser_element_text(GMarkupParseContext *context, const char *text,
|
|
498 gsize text_len, gpointer user_data, GError **error)
|
|
499 {
|
|
500 struct _xmlnode_parser_data *xpd = user_data;
|
|
501
|
|
502 if(!xpd->current)
|
|
503 return;
|
|
504
|
|
505 if(!text || !text_len)
|
|
506 return;
|
|
507
|
|
508 xmlnode_insert_data(xpd->current, text, text_len);
|
|
509 }
|
|
510 #endif
|
|
511
|
|
512 #ifdef HAVE_LIBXML
|
|
513 static xmlSAXHandler xmlnode_parser_libxml = {
|
|
514 .internalSubset = NULL,
|
|
515 .isStandalone = NULL,
|
|
516 .hasInternalSubset = NULL,
|
|
517 .hasExternalSubset = NULL,
|
|
518 .resolveEntity = NULL,
|
|
519 .getEntity = NULL,
|
|
520 .entityDecl = NULL,
|
|
521 .notationDecl = NULL,
|
|
522 .attributeDecl = NULL,
|
|
523 .elementDecl = NULL,
|
|
524 .unparsedEntityDecl = NULL,
|
|
525 .setDocumentLocator = NULL,
|
|
526 .startDocument = NULL,
|
|
527 .endDocument = NULL,
|
|
528 .startElement = NULL,
|
|
529 .endElement = NULL,
|
|
530 .reference = NULL,
|
|
531 .characters = xmlnode_parser_element_text_libxml,
|
|
532 .ignorableWhitespace = NULL,
|
|
533 .processingInstruction = NULL,
|
|
534 .comment = NULL,
|
|
535 .warning = NULL,
|
|
536 .error = NULL,
|
|
537 .fatalError = NULL,
|
|
538 .getParameterEntity = NULL,
|
|
539 .cdataBlock = NULL,
|
|
540 .externalSubset = NULL,
|
|
541 .initialized = XML_SAX2_MAGIC,
|
|
542 ._private = NULL,
|
|
543 .startElementNs = xmlnode_parser_element_start_libxml,
|
|
544 .endElementNs = xmlnode_parser_element_end_libxml,
|
|
545 .serror = NULL
|
|
546 };
|
|
547 #else
|
|
548 static GMarkupParser xmlnode_parser = {
|
|
549 xmlnode_parser_element_start,
|
|
550 xmlnode_parser_element_end,
|
|
551 xmlnode_parser_element_text,
|
|
552 NULL,
|
|
553 NULL
|
|
554 };
|
|
555 #endif
|
|
556
|
|
557 xmlnode *
|
|
558 xmlnode_from_str(const char *str, gssize size)
|
|
559 {
|
|
560 struct _xmlnode_parser_data *xpd;
|
|
561 xmlnode *ret;
|
|
562 #ifndef HAVE_LIBXML
|
|
563 GMarkupParseContext *context;
|
|
564 #endif
|
|
565 gsize real_size;
|
|
566
|
|
567 g_return_val_if_fail(str != NULL, NULL);
|
|
568
|
|
569 real_size = size < 0 ? strlen(str) : size;
|
|
570 xpd = g_new0(struct _xmlnode_parser_data, 1);
|
|
571
|
|
572 #ifdef HAVE_LIBXML
|
|
573 if (xmlSAXUserParseMemory(&xmlnode_parser_libxml, xpd, str, size) < 0) {
|
|
574 while(xpd->current && xpd->current->parent)
|
|
575 xpd->current = xpd->current->parent;
|
|
576 if(xpd->current)
|
|
577 xmlnode_free(xpd->current);
|
|
578 xpd->current = NULL;
|
|
579 }
|
|
580 #else
|
|
581 context = g_markup_parse_context_new(&xmlnode_parser, 0, xpd, NULL);
|
|
582
|
|
583 if(!g_markup_parse_context_parse(context, str, real_size, NULL)) {
|
|
584 while(xpd->current && xpd->current->parent)
|
|
585 xpd->current = xpd->current->parent;
|
|
586 if(xpd->current)
|
|
587 xmlnode_free(xpd->current);
|
|
588 xpd->current = NULL;
|
|
589 }
|
|
590 g_markup_parse_context_free(context);
|
|
591 #endif
|
|
592 ret = xpd->current;
|
|
593 g_free(xpd);
|
|
594 return ret;
|
|
595 }
|
|
596
|
|
597 xmlnode *
|
|
598 xmlnode_copy(xmlnode *src)
|
|
599 {
|
|
600 xmlnode *ret;
|
|
601 xmlnode *child;
|
|
602 xmlnode *sibling = NULL;
|
|
603
|
|
604 g_return_val_if_fail(src != NULL, NULL);
|
|
605
|
|
606 ret = new_node(src->name, src->type);
|
|
607 if(src->data) {
|
|
608 if(src->data_sz) {
|
|
609 ret->data = g_memdup(src->data, src->data_sz);
|
|
610 ret->data_sz = src->data_sz;
|
|
611 } else {
|
|
612 ret->data = g_strdup(src->data);
|
|
613 }
|
|
614 }
|
|
615
|
|
616 for(child = src->child; child; child = child->next) {
|
|
617 if(sibling) {
|
|
618 sibling->next = xmlnode_copy(child);
|
|
619 sibling = sibling->next;
|
|
620 } else {
|
|
621 ret->child = xmlnode_copy(child);
|
|
622 sibling = ret->child;
|
|
623 }
|
|
624 sibling->parent = ret;
|
|
625 }
|
|
626
|
|
627 ret->lastchild = sibling;
|
|
628
|
|
629 return ret;
|
|
630 }
|
|
631
|
|
632 xmlnode *
|
|
633 xmlnode_get_next_twin(xmlnode *node)
|
|
634 {
|
|
635 xmlnode *sibling;
|
|
636 const char *ns = xmlnode_get_namespace(node);
|
|
637
|
|
638 g_return_val_if_fail(node != NULL, NULL);
|
|
639 g_return_val_if_fail(node->type == XMLNODE_TYPE_TAG, NULL);
|
|
640
|
|
641 for(sibling = node->next; sibling; sibling = sibling->next) {
|
|
642 const char *xmlns = NULL;
|
|
643 if(ns)
|
|
644 xmlns = xmlnode_get_namespace(sibling);
|
|
645
|
|
646 if(sibling->type == XMLNODE_TYPE_TAG && !strcmp(node->name, sibling->name) &&
|
|
647 (!ns || (xmlns && !strcmp(ns, xmlns))))
|
|
648 return sibling;
|
|
649 }
|
|
650
|
|
651 return NULL;
|
|
652 }
|