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