/src/libxslt/libexslt/dynamic.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * dynamic.c: Implementation of the EXSLT -- Dynamic module  | 
3  |  |  *  | 
4  |  |  * References:  | 
5  |  |  *   http://www.exslt.org/dyn/dyn.html  | 
6  |  |  *  | 
7  |  |  * See Copyright for the status of this software.  | 
8  |  |  *  | 
9  |  |  * Authors:  | 
10  |  |  *   Mark Vakoc <mark_vakoc@jdedwards.com>  | 
11  |  |  *   Thomas Broyer <tbroyer@ltgt.net>  | 
12  |  |  *  | 
13  |  |  * TODO:  | 
14  |  |  * elements:  | 
15  |  |  * functions:  | 
16  |  |  *    min  | 
17  |  |  *    max  | 
18  |  |  *    sum  | 
19  |  |  *    map  | 
20  |  |  *    closure  | 
21  |  |  */  | 
22  |  |  | 
23  |  | #define IN_LIBEXSLT  | 
24  |  | #include "libexslt/libexslt.h"  | 
25  |  |  | 
26  |  | #include <libxml/tree.h>  | 
27  |  | #include <libxml/xpath.h>  | 
28  |  | #include <libxml/xpathInternals.h>  | 
29  |  |  | 
30  |  | #include <libxslt/xsltutils.h>  | 
31  |  | #include <libxslt/xsltInternals.h>  | 
32  |  | #include <libxslt/extensions.h>  | 
33  |  |  | 
34  |  | #include "exslt.h"  | 
35  |  |  | 
36  |  | /**  | 
37  |  |  * exsltDynEvaluateFunction:  | 
38  |  |  * @ctxt:  an XPath parser context  | 
39  |  |  * @nargs:  the number of arguments  | 
40  |  |  *  | 
41  |  |  * Evaluates the string as an XPath expression and returns the result  | 
42  |  |  * value, which may be a boolean, number, string, node set, result tree  | 
43  |  |  * fragment or external object.  | 
44  |  |  */  | 
45  |  |  | 
46  |  | static void  | 
47  | 12  | exsltDynEvaluateFunction(xmlXPathParserContextPtr ctxt, int nargs) { | 
48  | 12  |   xmlChar *str = NULL;  | 
49  | 12  |   xmlXPathObjectPtr ret = NULL;  | 
50  |  |  | 
51  | 12  |   if (ctxt == NULL)  | 
52  | 0  |     return;  | 
53  | 12  |   if (nargs != 1) { | 
54  | 2  |     xsltPrintErrorContext(xsltXPathGetTransformContext(ctxt), NULL, NULL);  | 
55  | 2  |         xsltGenericError(xsltGenericErrorContext,  | 
56  | 2  |       "dyn:evalute() : invalid number of args %d\n", nargs);  | 
57  | 2  |     ctxt->error = XPATH_INVALID_ARITY;  | 
58  | 2  |     return;  | 
59  | 2  |   }  | 
60  | 10  |   str = xmlXPathPopString(ctxt);  | 
61  |  |   /* return an empty node-set if an empty string is passed in */  | 
62  | 10  |   if (!str||!xmlStrlen(str)) { | 
63  | 1  |     if (str) xmlFree(str);  | 
64  | 1  |     valuePush(ctxt,xmlXPathNewNodeSet(NULL));  | 
65  | 1  |     return;  | 
66  | 1  |   }  | 
67  | 9  |   ret = xmlXPathEval(str,ctxt->context);  | 
68  | 9  |   if (ret)  | 
69  | 4  |     valuePush(ctxt,ret);  | 
70  | 5  |   else { | 
71  | 5  |     xsltGenericError(xsltGenericErrorContext,  | 
72  | 5  |       "dyn:evaluate() : unable to evaluate expression '%s'\n",str);  | 
73  | 5  |     valuePush(ctxt,xmlXPathNewNodeSet(NULL));  | 
74  | 5  |   }  | 
75  | 9  |   xmlFree(str);  | 
76  | 9  |   return;  | 
77  | 10  | }  | 
78  |  |  | 
79  |  | /**  | 
80  |  |  * exsltDynMapFunction:  | 
81  |  |  * @ctxt:  an XPath parser context  | 
82  |  |  * @nargs:  the number of arguments  | 
83  |  |  *  | 
84  |  |  * Evaluates the string as an XPath expression and returns the result  | 
85  |  |  * value, which may be a boolean, number, string, node set, result tree  | 
86  |  |  * fragment or external object.  | 
87  |  |  */  | 
88  |  |  | 
89  |  | static void  | 
90  |  | exsltDynMapFunction(xmlXPathParserContextPtr ctxt, int nargs)  | 
91  | 0  | { | 
92  | 0  |     xmlChar *str = NULL;  | 
93  | 0  |     xmlNodeSetPtr nodeset = NULL;  | 
94  | 0  |     xsltTransformContextPtr tctxt;  | 
95  | 0  |     xmlXPathCompExprPtr comp = NULL;  | 
96  | 0  |     xmlXPathObjectPtr ret = NULL;  | 
97  | 0  |     xmlDocPtr oldDoc, container = NULL;  | 
98  | 0  |     xmlNodePtr oldNode;  | 
99  | 0  |     int oldContextSize;  | 
100  | 0  |     int oldProximityPosition;  | 
101  | 0  |     int i, j;  | 
102  |  |  | 
103  |  | 
  | 
104  | 0  |     if (nargs != 2) { | 
105  | 0  |         xmlXPathSetArityError(ctxt);  | 
106  | 0  |         return;  | 
107  | 0  |     }  | 
108  | 0  |     str = xmlXPathPopString(ctxt);  | 
109  | 0  |     if (xmlXPathCheckError(ctxt))  | 
110  | 0  |         goto cleanup;  | 
111  |  |  | 
112  | 0  |     nodeset = xmlXPathPopNodeSet(ctxt);  | 
113  | 0  |     if (xmlXPathCheckError(ctxt))  | 
114  | 0  |         goto cleanup;  | 
115  |  |  | 
116  | 0  |     ret = xmlXPathNewNodeSet(NULL);  | 
117  | 0  |     if (ret == NULL) { | 
118  | 0  |         xsltGenericError(xsltGenericErrorContext,  | 
119  | 0  |                          "exsltDynMapFunction: ret == NULL\n");  | 
120  | 0  |         goto cleanup;  | 
121  | 0  |     }  | 
122  |  |  | 
123  | 0  |     tctxt = xsltXPathGetTransformContext(ctxt);  | 
124  | 0  |     if (tctxt == NULL) { | 
125  | 0  |   xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,  | 
126  | 0  |         "dyn:map : internal error tctxt == NULL\n");  | 
127  | 0  |   goto cleanup;  | 
128  | 0  |     }  | 
129  |  |  | 
130  | 0  |     if (str == NULL || !xmlStrlen(str) ||  | 
131  | 0  |         !(comp = xmlXPathCtxtCompile(tctxt->xpathCtxt, str)))  | 
132  | 0  |         goto cleanup;  | 
133  |  |  | 
134  | 0  |     oldDoc = ctxt->context->doc;  | 
135  | 0  |     oldNode = ctxt->context->node;  | 
136  | 0  |     oldContextSize = ctxt->context->contextSize;  | 
137  | 0  |     oldProximityPosition = ctxt->context->proximityPosition;  | 
138  |  |  | 
139  |  |         /**  | 
140  |  |    * since we really don't know we're going to be adding node(s)  | 
141  |  |    * down the road we create the RVT regardless  | 
142  |  |    */  | 
143  | 0  |     container = xsltCreateRVT(tctxt);  | 
144  | 0  |     if (container == NULL) { | 
145  | 0  |   xsltTransformError(tctxt, NULL, NULL,  | 
146  | 0  |         "dyn:map : internal error container == NULL\n");  | 
147  | 0  |   goto cleanup;  | 
148  | 0  |     }  | 
149  | 0  |     xsltRegisterLocalRVT(tctxt, container);  | 
150  | 0  |     if (nodeset && nodeset->nodeNr > 0) { | 
151  | 0  |         xmlXPathNodeSetSort(nodeset);  | 
152  | 0  |         ctxt->context->contextSize = nodeset->nodeNr;  | 
153  | 0  |         ctxt->context->proximityPosition = 0;  | 
154  | 0  |         for (i = 0; i < nodeset->nodeNr; i++) { | 
155  | 0  |             xmlXPathObjectPtr subResult = NULL;  | 
156  | 0  |             xmlNodePtr cur = nodeset->nodeTab[i];  | 
157  |  | 
  | 
158  | 0  |             ctxt->context->proximityPosition++;  | 
159  | 0  |             ctxt->context->node = cur;  | 
160  |  | 
  | 
161  | 0  |             if (cur->type == XML_NAMESPACE_DECL) { | 
162  |  |                 /*  | 
163  |  |                 * The XPath module sets the owner element of a ns-node on  | 
164  |  |                 * the ns->next field.  | 
165  |  |                 */  | 
166  | 0  |                 cur = (xmlNodePtr) ((xmlNsPtr) cur)->next;  | 
167  | 0  |                 if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE)) { | 
168  | 0  |                     xsltGenericError(xsltGenericErrorContext,  | 
169  | 0  |                         "Internal error in exsltDynMapFunction: "  | 
170  | 0  |                         "Cannot retrieve the doc of a namespace node.\n");  | 
171  | 0  |                     continue;  | 
172  | 0  |                 }  | 
173  | 0  |                 ctxt->context->doc = cur->doc;  | 
174  | 0  |             } else { | 
175  | 0  |                 ctxt->context->doc = cur->doc;  | 
176  | 0  |             }  | 
177  |  |  | 
178  | 0  |             subResult = xmlXPathCompiledEval(comp, ctxt->context);  | 
179  | 0  |             if (subResult != NULL) { | 
180  | 0  |                 switch (subResult->type) { | 
181  | 0  |                     case XPATH_NODESET:  | 
182  | 0  |                         if (subResult->nodesetval != NULL)  | 
183  | 0  |                             for (j = 0; j < subResult->nodesetval->nodeNr;  | 
184  | 0  |                                  j++)  | 
185  | 0  |                                 xmlXPathNodeSetAdd(ret->nodesetval,  | 
186  | 0  |                                                    subResult->nodesetval->  | 
187  | 0  |                                                    nodeTab[j]);  | 
188  | 0  |                         break;  | 
189  | 0  |                     case XPATH_BOOLEAN:  | 
190  | 0  |                         if (container != NULL) { | 
191  | 0  |                             xmlNodePtr newChildNode =  | 
192  | 0  |                                 xmlNewTextChild((xmlNodePtr) container, NULL,  | 
193  | 0  |                                                 BAD_CAST "boolean",  | 
194  | 0  |                                                 BAD_CAST (subResult->  | 
195  | 0  |                                                 boolval ? "true" : ""));  | 
196  | 0  |                             if (newChildNode != NULL) { | 
197  | 0  |                                 newChildNode->ns =  | 
198  | 0  |                                     xmlNewNs(newChildNode,  | 
199  | 0  |                                              BAD_CAST  | 
200  | 0  |                                              "http://exslt.org/common",  | 
201  | 0  |                                              BAD_CAST "exsl");  | 
202  | 0  |                                 xmlXPathNodeSetAddUnique(ret->nodesetval,  | 
203  | 0  |                                                          newChildNode);  | 
204  | 0  |                             }  | 
205  | 0  |                         }  | 
206  | 0  |                         break;  | 
207  | 0  |                     case XPATH_NUMBER:  | 
208  | 0  |                         if (container != NULL) { | 
209  | 0  |                             xmlChar *val =  | 
210  | 0  |                                 xmlXPathCastNumberToString(subResult->  | 
211  | 0  |                                                            floatval);  | 
212  | 0  |                             xmlNodePtr newChildNode =  | 
213  | 0  |                                 xmlNewTextChild((xmlNodePtr) container, NULL,  | 
214  | 0  |                                                 BAD_CAST "number", val);  | 
215  | 0  |                             if (val != NULL)  | 
216  | 0  |                                 xmlFree(val);  | 
217  |  | 
  | 
218  | 0  |                             if (newChildNode != NULL) { | 
219  | 0  |                                 newChildNode->ns =  | 
220  | 0  |                                     xmlNewNs(newChildNode,  | 
221  | 0  |                                              BAD_CAST  | 
222  | 0  |                                              "http://exslt.org/common",  | 
223  | 0  |                                              BAD_CAST "exsl");  | 
224  | 0  |                                 xmlXPathNodeSetAddUnique(ret->nodesetval,  | 
225  | 0  |                                                          newChildNode);  | 
226  | 0  |                             }  | 
227  | 0  |                         }  | 
228  | 0  |                         break;  | 
229  | 0  |                     case XPATH_STRING:  | 
230  | 0  |                         if (container != NULL) { | 
231  | 0  |                             xmlNodePtr newChildNode =  | 
232  | 0  |                                 xmlNewTextChild((xmlNodePtr) container, NULL,  | 
233  | 0  |                                                 BAD_CAST "string",  | 
234  | 0  |                                                 subResult->stringval);  | 
235  | 0  |                             if (newChildNode != NULL) { | 
236  | 0  |                                 newChildNode->ns =  | 
237  | 0  |                                     xmlNewNs(newChildNode,  | 
238  | 0  |                                              BAD_CAST  | 
239  | 0  |                                              "http://exslt.org/common",  | 
240  | 0  |                                              BAD_CAST "exsl");  | 
241  | 0  |                                 xmlXPathNodeSetAddUnique(ret->nodesetval,  | 
242  | 0  |                                                          newChildNode);  | 
243  | 0  |                             }  | 
244  | 0  |                         }  | 
245  | 0  |                         break;  | 
246  | 0  |         default:  | 
247  | 0  |                         break;  | 
248  | 0  |                 }  | 
249  | 0  |                 xmlXPathFreeObject(subResult);  | 
250  | 0  |             }  | 
251  | 0  |         }  | 
252  | 0  |     }  | 
253  | 0  |     ctxt->context->doc = oldDoc;  | 
254  | 0  |     ctxt->context->node = oldNode;  | 
255  | 0  |     ctxt->context->contextSize = oldContextSize;  | 
256  | 0  |     ctxt->context->proximityPosition = oldProximityPosition;  | 
257  |  |  | 
258  |  | 
  | 
259  | 0  |   cleanup:  | 
260  |  |     /* restore the xpath context */  | 
261  | 0  |     if (comp != NULL)  | 
262  | 0  |         xmlXPathFreeCompExpr(comp);  | 
263  | 0  |     if (nodeset != NULL)  | 
264  | 0  |         xmlXPathFreeNodeSet(nodeset);  | 
265  | 0  |     if (str != NULL)  | 
266  | 0  |         xmlFree(str);  | 
267  | 0  |     valuePush(ctxt, ret);  | 
268  | 0  |     return;  | 
269  | 0  | }  | 
270  |  |  | 
271  |  |  | 
272  |  | /**  | 
273  |  |  * exsltDynRegister:  | 
274  |  |  *  | 
275  |  |  * Registers the EXSLT - Dynamic module  | 
276  |  |  */  | 
277  |  |  | 
278  |  | void  | 
279  | 29  | exsltDynRegister (void) { | 
280  | 29  |     xsltRegisterExtModuleFunction ((const xmlChar *) "evaluate",  | 
281  | 29  |            EXSLT_DYNAMIC_NAMESPACE,  | 
282  | 29  |            exsltDynEvaluateFunction);  | 
283  | 29  |   xsltRegisterExtModuleFunction ((const xmlChar *) "map",  | 
284  | 29  |            EXSLT_DYNAMIC_NAMESPACE,  | 
285  | 29  |            exsltDynMapFunction);  | 
286  |  |  | 
287  | 29  | }  |