/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 | 30.0k | exsltDynEvaluateFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
48 | 30.0k | xmlChar *str = NULL; |
49 | 30.0k | xmlXPathObjectPtr ret = NULL; |
50 | | |
51 | 30.0k | if (ctxt == NULL) |
52 | 0 | return; |
53 | 30.0k | if (nargs != 1) { |
54 | 145 | xsltPrintErrorContext(xsltXPathGetTransformContext(ctxt), NULL, NULL); |
55 | 145 | xsltGenericError(xsltGenericErrorContext, |
56 | 145 | "dyn:evalute() : invalid number of args %d\n", nargs); |
57 | 145 | ctxt->error = XPATH_INVALID_ARITY; |
58 | 145 | return; |
59 | 145 | } |
60 | 29.8k | str = xmlXPathPopString(ctxt); |
61 | | /* return an empty node-set if an empty string is passed in */ |
62 | 29.8k | if (!str||!xmlStrlen(str)) { |
63 | 1.20k | if (str) xmlFree(str); |
64 | 1.20k | valuePush(ctxt,xmlXPathNewNodeSet(NULL)); |
65 | 1.20k | return; |
66 | 1.20k | } |
67 | 28.6k | ret = xmlXPathEval(str,ctxt->context); |
68 | 28.6k | if (ret) |
69 | 9.79k | valuePush(ctxt,ret); |
70 | 18.8k | else { |
71 | 18.8k | xsltGenericError(xsltGenericErrorContext, |
72 | 18.8k | "dyn:evaluate() : unable to evaluate expression '%s'\n",str); |
73 | 18.8k | valuePush(ctxt,xmlXPathNewNodeSet(NULL)); |
74 | 18.8k | } |
75 | 28.6k | xmlFree(str); |
76 | 28.6k | return; |
77 | 29.8k | } |
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 | 1.06M | { |
92 | 1.06M | xmlChar *str = NULL; |
93 | 1.06M | xmlNodeSetPtr nodeset = NULL; |
94 | 1.06M | xsltTransformContextPtr tctxt; |
95 | 1.06M | xmlXPathCompExprPtr comp = NULL; |
96 | 1.06M | xmlXPathObjectPtr ret = NULL; |
97 | 1.06M | xmlDocPtr oldDoc, container = NULL; |
98 | 1.06M | xmlNodePtr oldNode; |
99 | 1.06M | int oldContextSize; |
100 | 1.06M | int oldProximityPosition; |
101 | 1.06M | int i, j; |
102 | | |
103 | | |
104 | 1.06M | if (nargs != 2) { |
105 | 5.77k | xmlXPathSetArityError(ctxt); |
106 | 5.77k | return; |
107 | 5.77k | } |
108 | 1.06M | str = xmlXPathPopString(ctxt); |
109 | 1.06M | if (xmlXPathCheckError(ctxt)) |
110 | 0 | goto cleanup; |
111 | | |
112 | 1.06M | nodeset = xmlXPathPopNodeSet(ctxt); |
113 | 1.06M | if (xmlXPathCheckError(ctxt)) |
114 | 369 | goto cleanup; |
115 | | |
116 | 1.06M | ret = xmlXPathNewNodeSet(NULL); |
117 | 1.06M | if (ret == NULL) { |
118 | 625 | xsltGenericError(xsltGenericErrorContext, |
119 | 625 | "exsltDynMapFunction: ret == NULL\n"); |
120 | 625 | goto cleanup; |
121 | 625 | } |
122 | | |
123 | 1.06M | tctxt = xsltXPathGetTransformContext(ctxt); |
124 | 1.06M | 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 | 1.06M | if (str == NULL || !xmlStrlen(str) || |
131 | 1.06M | !(comp = xmlXPathCtxtCompile(tctxt->xpathCtxt, str))) |
132 | 595k | goto cleanup; |
133 | | |
134 | 466k | oldDoc = ctxt->context->doc; |
135 | 466k | oldNode = ctxt->context->node; |
136 | 466k | oldContextSize = ctxt->context->contextSize; |
137 | 466k | 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 | 466k | container = xsltCreateRVT(tctxt); |
144 | 466k | if (container == NULL) { |
145 | 81 | xsltTransformError(tctxt, NULL, NULL, |
146 | 81 | "dyn:map : internal error container == NULL\n"); |
147 | 81 | goto cleanup; |
148 | 81 | } |
149 | 466k | xsltRegisterLocalRVT(tctxt, container); |
150 | 466k | if (nodeset && nodeset->nodeNr > 0) { |
151 | 460k | xmlXPathNodeSetSort(nodeset); |
152 | 460k | ctxt->context->contextSize = nodeset->nodeNr; |
153 | 460k | ctxt->context->proximityPosition = 0; |
154 | 4.42M | for (i = 0; i < nodeset->nodeNr; i++) { |
155 | 3.96M | xmlXPathObjectPtr subResult = NULL; |
156 | 3.96M | xmlNodePtr cur = nodeset->nodeTab[i]; |
157 | | |
158 | 3.96M | ctxt->context->proximityPosition++; |
159 | 3.96M | ctxt->context->node = cur; |
160 | | |
161 | 3.96M | 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 | 19.2k | cur = (xmlNodePtr) ((xmlNsPtr) cur)->next; |
167 | 19.2k | 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 | 19.2k | ctxt->context->doc = cur->doc; |
174 | 3.94M | } else { |
175 | 3.94M | ctxt->context->doc = cur->doc; |
176 | 3.94M | } |
177 | | |
178 | 3.96M | subResult = xmlXPathCompiledEval(comp, ctxt->context); |
179 | 3.96M | if (subResult != NULL) { |
180 | 3.17M | switch (subResult->type) { |
181 | 2.51M | case XPATH_NODESET: |
182 | 2.51M | if (subResult->nodesetval != NULL) |
183 | 28.4M | for (j = 0; j < subResult->nodesetval->nodeNr; |
184 | 25.9M | j++) |
185 | 25.9M | xmlXPathNodeSetAdd(ret->nodesetval, |
186 | 25.9M | subResult->nodesetval-> |
187 | 25.9M | nodeTab[j]); |
188 | 2.51M | break; |
189 | 463k | case XPATH_BOOLEAN: |
190 | 463k | if (container != NULL) { |
191 | 463k | xmlNodePtr newChildNode = |
192 | 463k | xmlNewTextChild((xmlNodePtr) container, NULL, |
193 | 463k | BAD_CAST "boolean", |
194 | 463k | BAD_CAST (subResult-> |
195 | 463k | boolval ? "true" : "")); |
196 | 463k | if (newChildNode != NULL) { |
197 | 461k | newChildNode->ns = |
198 | 461k | xmlNewNs(newChildNode, |
199 | 461k | BAD_CAST |
200 | 461k | "http://exslt.org/common", |
201 | 461k | BAD_CAST "exsl"); |
202 | 461k | xmlXPathNodeSetAddUnique(ret->nodesetval, |
203 | 461k | newChildNode); |
204 | 461k | } |
205 | 463k | } |
206 | 463k | break; |
207 | 166k | case XPATH_NUMBER: |
208 | 166k | if (container != NULL) { |
209 | 166k | xmlChar *val = |
210 | 166k | xmlXPathCastNumberToString(subResult-> |
211 | 166k | floatval); |
212 | 166k | xmlNodePtr newChildNode = |
213 | 166k | xmlNewTextChild((xmlNodePtr) container, NULL, |
214 | 166k | BAD_CAST "number", val); |
215 | 166k | if (val != NULL) |
216 | 165k | xmlFree(val); |
217 | | |
218 | 166k | if (newChildNode != NULL) { |
219 | 165k | newChildNode->ns = |
220 | 165k | xmlNewNs(newChildNode, |
221 | 165k | BAD_CAST |
222 | 165k | "http://exslt.org/common", |
223 | 165k | BAD_CAST "exsl"); |
224 | 165k | xmlXPathNodeSetAddUnique(ret->nodesetval, |
225 | 165k | newChildNode); |
226 | 165k | } |
227 | 166k | } |
228 | 166k | break; |
229 | 22.8k | case XPATH_STRING: |
230 | 22.8k | if (container != NULL) { |
231 | 22.8k | xmlNodePtr newChildNode = |
232 | 22.8k | xmlNewTextChild((xmlNodePtr) container, NULL, |
233 | 22.8k | BAD_CAST "string", |
234 | 22.8k | subResult->stringval); |
235 | 22.8k | if (newChildNode != NULL) { |
236 | 22.8k | newChildNode->ns = |
237 | 22.8k | xmlNewNs(newChildNode, |
238 | 22.8k | BAD_CAST |
239 | 22.8k | "http://exslt.org/common", |
240 | 22.8k | BAD_CAST "exsl"); |
241 | 22.8k | xmlXPathNodeSetAddUnique(ret->nodesetval, |
242 | 22.8k | newChildNode); |
243 | 22.8k | } |
244 | 22.8k | } |
245 | 22.8k | break; |
246 | 0 | default: |
247 | 0 | break; |
248 | 3.17M | } |
249 | 3.17M | xmlXPathFreeObject(subResult); |
250 | 3.17M | } |
251 | 3.96M | } |
252 | 460k | } |
253 | 466k | ctxt->context->doc = oldDoc; |
254 | 466k | ctxt->context->node = oldNode; |
255 | 466k | ctxt->context->contextSize = oldContextSize; |
256 | 466k | ctxt->context->proximityPosition = oldProximityPosition; |
257 | | |
258 | | |
259 | 1.06M | cleanup: |
260 | | /* restore the xpath context */ |
261 | 1.06M | if (comp != NULL) |
262 | 466k | xmlXPathFreeCompExpr(comp); |
263 | 1.06M | if (nodeset != NULL) |
264 | 1.06M | xmlXPathFreeNodeSet(nodeset); |
265 | 1.06M | if (str != NULL) |
266 | 1.06M | xmlFree(str); |
267 | 1.06M | valuePush(ctxt, ret); |
268 | 1.06M | return; |
269 | 466k | } |
270 | | |
271 | | |
272 | | /** |
273 | | * exsltDynRegister: |
274 | | * |
275 | | * Registers the EXSLT - Dynamic module |
276 | | */ |
277 | | |
278 | | void |
279 | 2 | exsltDynRegister (void) { |
280 | 2 | xsltRegisterExtModuleFunction ((const xmlChar *) "evaluate", |
281 | 2 | EXSLT_DYNAMIC_NAMESPACE, |
282 | 2 | exsltDynEvaluateFunction); |
283 | 2 | xsltRegisterExtModuleFunction ((const xmlChar *) "map", |
284 | 2 | EXSLT_DYNAMIC_NAMESPACE, |
285 | 2 | exsltDynMapFunction); |
286 | | |
287 | 2 | } |