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