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