| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ |
| 2 |
/* gdome-xpath-xpnsresolv.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/tree.h> |
| 27 |
#include <libxml/xpath.h> |
| 28 |
#include "gdome.h" |
| 29 |
#include "gdome-xml-node.h" |
| 30 |
#include "gdome-xpath.h" |
| 31 |
#include "gdome-xpath-xpnsresolv.h" |
| 32 |
|
| 33 |
const GdomeXPathNSResolverVtab gdome_xpath_xpnsresolv_vtab = |
| 34 |
{ |
| 35 |
gdome_xpath_xpnsresolv_ref, |
| 36 |
gdome_xpath_xpnsresolv_unref, |
| 37 |
gdome_xpath_xpnsresolv_lookupNamespaceURI |
| 38 |
}; |
| 39 |
|
| 40 |
GdomeXPathNSResolver * |
| 41 |
gdome_xpath_xpnsresolv_mkref( GdomeNode *nodeResolver ) |
| 42 |
{ |
| 43 |
Gdome_xml_Node *priv = (Gdome_xml_Node *)nodeResolver; |
| 44 |
Gdome_xpath_XPathNSResolver *resolv = g_new (Gdome_xpath_XPathNSResolver, 1); |
| 45 |
GdomeException exc; |
| 46 |
|
| 47 |
resolv->vtab = &gdome_xpath_xpnsresolv_vtab; |
| 48 |
resolv->n = priv->n; |
| 49 |
resolv->refcnt = 1; |
| 50 |
resolv->gnode = nodeResolver; |
| 51 |
gdome_xml_n_ref ( nodeResolver, &exc); |
| 52 |
return (GdomeXPathNSResolver *)resolv; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* gdome_xpath_xpnsresolv_ref: |
| 57 |
* @self: XPathNSResolver Object ref |
| 58 |
* @exc: Exception Object ref |
| 59 |
* |
| 60 |
* Increase the reference count of the XPathNSResolver structure. |
| 61 |
*/ |
| 62 |
void |
| 63 |
gdome_xpath_xpnsresolv_ref (GdomeXPathNSResolver *self, GdomeException *exc) |
| 64 |
{ |
| 65 |
Gdome_xpath_XPathNSResolver *priv = (Gdome_xpath_XPathNSResolver *)self; |
| 66 |
|
| 67 |
g_return_if_fail (self != NULL); |
| 68 |
g_return_if_fail (exc != NULL); |
| 69 |
|
| 70 |
priv->refcnt++; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* gdome_xpath_xpnsresolv_unref: |
| 75 |
* @self: XPathNSResolver Object ref |
| 76 |
* @exc: Exception Object ref |
| 77 |
* |
| 78 |
* Decrease the reference count of the XPathNSResolver. Free the structure |
| 79 |
* if XPathNSResolver will have zero reference. |
| 80 |
*/ |
| 81 |
void |
| 82 |
gdome_xpath_xpnsresolv_unref (GdomeXPathNSResolver *self, GdomeException *exc) |
| 83 |
{ |
| 84 |
Gdome_xpath_XPathNSResolver *priv = (Gdome_xpath_XPathNSResolver *)self; |
| 85 |
|
| 86 |
g_return_if_fail (self != NULL); |
| 87 |
g_return_if_fail (exc != NULL); |
| 88 |
|
| 89 |
g_assert(priv->refcnt > 0); |
| 90 |
priv->refcnt--; |
| 91 |
|
| 92 |
if (priv->refcnt == 0) { |
| 93 |
gdome_xml_n_unref (priv->gnode, exc); |
| 94 |
g_free (self); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* gdome_xpath_xpnsresolv_lookupNamespaceURI: |
| 100 |
* @self: GdomeXPathNSResolver Object ref |
| 101 |
* @prefix: The prefix of the namespace to look for |
| 102 |
* @exc: Exception Object ref |
| 103 |
* |
| 104 |
* Look up the namespace URI associated to the given namespace prefix. The |
| 105 |
* XPath evaluator must never call this with a null or empty argument, because |
| 106 |
* the result of doing this is undefined. |
| 107 |
* Returns: namespace URI |
| 108 |
*/ |
| 109 |
GdomeDOMString * |
| 110 |
gdome_xpath_xpnsresolv_lookupNamespaceURI( GdomeXPathNSResolver *self, GdomeDOMString *prefix, GdomeException *exc) |
| 111 |
{ |
| 112 |
xmlNsPtr ns = NULL; |
| 113 |
Gdome_xpath_XPathNSResolver *priv = (Gdome_xpath_XPathNSResolver *)self; |
| 114 |
|
| 115 |
ns = xmlSearchNs(priv->n->doc, priv->n, prefix->str); |
| 116 |
if (ns != NULL) { |
| 117 |
return gdome_str_mkref_own ((gchar *)ns->href); |
| 118 |
} else { |
| 119 |
return NULL; |
| 120 |
} |
| 121 |
} |