Debian Codegraph @

libgdome/xpath/gdome-xpath-xpeval.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
2 /* gdome-xpath-xpeval.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-xpath.h"
30 #include "gdome-xpath-xpeval.h"
31 #include "gdome-xpath-xpnsresolv.h"
32 #include "gdome-xpath-xpresult.h"
33 #include "gdome-xml-node.h"
34
35 Gdome_xpath_XPathEvaluator *gdome_xpath_XPathEvaluator = NULL;
36
37 const GdomeXPathEvaluatorVtab gdome_xpath_xpeval_vtab =
38 {
39 gdome_xpath_xpeval_ref,
40 gdome_xpath_xpeval_unref,
41 gdome_xpath_xpeval_createNSResolver,
42 gdome_xpath_xpeval_createResult,
43 gdome_xpath_xpeval_evaluate
44 };
45
46 /**
47 * gdome_xpath_xpeval_mkref:
48 *
49 * Constructor of the XPathEvaluator interface.
50 * Returns: the reference to the XPathEvaluator structure.
51 */
52 GdomeXPathEvaluator *
53 gdome_xpath_xpeval_mkref (void)
54 {
55 if (gdome_xpath_XPathEvaluator == NULL) {
56 gdome_xpath_XPathEvaluator = g_new (Gdome_xpath_XPathEvaluator, 1);
57 gdome_xpath_XPathEvaluator->refcnt = 1;
58 gdome_xpath_XPathEvaluator->vtab = &gdome_xpath_xpeval_vtab;
59 } else
60 gdome_xpath_XPathEvaluator->refcnt++;
61
62 return (GdomeXPathEvaluator *)gdome_xpath_XPathEvaluator;
63 }
64
65 /**
66 * gdome_xpath_xpeval_ref:
67 * @self: XPathEvaluator Object ref
68 * @exc: Exception Object ref
69 *
70 * Increase the reference count of the XPathEvaluator structure.
71 */
72 void
73 gdome_xpath_xpeval_ref (GdomeXPathEvaluator *self, GdomeException *exc)
74 {
75 Gdome_xpath_XPathEvaluator *priv = (Gdome_xpath_XPathEvaluator *)self;
76
77 g_return_if_fail (self != NULL);
78 g_return_if_fail (exc != NULL);
79
80 g_assert(self == (GdomeXPathEvaluator *)gdome_xpath_XPathEvaluator);
81
82 priv->refcnt++;
83 }
84
85 /**
86 * gdome_xpath_xpeval_unref:
87 * @self: XPathEvaluator Object ref
88 * @exc: Exception Object ref
89 *
90 * Decrease the reference count of the XPathEvaluator. Free the structure
91 * if XPathEvaluator will have zero reference.
92 */
93 void
94 gdome_xpath_xpeval_unref (GdomeXPathEvaluator *self, GdomeException *exc)
95 {
96 Gdome_xpath_XPathEvaluator *priv = (Gdome_xpath_XPathEvaluator *)self;
97
98 g_return_if_fail (self != NULL);
99 g_return_if_fail (exc != NULL);
100
101 g_assert(self == (GdomeXPathEvaluator *)gdome_xpath_XPathEvaluator);
102
103 g_assert(priv->refcnt > 0);
104 priv->refcnt--;
105
106 if (priv->refcnt == 0) {
107 g_free (self);
108 gdome_xpath_XPathEvaluator = NULL;
109 }
110 }
111
112 /**
113 * gdome_xpath_xpeval_createNSResolver:
114 * @self: GdomeXPathEvaluator Object ref
115 * @nodeResolver: The node to be used as a context for namespace resolution.
116 * @exc: Exception Object ref
117 *
118 * Adapts any DOM node to resolve namespaces so that an XPath expression
119 * can be easily evaluated relative to the context of the node where it
120 * appeared within the document.
121 * Returns: GdomeXPathNSResolver object.
122 */
123 GdomeXPathNSResolver *
124 gdome_xpath_xpeval_createNSResolver (GdomeXPathEvaluator *self, GdomeNode *nodeResolver, GdomeException *exc)
125 {
126 return (GdomeXPathNSResolver *)gdome_xpath_xpnsresolv_mkref(nodeResolver);
127 }
128
129 /**
130 * gdome_xpath_xpeval_createResult:
131 * @self: GdomeXPathEvaluator Object ref
132 * @exc: Exception Object ref
133 *
134 * Returns: an GdomeXPathResult object which may be passed as a parameter to the
135 * evaluation methods of this GdomeXPathEvaluator so that a new one is not
136 * created on each call to an evaluation method.
137 */
138 GdomeXPathResult *
139 gdome_xpath_xpeval_createResult (GdomeXPathEvaluator *self, GdomeException *exc)
140 {
141 return gdome_xpath_xpresult_mkref(NULL, NULL);
142 }
143
144 /**
145 * gdome_xpath_xpeval_evaluate:
146 * @self: GdomeXPathEvaluator Object ref
147 * @expression: The XPath expression string to be parsed and evaluated.
148 * @contextNode: Context node for the evaluation of this XPath expression.
149 * @resolver: The resolver permits translation of prefixes within the XPath
150 * expression into appropriate namespace URIs.
151 * @type: If specified, result will be coerced to return the specified type.
152 * @result: Specifies a GdomeXPathResult object to be re-used.
153 * @exc: Exception Object ref
154 *
155 * Evaluates an XPath expression string
156 * Returns: a result of the specified type if possible.
157 */
158 GdomeXPathResult *
159 gdome_xpath_xpeval_evaluate (GdomeXPathEvaluator *self, GdomeDOMString *expression, GdomeNode *contextNode, GdomeXPathNSResolver *resolver, unsigned int type, GdomeXPathResult *result, GdomeException *exc)
160 {
161 xmlXPathObjectPtr res = NULL;
162 xmlXPathContextPtr ctxt;
163 xmlNodePtr refNode;
164 xmlChar *path;
165 Gdome_xml_Node *cn = (Gdome_xml_Node *)contextNode;
166 Gdome_xpath_XPathNSResolver *resolv = (Gdome_xpath_XPathNSResolver *)resolver;
167
168 g_return_val_if_fail (expression != NULL, NULL);
169 refNode = cn->n;
170
171 ctxt = xmlXPathNewContext( refNode->doc );
172 ctxt->node = refNode;
173
174 if (resolv != NULL) {
175 ctxt->namespaces = xmlGetNsList(resolv->n->doc, resolv->n);
176 ctxt->nsNr = 0;
177 if (ctxt->namespaces != NULL) {
178 while (ctxt->namespaces[ctxt->nsNr] != NULL)
179 ctxt->nsNr++;
180 }
181 }
182
183 path = expression->str;
184 res = xmlXPathEval( path, ctxt );
185
186 /* XML::LibXML should also have this patch... */
187 if (ctxt->namespaces != NULL)
188 xmlFree(ctxt->namespaces);
189
190 xmlXPathFreeContext(ctxt);
191
192 return gdome_xpath_xpresult_mkref(contextNode, res);
193 }