/src/libxslt/libexslt/functions.c
Line | Count | Source (jump to first uncovered line) |
1 | | #define IN_LIBEXSLT |
2 | | #include "libexslt/libexslt.h" |
3 | | |
4 | | #include <string.h> |
5 | | |
6 | | #include <libxml/tree.h> |
7 | | #include <libxml/xpath.h> |
8 | | #include <libxml/xpathInternals.h> |
9 | | #include <libxml/hash.h> |
10 | | #include <libxml/debugXML.h> |
11 | | |
12 | | #include <libxslt/xsltutils.h> |
13 | | #include <libxslt/variables.h> |
14 | | #include <libxslt/xsltInternals.h> |
15 | | #include <libxslt/extensions.h> |
16 | | #include <libxslt/transform.h> |
17 | | #include <libxslt/imports.h> |
18 | | |
19 | | #include "exslt.h" |
20 | | |
21 | | typedef struct _exsltFuncFunctionData exsltFuncFunctionData; |
22 | | struct _exsltFuncFunctionData { |
23 | | int nargs; /* number of arguments to the function */ |
24 | | xmlNodePtr content; /* the func:fuction template content */ |
25 | | }; |
26 | | |
27 | | typedef struct _exsltFuncData exsltFuncData; |
28 | | struct _exsltFuncData { |
29 | | xmlHashTablePtr funcs; /* pointer to the stylesheet module data */ |
30 | | xmlXPathObjectPtr result; /* returned by func:result */ |
31 | | xsltStackElemPtr ctxtVar; /* context variable */ |
32 | | int error; /* did an error occur? */ |
33 | | }; |
34 | | |
35 | | typedef struct _exsltFuncResultPreComp exsltFuncResultPreComp; |
36 | | struct _exsltFuncResultPreComp { |
37 | | xsltElemPreComp comp; |
38 | | xmlXPathCompExprPtr select; |
39 | | xmlNsPtr *nsList; |
40 | | int nsNr; |
41 | | }; |
42 | | |
43 | | /* Used for callback function in exsltInitFunc */ |
44 | | typedef struct _exsltFuncImportRegData exsltFuncImportRegData; |
45 | | struct _exsltFuncImportRegData { |
46 | | xsltTransformContextPtr ctxt; |
47 | | xmlHashTablePtr hash; |
48 | | }; |
49 | | |
50 | | static void exsltFuncFunctionFunction (xmlXPathParserContextPtr ctxt, |
51 | | int nargs); |
52 | | static exsltFuncFunctionData *exsltFuncNewFunctionData(void); |
53 | | |
54 | | /*static const xmlChar *exsltResultDataID = (const xmlChar *) "EXSLT Result";*/ |
55 | | |
56 | | /** |
57 | | * exsltFuncRegisterFunc: |
58 | | * @func: the #exsltFuncFunctionData for the function |
59 | | * @ctxt: an XSLT transformation context |
60 | | * @URI: the function namespace URI |
61 | | * @name: the function name |
62 | | * |
63 | | * Registers a function declared by a func:function element |
64 | | */ |
65 | | static void |
66 | | exsltFuncRegisterFunc (void *payload, void *vctxt, |
67 | | const xmlChar *URI, const xmlChar *name, |
68 | 0 | ATTRIBUTE_UNUSED const xmlChar *ignored) { |
69 | 0 | exsltFuncFunctionData *data = (exsltFuncFunctionData *) payload; |
70 | 0 | xsltTransformContextPtr ctxt = (xsltTransformContextPtr) vctxt; |
71 | |
|
72 | 0 | if ((data == NULL) || (ctxt == NULL) || (URI == NULL) || (name == NULL)) |
73 | 0 | return; |
74 | | |
75 | 0 | xsltGenericDebug(xsltGenericDebugContext, |
76 | 0 | "exsltFuncRegisterFunc: register {%s}%s\n", |
77 | 0 | URI, name); |
78 | 0 | xsltRegisterExtFunction(ctxt, name, URI, |
79 | 0 | exsltFuncFunctionFunction); |
80 | 0 | } |
81 | | |
82 | | /* |
83 | | * exsltFuncRegisterImportFunc |
84 | | * @data: the exsltFuncFunctionData for the function |
85 | | * @ch: structure containing context and hash table |
86 | | * @URI: the function namespace URI |
87 | | * @name: the function name |
88 | | * |
89 | | * Checks if imported function is already registered in top-level |
90 | | * stylesheet. If not, copies function data and registers function |
91 | | */ |
92 | | static void |
93 | | exsltFuncRegisterImportFunc (void *payload, void *vctxt, |
94 | | const xmlChar *URI, const xmlChar *name, |
95 | 0 | ATTRIBUTE_UNUSED const xmlChar *ignored) { |
96 | 0 | exsltFuncFunctionData *data = (exsltFuncFunctionData *) payload; |
97 | 0 | exsltFuncImportRegData *ch = (exsltFuncImportRegData *) vctxt; |
98 | 0 | exsltFuncFunctionData *func=NULL; |
99 | |
|
100 | 0 | if ((data == NULL) || (ch == NULL) || (URI == NULL) || (name == NULL)) |
101 | 0 | return; |
102 | | |
103 | 0 | if (ch->ctxt == NULL || ch->hash == NULL) |
104 | 0 | return; |
105 | | |
106 | | /* Check if already present */ |
107 | 0 | func = (exsltFuncFunctionData*)xmlHashLookup2(ch->hash, URI, name); |
108 | 0 | if (func == NULL) { /* Not yet present - copy it in */ |
109 | 0 | func = exsltFuncNewFunctionData(); |
110 | 0 | if (func == NULL) |
111 | 0 | return; |
112 | 0 | memcpy(func, data, sizeof(exsltFuncFunctionData)); |
113 | 0 | if (xmlHashAddEntry2(ch->hash, URI, name, func) < 0) { |
114 | 0 | xsltGenericError(xsltGenericErrorContext, |
115 | 0 | "Failed to register function {%s}%s\n", |
116 | 0 | URI, name); |
117 | 0 | } else { /* Do the registration */ |
118 | 0 | xsltGenericDebug(xsltGenericDebugContext, |
119 | 0 | "exsltFuncRegisterImportFunc: register {%s}%s\n", |
120 | 0 | URI, name); |
121 | 0 | xsltRegisterExtFunction(ch->ctxt, name, URI, |
122 | 0 | exsltFuncFunctionFunction); |
123 | 0 | } |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | /** |
128 | | * exsltFuncInit: |
129 | | * @ctxt: an XSLT transformation context |
130 | | * @URI: the namespace URI for the extension |
131 | | * |
132 | | * Initializes the EXSLT - Functions module. |
133 | | * Called at transformation-time; merges all |
134 | | * functions declared in the import tree taking |
135 | | * import precedence into account, i.e. overriding |
136 | | * functions with lower import precedence. |
137 | | * |
138 | | * Returns the data for this transformation |
139 | | */ |
140 | | static void * |
141 | 0 | exsltFuncInit (xsltTransformContextPtr ctxt, const xmlChar *URI) { |
142 | 0 | exsltFuncData *ret; |
143 | 0 | xsltStylesheetPtr tmp; |
144 | 0 | exsltFuncImportRegData ch; |
145 | 0 | xmlHashTablePtr hash; |
146 | |
|
147 | 0 | ret = (exsltFuncData *) xmlMalloc (sizeof(exsltFuncData)); |
148 | 0 | if (ret == NULL) { |
149 | 0 | xsltGenericError(xsltGenericErrorContext, |
150 | 0 | "exsltFuncInit: not enough memory\n"); |
151 | 0 | return(NULL); |
152 | 0 | } |
153 | 0 | memset(ret, 0, sizeof(exsltFuncData)); |
154 | |
|
155 | 0 | ret->result = NULL; |
156 | 0 | ret->error = 0; |
157 | |
|
158 | 0 | ch.hash = (xmlHashTablePtr) xsltStyleGetExtData(ctxt->style, URI); |
159 | 0 | ret->funcs = ch.hash; |
160 | 0 | xmlHashScanFull(ch.hash, exsltFuncRegisterFunc, ctxt); |
161 | 0 | tmp = ctxt->style; |
162 | 0 | ch.ctxt = ctxt; |
163 | 0 | while ((tmp=xsltNextImport(tmp))!=NULL) { |
164 | 0 | hash = xsltGetExtInfo(tmp, URI); |
165 | 0 | if (hash != NULL) { |
166 | 0 | xmlHashScanFull(hash, exsltFuncRegisterImportFunc, &ch); |
167 | 0 | } |
168 | 0 | } |
169 | |
|
170 | 0 | return(ret); |
171 | 0 | } |
172 | | |
173 | | /** |
174 | | * exsltFuncShutdown: |
175 | | * @ctxt: an XSLT transformation context |
176 | | * @URI: the namespace URI for the extension |
177 | | * @data: the module data to free up |
178 | | * |
179 | | * Shutdown the EXSLT - Functions module |
180 | | * Called at transformation-time. |
181 | | */ |
182 | | static void |
183 | | exsltFuncShutdown (xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED, |
184 | | const xmlChar *URI ATTRIBUTE_UNUSED, |
185 | 0 | void *vdata) { |
186 | 0 | exsltFuncData *data = (exsltFuncData *) vdata; |
187 | |
|
188 | 0 | if (data->result != NULL) |
189 | 0 | xmlXPathFreeObject(data->result); |
190 | 0 | xmlFree(data); |
191 | 0 | } |
192 | | |
193 | | /** |
194 | | * exsltFuncStyleInit: |
195 | | * @style: an XSLT stylesheet |
196 | | * @URI: the namespace URI for the extension |
197 | | * |
198 | | * Allocates the stylesheet data for EXSLT - Function |
199 | | * Called at compile-time. |
200 | | * |
201 | | * Returns the allocated data |
202 | | */ |
203 | | static void * |
204 | | exsltFuncStyleInit (xsltStylesheetPtr style ATTRIBUTE_UNUSED, |
205 | 0 | const xmlChar *URI ATTRIBUTE_UNUSED) { |
206 | 0 | return xmlHashCreate(1); |
207 | 0 | } |
208 | | |
209 | | static void |
210 | 0 | exsltFuncFreeDataEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) { |
211 | 0 | xmlFree(payload); |
212 | 0 | } |
213 | | |
214 | | /** |
215 | | * exsltFuncStyleShutdown: |
216 | | * @style: an XSLT stylesheet |
217 | | * @URI: the namespace URI for the extension |
218 | | * @data: the stylesheet data to free up |
219 | | * |
220 | | * Shutdown the EXSLT - Function module |
221 | | * Called at compile-time. |
222 | | */ |
223 | | static void |
224 | | exsltFuncStyleShutdown (xsltStylesheetPtr style ATTRIBUTE_UNUSED, |
225 | | const xmlChar *URI ATTRIBUTE_UNUSED, |
226 | 0 | void *vdata) { |
227 | 0 | xmlHashTablePtr data = (xmlHashTablePtr) vdata; |
228 | 0 | xmlHashFree(data, exsltFuncFreeDataEntry); |
229 | 0 | } |
230 | | |
231 | | /** |
232 | | * exsltFuncNewFunctionData: |
233 | | * |
234 | | * Allocates an #exslFuncFunctionData object |
235 | | * |
236 | | * Returns the new structure |
237 | | */ |
238 | | static exsltFuncFunctionData * |
239 | 0 | exsltFuncNewFunctionData (void) { |
240 | 0 | exsltFuncFunctionData *ret; |
241 | |
|
242 | 0 | ret = (exsltFuncFunctionData *) xmlMalloc (sizeof(exsltFuncFunctionData)); |
243 | 0 | if (ret == NULL) { |
244 | 0 | xsltGenericError(xsltGenericErrorContext, |
245 | 0 | "exsltFuncNewFunctionData: not enough memory\n"); |
246 | 0 | return (NULL); |
247 | 0 | } |
248 | 0 | memset(ret, 0, sizeof(exsltFuncFunctionData)); |
249 | |
|
250 | 0 | ret->nargs = 0; |
251 | 0 | ret->content = NULL; |
252 | |
|
253 | 0 | return(ret); |
254 | 0 | } |
255 | | |
256 | | /** |
257 | | * exsltFreeFuncResultPreComp: |
258 | | * @comp: the #exsltFuncResultPreComp to free up |
259 | | * |
260 | | * Deallocates an #exsltFuncResultPreComp |
261 | | */ |
262 | | static void |
263 | 0 | exsltFreeFuncResultPreComp (xsltElemPreCompPtr ecomp) { |
264 | 0 | exsltFuncResultPreComp *comp = (exsltFuncResultPreComp *) ecomp; |
265 | |
|
266 | 0 | if (comp == NULL) |
267 | 0 | return; |
268 | | |
269 | 0 | if (comp->select != NULL) |
270 | 0 | xmlXPathFreeCompExpr (comp->select); |
271 | 0 | if (comp->nsList != NULL) |
272 | 0 | xmlFree(comp->nsList); |
273 | 0 | xmlFree(comp); |
274 | 0 | } |
275 | | |
276 | | /** |
277 | | * exsltFuncFunctionFunction: |
278 | | * @ctxt: an XPath parser context |
279 | | * @nargs: the number of arguments |
280 | | * |
281 | | * Evaluates the func:function element that defines the called function. |
282 | | */ |
283 | | static void |
284 | 0 | exsltFuncFunctionFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
285 | 0 | xmlXPathObjectPtr oldResult, ret; |
286 | 0 | exsltFuncData *data; |
287 | 0 | exsltFuncFunctionData *func; |
288 | 0 | xmlNodePtr paramNode, oldInsert, oldXPNode, fake; |
289 | 0 | int oldBase; |
290 | 0 | void *oldCtxtVar; |
291 | 0 | xsltStackElemPtr params = NULL, param; |
292 | 0 | xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt); |
293 | 0 | int i, notSet; |
294 | 0 | struct objChain { |
295 | 0 | struct objChain *next; |
296 | 0 | xmlXPathObjectPtr obj; |
297 | 0 | }; |
298 | 0 | struct objChain *savedObjChain = NULL, *savedObj; |
299 | | |
300 | | /* |
301 | | * retrieve func:function template |
302 | | */ |
303 | 0 | data = (exsltFuncData *) xsltGetExtData (tctxt, |
304 | 0 | EXSLT_FUNCTIONS_NAMESPACE); |
305 | 0 | oldResult = data->result; |
306 | 0 | data->result = NULL; |
307 | |
|
308 | 0 | func = (exsltFuncFunctionData*) xmlHashLookup2 (data->funcs, |
309 | 0 | ctxt->context->functionURI, |
310 | 0 | ctxt->context->function); |
311 | 0 | if (func == NULL) { |
312 | | /* Should never happen */ |
313 | 0 | xsltGenericError(xsltGenericErrorContext, |
314 | 0 | "{%s}%s: not found\n", |
315 | 0 | ctxt->context->functionURI, ctxt->context->function); |
316 | 0 | ctxt->error = XPATH_UNKNOWN_FUNC_ERROR; |
317 | 0 | return; |
318 | 0 | } |
319 | | |
320 | | /* |
321 | | * params handling |
322 | | */ |
323 | 0 | if (nargs > func->nargs) { |
324 | 0 | xsltGenericError(xsltGenericErrorContext, |
325 | 0 | "{%s}%s: called with too many arguments\n", |
326 | 0 | ctxt->context->functionURI, ctxt->context->function); |
327 | 0 | ctxt->error = XPATH_INVALID_ARITY; |
328 | 0 | return; |
329 | 0 | } |
330 | 0 | if (func->content != NULL) { |
331 | 0 | paramNode = func->content->prev; |
332 | 0 | } |
333 | 0 | else |
334 | 0 | paramNode = NULL; |
335 | 0 | if ((paramNode == NULL) && (func->nargs != 0)) { |
336 | 0 | xsltGenericError(xsltGenericErrorContext, |
337 | 0 | "exsltFuncFunctionFunction: nargs != 0 and " |
338 | 0 | "param == NULL\n"); |
339 | 0 | return; |
340 | 0 | } |
341 | | |
342 | | /* |
343 | | * When a function is called recursively during evaluation of its |
344 | | * arguments, the recursion check in xsltApplySequenceConstructor |
345 | | * isn't reached. |
346 | | */ |
347 | 0 | if (tctxt->depth >= tctxt->maxTemplateDepth) { |
348 | 0 | xsltTransformError(tctxt, NULL, NULL, |
349 | 0 | "exsltFuncFunctionFunction: Potentially infinite recursion " |
350 | 0 | "detected in function {%s}%s.\n", |
351 | 0 | ctxt->context->functionURI, ctxt->context->function); |
352 | 0 | tctxt->state = XSLT_STATE_STOPPED; |
353 | 0 | return; |
354 | 0 | } |
355 | 0 | tctxt->depth++; |
356 | | |
357 | | /* Evaluating templates can change the XPath context node. */ |
358 | 0 | oldXPNode = tctxt->xpathCtxt->node; |
359 | | |
360 | | /* |
361 | | * We have a problem with the evaluation of function parameters. |
362 | | * The original library code did not evaluate XPath expressions until |
363 | | * the last moment. After version 1.1.17 of the libxslt, the logic |
364 | | * of other parts of the library was changed, and the evaluation of |
365 | | * XPath expressions within parameters now takes place as soon as the |
366 | | * parameter is parsed/evaluated (xsltParseStylesheetCallerParam). |
367 | | * This means that the parameters need to be evaluated in lexical |
368 | | * order (since a variable is "in scope" as soon as it is declared). |
369 | | * However, on entry to this routine, the values (from the caller) are |
370 | | * in reverse order (held on the XPath context variable stack). To |
371 | | * accomplish what is required, I have added code to pop the XPath |
372 | | * objects off of the stack at the beginning and save them, then use |
373 | | * them (in the reverse order) as the params are evaluated. This |
374 | | * requires an xmlMalloc/xmlFree for each param set by the caller, |
375 | | * which is not very nice. There is probably a much better solution |
376 | | * (like change other code to delay the evaluation). |
377 | | */ |
378 | | /* |
379 | | * In order to give the function params and variables a new 'scope' |
380 | | * we change varsBase in the context. |
381 | | */ |
382 | 0 | oldBase = tctxt->varsBase; |
383 | 0 | tctxt->varsBase = tctxt->varsNr; |
384 | | /* If there are any parameters */ |
385 | 0 | if (paramNode != NULL) { |
386 | | /* Fetch the stored argument values from the caller */ |
387 | 0 | for (i = 0; i < nargs; i++) { |
388 | 0 | savedObj = xmlMalloc(sizeof(struct objChain)); |
389 | 0 | savedObj->next = savedObjChain; |
390 | 0 | savedObj->obj = valuePop(ctxt); |
391 | 0 | savedObjChain = savedObj; |
392 | 0 | } |
393 | | |
394 | | /* |
395 | | * Prepare to process params in reverse order. First, go to |
396 | | * the beginning of the param chain. |
397 | | */ |
398 | 0 | for (i = 1; i <= func->nargs; i++) { |
399 | 0 | if (paramNode->prev == NULL) |
400 | 0 | break; |
401 | 0 | paramNode = paramNode->prev; |
402 | 0 | } |
403 | | /* |
404 | | * i has total # params found, nargs is number which are present |
405 | | * as arguments from the caller |
406 | | * Calculate the number of un-set parameters |
407 | | */ |
408 | 0 | notSet = func->nargs - nargs; |
409 | 0 | for (; i > 0; i--) { |
410 | 0 | param = xsltParseStylesheetCallerParam (tctxt, paramNode); |
411 | 0 | if (i > notSet) { /* if parameter value set */ |
412 | 0 | param->computed = 1; |
413 | 0 | if (param->value != NULL) |
414 | 0 | xmlXPathFreeObject(param->value); |
415 | 0 | savedObj = savedObjChain; /* get next val from chain */ |
416 | 0 | param->value = savedObj->obj; |
417 | 0 | savedObjChain = savedObjChain->next; |
418 | 0 | xmlFree(savedObj); |
419 | 0 | } |
420 | 0 | xsltLocalVariablePush(tctxt, param, -1); |
421 | 0 | param->next = params; |
422 | 0 | params = param; |
423 | 0 | paramNode = paramNode->next; |
424 | 0 | } |
425 | 0 | } |
426 | | /* |
427 | | * Actual processing. The context variable is cleared and restored |
428 | | * when func:result is evaluated. |
429 | | */ |
430 | 0 | fake = xmlNewDocNode(tctxt->output, NULL, |
431 | 0 | (const xmlChar *)"fake", NULL); |
432 | 0 | oldInsert = tctxt->insert; |
433 | 0 | oldCtxtVar = data->ctxtVar; |
434 | 0 | data->ctxtVar = tctxt->contextVariable; |
435 | 0 | tctxt->insert = fake; |
436 | 0 | tctxt->contextVariable = NULL; |
437 | 0 | xsltApplyOneTemplate (tctxt, tctxt->node, |
438 | 0 | func->content, NULL, NULL); |
439 | 0 | xsltLocalVariablePop(tctxt, tctxt->varsBase, -2); |
440 | 0 | tctxt->insert = oldInsert; |
441 | 0 | tctxt->contextVariable = data->ctxtVar; |
442 | 0 | tctxt->varsBase = oldBase; /* restore original scope */ |
443 | 0 | data->ctxtVar = oldCtxtVar; |
444 | 0 | if (params != NULL) |
445 | 0 | xsltFreeStackElemList(params); |
446 | 0 | tctxt->xpathCtxt->node = oldXPNode; |
447 | |
|
448 | 0 | if (data->error != 0) |
449 | 0 | goto error; |
450 | | |
451 | 0 | if (data->result != NULL) { |
452 | 0 | ret = data->result; |
453 | | /* |
454 | | * IMPORTANT: This enables previously tree fragments marked as |
455 | | * being results of a function, to be garbage-collected after |
456 | | * the calling process exits. |
457 | | */ |
458 | 0 | xsltFlagRVTs(tctxt, ret, XSLT_RVT_LOCAL); |
459 | 0 | } else |
460 | 0 | ret = xmlXPathNewCString(""); |
461 | |
|
462 | 0 | data->result = oldResult; |
463 | | |
464 | | /* |
465 | | * It is an error if the instantiation of the template results in |
466 | | * the generation of result nodes. |
467 | | */ |
468 | 0 | if (fake->children != NULL) { |
469 | 0 | xsltGenericError(xsltGenericErrorContext, |
470 | 0 | "{%s}%s: cannot write to result tree while " |
471 | 0 | "executing a function\n", |
472 | 0 | ctxt->context->functionURI, ctxt->context->function); |
473 | 0 | xmlFreeNode(fake); |
474 | 0 | xmlXPathFreeObject(ret); |
475 | 0 | goto error; |
476 | 0 | } |
477 | 0 | xmlFreeNode(fake); |
478 | 0 | valuePush(ctxt, ret); |
479 | |
|
480 | 0 | error: |
481 | 0 | tctxt->depth--; |
482 | 0 | } |
483 | | |
484 | | |
485 | | static void |
486 | 0 | exsltFuncFunctionComp (xsltStylesheetPtr style, xmlNodePtr inst) { |
487 | 0 | xmlChar *name, *prefix; |
488 | 0 | xmlNsPtr ns; |
489 | 0 | xmlHashTablePtr data; |
490 | 0 | exsltFuncFunctionData *func; |
491 | |
|
492 | 0 | if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE)) |
493 | 0 | return; |
494 | | |
495 | 0 | { |
496 | 0 | xmlChar *qname; |
497 | |
|
498 | 0 | qname = xmlGetProp(inst, (const xmlChar *) "name"); |
499 | 0 | name = xmlSplitQName2 (qname, &prefix); |
500 | 0 | xmlFree(qname); |
501 | 0 | } |
502 | 0 | if ((name == NULL) || (prefix == NULL)) { |
503 | 0 | xsltGenericError(xsltGenericErrorContext, |
504 | 0 | "func:function: not a QName\n"); |
505 | 0 | if (name != NULL) |
506 | 0 | xmlFree(name); |
507 | 0 | return; |
508 | 0 | } |
509 | | /* namespace lookup */ |
510 | 0 | ns = xmlSearchNs (inst->doc, inst, prefix); |
511 | 0 | if (ns == NULL) { |
512 | 0 | xsltGenericError(xsltGenericErrorContext, |
513 | 0 | "func:function: undeclared prefix %s\n", |
514 | 0 | prefix); |
515 | 0 | xmlFree(name); |
516 | 0 | xmlFree(prefix); |
517 | 0 | return; |
518 | 0 | } |
519 | 0 | xmlFree(prefix); |
520 | |
|
521 | 0 | xsltParseTemplateContent(style, inst); |
522 | | |
523 | | /* |
524 | | * Create function data |
525 | | */ |
526 | 0 | func = exsltFuncNewFunctionData(); |
527 | 0 | if (func == NULL) { |
528 | 0 | xmlFree(name); |
529 | 0 | return; |
530 | 0 | } |
531 | 0 | func->content = inst->children; |
532 | 0 | while (IS_XSLT_ELEM(func->content) && |
533 | 0 | IS_XSLT_NAME(func->content, "param")) { |
534 | 0 | func->content = func->content->next; |
535 | 0 | func->nargs++; |
536 | 0 | } |
537 | | |
538 | | /* |
539 | | * Register the function data such that it can be retrieved |
540 | | * by exslFuncFunctionFunction |
541 | | */ |
542 | | #ifdef XSLT_REFACTORED |
543 | | /* |
544 | | * Ensure that the hash table will be stored in the *current* |
545 | | * stylesheet level in order to correctly evaluate the |
546 | | * import precedence. |
547 | | */ |
548 | | data = (xmlHashTablePtr) |
549 | | xsltStyleStylesheetLevelGetExtData(style, |
550 | | EXSLT_FUNCTIONS_NAMESPACE); |
551 | | #else |
552 | 0 | data = (xmlHashTablePtr) |
553 | 0 | xsltStyleGetExtData (style, EXSLT_FUNCTIONS_NAMESPACE); |
554 | 0 | #endif |
555 | 0 | if (data == NULL) { |
556 | 0 | xsltGenericError(xsltGenericErrorContext, |
557 | 0 | "exsltFuncFunctionComp: no stylesheet data\n"); |
558 | 0 | xmlFree(name); |
559 | 0 | xmlFree(func); |
560 | 0 | return; |
561 | 0 | } |
562 | | |
563 | 0 | if (xmlHashAddEntry2 (data, ns->href, name, func) < 0) { |
564 | 0 | xsltTransformError(NULL, style, inst, |
565 | 0 | "Failed to register function {%s}%s\n", |
566 | 0 | ns->href, name); |
567 | 0 | style->errors++; |
568 | 0 | xmlFree(func); |
569 | 0 | } else { |
570 | 0 | xsltGenericDebug(xsltGenericDebugContext, |
571 | 0 | "exsltFuncFunctionComp: register {%s}%s\n", |
572 | 0 | ns->href, name); |
573 | 0 | } |
574 | 0 | xmlFree(name); |
575 | 0 | } |
576 | | |
577 | | static xsltElemPreCompPtr |
578 | | exsltFuncResultComp (xsltStylesheetPtr style, xmlNodePtr inst, |
579 | 0 | xsltTransformFunction function) { |
580 | 0 | xmlNodePtr test; |
581 | 0 | xmlChar *sel; |
582 | 0 | exsltFuncResultPreComp *ret; |
583 | |
|
584 | 0 | if ((style == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE)) |
585 | 0 | return (NULL); |
586 | | |
587 | | /* |
588 | | * "Validity" checking |
589 | | */ |
590 | | /* it is an error to have any following sibling elements aside |
591 | | * from the xsl:fallback element. |
592 | | */ |
593 | 0 | for (test = inst->next; test != NULL; test = test->next) { |
594 | 0 | if (test->type != XML_ELEMENT_NODE) |
595 | 0 | continue; |
596 | 0 | if (IS_XSLT_ELEM(test) && IS_XSLT_NAME(test, "fallback")) |
597 | 0 | continue; |
598 | 0 | xsltGenericError(xsltGenericErrorContext, |
599 | 0 | "exsltFuncResultElem: only xsl:fallback is " |
600 | 0 | "allowed to follow func:result\n"); |
601 | 0 | style->errors++; |
602 | 0 | return (NULL); |
603 | 0 | } |
604 | | /* it is an error for a func:result element to not be a descendant |
605 | | * of func:function. |
606 | | * it is an error if a func:result occurs within a func:result |
607 | | * element. |
608 | | * it is an error if instanciating the content of a variable |
609 | | * binding element (i.e. xsl:variable, xsl:param) results in the |
610 | | * instanciation of a func:result element. |
611 | | */ |
612 | 0 | for (test = inst->parent; test != NULL; test = test->parent) { |
613 | 0 | if (IS_XSLT_ELEM(test) && |
614 | 0 | IS_XSLT_NAME(test, "stylesheet")) { |
615 | 0 | xsltGenericError(xsltGenericErrorContext, |
616 | 0 | "func:result element not a descendant " |
617 | 0 | "of a func:function\n"); |
618 | 0 | style->errors++; |
619 | 0 | return (NULL); |
620 | 0 | } |
621 | 0 | if ((test->ns != NULL) && |
622 | 0 | (xmlStrEqual(test->ns->href, EXSLT_FUNCTIONS_NAMESPACE))) { |
623 | 0 | if (xmlStrEqual(test->name, (const xmlChar *) "function")) { |
624 | 0 | break; |
625 | 0 | } |
626 | 0 | if (xmlStrEqual(test->name, (const xmlChar *) "result")) { |
627 | 0 | xsltGenericError(xsltGenericErrorContext, |
628 | 0 | "func:result element not allowed within" |
629 | 0 | " another func:result element\n"); |
630 | 0 | style->errors++; |
631 | 0 | return (NULL); |
632 | 0 | } |
633 | 0 | } |
634 | 0 | if (IS_XSLT_ELEM(test) && |
635 | 0 | (IS_XSLT_NAME(test, "variable") || |
636 | 0 | IS_XSLT_NAME(test, "param"))) { |
637 | 0 | xsltGenericError(xsltGenericErrorContext, |
638 | 0 | "func:result element not allowed within" |
639 | 0 | " a variable binding element\n"); |
640 | 0 | style->errors++; |
641 | 0 | return (NULL); |
642 | 0 | } |
643 | 0 | } |
644 | | |
645 | | /* |
646 | | * Precomputation |
647 | | */ |
648 | 0 | ret = (exsltFuncResultPreComp *) |
649 | 0 | xmlMalloc (sizeof(exsltFuncResultPreComp)); |
650 | 0 | if (ret == NULL) { |
651 | 0 | xsltPrintErrorContext(NULL, NULL, NULL); |
652 | 0 | xsltGenericError(xsltGenericErrorContext, |
653 | 0 | "exsltFuncResultComp : malloc failed\n"); |
654 | 0 | style->errors++; |
655 | 0 | return (NULL); |
656 | 0 | } |
657 | 0 | memset(ret, 0, sizeof(exsltFuncResultPreComp)); |
658 | |
|
659 | 0 | xsltInitElemPreComp ((xsltElemPreCompPtr) ret, style, inst, function, |
660 | 0 | exsltFreeFuncResultPreComp); |
661 | 0 | ret->select = NULL; |
662 | | |
663 | | /* |
664 | | * Precompute the select attribute |
665 | | */ |
666 | 0 | sel = xmlGetNsProp(inst, (const xmlChar *) "select", NULL); |
667 | 0 | if (sel != NULL) { |
668 | 0 | ret->select = xsltXPathCompileFlags(style, sel, 0); |
669 | 0 | xmlFree(sel); |
670 | 0 | } |
671 | | /* |
672 | | * Precompute the namespace list |
673 | | */ |
674 | 0 | ret->nsList = xmlGetNsList(inst->doc, inst); |
675 | 0 | if (ret->nsList != NULL) { |
676 | 0 | int i = 0; |
677 | 0 | while (ret->nsList[i] != NULL) |
678 | 0 | i++; |
679 | 0 | ret->nsNr = i; |
680 | 0 | } |
681 | 0 | return ((xsltElemPreCompPtr) ret); |
682 | 0 | } |
683 | | |
684 | | static void |
685 | | exsltFuncResultElem (xsltTransformContextPtr ctxt, |
686 | | xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr inst, |
687 | 0 | xsltElemPreCompPtr ecomp) { |
688 | 0 | exsltFuncResultPreComp *comp = (exsltFuncResultPreComp *) ecomp; |
689 | 0 | exsltFuncData *data; |
690 | 0 | xmlXPathObjectPtr ret; |
691 | | |
692 | | |
693 | | /* It is an error if instantiating the content of the |
694 | | * func:function element results in the instantiation of more than |
695 | | * one func:result elements. |
696 | | */ |
697 | 0 | data = (exsltFuncData *) xsltGetExtData (ctxt, EXSLT_FUNCTIONS_NAMESPACE); |
698 | 0 | if (data == NULL) { |
699 | 0 | xsltGenericError(xsltGenericErrorContext, |
700 | 0 | "exsltFuncReturnElem: data == NULL\n"); |
701 | 0 | return; |
702 | 0 | } |
703 | 0 | if (data->result != NULL) { |
704 | 0 | xsltGenericError(xsltGenericErrorContext, |
705 | 0 | "func:result already instanciated\n"); |
706 | 0 | data->error = 1; |
707 | 0 | return; |
708 | 0 | } |
709 | | /* |
710 | | * Restore context variable, so that it will receive the function |
711 | | * result RVTs. |
712 | | */ |
713 | 0 | ctxt->contextVariable = data->ctxtVar; |
714 | | /* |
715 | | * Processing |
716 | | */ |
717 | 0 | if (comp->select != NULL) { |
718 | 0 | xmlNsPtr *oldXPNsList; |
719 | 0 | int oldXPNsNr; |
720 | 0 | xmlNodePtr oldXPContextNode; |
721 | | /* If the func:result element has a select attribute, then the |
722 | | * value of the attribute must be an expression and the |
723 | | * returned value is the object that results from evaluating |
724 | | * the expression. In this case, the content must be empty. |
725 | | */ |
726 | 0 | if (inst->children != NULL) { |
727 | 0 | xsltGenericError(xsltGenericErrorContext, |
728 | 0 | "func:result content must be empty if" |
729 | 0 | " the function has a select attribute\n"); |
730 | 0 | data->error = 1; |
731 | 0 | return; |
732 | 0 | } |
733 | 0 | oldXPNsList = ctxt->xpathCtxt->namespaces; |
734 | 0 | oldXPNsNr = ctxt->xpathCtxt->nsNr; |
735 | 0 | oldXPContextNode = ctxt->xpathCtxt->node; |
736 | |
|
737 | 0 | ctxt->xpathCtxt->namespaces = comp->nsList; |
738 | 0 | ctxt->xpathCtxt->nsNr = comp->nsNr; |
739 | 0 | ctxt->xpathCtxt->node = ctxt->node; |
740 | |
|
741 | 0 | ret = xmlXPathCompiledEval(comp->select, ctxt->xpathCtxt); |
742 | |
|
743 | 0 | ctxt->xpathCtxt->node = oldXPContextNode; |
744 | 0 | ctxt->xpathCtxt->nsNr = oldXPNsNr; |
745 | 0 | ctxt->xpathCtxt->namespaces = oldXPNsList; |
746 | |
|
747 | 0 | if (ret == NULL) { |
748 | 0 | xsltGenericError(xsltGenericErrorContext, |
749 | 0 | "exsltFuncResultElem: ret == NULL\n"); |
750 | 0 | return; |
751 | 0 | } |
752 | | /* |
753 | | * Mark it as a function result in order to avoid garbage |
754 | | * collecting of tree fragments before the function exits. |
755 | | */ |
756 | 0 | xsltFlagRVTs(ctxt, ret, XSLT_RVT_FUNC_RESULT); |
757 | 0 | } else if (inst->children != NULL) { |
758 | | /* If the func:result element does not have a select attribute |
759 | | * and has non-empty content (i.e. the func:result element has |
760 | | * one or more child nodes), then the content of the |
761 | | * func:result element specifies the value. |
762 | | */ |
763 | 0 | xmlNodePtr oldInsert; |
764 | 0 | xmlDocPtr container; |
765 | |
|
766 | 0 | container = xsltCreateRVT(ctxt); |
767 | 0 | if (container == NULL) { |
768 | 0 | xsltGenericError(xsltGenericErrorContext, |
769 | 0 | "exsltFuncResultElem: out of memory\n"); |
770 | 0 | data->error = 1; |
771 | 0 | return; |
772 | 0 | } |
773 | | /* Mark as function result. */ |
774 | 0 | xsltRegisterLocalRVT(ctxt, container); |
775 | 0 | container->compression = XSLT_RVT_FUNC_RESULT; |
776 | |
|
777 | 0 | oldInsert = ctxt->insert; |
778 | 0 | ctxt->insert = (xmlNodePtr) container; |
779 | 0 | xsltApplyOneTemplate (ctxt, ctxt->node, |
780 | 0 | inst->children, NULL, NULL); |
781 | 0 | ctxt->insert = oldInsert; |
782 | |
|
783 | 0 | ret = xmlXPathNewValueTree((xmlNodePtr) container); |
784 | 0 | if (ret == NULL) { |
785 | 0 | xsltGenericError(xsltGenericErrorContext, |
786 | 0 | "exsltFuncResultElem: ret == NULL\n"); |
787 | 0 | data->error = 1; |
788 | 0 | } else { |
789 | 0 | ret->boolval = 0; /* Freeing is not handled there anymore */ |
790 | 0 | } |
791 | 0 | } else { |
792 | | /* If the func:result element has empty content and does not |
793 | | * have a select attribute, then the returned value is an |
794 | | * empty string. |
795 | | */ |
796 | 0 | ret = xmlXPathNewCString(""); |
797 | 0 | } |
798 | 0 | data->result = ret; |
799 | 0 | } |
800 | | |
801 | | /** |
802 | | * exsltFuncRegister: |
803 | | * |
804 | | * Registers the EXSLT - Functions module |
805 | | */ |
806 | | void |
807 | 3.39k | exsltFuncRegister (void) { |
808 | 3.39k | xsltRegisterExtModuleFull (EXSLT_FUNCTIONS_NAMESPACE, |
809 | 3.39k | exsltFuncInit, |
810 | 3.39k | exsltFuncShutdown, |
811 | 3.39k | exsltFuncStyleInit, |
812 | 3.39k | exsltFuncStyleShutdown); |
813 | | |
814 | 3.39k | xsltRegisterExtModuleTopLevel ((const xmlChar *) "function", |
815 | 3.39k | EXSLT_FUNCTIONS_NAMESPACE, |
816 | 3.39k | exsltFuncFunctionComp); |
817 | 3.39k | xsltRegisterExtModuleElement ((const xmlChar *) "result", |
818 | 3.39k | EXSLT_FUNCTIONS_NAMESPACE, |
819 | 3.39k | exsltFuncResultComp, |
820 | 3.39k | exsltFuncResultElem); |
821 | 3.39k | } |