| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ |
| 2 |
/* gdome-xpath-xpresult.c |
| 3 |
* |
| 4 |
* CopyRight (C) 2002 T.J. Mather <[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 |
#ifdef HAVE_CONFIG_H |
| 22 |
# include <config.h> |
| 23 |
#endif |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include <libxml/xpath.h> |
| 27 |
#include "gdome.h" |
| 28 |
#include "gdome-xpath.h" |
| 29 |
#include "gdome-xpath-xpresult.h" |
| 30 |
#include "gdome-xml-node.h" |
| 31 |
|
| 32 |
const GdomeXPathResultVtab gdome_xpath_xpresult_vtab = |
| 33 |
{ |
| 34 |
gdome_xpath_xpresult_ref, |
| 35 |
gdome_xpath_xpresult_unref, |
| 36 |
gdome_xpath_xpresult_resultType, |
| 37 |
gdome_xpath_xpresult_booleanValue, |
| 38 |
gdome_xpath_xpresult_numberValue, |
| 39 |
gdome_xpath_xpresult_stringValue, |
| 40 |
gdome_xpath_xpresult_singleNodeValue, |
| 41 |
gdome_xpath_xpresult_iterateNext |
| 42 |
}; |
| 43 |
|
| 44 |
GdomeXPathResult * |
| 45 |
gdome_xpath_xpresult_mkref (GdomeNode *gnode, xmlXPathObjectPtr res) |
| 46 |
{ |
| 47 |
Gdome_xpath_XPathResult *result = NULL; |
| 48 |
GdomeException exc; |
| 49 |
|
| 50 |
result = g_new (Gdome_xpath_XPathResult, 1); |
| 51 |
result->res = res; |
| 52 |
result->refcnt = 1; |
| 53 |
result->gnode = gnode; |
| 54 |
result->pos = 0; |
| 55 |
result->vtab = &gdome_xpath_xpresult_vtab; |
| 56 |
|
| 57 |
gdome_xml_n_ref (gnode, &exc); |
| 58 |
|
| 59 |
return (GdomeXPathResult *)result; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* gdome_xpath_xpresult_ref: |
| 64 |
* @self: XPathResult Object ref |
| 65 |
* @exc: Exception Object ref |
| 66 |
* |
| 67 |
* Increase the reference count of the XPathResult structure. |
| 68 |
*/ |
| 69 |
void |
| 70 |
gdome_xpath_xpresult_ref (GdomeXPathResult *self, GdomeException *exc) |
| 71 |
{ |
| 72 |
Gdome_xpath_XPathResult *priv = (Gdome_xpath_XPathResult *)self; |
| 73 |
|
| 74 |
g_return_if_fail (self != NULL); |
| 75 |
g_return_if_fail (exc != NULL); |
| 76 |
|
| 77 |
priv->refcnt++; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* gdome_xpath_xpresult_unref: |
| 82 |
* @self: XPathResult Object ref |
| 83 |
* @exc: Exception Object ref |
| 84 |
* |
| 85 |
* Decrease the reference count of the XPathResult. Free the structure |
| 86 |
* if XPathResult will have zero reference. |
| 87 |
*/ |
| 88 |
void |
| 89 |
gdome_xpath_xpresult_unref (GdomeXPathResult *self, GdomeException *exc) |
| 90 |
{ |
| 91 |
Gdome_xpath_XPathResult *priv = (Gdome_xpath_XPathResult *)self; |
| 92 |
|
| 93 |
g_return_if_fail (self != NULL); |
| 94 |
g_return_if_fail (exc != NULL); |
| 95 |
|
| 96 |
g_assert(priv->refcnt > 0); |
| 97 |
priv->refcnt--; |
| 98 |
|
| 99 |
if (priv->refcnt == 0) { |
| 100 |
gdome_xml_n_unref (priv->gnode, exc); |
| 101 |
/* xmlXPathFreeNodeSet(priv->res->nodesetval); |
| 102 |
priv->res->nodesetval = 0;*/ |
| 103 |
xmlXPathFreeObject(priv->res); |
| 104 |
g_free (self); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* gdome_xpath_xpresult_resultType: |
| 110 |
* @self: GdomeXPathResult Object ref |
| 111 |
* @exc: Exception Object ref |
| 112 |
* |
| 113 |
* Returns: a code representing the type of this result, as defined by the type |
| 114 |
* constants. |
| 115 |
*/ |
| 116 |
unsigned short |
| 117 |
gdome_xpath_xpresult_resultType (GdomeXPathResult *self, GdomeException *exc) |
| 118 |
{ |
| 119 |
Gdome_xpath_XPathResult *result = (Gdome_xpath_XPathResult *) self; |
| 120 |
|
| 121 |
if (result->res == NULL) { |
| 122 |
return GDOME_ANY_TYPE; |
| 123 |
} else { |
| 124 |
switch (result->res->type) { |
| 125 |
case XPATH_NODESET: |
| 126 |
return GDOME_ORDERED_NODE_ITERATOR_TYPE; |
| 127 |
case XPATH_BOOLEAN: |
| 128 |
return GDOME_BOOLEAN_TYPE; |
| 129 |
case XPATH_NUMBER: |
| 130 |
return GDOME_NUMBER_TYPE; |
| 131 |
case XPATH_STRING: |
| 132 |
return GDOME_STRING_TYPE; |
| 133 |
default: |
| 134 |
return GDOME_ANY_TYPE; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* gdome_xpath_xpresult_booleanValue: |
| 141 |
* @self: GdomeXPathResult Object ref |
| 142 |
* @exc: Exception Object ref |
| 143 |
* |
| 144 |
* Returns: the value of this boolean result. |
| 145 |
*/ |
| 146 |
GdomeBoolean |
| 147 |
gdome_xpath_xpresult_booleanValue (GdomeXPathResult *self, GdomeException *exc) |
| 148 |
{ |
| 149 |
Gdome_xpath_XPathResult *result = (Gdome_xpath_XPathResult *) self; |
| 150 |
if (result->res->type == XPATH_BOOLEAN) { |
| 151 |
return result->res->boolval; |
| 152 |
} else { |
| 153 |
*exc = GDOME_TYPE_ERR; |
| 154 |
return 0; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* gdome_xpath_xpresult_numberValue: |
| 160 |
* @self: GdomeXPathResult Object ref |
| 161 |
* @exc: Exception Object ref |
| 162 |
* |
| 163 |
* Returns: the value of this number result. |
| 164 |
*/ |
| 165 |
double |
| 166 |
gdome_xpath_xpresult_numberValue (GdomeXPathResult *self, GdomeException *exc) |
| 167 |
{ |
| 168 |
Gdome_xpath_XPathResult *result = (Gdome_xpath_XPathResult *) self; |
| 169 |
if (result->res->type == XPATH_NUMBER) { |
| 170 |
return result->res->floatval; |
| 171 |
} else { |
| 172 |
*exc = GDOME_TYPE_ERR; |
| 173 |
return 0; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* gdome_xpath_xpresult_stringValue: |
| 179 |
* @self: GdomeXPathResult Object ref |
| 180 |
* @exc: Exception Object ref |
| 181 |
* |
| 182 |
* Returns: the value of this GdomeDOMString result. |
| 183 |
*/ |
| 184 |
GdomeDOMString * |
| 185 |
gdome_xpath_xpresult_stringValue (GdomeXPathResult *self, GdomeException *exc) |
| 186 |
{ |
| 187 |
Gdome_xpath_XPathResult *result = (Gdome_xpath_XPathResult *) self; |
| 188 |
if (result->res->type == XPATH_STRING) { |
| 189 |
return gdome_str_mkref_dup (result->res->stringval); |
| 190 |
} else { |
| 191 |
*exc = GDOME_TYPE_ERR; |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* gdome_xpath_xpresult_singleNodeValue: |
| 198 |
* @self: GdomeXPathResult Object ref |
| 199 |
* @exc: Exception Object ref |
| 200 |
* |
| 201 |
* Returns: The value of this single node result, which may be %NULL. This result is not |
| 202 |
* guaranteed to be the first node in document order where the expression |
| 203 |
* evaluates to multiple nodes. |
| 204 |
*/ |
| 205 |
GdomeNode * |
| 206 |
gdome_xpath_xpresult_singleNodeValue (GdomeXPathResult *self, GdomeException *exc) |
| 207 |
{ |
| 208 |
xmlNodeSetPtr nodelist = NULL; |
| 209 |
Gdome_xpath_XPathResult *result = (Gdome_xpath_XPathResult *) self; |
| 210 |
if (result->res->type == XPATH_NODESET) { |
| 211 |
nodelist = result->res->nodesetval; |
| 212 |
if ( nodelist && nodelist->nodeNr > 0 ) { |
| 213 |
return (GdomeNode *)gdome_xml_n_mkref (nodelist->nodeTab[0]); |
| 214 |
} else { |
| 215 |
return NULL; |
| 216 |
} |
| 217 |
} else { |
| 218 |
*exc = GDOME_TYPE_ERR; |
| 219 |
return NULL; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* gdome_xpath_xpresult_iterateNext: |
| 225 |
* @self: GdomeXPathResult Object ref |
| 226 |
* @exc: Exception Object ref |
| 227 |
* |
| 228 |
* Iterates and returns the next node from the node set or %NULL if there |
| 229 |
* are no more nodes. |
| 230 |
* Returns: the next node. |
| 231 |
*/ |
| 232 |
GdomeNode * |
| 233 |
gdome_xpath_xpresult_iterateNext (GdomeXPathResult *self, GdomeException *exc) |
| 234 |
{ |
| 235 |
Gdome_xpath_XPathResult *result = (Gdome_xpath_XPathResult *) self; |
| 236 |
xmlNodeSetPtr nsp = result->res->nodesetval; |
| 237 |
int pos = result->pos; |
| 238 |
xmlNodePtr tnode; |
| 239 |
|
| 240 |
if (nsp == NULL) |
| 241 |
return NULL; |
| 242 |
|
| 243 |
if (pos >= nsp->nodeNr) |
| 244 |
return NULL; |
| 245 |
|
| 246 |
tnode = nsp->nodeTab[pos]; |
| 247 |
result->pos++; |
| 248 |
|
| 249 |
return gdome_xml_n_mkref (tnode); |
| 250 |
} |