Debian Codegraph @

libgdome/gdomecore/gdome-xml-xmlutil.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
2 /* gdome-xml-xmlutil.c
3 *
4 * Copyright (C) 2001 Paolo Casarini <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /* NOTE
22
23 This file contains some functions that work directly on the gnome-xml
24 tree structure. So if something change we'll have to work only on this
25 module.
26
27 */
28
29 #include <glib.h>
30 #include <libxml/tree.h>
31 #include <libxml/parser.h>
32 #include <libxml/xmlmemory.h>
33 #include <string.h>
34 #include "gdome.h"
35 #include "gdome-xml-xmlutil.h"
36
37 #ifndef INLINE
38 /**
39 * gdome_xmlGetPrevChild:
40 * @node: the node of which the previous sibling is wanted
41 *
42 * Returns: the previous child of the node specified.
43 */
44 xmlNode *
45 gdome_xmlGetPrev(xmlNode *node) {
46 if (node == NULL)
47 return NULL;
48
49 return node->prev;
50 }
51
52 /**
53 * gdome_xmlSetPrev:
54 * @node: the node of which the previous sibling is to be set
55 * @prev: the node to set as previous
56 *
57 * Sets <code>prev</code> as the previous sibling of <code>node</code>
58 */
59 void
60 gdome_xmlSetPrev(xmlNode *node, xmlNode *prev) {
61 if (node == NULL)
62 return;
63
64 node->prev = prev;
65 }
66
67 /**
68 * gdome_xmlGetNextChild:
69 * @node: the node of which the next child is wanted
70 *
71 * Returns: the next child of the node specified.
72 */
73 xmlNode *
74 gdome_xmlGetNext(xmlNode *node) {
75 if (node == NULL)
76 return NULL;
77
78 return node->next;
79 }
80
81 /**
82 * gdome_xmlSetNext:
83 * @node: the node of which the next sibling is to be set
84 * @prev: the node to set as next
85 *
86 * Sets <code>next</code> as the next sibling of <code>node</code>
87 */
88 void
89 gdome_xmlSetNext(xmlNode *node, xmlNode *next) {
90 if (node == NULL)
91 return;
92
93 node->next= next;
94 }
95
96 /**
97 * gdome_xmlGetFirstChild:
98 * @node: the node of which the first child is wanted
99 *
100 * Returns: the first child of the node specified.
101 */
102 xmlNode *
103 gdome_xmlGetFirstChild(xmlNode *node) {
104 if (node == NULL)
105 return NULL;
106
107 return node->children;
108 }
109
110 /**
111 * gdome_xmlSetLastChild:
112 * @node: the node of which the last child is to be set
113 * @last: the node to set as last child
114 *
115 * Sets @last as the last child of @node.
116 */
117 void
118 gdome_xmlSetFirstChild (xmlNode *node, xmlNode *first) {
119 if (node == NULL)
120 return;
121
122 node->children = first;
123 }
124
125 /**
126 * gdome_xmlGetLastChild:
127 * @node: the node of which the last child is wanted
128 *
129 * Returns: the last child of the node specified.
130 */
131 xmlNode *
132 gdome_xmlGetLastChild(xmlNode *node) {
133 if (node == NULL)
134 return NULL;
135
136 return node->last;
137 }
138
139 /**
140 * gdome_xmlSetLastChild:
141 * @node: the node of which the last child is to be set
142 * @last: the node to set as last child
143 *
144 * Sets @last as the last child of @node.
145 */
146 void gdome_xmlSetLastChild (xmlNode *node, xmlNode *last) {
147 if (node == NULL)
148 return;
149
150 node->last = last;
151 }
152
153 /**
154 * gdome_xmlGetAttrList:
155 * @elem: the element node of which the attribute list is wanted
156 *
157 * Returns: the first attribute of the list.
158 */
159 xmlAttr *
160 gdome_xmlGetAttrList(xmlNode *elem) {
161 if (elem == NULL)
162 return NULL;
163
164 return elem->properties;
165 }
166
167 /**
168 * gdome_xmlGetNsDeclList:
169 * @elem: the element node of which the namespace decl list is wanted
170 *
171 * Returns: the first namespace of the list.
172 */
173 xmlNs *
174 gdome_xmlGetNsDeclList(xmlNode *elem) {
175 if (elem == NULL)
176 return NULL;
177
178 return elem->nsDef;
179 }
180
181 /**
182 * gdome_xmlSetAttrList:
183 * @elem: the element of node which the attribute list is to be set
184 * @attr: the attribute that has to start the list
185 *
186 * Set the attribute list of the element specified to <code>attr</code>
187 */
188 void
189 gdome_xmlSetAttrList(xmlNode *elem, xmlAttr *attr) {
190 if(elem == NULL)
191 return;
192
193 elem->properties = attr;
194 }
195 #endif /* INLINE */
196
197 /**
198 * gdome_xmlSetNsDeclList:
199 * @elem: the element of node which the attribute list is to be set
200 * @attr: the namespace declarationattribute that has to start the list
201 *
202 * Set the namespace definition list of the element specified to
203 * <code>attr</code>
204 */
205 void
206 gdome_xmlSetNsDeclList(xmlNode *elem, xmlChar *prefix, xmlChar *href) {
207 xmlNs * ns;
208
209 if(elem == NULL)
210 return;
211
212 ns = xmlNewNs(elem, href, prefix);
213
214 elem->nsDef = ns;
215 }
216
217 #ifndef INLINE
218 /**
219 * gdome_xmlGetOwner:
220 * @node: the node of which the owner document is wanted
221 *
222 * Returns: the document that owns the node specified.
223 */
224 xmlDoc *
225 gdome_xmlGetOwner(xmlNode *node) {
226 if (node == NULL)
227 return NULL;
228
229 return node->doc;
230 }
231 #endif /* INLINE */
232
233 /**
234 * gdome_xmlGetOwnerOrSelf:
235 * @node: the node of which the owner document is wanted
236 *
237 * Returns: the document that owns the node specified,
238 * or the node itself if it turns out to be the document node
239 */
240 xmlDoc *
241 gdome_xmlGetOwnerOrSelf(xmlNode *node) {
242 if (node == NULL)
243 return NULL;
244
245 if (gdome_xmlGetType(node) == XML_DOCUMENT_NODE)
246 return (xmlDoc *) node;
247 else
248 return gdome_xmlGetOwner(node);
249 }
250
251 #ifndef INLINE
252 /**
253 * gdome_xmlSetOwner:
254 * @node: the node of which the owner document is to be set
255 *
256 * Set the owner of @node.
257 */
258 void
259 gdome_xmlSetOwner(xmlNode *node, xmlDoc *owner) {
260 if (node == NULL)
261 return;
262
263 node->doc = owner;
264 }
265
266 /**
267 * gdome_xmlGetParent:
268 * @node: the node of which the parent node is wanted
269 *
270 * Returns: the parent of the node specified.
271 */
272 xmlNode *
273 gdome_xmlGetParent(xmlNode *node) {
274 if (node == NULL)
275 return NULL;
276
277 return node->parent;
278 }
279
280 /**
281 * gdome_xmlSetParent:
282 * @node: the node of which the parent is to be set
283 * @parent: the parent to set
284 *
285 * Set the parent of @node.
286 */
287 void
288 gdome_xmlSetParent(xmlNode *node, xmlNode *parent) {
289 if (node == NULL)
290 return;
291
292 node->parent = parent;
293 }
294 #endif /* INLINE */
295
296 /**
297 * gdome_xmlGetNsPrefix:
298 * @node: the node of which the prefix is wanted
299 *
300 * Returns: the prefix of the node specified or %NULL if the node has no
301 * prefix.
302 */
303 const xmlChar *
304 gdome_xmlGetNsPrefix(xmlNode *node) {
305 if (node == NULL || node->ns == NULL)
306 return NULL;
307
308 return node->ns->prefix;
309 }
310
311 /**
312 * gdome_xmlGetNsURI:
313 * @node: the node of which the namespaceURI is wanted
314 *
315 * Returns: the namespaceURI of the node specified or %NULL if the node
316 * has no namespace.
317 */
318 const xmlChar *
319 gdome_xmlGetNsURI(xmlNode *node) {
320 if (node == NULL || node->ns == NULL)
321 return NULL;
322
323 return node->ns->href;
324 }
325
326 #ifndef INLINE
327 /**
328 * gdome_xmlGetNs:
329 * @node: the node of which the namespace is wanted
330 *
331 * Returns: the namespace of the node specified or %NULL if the node
332 * has no namespace.
333 */
334 xmlNs *
335 gdome_xmlGetNs(xmlNode *node) {
336 if (node == NULL)
337 return NULL;
338
339 return node->ns;
340 }
341
342 /**
343 * gdome_xmlSetNs:
344 * @node: the node of which the namespace is to be set
345 * @ns: the namespace to set
346 *
347 * Set @ns as namespace for @node
348 */
349 void
350 gdome_xmlSetNs (xmlNode *node, xmlNs *ns) {
351 if (node == NULL || ns == NULL)
352 return;
353
354 node->ns = ns;
355 }
356
357 /**
358 * gdome_xmlGetType:
359 * @node: the node of which the type is wanted
360 *
361 * Returns: the type of the node specified.
362 */
363 xmlElementType
364 gdome_xmlGetType(xmlNode *node) {
365 if (node == NULL)
366 return 0;
367
368 return node->type;
369 }
370
371 /**
372 * gdome_xmlGetContent:
373 * @node: the node of which the content is wanted
374 *
375 * Returns: the content of the node specified.
376 */
377 xmlChar *
378 gdome_xmlGetContent(xmlNode *node) {
379 if (node == NULL)
380 return NULL;
381
382 return node->content;
383 }
384
385 /**
386 * gdome_xmlGetName:
387 * @node: the node of which the name is wanted
388 *
389 * Returns: the name of the node specified.
390 */
391 const xmlChar *
392 gdome_xmlGetName(xmlNode *node) {
393 if (node == NULL)
394 return NULL;
395
396 return node->name;
397 }
398 #endif /* INLINE */
399
400 /**
401 * gdome_xmlGetAttr:
402 * @elem: the element node where find the attribute to unlink
403 * @name: the name of attribute to search
404 *
405 * Get the attribute specified by @name if exists.
406 * Returns: the attribute if found, otherwise %NULL
407 */
408 xmlAttr *
409 gdome_xmlGetAttr(xmlNode *elem, const xmlChar* name) {
410 xmlAttr *cur = NULL;
411 if(elem == NULL || name == NULL)
412 return NULL;
413
414 for(cur = elem->properties;
415 cur && !xmlStrEqual(name, gdome_xmlGetName((xmlNode *)cur));
416 cur = cur->next);
417
418 return cur;
419 }
420
421 /**
422 * gdome_xmlGetNsAttr:
423 * @elem: the element node where find the attribute to unlink
424 * @namespaceURI: the namespace URI of attribute to search
425 * @localName: the local name of the attribute to search
426 *
427 * Get the attribute specified by @namespaceURI and its
428 * @localName, if exists.
429 * Returns: the attribute if found, %NULL otherwise.
430 */
431 xmlAttr *
432 gdome_xmlGetNsAttr(xmlNode *elem, const xmlChar* namespaceURI, const xmlChar* localName) {
433 xmlAttr *cur = NULL;
434
435 if(elem == NULL || namespaceURI == NULL || localName == NULL)
436 return NULL;
437
438 for(cur = elem->properties;
439 cur != NULL &&
440 (!xmlStrEqual(localName, gdome_xmlGetName((xmlNode *)cur)) ||
441 ((cur->ns != NULL && !xmlStrEqual(namespaceURI, gdome_xmlGetNsURI((xmlNode *)cur))) ||
442 cur->ns == NULL));
443 cur = cur->next);
444
445 return cur;
446 }
447
448
449 /**
450 * gdome_xmlSetAttrValue:
451 * @attr: the attribute which the value is to be set
452 * @value: the value to set
453 *
454 * Set a new value to an Attribute node.
455 */
456 void
457 gdome_xmlSetAttrValue(xmlAttr *attr, xmlChar *value) {
458 if(attr == NULL)
459 return;
460
461 if (attr->children != NULL)
462 xmlFreeNodeList(attr->children);
463 attr->children = NULL;
464 attr->last = NULL;
465
466 if (value != NULL) {
467 xmlChar *buffer;
468 xmlNode *tmp;
469
470 buffer = xmlEncodeEntitiesReentrant(attr->doc, value);
471 attr->children = xmlStringGetNodeList(attr->doc, buffer);
472 attr->last = NULL;
473 tmp = attr->children;
474 for(tmp = attr->children; tmp != NULL; tmp = tmp->next) {
475 tmp->parent = (xmlNode *)attr;
476 tmp->doc = attr->doc;
477 if (tmp->next == NULL)
478 attr->last = tmp;
479 }
480 xmlFree (buffer);
481 }
482
483 return;
484 }
485
486 /**
487 * gdome_xmlUnlinkChild:
488 * @parent: the node that owns the node to unlink
489 * @child: the node to unlink
490 *
491 * Unlink the node specified, if exists
492 * Returns: the unlinked node if found, otherwise %NULL
493 */
494 xmlNode *
495 gdome_xmlUnlinkChild(xmlNode *parent, xmlNode* child) {
496
497 if(parent == NULL || child == NULL)
498 return NULL;
499
500 if(gdome_xmlGetParent(child) != parent)
501 return NULL;
502
503 if (child->prev)
504 child->prev->next = child->next;
505 else
506 parent->children = child->next;
507 if (child->next)
508 child->next->prev = child->prev;
509 else
510 parent->last = child->prev;
511 child->prev = child->next = NULL;
512 child->parent = NULL;
513
514 return child;
515 }
516
517 /**
518 * gdome_xmlAppendChild:
519 * @parent: the node that where append the child
520 * @child: the node to append
521 *
522 * Append @child at the end of the @parent children
523 * list. If @child is a %XML_DOCUMENT_FRAG_NODE, all its childrens
524 * are appended at the end of the @parent children list.
525 * Returns: the appended node.
526 */
527 xmlNode *
528 gdome_xmlAppendChild(xmlNode *parent, xmlNode* child) {
529 xmlNode *iter;
530
531 if (parent == NULL || child == NULL)
532 return NULL;
533
534 if (child->type == XML_DOCUMENT_FRAG_NODE) {
535 if (child->children != NULL && child->last != NULL) {
536 for (iter = child->children; iter != NULL; iter = iter->next)
537 iter->parent = parent;
538
539 if (parent->last) {
540 parent->last->next = child->children;
541 child->children->prev = parent->last;
542 } else {
543 parent->children = child->children;
544 child->children->prev = NULL;
545 }
546 parent->last = child->last;
547 child->last->next = NULL;
548 }
549 } else {
550 if (parent->last) {
551 parent->last->next = child;
552 child->prev = parent->last;
553 } else {
554 parent->children = child;
555 child->prev = NULL;
556 }
557 parent->last = child;
558 child->next = NULL;
559 child->parent = parent;
560 }
561
562 return child;
563 }
564
565 /**
566 * gdome_xmlInsertBeforeChild:
567 * @parent: the node where insert the child
568 * @newchild: the node to add
569 * @refchild: the node before which the new node must be inserted.
570 *
571 * Inserts @newchild before @refchild in the
572 * @parent list of childrens. If @newchild is a
573 * %XML_DOCUMENT_FRAG_NODE, all its childrens are inserted before
574 * @refchild in the @parent list of childrens.
575 * Returns: the inserted node or %NULL if @refchild doesn't belong to
576 * @parent.
577 */
578 xmlNode *
579 gdome_xmlInsertBeforeChild(xmlNode *parent, xmlNode* newchild, xmlNode *refchild) {
580 xmlNode *iter;
581
582 if (parent == NULL || newchild == NULL || refchild == NULL)
583 return NULL;
584
585 if (gdome_xmlGetParent(refchild) != parent)
586 return NULL;
587
588 if (newchild->type == XML_DOCUMENT_FRAG_NODE) {
589 if (newchild->children != NULL && newchild->last != NULL) {
590 for (iter = newchild->children; iter != NULL; iter = iter->next)
591 iter->parent = parent;
592
593 if (refchild->prev) {
594 refchild->prev->next = newchild->children;
595 newchild->children->prev = refchild->prev;
596 } else {
597 parent->children = newchild->children;
598 newchild->children->prev = NULL;
599 }
600 refchild->prev = newchild->last;
601 newchild->last->next = refchild;
602 }
603 } else {
604 if (refchild->prev) {
605 refchild->prev->next = newchild;
606 newchild->prev = refchild->prev;
607 } else {
608 parent->children = newchild;
609 newchild->prev = NULL;
610 }
611 refchild->prev = newchild;
612 newchild->next = refchild;
613 newchild->parent = parent;
614 }
615
616 return newchild;
617 }
618
619 /**
620 * gdome_xmlUnlinkAttr:
621 * @elem: the element node where find the attribute to unlink
622 * @name: the name of attribute to search
623 *
624 * Unlink the attribute specified by @name, if an attribute with
625 * such name exists in @elem.
626 * Returns: the attribute unlinked if found, otherwise %NULL.
627 */
628 xmlAttr *
629 gdome_xmlUnlinkAttr(xmlNode *elem, const xmlChar* name) {
630 xmlAttr *cur = NULL;
631
632 if(elem == NULL || name == NULL)
633 return NULL;
634
635 cur = gdome_xmlGetAttr(elem, name);
636
637 if(cur) {
638 if (cur->prev)
639 cur->prev->next = cur->next;
640 else
641 elem->properties = cur->next;
642 if (cur->next)
643 cur->next->prev = cur->prev;
644 cur->prev = cur->next = NULL;
645 cur->parent = NULL;
646 }
647
648 return cur;
649 }
650
651 /**
652 * gdome_xmlUnlinkNsAttr:
653 * @elem: the element node where find the attribute to unlink
654 * @namespaceURI: the namespace URI of attribute to search
655 * @localName: the local name of the attribute to search
656 *
657 * Unlink the attribute specified by @namespaceURI and its
658 * @localName, if an attribute with such a local name and
659 * namespace URI exists in @elem.
660 * Returns: the attribute unlinked if found, %NULL otherwise.
661 */
662 xmlAttr *
663 gdome_xmlUnlinkNsAttr(xmlNode *elem, const xmlChar* namespaceURI, const xmlChar* localName) {
664 xmlAttr *cur = NULL;
665
666 if(elem == NULL || namespaceURI == NULL || localName == NULL)
667 return NULL;
668
669 cur = gdome_xmlGetNsAttr(elem, namespaceURI, localName);
670
671 if(cur) {
672 if (cur->prev)
673 cur->prev->next = cur->next;
674 else
675 elem->properties = cur->next;
676 if (cur->next)
677 cur->next->prev = cur->prev;
678 cur->prev = cur->next = NULL;
679 cur->parent = NULL;
680 return cur;
681 }
682
683 return NULL;
684 }
685
686 /**
687 * gdome_xmlUnlinkNsDecl:
688 * @elem: the element node where find the attribute to unlink
689 * @localName: the name of attribute to search
690 *
691 * Unlink the attribute specified by @name, if an attribute with
692 * such name exists in @elem.
693 * Returns: the attribute unlinked if found, otherwise %NULL.
694 */
695 xmlNs *
696 gdome_xmlUnlinkNsDecl(xmlNode *elem, const xmlChar* localName) {
697 xmlNs *cur = NULL;
698 xmlNs *prev = NULL;
699
700 if(elem == NULL)
701 return NULL;
702
703 if (localName == NULL || xmlStrEqual(localName, "")) {
704 cur = elem->nsDef;
705 while (cur != NULL) {
706 if (cur->prefix == NULL) {
707 if (prev)
708 prev->next = cur->next;
709 else
710 elem->nsDef = cur->next;
711 cur->next = NULL;
712 return cur;
713 }
714 prev = cur;
715 cur = cur->next;
716 }
717 } else {
718 cur = elem->nsDef;
719 while (cur != NULL) {
720 if (cur->prefix != NULL && xmlStrEqual(localName, cur->prefix)) {
721 if (prev)
722 prev->next = cur->next;
723 else
724 elem->nsDef = cur->next;
725 cur->next = NULL;
726 return cur;
727 }
728 prev = cur;
729 cur = cur->next;
730 }
731 }
732
733 return NULL;
734 }
735
736 /**
737 * gdome_xmlLinkAttr:
738 * @elem: the element node where add the attribute
739 * @attr: the attribute to add
740 *
741 * Link @attribute to the @elem.
742 * Returns: -1 if fails, 0 otherwise.
743 */
744 int
745 gdome_xmlLinkAttr(xmlNode *elem, xmlAttr *attr) {
746 xmlAttr *cur = NULL;
747
748 if (elem == NULL || attr == NULL)
749 return -1;
750
751 gdome_xmlSetParent((xmlNode *)attr, (xmlNode *)elem);
752 gdome_xmlSetOwner((xmlNode *)attr, gdome_xmlGetOwner(elem));
753
754 if(gdome_xmlGetAttrList(elem) == NULL) {
755 gdome_xmlSetAttrList(elem, attr);
756 return 0;
757 } else {
758 for(cur = gdome_xmlGetAttrList(elem); cur->next != NULL; cur = cur->next);
759
760 cur->next = attr;
761 attr->prev = cur;
762 return 0;
763 }
764 }
765
766 /**
767 * gdome_xmlLinkNsDecl:
768 * @elem: the element node where add the namespace declaration
769 * @attr: the namespace declaration attribute to add
770 *
771 * Link @attr to the @elem.
772 * Returns: -1 if fails, 0 otherwise.
773 */
774 int
775 gdome_xmlLinkNsDecl(xmlNode *elem, xmlChar *prefix, xmlChar *href) {
776 xmlNs *cur = NULL;
777 xmlNs * ns;
778
779 if (elem == NULL)
780 return -1;
781
782 if(gdome_xmlGetNsDeclList(elem) == NULL) {
783 gdome_xmlSetNsDeclList(elem, prefix, href);
784 return 0;
785 } else {
786 for(cur = gdome_xmlGetNsDeclList(elem); cur->next != NULL; cur = cur->next);
787 ns = xmlNewNs(elem, href, prefix);
788 cur->next = ns;
789 return 0;
790 }
791 }
792
793 /**
794 * gdome_xmlGetChildrenItem:
795 * @parent: The node of which the children this is to be scan
796 * @index: The index of the children wanted
797 *
798 * Returns: the index-th children of the parent specified.
799 */
800 xmlNode *
801 gdome_xmlGetChildrenItem (xmlNode *parent, gulong index) {
802 xmlNode *ret = NULL;
803 gulong cur;
804
805 for(ret = parent->children, cur = 0;
806 ret != NULL && cur < index;
807 cur++, ret = ret->next);
808
809 return ret;
810 }
811
812 /**
813 * gdome_xmlNamedPreorderTraversal:
814 * @root: The root node of the subtree to scan
815 * @tagURI: the namespaceURI of the nodes to select or NULL if no namespace
816 * check is wanted
817 * @tagName: the localName of the nodes to select
818 * @cur: The current node counter (must be 0L)
819 * @index: The index of node wanted
820 *
821 * Returns: the index-th node of the subtree with the localName and nameSpace
822 * specified.
823 */
824 xmlNode *
825 gdome_xmlNamedPreorderTraversal (xmlNode *root, xmlChar *tagURI, xmlChar *tagName, gulong *cur, gulong index) {
826 xmlNode *iter, *ret = NULL;
827
828 iter = root->children;
829
830 if (*cur == index)
831 return root;
832 else if (iter == NULL)
833 return NULL;
834
835 while(iter != NULL && *cur<index) {
836 if(iter->type == XML_ELEMENT_NODE &&
837 (xmlStrEqual(gdome_xmlGetName(iter), tagName) ||
838 xmlStrEqual(tagName, "*"))) {
839 if(tagURI != NULL &&
840 (xmlStrEqual(gdome_xmlGetNsURI(iter), tagURI) ||
841 xmlStrEqual(tagURI, "*")))
842 (*cur)++;
843 else if(tagURI == NULL)
844 (*cur)++;
845 }
846 ret = gdome_xmlNamedPreorderTraversal(iter, tagURI, tagName, cur, index);
847 if(ret == NULL)
848 iter = iter->next;
849 }
850
851 return ret;
852 }
853
854 /**
855 * gdome_xmlGetChildrensLength:
856 * @parent: The node of which the length of the children list is wanted
857 *
858 * Returns: the length of the children list of the node specified.
859 */
860 gulong
861 gdome_xmlGetChildrensLength (xmlNode *parent) {
862 xmlNode *iter = NULL;
863 gulong ret;
864
865 for(iter = parent->children, ret = 0;
866 iter != NULL;
867 ret++, iter = iter->next);
868
869 return ret;
870 }
871
872 /**
873 * gdome_xmlNamedCntTreeElementes:
874 * @root: The root node of the subtree to scan
875 * @tagURI: the namespaceURI of the nodes to select or NULL if no namespace
876 * check is wanted
877 * @tagName: the localName of the nodes to select
878 * @cur: The current node counter (must be %0L)
879 *
880 * Sets in @cur the length of thr subtree specified.
881 */
882 void
883 gdome_xmlNamedCntTreeElements (xmlNode *root, xmlChar *tagURI, xmlChar *tagName, gulong *cur) {
884 xmlNode *iter;
885
886 iter = root->children;
887
888 if (iter == NULL)
889 return;
890
891 while(iter != NULL) {
892 if(iter->type == XML_ELEMENT_NODE &&
893 (xmlStrEqual(gdome_xmlGetName(iter), tagName) ||
894 xmlStrEqual(tagName, "*"))) {
895 if(tagURI != NULL &&
896 (xmlStrEqual(gdome_xmlGetNsURI(iter), tagURI) ||
897 xmlStrEqual(tagURI, "*")))
898 (*cur)++;
899 else if(tagURI == NULL)
900 (*cur)++;
901 }
902 gdome_xmlNamedCntTreeElements (iter, tagURI, tagName, cur);
903 iter = iter->next;
904 }
905
906 return;
907 }
908
909 /*
910 * gdome_xmlGetNsDecl:
911 * @elem: Element on which to look for namespace declaration attribute
912 * @name: namespace declaration prefix
913 *
914 * Similar to xmlGetProp, but gets the namespace declaration attributes.
915 * Note that expects name to not include 'xmlns' string
916 */
917 xmlNs *
918 gdome_xmlGetNsDecl (xmlNode *node, xmlChar *localName) {
919 xmlNsPtr cur;
920 xmlNs *ret = NULL;
921 if (node == NULL)
922 return NULL;
923
924 if (localName == NULL || xmlStrEqual(localName, "")) {
925 cur = node->nsDef;
926 while (cur != NULL) {
927 if (cur->prefix == NULL) {
928 ret = cur;
929 break;
930 }
931 cur = cur->next;
932 }
933 } else {
934 cur = node->nsDef;
935 while (cur != NULL) {
936 if (cur->prefix != NULL && xmlStrEqual(localName, cur->prefix)) {
937 ret = cur;
938 break;
939 }
940 cur = cur->next;
941 }
942 }
943 return ret;
944 }
945
946 /*
947 * gdome_xmlNewNsDecl
948 * @node: The holding node
949 * @prefix: namespace declaration prefix
950 * @value: namespace URI
951 *
952 * Similar to xmlNewProp, but does not attach attribute to owner element's
953 * children.
954 */
955 xmlAttrPtr
956 gdome_xmlNewNsDecl(xmlNodePtr node, const xmlChar *prefix, const xmlChar *value) {
957 xmlAttrPtr cur;
958 xmlDocPtr doc = NULL;
959
960 /*
961 * Allocate a new property and fill the fields.
962 */
963 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
964 if (cur == NULL) {
965 xmlGenericError(xmlGenericErrorContext,
966 "xmlNewProp : malloc failed\n");
967 return(NULL);
968 }
969 memset(cur, 0, sizeof(xmlAttr));
970 cur->type = XML_ATTRIBUTE_NODE;
971
972 cur->parent = node;
973 if (node != NULL) {
974 doc = node->doc;
975 cur->doc = doc;
976 }
977 if (prefix == NULL) {
978 cur->name = g_strdup("xmlns");
979 } else {
980 cur->name = g_strdup(prefix);
981 cur->ns = gdome_xmlNewNs (doc, GDOME_XMLNS_NAMESPACE, "xmlns");
982 }
983 if (value != NULL) {
984 xmlChar *buffer;
985 xmlNodePtr tmp;
986
987 buffer = xmlEncodeEntitiesReentrant(doc, value);
988 cur->children = xmlStringGetNodeList(doc, buffer);
989 cur->last = NULL;
990 tmp = cur->children;
991 while (tmp != NULL) {
992 tmp->parent = (xmlNodePtr) cur;
993 tmp->doc = doc;
994 if (tmp->next == NULL)
995 cur->last = tmp;
996 tmp = tmp->next;
997 }
998 xmlFree(buffer);
999 }
1000
1001 return(cur);
1002 }
1003
1004 /*
1005 * gdome_xmlIsNsDecl:
1006 * @a: Attribute node
1007 *
1008 * Returns %TRUE if attribute is namespace declaration node, %FALSE otherwise.
1009 */
1010 GdomeBoolean
1011 gdome_xmlIsNsDecl (xmlAttr *a) {
1012 if (xmlStrEqual(a->name, "xmlns"))
1013 return 1;
1014 if (a->ns != NULL)
1015 if (xmlStrEqual(a->ns->href, GDOME_XMLNS_NAMESPACE))
1016 return 1;
1017 return 0;
1018 }
1019
1020 /*
1021 * gdome_xmlGetNsDeclByAttr:
1022 * @a: Attribute Node
1023 *
1024 * Get pointer to namespace node corresponding to namespace declaration
1025 * attribute. Used by getUserData and setUserData.
1026 */
1027 xmlNs *
1028 gdome_xmlGetNsDeclByAttr (xmlAttr *a) {
1029 if (a->ns == NULL && xmlStrEqual(a->name, "xmlns"))
1030 return gdome_xmlGetNsDecl(a->parent, NULL);
1031 else if (a->ns != NULL && xmlStrEqual(a->ns->href, GDOME_XMLNS_NAMESPACE))
1032 return gdome_xmlGetNsDecl(a->parent, (xmlChar *)a->name);
1033 else
1034 return NULL;
1035 }
1036
1037 /*
1038 * gdome_xmlSetOldNs:
1039 * @doc: Document node
1040 * @ns: Namespace node
1041 *
1042 * Adds namespace node to libxml's oldNs structure, so that it will
1043 * get freed when the document is freed.
1044 */
1045 void
1046 gdome_xmlSetOldNs (xmlDoc *doc, xmlNs *ns) {
1047 /*
1048 xmlNs *cur;
1049 */
1050 if (doc == NULL)
1051 return;
1052
1053 if (doc->oldNs == NULL) {
1054 /*
1055 * Allocate a new Namespace and fill the fields.
1056 */
1057 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
1058 if (doc->oldNs == NULL) {
1059 xmlGenericError(xmlGenericErrorContext,
1060 "xmlSearchNsByHref : malloc failed\n");
1061 return;
1062 }
1063 memset(doc->oldNs, 0, sizeof(xmlNs));
1064 doc->oldNs->type = XML_LOCAL_NAMESPACE;
1065
1066 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
1067 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
1068 }
1069
1070 ns->next = doc->oldNs;
1071 doc->oldNs = ns;
1072 /*
1073 cur = doc->oldNs;
1074 while (cur->next != NULL) {
1075 cur = cur->next;
1076 }
1077 cur->next = ns;
1078 */
1079 }
1080
1081 xmlNs*
1082 gdome_xmlNewNs (xmlDoc *doc, const xmlChar *href, const xmlChar *prefix)
1083 {
1084 xmlNs *cur;
1085
1086 g_return_val_if_fail (doc != NULL, NULL);
1087 g_return_val_if_fail (href != NULL, NULL);
1088
1089 cur = doc->oldNs;
1090 while (cur != NULL &&
1091 ((cur->prefix == NULL && prefix != NULL) ||
1092 (cur->prefix != NULL && prefix == NULL) ||
1093 !xmlStrEqual (cur->prefix, prefix)) &&
1094 !xmlStrEqual (cur->href, href))
1095 cur = cur->next;
1096
1097 if (cur == NULL) {
1098 cur = xmlNewNs (NULL, href, prefix);
1099 cur->next = doc->oldNs;
1100 doc->oldNs = cur;
1101 }
1102
1103 return cur;
1104 }