/src/libxslt/libxslt/variables.c
Line | Count | Source |
1 | | /* |
2 | | * variables.c: Implementation of the variable storage and lookup |
3 | | * |
4 | | * Reference: |
5 | | * http://www.w3.org/TR/1999/REC-xslt-19991116 |
6 | | * |
7 | | * See Copyright for the status of this software. |
8 | | * |
9 | | * daniel@veillard.com |
10 | | */ |
11 | | |
12 | | #define IN_LIBXSLT |
13 | | #include "libxslt.h" |
14 | | |
15 | | #include <string.h> |
16 | | |
17 | | #include <libxml/xmlmemory.h> |
18 | | #include <libxml/tree.h> |
19 | | #include <libxml/valid.h> |
20 | | #include <libxml/hash.h> |
21 | | #include <libxml/xmlerror.h> |
22 | | #include <libxml/xpath.h> |
23 | | #include <libxml/xpathInternals.h> |
24 | | #include <libxml/parserInternals.h> |
25 | | #include <libxml/dict.h> |
26 | | #include "xslt.h" |
27 | | #include "xsltInternals.h" |
28 | | #include "xsltutils.h" |
29 | | #include "variables.h" |
30 | | #include "transform.h" |
31 | | #include "imports.h" |
32 | | #include "preproc.h" |
33 | | #include "keys.h" |
34 | | |
35 | | #ifdef WITH_XSLT_DEBUG |
36 | | #define WITH_XSLT_DEBUG_VARIABLE |
37 | | #endif |
38 | | |
39 | | #ifdef XSLT_REFACTORED |
40 | | const xmlChar *xsltDocFragFake = (const xmlChar *) " fake node libxslt"; |
41 | | #endif |
42 | | |
43 | | static const xmlChar *xsltComputingGlobalVarMarker = |
44 | | (const xmlChar *) " var/param being computed"; |
45 | | |
46 | | #define XSLT_VAR_GLOBAL (1<<0) |
47 | 0 | #define XSLT_VAR_IN_SELECT (1<<1) |
48 | 0 | #define XSLT_TCTXT_VARIABLE(c) ((xsltStackElemPtr) (c)->contextVariable) |
49 | | |
50 | | /************************************************************************ |
51 | | * * |
52 | | * Result Value Tree (Result Tree Fragment) interfaces * |
53 | | * * |
54 | | ************************************************************************/ |
55 | | /** |
56 | | * xsltCreateRVT: |
57 | | * @ctxt: an XSLT transformation context |
58 | | * |
59 | | * Creates a Result Value Tree |
60 | | * (the XSLT 1.0 term for this is "Result Tree Fragment") |
61 | | * |
62 | | * Returns the result value tree or NULL in case of API or internal errors. |
63 | | */ |
64 | | xmlDocPtr |
65 | | xsltCreateRVT(xsltTransformContextPtr ctxt) |
66 | 0 | { |
67 | 0 | xmlDocPtr container; |
68 | | |
69 | | /* |
70 | | * Question: Why is this function public? |
71 | | * Answer: It is called by the EXSLT module. |
72 | | */ |
73 | 0 | if (ctxt == NULL) |
74 | 0 | return(NULL); |
75 | | |
76 | | /* |
77 | | * Reuse a RTF from the cache if available. |
78 | | */ |
79 | 0 | if (ctxt->cache->RVT) { |
80 | 0 | container = ctxt->cache->RVT; |
81 | 0 | ctxt->cache->RVT = (xmlDocPtr) container->next; |
82 | | /* clear the internal pointers */ |
83 | 0 | container->next = NULL; |
84 | 0 | container->prev = NULL; |
85 | 0 | if (ctxt->cache->nbRVT > 0) |
86 | 0 | ctxt->cache->nbRVT--; |
87 | | #ifdef XSLT_DEBUG_PROFILE_CACHE |
88 | | ctxt->cache->dbgReusedRVTs++; |
89 | | #endif |
90 | 0 | return(container); |
91 | 0 | } |
92 | | |
93 | 0 | container = xmlNewDoc(NULL); |
94 | 0 | if (container == NULL) |
95 | 0 | return(NULL); |
96 | 0 | container->dict = ctxt->dict; |
97 | 0 | xmlDictReference(container->dict); |
98 | 0 | XSLT_MARK_RES_TREE_FRAG(container); |
99 | 0 | container->doc = container; |
100 | 0 | container->parent = NULL; |
101 | 0 | return(container); |
102 | 0 | } |
103 | | |
104 | | /** |
105 | | * xsltRegisterTmpRVT: |
106 | | * @ctxt: an XSLT transformation context |
107 | | * @RVT: a result value tree (Result Tree Fragment) |
108 | | * |
109 | | * Registers the result value tree (XSLT 1.0 term: Result Tree Fragment) |
110 | | * in the garbage collector. |
111 | | * The fragment will be freed at the exit of the currently |
112 | | * instantiated xsl:template. |
113 | | * Obsolete; this function might produce massive memory overhead, |
114 | | * since the fragment is only freed when the current xsl:template |
115 | | * exits. Use xsltRegisterLocalRVT() instead. |
116 | | * |
117 | | * Returns 0 in case of success and -1 in case of API or internal errors. |
118 | | */ |
119 | | int |
120 | | xsltRegisterTmpRVT(xsltTransformContextPtr ctxt, xmlDocPtr RVT) |
121 | 0 | { |
122 | 0 | if ((ctxt == NULL) || (RVT == NULL)) |
123 | 0 | return(-1); |
124 | | |
125 | 0 | RVT->prev = NULL; |
126 | 0 | RVT->compression = XSLT_RVT_LOCAL; |
127 | | |
128 | | /* |
129 | | * We'll restrict the lifetime of user-created fragments |
130 | | * insinde an xsl:variable and xsl:param to the lifetime of the |
131 | | * var/param itself. |
132 | | */ |
133 | 0 | if (ctxt->contextVariable != NULL) { |
134 | 0 | RVT->next = (xmlNodePtr) XSLT_TCTXT_VARIABLE(ctxt)->fragment; |
135 | 0 | XSLT_TCTXT_VARIABLE(ctxt)->fragment = RVT; |
136 | 0 | return(0); |
137 | 0 | } |
138 | | |
139 | 0 | RVT->next = (xmlNodePtr) ctxt->tmpRVT; |
140 | 0 | if (ctxt->tmpRVT != NULL) |
141 | 0 | ctxt->tmpRVT->prev = (xmlNodePtr) RVT; |
142 | 0 | ctxt->tmpRVT = RVT; |
143 | 0 | return(0); |
144 | 0 | } |
145 | | |
146 | | /** |
147 | | * xsltRegisterLocalRVT: |
148 | | * @ctxt: an XSLT transformation context |
149 | | * @RVT: a result value tree (Result Tree Fragment; xmlDocPtr) |
150 | | * |
151 | | * Registers a result value tree (XSLT 1.0 term: Result Tree Fragment) |
152 | | * in the RVT garbage collector. |
153 | | * The fragment will be freed when the instruction which created the |
154 | | * fragment exits. |
155 | | * |
156 | | * Returns 0 in case of success and -1 in case of API or internal errors. |
157 | | */ |
158 | | int |
159 | | xsltRegisterLocalRVT(xsltTransformContextPtr ctxt, |
160 | | xmlDocPtr RVT) |
161 | 0 | { |
162 | 0 | if ((ctxt == NULL) || (RVT == NULL)) |
163 | 0 | return(-1); |
164 | | |
165 | 0 | RVT->prev = NULL; |
166 | 0 | RVT->compression = XSLT_RVT_LOCAL; |
167 | | |
168 | | /* |
169 | | * When evaluating "select" expressions of xsl:variable |
170 | | * and xsl:param, we need to bind newly created tree fragments |
171 | | * to the variable itself; otherwise the fragment will be |
172 | | * freed before we leave the scope of a var. |
173 | | */ |
174 | 0 | if ((ctxt->contextVariable != NULL) && |
175 | 0 | (XSLT_TCTXT_VARIABLE(ctxt)->flags & XSLT_VAR_IN_SELECT)) |
176 | 0 | { |
177 | 0 | RVT->next = (xmlNodePtr) XSLT_TCTXT_VARIABLE(ctxt)->fragment; |
178 | 0 | XSLT_TCTXT_VARIABLE(ctxt)->fragment = RVT; |
179 | 0 | return(0); |
180 | 0 | } |
181 | | /* |
182 | | * Store the fragment in the scope of the current instruction. |
183 | | * If not reference by a returning instruction (like EXSLT's function), |
184 | | * then this fragment will be freed, when the instruction exits. |
185 | | */ |
186 | 0 | RVT->next = (xmlNodePtr) ctxt->localRVT; |
187 | 0 | if (ctxt->localRVT != NULL) |
188 | 0 | ctxt->localRVT->prev = (xmlNodePtr) RVT; |
189 | 0 | ctxt->localRVT = RVT; |
190 | 0 | return(0); |
191 | 0 | } |
192 | | |
193 | | /** |
194 | | * xsltExtensionInstructionResultFinalize: |
195 | | * @ctxt: an XSLT transformation context |
196 | | * |
197 | | * Finalizes the data (e.g. result tree fragments) created |
198 | | * within a value-returning process (e.g. EXSLT's function). |
199 | | * Tree fragments marked as being returned by a function are |
200 | | * set to normal state, which means that the fragment garbage |
201 | | * collector will free them after the function-calling process exits. |
202 | | * |
203 | | * Returns 0 in case of success and -1 in case of API or internal errors. |
204 | | * |
205 | | * This function is unsupported in newer releases of libxslt. |
206 | | */ |
207 | | int |
208 | | xsltExtensionInstructionResultFinalize( |
209 | | xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED) |
210 | 0 | { |
211 | 0 | xmlGenericError(xmlGenericErrorContext, |
212 | 0 | "xsltExtensionInstructionResultFinalize is unsupported " |
213 | 0 | "in this release of libxslt.\n"); |
214 | 0 | return(-1); |
215 | 0 | } |
216 | | |
217 | | /** |
218 | | * xsltExtensionInstructionResultRegister: |
219 | | * @ctxt: an XSLT transformation context |
220 | | * @obj: an XPath object to be inspected for result tree fragments |
221 | | * |
222 | | * Marks the result of a value-returning extension instruction |
223 | | * in order to avoid it being garbage collected before the |
224 | | * extension instruction exits. |
225 | | * Note that one still has to additionally register any newly created |
226 | | * tree fragments (via xsltCreateRVT()) with xsltRegisterLocalRVT(). |
227 | | * |
228 | | * Returns 0 in case of success and -1 in case of error. |
229 | | * |
230 | | * It isn't necessary to call this function in newer releases of |
231 | | * libxslt. |
232 | | */ |
233 | | int |
234 | | xsltExtensionInstructionResultRegister( |
235 | | xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED, |
236 | | xmlXPathObjectPtr obj ATTRIBUTE_UNUSED) |
237 | 0 | { |
238 | 0 | return(0); |
239 | 0 | } |
240 | | |
241 | | /** |
242 | | * xsltFlagRVTs: |
243 | | * @ctxt: an XSLT transformation context |
244 | | * @obj: an XPath object to be inspected for result tree fragments |
245 | | * @val: the flag value |
246 | | * |
247 | | * Updates ownership information of RVTs in @obj according to @val. |
248 | | * |
249 | | * @val = XSLT_RVT_FUNC_RESULT for the result of an extension function, so its |
250 | | * RVTs won't be destroyed after leaving the returning scope. |
251 | | * @val = XSLT_RVT_LOCAL for the result of an extension function to reset |
252 | | * the state of its RVTs after it was returned to a new scope. |
253 | | * @val = XSLT_RVT_GLOBAL for parts of global variables. |
254 | | * |
255 | | * Returns 0 in case of success and -1 in case of error. |
256 | | */ |
257 | | int |
258 | 0 | xsltFlagRVTs(xsltTransformContextPtr ctxt, xmlXPathObjectPtr obj, int val) { |
259 | 0 | int i; |
260 | 0 | xmlNodePtr cur; |
261 | 0 | xmlDocPtr doc; |
262 | |
|
263 | 0 | if ((ctxt == NULL) || (obj == NULL)) |
264 | 0 | return(-1); |
265 | | |
266 | | /* |
267 | | * OPTIMIZE TODO: If no local variables/params and no local tree |
268 | | * fragments were created, then we don't need to analyse the XPath |
269 | | * objects for tree fragments. |
270 | | */ |
271 | | |
272 | 0 | if ((obj->type != XPATH_NODESET) && (obj->type != XPATH_XSLT_TREE)) |
273 | 0 | return(0); |
274 | 0 | if ((obj->nodesetval == NULL) || (obj->nodesetval->nodeNr == 0)) |
275 | 0 | return(0); |
276 | | |
277 | 0 | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
278 | 0 | cur = obj->nodesetval->nodeTab[i]; |
279 | 0 | if (cur->type == XML_NAMESPACE_DECL) { |
280 | | /* |
281 | | * The XPath module sets the owner element of a ns-node on |
282 | | * the ns->next field. |
283 | | */ |
284 | 0 | if ((((xmlNsPtr) cur)->next != NULL) && |
285 | 0 | (((xmlNsPtr) cur)->next->type == XML_ELEMENT_NODE)) |
286 | 0 | { |
287 | 0 | cur = (xmlNodePtr) ((xmlNsPtr) cur)->next; |
288 | 0 | doc = cur->doc; |
289 | 0 | } else { |
290 | 0 | xsltTransformError(ctxt, NULL, ctxt->inst, |
291 | 0 | "Internal error in xsltFlagRVTs(): " |
292 | 0 | "Cannot retrieve the doc of a namespace node.\n"); |
293 | 0 | return(-1); |
294 | 0 | } |
295 | 0 | } else { |
296 | 0 | doc = cur->doc; |
297 | 0 | } |
298 | 0 | if (doc == NULL) { |
299 | 0 | xsltTransformError(ctxt, NULL, ctxt->inst, |
300 | 0 | "Internal error in xsltFlagRVTs(): " |
301 | 0 | "Cannot retrieve the doc of a node.\n"); |
302 | 0 | return(-1); |
303 | 0 | } |
304 | 0 | if (doc->name && (doc->name[0] == ' ') && |
305 | 0 | doc->compression != XSLT_RVT_GLOBAL) { |
306 | | /* |
307 | | * This is a result tree fragment. |
308 | | * We store ownership information in the @compression field. |
309 | | * TODO: How do we know if this is a doc acquired via the |
310 | | * document() function? |
311 | | */ |
312 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
313 | | XSLT_TRACE(ctxt, XSLT_TRACE_VARIABLES, |
314 | | xsltGenericDebug(xsltGenericDebugContext, |
315 | | "Flagging RVT %p: %d -> %d\n", |
316 | | (void *) doc, doc->compression, val)); |
317 | | #endif |
318 | |
|
319 | 0 | if (val == XSLT_RVT_LOCAL) { |
320 | 0 | if (doc->compression == XSLT_RVT_FUNC_RESULT) |
321 | 0 | doc->compression = XSLT_RVT_LOCAL; |
322 | 0 | } else if (val == XSLT_RVT_GLOBAL) { |
323 | 0 | if (doc->compression != XSLT_RVT_LOCAL) { |
324 | 0 | xmlGenericError(xmlGenericErrorContext, |
325 | 0 | "xsltFlagRVTs: Invalid transition %d => GLOBAL\n", |
326 | 0 | doc->compression); |
327 | 0 | doc->compression = XSLT_RVT_GLOBAL; |
328 | 0 | return(-1); |
329 | 0 | } |
330 | | |
331 | | /* Will be registered as persistant in xsltReleaseLocalRVTs. */ |
332 | 0 | doc->compression = XSLT_RVT_GLOBAL; |
333 | 0 | } else if (val == XSLT_RVT_FUNC_RESULT) { |
334 | 0 | doc->compression = val; |
335 | 0 | } |
336 | 0 | } |
337 | 0 | } |
338 | | |
339 | 0 | return(0); |
340 | 0 | } |
341 | | |
342 | | /** |
343 | | * xsltReleaseRVT: |
344 | | * @ctxt: an XSLT transformation context |
345 | | * @RVT: a result value tree (Result Tree Fragment) |
346 | | * |
347 | | * Either frees the RVT (which is an xmlDoc) or stores |
348 | | * it in the context's cache for later reuse. |
349 | | */ |
350 | | void |
351 | | xsltReleaseRVT(xsltTransformContextPtr ctxt, xmlDocPtr RVT) |
352 | 0 | { |
353 | 0 | if (RVT == NULL) |
354 | 0 | return; |
355 | | |
356 | 0 | if (ctxt && (ctxt->cache->nbRVT < 40)) { |
357 | | /* |
358 | | * Store the Result Tree Fragment. |
359 | | * Free the document info. |
360 | | */ |
361 | 0 | if (RVT->_private != NULL) { |
362 | 0 | xsltFreeDocumentKeys((xsltDocumentPtr) RVT->_private); |
363 | 0 | xmlFree(RVT->_private); |
364 | 0 | RVT->_private = NULL; |
365 | 0 | } |
366 | | /* |
367 | | * Clear the document tree. |
368 | | */ |
369 | 0 | if (RVT->children != NULL) { |
370 | 0 | xmlFreeNodeList(RVT->children); |
371 | 0 | RVT->children = NULL; |
372 | 0 | RVT->last = NULL; |
373 | 0 | } |
374 | 0 | if (RVT->ids != NULL) { |
375 | 0 | xmlFreeIDTable((xmlIDTablePtr) RVT->ids); |
376 | 0 | RVT->ids = NULL; |
377 | 0 | } |
378 | | |
379 | | /* |
380 | | * Reset the ownership information. |
381 | | */ |
382 | 0 | RVT->compression = 0; |
383 | |
|
384 | 0 | RVT->next = (xmlNodePtr) ctxt->cache->RVT; |
385 | 0 | ctxt->cache->RVT = RVT; |
386 | |
|
387 | 0 | ctxt->cache->nbRVT++; |
388 | |
|
389 | | #ifdef XSLT_DEBUG_PROFILE_CACHE |
390 | | ctxt->cache->dbgCachedRVTs++; |
391 | | #endif |
392 | 0 | return; |
393 | 0 | } |
394 | | /* |
395 | | * Free it. |
396 | | */ |
397 | 0 | if (RVT->_private != NULL) { |
398 | 0 | xsltFreeDocumentKeys((xsltDocumentPtr) RVT->_private); |
399 | 0 | xmlFree(RVT->_private); |
400 | 0 | } |
401 | 0 | xmlFreeDoc(RVT); |
402 | 0 | } |
403 | | |
404 | | /** |
405 | | * xsltRegisterPersistRVT: |
406 | | * @ctxt: an XSLT transformation context |
407 | | * @RVT: a result value tree (Result Tree Fragment) |
408 | | * |
409 | | * Register the result value tree (XSLT 1.0 term: Result Tree Fragment) |
410 | | * in the fragment garbage collector. |
411 | | * The fragment will be freed when the transformation context is |
412 | | * freed. |
413 | | * |
414 | | * Returns 0 in case of success and -1 in case of error. |
415 | | */ |
416 | | int |
417 | | xsltRegisterPersistRVT(xsltTransformContextPtr ctxt, xmlDocPtr RVT) |
418 | 0 | { |
419 | 0 | if ((ctxt == NULL) || (RVT == NULL)) return(-1); |
420 | | |
421 | 0 | RVT->compression = XSLT_RVT_GLOBAL; |
422 | 0 | RVT->prev = NULL; |
423 | 0 | RVT->next = (xmlNodePtr) ctxt->persistRVT; |
424 | 0 | if (ctxt->persistRVT != NULL) |
425 | 0 | ctxt->persistRVT->prev = (xmlNodePtr) RVT; |
426 | 0 | ctxt->persistRVT = RVT; |
427 | 0 | return(0); |
428 | 0 | } |
429 | | |
430 | | /** |
431 | | * xsltFreeRVTs: |
432 | | * @ctxt: an XSLT transformation context |
433 | | * |
434 | | * Frees all registered result value trees (Result Tree Fragments) |
435 | | * of the transformation. Internal function; should not be called |
436 | | * by user-code. |
437 | | */ |
438 | | void |
439 | | xsltFreeRVTs(xsltTransformContextPtr ctxt) |
440 | 0 | { |
441 | 0 | xmlDocPtr cur, next; |
442 | |
|
443 | 0 | if (ctxt == NULL) |
444 | 0 | return; |
445 | | /* |
446 | | * Local fragments. |
447 | | */ |
448 | 0 | cur = ctxt->localRVT; |
449 | 0 | while (cur != NULL) { |
450 | 0 | next = (xmlDocPtr) cur->next; |
451 | 0 | if (cur->_private != NULL) { |
452 | 0 | xsltFreeDocumentKeys(cur->_private); |
453 | 0 | xmlFree(cur->_private); |
454 | 0 | } |
455 | 0 | xmlFreeDoc(cur); |
456 | 0 | cur = next; |
457 | 0 | } |
458 | 0 | ctxt->localRVT = NULL; |
459 | | /* |
460 | | * User-created per-template fragments. |
461 | | */ |
462 | 0 | cur = ctxt->tmpRVT; |
463 | 0 | while (cur != NULL) { |
464 | 0 | next = (xmlDocPtr) cur->next; |
465 | 0 | if (cur->_private != NULL) { |
466 | 0 | xsltFreeDocumentKeys(cur->_private); |
467 | 0 | xmlFree(cur->_private); |
468 | 0 | } |
469 | 0 | xmlFreeDoc(cur); |
470 | 0 | cur = next; |
471 | 0 | } |
472 | 0 | ctxt->tmpRVT = NULL; |
473 | | /* |
474 | | * Global fragments. |
475 | | */ |
476 | 0 | cur = ctxt->persistRVT; |
477 | 0 | while (cur != NULL) { |
478 | 0 | next = (xmlDocPtr) cur->next; |
479 | 0 | if (cur->_private != NULL) { |
480 | 0 | xsltFreeDocumentKeys(cur->_private); |
481 | 0 | xmlFree(cur->_private); |
482 | 0 | } |
483 | 0 | xmlFreeDoc(cur); |
484 | 0 | cur = next; |
485 | 0 | } |
486 | 0 | ctxt->persistRVT = NULL; |
487 | 0 | } |
488 | | |
489 | | /************************************************************************ |
490 | | * * |
491 | | * Module interfaces * |
492 | | * * |
493 | | ************************************************************************/ |
494 | | |
495 | | /** |
496 | | * xsltNewStackElem: |
497 | | * |
498 | | * Create a new XSLT ParserContext |
499 | | * |
500 | | * Returns the newly allocated xsltParserStackElem or NULL in case of error |
501 | | */ |
502 | | static xsltStackElemPtr |
503 | | xsltNewStackElem(xsltTransformContextPtr ctxt) |
504 | 0 | { |
505 | 0 | xsltStackElemPtr ret; |
506 | | /* |
507 | | * Reuse a stack item from the cache if available. |
508 | | */ |
509 | 0 | if (ctxt && ctxt->cache->stackItems) { |
510 | 0 | ret = ctxt->cache->stackItems; |
511 | 0 | ctxt->cache->stackItems = ret->next; |
512 | 0 | ret->next = NULL; |
513 | 0 | ctxt->cache->nbStackItems--; |
514 | | #ifdef XSLT_DEBUG_PROFILE_CACHE |
515 | | ctxt->cache->dbgReusedVars++; |
516 | | #endif |
517 | 0 | return(ret); |
518 | 0 | } |
519 | 0 | ret = (xsltStackElemPtr) xmlMalloc(sizeof(xsltStackElem)); |
520 | 0 | if (ret == NULL) { |
521 | 0 | xsltTransformError(NULL, NULL, NULL, |
522 | 0 | "xsltNewStackElem : malloc failed\n"); |
523 | 0 | return(NULL); |
524 | 0 | } |
525 | 0 | memset(ret, 0, sizeof(xsltStackElem)); |
526 | 0 | ret->context = ctxt; |
527 | 0 | return(ret); |
528 | 0 | } |
529 | | |
530 | | /** |
531 | | * xsltCopyStackElem: |
532 | | * @elem: an XSLT stack element |
533 | | * |
534 | | * Makes a copy of the stack element |
535 | | * |
536 | | * Returns the copy of NULL |
537 | | */ |
538 | | static xsltStackElemPtr |
539 | 0 | xsltCopyStackElem(xsltStackElemPtr elem) { |
540 | 0 | xsltStackElemPtr cur; |
541 | |
|
542 | 0 | cur = (xsltStackElemPtr) xmlMalloc(sizeof(xsltStackElem)); |
543 | 0 | if (cur == NULL) { |
544 | 0 | xsltTransformError(NULL, NULL, NULL, |
545 | 0 | "xsltCopyStackElem : malloc failed\n"); |
546 | 0 | return(NULL); |
547 | 0 | } |
548 | 0 | memset(cur, 0, sizeof(xsltStackElem)); |
549 | 0 | cur->context = elem->context; |
550 | 0 | cur->name = elem->name; |
551 | 0 | cur->nameURI = elem->nameURI; |
552 | 0 | cur->select = elem->select; |
553 | 0 | cur->tree = elem->tree; |
554 | 0 | cur->comp = elem->comp; |
555 | 0 | return(cur); |
556 | 0 | } |
557 | | |
558 | | /** |
559 | | * xsltFreeStackElem: |
560 | | * @elem: an XSLT stack element |
561 | | * |
562 | | * Free up the memory allocated by @elem |
563 | | */ |
564 | | static void |
565 | 0 | xsltFreeStackElem(xsltStackElemPtr elem) { |
566 | 0 | if (elem == NULL) |
567 | 0 | return; |
568 | 0 | if (elem->value != NULL) |
569 | 0 | xmlXPathFreeObject(elem->value); |
570 | | /* |
571 | | * Release the list of temporary Result Tree Fragments. |
572 | | */ |
573 | 0 | if (elem->context) { |
574 | 0 | xmlDocPtr cur; |
575 | |
|
576 | 0 | while (elem->fragment != NULL) { |
577 | 0 | cur = elem->fragment; |
578 | 0 | elem->fragment = (xmlDocPtr) cur->next; |
579 | |
|
580 | 0 | if (cur->compression == XSLT_RVT_LOCAL) { |
581 | 0 | xsltReleaseRVT(elem->context, cur); |
582 | 0 | } else if (cur->compression == XSLT_RVT_FUNC_RESULT) { |
583 | 0 | xsltRegisterLocalRVT(elem->context, cur); |
584 | 0 | cur->compression = XSLT_RVT_FUNC_RESULT; |
585 | 0 | } else { |
586 | 0 | xmlGenericError(xmlGenericErrorContext, |
587 | 0 | "xsltFreeStackElem: Unexpected RVT flag %d\n", |
588 | 0 | cur->compression); |
589 | 0 | } |
590 | 0 | } |
591 | 0 | } |
592 | | /* |
593 | | * Cache or free the variable structure. |
594 | | */ |
595 | 0 | if (elem->context && (elem->context->cache->nbStackItems < 50)) { |
596 | | /* |
597 | | * Store the item in the cache. |
598 | | */ |
599 | 0 | xsltTransformContextPtr ctxt = elem->context; |
600 | 0 | memset(elem, 0, sizeof(xsltStackElem)); |
601 | 0 | elem->context = ctxt; |
602 | 0 | elem->next = ctxt->cache->stackItems; |
603 | 0 | ctxt->cache->stackItems = elem; |
604 | 0 | ctxt->cache->nbStackItems++; |
605 | | #ifdef XSLT_DEBUG_PROFILE_CACHE |
606 | | ctxt->cache->dbgCachedVars++; |
607 | | #endif |
608 | 0 | return; |
609 | 0 | } |
610 | 0 | xmlFree(elem); |
611 | 0 | } |
612 | | |
613 | | static void |
614 | 0 | xsltFreeStackElemEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) { |
615 | 0 | xsltFreeStackElem((xsltStackElemPtr) payload); |
616 | 0 | } |
617 | | |
618 | | |
619 | | /** |
620 | | * xsltFreeStackElemList: |
621 | | * @elem: an XSLT stack element |
622 | | * |
623 | | * Free up the memory allocated by @elem |
624 | | */ |
625 | | void |
626 | 0 | xsltFreeStackElemList(xsltStackElemPtr elem) { |
627 | 0 | xsltStackElemPtr next; |
628 | |
|
629 | 0 | while (elem != NULL) { |
630 | 0 | next = elem->next; |
631 | 0 | xsltFreeStackElem(elem); |
632 | 0 | elem = next; |
633 | 0 | } |
634 | 0 | } |
635 | | |
636 | | /** |
637 | | * xsltStackLookup: |
638 | | * @ctxt: an XSLT transformation context |
639 | | * @name: the local part of the name |
640 | | * @nameURI: the URI part of the name |
641 | | * |
642 | | * Locate an element in the stack based on its name. |
643 | | */ |
644 | | static xsltStackElemPtr |
645 | | xsltStackLookup(xsltTransformContextPtr ctxt, const xmlChar *name, |
646 | 0 | const xmlChar *nameURI) { |
647 | 0 | int i; |
648 | 0 | xsltStackElemPtr cur; |
649 | |
|
650 | 0 | if ((ctxt == NULL) || (name == NULL) || (ctxt->varsNr == 0)) |
651 | 0 | return(NULL); |
652 | | |
653 | | /* |
654 | | * Do the lookup from the top of the stack, but |
655 | | * don't use params being computed in a call-param |
656 | | * First lookup expects the variable name and URI to |
657 | | * come from the disctionnary and hence pointer comparison. |
658 | | */ |
659 | 0 | for (i = ctxt->varsNr; i > ctxt->varsBase; i--) { |
660 | 0 | cur = ctxt->varsTab[i-1]; |
661 | 0 | while (cur != NULL) { |
662 | 0 | if ((cur->name == name) && (cur->nameURI == nameURI)) { |
663 | 0 | return(cur); |
664 | 0 | } |
665 | 0 | cur = cur->next; |
666 | 0 | } |
667 | 0 | } |
668 | | |
669 | | /* |
670 | | * Redo the lookup with interned string compares |
671 | | * to avoid string compares. |
672 | | */ |
673 | 0 | name = xmlDictLookup(ctxt->dict, name, -1); |
674 | 0 | if (nameURI != NULL) |
675 | 0 | nameURI = xmlDictLookup(ctxt->dict, nameURI, -1); |
676 | |
|
677 | 0 | for (i = ctxt->varsNr; i > ctxt->varsBase; i--) { |
678 | 0 | cur = ctxt->varsTab[i-1]; |
679 | 0 | while (cur != NULL) { |
680 | 0 | if ((cur->name == name) && (cur->nameURI == nameURI)) { |
681 | 0 | return(cur); |
682 | 0 | } |
683 | 0 | cur = cur->next; |
684 | 0 | } |
685 | 0 | } |
686 | | |
687 | 0 | return(NULL); |
688 | 0 | } |
689 | | |
690 | | #ifdef XSLT_REFACTORED |
691 | | #else |
692 | | |
693 | | /** |
694 | | * xsltCheckStackElem: |
695 | | * @ctxt: xn XSLT transformation context |
696 | | * @name: the variable name |
697 | | * @nameURI: the variable namespace URI |
698 | | * |
699 | | * Checks whether a variable or param is already defined. |
700 | | * |
701 | | * URGENT TODO: Checks for redefinition of vars/params should be |
702 | | * done only at compilation time. |
703 | | * |
704 | | * Returns 1 if variable is present, 2 if param is present, 3 if this |
705 | | * is an inherited param, 0 if not found, -1 in case of failure. |
706 | | */ |
707 | | static int |
708 | | xsltCheckStackElem(xsltTransformContextPtr ctxt, const xmlChar *name, |
709 | 0 | const xmlChar *nameURI) { |
710 | 0 | xsltStackElemPtr cur; |
711 | |
|
712 | 0 | if ((ctxt == NULL) || (name == NULL)) |
713 | 0 | return(-1); |
714 | | |
715 | 0 | cur = xsltStackLookup(ctxt, name, nameURI); |
716 | 0 | if (cur == NULL) |
717 | 0 | return(0); |
718 | 0 | if (cur->comp != NULL) { |
719 | 0 | if (cur->comp->type == XSLT_FUNC_WITHPARAM) |
720 | 0 | return(3); |
721 | 0 | else if (cur->comp->type == XSLT_FUNC_PARAM) |
722 | 0 | return(2); |
723 | 0 | } |
724 | | |
725 | 0 | return(1); |
726 | 0 | } |
727 | | |
728 | | #endif /* XSLT_REFACTORED */ |
729 | | |
730 | | /** |
731 | | * xsltAddStackElem: |
732 | | * @ctxt: xn XSLT transformation context |
733 | | * @elem: a stack element |
734 | | * |
735 | | * Push an element (or list) onto the stack. |
736 | | * In case of a list, each member will be pushed into |
737 | | * a seperate slot; i.e. there's always 1 stack entry for |
738 | | * 1 stack element. |
739 | | * |
740 | | * Returns 0 in case of success, -1 in case of failure. |
741 | | */ |
742 | | static int |
743 | | xsltAddStackElem(xsltTransformContextPtr ctxt, xsltStackElemPtr elem) |
744 | 0 | { |
745 | 0 | if ((ctxt == NULL) || (elem == NULL)) |
746 | 0 | return(-1); |
747 | | |
748 | 0 | do { |
749 | 0 | if (ctxt->varsNr >= ctxt->varsMax) { |
750 | 0 | xsltStackElemPtr *tmp; |
751 | 0 | int newMax = ctxt->varsMax == 0 ? 10 : 2 * ctxt->varsMax; |
752 | |
|
753 | 0 | tmp = (xsltStackElemPtr *) xmlRealloc(ctxt->varsTab, |
754 | 0 | newMax * sizeof(*tmp)); |
755 | 0 | if (tmp == NULL) { |
756 | 0 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
757 | 0 | return (-1); |
758 | 0 | } |
759 | 0 | ctxt->varsTab = tmp; |
760 | 0 | ctxt->varsMax = newMax; |
761 | 0 | } |
762 | 0 | ctxt->varsTab[ctxt->varsNr++] = elem; |
763 | 0 | ctxt->vars = elem; |
764 | |
|
765 | 0 | elem = elem->next; |
766 | 0 | } while (elem != NULL); |
767 | | |
768 | 0 | return(0); |
769 | 0 | } |
770 | | |
771 | | /** |
772 | | * xsltAddStackElemList: |
773 | | * @ctxt: xn XSLT transformation context |
774 | | * @elems: a stack element list |
775 | | * |
776 | | * Push an element list onto the stack. |
777 | | * |
778 | | * Returns 0 in case of success, -1 in case of failure. |
779 | | */ |
780 | | int |
781 | | xsltAddStackElemList(xsltTransformContextPtr ctxt, xsltStackElemPtr elems) |
782 | 0 | { |
783 | 0 | return(xsltAddStackElem(ctxt, elems)); |
784 | 0 | } |
785 | | |
786 | | /************************************************************************ |
787 | | * * |
788 | | * Module interfaces * |
789 | | * * |
790 | | ************************************************************************/ |
791 | | |
792 | | /** |
793 | | * xsltEvalVariable: |
794 | | * @ctxt: the XSLT transformation context |
795 | | * @variable: the variable or parameter item |
796 | | * @comp: the compiled XSLT instruction |
797 | | * |
798 | | * Evaluate a variable value. |
799 | | * |
800 | | * Returns the XPath Object value or NULL in case of error |
801 | | */ |
802 | | static xmlXPathObjectPtr |
803 | | xsltEvalVariable(xsltTransformContextPtr ctxt, xsltStackElemPtr variable, |
804 | | xsltStylePreCompPtr castedComp) |
805 | 0 | { |
806 | | #ifdef XSLT_REFACTORED |
807 | | xsltStyleItemVariablePtr comp = |
808 | | (xsltStyleItemVariablePtr) castedComp; |
809 | | #else |
810 | 0 | xsltStylePreCompPtr comp = castedComp; |
811 | 0 | #endif |
812 | 0 | xmlXPathObjectPtr result = NULL; |
813 | 0 | xmlNodePtr oldInst; |
814 | |
|
815 | 0 | if ((ctxt == NULL) || (variable == NULL)) |
816 | 0 | return(NULL); |
817 | | |
818 | | /* |
819 | | * A variable or parameter are evaluated on demand; thus the |
820 | | * context (of XSLT and XPath) need to be temporarily adjusted and |
821 | | * restored on exit. |
822 | | */ |
823 | 0 | oldInst = ctxt->inst; |
824 | |
|
825 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
826 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
827 | | "Evaluating variable '%s'\n", variable->name)); |
828 | | #endif |
829 | 0 | if (variable->select != NULL) { |
830 | 0 | xmlXPathCompExprPtr xpExpr = NULL; |
831 | 0 | xmlDocPtr oldXPDoc; |
832 | 0 | xmlNodePtr oldXPContextNode; |
833 | 0 | int oldXPProximityPosition, oldXPContextSize, oldXPNsNr; |
834 | 0 | xmlNsPtr *oldXPNamespaces; |
835 | 0 | xmlXPathContextPtr xpctxt = ctxt->xpathCtxt; |
836 | 0 | xsltStackElemPtr oldVar = ctxt->contextVariable; |
837 | |
|
838 | 0 | if ((comp != NULL) && (comp->comp != NULL)) { |
839 | 0 | xpExpr = comp->comp; |
840 | 0 | } else { |
841 | 0 | xpExpr = xmlXPathCtxtCompile(ctxt->xpathCtxt, variable->select); |
842 | 0 | } |
843 | 0 | if (xpExpr == NULL) |
844 | 0 | return(NULL); |
845 | | /* |
846 | | * Save context states. |
847 | | */ |
848 | 0 | oldXPDoc = xpctxt->doc; |
849 | 0 | oldXPContextNode = xpctxt->node; |
850 | 0 | oldXPProximityPosition = xpctxt->proximityPosition; |
851 | 0 | oldXPContextSize = xpctxt->contextSize; |
852 | 0 | oldXPNamespaces = xpctxt->namespaces; |
853 | 0 | oldXPNsNr = xpctxt->nsNr; |
854 | |
|
855 | 0 | xpctxt->node = ctxt->node; |
856 | | /* |
857 | | * OPTIMIZE TODO: Lame try to set the context doc. |
858 | | * Get rid of this somehow in xpath.c. |
859 | | */ |
860 | 0 | if ((ctxt->node->type != XML_NAMESPACE_DECL) && |
861 | 0 | ctxt->node->doc) |
862 | 0 | xpctxt->doc = ctxt->node->doc; |
863 | | /* |
864 | | * BUG TODO: The proximity position and the context size will |
865 | | * potentially be wrong. |
866 | | * Example: |
867 | | * <xsl:template select="foo"> |
868 | | * <xsl:variable name="pos" select="position()"/> |
869 | | * <xsl:for-each select="bar"> |
870 | | * <xsl:value-of select="$pos"/> |
871 | | * </xsl:for-each> |
872 | | * </xsl:template> |
873 | | * Here the proximity position and context size are changed |
874 | | * to the context of <xsl:for-each select="bar">, but |
875 | | * the variable needs to be evaluated in the context of |
876 | | * <xsl:template select="foo">. |
877 | | */ |
878 | 0 | if (comp != NULL) { |
879 | |
|
880 | | #ifdef XSLT_REFACTORED |
881 | | if (comp->inScopeNs != NULL) { |
882 | | xpctxt->namespaces = comp->inScopeNs->list; |
883 | | xpctxt->nsNr = comp->inScopeNs->xpathNumber; |
884 | | } else { |
885 | | xpctxt->namespaces = NULL; |
886 | | xpctxt->nsNr = 0; |
887 | | } |
888 | | #else |
889 | 0 | xpctxt->namespaces = comp->nsList; |
890 | 0 | xpctxt->nsNr = comp->nsNr; |
891 | 0 | #endif |
892 | 0 | } else { |
893 | 0 | xpctxt->namespaces = NULL; |
894 | 0 | xpctxt->nsNr = 0; |
895 | 0 | } |
896 | | |
897 | | /* |
898 | | * We need to mark that we are "selecting" a var's value; |
899 | | * if any tree fragments are created inside the expression, |
900 | | * then those need to be stored inside the variable; otherwise |
901 | | * we'll eventually free still referenced fragments, before |
902 | | * we leave the scope of the variable. |
903 | | */ |
904 | 0 | ctxt->contextVariable = variable; |
905 | 0 | variable->flags |= XSLT_VAR_IN_SELECT; |
906 | |
|
907 | 0 | result = xmlXPathCompiledEval(xpExpr, xpctxt); |
908 | |
|
909 | 0 | variable->flags ^= XSLT_VAR_IN_SELECT; |
910 | | /* |
911 | | * Restore Context states. |
912 | | */ |
913 | 0 | ctxt->contextVariable = oldVar; |
914 | |
|
915 | 0 | xpctxt->doc = oldXPDoc; |
916 | 0 | xpctxt->node = oldXPContextNode; |
917 | 0 | xpctxt->contextSize = oldXPContextSize; |
918 | 0 | xpctxt->proximityPosition = oldXPProximityPosition; |
919 | 0 | xpctxt->namespaces = oldXPNamespaces; |
920 | 0 | xpctxt->nsNr = oldXPNsNr; |
921 | |
|
922 | 0 | if ((comp == NULL) || (comp->comp == NULL)) |
923 | 0 | xmlXPathFreeCompExpr(xpExpr); |
924 | 0 | if (result == NULL) { |
925 | 0 | xsltTransformError(ctxt, NULL, |
926 | 0 | (comp != NULL) ? comp->inst : NULL, |
927 | 0 | "Failed to evaluate the expression of variable '%s'.\n", |
928 | 0 | variable->name); |
929 | 0 | ctxt->state = XSLT_STATE_STOPPED; |
930 | |
|
931 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
932 | | #ifdef LIBXML_DEBUG_ENABLED |
933 | | } else { |
934 | | if ((xsltGenericDebugContext == stdout) || |
935 | | (xsltGenericDebugContext == stderr)) |
936 | | xmlXPathDebugDumpObject((FILE *)xsltGenericDebugContext, |
937 | | result, 0); |
938 | | #endif |
939 | | #endif |
940 | 0 | } |
941 | 0 | } else { |
942 | 0 | if (variable->tree == NULL) { |
943 | 0 | result = xmlXPathNewCString(""); |
944 | 0 | } else { |
945 | 0 | if (variable->tree) { |
946 | 0 | xmlDocPtr container; |
947 | 0 | xmlNodePtr oldInsert; |
948 | 0 | xmlDocPtr oldOutput; |
949 | 0 | const xmlChar *oldLastText; |
950 | 0 | int oldLastTextSize, oldLastTextUse; |
951 | 0 | xsltStackElemPtr oldVar = ctxt->contextVariable; |
952 | | |
953 | | /* |
954 | | * Generate a result tree fragment. |
955 | | */ |
956 | 0 | container = xsltCreateRVT(ctxt); |
957 | 0 | if (container == NULL) |
958 | 0 | goto error; |
959 | | /* |
960 | | * NOTE: Local Result Tree Fragments of params/variables |
961 | | * are not registered globally anymore; the life-time |
962 | | * is not directly dependant of the param/variable itself. |
963 | | * |
964 | | * OLD: xsltRegisterTmpRVT(ctxt, container); |
965 | | */ |
966 | | /* |
967 | | * Attach the Result Tree Fragment to the variable; |
968 | | * when the variable is freed, it will also free |
969 | | * the Result Tree Fragment. |
970 | | */ |
971 | 0 | variable->fragment = container; |
972 | 0 | container->compression = XSLT_RVT_LOCAL; |
973 | |
|
974 | 0 | oldOutput = ctxt->output; |
975 | 0 | oldInsert = ctxt->insert; |
976 | 0 | oldLastText = ctxt->lasttext; |
977 | 0 | oldLastTextSize = ctxt->lasttsize; |
978 | 0 | oldLastTextUse = ctxt->lasttuse; |
979 | |
|
980 | 0 | ctxt->output = container; |
981 | 0 | ctxt->insert = (xmlNodePtr) container; |
982 | 0 | ctxt->contextVariable = variable; |
983 | | /* |
984 | | * Process the sequence constructor (variable->tree). |
985 | | * The resulting tree will be held by @container. |
986 | | */ |
987 | 0 | xsltApplyOneTemplate(ctxt, ctxt->node, variable->tree, |
988 | 0 | NULL, NULL); |
989 | |
|
990 | 0 | ctxt->contextVariable = oldVar; |
991 | 0 | ctxt->insert = oldInsert; |
992 | 0 | ctxt->output = oldOutput; |
993 | 0 | ctxt->lasttext = oldLastText; |
994 | 0 | ctxt->lasttsize = oldLastTextSize; |
995 | 0 | ctxt->lasttuse = oldLastTextUse; |
996 | |
|
997 | 0 | result = xmlXPathNewValueTree((xmlNodePtr) container); |
998 | 0 | } |
999 | 0 | if (result == NULL) { |
1000 | 0 | result = xmlXPathNewCString(""); |
1001 | 0 | } else { |
1002 | | /* |
1003 | | * This stops older libxml2 versions from freeing the nodes |
1004 | | * in the tree. |
1005 | | */ |
1006 | 0 | result->boolval = 0; |
1007 | 0 | } |
1008 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
1009 | | #ifdef LIBXML_DEBUG_ENABLED |
1010 | | |
1011 | | if ((xsltGenericDebugContext == stdout) || |
1012 | | (xsltGenericDebugContext == stderr)) |
1013 | | xmlXPathDebugDumpObject((FILE *)xsltGenericDebugContext, |
1014 | | result, 0); |
1015 | | #endif |
1016 | | #endif |
1017 | 0 | } |
1018 | 0 | } |
1019 | | |
1020 | 0 | error: |
1021 | 0 | ctxt->inst = oldInst; |
1022 | 0 | return(result); |
1023 | 0 | } |
1024 | | |
1025 | | /** |
1026 | | * xsltEvalGlobalVariable: |
1027 | | * @elem: the variable or parameter |
1028 | | * @ctxt: the XSLT transformation context |
1029 | | * |
1030 | | * Evaluates a the value of a global xsl:variable or |
1031 | | * xsl:param declaration. |
1032 | | * |
1033 | | * Returns the XPath Object value or NULL in case of error |
1034 | | */ |
1035 | | static xmlXPathObjectPtr |
1036 | | xsltEvalGlobalVariable(xsltStackElemPtr elem, xsltTransformContextPtr ctxt) |
1037 | 0 | { |
1038 | 0 | xmlXPathObjectPtr result = NULL; |
1039 | 0 | xmlNodePtr oldInst; |
1040 | 0 | const xmlChar* oldVarName; |
1041 | |
|
1042 | | #ifdef XSLT_REFACTORED |
1043 | | xsltStyleBasicItemVariablePtr comp; |
1044 | | #else |
1045 | 0 | xsltStylePreCompPtr comp; |
1046 | 0 | #endif |
1047 | |
|
1048 | 0 | if ((ctxt == NULL) || (elem == NULL)) |
1049 | 0 | return(NULL); |
1050 | 0 | if (elem->computed) |
1051 | 0 | return(elem->value); |
1052 | | |
1053 | | |
1054 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
1055 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
1056 | | "Evaluating global variable %s\n", elem->name)); |
1057 | | #endif |
1058 | | |
1059 | | #ifdef WITH_DEBUGGER |
1060 | | if ((ctxt->debugStatus != XSLT_DEBUG_NONE) && |
1061 | | elem->comp && elem->comp->inst) |
1062 | | xslHandleDebugger(elem->comp->inst, NULL, NULL, ctxt); |
1063 | | #endif |
1064 | | |
1065 | 0 | oldInst = ctxt->inst; |
1066 | | #ifdef XSLT_REFACTORED |
1067 | | comp = (xsltStyleBasicItemVariablePtr) elem->comp; |
1068 | | #else |
1069 | 0 | comp = elem->comp; |
1070 | 0 | #endif |
1071 | 0 | oldVarName = elem->name; |
1072 | 0 | elem->name = xsltComputingGlobalVarMarker; |
1073 | | /* |
1074 | | * OPTIMIZE TODO: We should consider instantiating global vars/params |
1075 | | * on-demand. The vars/params don't need to be evaluated if never |
1076 | | * called; and in the case of global params, if values for such params |
1077 | | * are provided by the user. |
1078 | | */ |
1079 | 0 | if (elem->select != NULL) { |
1080 | 0 | xmlXPathCompExprPtr xpExpr = NULL; |
1081 | 0 | xmlDocPtr oldXPDoc; |
1082 | 0 | xmlNodePtr oldXPContextNode; |
1083 | 0 | int oldXPProximityPosition, oldXPContextSize, oldXPNsNr; |
1084 | 0 | xmlNsPtr *oldXPNamespaces; |
1085 | 0 | xmlXPathContextPtr xpctxt = ctxt->xpathCtxt; |
1086 | |
|
1087 | 0 | if ((comp != NULL) && (comp->comp != NULL)) { |
1088 | 0 | xpExpr = comp->comp; |
1089 | 0 | } else { |
1090 | 0 | xpExpr = xmlXPathCtxtCompile(ctxt->xpathCtxt, elem->select); |
1091 | 0 | } |
1092 | 0 | if (xpExpr == NULL) |
1093 | 0 | goto error; |
1094 | | |
1095 | | |
1096 | 0 | if (comp != NULL) |
1097 | 0 | ctxt->inst = comp->inst; |
1098 | 0 | else |
1099 | 0 | ctxt->inst = NULL; |
1100 | | /* |
1101 | | * SPEC XSLT 1.0: |
1102 | | * "At top-level, the expression or template specifying the |
1103 | | * variable value is evaluated with the same context as that used |
1104 | | * to process the root node of the source document: the current |
1105 | | * node is the root node of the source document and the current |
1106 | | * node list is a list containing just the root node of the source |
1107 | | * document." |
1108 | | */ |
1109 | | /* |
1110 | | * Save context states. |
1111 | | */ |
1112 | 0 | oldXPDoc = xpctxt->doc; |
1113 | 0 | oldXPContextNode = xpctxt->node; |
1114 | 0 | oldXPProximityPosition = xpctxt->proximityPosition; |
1115 | 0 | oldXPContextSize = xpctxt->contextSize; |
1116 | 0 | oldXPNamespaces = xpctxt->namespaces; |
1117 | 0 | oldXPNsNr = xpctxt->nsNr; |
1118 | |
|
1119 | 0 | xpctxt->node = ctxt->initialContextNode; |
1120 | 0 | xpctxt->doc = ctxt->initialContextDoc; |
1121 | 0 | xpctxt->contextSize = 1; |
1122 | 0 | xpctxt->proximityPosition = 1; |
1123 | |
|
1124 | 0 | if (comp != NULL) { |
1125 | |
|
1126 | | #ifdef XSLT_REFACTORED |
1127 | | if (comp->inScopeNs != NULL) { |
1128 | | xpctxt->namespaces = comp->inScopeNs->list; |
1129 | | xpctxt->nsNr = comp->inScopeNs->xpathNumber; |
1130 | | } else { |
1131 | | xpctxt->namespaces = NULL; |
1132 | | xpctxt->nsNr = 0; |
1133 | | } |
1134 | | #else |
1135 | 0 | xpctxt->namespaces = comp->nsList; |
1136 | 0 | xpctxt->nsNr = comp->nsNr; |
1137 | 0 | #endif |
1138 | 0 | } else { |
1139 | 0 | xpctxt->namespaces = NULL; |
1140 | 0 | xpctxt->nsNr = 0; |
1141 | 0 | } |
1142 | |
|
1143 | 0 | result = xmlXPathCompiledEval(xpExpr, xpctxt); |
1144 | | |
1145 | | /* |
1146 | | * Restore Context states. |
1147 | | */ |
1148 | 0 | xpctxt->doc = oldXPDoc; |
1149 | 0 | xpctxt->node = oldXPContextNode; |
1150 | 0 | xpctxt->contextSize = oldXPContextSize; |
1151 | 0 | xpctxt->proximityPosition = oldXPProximityPosition; |
1152 | 0 | xpctxt->namespaces = oldXPNamespaces; |
1153 | 0 | xpctxt->nsNr = oldXPNsNr; |
1154 | |
|
1155 | 0 | if ((comp == NULL) || (comp->comp == NULL)) |
1156 | 0 | xmlXPathFreeCompExpr(xpExpr); |
1157 | 0 | if (result == NULL) { |
1158 | 0 | if (comp == NULL) |
1159 | 0 | xsltTransformError(ctxt, NULL, NULL, |
1160 | 0 | "Evaluating global variable %s failed\n", elem->name); |
1161 | 0 | else |
1162 | 0 | xsltTransformError(ctxt, NULL, comp->inst, |
1163 | 0 | "Evaluating global variable %s failed\n", elem->name); |
1164 | 0 | ctxt->state = XSLT_STATE_STOPPED; |
1165 | 0 | goto error; |
1166 | 0 | } |
1167 | | |
1168 | | /* |
1169 | | * Mark all RVTs that are referenced from result as part |
1170 | | * of this variable so they won't be freed too early. |
1171 | | */ |
1172 | 0 | xsltFlagRVTs(ctxt, result, XSLT_RVT_GLOBAL); |
1173 | |
|
1174 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
1175 | | #ifdef LIBXML_DEBUG_ENABLED |
1176 | | if ((xsltGenericDebugContext == stdout) || |
1177 | | (xsltGenericDebugContext == stderr)) |
1178 | | xmlXPathDebugDumpObject((FILE *)xsltGenericDebugContext, |
1179 | | result, 0); |
1180 | | #endif |
1181 | | #endif |
1182 | 0 | } else { |
1183 | 0 | if (elem->tree == NULL) { |
1184 | 0 | result = xmlXPathNewCString(""); |
1185 | 0 | } else { |
1186 | 0 | xmlDocPtr container; |
1187 | 0 | xmlNodePtr oldInsert; |
1188 | 0 | xmlDocPtr oldOutput, oldXPDoc; |
1189 | | /* |
1190 | | * Generate a result tree fragment. |
1191 | | */ |
1192 | 0 | container = xsltCreateRVT(ctxt); |
1193 | 0 | if (container == NULL) |
1194 | 0 | goto error; |
1195 | | /* |
1196 | | * Let the lifetime of the tree fragment be handled by |
1197 | | * the Libxslt's garbage collector. |
1198 | | */ |
1199 | 0 | xsltRegisterPersistRVT(ctxt, container); |
1200 | |
|
1201 | 0 | oldOutput = ctxt->output; |
1202 | 0 | oldInsert = ctxt->insert; |
1203 | |
|
1204 | 0 | oldXPDoc = ctxt->xpathCtxt->doc; |
1205 | |
|
1206 | 0 | ctxt->output = container; |
1207 | 0 | ctxt->insert = (xmlNodePtr) container; |
1208 | |
|
1209 | 0 | ctxt->xpathCtxt->doc = ctxt->initialContextDoc; |
1210 | | /* |
1211 | | * Process the sequence constructor. |
1212 | | */ |
1213 | 0 | xsltApplyOneTemplate(ctxt, ctxt->node, elem->tree, NULL, NULL); |
1214 | |
|
1215 | 0 | ctxt->xpathCtxt->doc = oldXPDoc; |
1216 | |
|
1217 | 0 | ctxt->insert = oldInsert; |
1218 | 0 | ctxt->output = oldOutput; |
1219 | |
|
1220 | 0 | result = xmlXPathNewValueTree((xmlNodePtr) container); |
1221 | 0 | if (result == NULL) { |
1222 | 0 | result = xmlXPathNewCString(""); |
1223 | 0 | } else { |
1224 | | /* |
1225 | | * This stops older libxml2 versions from freeing the nodes |
1226 | | * in the tree. |
1227 | | */ |
1228 | 0 | result->boolval = 0; |
1229 | 0 | } |
1230 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
1231 | | #ifdef LIBXML_DEBUG_ENABLED |
1232 | | if ((xsltGenericDebugContext == stdout) || |
1233 | | (xsltGenericDebugContext == stderr)) |
1234 | | xmlXPathDebugDumpObject((FILE *)xsltGenericDebugContext, |
1235 | | result, 0); |
1236 | | #endif |
1237 | | #endif |
1238 | 0 | } |
1239 | 0 | } |
1240 | | |
1241 | 0 | error: |
1242 | 0 | elem->name = oldVarName; |
1243 | 0 | ctxt->inst = oldInst; |
1244 | 0 | if (result != NULL) { |
1245 | 0 | elem->value = result; |
1246 | 0 | elem->computed = 1; |
1247 | 0 | } |
1248 | 0 | return(result); |
1249 | 0 | } |
1250 | | |
1251 | | /** |
1252 | | * xsltEvalGlobalVariables: |
1253 | | * @ctxt: the XSLT transformation context |
1254 | | * |
1255 | | * Evaluates all global variables and parameters of a stylesheet. |
1256 | | * For internal use only. This is called at start of a transformation. |
1257 | | * |
1258 | | * Returns 0 in case of success, -1 in case of error |
1259 | | */ |
1260 | | int |
1261 | 0 | xsltEvalGlobalVariables(xsltTransformContextPtr ctxt) { |
1262 | 0 | xsltStackElemPtr elem; |
1263 | 0 | xsltStackElemPtr head = NULL; |
1264 | 0 | xsltStylesheetPtr style; |
1265 | |
|
1266 | 0 | if ((ctxt == NULL) || (ctxt->document == NULL)) |
1267 | 0 | return(-1); |
1268 | | |
1269 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
1270 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
1271 | | "Registering global variables\n")); |
1272 | | #endif |
1273 | | /* |
1274 | | * Walk the list from the stylesheets and populate the hash table |
1275 | | */ |
1276 | 0 | style = ctxt->style; |
1277 | 0 | while (style != NULL) { |
1278 | 0 | elem = style->variables; |
1279 | |
|
1280 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
1281 | | if ((style->doc != NULL) && (style->doc->URL != NULL)) { |
1282 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
1283 | | "Registering global variables from %s\n", |
1284 | | style->doc->URL)); |
1285 | | } |
1286 | | #endif |
1287 | |
|
1288 | 0 | while (elem != NULL) { |
1289 | 0 | xsltStackElemPtr def; |
1290 | | |
1291 | | /* |
1292 | | * Global variables are stored in the variables pool. |
1293 | | */ |
1294 | 0 | def = (xsltStackElemPtr) |
1295 | 0 | xmlHashLookup2(ctxt->globalVars, |
1296 | 0 | elem->name, elem->nameURI); |
1297 | 0 | if (def == NULL) { |
1298 | |
|
1299 | 0 | def = xsltCopyStackElem(elem); |
1300 | 0 | if (xmlHashAddEntry2(ctxt->globalVars, |
1301 | 0 | elem->name, elem->nameURI, def) < 0) { |
1302 | 0 | xmlGenericError(xmlGenericErrorContext, |
1303 | 0 | "hash update failed\n"); |
1304 | 0 | xsltFreeStackElem(def); |
1305 | 0 | return(-1); |
1306 | 0 | } |
1307 | 0 | def->next = head; |
1308 | 0 | head = def; |
1309 | 0 | } else if ((elem->comp != NULL) && |
1310 | 0 | (elem->comp->type == XSLT_FUNC_VARIABLE)) { |
1311 | | /* |
1312 | | * Redefinition of variables from a different stylesheet |
1313 | | * should not generate a message. |
1314 | | */ |
1315 | 0 | if ((elem->comp->inst != NULL) && |
1316 | 0 | (def->comp != NULL) && (def->comp->inst != NULL) && |
1317 | 0 | (elem->comp->inst->doc == def->comp->inst->doc)) |
1318 | 0 | { |
1319 | 0 | xsltTransformError(ctxt, style, elem->comp->inst, |
1320 | 0 | "Global variable %s already defined\n", elem->name); |
1321 | 0 | if (style != NULL) style->errors++; |
1322 | 0 | } |
1323 | 0 | } |
1324 | 0 | elem = elem->next; |
1325 | 0 | } |
1326 | | |
1327 | 0 | style = xsltNextImport(style); |
1328 | 0 | } |
1329 | | |
1330 | | /* |
1331 | | * This part does the actual evaluation. Note that scanning the hash |
1332 | | * table would result in a non-deterministic order, leading to |
1333 | | * non-deterministic generated IDs. |
1334 | | */ |
1335 | 0 | elem = head; |
1336 | 0 | while (elem != NULL) { |
1337 | 0 | xsltStackElemPtr next; |
1338 | |
|
1339 | 0 | xsltEvalGlobalVariable(elem, ctxt); |
1340 | 0 | next = elem->next; |
1341 | 0 | elem->next = NULL; |
1342 | 0 | elem = next; |
1343 | 0 | } |
1344 | |
|
1345 | 0 | return(0); |
1346 | 0 | } |
1347 | | |
1348 | | /** |
1349 | | * xsltRegisterGlobalVariable: |
1350 | | * @style: the XSLT transformation context |
1351 | | * @name: the variable name |
1352 | | * @ns_uri: the variable namespace URI |
1353 | | * @sel: the expression which need to be evaluated to generate a value |
1354 | | * @tree: the subtree if sel is NULL |
1355 | | * @comp: the precompiled value |
1356 | | * @value: the string value if available |
1357 | | * |
1358 | | * Register a new variable value. If @value is NULL it unregisters |
1359 | | * the variable |
1360 | | * |
1361 | | * Returns 0 in case of success, -1 in case of error |
1362 | | */ |
1363 | | static int |
1364 | | xsltRegisterGlobalVariable(xsltStylesheetPtr style, const xmlChar *name, |
1365 | | const xmlChar *ns_uri, const xmlChar *sel, |
1366 | | xmlNodePtr tree, xsltStylePreCompPtr comp, |
1367 | 0 | const xmlChar *value) { |
1368 | 0 | xsltStackElemPtr elem, tmp; |
1369 | 0 | if (style == NULL) |
1370 | 0 | return(-1); |
1371 | 0 | if (name == NULL) |
1372 | 0 | return(-1); |
1373 | 0 | if (comp == NULL) |
1374 | 0 | return(-1); |
1375 | | |
1376 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
1377 | | if (comp->type == XSLT_FUNC_PARAM) |
1378 | | xsltGenericDebug(xsltGenericDebugContext, |
1379 | | "Defining global param %s\n", name); |
1380 | | else |
1381 | | xsltGenericDebug(xsltGenericDebugContext, |
1382 | | "Defining global variable %s\n", name); |
1383 | | #endif |
1384 | | |
1385 | 0 | elem = xsltNewStackElem(NULL); |
1386 | 0 | if (elem == NULL) |
1387 | 0 | return(-1); |
1388 | 0 | elem->comp = comp; |
1389 | 0 | elem->name = xmlDictLookup(style->dict, name, -1); |
1390 | 0 | elem->select = xmlDictLookup(style->dict, sel, -1); |
1391 | 0 | if (ns_uri) |
1392 | 0 | elem->nameURI = xmlDictLookup(style->dict, ns_uri, -1); |
1393 | 0 | elem->tree = tree; |
1394 | 0 | tmp = style->variables; |
1395 | 0 | while (tmp != NULL) { |
1396 | 0 | if ((elem->comp->type == XSLT_FUNC_VARIABLE) && |
1397 | 0 | (tmp->comp->type == XSLT_FUNC_VARIABLE) && |
1398 | 0 | (xmlStrEqual(elem->name, tmp->name)) && |
1399 | 0 | ((elem->nameURI == tmp->nameURI) || |
1400 | 0 | (xmlStrEqual(elem->nameURI, tmp->nameURI)))) |
1401 | 0 | { |
1402 | 0 | xsltTransformError(NULL, style, comp->inst, |
1403 | 0 | "redefinition of global variable %s\n", elem->name); |
1404 | 0 | style->errors++; |
1405 | 0 | } |
1406 | 0 | tmp = tmp->next; |
1407 | 0 | } |
1408 | 0 | elem->next = style->variables;; |
1409 | 0 | style->variables = elem; |
1410 | 0 | if (value != NULL) { |
1411 | 0 | elem->computed = 1; |
1412 | 0 | elem->value = xmlXPathNewString(value); |
1413 | 0 | } |
1414 | 0 | return(0); |
1415 | 0 | } |
1416 | | |
1417 | | /** |
1418 | | * xsltProcessUserParamInternal |
1419 | | * |
1420 | | * @ctxt: the XSLT transformation context |
1421 | | * @name: a null terminated parameter name |
1422 | | * @value: a null terminated value (may be an XPath expression) |
1423 | | * @eval: 0 to treat the value literally, else evaluate as XPath expression |
1424 | | * |
1425 | | * If @eval is 0 then @value is treated literally and is stored in the global |
1426 | | * parameter/variable table without any change. |
1427 | | * |
1428 | | * Uf @eval is 1 then @value is treated as an XPath expression and is |
1429 | | * evaluated. In this case, if you want to pass a string which will be |
1430 | | * interpreted literally then it must be enclosed in single or double quotes. |
1431 | | * If the string contains single quotes (double quotes) then it cannot be |
1432 | | * enclosed single quotes (double quotes). If the string which you want to |
1433 | | * be treated literally contains both single and double quotes (e.g. Meet |
1434 | | * at Joe's for "Twelfth Night" at 7 o'clock) then there is no suitable |
1435 | | * quoting character. You cannot use ' or " inside the string |
1436 | | * because the replacement of character entities with their equivalents is |
1437 | | * done at a different stage of processing. The solution is to call |
1438 | | * xsltQuoteUserParams or xsltQuoteOneUserParam. |
1439 | | * |
1440 | | * This needs to be done on parsed stylesheets before starting to apply |
1441 | | * transformations. Normally this will be called (directly or indirectly) |
1442 | | * only from xsltEvalUserParams, xsltEvalOneUserParam, xsltQuoteUserParams, |
1443 | | * or xsltQuoteOneUserParam. |
1444 | | * |
1445 | | * Returns 0 in case of success, -1 in case of error |
1446 | | */ |
1447 | | |
1448 | | static |
1449 | | int |
1450 | | xsltProcessUserParamInternal(xsltTransformContextPtr ctxt, |
1451 | | const xmlChar * name, |
1452 | | const xmlChar * value, |
1453 | 0 | int eval) { |
1454 | |
|
1455 | 0 | xsltStylesheetPtr style; |
1456 | 0 | const xmlChar *prefix; |
1457 | 0 | const xmlChar *href; |
1458 | 0 | xmlXPathCompExprPtr xpExpr; |
1459 | 0 | xmlXPathObjectPtr result; |
1460 | |
|
1461 | 0 | xsltStackElemPtr elem; |
1462 | 0 | int res; |
1463 | 0 | void *res_ptr; |
1464 | |
|
1465 | 0 | if (ctxt == NULL) |
1466 | 0 | return(-1); |
1467 | 0 | if (name == NULL) |
1468 | 0 | return(0); |
1469 | 0 | if (value == NULL) |
1470 | 0 | return(0); |
1471 | | |
1472 | 0 | style = ctxt->style; |
1473 | |
|
1474 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
1475 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
1476 | | "Evaluating user parameter %s=%s\n", name, value)); |
1477 | | #endif |
1478 | | |
1479 | | /* |
1480 | | * Name lookup |
1481 | | */ |
1482 | 0 | href = NULL; |
1483 | |
|
1484 | 0 | if (name[0] == '{') { |
1485 | 0 | int len = 0; |
1486 | |
|
1487 | 0 | while ((name[len] != 0) && (name[len] != '}')) len++; |
1488 | 0 | if (name[len] == 0) { |
1489 | 0 | xsltTransformError(ctxt, style, NULL, |
1490 | 0 | "user param : malformed parameter name : %s\n", name); |
1491 | 0 | } else { |
1492 | 0 | href = xmlDictLookup(ctxt->dict, &name[1], len-1); |
1493 | 0 | name = xmlDictLookup(ctxt->dict, &name[len + 1], -1); |
1494 | 0 | } |
1495 | 0 | } |
1496 | 0 | else { |
1497 | 0 | name = xsltSplitQName(ctxt->dict, name, &prefix); |
1498 | 0 | if (prefix != NULL) { |
1499 | 0 | xmlNsPtr ns; |
1500 | |
|
1501 | 0 | ns = xmlSearchNs(style->doc, xmlDocGetRootElement(style->doc), |
1502 | 0 | prefix); |
1503 | 0 | if (ns == NULL) { |
1504 | 0 | xsltTransformError(ctxt, style, NULL, |
1505 | 0 | "user param : no namespace bound to prefix %s\n", prefix); |
1506 | 0 | href = NULL; |
1507 | 0 | } else { |
1508 | 0 | href = ns->href; |
1509 | 0 | } |
1510 | 0 | } |
1511 | 0 | } |
1512 | |
|
1513 | 0 | if (name == NULL) |
1514 | 0 | return (-1); |
1515 | | |
1516 | 0 | res_ptr = xmlHashLookup2(ctxt->globalVars, name, href); |
1517 | 0 | if (res_ptr != 0) { |
1518 | 0 | xsltTransformError(ctxt, style, NULL, |
1519 | 0 | "Global parameter %s already defined\n", name); |
1520 | 0 | } |
1521 | 0 | if (ctxt->globalVars == NULL) |
1522 | 0 | ctxt->globalVars = xmlHashCreate(20); |
1523 | | |
1524 | | /* |
1525 | | * do not overwrite variables with parameters from the command line |
1526 | | */ |
1527 | 0 | while (style != NULL) { |
1528 | 0 | elem = ctxt->style->variables; |
1529 | 0 | while (elem != NULL) { |
1530 | 0 | if ((elem->comp != NULL) && |
1531 | 0 | (elem->comp->type == XSLT_FUNC_VARIABLE) && |
1532 | 0 | (xmlStrEqual(elem->name, name)) && |
1533 | 0 | (xmlStrEqual(elem->nameURI, href))) { |
1534 | 0 | return(0); |
1535 | 0 | } |
1536 | 0 | elem = elem->next; |
1537 | 0 | } |
1538 | 0 | style = xsltNextImport(style); |
1539 | 0 | } |
1540 | 0 | style = ctxt->style; |
1541 | 0 | elem = NULL; |
1542 | | |
1543 | | /* |
1544 | | * Do the evaluation if @eval is non-zero. |
1545 | | */ |
1546 | |
|
1547 | 0 | result = NULL; |
1548 | 0 | if (eval != 0) { |
1549 | 0 | xpExpr = xmlXPathCtxtCompile(ctxt->xpathCtxt, value); |
1550 | 0 | if (xpExpr != NULL) { |
1551 | 0 | xmlDocPtr oldXPDoc; |
1552 | 0 | xmlNodePtr oldXPContextNode; |
1553 | 0 | int oldXPProximityPosition, oldXPContextSize, oldXPNsNr; |
1554 | 0 | xmlNsPtr *oldXPNamespaces; |
1555 | 0 | xmlXPathContextPtr xpctxt = ctxt->xpathCtxt; |
1556 | | |
1557 | | /* |
1558 | | * Save context states. |
1559 | | */ |
1560 | 0 | oldXPDoc = xpctxt->doc; |
1561 | 0 | oldXPContextNode = xpctxt->node; |
1562 | 0 | oldXPProximityPosition = xpctxt->proximityPosition; |
1563 | 0 | oldXPContextSize = xpctxt->contextSize; |
1564 | 0 | oldXPNamespaces = xpctxt->namespaces; |
1565 | 0 | oldXPNsNr = xpctxt->nsNr; |
1566 | | |
1567 | | /* |
1568 | | * SPEC XSLT 1.0: |
1569 | | * "At top-level, the expression or template specifying the |
1570 | | * variable value is evaluated with the same context as that used |
1571 | | * to process the root node of the source document: the current |
1572 | | * node is the root node of the source document and the current |
1573 | | * node list is a list containing just the root node of the source |
1574 | | * document." |
1575 | | */ |
1576 | 0 | xpctxt->doc = ctxt->initialContextDoc; |
1577 | 0 | xpctxt->node = ctxt->initialContextNode; |
1578 | 0 | xpctxt->contextSize = 1; |
1579 | 0 | xpctxt->proximityPosition = 1; |
1580 | | /* |
1581 | | * There is really no in scope namespace for parameters on the |
1582 | | * command line. |
1583 | | */ |
1584 | 0 | xpctxt->namespaces = NULL; |
1585 | 0 | xpctxt->nsNr = 0; |
1586 | |
|
1587 | 0 | result = xmlXPathCompiledEval(xpExpr, xpctxt); |
1588 | | |
1589 | | /* |
1590 | | * Restore Context states. |
1591 | | */ |
1592 | 0 | xpctxt->doc = oldXPDoc; |
1593 | 0 | xpctxt->node = oldXPContextNode; |
1594 | 0 | xpctxt->contextSize = oldXPContextSize; |
1595 | 0 | xpctxt->proximityPosition = oldXPProximityPosition; |
1596 | 0 | xpctxt->namespaces = oldXPNamespaces; |
1597 | 0 | xpctxt->nsNr = oldXPNsNr; |
1598 | |
|
1599 | 0 | xmlXPathFreeCompExpr(xpExpr); |
1600 | 0 | } |
1601 | 0 | if (result == NULL) { |
1602 | 0 | xsltTransformError(ctxt, style, NULL, |
1603 | 0 | "Evaluating user parameter %s failed\n", name); |
1604 | 0 | ctxt->state = XSLT_STATE_STOPPED; |
1605 | 0 | return(-1); |
1606 | 0 | } |
1607 | 0 | } |
1608 | | |
1609 | | /* |
1610 | | * If @eval is 0 then @value is to be taken literally and result is NULL |
1611 | | * |
1612 | | * If @eval is not 0, then @value is an XPath expression and has been |
1613 | | * successfully evaluated and result contains the resulting value and |
1614 | | * is not NULL. |
1615 | | * |
1616 | | * Now create an xsltStackElemPtr for insertion into the context's |
1617 | | * global variable/parameter hash table. |
1618 | | */ |
1619 | | |
1620 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
1621 | | #ifdef LIBXML_DEBUG_ENABLED |
1622 | | if ((xsltGenericDebugContext == stdout) || |
1623 | | (xsltGenericDebugContext == stderr)) |
1624 | | xmlXPathDebugDumpObject((FILE *)xsltGenericDebugContext, |
1625 | | result, 0); |
1626 | | #endif |
1627 | | #endif |
1628 | | |
1629 | 0 | elem = xsltNewStackElem(NULL); |
1630 | 0 | if (elem != NULL) { |
1631 | 0 | elem->name = name; |
1632 | 0 | elem->select = xmlDictLookup(ctxt->dict, value, -1); |
1633 | 0 | if (href != NULL) |
1634 | 0 | elem->nameURI = xmlDictLookup(ctxt->dict, href, -1); |
1635 | 0 | elem->tree = NULL; |
1636 | 0 | elem->computed = 1; |
1637 | 0 | if (eval == 0) { |
1638 | 0 | elem->value = xmlXPathNewString(value); |
1639 | 0 | } |
1640 | 0 | else { |
1641 | 0 | elem->value = result; |
1642 | 0 | } |
1643 | 0 | } |
1644 | | |
1645 | | /* |
1646 | | * Global parameters are stored in the XPath context variables pool. |
1647 | | */ |
1648 | |
|
1649 | 0 | res = xmlHashAddEntry2(ctxt->globalVars, name, href, elem); |
1650 | 0 | if (res != 0) { |
1651 | 0 | xsltFreeStackElem(elem); |
1652 | 0 | xsltTransformError(ctxt, style, NULL, |
1653 | 0 | "Global parameter %s already defined\n", name); |
1654 | 0 | } |
1655 | 0 | return(0); |
1656 | 0 | } |
1657 | | |
1658 | | /** |
1659 | | * xsltEvalUserParams: |
1660 | | * |
1661 | | * @ctxt: the XSLT transformation context |
1662 | | * @params: a NULL terminated array of parameters name/value tuples |
1663 | | * |
1664 | | * Evaluate the global variables of a stylesheet. This needs to be |
1665 | | * done on parsed stylesheets before starting to apply transformations. |
1666 | | * Each of the parameters is evaluated as an XPath expression and stored |
1667 | | * in the global variables/parameter hash table. If you want your |
1668 | | * parameter used literally, use xsltQuoteUserParams. |
1669 | | * |
1670 | | * Returns 0 in case of success, -1 in case of error |
1671 | | */ |
1672 | | |
1673 | | int |
1674 | 0 | xsltEvalUserParams(xsltTransformContextPtr ctxt, const char **params) { |
1675 | 0 | size_t indx = 0; |
1676 | 0 | const xmlChar *name; |
1677 | 0 | const xmlChar *value; |
1678 | |
|
1679 | 0 | if (params == NULL) |
1680 | 0 | return(0); |
1681 | 0 | while (params[indx] != NULL) { |
1682 | 0 | name = (const xmlChar *) params[indx++]; |
1683 | 0 | value = (const xmlChar *) params[indx++]; |
1684 | 0 | if (xsltEvalOneUserParam(ctxt, name, value) != 0) |
1685 | 0 | return(-1); |
1686 | 0 | } |
1687 | 0 | return 0; |
1688 | 0 | } |
1689 | | |
1690 | | /** |
1691 | | * xsltQuoteUserParams: |
1692 | | * |
1693 | | * @ctxt: the XSLT transformation context |
1694 | | * @params: a NULL terminated arry of parameters names/values tuples |
1695 | | * |
1696 | | * Similar to xsltEvalUserParams, but the values are treated literally and |
1697 | | * are * *not* evaluated as XPath expressions. This should be done on parsed |
1698 | | * stylesheets before starting to apply transformations. |
1699 | | * |
1700 | | * Returns 0 in case of success, -1 in case of error. |
1701 | | */ |
1702 | | |
1703 | | int |
1704 | 0 | xsltQuoteUserParams(xsltTransformContextPtr ctxt, const char **params) { |
1705 | 0 | size_t indx = 0; |
1706 | 0 | const xmlChar *name; |
1707 | 0 | const xmlChar *value; |
1708 | |
|
1709 | 0 | if (params == NULL) |
1710 | 0 | return(0); |
1711 | 0 | while (params[indx] != NULL) { |
1712 | 0 | name = (const xmlChar *) params[indx++]; |
1713 | 0 | value = (const xmlChar *) params[indx++]; |
1714 | 0 | if (xsltQuoteOneUserParam(ctxt, name, value) != 0) |
1715 | 0 | return(-1); |
1716 | 0 | } |
1717 | 0 | return 0; |
1718 | 0 | } |
1719 | | |
1720 | | /** |
1721 | | * xsltEvalOneUserParam: |
1722 | | * @ctxt: the XSLT transformation context |
1723 | | * @name: a null terminated string giving the name of the parameter |
1724 | | * @value: a null terminated string giving the XPath expression to be evaluated |
1725 | | * |
1726 | | * This is normally called from xsltEvalUserParams to process a single |
1727 | | * parameter from a list of parameters. The @value is evaluated as an |
1728 | | * XPath expression and the result is stored in the context's global |
1729 | | * variable/parameter hash table. |
1730 | | * |
1731 | | * To have a parameter treated literally (not as an XPath expression) |
1732 | | * use xsltQuoteUserParams (or xsltQuoteOneUserParam). For more |
1733 | | * details see description of xsltProcessOneUserParamInternal. |
1734 | | * |
1735 | | * Returns 0 in case of success, -1 in case of error. |
1736 | | */ |
1737 | | |
1738 | | int |
1739 | | xsltEvalOneUserParam(xsltTransformContextPtr ctxt, |
1740 | | const xmlChar * name, |
1741 | 0 | const xmlChar * value) { |
1742 | 0 | return xsltProcessUserParamInternal(ctxt, name, value, |
1743 | 0 | 1 /* xpath eval ? */); |
1744 | 0 | } |
1745 | | |
1746 | | /** |
1747 | | * xsltQuoteOneUserParam: |
1748 | | * @ctxt: the XSLT transformation context |
1749 | | * @name: a null terminated string giving the name of the parameter |
1750 | | * @value: a null terminated string giving the parameter value |
1751 | | * |
1752 | | * This is normally called from xsltQuoteUserParams to process a single |
1753 | | * parameter from a list of parameters. The @value is stored in the |
1754 | | * context's global variable/parameter hash table. |
1755 | | * |
1756 | | * Returns 0 in case of success, -1 in case of error. |
1757 | | */ |
1758 | | |
1759 | | int |
1760 | | xsltQuoteOneUserParam(xsltTransformContextPtr ctxt, |
1761 | | const xmlChar * name, |
1762 | 0 | const xmlChar * value) { |
1763 | 0 | return xsltProcessUserParamInternal(ctxt, name, value, |
1764 | 0 | 0 /* xpath eval ? */); |
1765 | 0 | } |
1766 | | |
1767 | | /** |
1768 | | * xsltBuildVariable: |
1769 | | * @ctxt: the XSLT transformation context |
1770 | | * @comp: the precompiled form |
1771 | | * @tree: the tree if select is NULL |
1772 | | * |
1773 | | * Computes a new variable value. |
1774 | | * |
1775 | | * Returns the xsltStackElemPtr or NULL in case of error |
1776 | | */ |
1777 | | static xsltStackElemPtr |
1778 | | xsltBuildVariable(xsltTransformContextPtr ctxt, |
1779 | | xsltStylePreCompPtr castedComp, |
1780 | | xmlNodePtr tree) |
1781 | 0 | { |
1782 | | #ifdef XSLT_REFACTORED |
1783 | | xsltStyleBasicItemVariablePtr comp = |
1784 | | (xsltStyleBasicItemVariablePtr) castedComp; |
1785 | | #else |
1786 | 0 | xsltStylePreCompPtr comp = castedComp; |
1787 | 0 | #endif |
1788 | 0 | xsltStackElemPtr elem; |
1789 | |
|
1790 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
1791 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
1792 | | "Building variable %s", comp->name)); |
1793 | | if (comp->select != NULL) |
1794 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
1795 | | " select %s", comp->select)); |
1796 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, "\n")); |
1797 | | #endif |
1798 | |
|
1799 | 0 | elem = xsltNewStackElem(ctxt); |
1800 | 0 | if (elem == NULL) |
1801 | 0 | return(NULL); |
1802 | 0 | elem->comp = (xsltStylePreCompPtr) comp; |
1803 | 0 | elem->name = comp->name; |
1804 | 0 | elem->select = comp->select; |
1805 | 0 | elem->nameURI = comp->ns; |
1806 | 0 | elem->tree = tree; |
1807 | 0 | elem->value = xsltEvalVariable(ctxt, elem, |
1808 | 0 | (xsltStylePreCompPtr) comp); |
1809 | 0 | elem->computed = 1; |
1810 | 0 | return(elem); |
1811 | 0 | } |
1812 | | |
1813 | | /** |
1814 | | * xsltRegisterVariable: |
1815 | | * @ctxt: the XSLT transformation context |
1816 | | * @comp: the compiled XSLT-variable (or param) instruction |
1817 | | * @tree: the tree if select is NULL |
1818 | | * @isParam: indicates if this is a parameter |
1819 | | * |
1820 | | * Computes and registers a new variable. |
1821 | | * |
1822 | | * Returns 0 in case of success, -1 in case of error |
1823 | | */ |
1824 | | static int |
1825 | | xsltRegisterVariable(xsltTransformContextPtr ctxt, |
1826 | | xsltStylePreCompPtr castedComp, |
1827 | | xmlNodePtr tree, int isParam) |
1828 | 0 | { |
1829 | | #ifdef XSLT_REFACTORED |
1830 | | xsltStyleBasicItemVariablePtr comp = |
1831 | | (xsltStyleBasicItemVariablePtr) castedComp; |
1832 | | #else |
1833 | 0 | xsltStylePreCompPtr comp = castedComp; |
1834 | 0 | int present; |
1835 | 0 | #endif |
1836 | 0 | xsltStackElemPtr variable; |
1837 | |
|
1838 | | #ifdef XSLT_REFACTORED |
1839 | | /* |
1840 | | * REFACTORED NOTE: Redefinitions of vars/params are checked |
1841 | | * at compilation time in the refactored code. |
1842 | | * xsl:with-param parameters are checked in xsltApplyXSLTTemplate(). |
1843 | | */ |
1844 | | #else |
1845 | 0 | present = xsltCheckStackElem(ctxt, comp->name, comp->ns); |
1846 | 0 | if (isParam == 0) { |
1847 | 0 | if ((present != 0) && (present != 3)) { |
1848 | | /* TODO: report QName. */ |
1849 | 0 | xsltTransformError(ctxt, NULL, comp->inst, |
1850 | 0 | "XSLT-variable: Redefinition of variable '%s'.\n", comp->name); |
1851 | 0 | return(0); |
1852 | 0 | } |
1853 | 0 | } else if (present != 0) { |
1854 | 0 | if ((present == 1) || (present == 2)) { |
1855 | | /* TODO: report QName. */ |
1856 | 0 | xsltTransformError(ctxt, NULL, comp->inst, |
1857 | 0 | "XSLT-param: Redefinition of parameter '%s'.\n", comp->name); |
1858 | 0 | return(0); |
1859 | 0 | } |
1860 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
1861 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
1862 | | "param %s defined by caller\n", comp->name)); |
1863 | | #endif |
1864 | 0 | return(0); |
1865 | 0 | } |
1866 | 0 | #endif /* else of XSLT_REFACTORED */ |
1867 | | |
1868 | 0 | variable = xsltBuildVariable(ctxt, (xsltStylePreCompPtr) comp, tree); |
1869 | 0 | if (xsltAddStackElem(ctxt, variable) < 0) { |
1870 | 0 | xsltFreeStackElem(variable); |
1871 | 0 | return(-1); |
1872 | 0 | } |
1873 | 0 | return(0); |
1874 | 0 | } |
1875 | | |
1876 | | /** |
1877 | | * xsltGlobalVariableLookup: |
1878 | | * @ctxt: the XSLT transformation context |
1879 | | * @name: the variable name |
1880 | | * @ns_uri: the variable namespace URI |
1881 | | * |
1882 | | * Search in the Variable array of the context for the given |
1883 | | * variable value. |
1884 | | * |
1885 | | * Returns the value or NULL if not found |
1886 | | */ |
1887 | | static xmlXPathObjectPtr |
1888 | | xsltGlobalVariableLookup(xsltTransformContextPtr ctxt, const xmlChar *name, |
1889 | 0 | const xmlChar *ns_uri) { |
1890 | 0 | xsltStackElemPtr elem; |
1891 | 0 | xmlXPathObjectPtr ret = NULL; |
1892 | | |
1893 | | /* |
1894 | | * Lookup the global variables in XPath global variable hash table |
1895 | | */ |
1896 | 0 | if ((ctxt->xpathCtxt == NULL) || (ctxt->globalVars == NULL)) |
1897 | 0 | return(NULL); |
1898 | 0 | elem = (xsltStackElemPtr) |
1899 | 0 | xmlHashLookup2(ctxt->globalVars, name, ns_uri); |
1900 | 0 | if (elem == NULL) { |
1901 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
1902 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
1903 | | "global variable not found %s\n", name)); |
1904 | | #endif |
1905 | 0 | return(NULL); |
1906 | 0 | } |
1907 | | /* |
1908 | | * URGENT TODO: Move the detection of recursive definitions |
1909 | | * to compile-time. |
1910 | | */ |
1911 | 0 | if (elem->computed == 0) { |
1912 | 0 | if (elem->name == xsltComputingGlobalVarMarker) { |
1913 | 0 | xsltTransformError(ctxt, NULL, elem->comp->inst, |
1914 | 0 | "Recursive definition of %s\n", name); |
1915 | 0 | return(NULL); |
1916 | 0 | } |
1917 | 0 | ret = xsltEvalGlobalVariable(elem, ctxt); |
1918 | 0 | } else |
1919 | 0 | ret = elem->value; |
1920 | 0 | return(xmlXPathObjectCopy(ret)); |
1921 | 0 | } |
1922 | | |
1923 | | /** |
1924 | | * xsltVariableLookup: |
1925 | | * @ctxt: the XSLT transformation context |
1926 | | * @name: the variable name |
1927 | | * @ns_uri: the variable namespace URI |
1928 | | * |
1929 | | * Search in the Variable array of the context for the given |
1930 | | * variable value. |
1931 | | * |
1932 | | * Returns the value or NULL if not found |
1933 | | */ |
1934 | | xmlXPathObjectPtr |
1935 | | xsltVariableLookup(xsltTransformContextPtr ctxt, const xmlChar *name, |
1936 | 0 | const xmlChar *ns_uri) { |
1937 | 0 | xsltStackElemPtr elem; |
1938 | |
|
1939 | 0 | if (ctxt == NULL) |
1940 | 0 | return(NULL); |
1941 | | |
1942 | 0 | elem = xsltStackLookup(ctxt, name, ns_uri); |
1943 | 0 | if (elem == NULL) { |
1944 | 0 | return(xsltGlobalVariableLookup(ctxt, name, ns_uri)); |
1945 | 0 | } |
1946 | 0 | if (elem->computed == 0) { |
1947 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
1948 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
1949 | | "uncomputed variable %s\n", name)); |
1950 | | #endif |
1951 | 0 | elem->value = xsltEvalVariable(ctxt, elem, NULL); |
1952 | 0 | elem->computed = 1; |
1953 | 0 | } |
1954 | 0 | if (elem->value != NULL) |
1955 | 0 | return(xmlXPathObjectCopy(elem->value)); |
1956 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
1957 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
1958 | | "variable not found %s\n", name)); |
1959 | | #endif |
1960 | 0 | return(NULL); |
1961 | 0 | } |
1962 | | |
1963 | | /** |
1964 | | * xsltParseStylesheetCallerParam: |
1965 | | * @ctxt: the XSLT transformation context |
1966 | | * @inst: the xsl:with-param instruction element |
1967 | | * |
1968 | | * Processes an xsl:with-param instruction at transformation time. |
1969 | | * The value is computed, but not recorded. |
1970 | | * NOTE that this is also called with an *xsl:param* element |
1971 | | * from exsltFuncFunctionFunction(). |
1972 | | * |
1973 | | * Returns the new xsltStackElemPtr or NULL |
1974 | | */ |
1975 | | |
1976 | | xsltStackElemPtr |
1977 | | xsltParseStylesheetCallerParam(xsltTransformContextPtr ctxt, xmlNodePtr inst) |
1978 | 0 | { |
1979 | | #ifdef XSLT_REFACTORED |
1980 | | xsltStyleBasicItemVariablePtr comp; |
1981 | | #else |
1982 | 0 | xsltStylePreCompPtr comp; |
1983 | 0 | #endif |
1984 | 0 | xmlNodePtr tree = NULL; /* The first child node of the instruction or |
1985 | | the instruction itself. */ |
1986 | 0 | xsltStackElemPtr param = NULL; |
1987 | |
|
1988 | 0 | if ((ctxt == NULL) || (inst == NULL) || (inst->type != XML_ELEMENT_NODE)) |
1989 | 0 | return(NULL); |
1990 | | |
1991 | | #ifdef XSLT_REFACTORED |
1992 | | comp = (xsltStyleBasicItemVariablePtr) inst->psvi; |
1993 | | #else |
1994 | 0 | comp = (xsltStylePreCompPtr) inst->psvi; |
1995 | 0 | #endif |
1996 | |
|
1997 | 0 | if (comp == NULL) { |
1998 | 0 | xsltTransformError(ctxt, NULL, inst, |
1999 | 0 | "Internal error in xsltParseStylesheetCallerParam(): " |
2000 | 0 | "The XSLT 'with-param' instruction was not compiled.\n"); |
2001 | 0 | return(NULL); |
2002 | 0 | } |
2003 | 0 | if (comp->name == NULL) { |
2004 | 0 | xsltTransformError(ctxt, NULL, inst, |
2005 | 0 | "Internal error in xsltParseStylesheetCallerParam(): " |
2006 | 0 | "XSLT 'with-param': The attribute 'name' was not compiled.\n"); |
2007 | 0 | return(NULL); |
2008 | 0 | } |
2009 | | |
2010 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
2011 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
2012 | | "Handling xsl:with-param %s\n", comp->name)); |
2013 | | #endif |
2014 | | |
2015 | 0 | if (comp->select == NULL) { |
2016 | 0 | tree = inst->children; |
2017 | 0 | } else { |
2018 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
2019 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
2020 | | " select %s\n", comp->select)); |
2021 | | #endif |
2022 | 0 | tree = inst; |
2023 | 0 | } |
2024 | |
|
2025 | 0 | param = xsltBuildVariable(ctxt, (xsltStylePreCompPtr) comp, tree); |
2026 | |
|
2027 | 0 | return(param); |
2028 | 0 | } |
2029 | | |
2030 | | /** |
2031 | | * xsltParseGlobalVariable: |
2032 | | * @style: the XSLT stylesheet |
2033 | | * @cur: the "variable" element |
2034 | | * |
2035 | | * Parses a global XSLT 'variable' declaration at compilation time |
2036 | | * and registers it |
2037 | | */ |
2038 | | void |
2039 | | xsltParseGlobalVariable(xsltStylesheetPtr style, xmlNodePtr cur) |
2040 | 0 | { |
2041 | | #ifdef XSLT_REFACTORED |
2042 | | xsltStyleItemVariablePtr comp; |
2043 | | #else |
2044 | 0 | xsltStylePreCompPtr comp; |
2045 | 0 | #endif |
2046 | |
|
2047 | 0 | if ((cur == NULL) || (style == NULL) || (cur->type != XML_ELEMENT_NODE)) |
2048 | 0 | return; |
2049 | | |
2050 | | #ifdef XSLT_REFACTORED |
2051 | | /* |
2052 | | * Note that xsltStylePreCompute() will be called from |
2053 | | * xslt.c only. |
2054 | | */ |
2055 | | comp = (xsltStyleItemVariablePtr) cur->psvi; |
2056 | | #else |
2057 | 0 | xsltStylePreCompute(style, cur); |
2058 | 0 | comp = (xsltStylePreCompPtr) cur->psvi; |
2059 | 0 | #endif |
2060 | 0 | if (comp == NULL) { |
2061 | 0 | xsltTransformError(NULL, style, cur, |
2062 | 0 | "xsl:variable : compilation failed\n"); |
2063 | 0 | return; |
2064 | 0 | } |
2065 | | |
2066 | 0 | if (comp->name == NULL) { |
2067 | 0 | xsltTransformError(NULL, style, cur, |
2068 | 0 | "xsl:variable : missing name attribute\n"); |
2069 | 0 | return; |
2070 | 0 | } |
2071 | | |
2072 | | /* |
2073 | | * Parse the content (a sequence constructor) of xsl:variable. |
2074 | | */ |
2075 | 0 | if (cur->children != NULL) { |
2076 | | #ifdef XSLT_REFACTORED |
2077 | | xsltParseSequenceConstructor(XSLT_CCTXT(style), cur->children); |
2078 | | #else |
2079 | 0 | xsltParseTemplateContent(style, cur); |
2080 | 0 | #endif |
2081 | 0 | } |
2082 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
2083 | | xsltGenericDebug(xsltGenericDebugContext, |
2084 | | "Registering global variable %s\n", comp->name); |
2085 | | #endif |
2086 | |
|
2087 | 0 | xsltRegisterGlobalVariable(style, comp->name, comp->ns, |
2088 | 0 | comp->select, cur->children, (xsltStylePreCompPtr) comp, |
2089 | 0 | NULL); |
2090 | 0 | } |
2091 | | |
2092 | | /** |
2093 | | * xsltParseGlobalParam: |
2094 | | * @style: the XSLT stylesheet |
2095 | | * @cur: the "param" element |
2096 | | * |
2097 | | * parse an XSLT transformation param declaration and record |
2098 | | * its value. |
2099 | | */ |
2100 | | |
2101 | | void |
2102 | 0 | xsltParseGlobalParam(xsltStylesheetPtr style, xmlNodePtr cur) { |
2103 | | #ifdef XSLT_REFACTORED |
2104 | | xsltStyleItemParamPtr comp; |
2105 | | #else |
2106 | 0 | xsltStylePreCompPtr comp; |
2107 | 0 | #endif |
2108 | |
|
2109 | 0 | if ((cur == NULL) || (style == NULL) || (cur->type != XML_ELEMENT_NODE)) |
2110 | 0 | return; |
2111 | | |
2112 | | #ifdef XSLT_REFACTORED |
2113 | | /* |
2114 | | * Note that xsltStylePreCompute() will be called from |
2115 | | * xslt.c only. |
2116 | | */ |
2117 | | comp = (xsltStyleItemParamPtr) cur->psvi; |
2118 | | #else |
2119 | 0 | xsltStylePreCompute(style, cur); |
2120 | 0 | comp = (xsltStylePreCompPtr) cur->psvi; |
2121 | 0 | #endif |
2122 | 0 | if (comp == NULL) { |
2123 | 0 | xsltTransformError(NULL, style, cur, |
2124 | 0 | "xsl:param : compilation failed\n"); |
2125 | 0 | return; |
2126 | 0 | } |
2127 | | |
2128 | 0 | if (comp->name == NULL) { |
2129 | 0 | xsltTransformError(NULL, style, cur, |
2130 | 0 | "xsl:param : missing name attribute\n"); |
2131 | 0 | return; |
2132 | 0 | } |
2133 | | |
2134 | | /* |
2135 | | * Parse the content (a sequence constructor) of xsl:param. |
2136 | | */ |
2137 | 0 | if (cur->children != NULL) { |
2138 | | #ifdef XSLT_REFACTORED |
2139 | | xsltParseSequenceConstructor(XSLT_CCTXT(style), cur->children); |
2140 | | #else |
2141 | 0 | xsltParseTemplateContent(style, cur); |
2142 | 0 | #endif |
2143 | 0 | } |
2144 | |
|
2145 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
2146 | | xsltGenericDebug(xsltGenericDebugContext, |
2147 | | "Registering global param %s\n", comp->name); |
2148 | | #endif |
2149 | |
|
2150 | 0 | xsltRegisterGlobalVariable(style, comp->name, comp->ns, |
2151 | 0 | comp->select, cur->children, (xsltStylePreCompPtr) comp, |
2152 | 0 | NULL); |
2153 | 0 | } |
2154 | | |
2155 | | /** |
2156 | | * xsltParseStylesheetVariable: |
2157 | | * @ctxt: the XSLT transformation context |
2158 | | * @inst: the xsl:variable instruction element |
2159 | | * |
2160 | | * Registers a local XSLT 'variable' instruction at transformation time |
2161 | | * and evaluates its value. |
2162 | | */ |
2163 | | void |
2164 | | xsltParseStylesheetVariable(xsltTransformContextPtr ctxt, xmlNodePtr inst) |
2165 | 0 | { |
2166 | | #ifdef XSLT_REFACTORED |
2167 | | xsltStyleItemVariablePtr comp; |
2168 | | #else |
2169 | 0 | xsltStylePreCompPtr comp; |
2170 | 0 | #endif |
2171 | |
|
2172 | 0 | if ((inst == NULL) || (ctxt == NULL) || (inst->type != XML_ELEMENT_NODE)) |
2173 | 0 | return; |
2174 | | |
2175 | 0 | comp = inst->psvi; |
2176 | 0 | if (comp == NULL) { |
2177 | 0 | xsltTransformError(ctxt, NULL, inst, |
2178 | 0 | "Internal error in xsltParseStylesheetVariable(): " |
2179 | 0 | "The XSLT 'variable' instruction was not compiled.\n"); |
2180 | 0 | return; |
2181 | 0 | } |
2182 | 0 | if (comp->name == NULL) { |
2183 | 0 | xsltTransformError(ctxt, NULL, inst, |
2184 | 0 | "Internal error in xsltParseStylesheetVariable(): " |
2185 | 0 | "The attribute 'name' was not compiled.\n"); |
2186 | 0 | return; |
2187 | 0 | } |
2188 | | |
2189 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
2190 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
2191 | | "Registering variable '%s'\n", comp->name)); |
2192 | | #endif |
2193 | | |
2194 | 0 | xsltRegisterVariable(ctxt, (xsltStylePreCompPtr) comp, inst->children, 0); |
2195 | 0 | } |
2196 | | |
2197 | | /** |
2198 | | * xsltParseStylesheetParam: |
2199 | | * @ctxt: the XSLT transformation context |
2200 | | * @cur: the XSLT 'param' element |
2201 | | * |
2202 | | * Registers a local XSLT 'param' declaration at transformation time and |
2203 | | * evaluates its value. |
2204 | | */ |
2205 | | void |
2206 | | xsltParseStylesheetParam(xsltTransformContextPtr ctxt, xmlNodePtr cur) |
2207 | 0 | { |
2208 | | #ifdef XSLT_REFACTORED |
2209 | | xsltStyleItemParamPtr comp; |
2210 | | #else |
2211 | 0 | xsltStylePreCompPtr comp; |
2212 | 0 | #endif |
2213 | |
|
2214 | 0 | if ((cur == NULL) || (ctxt == NULL) || (cur->type != XML_ELEMENT_NODE)) |
2215 | 0 | return; |
2216 | | |
2217 | 0 | comp = cur->psvi; |
2218 | 0 | if ((comp == NULL) || (comp->name == NULL)) { |
2219 | 0 | xsltTransformError(ctxt, NULL, cur, |
2220 | 0 | "Internal error in xsltParseStylesheetParam(): " |
2221 | 0 | "The XSLT 'param' declaration was not compiled correctly.\n"); |
2222 | 0 | return; |
2223 | 0 | } |
2224 | | |
2225 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
2226 | | XSLT_TRACE(ctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
2227 | | "Registering param %s\n", comp->name)); |
2228 | | #endif |
2229 | | |
2230 | 0 | xsltRegisterVariable(ctxt, (xsltStylePreCompPtr) comp, cur->children, 1); |
2231 | 0 | } |
2232 | | |
2233 | | /** |
2234 | | * xsltFreeGlobalVariables: |
2235 | | * @ctxt: the XSLT transformation context |
2236 | | * |
2237 | | * Free up the data associated to the global variables |
2238 | | * its value. |
2239 | | */ |
2240 | | |
2241 | | void |
2242 | 0 | xsltFreeGlobalVariables(xsltTransformContextPtr ctxt) { |
2243 | 0 | xmlHashFree(ctxt->globalVars, xsltFreeStackElemEntry); |
2244 | 0 | } |
2245 | | |
2246 | | /** |
2247 | | * xsltXPathVariableLookup: |
2248 | | * @ctxt: a void * but the the XSLT transformation context actually |
2249 | | * @name: the variable name |
2250 | | * @ns_uri: the variable namespace URI |
2251 | | * |
2252 | | * This is the entry point when a varibale is needed by the XPath |
2253 | | * interpretor. |
2254 | | * |
2255 | | * Returns the value or NULL if not found |
2256 | | */ |
2257 | | xmlXPathObjectPtr |
2258 | | xsltXPathVariableLookup(void *ctxt, const xmlChar *name, |
2259 | 0 | const xmlChar *ns_uri) { |
2260 | 0 | xsltTransformContextPtr tctxt; |
2261 | 0 | xmlXPathObjectPtr valueObj = NULL; |
2262 | |
|
2263 | 0 | if ((ctxt == NULL) || (name == NULL)) |
2264 | 0 | return(NULL); |
2265 | | |
2266 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
2267 | | XSLT_TRACE(((xsltTransformContextPtr)ctxt),XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
2268 | | "Lookup variable '%s'\n", name)); |
2269 | | #endif |
2270 | | |
2271 | 0 | tctxt = (xsltTransformContextPtr) ctxt; |
2272 | | /* |
2273 | | * Local variables/params --------------------------------------------- |
2274 | | * |
2275 | | * Do the lookup from the top of the stack, but |
2276 | | * don't use params being computed in a call-param |
2277 | | * First lookup expects the variable name and URI to |
2278 | | * come from the disctionnary and hence pointer comparison. |
2279 | | */ |
2280 | 0 | if (tctxt->varsNr != 0) { |
2281 | 0 | int i; |
2282 | 0 | xsltStackElemPtr variable = NULL, cur; |
2283 | |
|
2284 | 0 | for (i = tctxt->varsNr; i > tctxt->varsBase; i--) { |
2285 | 0 | cur = tctxt->varsTab[i-1]; |
2286 | 0 | if ((cur->name == name) && (cur->nameURI == ns_uri)) { |
2287 | 0 | variable = cur; |
2288 | 0 | goto local_variable_found; |
2289 | 0 | } |
2290 | 0 | cur = cur->next; |
2291 | 0 | } |
2292 | | /* |
2293 | | * Redo the lookup with interned strings to avoid string comparison. |
2294 | | * |
2295 | | * OPTIMIZE TODO: The problem here is, that if we request a |
2296 | | * global variable, then this will be also executed. |
2297 | | */ |
2298 | 0 | { |
2299 | 0 | const xmlChar *tmpName = name, *tmpNsName = ns_uri; |
2300 | |
|
2301 | 0 | name = xmlDictLookup(tctxt->dict, name, -1); |
2302 | 0 | if (ns_uri) |
2303 | 0 | ns_uri = xmlDictLookup(tctxt->dict, ns_uri, -1); |
2304 | 0 | if ((tmpName != name) || (tmpNsName != ns_uri)) { |
2305 | 0 | for (i = tctxt->varsNr; i > tctxt->varsBase; i--) { |
2306 | 0 | cur = tctxt->varsTab[i-1]; |
2307 | 0 | if ((cur->name == name) && (cur->nameURI == ns_uri)) { |
2308 | 0 | variable = cur; |
2309 | 0 | goto local_variable_found; |
2310 | 0 | } |
2311 | 0 | } |
2312 | 0 | } |
2313 | 0 | } |
2314 | | |
2315 | 0 | local_variable_found: |
2316 | |
|
2317 | 0 | if (variable) { |
2318 | 0 | if (variable->computed == 0) { |
2319 | |
|
2320 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
2321 | | XSLT_TRACE(tctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
2322 | | "uncomputed variable '%s'\n", name)); |
2323 | | #endif |
2324 | 0 | variable->value = xsltEvalVariable(tctxt, variable, NULL); |
2325 | 0 | variable->computed = 1; |
2326 | 0 | } |
2327 | 0 | if (variable->value != NULL) { |
2328 | 0 | valueObj = xmlXPathObjectCopy(variable->value); |
2329 | 0 | } |
2330 | 0 | return(valueObj); |
2331 | 0 | } |
2332 | 0 | } |
2333 | | /* |
2334 | | * Global variables/params -------------------------------------------- |
2335 | | */ |
2336 | 0 | if (tctxt->globalVars) { |
2337 | 0 | valueObj = xsltGlobalVariableLookup(tctxt, name, ns_uri); |
2338 | 0 | } |
2339 | |
|
2340 | 0 | if (valueObj == NULL) { |
2341 | |
|
2342 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
2343 | | XSLT_TRACE(tctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
2344 | | "variable not found '%s'\n", name)); |
2345 | | #endif |
2346 | |
|
2347 | 0 | if (ns_uri) { |
2348 | 0 | xsltTransformError(tctxt, NULL, tctxt->inst, |
2349 | 0 | "Variable '{%s}%s' has not been declared.\n", ns_uri, name); |
2350 | 0 | } else { |
2351 | 0 | xsltTransformError(tctxt, NULL, tctxt->inst, |
2352 | 0 | "Variable '%s' has not been declared.\n", name); |
2353 | 0 | } |
2354 | 0 | } else { |
2355 | |
|
2356 | | #ifdef WITH_XSLT_DEBUG_VARIABLE |
2357 | | XSLT_TRACE(tctxt,XSLT_TRACE_VARIABLES,xsltGenericDebug(xsltGenericDebugContext, |
2358 | | "found variable '%s'\n", name)); |
2359 | | #endif |
2360 | 0 | } |
2361 | |
|
2362 | 0 | return(valueObj); |
2363 | 0 | } |
2364 | | |
2365 | | |