Coverage Report

Created: 2025-10-13 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxslt/libexslt/saxon.c
Line
Count
Source
1
#define IN_LIBEXSLT
2
#include "libexslt/libexslt.h"
3
4
#include <libxml/tree.h>
5
#include <libxml/xpath.h>
6
#include <libxml/xpathInternals.h>
7
#include <libxml/parser.h>
8
#include <libxml/hash.h>
9
10
#include <libxslt/xsltutils.h>
11
#include <libxslt/xsltInternals.h>
12
#include <libxslt/extensions.h>
13
14
#include "exslt.h"
15
16
/**
17
 * exsltSaxonInit:
18
 * @ctxt: an XSLT transformation context
19
 * @URI: the namespace URI for the extension
20
 *
21
 * Initializes the SAXON module.
22
 *
23
 * Returns the data for this transformation
24
 */
25
static void *
26
exsltSaxonInit (xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED,
27
0
    const xmlChar *URI ATTRIBUTE_UNUSED) {
28
0
    return xmlHashCreate(1);
29
0
}
30
31
static void
32
exsltSaxonFreeCompExprEntry(void *payload,
33
0
                            const xmlChar *name ATTRIBUTE_UNUSED) {
34
0
    xmlXPathFreeCompExpr((xmlXPathCompExprPtr) payload);
35
0
}
36
37
/**
38
 * exsltSaxonShutdown:
39
 * @ctxt: an XSLT transformation context
40
 * @URI: the namespace URI for the extension
41
 * @data: the module data to free up
42
 *
43
 * Shutdown the SAXON extension module
44
 */
45
static void
46
exsltSaxonShutdown (xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED,
47
        const xmlChar *URI ATTRIBUTE_UNUSED,
48
0
        void *vdata) {
49
0
    xmlHashTablePtr data = (xmlHashTablePtr) vdata;
50
0
    xmlHashFree(data, exsltSaxonFreeCompExprEntry);
51
0
}
52
53
54
/**
55
 * exsltSaxonExpressionFunction:
56
 * @ctxt: an XPath parser context
57
 * @nargs: the number of arguments
58
 *
59
 * The supplied string must contain an XPath expression. The result of
60
 * the function is a stored expression, which may be supplied as an
61
 * argument to other extension functions such as saxon:eval(),
62
 * saxon:sum() and saxon:distinct(). The result of the expression will
63
 * usually depend on the current node. The expression may contain
64
 * references to variables that are in scope at the point where
65
 * saxon:expression() is called: these variables will be replaced in
66
 * the stored expression with the values they take at the time
67
 * saxon:expression() is called, not the values of the variables at
68
 * the time the stored expression is evaluated.  Similarly, if the
69
 * expression contains namespace prefixes, these are interpreted in
70
 * terms of the namespace declarations in scope at the point where the
71
 * saxon:expression() function is called, not those in scope where the
72
 * stored expression is evaluated.
73
 *
74
 * TODO: current implementation doesn't conform to SAXON behaviour
75
 * regarding context and namespaces.
76
 */
77
static void
78
0
exsltSaxonExpressionFunction (xmlXPathParserContextPtr ctxt, int nargs) {
79
0
    xmlChar *arg;
80
0
    xmlXPathCompExprPtr ret;
81
0
    xmlHashTablePtr hash;
82
0
    xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt);
83
84
0
    if (nargs != 1) {
85
0
  xmlXPathSetArityError(ctxt);
86
0
  return;
87
0
    }
88
89
0
    arg = xmlXPathPopString(ctxt);
90
0
    if (xmlXPathCheckError(ctxt) || (arg == NULL)) {
91
0
  xmlXPathSetTypeError(ctxt);
92
0
  return;
93
0
    }
94
95
0
    hash = (xmlHashTablePtr) xsltGetExtData(tctxt,
96
0
              ctxt->context->functionURI);
97
98
0
    ret = xmlHashLookup(hash, arg);
99
100
0
    if (ret == NULL) {
101
0
        ret = xmlXPathCtxtCompile(tctxt->xpathCtxt, arg);
102
0
        if (ret == NULL) {
103
0
            xmlFree(arg);
104
0
            xmlXPathSetError(ctxt, XPATH_EXPR_ERROR);
105
0
            return;
106
0
        }
107
0
        if (xmlHashAddEntry(hash, arg, (void *) ret) < 0) {
108
0
            xmlXPathFreeCompExpr(ret);
109
0
            xmlFree(arg);
110
0
            xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR);
111
0
            return;
112
0
        }
113
0
    }
114
115
0
    xmlFree(arg);
116
117
0
    xmlXPathReturnExternal(ctxt, ret);
118
0
}
119
120
/**
121
 * exsltSaxonEvalFunction:
122
 * @ctxt:  an XPath parser context
123
 * @nargs:  number of arguments
124
 *
125
 * Implements de SAXON eval() function:
126
 *    object saxon:eval (saxon:stored-expression)
127
 * Returns the result of evaluating the supplied stored expression.
128
 * A stored expression may be obtained as the result of calling
129
 * the saxon:expression() function.
130
 * The stored expression is evaluated in the current context, that
131
 * is, the context node is the current node, and the context position
132
 * and context size are the same as the result of calling position()
133
 * or last() respectively.
134
 */
135
static void
136
0
exsltSaxonEvalFunction (xmlXPathParserContextPtr ctxt, int nargs) {
137
0
     xmlXPathCompExprPtr expr;
138
0
     xmlXPathObjectPtr ret;
139
140
0
     if (nargs != 1) {
141
0
    xmlXPathSetArityError(ctxt);
142
0
    return;
143
0
     }
144
145
0
     if (!xmlXPathStackIsExternal(ctxt)) {
146
0
    xmlXPathSetTypeError(ctxt);
147
0
    return;
148
0
     }
149
150
0
     expr = (xmlXPathCompExprPtr) xmlXPathPopExternal(ctxt);
151
152
0
     ret = xmlXPathCompiledEval(expr, ctxt->context);
153
0
     if (ret == NULL) {
154
0
    xmlXPathSetError(ctxt, XPATH_EXPR_ERROR);
155
0
    return;
156
0
     }
157
158
0
     valuePush(ctxt, ret);
159
0
}
160
161
/**
162
 * exsltSaxonEvaluateFunction:
163
 * @ctxt:  an XPath parser context
164
 * @nargs: number of arguments
165
 *
166
 * Implements the SAXON evaluate() function
167
 *     object saxon:evaluate (string)
168
 * The supplied string must contain an XPath expression. The result of
169
 * the function is the result of evaluating the XPath expression. This
170
 * is useful where an expression needs to be constructed at run-time or
171
 * passed to the stylesheet as a parameter, for example where the sort
172
 * key is determined dynamically. The context for the expression (e.g.
173
 * which variables and namespaces are available) is exactly the same as
174
 * if the expression were written explicitly at this point in the
175
 * stylesheet. The function saxon:evaluate(string) is shorthand for
176
 * saxon:eval(saxon:expression(string)).
177
 */
178
static void
179
0
exsltSaxonEvaluateFunction (xmlXPathParserContextPtr ctxt, int nargs) {
180
0
     if (nargs != 1) {
181
0
    xmlXPathSetArityError(ctxt);
182
0
    return;
183
0
     }
184
185
0
     exsltSaxonExpressionFunction(ctxt, 1);
186
0
     exsltSaxonEvalFunction(ctxt, 1);
187
0
}
188
189
/**
190
 * exsltSaxonSystemIdFunction:
191
 * @ctxt:  an XPath parser context
192
 * @nargs: number of arguments
193
 *
194
 * Implements the SAXON systemId() function
195
 *     string saxon:systemId ()
196
 * This function returns the system ID of the document being styled.
197
 */
198
static void
199
exsltSaxonSystemIdFunction(xmlXPathParserContextPtr ctxt, int nargs)
200
0
{
201
0
    if (ctxt == NULL)
202
0
        return;
203
0
    if (nargs != 0) {
204
0
        xmlXPathSetArityError(ctxt);
205
0
        return;
206
0
    }
207
208
0
    if ((ctxt->context) && (ctxt->context->doc) &&
209
0
        (ctxt->context->doc->URL))
210
0
  valuePush(ctxt, xmlXPathNewString(ctxt->context->doc->URL));
211
0
    else
212
0
  valuePush(ctxt, xmlXPathNewString(BAD_CAST ""));
213
0
}
214
215
/**
216
 * exsltSaxonLineNumberFunction:
217
 * @ctxt:  an XPath parser context
218
 * @nargs: number of arguments
219
 *
220
 * Implements the SAXON line-number() function
221
 *     integer saxon:line-number()
222
 *
223
 * This returns the line number of the context node in the source document
224
 * within the entity that contains it. There are no arguments. If line numbers
225
 * are not maintained for the current document, the function returns -1. (To
226
 * ensure that line numbers are maintained, use the -l option on the command
227
 * line)
228
 *
229
 * The extension has been extended to have the following form:
230
 *     integer saxon:line-number([node-set-1])
231
 * If a node-set is given, this extension will return the line number of the
232
 * node in the argument node-set that is first in document order.
233
 */
234
static void
235
30.1k
exsltSaxonLineNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) {
236
30.1k
    xmlNodePtr cur = NULL;
237
30.1k
    xmlXPathObjectPtr obj = NULL;
238
30.1k
    long lineNo = -1;
239
240
30.1k
    if (nargs == 0) {
241
30.1k
  cur = ctxt->context->node;
242
30.1k
    } else if (nargs == 1) {
243
0
  xmlNodeSetPtr nodelist;
244
0
  int i;
245
246
0
  if ((ctxt->value == NULL) || (ctxt->value->type != XPATH_NODESET)) {
247
0
      xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
248
0
    "saxon:line-number() : invalid arg expecting a node-set\n");
249
0
      ctxt->error = XPATH_INVALID_TYPE;
250
0
      return;
251
0
  }
252
253
0
  obj = valuePop(ctxt);
254
0
  nodelist = obj->nodesetval;
255
0
  if ((nodelist != NULL) && (nodelist->nodeNr > 0)) {
256
0
            cur = nodelist->nodeTab[0];
257
0
            for (i = 1;i < nodelist->nodeNr;i++) {
258
0
                int ret = xmlXPathCmpNodes(cur, nodelist->nodeTab[i]);
259
0
                if (ret == -1)
260
0
                    cur = nodelist->nodeTab[i];
261
0
            }
262
0
        }
263
0
    } else {
264
0
  xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
265
0
    "saxon:line-number() : invalid number of args %d\n",
266
0
    nargs);
267
0
  ctxt->error = XPATH_INVALID_ARITY;
268
0
  return;
269
0
    }
270
271
30.1k
    if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) {
272
        /*
273
        * The XPath module sets the owner element of a ns-node on
274
        * the ns->next field.
275
        */
276
0
        cur = (xmlNodePtr) ((xmlNsPtr) cur)->next;
277
0
        if (cur == NULL || cur->type != XML_ELEMENT_NODE) {
278
0
            xsltGenericError(xsltGenericErrorContext,
279
0
                "Internal error in exsltSaxonLineNumberFunction: "
280
0
                "Cannot retrieve the doc of a namespace node.\n");
281
0
            cur = NULL;
282
0
        }
283
0
    }
284
285
30.1k
    if (cur != NULL)
286
30.1k
        lineNo = xmlGetLineNo(cur);
287
288
30.1k
    valuePush(ctxt, xmlXPathNewFloat(lineNo));
289
290
30.1k
    xmlXPathFreeObject(obj);
291
30.1k
}
292
293
/**
294
 * exsltSaxonRegister:
295
 *
296
 * Registers the SAXON extension module
297
 */
298
void
299
2
exsltSaxonRegister (void) {
300
2
     xsltRegisterExtModule (SAXON_NAMESPACE,
301
2
          exsltSaxonInit,
302
2
          exsltSaxonShutdown);
303
2
     xsltRegisterExtModuleFunction((const xmlChar *) "expression",
304
2
           SAXON_NAMESPACE,
305
2
           exsltSaxonExpressionFunction);
306
2
     xsltRegisterExtModuleFunction((const xmlChar *) "eval",
307
2
           SAXON_NAMESPACE,
308
2
           exsltSaxonEvalFunction);
309
2
     xsltRegisterExtModuleFunction((const xmlChar *) "evaluate",
310
2
           SAXON_NAMESPACE,
311
2
           exsltSaxonEvaluateFunction);
312
2
    xsltRegisterExtModuleFunction ((const xmlChar *) "line-number",
313
2
           SAXON_NAMESPACE,
314
2
           exsltSaxonLineNumberFunction);
315
2
    xsltRegisterExtModuleFunction ((const xmlChar *) "systemId",
316
2
           SAXON_NAMESPACE,
317
2
           exsltSaxonSystemIdFunction);
318
2
}