Coverage Report

Created: 2024-01-20 12:31

/src/libxslt/libxslt/extra.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * extra.c: Implementation of non-standard features
3
 *
4
 * Reference:
5
 *   Michael Kay "XSLT Programmer's Reference" pp 637-643
6
 *   The node-set() extension function
7
 *
8
 * See Copyright for the status of this software.
9
 *
10
 * daniel@veillard.com
11
 */
12
13
#define IN_LIBXSLT
14
#include "libxslt.h"
15
16
#include <string.h>
17
#include <stdlib.h>
18
19
#include <libxml/xmlmemory.h>
20
#include <libxml/tree.h>
21
#include <libxml/hash.h>
22
#include <libxml/xmlerror.h>
23
#include <libxml/parserInternals.h>
24
#include "xslt.h"
25
#include "xsltInternals.h"
26
#include "xsltutils.h"
27
#include "extensions.h"
28
#include "variables.h"
29
#include "transform.h"
30
#include "extra.h"
31
#include "preproc.h"
32
33
#ifdef WITH_XSLT_DEBUG
34
#define WITH_XSLT_DEBUG_EXTRA
35
#endif
36
37
/************************************************************************
38
 *                  *
39
 *    Handling of XSLT debugging        *
40
 *                  *
41
 ************************************************************************/
42
43
/**
44
 * xsltDebug:
45
 * @ctxt:  an XSLT processing context
46
 * @node:  The current node
47
 * @inst:  the instruction in the stylesheet
48
 * @comp:  precomputed information
49
 *
50
 * Process an debug node
51
 */
52
void
53
xsltDebug(xsltTransformContextPtr ctxt, xmlNodePtr node ATTRIBUTE_UNUSED,
54
          xmlNodePtr inst ATTRIBUTE_UNUSED,
55
          xsltElemPreCompPtr comp ATTRIBUTE_UNUSED)
56
0
{
57
0
    int i, j;
58
59
0
    xsltGenericError(xsltGenericErrorContext, "Templates:\n");
60
0
    for (i = 0, j = ctxt->templNr - 1; ((i < 15) && (j >= 0)); i++, j--) {
61
0
        xsltGenericError(xsltGenericErrorContext, "#%d ", i);
62
0
        if (ctxt->templTab[j]->name != NULL)
63
0
            xsltGenericError(xsltGenericErrorContext, "name %s ",
64
0
                             ctxt->templTab[j]->name);
65
0
        if (ctxt->templTab[j]->match != NULL)
66
0
            xsltGenericError(xsltGenericErrorContext, "name %s ",
67
0
                             ctxt->templTab[j]->match);
68
0
        if (ctxt->templTab[j]->mode != NULL)
69
0
            xsltGenericError(xsltGenericErrorContext, "name %s ",
70
0
                             ctxt->templTab[j]->mode);
71
0
        xsltGenericError(xsltGenericErrorContext, "\n");
72
0
    }
73
0
    xsltGenericError(xsltGenericErrorContext, "Variables:\n");
74
0
    for (i = 0, j = ctxt->varsNr - 1; ((i < 15) && (j >= 0)); i++, j--) {
75
0
        xsltStackElemPtr cur;
76
77
0
        if (ctxt->varsTab[j] == NULL)
78
0
            continue;
79
0
        xsltGenericError(xsltGenericErrorContext, "#%d\n", i);
80
0
        cur = ctxt->varsTab[j];
81
0
        while (cur != NULL) {
82
0
            if (cur->comp == NULL) {
83
0
                xsltGenericError(xsltGenericErrorContext,
84
0
                                 "corrupted !!!\n");
85
0
            } else if (cur->comp->type == XSLT_FUNC_PARAM) {
86
0
                xsltGenericError(xsltGenericErrorContext, "param ");
87
0
            } else if (cur->comp->type == XSLT_FUNC_VARIABLE) {
88
0
                xsltGenericError(xsltGenericErrorContext, "var ");
89
0
            }
90
0
            if (cur->name != NULL)
91
0
                xsltGenericError(xsltGenericErrorContext, "%s ",
92
0
                                 cur->name);
93
0
            else
94
0
                xsltGenericError(xsltGenericErrorContext, "noname !!!!");
95
0
#ifdef LIBXML_DEBUG_ENABLED
96
0
            if (cur->value != NULL) {
97
0
                if ((xsltGenericDebugContext == stdout) ||
98
0
                    (xsltGenericDebugContext == stderr))
99
0
                    xmlXPathDebugDumpObject((FILE*)xsltGenericDebugContext,
100
0
                                            cur->value, 1);
101
0
            } else {
102
0
                xsltGenericError(xsltGenericErrorContext, "NULL !!!!");
103
0
            }
104
0
#endif
105
0
            xsltGenericError(xsltGenericErrorContext, "\n");
106
0
            cur = cur->next;
107
0
        }
108
109
0
    }
110
0
}
111
112
/************************************************************************
113
 *                  *
114
 *    Classic extensions as described by M. Kay   *
115
 *                  *
116
 ************************************************************************/
117
118
/**
119
 * xsltFunctionNodeSet:
120
 * @ctxt:  the XPath Parser context
121
 * @nargs:  the number of arguments
122
 *
123
 * Implement the node-set() XSLT function
124
 *   node-set node-set(result-tree)
125
 *
126
 * This function is available in libxslt, saxon or xt namespace.
127
 */
128
void
129
13
xsltFunctionNodeSet(xmlXPathParserContextPtr ctxt, int nargs){
130
13
    if (nargs != 1) {
131
0
  xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
132
0
    "node-set() : expects one result-tree arg\n");
133
0
  ctxt->error = XPATH_INVALID_ARITY;
134
0
  return;
135
0
    }
136
13
    if ((ctxt->value == NULL) ||
137
13
  ((ctxt->value->type != XPATH_XSLT_TREE) &&
138
13
   (ctxt->value->type != XPATH_NODESET))) {
139
0
  xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
140
0
      "node-set() invalid arg expecting a result tree\n");
141
0
  ctxt->error = XPATH_INVALID_TYPE;
142
0
  return;
143
0
    }
144
13
    if (ctxt->value->type == XPATH_XSLT_TREE) {
145
0
  ctxt->value->type = XPATH_NODESET;
146
0
    }
147
13
}
148
149
/**
150
 * xsltRegisterExtras:
151
 * @ctxt:  a XSLT process context
152
 *
153
 * Registers the built-in extensions. This function is deprecated, use
154
 * xsltRegisterAllExtras instead.
155
 */
156
void
157
0
xsltRegisterExtras(xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED) {
158
0
    xsltRegisterAllExtras();
159
0
}
160
161
/**
162
 * xsltRegisterAllExtras:
163
 *
164
 * Registers the built-in extensions
165
 */
166
void
167
2.97k
xsltRegisterAllExtras (void) {
168
2.97k
    xsltRegisterExtModuleFunction((const xmlChar *) "node-set",
169
2.97k
          XSLT_LIBXSLT_NAMESPACE,
170
2.97k
          xsltFunctionNodeSet);
171
2.97k
    xsltRegisterExtModuleFunction((const xmlChar *) "node-set",
172
2.97k
          XSLT_SAXON_NAMESPACE,
173
2.97k
          xsltFunctionNodeSet);
174
2.97k
    xsltRegisterExtModuleFunction((const xmlChar *) "node-set",
175
2.97k
          XSLT_XT_NAMESPACE,
176
2.97k
          xsltFunctionNodeSet);
177
2.97k
    xsltRegisterExtModuleElement((const xmlChar *) "debug",
178
2.97k
         XSLT_LIBXSLT_NAMESPACE,
179
2.97k
         NULL,
180
2.97k
         xsltDebug);
181
2.97k
    xsltRegisterExtModuleElement((const xmlChar *) "output",
182
2.97k
         XSLT_SAXON_NAMESPACE,
183
2.97k
         xsltDocumentComp,
184
2.97k
         xsltDocumentElem);
185
2.97k
    xsltRegisterExtModuleElement((const xmlChar *) "write",
186
2.97k
         XSLT_XALAN_NAMESPACE,
187
2.97k
         xsltDocumentComp,
188
2.97k
         xsltDocumentElem);
189
2.97k
    xsltRegisterExtModuleElement((const xmlChar *) "document",
190
2.97k
         XSLT_XT_NAMESPACE,
191
2.97k
         xsltDocumentComp,
192
2.97k
         xsltDocumentElem);
193
2.97k
    xsltRegisterExtModuleElement((const xmlChar *) "document",
194
2.97k
         XSLT_NAMESPACE,
195
2.97k
         xsltDocumentComp,
196
2.97k
         xsltDocumentElem);
197
2.97k
}