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