12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642 |
- /*
- * pattern.c: Implemetation of the template match compilation and lookup
- *
- * Reference:
- * http://www.w3.org/TR/1999/REC-xslt-19991116
- *
- * See Copyright for the status of this software.
- *
- * daniel@veillard.com
- */
- /*
- * TODO: handle pathological cases like *[*[@a="b"]]
- * TODO: detect [number] at compilation, optimize accordingly
- */
- #define IN_LIBXSLT
- #include "libxslt.h"
- #include <string.h>
- #include <libxml/xmlmemory.h>
- #include <libxml/tree.h>
- #include <libxml/valid.h>
- #include <libxml/hash.h>
- #include <libxml/xmlerror.h>
- #include <libxml/parserInternals.h>
- #include <libxml/xpath.h>
- #include "xslt.h"
- #include "xsltInternals.h"
- #include "xsltutils.h"
- #include "imports.h"
- #include "templates.h"
- #include "keys.h"
- #include "pattern.h"
- #include "documents.h"
- #ifdef WITH_XSLT_DEBUG
- #define WITH_XSLT_DEBUG_PATTERN
- #endif
- /*
- * Types are private:
- */
- typedef enum {
- XSLT_OP_END=0,
- XSLT_OP_ROOT,
- XSLT_OP_ELEM,
- XSLT_OP_ATTR,
- XSLT_OP_PARENT,
- XSLT_OP_ANCESTOR,
- XSLT_OP_ID,
- XSLT_OP_KEY,
- XSLT_OP_NS,
- XSLT_OP_ALL,
- XSLT_OP_PI,
- XSLT_OP_COMMENT,
- XSLT_OP_TEXT,
- XSLT_OP_NODE,
- XSLT_OP_PREDICATE
- } xsltOp;
- typedef enum {
- AXIS_CHILD=1,
- AXIS_ATTRIBUTE
- } xsltAxis;
- typedef struct _xsltStepState xsltStepState;
- typedef xsltStepState *xsltStepStatePtr;
- struct _xsltStepState {
- int step;
- xmlNodePtr node;
- };
- typedef struct _xsltStepStates xsltStepStates;
- typedef xsltStepStates *xsltStepStatesPtr;
- struct _xsltStepStates {
- int nbstates;
- int maxstates;
- xsltStepStatePtr states;
- };
- typedef struct _xsltStepOp xsltStepOp;
- typedef xsltStepOp *xsltStepOpPtr;
- struct _xsltStepOp {
- xsltOp op;
- xmlChar *value;
- xmlChar *value2;
- xmlChar *value3;
- xmlXPathCompExprPtr comp;
- /*
- * Optimisations for count
- */
- int previousExtra;
- int indexExtra;
- int lenExtra;
- };
- struct _xsltCompMatch {
- struct _xsltCompMatch *next; /* siblings in the name hash */
- float priority; /* the priority */
- const xmlChar *pattern; /* the pattern */
- const xmlChar *mode; /* the mode */
- const xmlChar *modeURI; /* the mode URI */
- xsltTemplatePtr template; /* the associated template */
- xmlNodePtr node; /* the containing element */
- int direct;
- /* TODO fix the statically allocated size steps[] */
- int nbStep;
- int maxStep;
- xmlNsPtr *nsList; /* the namespaces in scope */
- int nsNr; /* the number of namespaces in scope */
- xsltStepOpPtr steps; /* ops for computation */
- };
- typedef struct _xsltParserContext xsltParserContext;
- typedef xsltParserContext *xsltParserContextPtr;
- struct _xsltParserContext {
- xsltStylesheetPtr style; /* the stylesheet */
- xsltTransformContextPtr ctxt; /* the transformation or NULL */
- const xmlChar *cur; /* the current char being parsed */
- const xmlChar *base; /* the full expression */
- xmlDocPtr doc; /* the source document */
- xmlNodePtr elem; /* the source element */
- int error; /* error code */
- xsltCompMatchPtr comp; /* the result */
- };
- /************************************************************************
- * *
- * Type functions *
- * *
- ************************************************************************/
- /**
- * xsltNewCompMatch:
- *
- * Create a new XSLT CompMatch
- *
- * Returns the newly allocated xsltCompMatchPtr or NULL in case of error
- */
- static xsltCompMatchPtr
- xsltNewCompMatch(void) {
- xsltCompMatchPtr cur;
- cur = (xsltCompMatchPtr) xmlMalloc(sizeof(xsltCompMatch));
- if (cur == NULL) {
- xsltTransformError(NULL, NULL, NULL,
- "xsltNewCompMatch : out of memory error\n");
- return(NULL);
- }
- memset(cur, 0, sizeof(xsltCompMatch));
- cur->maxStep = 10;
- cur->nbStep = 0;
- cur-> steps = (xsltStepOpPtr) xmlMalloc(sizeof(xsltStepOp) *
- cur->maxStep);
- if (cur->steps == NULL) {
- xsltTransformError(NULL, NULL, NULL,
- "xsltNewCompMatch : out of memory error\n");
- xmlFree(cur);
- return(NULL);
- }
- cur->nsNr = 0;
- cur->nsList = NULL;
- cur->direct = 0;
- return(cur);
- }
- /**
- * xsltFreeCompMatch:
- * @comp: an XSLT comp
- *
- * Free up the memory allocated by @comp
- */
- static void
- xsltFreeCompMatch(xsltCompMatchPtr comp) {
- xsltStepOpPtr op;
- int i;
- if (comp == NULL)
- return;
- if (comp->pattern != NULL)
- xmlFree((xmlChar *)comp->pattern);
- if (comp->nsList != NULL)
- xmlFree(comp->nsList);
- for (i = 0;i < comp->nbStep;i++) {
- op = &comp->steps[i];
- if (op->value != NULL)
- xmlFree(op->value);
- if (op->value2 != NULL)
- xmlFree(op->value2);
- if (op->value3 != NULL)
- xmlFree(op->value3);
- if (op->comp != NULL)
- xmlXPathFreeCompExpr(op->comp);
- }
- xmlFree(comp->steps);
- memset(comp, -1, sizeof(xsltCompMatch));
- xmlFree(comp);
- }
- /**
- * xsltFreeCompMatchList:
- * @comp: an XSLT comp list
- *
- * Free up the memory allocated by all the elements of @comp
- */
- void
- xsltFreeCompMatchList(xsltCompMatchPtr comp) {
- xsltCompMatchPtr cur;
- while (comp != NULL) {
- cur = comp;
- comp = comp->next;
- xsltFreeCompMatch(cur);
- }
- }
- static void
- xsltFreeCompMatchListEntry(void *payload,
- const xmlChar *name ATTRIBUTE_UNUSED) {
- xsltFreeCompMatchList((xsltCompMatchPtr) payload);
- }
- /**
- * xsltNormalizeCompSteps:
- * @payload: pointer to template hash table entry
- * @data: pointer to the stylesheet
- * @name: template match name
- *
- * This is a hashtable scanner function to normalize the compiled
- * steps of an imported stylesheet.
- */
- void xsltNormalizeCompSteps(void *payload,
- void *data, const xmlChar *name ATTRIBUTE_UNUSED) {
- xsltCompMatchPtr comp = payload;
- xsltStylesheetPtr style = data;
- int ix;
- for (ix = 0; ix < comp->nbStep; ix++) {
- comp->steps[ix].previousExtra += style->extrasNr;
- comp->steps[ix].indexExtra += style->extrasNr;
- comp->steps[ix].lenExtra += style->extrasNr;
- }
- }
- /**
- * xsltNewParserContext:
- * @style: the stylesheet
- * @ctxt: the transformation context, if done at run-time
- *
- * Create a new XSLT ParserContext
- *
- * Returns the newly allocated xsltParserContextPtr or NULL in case of error
- */
- static xsltParserContextPtr
- xsltNewParserContext(xsltStylesheetPtr style, xsltTransformContextPtr ctxt) {
- xsltParserContextPtr cur;
- cur = (xsltParserContextPtr) xmlMalloc(sizeof(xsltParserContext));
- if (cur == NULL) {
- xsltTransformError(NULL, NULL, NULL,
- "xsltNewParserContext : malloc failed\n");
- return(NULL);
- }
- memset(cur, 0, sizeof(xsltParserContext));
- cur->style = style;
- cur->ctxt = ctxt;
- return(cur);
- }
- /**
- * xsltFreeParserContext:
- * @ctxt: an XSLT parser context
- *
- * Free up the memory allocated by @ctxt
- */
- static void
- xsltFreeParserContext(xsltParserContextPtr ctxt) {
- if (ctxt == NULL)
- return;
- memset(ctxt, -1, sizeof(xsltParserContext));
- xmlFree(ctxt);
- }
- /**
- * xsltCompMatchAdd:
- * @comp: the compiled match expression
- * @op: an op
- * @value: the first value
- * @value2: the second value
- * @novar: flag to set XML_XPATH_NOVAR
- *
- * Add an step to an XSLT Compiled Match
- *
- * Returns -1 in case of failure, 0 otherwise.
- */
- static int
- xsltCompMatchAdd(xsltParserContextPtr ctxt, xsltCompMatchPtr comp,
- xsltOp op, xmlChar * value, xmlChar * value2, int novar)
- {
- if (comp->nbStep >= comp->maxStep) {
- xsltStepOpPtr tmp;
- tmp = (xsltStepOpPtr) xmlRealloc(comp->steps, comp->maxStep * 2 *
- sizeof(xsltStepOp));
- if (tmp == NULL) {
- xsltGenericError(xsltGenericErrorContext,
- "xsltCompMatchAdd: memory re-allocation failure.\n");
- if (ctxt->style != NULL)
- ctxt->style->errors++;
- if (value)
- xmlFree(value);
- if (value2)
- xmlFree(value2);
- return (-1);
- }
- comp->maxStep *= 2;
- comp->steps = tmp;
- }
- comp->steps[comp->nbStep].op = op;
- comp->steps[comp->nbStep].value = value;
- comp->steps[comp->nbStep].value2 = value2;
- comp->steps[comp->nbStep].value3 = NULL;
- comp->steps[comp->nbStep].comp = NULL;
- if (ctxt->ctxt != NULL) {
- comp->steps[comp->nbStep].previousExtra =
- xsltAllocateExtraCtxt(ctxt->ctxt);
- comp->steps[comp->nbStep].indexExtra =
- xsltAllocateExtraCtxt(ctxt->ctxt);
- comp->steps[comp->nbStep].lenExtra =
- xsltAllocateExtraCtxt(ctxt->ctxt);
- } else {
- comp->steps[comp->nbStep].previousExtra =
- xsltAllocateExtra(ctxt->style);
- comp->steps[comp->nbStep].indexExtra =
- xsltAllocateExtra(ctxt->style);
- comp->steps[comp->nbStep].lenExtra =
- xsltAllocateExtra(ctxt->style);
- }
- if (op == XSLT_OP_PREDICATE) {
- int flags = 0;
- #ifdef XML_XPATH_NOVAR
- if (novar != 0)
- flags = XML_XPATH_NOVAR;
- #endif
- comp->steps[comp->nbStep].comp = xsltXPathCompileFlags(ctxt->style,
- value, flags);
- if (comp->steps[comp->nbStep].comp == NULL) {
- xsltTransformError(NULL, ctxt->style, ctxt->elem,
- "Failed to compile predicate\n");
- if (ctxt->style != NULL)
- ctxt->style->errors++;
- }
- }
- comp->nbStep++;
- return (0);
- }
- /**
- * xsltSwapTopCompMatch:
- * @comp: the compiled match expression
- *
- * reverse the two top steps.
- */
- static void
- xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
- int i;
- int j = comp->nbStep - 1;
- if (j > 0) {
- register xmlChar *tmp;
- register xsltOp op;
- register xmlXPathCompExprPtr expr;
- register int t;
- i = j - 1;
- tmp = comp->steps[i].value;
- comp->steps[i].value = comp->steps[j].value;
- comp->steps[j].value = tmp;
- tmp = comp->steps[i].value2;
- comp->steps[i].value2 = comp->steps[j].value2;
- comp->steps[j].value2 = tmp;
- tmp = comp->steps[i].value3;
- comp->steps[i].value3 = comp->steps[j].value3;
- comp->steps[j].value3 = tmp;
- op = comp->steps[i].op;
- comp->steps[i].op = comp->steps[j].op;
- comp->steps[j].op = op;
- expr = comp->steps[i].comp;
- comp->steps[i].comp = comp->steps[j].comp;
- comp->steps[j].comp = expr;
- t = comp->steps[i].previousExtra;
- comp->steps[i].previousExtra = comp->steps[j].previousExtra;
- comp->steps[j].previousExtra = t;
- t = comp->steps[i].indexExtra;
- comp->steps[i].indexExtra = comp->steps[j].indexExtra;
- comp->steps[j].indexExtra = t;
- t = comp->steps[i].lenExtra;
- comp->steps[i].lenExtra = comp->steps[j].lenExtra;
- comp->steps[j].lenExtra = t;
- }
- }
- /**
- * xsltReverseCompMatch:
- * @ctxt: the parser context
- * @comp: the compiled match expression
- *
- * reverse all the stack of expressions
- */
- static void
- xsltReverseCompMatch(xsltParserContextPtr ctxt, xsltCompMatchPtr comp) {
- int i = 0;
- int j = comp->nbStep - 1;
- while (j > i) {
- register xmlChar *tmp;
- register xsltOp op;
- register xmlXPathCompExprPtr expr;
- register int t;
- tmp = comp->steps[i].value;
- comp->steps[i].value = comp->steps[j].value;
- comp->steps[j].value = tmp;
- tmp = comp->steps[i].value2;
- comp->steps[i].value2 = comp->steps[j].value2;
- comp->steps[j].value2 = tmp;
- tmp = comp->steps[i].value3;
- comp->steps[i].value3 = comp->steps[j].value3;
- comp->steps[j].value3 = tmp;
- op = comp->steps[i].op;
- comp->steps[i].op = comp->steps[j].op;
- comp->steps[j].op = op;
- expr = comp->steps[i].comp;
- comp->steps[i].comp = comp->steps[j].comp;
- comp->steps[j].comp = expr;
- t = comp->steps[i].previousExtra;
- comp->steps[i].previousExtra = comp->steps[j].previousExtra;
- comp->steps[j].previousExtra = t;
- t = comp->steps[i].indexExtra;
- comp->steps[i].indexExtra = comp->steps[j].indexExtra;
- comp->steps[j].indexExtra = t;
- t = comp->steps[i].lenExtra;
- comp->steps[i].lenExtra = comp->steps[j].lenExtra;
- comp->steps[j].lenExtra = t;
- j--;
- i++;
- }
- xsltCompMatchAdd(ctxt, comp, XSLT_OP_END, NULL, NULL, 0);
- /*
- * Detect consecutive XSLT_OP_PREDICATE and predicates on ops which
- * haven't been optimized yet indicating a direct matching should be done.
- */
- for (i = 0;i < comp->nbStep - 1;i++) {
- xsltOp op = comp->steps[i].op;
- if ((op != XSLT_OP_ELEM) &&
- (op != XSLT_OP_ALL) &&
- (comp->steps[i + 1].op == XSLT_OP_PREDICATE)) {
- comp->direct = 1;
- if (comp->pattern[0] != '/') {
- xmlChar *query;
- query = xmlStrdup((const xmlChar *)"//");
- query = xmlStrcat(query, comp->pattern);
- xmlFree((xmlChar *) comp->pattern);
- comp->pattern = query;
- }
- break;
- }
- }
- }
- /************************************************************************
- * *
- * The interpreter for the precompiled patterns *
- * *
- ************************************************************************/
- static int
- xsltPatPushState(xsltTransformContextPtr ctxt, xsltStepStates *states,
- int step, xmlNodePtr node) {
- if ((states->states == NULL) || (states->maxstates <= 0)) {
- states->maxstates = 4;
- states->nbstates = 0;
- states->states = xmlMalloc(4 * sizeof(xsltStepState));
- }
- else if (states->maxstates <= states->nbstates) {
- xsltStepState *tmp;
- tmp = (xsltStepStatePtr) xmlRealloc(states->states,
- 2 * states->maxstates * sizeof(xsltStepState));
- if (tmp == NULL) {
- xsltGenericError(xsltGenericErrorContext,
- "xsltPatPushState: memory re-allocation failure.\n");
- ctxt->state = XSLT_STATE_STOPPED;
- return(-1);
- }
- states->states = tmp;
- states->maxstates *= 2;
- }
- states->states[states->nbstates].step = step;
- states->states[states->nbstates++].node = node;
- #if 0
- fprintf(stderr, "Push: %d, %s\n", step, node->name);
- #endif
- return(0);
- }
- static void
- xmlXPathFreeObjectWrapper(void *obj) {
- xmlXPathFreeObject((xmlXPathObjectPtr) obj);
- }
- /**
- * xsltTestCompMatchDirect:
- * @ctxt: a XSLT process context
- * @comp: the precompiled pattern
- * @node: a node
- * @nsList: the namespaces in scope
- * @nsNr: the number of namespaces in scope
- *
- * Test whether the node matches the pattern, do a direct evalutation
- * and not a step by step evaluation.
- *
- * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
- */
- static int
- xsltTestCompMatchDirect(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
- xmlNodePtr node, xmlNsPtr *nsList, int nsNr) {
- xsltStepOpPtr sel = NULL;
- xmlDocPtr prevdoc;
- xmlDocPtr doc;
- xmlXPathObjectPtr list;
- int ix, j;
- int nocache = 0;
- int isRVT;
- doc = node->doc;
- if (XSLT_IS_RES_TREE_FRAG(doc))
- isRVT = 1;
- else
- isRVT = 0;
- sel = &comp->steps[0]; /* store extra in first step arbitrarily */
- prevdoc = (xmlDocPtr)
- XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
- ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
- list = (xmlXPathObjectPtr)
- XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra);
- if ((list == NULL) || (prevdoc != doc)) {
- xmlXPathObjectPtr newlist;
- xmlNodePtr parent = node->parent;
- xmlDocPtr olddoc;
- xmlNodePtr oldnode;
- int oldNsNr, oldContextSize, oldProximityPosition;
- xmlNsPtr *oldNamespaces;
- oldnode = ctxt->xpathCtxt->node;
- olddoc = ctxt->xpathCtxt->doc;
- oldNsNr = ctxt->xpathCtxt->nsNr;
- oldNamespaces = ctxt->xpathCtxt->namespaces;
- oldContextSize = ctxt->xpathCtxt->contextSize;
- oldProximityPosition = ctxt->xpathCtxt->proximityPosition;
- ctxt->xpathCtxt->node = node;
- ctxt->xpathCtxt->doc = doc;
- ctxt->xpathCtxt->namespaces = nsList;
- ctxt->xpathCtxt->nsNr = nsNr;
- newlist = xmlXPathEval(comp->pattern, ctxt->xpathCtxt);
- ctxt->xpathCtxt->node = oldnode;
- ctxt->xpathCtxt->doc = olddoc;
- ctxt->xpathCtxt->namespaces = oldNamespaces;
- ctxt->xpathCtxt->nsNr = oldNsNr;
- ctxt->xpathCtxt->contextSize = oldContextSize;
- ctxt->xpathCtxt->proximityPosition = oldProximityPosition;
- if (newlist == NULL)
- return(-1);
- if (newlist->type != XPATH_NODESET) {
- xmlXPathFreeObject(newlist);
- return(-1);
- }
- ix = 0;
- if ((parent == NULL) || (node->doc == NULL) || isRVT)
- nocache = 1;
- if (nocache == 0) {
- if (list != NULL)
- xmlXPathFreeObject(list);
- list = newlist;
- XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra) =
- (void *) list;
- XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
- (void *) doc;
- XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
- 0;
- XSLT_RUNTIME_EXTRA_FREE(ctxt, sel->lenExtra) =
- xmlXPathFreeObjectWrapper;
- } else
- list = newlist;
- }
- if ((list->nodesetval == NULL) ||
- (list->nodesetval->nodeNr <= 0)) {
- if (nocache == 1)
- xmlXPathFreeObject(list);
- return(0);
- }
- /* TODO: store the index and use it for the scan */
- if (ix == 0) {
- for (j = 0;j < list->nodesetval->nodeNr;j++) {
- if (list->nodesetval->nodeTab[j] == node) {
- if (nocache == 1)
- xmlXPathFreeObject(list);
- return(1);
- }
- }
- } else {
- }
- if (nocache == 1)
- xmlXPathFreeObject(list);
- return(0);
- }
- /**
- * xsltTestPredicateMatch:
- * @ctxt: a XSLT process context
- * @comp: the precompiled pattern
- * @node: a node
- * @step: the predicate step
- * @sel: the previous step
- *
- * Test whether the node matches the predicate
- *
- * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
- */
- static int
- xsltTestPredicateMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
- xmlNodePtr node, xsltStepOpPtr step,
- xsltStepOpPtr sel) {
- xmlNodePtr oldNode;
- xmlDocPtr doc;
- int oldCS, oldCP;
- int pos = 0, len = 0;
- int isRVT;
- int match;
- if (step->value == NULL)
- return(0);
- if (step->comp == NULL)
- return(0);
- doc = node->doc;
- if (XSLT_IS_RES_TREE_FRAG(doc))
- isRVT = 1;
- else
- isRVT = 0;
- /*
- * Recompute contextSize and proximityPosition.
- *
- * TODO: Make this work for additional ops. Currently, only XSLT_OP_ELEM
- * and XSLT_OP_ALL are supported.
- */
- oldCS = ctxt->xpathCtxt->contextSize;
- oldCP = ctxt->xpathCtxt->proximityPosition;
- if ((sel != NULL) &&
- (sel->op == XSLT_OP_ELEM) &&
- (sel->value != NULL) &&
- (node->type == XML_ELEMENT_NODE) &&
- (node->parent != NULL)) {
- xmlNodePtr previous;
- int nocache = 0;
- previous = (xmlNodePtr)
- XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
- if ((previous != NULL) &&
- (previous->parent == node->parent)) {
- /*
- * just walk back to adjust the index
- */
- int indx = 0;
- xmlNodePtr sibling = node;
- while (sibling != NULL) {
- if (sibling == previous)
- break;
- if ((sibling->type == XML_ELEMENT_NODE) &&
- (previous->name != NULL) &&
- (sibling->name != NULL) &&
- (previous->name[0] == sibling->name[0]) &&
- (xmlStrEqual(previous->name, sibling->name)))
- {
- if ((sel->value2 == NULL) ||
- ((sibling->ns != NULL) &&
- (xmlStrEqual(sel->value2, sibling->ns->href))))
- indx++;
- }
- sibling = sibling->prev;
- }
- if (sibling == NULL) {
- /* hum going backward in document order ... */
- indx = 0;
- sibling = node;
- while (sibling != NULL) {
- if (sibling == previous)
- break;
- if ((sibling->type == XML_ELEMENT_NODE) &&
- (previous->name != NULL) &&
- (sibling->name != NULL) &&
- (previous->name[0] == sibling->name[0]) &&
- (xmlStrEqual(previous->name, sibling->name)))
- {
- if ((sel->value2 == NULL) ||
- ((sibling->ns != NULL) &&
- (xmlStrEqual(sel->value2,
- sibling->ns->href))))
- {
- indx--;
- }
- }
- sibling = sibling->next;
- }
- }
- if (sibling != NULL) {
- pos = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) + indx;
- /*
- * If the node is in a Value Tree we need to
- * save len, but cannot cache the node!
- * (bugs 153137 and 158840)
- */
- if (node->doc != NULL) {
- len = XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival);
- if (!isRVT) {
- XSLT_RUNTIME_EXTRA(ctxt,
- sel->previousExtra, ptr) = node;
- XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) = pos;
- }
- }
- } else
- pos = 0;
- } else {
- /*
- * recompute the index
- */
- xmlNodePtr parent = node->parent;
- xmlNodePtr siblings = NULL;
- if (parent) siblings = parent->children;
- while (siblings != NULL) {
- if (siblings->type == XML_ELEMENT_NODE) {
- if (siblings == node) {
- len++;
- pos = len;
- } else if ((node->name != NULL) &&
- (siblings->name != NULL) &&
- (node->name[0] == siblings->name[0]) &&
- (xmlStrEqual(node->name, siblings->name))) {
- if ((sel->value2 == NULL) ||
- ((siblings->ns != NULL) &&
- (xmlStrEqual(sel->value2, siblings->ns->href))))
- len++;
- }
- }
- siblings = siblings->next;
- }
- if ((parent == NULL) || (node->doc == NULL))
- nocache = 1;
- else {
- while (parent->parent != NULL)
- parent = parent->parent;
- if (((parent->type != XML_DOCUMENT_NODE) &&
- (parent->type != XML_HTML_DOCUMENT_NODE)) ||
- (parent != (xmlNodePtr) node->doc))
- nocache = 1;
- }
- }
- if (pos != 0) {
- ctxt->xpathCtxt->contextSize = len;
- ctxt->xpathCtxt->proximityPosition = pos;
- /*
- * If the node is in a Value Tree we cannot
- * cache it !
- */
- if ((!isRVT) && (node->doc != NULL) &&
- (nocache == 0)) {
- XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) = node;
- XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) = pos;
- XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival) = len;
- }
- }
- } else if ((sel != NULL) && (sel->op == XSLT_OP_ALL) &&
- (node->type == XML_ELEMENT_NODE)) {
- xmlNodePtr previous;
- int nocache = 0;
- previous = (xmlNodePtr)
- XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
- if ((previous != NULL) &&
- (previous->parent == node->parent)) {
- /*
- * just walk back to adjust the index
- */
- int indx = 0;
- xmlNodePtr sibling = node;
- while (sibling != NULL) {
- if (sibling == previous)
- break;
- if (sibling->type == XML_ELEMENT_NODE)
- indx++;
- sibling = sibling->prev;
- }
- if (sibling == NULL) {
- /* hum going backward in document order ... */
- indx = 0;
- sibling = node;
- while (sibling != NULL) {
- if (sibling == previous)
- break;
- if (sibling->type == XML_ELEMENT_NODE)
- indx--;
- sibling = sibling->next;
- }
- }
- if (sibling != NULL) {
- pos = XSLT_RUNTIME_EXTRA(ctxt,
- sel->indexExtra, ival) + indx;
- /*
- * If the node is in a Value Tree we cannot
- * cache it !
- */
- if ((node->doc != NULL) && !isRVT) {
- len = XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival);
- XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) = node;
- XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) = pos;
- }
- } else
- pos = 0;
- } else {
- /*
- * recompute the index
- */
- xmlNodePtr parent = node->parent;
- xmlNodePtr siblings = NULL;
- if (parent) siblings = parent->children;
- while (siblings != NULL) {
- if (siblings->type == XML_ELEMENT_NODE) {
- len++;
- if (siblings == node) {
- pos = len;
- }
- }
- siblings = siblings->next;
- }
- if ((parent == NULL) || (node->doc == NULL))
- nocache = 1;
- else {
- while (parent->parent != NULL)
- parent = parent->parent;
- if (((parent->type != XML_DOCUMENT_NODE) &&
- (parent->type != XML_HTML_DOCUMENT_NODE)) ||
- (parent != (xmlNodePtr) node->doc))
- nocache = 1;
- }
- }
- if (pos != 0) {
- ctxt->xpathCtxt->contextSize = len;
- ctxt->xpathCtxt->proximityPosition = pos;
- /*
- * If the node is in a Value Tree we cannot
- * cache it !
- */
- if ((node->doc != NULL) && (nocache == 0) && !isRVT) {
- XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) = node;
- XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) = pos;
- XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival) = len;
- }
- }
- }
- oldNode = ctxt->node;
- ctxt->node = node;
- match = xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList, comp->nsNr);
- if (pos != 0) {
- ctxt->xpathCtxt->contextSize = oldCS;
- ctxt->xpathCtxt->proximityPosition = oldCP;
- }
- ctxt->node = oldNode;
- return match;
- }
- /**
- * xsltTestCompMatch:
- * @ctxt: a XSLT process context
- * @comp: the precompiled pattern
- * @node: a node
- * @mode: the mode name or NULL
- * @modeURI: the mode URI or NULL
- *
- * Test whether the node matches the pattern
- *
- * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
- */
- static int
- xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
- xmlNodePtr matchNode, const xmlChar *mode,
- const xmlChar *modeURI) {
- int i;
- int found = 0;
- xmlNodePtr node = matchNode;
- xmlNodePtr oldInst;
- xsltStepOpPtr step, sel = NULL;
- xsltStepStates states = {0, 0, NULL}; /* // may require backtrack */
- if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
- xsltTransformError(ctxt, NULL, node,
- "xsltTestCompMatch: null arg\n");
- return(-1);
- }
- if (mode != NULL) {
- if (comp->mode == NULL)
- return(0);
- /*
- * both mode strings must be interned on the stylesheet dictionary
- */
- if (comp->mode != mode)
- return(0);
- } else {
- if (comp->mode != NULL)
- return(0);
- }
- if (modeURI != NULL) {
- if (comp->modeURI == NULL)
- return(0);
- /*
- * both modeURI strings must be interned on the stylesheet dictionary
- */
- if (comp->modeURI != modeURI)
- return(0);
- } else {
- if (comp->modeURI != NULL)
- return(0);
- }
- /* Some XPath functions rely on inst being set correctly. */
- oldInst = ctxt->inst;
- ctxt->inst = comp->node;
- i = 0;
- restart:
- for (;i < comp->nbStep;i++) {
- step = &comp->steps[i];
- if (step->op != XSLT_OP_PREDICATE)
- sel = step;
- switch (step->op) {
- case XSLT_OP_END:
- goto found;
- case XSLT_OP_ROOT:
- if ((node->type == XML_DOCUMENT_NODE) ||
- #ifdef LIBXML_DOCB_ENABLED
- (node->type == XML_DOCB_DOCUMENT_NODE) ||
- #endif
- (node->type == XML_HTML_DOCUMENT_NODE))
- continue;
- if ((node->type == XML_ELEMENT_NODE) && (node->name[0] == ' '))
- continue;
- goto rollback;
- case XSLT_OP_ELEM:
- if (node->type != XML_ELEMENT_NODE)
- goto rollback;
- if (step->value == NULL)
- continue;
- if (step->value[0] != node->name[0])
- goto rollback;
- if (!xmlStrEqual(step->value, node->name))
- goto rollback;
- /* Namespace test */
- if (node->ns == NULL) {
- if (step->value2 != NULL)
- goto rollback;
- } else if (node->ns->href != NULL) {
- if (step->value2 == NULL)
- goto rollback;
- if (!xmlStrEqual(step->value2, node->ns->href))
- goto rollback;
- }
- continue;
- case XSLT_OP_ATTR:
- if (node->type != XML_ATTRIBUTE_NODE)
- goto rollback;
- if (step->value != NULL) {
- if (step->value[0] != node->name[0])
- goto rollback;
- if (!xmlStrEqual(step->value, node->name))
- goto rollback;
- }
- /* Namespace test */
- if (node->ns == NULL) {
- if (step->value2 != NULL)
- goto rollback;
- } else if (step->value2 != NULL) {
- if (!xmlStrEqual(step->value2, node->ns->href))
- goto rollback;
- }
- continue;
- case XSLT_OP_PARENT:
- if ((node->type == XML_DOCUMENT_NODE) ||
- (node->type == XML_HTML_DOCUMENT_NODE) ||
- #ifdef LIBXML_DOCB_ENABLED
- (node->type == XML_DOCB_DOCUMENT_NODE) ||
- #endif
- (node->type == XML_NAMESPACE_DECL))
- goto rollback;
- node = node->parent;
- if (node == NULL)
- goto rollback;
- if (step->value == NULL)
- continue;
- if (step->value[0] != node->name[0])
- goto rollback;
- if (!xmlStrEqual(step->value, node->name))
- goto rollback;
- /* Namespace test */
- if (node->ns == NULL) {
- if (step->value2 != NULL)
- goto rollback;
- } else if (node->ns->href != NULL) {
- if (step->value2 == NULL)
- goto rollback;
- if (!xmlStrEqual(step->value2, node->ns->href))
- goto rollback;
- }
- continue;
- case XSLT_OP_ANCESTOR:
- /* TODO: implement coalescing of ANCESTOR/NODE ops */
- if (step->value == NULL) {
- step = &comp->steps[i+1];
- if (step->op == XSLT_OP_ROOT)
- goto found;
- /* added NS, ID and KEY as a result of bug 168208 */
- if ((step->op != XSLT_OP_ELEM) &&
- (step->op != XSLT_OP_ALL) &&
- (step->op != XSLT_OP_NS) &&
- (step->op != XSLT_OP_ID) &&
- (step->op != XSLT_OP_KEY))
- goto rollback;
- }
- if (node == NULL)
- goto rollback;
- if ((node->type == XML_DOCUMENT_NODE) ||
- (node->type == XML_HTML_DOCUMENT_NODE) ||
- #ifdef LIBXML_DOCB_ENABLED
- (node->type == XML_DOCB_DOCUMENT_NODE) ||
- #endif
- (node->type == XML_NAMESPACE_DECL))
- goto rollback;
- node = node->parent;
- if ((step->op != XSLT_OP_ELEM) && step->op != XSLT_OP_ALL) {
- xsltPatPushState(ctxt, &states, i, node);
- continue;
- }
- i++;
- if (step->value == NULL) {
- xsltPatPushState(ctxt, &states, i - 1, node);
- continue;
- }
- while (node != NULL) {
- if ((node->type == XML_ELEMENT_NODE) &&
- (step->value[0] == node->name[0]) &&
- (xmlStrEqual(step->value, node->name))) {
- /* Namespace test */
- if (node->ns == NULL) {
- if (step->value2 == NULL)
- break;
- } else if (node->ns->href != NULL) {
- if ((step->value2 != NULL) &&
- (xmlStrEqual(step->value2, node->ns->href)))
- break;
- }
- }
- node = node->parent;
- }
- if (node == NULL)
- goto rollback;
- xsltPatPushState(ctxt, &states, i - 1, node);
- continue;
- case XSLT_OP_ID: {
- /* TODO Handle IDs decently, must be done differently */
- xmlAttrPtr id;
- if (node->type != XML_ELEMENT_NODE)
- goto rollback;
- id = xmlGetID(node->doc, step->value);
- if ((id == NULL) || (id->parent != node))
- goto rollback;
- break;
- }
- case XSLT_OP_KEY: {
- xmlNodeSetPtr list;
- int indx;
- list = xsltGetKey(ctxt, step->value,
- step->value3, step->value2);
- if (list == NULL)
- goto rollback;
- for (indx = 0;indx < list->nodeNr;indx++)
- if (list->nodeTab[indx] == node)
- break;
- if (indx >= list->nodeNr)
- goto rollback;
- break;
- }
- case XSLT_OP_NS:
- if (node->type != XML_ELEMENT_NODE)
- goto rollback;
- if (node->ns == NULL) {
- if (step->value != NULL)
- goto rollback;
- } else if (node->ns->href != NULL) {
- if (step->value == NULL)
- goto rollback;
- if (!xmlStrEqual(step->value, node->ns->href))
- goto rollback;
- }
- break;
- case XSLT_OP_ALL:
- if (node->type != XML_ELEMENT_NODE)
- goto rollback;
- break;
- case XSLT_OP_PREDICATE: {
- /*
- * When there is cascading XSLT_OP_PREDICATE or a predicate
- * after an op which hasn't been optimized yet, then use a
- * direct computation approach. It's not done directly
- * at the beginning of the routine to filter out as much
- * as possible this costly computation.
- */
- if (comp->direct) {
- found = xsltTestCompMatchDirect(ctxt, comp, matchNode,
- comp->nsList, comp->nsNr);
- goto exit;
- }
- if (!xsltTestPredicateMatch(ctxt, comp, node, step, sel))
- goto rollback;
- break;
- }
- case XSLT_OP_PI:
- if (node->type != XML_PI_NODE)
- goto rollback;
- if (step->value != NULL) {
- if (!xmlStrEqual(step->value, node->name))
- goto rollback;
- }
- break;
- case XSLT_OP_COMMENT:
- if (node->type != XML_COMMENT_NODE)
- goto rollback;
- break;
- case XSLT_OP_TEXT:
- if ((node->type != XML_TEXT_NODE) &&
- (node->type != XML_CDATA_SECTION_NODE))
- goto rollback;
- break;
- case XSLT_OP_NODE:
- switch (node->type) {
- case XML_ELEMENT_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_PI_NODE:
- case XML_COMMENT_NODE:
- case XML_TEXT_NODE:
- break;
- default:
- goto rollback;
- }
- break;
- }
- }
- found:
- found = 1;
- exit:
- ctxt->inst = oldInst;
- if (states.states != NULL) {
- /* Free the rollback states */
- xmlFree(states.states);
- }
- return found;
- rollback:
- /* got an error try to rollback */
- if (states.states == NULL || states.nbstates <= 0) {
- found = 0;
- goto exit;
- }
- states.nbstates--;
- i = states.states[states.nbstates].step;
- node = states.states[states.nbstates].node;
- #if 0
- fprintf(stderr, "Pop: %d, %s\n", i, node->name);
- #endif
- goto restart;
- }
- /**
- * xsltTestCompMatchList:
- * @ctxt: a XSLT process context
- * @node: a node
- * @comp: the precompiled pattern list
- *
- * Test whether the node matches one of the patterns in the list
- *
- * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
- */
- int
- xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
- xsltCompMatchPtr comp) {
- int ret;
- if ((ctxt == NULL) || (node == NULL))
- return(-1);
- while (comp != NULL) {
- ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
- if (ret == 1)
- return(1);
- comp = comp->next;
- }
- return(0);
- }
- /**
- * xsltCompMatchClearCache:
- * @ctxt: a XSLT process context
- * @comp: the precompiled pattern list
- *
- * Clear pattern match cache.
- */
- void
- xsltCompMatchClearCache(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp) {
- xsltStepOpPtr sel;
- xmlXPathObjectPtr list;
- if ((ctxt == NULL) || (comp == NULL))
- return;
- sel = &comp->steps[0];
- list = (xmlXPathObjectPtr) XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra);
- if (list != NULL) {
- xmlXPathFreeObject(list);
- XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra) = NULL;
- XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) = NULL;
- XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) = 0;
- XSLT_RUNTIME_EXTRA_FREE(ctxt, sel->lenExtra) = NULL;
- }
- }
- /************************************************************************
- * *
- * Dedicated parser for templates *
- * *
- ************************************************************************/
- #define CUR (*ctxt->cur)
- #define SKIP(val) ctxt->cur += (val)
- #define NXT(val) ctxt->cur[(val)]
- #define CUR_PTR ctxt->cur
- #define SKIP_BLANKS \
- while (IS_BLANK_CH(CUR)) NEXT
- #define CURRENT (*ctxt->cur)
- #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
- #define PUSH(op, val, val2, novar) \
- if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2), (novar))) goto error;
- #define SWAP() \
- xsltSwapTopCompMatch(ctxt->comp);
- #define XSLT_ERROR(X) \
- { xsltError(ctxt, __FILE__, __LINE__, X); \
- ctxt->error = (X); return; }
- #define XSLT_ERROR0(X) \
- { xsltError(ctxt, __FILE__, __LINE__, X); \
- ctxt->error = (X); return(0); }
- /**
- * xsltScanLiteral:
- * @ctxt: the XPath Parser context
- *
- * Parse an XPath Litteral:
- *
- * [29] Literal ::= '"' [^"]* '"'
- * | "'" [^']* "'"
- *
- * Returns the Literal parsed or NULL
- */
- static xmlChar *
- xsltScanLiteral(xsltParserContextPtr ctxt) {
- const xmlChar *q, *cur;
- xmlChar *ret = NULL;
- int val, len;
- SKIP_BLANKS;
- if (CUR == '"') {
- NEXT;
- cur = q = CUR_PTR;
- val = xmlStringCurrentChar(NULL, cur, &len);
- while ((IS_CHAR(val)) && (val != '"')) {
- cur += len;
- val = xmlStringCurrentChar(NULL, cur, &len);
- }
- if (!IS_CHAR(val)) {
- ctxt->error = 1;
- return(NULL);
- } else {
- ret = xmlStrndup(q, cur - q);
- }
- cur += len;
- CUR_PTR = cur;
- } else if (CUR == '\'') {
- NEXT;
- cur = q = CUR_PTR;
- val = xmlStringCurrentChar(NULL, cur, &len);
- while ((IS_CHAR(val)) && (val != '\'')) {
- cur += len;
- val = xmlStringCurrentChar(NULL, cur, &len);
- }
- if (!IS_CHAR(val)) {
- ctxt->error = 1;
- return(NULL);
- } else {
- ret = xmlStrndup(q, cur - q);
- }
- cur += len;
- CUR_PTR = cur;
- } else {
- /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
- ctxt->error = 1;
- return(NULL);
- }
- return(ret);
- }
- /**
- * xsltScanNCName:
- * @ctxt: the XPath Parser context
- *
- * Parses a non qualified name
- *
- * Returns the Name parsed or NULL
- */
- static xmlChar *
- xsltScanNCName(xsltParserContextPtr ctxt) {
- const xmlChar *q, *cur;
- xmlChar *ret = NULL;
- int val, len;
- SKIP_BLANKS;
- cur = q = CUR_PTR;
- val = xmlStringCurrentChar(NULL, cur, &len);
- if (!IS_LETTER(val) && (val != '_'))
- return(NULL);
- while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
- (val == '.') || (val == '-') ||
- (val == '_') ||
- (IS_COMBINING(val)) ||
- (IS_EXTENDER(val))) {
- cur += len;
- val = xmlStringCurrentChar(NULL, cur, &len);
- }
- ret = xmlStrndup(q, cur - q);
- CUR_PTR = cur;
- return(ret);
- }
- /*
- * xsltCompileIdKeyPattern:
- * @ctxt: the compilation context
- * @name: a preparsed name
- * @aid: whether id/key are allowed there
- * @novar: flag to prohibit xslt var
- *
- * Compile the XSLT LocationIdKeyPattern
- * [3] IdKeyPattern ::= 'id' '(' Literal ')'
- * | 'key' '(' Literal ',' Literal ')'
- *
- * also handle NodeType and PI from:
- *
- * [7] NodeTest ::= NameTest
- * | NodeType '(' ')'
- * | 'processing-instruction' '(' Literal ')'
- */
- static void
- xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name,
- int aid, int novar, xsltAxis axis) {
- xmlChar *lit = NULL;
- xmlChar *lit2 = NULL;
- if (CUR != '(') {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : ( expected\n");
- ctxt->error = 1;
- return;
- }
- if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
- if (axis != 0) {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : NodeTest expected\n");
- ctxt->error = 1;
- return;
- }
- NEXT;
- SKIP_BLANKS;
- lit = xsltScanLiteral(ctxt);
- if (ctxt->error) {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : Literal expected\n");
- return;
- }
- SKIP_BLANKS;
- if (CUR != ')') {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : ) expected\n");
- xmlFree(lit);
- ctxt->error = 1;
- return;
- }
- NEXT;
- PUSH(XSLT_OP_ID, lit, NULL, novar);
- lit = NULL;
- } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
- if (axis != 0) {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : NodeTest expected\n");
- ctxt->error = 1;
- return;
- }
- NEXT;
- SKIP_BLANKS;
- lit = xsltScanLiteral(ctxt);
- if (ctxt->error) {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : Literal expected\n");
- return;
- }
- SKIP_BLANKS;
- if (CUR != ',') {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : , expected\n");
- xmlFree(lit);
- ctxt->error = 1;
- return;
- }
- NEXT;
- SKIP_BLANKS;
- lit2 = xsltScanLiteral(ctxt);
- if (ctxt->error) {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : Literal expected\n");
- xmlFree(lit);
- return;
- }
- SKIP_BLANKS;
- if (CUR != ')') {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : ) expected\n");
- xmlFree(lit);
- xmlFree(lit2);
- ctxt->error = 1;
- return;
- }
- NEXT;
- /* URGENT TODO: support namespace in keys */
- PUSH(XSLT_OP_KEY, lit, lit2, novar);
- lit = NULL;
- lit2 = NULL;
- } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
- NEXT;
- SKIP_BLANKS;
- if (CUR != ')') {
- lit = xsltScanLiteral(ctxt);
- if (ctxt->error) {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : Literal expected\n");
- return;
- }
- SKIP_BLANKS;
- if (CUR != ')') {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : ) expected\n");
- ctxt->error = 1;
- xmlFree(lit);
- return;
- }
- }
- NEXT;
- PUSH(XSLT_OP_PI, lit, NULL, novar);
- lit = NULL;
- } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
- NEXT;
- SKIP_BLANKS;
- if (CUR != ')') {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : ) expected\n");
- ctxt->error = 1;
- return;
- }
- NEXT;
- PUSH(XSLT_OP_TEXT, NULL, NULL, novar);
- } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
- NEXT;
- SKIP_BLANKS;
- if (CUR != ')') {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : ) expected\n");
- ctxt->error = 1;
- return;
- }
- NEXT;
- PUSH(XSLT_OP_COMMENT, NULL, NULL, novar);
- } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
- NEXT;
- SKIP_BLANKS;
- if (CUR != ')') {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : ) expected\n");
- ctxt->error = 1;
- return;
- }
- NEXT;
- if (axis == AXIS_ATTRIBUTE) {
- PUSH(XSLT_OP_ATTR, NULL, NULL, novar);
- }
- else {
- PUSH(XSLT_OP_NODE, NULL, NULL, novar);
- }
- } else if (aid) {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
- ctxt->error = 1;
- return;
- } else {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileIdKeyPattern : node type\n");
- ctxt->error = 1;
- return;
- }
- error:
- return;
- }
- /**
- * xsltCompileStepPattern:
- * @ctxt: the compilation context
- * @token: a posible precompiled name
- * @novar: flag to prohibit xslt variables from pattern
- *
- * Compile the XSLT StepPattern and generates a precompiled
- * form suitable for fast matching.
- *
- * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
- * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
- * | ('child' | 'attribute') '::'
- * from XPath
- * [7] NodeTest ::= NameTest
- * | NodeType '(' ')'
- * | 'processing-instruction' '(' Literal ')'
- * [8] Predicate ::= '[' PredicateExpr ']'
- * [9] PredicateExpr ::= Expr
- * [13] AbbreviatedAxisSpecifier ::= '@'?
- * [37] NameTest ::= '*' | NCName ':' '*' | QName
- */
- static void
- xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token, int novar) {
- xmlChar *name = NULL;
- const xmlChar *URI = NULL;
- xmlChar *URL = NULL;
- int level;
- xsltAxis axis = 0;
- SKIP_BLANKS;
- if ((token == NULL) && (CUR == '@')) {
- NEXT;
- axis = AXIS_ATTRIBUTE;
- }
- parse_node_test:
- if (token == NULL)
- token = xsltScanNCName(ctxt);
- if (token == NULL) {
- if (CUR == '*') {
- NEXT;
- if (axis == AXIS_ATTRIBUTE) {
- PUSH(XSLT_OP_ATTR, NULL, NULL, novar);
- }
- else {
- PUSH(XSLT_OP_ALL, NULL, NULL, novar);
- }
- goto parse_predicate;
- } else {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileStepPattern : Name expected\n");
- ctxt->error = 1;
- goto error;
- }
- }
- SKIP_BLANKS;
- if (CUR == '(') {
- xsltCompileIdKeyPattern(ctxt, token, 0, novar, axis);
- xmlFree(token);
- token = NULL;
- if (ctxt->error)
- goto error;
- } else if (CUR == ':') {
- NEXT;
- if (CUR != ':') {
- xmlChar *prefix = token;
- xmlNsPtr ns;
- /*
- * This is a namespace match
- */
- token = xsltScanNCName(ctxt);
- ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
- if (ns == NULL) {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileStepPattern : no namespace bound to prefix %s\n",
- prefix);
- xmlFree(prefix);
- prefix=NULL;
- ctxt->error = 1;
- goto error;
- } else {
- URL = xmlStrdup(ns->href);
- }
- xmlFree(prefix);
- prefix=NULL;
- if (token == NULL) {
- if (CUR == '*') {
- NEXT;
- if (axis == AXIS_ATTRIBUTE) {
- PUSH(XSLT_OP_ATTR, NULL, URL, novar);
- URL = NULL;
- }
- else {
- PUSH(XSLT_OP_NS, URL, NULL, novar);
- URL = NULL;
- }
- } else {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileStepPattern : Name expected\n");
- ctxt->error = 1;
- xmlFree(URL);
- goto error;
- }
- } else {
- if (axis == AXIS_ATTRIBUTE) {
- PUSH(XSLT_OP_ATTR, token, URL, novar);
- token = NULL;
- URL = NULL;
- }
- else {
- PUSH(XSLT_OP_ELEM, token, URL, novar);
- token = NULL;
- URL = NULL;
- }
- }
- } else {
- if (axis != 0) {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileStepPattern : NodeTest expected\n");
- ctxt->error = 1;
- goto error;
- }
- NEXT;
- if (xmlStrEqual(token, (const xmlChar *) "child")) {
- axis = AXIS_CHILD;
- } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
- axis = AXIS_ATTRIBUTE;
- } else {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
- ctxt->error = 1;
- goto error;
- }
- xmlFree(token);
- token = NULL;
- SKIP_BLANKS;
- token = xsltScanNCName(ctxt);
- goto parse_node_test;
- }
- } else {
- URI = xsltGetQNameURI(ctxt->elem, &token);
- if (token == NULL) {
- ctxt->error = 1;
- goto error;
- }
- if (URI != NULL)
- URL = xmlStrdup(URI);
- if (axis == AXIS_ATTRIBUTE) {
- PUSH(XSLT_OP_ATTR, token, URL, novar);
- token = NULL;
- URL = NULL;
- }
- else {
- PUSH(XSLT_OP_ELEM, token, URL, novar);
- token = NULL;
- URL = NULL;
- }
- }
- parse_predicate:
- SKIP_BLANKS;
- level = 0;
- while (CUR == '[') {
- const xmlChar *q;
- xmlChar *ret = NULL;
- level++;
- NEXT;
- q = CUR_PTR;
- while (CUR != 0) {
- /* Skip over nested predicates */
- if (CUR == '[')
- level++;
- else if (CUR == ']') {
- level--;
- if (level == 0)
- break;
- } else if (CUR == '"') {
- NEXT;
- while ((CUR != 0) && (CUR != '"'))
- NEXT;
- } else if (CUR == '\'') {
- NEXT;
- while ((CUR != 0) && (CUR != '\''))
- NEXT;
- }
- NEXT;
- }
- if (CUR == 0) {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileStepPattern : ']' expected\n");
- ctxt->error = 1;
- return;
- }
- ret = xmlStrndup(q, CUR_PTR - q);
- PUSH(XSLT_OP_PREDICATE, ret, NULL, novar);
- ret = NULL;
- /* push the predicate lower than local test */
- SWAP();
- NEXT;
- SKIP_BLANKS;
- }
- return;
- error:
- if (token != NULL)
- xmlFree(token);
- if (name != NULL)
- xmlFree(name);
- }
- /**
- * xsltCompileRelativePathPattern:
- * @comp: the compilation context
- * @token: a posible precompiled name
- * @novar: flag to prohibit xslt variables
- *
- * Compile the XSLT RelativePathPattern and generates a precompiled
- * form suitable for fast matching.
- *
- * [4] RelativePathPattern ::= StepPattern
- * | RelativePathPattern '/' StepPattern
- * | RelativePathPattern '//' StepPattern
- */
- static void
- xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token, int novar) {
- xsltCompileStepPattern(ctxt, token, novar);
- if (ctxt->error)
- goto error;
- SKIP_BLANKS;
- while ((CUR != 0) && (CUR != '|')) {
- if ((CUR == '/') && (NXT(1) == '/')) {
- PUSH(XSLT_OP_ANCESTOR, NULL, NULL, novar);
- NEXT;
- NEXT;
- SKIP_BLANKS;
- xsltCompileStepPattern(ctxt, NULL, novar);
- } else if (CUR == '/') {
- PUSH(XSLT_OP_PARENT, NULL, NULL, novar);
- NEXT;
- SKIP_BLANKS;
- xsltCompileStepPattern(ctxt, NULL, novar);
- } else {
- ctxt->error = 1;
- }
- if (ctxt->error)
- goto error;
- SKIP_BLANKS;
- }
- error:
- return;
- }
- /**
- * xsltCompileLocationPathPattern:
- * @ctxt: the compilation context
- * @novar: flag to prohibit xslt variables
- *
- * Compile the XSLT LocationPathPattern and generates a precompiled
- * form suitable for fast matching.
- *
- * [2] LocationPathPattern ::= '/' RelativePathPattern?
- * | IdKeyPattern (('/' | '//') RelativePathPattern)?
- * | '//'? RelativePathPattern
- */
- static void
- xsltCompileLocationPathPattern(xsltParserContextPtr ctxt, int novar) {
- SKIP_BLANKS;
- if ((CUR == '/') && (NXT(1) == '/')) {
- /*
- * since we reverse the query
- * a leading // can be safely ignored
- */
- NEXT;
- NEXT;
- ctxt->comp->priority = 0.5; /* '//' means not 0 priority */
- xsltCompileRelativePathPattern(ctxt, NULL, novar);
- } else if (CUR == '/') {
- /*
- * We need to find root as the parent
- */
- NEXT;
- SKIP_BLANKS;
- PUSH(XSLT_OP_ROOT, NULL, NULL, novar);
- if ((CUR != 0) && (CUR != '|')) {
- PUSH(XSLT_OP_PARENT, NULL, NULL, novar);
- xsltCompileRelativePathPattern(ctxt, NULL, novar);
- }
- } else if (CUR == '*') {
- xsltCompileRelativePathPattern(ctxt, NULL, novar);
- } else if (CUR == '@') {
- xsltCompileRelativePathPattern(ctxt, NULL, novar);
- } else {
- xmlChar *name;
- name = xsltScanNCName(ctxt);
- if (name == NULL) {
- xsltTransformError(NULL, NULL, NULL,
- "xsltCompileLocationPathPattern : Name expected\n");
- ctxt->error = 1;
- return;
- }
- SKIP_BLANKS;
- if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
- xsltCompileIdKeyPattern(ctxt, name, 1, novar, 0);
- xmlFree(name);
- name = NULL;
- if (ctxt->error)
- return;
- if ((CUR == '/') && (NXT(1) == '/')) {
- PUSH(XSLT_OP_ANCESTOR, NULL, NULL, novar);
- NEXT;
- NEXT;
- SKIP_BLANKS;
- xsltCompileRelativePathPattern(ctxt, NULL, novar);
- } else if (CUR == '/') {
- PUSH(XSLT_OP_PARENT, NULL, NULL, novar);
- NEXT;
- SKIP_BLANKS;
- xsltCompileRelativePathPattern(ctxt, NULL, novar);
- }
- return;
- }
- xsltCompileRelativePathPattern(ctxt, name, novar);
- }
- error:
- return;
- }
- /**
- * xsltCompilePatternInternal:
- * @pattern: an XSLT pattern
- * @doc: the containing document
- * @node: the containing element
- * @style: the stylesheet
- * @runtime: the transformation context, if done at run-time
- * @novar: flag to prohibit xslt variables
- *
- * Compile the XSLT pattern and generates a list of precompiled form suitable
- * for fast matching.
- *
- * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
- *
- * Returns the generated pattern list or NULL in case of failure
- */
- static xsltCompMatchPtr
- xsltCompilePatternInternal(const xmlChar *pattern, xmlDocPtr doc,
- xmlNodePtr node, xsltStylesheetPtr style,
- xsltTransformContextPtr runtime, int novar) {
- xsltParserContextPtr ctxt = NULL;
- xsltCompMatchPtr element, first = NULL, previous = NULL;
- int current, start, end, level, j;
- if (pattern == NULL) {
- xsltTransformError(NULL, NULL, node,
- "xsltCompilePattern : NULL pattern\n");
- return(NULL);
- }
- ctxt = xsltNewParserContext(style, runtime);
- if (ctxt == NULL)
- return(NULL);
- ctxt->doc = doc;
- ctxt->elem = node;
- current = end = 0;
- while (pattern[current] != 0) {
- start = current;
- while (IS_BLANK_CH(pattern[current]))
- current++;
- end = current;
- level = 0;
- while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
- if (pattern[end] == '[')
- level++;
- else if (pattern[end] == ']')
- level--;
- else if (pattern[end] == '\'') {
- end++;
- while ((pattern[end] != 0) && (pattern[end] != '\''))
- end++;
- } else if (pattern[end] == '"') {
- end++;
- while ((pattern[end] != 0) && (pattern[end] != '"'))
- end++;
- }
- if (pattern[end] == 0)
- break;
- end++;
- }
- if (current == end) {
- xsltTransformError(NULL, NULL, node,
- "xsltCompilePattern : NULL pattern\n");
- goto error;
- }
- element = xsltNewCompMatch();
- if (element == NULL) {
- goto error;
- }
- if (first == NULL)
- first = element;
- else if (previous != NULL)
- previous->next = element;
- previous = element;
- ctxt->comp = element;
- ctxt->base = xmlStrndup(&pattern[start], end - start);
- if (ctxt->base == NULL)
- goto error;
- ctxt->cur = &(ctxt->base)[current - start];
- element->pattern = ctxt->base;
- element->node = node;
- element->nsList = xmlGetNsList(doc, node);
- j = 0;
- if (element->nsList != NULL) {
- while (element->nsList[j] != NULL)
- j++;
- }
- element->nsNr = j;
- #ifdef WITH_XSLT_DEBUG_PATTERN
- xsltGenericDebug(xsltGenericDebugContext,
- "xsltCompilePattern : parsing '%s'\n",
- element->pattern);
- #endif
- /*
- Preset default priority to be zero.
- This may be changed by xsltCompileLocationPathPattern.
- */
- element->priority = 0;
- xsltCompileLocationPathPattern(ctxt, novar);
- if (ctxt->error) {
- xsltTransformError(NULL, style, node,
- "xsltCompilePattern : failed to compile '%s'\n",
- element->pattern);
- if (style != NULL) style->errors++;
- goto error;
- }
- /*
- * Reverse for faster interpretation.
- */
- xsltReverseCompMatch(ctxt, element);
- /*
- * Set-up the priority
- */
- if (element->priority == 0) { /* if not yet determined */
- if (((element->steps[0].op == XSLT_OP_ELEM) ||
- (element->steps[0].op == XSLT_OP_ATTR) ||
- (element->steps[0].op == XSLT_OP_PI)) &&
- (element->steps[0].value != NULL) &&
- (element->steps[1].op == XSLT_OP_END)) {
- ; /* previously preset */
- } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
- (element->steps[0].value2 != NULL) &&
- (element->steps[1].op == XSLT_OP_END)) {
- element->priority = -0.25;
- } else if ((element->steps[0].op == XSLT_OP_NS) &&
- (element->steps[0].value != NULL) &&
- (element->steps[1].op == XSLT_OP_END)) {
- element->priority = -0.25;
- } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
- (element->steps[0].value == NULL) &&
- (element->steps[0].value2 == NULL) &&
- (element->steps[1].op == XSLT_OP_END)) {
- element->priority = -0.5;
- } else if (((element->steps[0].op == XSLT_OP_PI) ||
- (element->steps[0].op == XSLT_OP_TEXT) ||
- (element->steps[0].op == XSLT_OP_ALL) ||
- (element->steps[0].op == XSLT_OP_NODE) ||
- (element->steps[0].op == XSLT_OP_COMMENT)) &&
- (element->steps[1].op == XSLT_OP_END)) {
- element->priority = -0.5;
- } else {
- element->priority = 0.5;
- }
- }
- #ifdef WITH_XSLT_DEBUG_PATTERN
- xsltGenericDebug(xsltGenericDebugContext,
- "xsltCompilePattern : parsed %s, default priority %f\n",
- element->pattern, element->priority);
- #endif
- if (pattern[end] == '|')
- end++;
- current = end;
- }
- if (end == 0) {
- xsltTransformError(NULL, style, node,
- "xsltCompilePattern : NULL pattern\n");
- if (style != NULL) style->errors++;
- goto error;
- }
- xsltFreeParserContext(ctxt);
- return(first);
- error:
- if (ctxt != NULL)
- xsltFreeParserContext(ctxt);
- if (first != NULL)
- xsltFreeCompMatchList(first);
- return(NULL);
- }
- /**
- * xsltCompilePattern:
- * @pattern: an XSLT pattern
- * @doc: the containing document
- * @node: the containing element
- * @style: the stylesheet
- * @runtime: the transformation context, if done at run-time
- *
- * Compile the XSLT pattern and generates a list of precompiled form suitable
- * for fast matching.
- *
- * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
- *
- * Returns the generated pattern list or NULL in case of failure
- */
- xsltCompMatchPtr
- xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
- xmlNodePtr node, xsltStylesheetPtr style,
- xsltTransformContextPtr runtime) {
- return (xsltCompilePatternInternal(pattern, doc, node, style, runtime, 0));
- }
- /************************************************************************
- * *
- * Module interfaces *
- * *
- ************************************************************************/
- /**
- * xsltAddTemplate:
- * @style: an XSLT stylesheet
- * @cur: an XSLT template
- * @mode: the mode name or NULL
- * @modeURI: the mode URI or NULL
- *
- * Register the XSLT pattern associated to @cur
- *
- * Returns -1 in case of error, 0 otherwise
- */
- int
- xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
- const xmlChar *mode, const xmlChar *modeURI) {
- xsltCompMatchPtr pat, list, next;
- /*
- * 'top' will point to style->xxxMatch ptr - declaring as 'void'
- * avoids gcc 'type-punned pointer' warning.
- */
- void **top = NULL;
- const xmlChar *name = NULL;
- float priority; /* the priority */
- if ((style == NULL) || (cur == NULL))
- return(-1);
- /* Register named template */
- if (cur->name != NULL) {
- if (style->namedTemplates == NULL) {
- style->namedTemplates = xmlHashCreate(10);
- if (style->namedTemplates == NULL)
- return(-1);
- }
- else {
- void *dup = xmlHashLookup2(style->namedTemplates, cur->name,
- cur->nameURI);
- if (dup != NULL) {
- xsltTransformError(NULL, style, cur->elem,
- "xsl:template: error duplicate name '%s'\n",
- cur->name);
- style->errors++;
- return(-1);
- }
- }
- xmlHashAddEntry2(style->namedTemplates, cur->name, cur->nameURI, cur);
- }
- if (cur->match == NULL) {
- if (cur->name == NULL) {
- xsltTransformError(NULL, style, cur->elem,
- "xsl:template: need to specify match or name attribute\n");
- style->errors++;
- return(-1);
- }
- return(0);
- }
- priority = cur->priority;
- pat = xsltCompilePatternInternal(cur->match, style->doc, cur->elem,
- style, NULL, 1);
- if (pat == NULL)
- return(-1);
- while (pat) {
- next = pat->next;
- pat->next = NULL;
- name = NULL;
- pat->template = cur;
- if (mode != NULL)
- pat->mode = xmlDictLookup(style->dict, mode, -1);
- if (modeURI != NULL)
- pat->modeURI = xmlDictLookup(style->dict, modeURI, -1);
- if (priority != XSLT_PAT_NO_PRIORITY)
- pat->priority = priority;
- /*
- * insert it in the hash table list corresponding to its lookup name
- */
- switch (pat->steps[0].op) {
- case XSLT_OP_ATTR:
- if (pat->steps[0].value != NULL)
- name = pat->steps[0].value;
- else
- top = &(style->attrMatch);
- break;
- case XSLT_OP_PARENT:
- case XSLT_OP_ANCESTOR:
- top = &(style->elemMatch);
- break;
- case XSLT_OP_ROOT:
- top = &(style->rootMatch);
- break;
- case XSLT_OP_KEY:
- top = &(style->keyMatch);
- break;
- case XSLT_OP_ID:
- /* TODO optimize ID !!! */
- case XSLT_OP_NS:
- case XSLT_OP_ALL:
- top = &(style->elemMatch);
- break;
- case XSLT_OP_END:
- case XSLT_OP_PREDICATE:
- xsltTransformError(NULL, style, NULL,
- "xsltAddTemplate: invalid compiled pattern\n");
- xsltFreeCompMatch(pat);
- return(-1);
- /*
- * TODO: some flags at the top level about type based patterns
- * would be faster than inclusion in the hash table.
- */
- case XSLT_OP_PI:
- if (pat->steps[0].value != NULL)
- name = pat->steps[0].value;
- else
- top = &(style->piMatch);
- break;
- case XSLT_OP_COMMENT:
- top = &(style->commentMatch);
- break;
- case XSLT_OP_TEXT:
- top = &(style->textMatch);
- break;
- case XSLT_OP_ELEM:
- case XSLT_OP_NODE:
- if (pat->steps[0].value != NULL)
- name = pat->steps[0].value;
- else
- top = &(style->elemMatch);
- break;
- }
- if (name != NULL) {
- if (style->templatesHash == NULL) {
- style->templatesHash = xmlHashCreate(1024);
- if (style->templatesHash == NULL) {
- xsltFreeCompMatch(pat);
- return(-1);
- }
- xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
- } else {
- list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
- name, mode, modeURI);
- if (list == NULL) {
- xmlHashAddEntry3(style->templatesHash, name,
- mode, modeURI, pat);
- } else {
- /*
- * Note '<=' since one must choose among the matching
- * template rules that are left, the one that occurs
- * last in the stylesheet
- */
- if (list->priority <= pat->priority) {
- pat->next = list;
- xmlHashUpdateEntry3(style->templatesHash, name,
- mode, modeURI, pat, NULL);
- } else {
- while (list->next != NULL) {
- if (list->next->priority <= pat->priority)
- break;
- list = list->next;
- }
- pat->next = list->next;
- list->next = pat;
- }
- }
- }
- } else if (top != NULL) {
- list = *top;
- if (list == NULL) {
- *top = pat;
- pat->next = NULL;
- } else if (list->priority <= pat->priority) {
- pat->next = list;
- *top = pat;
- } else {
- while (list->next != NULL) {
- if (list->next->priority <= pat->priority)
- break;
- list = list->next;
- }
- pat->next = list->next;
- list->next = pat;
- }
- } else {
- xsltTransformError(NULL, style, NULL,
- "xsltAddTemplate: invalid compiled pattern\n");
- xsltFreeCompMatch(pat);
- return(-1);
- }
- #ifdef WITH_XSLT_DEBUG_PATTERN
- if (mode)
- xsltGenericDebug(xsltGenericDebugContext,
- "added pattern : '%s' mode '%s' priority %f\n",
- pat->pattern, pat->mode, pat->priority);
- else
- xsltGenericDebug(xsltGenericDebugContext,
- "added pattern : '%s' priority %f\n",
- pat->pattern, pat->priority);
- #endif
- pat = next;
- }
- return(0);
- }
- static int
- xsltComputeAllKeys(xsltTransformContextPtr ctxt, xmlNodePtr contextNode)
- {
- if ((ctxt == NULL) || (contextNode == NULL)) {
- xsltTransformError(ctxt, NULL, ctxt->inst,
- "Internal error in xsltComputeAllKeys(): "
- "Bad arguments.\n");
- return(-1);
- }
- if (ctxt->document == NULL) {
- /*
- * The document info will only be NULL if we have a RTF.
- */
- if (contextNode->doc->_private != NULL)
- goto doc_info_mismatch;
- /*
- * On-demand creation of the document info (needed for keys).
- */
- ctxt->document = xsltNewDocument(ctxt, contextNode->doc);
- if (ctxt->document == NULL)
- return(-1);
- }
- return xsltInitAllDocKeys(ctxt);
- doc_info_mismatch:
- xsltTransformError(ctxt, NULL, ctxt->inst,
- "Internal error in xsltComputeAllKeys(): "
- "The context's document info doesn't match the "
- "document info of the current result tree.\n");
- ctxt->state = XSLT_STATE_STOPPED;
- return(-1);
- }
- /**
- * xsltGetTemplate:
- * @ctxt: a XSLT process context
- * @node: the node being processed
- * @style: the current style
- *
- * Finds the template applying to this node, if @style is non-NULL
- * it means one needs to look for the next imported template in scope.
- *
- * Returns the xsltTemplatePtr or NULL if not found
- */
- xsltTemplatePtr
- xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
- xsltStylesheetPtr style)
- {
- xsltStylesheetPtr curstyle;
- xsltTemplatePtr ret = NULL;
- const xmlChar *name = NULL;
- xsltCompMatchPtr list = NULL;
- float priority;
- int keyed = 0;
- if ((ctxt == NULL) || (node == NULL))
- return(NULL);
- if (style == NULL) {
- curstyle = ctxt->style;
- } else {
- curstyle = xsltNextImport(style);
- }
- while ((curstyle != NULL) && (curstyle != style)) {
- priority = XSLT_PAT_NO_PRIORITY;
- /* TODO : handle IDs/keys here ! */
- if (curstyle->templatesHash != NULL) {
- /*
- * Use the top name as selector
- */
- switch (node->type) {
- case XML_ELEMENT_NODE:
- if (node->name[0] == ' ')
- break;
- /* Intentional fall-through */
- case XML_ATTRIBUTE_NODE:
- case XML_PI_NODE:
- name = node->name;
- break;
- case XML_DOCUMENT_NODE:
- case XML_HTML_DOCUMENT_NODE:
- case XML_TEXT_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_COMMENT_NODE:
- case XML_ENTITY_REF_NODE:
- case XML_ENTITY_NODE:
- case XML_DOCUMENT_TYPE_NODE:
- case XML_DOCUMENT_FRAG_NODE:
- case XML_NOTATION_NODE:
- case XML_DTD_NODE:
- case XML_ELEMENT_DECL:
- case XML_ATTRIBUTE_DECL:
- case XML_ENTITY_DECL:
- case XML_NAMESPACE_DECL:
- case XML_XINCLUDE_START:
- case XML_XINCLUDE_END:
- break;
- default:
- return(NULL);
- }
- }
- if (name != NULL) {
- /*
- * find the list of applicable expressions based on the name
- */
- list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
- name, ctxt->mode, ctxt->modeURI);
- } else
- list = NULL;
- while (list != NULL) {
- if (xsltTestCompMatch(ctxt, list, node,
- ctxt->mode, ctxt->modeURI) == 1) {
- ret = list->template;
- priority = list->priority;
- break;
- }
- list = list->next;
- }
- list = NULL;
- /*
- * find alternate generic matches
- */
- switch (node->type) {
- case XML_ELEMENT_NODE:
- if (node->name[0] == ' ')
- list = curstyle->rootMatch;
- else
- list = curstyle->elemMatch;
- if (node->psvi != NULL) keyed = 1;
- break;
- case XML_ATTRIBUTE_NODE: {
- xmlAttrPtr attr;
- list = curstyle->attrMatch;
- attr = (xmlAttrPtr) node;
- if (attr->psvi != NULL) keyed = 1;
- break;
- }
- case XML_PI_NODE:
- list = curstyle->piMatch;
- if (node->psvi != NULL) keyed = 1;
- break;
- case XML_DOCUMENT_NODE:
- case XML_HTML_DOCUMENT_NODE: {
- xmlDocPtr doc;
- list = curstyle->rootMatch;
- doc = (xmlDocPtr) node;
- if (doc->psvi != NULL) keyed = 1;
- break;
- }
- case XML_TEXT_NODE:
- case XML_CDATA_SECTION_NODE:
- list = curstyle->textMatch;
- if (node->psvi != NULL) keyed = 1;
- break;
- case XML_COMMENT_NODE:
- list = curstyle->commentMatch;
- if (node->psvi != NULL) keyed = 1;
- break;
- case XML_ENTITY_REF_NODE:
- case XML_ENTITY_NODE:
- case XML_DOCUMENT_TYPE_NODE:
- case XML_DOCUMENT_FRAG_NODE:
- case XML_NOTATION_NODE:
- case XML_DTD_NODE:
- case XML_ELEMENT_DECL:
- case XML_ATTRIBUTE_DECL:
- case XML_ENTITY_DECL:
- case XML_NAMESPACE_DECL:
- case XML_XINCLUDE_START:
- case XML_XINCLUDE_END:
- break;
- default:
- break;
- }
- while ((list != NULL) &&
- ((ret == NULL) || (list->priority > priority))) {
- if (xsltTestCompMatch(ctxt, list, node,
- ctxt->mode, ctxt->modeURI) == 1) {
- ret = list->template;
- priority = list->priority;
- break;
- }
- list = list->next;
- }
- /*
- * Some of the tests for elements can also apply to documents
- */
- if ((node->type == XML_DOCUMENT_NODE) ||
- (node->type == XML_HTML_DOCUMENT_NODE) ||
- (node->type == XML_TEXT_NODE)) {
- list = curstyle->elemMatch;
- while ((list != NULL) &&
- ((ret == NULL) || (list->priority > priority))) {
- if (xsltTestCompMatch(ctxt, list, node,
- ctxt->mode, ctxt->modeURI) == 1) {
- ret = list->template;
- priority = list->priority;
- break;
- }
- list = list->next;
- }
- } else if ((node->type == XML_PI_NODE) ||
- (node->type == XML_COMMENT_NODE)) {
- list = curstyle->elemMatch;
- while ((list != NULL) &&
- ((ret == NULL) || (list->priority > priority))) {
- if (xsltTestCompMatch(ctxt, list, node,
- ctxt->mode, ctxt->modeURI) == 1) {
- ret = list->template;
- priority = list->priority;
- break;
- }
- list = list->next;
- }
- }
- keyed_match:
- if (keyed) {
- list = curstyle->keyMatch;
- while ((list != NULL) &&
- ((ret == NULL) || (list->priority > priority))) {
- if (xsltTestCompMatch(ctxt, list, node,
- ctxt->mode, ctxt->modeURI) == 1) {
- ret = list->template;
- priority = list->priority;
- break;
- }
- list = list->next;
- }
- }
- else if (ctxt->hasTemplKeyPatterns &&
- ((ctxt->document == NULL) ||
- (ctxt->document->nbKeysComputed < ctxt->nbKeys)))
- {
- /*
- * Compute all remaining keys for this document.
- *
- * REVISIT TODO: I think this could be further optimized.
- */
- if (xsltComputeAllKeys(ctxt, node) == -1)
- goto error;
- switch (node->type) {
- case XML_ELEMENT_NODE:
- if (node->psvi != NULL) keyed = 1;
- break;
- case XML_ATTRIBUTE_NODE:
- if (((xmlAttrPtr) node)->psvi != NULL) keyed = 1;
- break;
- case XML_TEXT_NODE:
- case XML_CDATA_SECTION_NODE:
- case XML_COMMENT_NODE:
- case XML_PI_NODE:
- if (node->psvi != NULL) keyed = 1;
- break;
- case XML_DOCUMENT_NODE:
- case XML_HTML_DOCUMENT_NODE:
- if (((xmlDocPtr) node)->psvi != NULL) keyed = 1;
- break;
- default:
- break;
- }
- if (keyed)
- goto keyed_match;
- }
- if (ret != NULL)
- return(ret);
- /*
- * Cycle on next curstylesheet import.
- */
- curstyle = xsltNextImport(curstyle);
- }
- error:
- return(NULL);
- }
- /**
- * xsltCleanupTemplates:
- * @style: an XSLT stylesheet
- *
- * Cleanup the state of the templates used by the stylesheet and
- * the ones it imports.
- */
- void
- xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
- }
- /**
- * xsltFreeTemplateHashes:
- * @style: an XSLT stylesheet
- *
- * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
- */
- void
- xsltFreeTemplateHashes(xsltStylesheetPtr style) {
- if (style->templatesHash != NULL)
- xmlHashFree((xmlHashTablePtr) style->templatesHash,
- xsltFreeCompMatchListEntry);
- if (style->rootMatch != NULL)
- xsltFreeCompMatchList(style->rootMatch);
- if (style->keyMatch != NULL)
- xsltFreeCompMatchList(style->keyMatch);
- if (style->elemMatch != NULL)
- xsltFreeCompMatchList(style->elemMatch);
- if (style->attrMatch != NULL)
- xsltFreeCompMatchList(style->attrMatch);
- if (style->parentMatch != NULL)
- xsltFreeCompMatchList(style->parentMatch);
- if (style->textMatch != NULL)
- xsltFreeCompMatchList(style->textMatch);
- if (style->piMatch != NULL)
- xsltFreeCompMatchList(style->piMatch);
- if (style->commentMatch != NULL)
- xsltFreeCompMatchList(style->commentMatch);
- if (style->namedTemplates != NULL)
- xmlHashFree(style->namedTemplates, NULL);
- }
|