From 232b43a4713024ac800e47eabd1b2bc286d33f91 Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Wed, 16 May 2001 10:30:37 +0000 Subject: [PATCH] - libxslt/extensions.c libxslt/preproc.c libxslt/transform.c libxslt/variables.c: force the precompilation of XPath expressions at stylesheet compilation time Daniel --- ChangeLog | 6 ++++ libxslt/extensions.c | 1 + libxslt/preproc.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++-- libxslt/transform.c | 78 ++++++++++++++++-------------------------------- libxslt/variables.c | 8 ++--- 5 files changed, 116 insertions(+), 61 deletions(-) diff --git a/ChangeLog b/ChangeLog index b85680a..b4004b7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Wed May 16 12:29:17 CEST 2001 Daniel Veillard + + * libxslt/extensions.c libxslt/preproc.c libxslt/transform.c + libxslt/variables.c: force the precompilation of XPath expressions + at stylesheet compilation time + Tue May 15 14:34:23 CEST 2001 Daniel Veillard * libxslt/keys.c libxslt/transform.c: avoid some possibilities diff --git a/libxslt/extensions.c b/libxslt/extensions.c index d56e6fb..46d1b3d 100644 --- a/libxslt/extensions.c +++ b/libxslt/extensions.c @@ -33,6 +33,7 @@ struct _xsltExtDef { struct _xsltExtDef *next; xmlChar *prefix; xmlChar *URI; + void *data; }; /************************************************************************ diff --git a/libxslt/preproc.c b/libxslt/preproc.c index d0d8bdf..766cc05 100644 --- a/libxslt/preproc.c +++ b/libxslt/preproc.c @@ -343,6 +343,13 @@ xsltSortComp(xsltStylesheetPtr style, xmlNodePtr inst) { */ comp->select = xmlStrdup((const xmlChar *)"."); } + comp->comp = xmlXPathCompile(comp->select); + if (comp->comp == NULL) { + xsltGenericError(xsltGenericErrorContext, + "xsltSortComp: could not compile select expression '%s'\n", + comp->select); + style->errors++; + } } /** @@ -545,6 +552,14 @@ xsltCopyOfComp(xsltStylesheetPtr style, xmlNodePtr inst) { xsltGenericError(xsltGenericErrorContext, "xslt:copy-of : select is missing\n"); style->errors++; + return; + } + comp->comp = xmlXPathCompile(comp->select); + if (comp->comp == NULL) { + xsltGenericError(xsltGenericErrorContext, + "xslt:copy-of : could not compile select expression '%s'\n", + comp->select); + style->errors++; } } @@ -588,6 +603,14 @@ xsltValueOfComp(xsltStylesheetPtr style, xmlNodePtr inst) { xsltGenericError(xsltGenericErrorContext, "xslt:value-of : select is missing\n"); style->errors++; + return; + } + comp->comp = xmlXPathCompile(comp->select); + if (comp->comp == NULL) { + xsltGenericError(xsltGenericErrorContext, + "xslt:value-of : could not compile select expression '%s'\n", + comp->select); + style->errors++; } } @@ -650,12 +673,18 @@ xsltWithParamComp(xsltStylesheetPtr style, xmlNodePtr inst) { comp->select = xmlGetNsProp(inst, (const xmlChar *)"select", XSLT_NAMESPACE); if (comp->select != NULL) { + comp->comp = xmlXPathCompile(comp->select); + if (comp->comp == NULL) { + xsltGenericError(xsltGenericErrorContext, + "xslt:param : could not compile select expression '%s'\n", + comp->select); + style->errors++; + } if (inst->children != NULL) xsltGenericError(xsltGenericErrorContext, "xsl:param : content should be empty since select is present \n"); style->warnings++; } - comp->comp = NULL; if (prop != NULL) xmlFree(prop); @@ -910,6 +939,15 @@ xsltApplyTemplatesComp(xsltStylesheetPtr style, xmlNodePtr inst) { } comp->select = xmlGetNsProp(inst, (const xmlChar *)"select", XSLT_NAMESPACE); + if (comp->select != NULL) { + comp->comp = xmlXPathCompile(comp->select); + if (comp->comp == NULL) { + xsltGenericError(xsltGenericErrorContext, + "xslt:apply-templates : could not compile select expression '%s'\n", + comp->select); + style->errors++; + } + } /* TODO: handle (or skip) the xsl:sort and xsl:with-param */ } @@ -960,6 +998,13 @@ xsltIfComp(xsltStylesheetPtr style, xmlNodePtr inst) { style->errors++; return; } + comp->comp = xmlXPathCompile(comp->test); + if (comp->comp == NULL) { + xsltGenericError(xsltGenericErrorContext, + "xslt:if : could not compile test expression '%s'\n", + comp->test); + style->errors++; + } } /** @@ -988,6 +1033,13 @@ xsltWhenComp(xsltStylesheetPtr style, xmlNodePtr inst) { style->errors++; return; } + comp->comp = xmlXPathCompile(comp->test); + if (comp->comp == NULL) { + xsltGenericError(xsltGenericErrorContext, + "xsl:when : could not compile test expression '%s'\n", + comp->test); + style->errors++; + } } /** @@ -1011,7 +1063,19 @@ xsltForEachComp(xsltStylesheetPtr style, xmlNodePtr inst) { comp->select = xmlGetNsProp(inst, (const xmlChar *)"select", XSLT_NAMESPACE); - + if (comp->select == NULL) { + xsltGenericError(xsltGenericErrorContext, + "xslt:for-each : select is missing\n"); + style->errors++; + } else { + comp->comp = xmlXPathCompile(comp->select); + if (comp->comp == NULL) { + xsltGenericError(xsltGenericErrorContext, + "xslt:for-each : could not compile select expression '%s'\n", + comp->select); + style->errors++; + } + } /* TODO: handle and skip the xsl:sort */ } @@ -1073,6 +1137,13 @@ xsltVariableComp(xsltStylesheetPtr style, xmlNodePtr inst) { comp->select = xmlGetNsProp(inst, (const xmlChar *)"select", XSLT_NAMESPACE); if (comp->select != NULL) { + comp->comp = xmlXPathCompile(comp->select); + if (comp->comp == NULL) { + xsltGenericError(xsltGenericErrorContext, + "xslt:variable : could not compile select expression '%s'\n", + comp->select); + style->errors++; + } if (inst->children != NULL) xsltGenericError(xsltGenericErrorContext, "xsl:variable : content should be empty since select is present \n"); @@ -1145,9 +1216,16 @@ xsltParamComp(xsltStylesheetPtr style, xmlNodePtr inst) { comp->select = xmlGetNsProp(inst, (const xmlChar *)"select", XSLT_NAMESPACE); if (comp->select != NULL) { + comp->comp = xmlXPathCompile(comp->select); + if (comp->comp == NULL) { + xsltGenericError(xsltGenericErrorContext, + "xslt:param : could not compile select expression '%s'\n", + comp->select); + style->errors++; + } if (inst->children != NULL) xsltGenericError(xsltGenericErrorContext, - "xsl:variable : content should be empty since select is present \n"); + "xsl:param : content should be empty since select is present \n"); style->warnings++; } diff --git a/libxslt/transform.c b/libxslt/transform.c index a12de9d..236c381 100644 --- a/libxslt/transform.c +++ b/libxslt/transform.c @@ -1696,14 +1696,12 @@ xsltCopyOf(xsltTransformContextPtr ctxt, xmlNodePtr node, if ((ctxt == NULL) || (node == NULL) || (inst == NULL)) return; - - if (comp->select == NULL) + if ((comp == NULL) || (comp->select == NULL) || (comp->comp == NULL)) { + xsltGenericError(xsltGenericErrorContext, + "xsl:copy-of : compilation have failed\n"); return; - if (comp->comp == NULL) { - comp->comp = xmlXPathCompile(comp->select); - if (comp->comp == NULL) - return; } + #ifdef WITH_XSLT_DEBUG_PROCESS xsltGenericDebug(xsltGenericDebugContext, "xsltCopyOf: select %s\n", comp->select); @@ -1796,15 +1794,12 @@ xsltValueOf(xsltTransformContextPtr ctxt, xmlNodePtr node, xmlNodePtr copy = NULL; int oldProximityPosition, oldContextSize; - if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL)) + if ((ctxt == NULL) || (node == NULL) || (inst == NULL)) return; - - if (comp->select == NULL) + if ((comp == NULL) || (comp->select == NULL) || (comp->comp == NULL)) { + xsltGenericError(xsltGenericErrorContext, + "xsl:value-of : compilation have failed\n"); return; - if (comp->comp == NULL) { - comp->comp = xmlXPathCompile(comp->select); - if (comp->comp == NULL) - return; } #ifdef WITH_XSLT_DEBUG_PROCESS @@ -2014,9 +2009,9 @@ xsltApplyTemplates(xsltTransformContextPtr ctxt, xmlNodePtr node, if (comp->select != NULL) { if (comp->comp == NULL) { - comp->comp = xmlXPathCompile(comp->select); - if (comp->comp == NULL) - goto error; + xsltGenericError(xsltGenericErrorContext, + "xslt:apply-templates : compilation had failed\n"); + goto error; } #ifdef WITH_XSLT_DEBUG_PROCESS xsltGenericDebug(xsltGenericDebugContext, @@ -2221,30 +2216,25 @@ xsltChoose(xsltTransformContextPtr ctxt, xmlNodePtr node, goto error; } while (IS_XSLT_ELEM(replacement) && (IS_XSLT_NAME(replacement, "when"))) { - xmlXPathCompExprPtr xpathComp; - /* TODO: build a prexpathCompiled block for when too ! */ - when = replacement; - prop = xmlGetNsProp(when, (const xmlChar *)"test", XSLT_NAMESPACE); - if (prop == NULL) { + xsltStylePreCompPtr wcomp = replacement->_private; + + if ((wcomp == NULL) || (wcomp->test == NULL) || (wcomp->comp == NULL)) { xsltGenericError(xsltGenericErrorContext, - "xsl:when: test is not defined\n"); - return; + "xsl:when: compilation failed !\n"); + goto error; } + when = replacement; #ifdef WITH_XSLT_DEBUG_PROCESS xsltGenericDebug(xsltGenericDebugContext, - "xsl:when: test %s\n", prop); + "xsl:when: test %s\n", wcomp->test); #endif - xpathComp = xmlXPathCompile(prop); - if (xpathComp == NULL) - goto error; oldProximityPosition = ctxt->xpathCtxt->proximityPosition; oldContextSize = ctxt->xpathCtxt->contextSize; ctxt->xpathCtxt->node = node; ctxt->xpathCtxt->namespaces = comp->nsList; ctxt->xpathCtxt->nsNr = comp->nsNr; - res = xmlXPathCompiledEval(xpathComp, ctxt->xpathCtxt); - xmlXPathFreeCompExpr(xpathComp); + res = xmlXPathCompiledEval(wcomp->comp, ctxt->xpathCtxt); ctxt->xpathCtxt->proximityPosition = oldProximityPosition; ctxt->xpathCtxt->contextSize = oldContextSize; if (res != NULL) { @@ -2319,20 +2309,12 @@ xsltIf(xsltTransformContextPtr ctxt, xmlNodePtr node, int doit = 1; int oldContextSize, oldProximityPosition; - if (comp == NULL) { - xsltGenericError(xsltGenericErrorContext, - "xslt:if : compilation had failed\n"); - return; - } - if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL)) + if ((ctxt == NULL) || (node == NULL) || (inst == NULL)) return; - - if (comp->test == NULL) + if ((comp == NULL) || (comp->test == NULL) || (comp->comp == NULL)) { + xsltGenericError(xsltGenericErrorContext, + "xsl:if : compilation have failed\n"); return; - if (comp->comp == NULL) { - comp->comp = xmlXPathCompile(comp->test); - if (comp->comp == NULL) - return; } #ifdef WITH_XSLT_DEBUG_PROCESS @@ -2397,21 +2379,13 @@ xsltForEach(xsltTransformContextPtr ctxt, xmlNodePtr node, int nbsorts = 0; xmlNodePtr sorts[XSLT_MAX_SORT]; - if (comp == NULL) { + if ((ctxt == NULL) || (node == NULL) || (inst == NULL)) + return; + if ((comp == NULL) || (comp->select == NULL) || (comp->comp == NULL)) { xsltGenericError(xsltGenericErrorContext, "xslt:for-each : compilation had failed\n"); return; } - if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL)) - return; - - if (comp->select == NULL) - return; - if (comp->comp == NULL) { - comp->comp = xmlXPathCompile(comp->select); - if (comp->comp == NULL) - return; - } #ifdef WITH_XSLT_DEBUG_PROCESS xsltGenericDebug(xsltGenericDebugContext, diff --git a/libxslt/variables.c b/libxslt/variables.c index 13aaac0..17043ff 100644 --- a/libxslt/variables.c +++ b/libxslt/variables.c @@ -263,12 +263,8 @@ xsltEvalVariable(xsltTransformContextPtr ctxt, xsltStackElemPtr elem, xmlXPathCompExprPtr comp = NULL; xmlXPathObjectPtr result; - if (precomp != NULL) { + if ((precomp != NULL) && (precomp->comp != NULL)) { comp = precomp->comp; - if (comp == NULL) { - comp = xmlXPathCompile(elem->select); - precomp->comp = comp; - } } else { comp = xmlXPathCompile(elem->select); } @@ -283,7 +279,7 @@ xsltEvalVariable(xsltTransformContextPtr ctxt, xsltStackElemPtr elem, result = xmlXPathCompiledEval(comp, ctxt->xpathCtxt); ctxt->xpathCtxt->contextSize = oldContextSize; ctxt->xpathCtxt->proximityPosition = oldProximityPosition; - if (precomp == NULL) + if ((precomp == NULL) || (precomp->comp == NULL)) xmlXPathFreeCompExpr(comp); if (result == NULL) { xsltGenericError(xsltGenericErrorContext, -- 2.7.4