Debian Codegraph @

libgdome/gdomecore/gdome-xml-nodel.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
2 /* gdome-xml-nodel.c
3 *
4 * Copyright (C) 1999 Raph Levien <[email protected]>
5 * Copyright (C) 2000 Mathieu Lacage <[email protected]>
6 * Copyright (C) 2000 Anders Carlsson <[email protected]>
7 * Copyright (C) 2001 Paolo Casarini <[email protected]>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27 #include <stdlib.h>
28 #include <string.h>
29 #include "gdome.h"
30 #include "gdome-refdebug.h"
31 #include <libxml/tree.h>
32 #include <libxml/parser.h>
33 #include <libxml/xmlmemory.h>
34 #include "gdome-xml-util.h"
35 #include "gdome-xml-node.h"
36 #include "gdome-xml-nodel.h"
37 #include "gdome-xml-str.h"
38 #include "gdome-xml-xmlutil.h"
39
40 const GdomeNodeListVtab gdome_xml_nl_vtab = {
41 gdome_xml_nl_ref,
42 gdome_xml_nl_unref,
43 gdome_xml_nl_query_interface,
44 gdome_xml_nl_item,
45 gdome_xml_nl_length
46 };
47
48 /**
49 * gdome_xml_nl_mkref:
50 * @root: Node Object root of the subtree wanted
51 * @tagName: name of tags wanted, may be "*" for all nodes or NULL
52 * @tagURI: namespaceURI of the tags wanted, may be "*" for any namespace or NULL
53 *
54 * Returns: a NodeList Objects ref that point to the subtree specified.
55 */
56 GdomeNodeList *
57 gdome_xml_nl_mkref (GdomeNode *root, GdomeDOMString *tagName, GdomeDOMString *tagURI, GdomeAccessType accessType) {
58 Gdome_xml_NodeList *result = NULL;
59 GdomeException exc;
60
61 if(root == NULL || (tagURI != NULL && tagName == NULL))
62 return NULL;
63
64 result = g_new (Gdome_xml_NodeList, 1);
65 memset(result, 0, sizeof(Gdome_xml_NodeList));
66 #ifdef DEBUG_REFCNT
67 gdome_refdbg_addRef ((void *)result, GDOME_REFDBG_NODELIST);
68 #endif
69
70 result->refcnt = 1;
71 gdome_xml_n_ref (root, &exc);
72 result->root = root;
73 if(tagName != NULL)
74 result->tagName = gdome_xml_str_mkref_dup(tagName->str);
75 if(tagURI != NULL)
76 result->tagURI = gdome_xml_str_mkref_dup(tagURI->str);
77 result->accessType = accessType;
78 result->vtab = &gdome_xml_nl_vtab;
79
80 return (GdomeNodeList *)result;
81 }
82
83 /**
84 * gdome_xml_nl_ref:
85 * @self: NodeList Object ref
86 * @exc: Exception Object ref
87 *
88 * Increase the reference count of the specified NodeList.
89 */
90 void
91 gdome_xml_nl_ref (GdomeNodeList *self, GdomeException *exc)
92 {
93 Gdome_xml_NodeList *priv = (Gdome_xml_NodeList *)self;
94
95 g_return_if_fail (priv != NULL);
96 g_return_if_fail (exc != NULL);
97
98 priv->refcnt++;
99 }
100
101 /**
102 * gdome_xml_nl_unref:
103 * @self: NodeList Object ref
104 * @exc: Exception Object ref
105 *
106 * Decrease the reference count of the specified NodeList. Free the NodeList
107 * structure if the specified NodeList will have zero reference.
108 */
109 void
110 gdome_xml_nl_unref (GdomeNodeList *self, GdomeException *exc)
111 {
112 Gdome_xml_NodeList *priv = (Gdome_xml_NodeList *)self;
113
114 g_return_if_fail (priv != NULL);
115 g_return_if_fail (exc != NULL);
116
117 if (--priv->refcnt == 0) {
118 gdome_xml_n_unref (priv->root, exc);
119 if(priv->tagName != NULL)
120 gdome_xml_str_unref(priv->tagName);
121 if(priv->tagURI != NULL)
122 gdome_xml_str_unref(priv->tagURI);
123 #ifdef DEBUG_REFCNT
124 gdome_refdbg_delRef ((void *)self, GDOME_REFDBG_NODELIST);
125 #endif
126 g_free (self);
127 }
128 }
129
130 /**
131 * gdome_xml_nl_query_interface:
132 * @self: NodeList Object ref
133 * @interface: interface needed
134 * @exc: Exception Object ref
135 *
136 * Returns: a reference to this object that implements the @interface needed,
137 * or %NULL if the @interface is not supported by this Object.
138 */
139 gpointer
140 gdome_xml_nl_query_interface (GdomeNodeList *self, const char *interface, GdomeException *exc)
141 {
142 Gdome_xml_NodeList *priv = (Gdome_xml_NodeList *)self;
143
144 g_return_val_if_fail (priv != NULL, NULL);
145 g_return_val_if_fail (interface != NULL, NULL);
146 g_return_val_if_fail (exc != NULL, NULL);
147
148 if (!strcmp (interface, "NodeList")) {
149 priv->refcnt++;
150 return self;
151 }
152 else
153 return NULL;
154 }
155
156 /**
157 * gdome_xml_nl_item:
158 * @self: NodeList Object ref
159 * @index: Index into the collection
160 * @exc: Exception Object ref
161 *
162 * Returns: the indexth item in the collection. If @index is greater than or
163 * equal to the number of nodes in the list, this returns %NULL.
164 */
165 GdomeNode *
166 gdome_xml_nl_item (GdomeNodeList *self, gulong index, GdomeException *exc)
167 {
168 Gdome_xml_NodeList *priv = (Gdome_xml_NodeList *)self;
169 xmlNode *root = NULL;
170 xmlNode *ret = NULL;
171 gulong cur = 0;
172
173 g_return_val_if_fail (priv != NULL, NULL);
174 g_return_val_if_fail (exc != NULL, NULL);
175
176 if (GDOME_XML_IS_LEAF_N (priv->root))
177 return NULL;
178
179 root = ((Gdome_xml_Node*)priv->root)->n;
180 if (priv->tagName != NULL && priv->tagURI != NULL)
181 ret = gdome_xmlNamedPreorderTraversal(root, (xmlChar *)priv->tagURI->str, (xmlChar *)priv->tagName->str, &cur, index+1);
182 else if (priv->tagName != NULL && priv->tagURI == NULL)
183 ret = gdome_xmlNamedPreorderTraversal(root, NULL, (xmlChar *)priv->tagName->str, &cur, index+1);
184 else
185 ret = gdome_xmlGetChildrenItem(root, index);
186
187 return gdome_xml_n_mkref (ret);
188 }
189
190 /**
191 * gdome_xml_nl_length:
192 * @self: NodeList Object ref
193 * @exc: Exception Object ref
194 *
195 * Returns: the number of nodes in the list. The range of valid child node
196 * indices is 0 to length-1 inclusive.
197 */
198 gulong
199 gdome_xml_nl_length (GdomeNodeList *self, GdomeException *exc)
200 {
201 Gdome_xml_NodeList *priv = (Gdome_xml_NodeList *)self;
202 xmlNode *root = NULL;
203 gulong ret = 0L;
204
205 g_return_val_if_fail (priv != NULL, 0L);
206 g_return_val_if_fail (exc != NULL, 0L);
207
208 if (GDOME_XML_IS_LEAF_N (priv->root))
209 return 0L;
210
211 root = ((Gdome_xml_Node*)priv->root)->n;
212 if (priv->tagName != NULL && priv->tagURI != NULL)
213 gdome_xmlNamedCntTreeElements(root, (xmlChar *)priv->tagURI->str, (xmlChar *)priv->tagName->str, &ret);
214 else if (priv->tagName != NULL && priv->tagURI == NULL)
215 gdome_xmlNamedCntTreeElements(root, NULL, (xmlChar *)priv->tagName->str, &ret);
216 else
217 ret = gdome_xmlGetChildrensLength(root);
218
219 return ret;
220 }