Minor changes for ATAPI Srb Functions
[reactos.git] / dll / 3rdparty / libxslt / pattern.c
1 /*
2 * pattern.c: Implemetation of the template match compilation and lookup
3 *
4 * Reference:
5 * http://www.w3.org/TR/1999/REC-xslt-19991116
6 *
7 * See Copyright for the status of this software.
8 *
9 * daniel@veillard.com
10 */
11
12 /*
13 * TODO: handle pathological cases like *[*[@a="b"]]
14 * TODO: detect [number] at compilation, optimize accordingly
15 */
16
17 #include "precomp.h"
18
19 #ifdef WITH_XSLT_DEBUG
20 #define WITH_XSLT_DEBUG_PATTERN
21 #endif
22
23 /*
24 * Types are private:
25 */
26
27 typedef enum {
28 XSLT_OP_END=0,
29 XSLT_OP_ROOT,
30 XSLT_OP_ELEM,
31 XSLT_OP_ATTR,
32 XSLT_OP_PARENT,
33 XSLT_OP_ANCESTOR,
34 XSLT_OP_ID,
35 XSLT_OP_KEY,
36 XSLT_OP_NS,
37 XSLT_OP_ALL,
38 XSLT_OP_PI,
39 XSLT_OP_COMMENT,
40 XSLT_OP_TEXT,
41 XSLT_OP_NODE,
42 XSLT_OP_PREDICATE
43 } xsltOp;
44
45 typedef enum {
46 AXIS_CHILD=1,
47 AXIS_ATTRIBUTE
48 } xsltAxis;
49
50 typedef struct _xsltStepState xsltStepState;
51 typedef xsltStepState *xsltStepStatePtr;
52 struct _xsltStepState {
53 int step;
54 xmlNodePtr node;
55 };
56
57 typedef struct _xsltStepStates xsltStepStates;
58 typedef xsltStepStates *xsltStepStatesPtr;
59 struct _xsltStepStates {
60 int nbstates;
61 int maxstates;
62 xsltStepStatePtr states;
63 };
64
65 typedef struct _xsltStepOp xsltStepOp;
66 typedef xsltStepOp *xsltStepOpPtr;
67 struct _xsltStepOp {
68 xsltOp op;
69 xmlChar *value;
70 xmlChar *value2;
71 xmlChar *value3;
72 xmlXPathCompExprPtr comp;
73 /*
74 * Optimisations for count
75 */
76 int previousExtra;
77 int indexExtra;
78 int lenExtra;
79 };
80
81 struct _xsltCompMatch {
82 struct _xsltCompMatch *next; /* siblings in the name hash */
83 float priority; /* the priority */
84 const xmlChar *pattern; /* the pattern */
85 const xmlChar *mode; /* the mode */
86 const xmlChar *modeURI; /* the mode URI */
87 xsltTemplatePtr template; /* the associated template */
88
89 int direct;
90 /* TODO fix the statically allocated size steps[] */
91 int nbStep;
92 int maxStep;
93 xmlNsPtr *nsList; /* the namespaces in scope */
94 int nsNr; /* the number of namespaces in scope */
95 xsltStepOpPtr steps; /* ops for computation */
96 };
97
98 typedef struct _xsltParserContext xsltParserContext;
99 typedef xsltParserContext *xsltParserContextPtr;
100 struct _xsltParserContext {
101 xsltStylesheetPtr style; /* the stylesheet */
102 xsltTransformContextPtr ctxt; /* the transformation or NULL */
103 const xmlChar *cur; /* the current char being parsed */
104 const xmlChar *base; /* the full expression */
105 xmlDocPtr doc; /* the source document */
106 xmlNodePtr elem; /* the source element */
107 int error; /* error code */
108 xsltCompMatchPtr comp; /* the result */
109 };
110
111 /************************************************************************
112 * *
113 * Type functions *
114 * *
115 ************************************************************************/
116
117 /**
118 * xsltNewCompMatch:
119 *
120 * Create a new XSLT CompMatch
121 *
122 * Returns the newly allocated xsltCompMatchPtr or NULL in case of error
123 */
124 static xsltCompMatchPtr
125 xsltNewCompMatch(void) {
126 xsltCompMatchPtr cur;
127
128 cur = (xsltCompMatchPtr) xmlMalloc(sizeof(xsltCompMatch));
129 if (cur == NULL) {
130 xsltTransformError(NULL, NULL, NULL,
131 "xsltNewCompMatch : out of memory error\n");
132 return(NULL);
133 }
134 memset(cur, 0, sizeof(xsltCompMatch));
135 cur->maxStep = 10;
136 cur->nbStep = 0;
137 cur-> steps = (xsltStepOpPtr) xmlMalloc(sizeof(xsltStepOp) *
138 cur->maxStep);
139 if (cur->steps == NULL) {
140 xsltTransformError(NULL, NULL, NULL,
141 "xsltNewCompMatch : out of memory error\n");
142 xmlFree(cur);
143 return(NULL);
144 }
145 cur->nsNr = 0;
146 cur->nsList = NULL;
147 cur->direct = 0;
148 return(cur);
149 }
150
151 /**
152 * xsltFreeCompMatch:
153 * @comp: an XSLT comp
154 *
155 * Free up the memory allocated by @comp
156 */
157 static void
158 xsltFreeCompMatch(xsltCompMatchPtr comp) {
159 xsltStepOpPtr op;
160 int i;
161
162 if (comp == NULL)
163 return;
164 if (comp->pattern != NULL)
165 xmlFree((xmlChar *)comp->pattern);
166 if (comp->nsList != NULL)
167 xmlFree(comp->nsList);
168 for (i = 0;i < comp->nbStep;i++) {
169 op = &comp->steps[i];
170 if (op->value != NULL)
171 xmlFree(op->value);
172 if (op->value2 != NULL)
173 xmlFree(op->value2);
174 if (op->value3 != NULL)
175 xmlFree(op->value3);
176 if (op->comp != NULL)
177 xmlXPathFreeCompExpr(op->comp);
178 }
179 xmlFree(comp->steps);
180 memset(comp, -1, sizeof(xsltCompMatch));
181 xmlFree(comp);
182 }
183
184 /**
185 * xsltFreeCompMatchList:
186 * @comp: an XSLT comp list
187 *
188 * Free up the memory allocated by all the elements of @comp
189 */
190 void
191 xsltFreeCompMatchList(xsltCompMatchPtr comp) {
192 xsltCompMatchPtr cur;
193
194 while (comp != NULL) {
195 cur = comp;
196 comp = comp->next;
197 xsltFreeCompMatch(cur);
198 }
199 }
200
201 /**
202 * xsltNormalizeCompSteps:
203 * @payload: pointer to template hash table entry
204 * @data: pointer to the stylesheet
205 * @name: template match name
206 *
207 * This is a hashtable scanner function to normalize the compiled
208 * steps of an imported stylesheet.
209 */
210 void xsltNormalizeCompSteps(void *payload,
211 void *data, const xmlChar *name ATTRIBUTE_UNUSED) {
212 xsltCompMatchPtr comp = payload;
213 xsltStylesheetPtr style = data;
214 int ix;
215
216 for (ix = 0; ix < comp->nbStep; ix++) {
217 comp->steps[ix].previousExtra += style->extrasNr;
218 comp->steps[ix].indexExtra += style->extrasNr;
219 comp->steps[ix].lenExtra += style->extrasNr;
220 }
221 }
222
223 /**
224 * xsltNewParserContext:
225 * @style: the stylesheet
226 * @ctxt: the transformation context, if done at run-time
227 *
228 * Create a new XSLT ParserContext
229 *
230 * Returns the newly allocated xsltParserContextPtr or NULL in case of error
231 */
232 static xsltParserContextPtr
233 xsltNewParserContext(xsltStylesheetPtr style, xsltTransformContextPtr ctxt) {
234 xsltParserContextPtr cur;
235
236 cur = (xsltParserContextPtr) xmlMalloc(sizeof(xsltParserContext));
237 if (cur == NULL) {
238 xsltTransformError(NULL, NULL, NULL,
239 "xsltNewParserContext : malloc failed\n");
240 return(NULL);
241 }
242 memset(cur, 0, sizeof(xsltParserContext));
243 cur->style = style;
244 cur->ctxt = ctxt;
245 return(cur);
246 }
247
248 /**
249 * xsltFreeParserContext:
250 * @ctxt: an XSLT parser context
251 *
252 * Free up the memory allocated by @ctxt
253 */
254 static void
255 xsltFreeParserContext(xsltParserContextPtr ctxt) {
256 if (ctxt == NULL)
257 return;
258 memset(ctxt, -1, sizeof(xsltParserContext));
259 xmlFree(ctxt);
260 }
261
262 /**
263 * xsltCompMatchAdd:
264 * @comp: the compiled match expression
265 * @op: an op
266 * @value: the first value
267 * @value2: the second value
268 * @novar: flag to set XML_XPATH_NOVAR
269 *
270 * Add an step to an XSLT Compiled Match
271 *
272 * Returns -1 in case of failure, 0 otherwise.
273 */
274 static int
275 xsltCompMatchAdd(xsltParserContextPtr ctxt, xsltCompMatchPtr comp,
276 xsltOp op, xmlChar * value, xmlChar * value2, int novar)
277 {
278 if (comp->nbStep >= comp->maxStep) {
279 xsltStepOpPtr tmp;
280
281 tmp = (xsltStepOpPtr) xmlRealloc(comp->steps, comp->maxStep * 2 *
282 sizeof(xsltStepOp));
283 if (tmp == NULL) {
284 xsltGenericError(xsltGenericErrorContext,
285 "xsltCompMatchAdd: memory re-allocation failure.\n");
286 if (ctxt->style != NULL)
287 ctxt->style->errors++;
288 if (value)
289 xmlFree(value);
290 if (value2)
291 xmlFree(value2);
292 return (-1);
293 }
294 comp->maxStep *= 2;
295 comp->steps = tmp;
296 }
297 comp->steps[comp->nbStep].op = op;
298 comp->steps[comp->nbStep].value = value;
299 comp->steps[comp->nbStep].value2 = value2;
300 comp->steps[comp->nbStep].value3 = NULL;
301 comp->steps[comp->nbStep].comp = NULL;
302 if (ctxt->ctxt != NULL) {
303 comp->steps[comp->nbStep].previousExtra =
304 xsltAllocateExtraCtxt(ctxt->ctxt);
305 comp->steps[comp->nbStep].indexExtra =
306 xsltAllocateExtraCtxt(ctxt->ctxt);
307 comp->steps[comp->nbStep].lenExtra =
308 xsltAllocateExtraCtxt(ctxt->ctxt);
309 } else {
310 comp->steps[comp->nbStep].previousExtra =
311 xsltAllocateExtra(ctxt->style);
312 comp->steps[comp->nbStep].indexExtra =
313 xsltAllocateExtra(ctxt->style);
314 comp->steps[comp->nbStep].lenExtra =
315 xsltAllocateExtra(ctxt->style);
316 }
317 if (op == XSLT_OP_PREDICATE) {
318 xmlXPathContextPtr xctxt;
319
320 if (ctxt->style != NULL)
321 xctxt = xmlXPathNewContext(ctxt->style->doc);
322 else
323 xctxt = xmlXPathNewContext(NULL);
324 #ifdef XML_XPATH_NOVAR
325 if (novar != 0)
326 xctxt->flags = XML_XPATH_NOVAR;
327 #endif
328 if (ctxt->style != NULL)
329 xctxt->dict = ctxt->style->dict;
330 comp->steps[comp->nbStep].comp = xmlXPathCtxtCompile(xctxt, value);
331 xmlXPathFreeContext(xctxt);
332 if (comp->steps[comp->nbStep].comp == NULL) {
333 xsltTransformError(NULL, ctxt->style, ctxt->elem,
334 "Failed to compile predicate\n");
335 if (ctxt->style != NULL)
336 ctxt->style->errors++;
337 }
338 }
339 comp->nbStep++;
340 return (0);
341 }
342
343 /**
344 * xsltSwapTopCompMatch:
345 * @comp: the compiled match expression
346 *
347 * reverse the two top steps.
348 */
349 static void
350 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
351 int i;
352 int j = comp->nbStep - 1;
353
354 if (j > 0) {
355 register xmlChar *tmp;
356 register xsltOp op;
357 register xmlXPathCompExprPtr expr;
358 register int t;
359 i = j - 1;
360 tmp = comp->steps[i].value;
361 comp->steps[i].value = comp->steps[j].value;
362 comp->steps[j].value = tmp;
363 tmp = comp->steps[i].value2;
364 comp->steps[i].value2 = comp->steps[j].value2;
365 comp->steps[j].value2 = tmp;
366 tmp = comp->steps[i].value3;
367 comp->steps[i].value3 = comp->steps[j].value3;
368 comp->steps[j].value3 = tmp;
369 op = comp->steps[i].op;
370 comp->steps[i].op = comp->steps[j].op;
371 comp->steps[j].op = op;
372 expr = comp->steps[i].comp;
373 comp->steps[i].comp = comp->steps[j].comp;
374 comp->steps[j].comp = expr;
375 t = comp->steps[i].previousExtra;
376 comp->steps[i].previousExtra = comp->steps[j].previousExtra;
377 comp->steps[j].previousExtra = t;
378 t = comp->steps[i].indexExtra;
379 comp->steps[i].indexExtra = comp->steps[j].indexExtra;
380 comp->steps[j].indexExtra = t;
381 t = comp->steps[i].lenExtra;
382 comp->steps[i].lenExtra = comp->steps[j].lenExtra;
383 comp->steps[j].lenExtra = t;
384 }
385 }
386
387 /**
388 * xsltReverseCompMatch:
389 * @ctxt: the parser context
390 * @comp: the compiled match expression
391 *
392 * reverse all the stack of expressions
393 */
394 static void
395 xsltReverseCompMatch(xsltParserContextPtr ctxt, xsltCompMatchPtr comp) {
396 int i = 0;
397 int j = comp->nbStep - 1;
398
399 while (j > i) {
400 register xmlChar *tmp;
401 register xsltOp op;
402 register xmlXPathCompExprPtr expr;
403 register int t;
404
405 tmp = comp->steps[i].value;
406 comp->steps[i].value = comp->steps[j].value;
407 comp->steps[j].value = tmp;
408 tmp = comp->steps[i].value2;
409 comp->steps[i].value2 = comp->steps[j].value2;
410 comp->steps[j].value2 = tmp;
411 tmp = comp->steps[i].value3;
412 comp->steps[i].value3 = comp->steps[j].value3;
413 comp->steps[j].value3 = tmp;
414 op = comp->steps[i].op;
415 comp->steps[i].op = comp->steps[j].op;
416 comp->steps[j].op = op;
417 expr = comp->steps[i].comp;
418 comp->steps[i].comp = comp->steps[j].comp;
419 comp->steps[j].comp = expr;
420 t = comp->steps[i].previousExtra;
421 comp->steps[i].previousExtra = comp->steps[j].previousExtra;
422 comp->steps[j].previousExtra = t;
423 t = comp->steps[i].indexExtra;
424 comp->steps[i].indexExtra = comp->steps[j].indexExtra;
425 comp->steps[j].indexExtra = t;
426 t = comp->steps[i].lenExtra;
427 comp->steps[i].lenExtra = comp->steps[j].lenExtra;
428 comp->steps[j].lenExtra = t;
429 j--;
430 i++;
431 }
432 xsltCompMatchAdd(ctxt, comp, XSLT_OP_END, NULL, NULL, 0);
433
434 /*
435 * detect consecutive XSLT_OP_PREDICATE indicating a direct
436 * matching should be done.
437 */
438 for (i = 0;i < comp->nbStep - 1;i++) {
439 if ((comp->steps[i].op == XSLT_OP_PREDICATE) &&
440 (comp->steps[i + 1].op == XSLT_OP_PREDICATE)) {
441
442 comp->direct = 1;
443 if (comp->pattern[0] != '/') {
444 xmlChar *query;
445
446 query = xmlStrdup((const xmlChar *)"//");
447 query = xmlStrcat(query, comp->pattern);
448
449 xmlFree((xmlChar *) comp->pattern);
450 comp->pattern = query;
451 }
452 break;
453 }
454 }
455 }
456
457 /************************************************************************
458 * *
459 * The interpreter for the precompiled patterns *
460 * *
461 ************************************************************************/
462
463 static int
464 xsltPatPushState(xsltTransformContextPtr ctxt, xsltStepStates *states,
465 int step, xmlNodePtr node) {
466 if ((states->states == NULL) || (states->maxstates <= 0)) {
467 states->maxstates = 4;
468 states->nbstates = 0;
469 states->states = xmlMalloc(4 * sizeof(xsltStepState));
470 }
471 else if (states->maxstates <= states->nbstates) {
472 xsltStepState *tmp;
473
474 tmp = (xsltStepStatePtr) xmlRealloc(states->states,
475 2 * states->maxstates * sizeof(xsltStepState));
476 if (tmp == NULL) {
477 xsltGenericError(xsltGenericErrorContext,
478 "xsltPatPushState: memory re-allocation failure.\n");
479 ctxt->state = XSLT_STATE_STOPPED;
480 return(-1);
481 }
482 states->states = tmp;
483 states->maxstates *= 2;
484 }
485 states->states[states->nbstates].step = step;
486 states->states[states->nbstates++].node = node;
487 #if 0
488 fprintf(stderr, "Push: %d, %s\n", step, node->name);
489 #endif
490 return(0);
491 }
492
493 /**
494 * xsltTestCompMatchDirect:
495 * @ctxt: a XSLT process context
496 * @comp: the precompiled pattern
497 * @node: a node
498 * @nsList: the namespaces in scope
499 * @nsNr: the number of namespaces in scope
500 *
501 * Test whether the node matches the pattern, do a direct evalutation
502 * and not a step by step evaluation.
503 *
504 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
505 */
506 static int
507 xsltTestCompMatchDirect(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
508 xmlNodePtr node, xmlNsPtr *nsList, int nsNr) {
509 xsltStepOpPtr sel = NULL;
510 xmlDocPtr prevdoc;
511 xmlDocPtr doc;
512 xmlXPathObjectPtr list;
513 int ix, j;
514 int nocache = 0;
515 int isRVT;
516
517 doc = node->doc;
518 if (XSLT_IS_RES_TREE_FRAG(doc))
519 isRVT = 1;
520 else
521 isRVT = 0;
522 sel = &comp->steps[0]; /* store extra in first step arbitrarily */
523
524 prevdoc = (xmlDocPtr)
525 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
526 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
527 list = (xmlXPathObjectPtr)
528 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra);
529
530 if ((list == NULL) || (prevdoc != doc)) {
531 xmlXPathObjectPtr newlist;
532 xmlNodePtr parent = node->parent;
533 xmlDocPtr olddoc;
534 xmlNodePtr oldnode;
535 int oldNsNr, oldContextSize, oldProximityPosition;
536 xmlNsPtr *oldNamespaces;
537
538 oldnode = ctxt->xpathCtxt->node;
539 olddoc = ctxt->xpathCtxt->doc;
540 oldNsNr = ctxt->xpathCtxt->nsNr;
541 oldNamespaces = ctxt->xpathCtxt->namespaces;
542 oldContextSize = ctxt->xpathCtxt->contextSize;
543 oldProximityPosition = ctxt->xpathCtxt->proximityPosition;
544 ctxt->xpathCtxt->node = node;
545 ctxt->xpathCtxt->doc = doc;
546 ctxt->xpathCtxt->namespaces = nsList;
547 ctxt->xpathCtxt->nsNr = nsNr;
548 newlist = xmlXPathEval(comp->pattern, ctxt->xpathCtxt);
549 ctxt->xpathCtxt->node = oldnode;
550 ctxt->xpathCtxt->doc = olddoc;
551 ctxt->xpathCtxt->namespaces = oldNamespaces;
552 ctxt->xpathCtxt->nsNr = oldNsNr;
553 ctxt->xpathCtxt->contextSize = oldContextSize;
554 ctxt->xpathCtxt->proximityPosition = oldProximityPosition;
555 if (newlist == NULL)
556 return(-1);
557 if (newlist->type != XPATH_NODESET) {
558 xmlXPathFreeObject(newlist);
559 return(-1);
560 }
561 ix = 0;
562
563 if ((parent == NULL) || (node->doc == NULL) || isRVT)
564 nocache = 1;
565
566 if (nocache == 0) {
567 if (list != NULL)
568 xmlXPathFreeObject(list);
569 list = newlist;
570
571 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra) =
572 (void *) list;
573 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
574 (void *) doc;
575 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
576 0;
577 XSLT_RUNTIME_EXTRA_FREE(ctxt, sel->lenExtra) =
578 (xmlFreeFunc) xmlXPathFreeObject;
579 } else
580 list = newlist;
581 }
582 if ((list->nodesetval == NULL) ||
583 (list->nodesetval->nodeNr <= 0)) {
584 if (nocache == 1)
585 xmlXPathFreeObject(list);
586 return(0);
587 }
588 /* TODO: store the index and use it for the scan */
589 if (ix == 0) {
590 for (j = 0;j < list->nodesetval->nodeNr;j++) {
591 if (list->nodesetval->nodeTab[j] == node) {
592 if (nocache == 1)
593 xmlXPathFreeObject(list);
594 return(1);
595 }
596 }
597 } else {
598 }
599 if (nocache == 1)
600 xmlXPathFreeObject(list);
601 return(0);
602 }
603
604 /**
605 * xsltTestCompMatch:
606 * @ctxt: a XSLT process context
607 * @comp: the precompiled pattern
608 * @node: a node
609 * @mode: the mode name or NULL
610 * @modeURI: the mode URI or NULL
611 *
612 * Test whether the node matches the pattern
613 *
614 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
615 */
616 static int
617 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
618 xmlNodePtr node, const xmlChar *mode,
619 const xmlChar *modeURI) {
620 int i;
621 xsltStepOpPtr step, sel = NULL;
622 xsltStepStates states = {0, 0, NULL}; /* // may require backtrack */
623
624 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
625 xsltTransformError(ctxt, NULL, node,
626 "xsltTestCompMatch: null arg\n");
627 return(-1);
628 }
629 if (mode != NULL) {
630 if (comp->mode == NULL)
631 return(0);
632 /*
633 * both mode strings must be interned on the stylesheet dictionary
634 */
635 if (comp->mode != mode)
636 return(0);
637 } else {
638 if (comp->mode != NULL)
639 return(0);
640 }
641 if (modeURI != NULL) {
642 if (comp->modeURI == NULL)
643 return(0);
644 /*
645 * both modeURI strings must be interned on the stylesheet dictionary
646 */
647 if (comp->modeURI != modeURI)
648 return(0);
649 } else {
650 if (comp->modeURI != NULL)
651 return(0);
652 }
653
654 i = 0;
655 restart:
656 for (;i < comp->nbStep;i++) {
657 step = &comp->steps[i];
658 if (step->op != XSLT_OP_PREDICATE)
659 sel = step;
660 switch (step->op) {
661 case XSLT_OP_END:
662 goto found;
663 case XSLT_OP_ROOT:
664 if ((node->type == XML_DOCUMENT_NODE) ||
665 #ifdef LIBXML_DOCB_ENABLED
666 (node->type == XML_DOCB_DOCUMENT_NODE) ||
667 #endif
668 (node->type == XML_HTML_DOCUMENT_NODE))
669 continue;
670 if ((node->type == XML_ELEMENT_NODE) && (node->name[0] == ' '))
671 continue;
672 goto rollback;
673 case XSLT_OP_ELEM:
674 if (node->type != XML_ELEMENT_NODE)
675 goto rollback;
676 if (step->value == NULL)
677 continue;
678 if (step->value[0] != node->name[0])
679 goto rollback;
680 if (!xmlStrEqual(step->value, node->name))
681 goto rollback;
682
683 /* Namespace test */
684 if (node->ns == NULL) {
685 if (step->value2 != NULL)
686 goto rollback;
687 } else if (node->ns->href != NULL) {
688 if (step->value2 == NULL)
689 goto rollback;
690 if (!xmlStrEqual(step->value2, node->ns->href))
691 goto rollback;
692 }
693 continue;
694 case XSLT_OP_ATTR:
695 if (node->type != XML_ATTRIBUTE_NODE)
696 goto rollback;
697 if (step->value != NULL) {
698 if (step->value[0] != node->name[0])
699 goto rollback;
700 if (!xmlStrEqual(step->value, node->name))
701 goto rollback;
702 }
703 /* Namespace test */
704 if (node->ns == NULL) {
705 if (step->value2 != NULL)
706 goto rollback;
707 } else if (step->value2 != NULL) {
708 if (!xmlStrEqual(step->value2, node->ns->href))
709 goto rollback;
710 }
711 continue;
712 case XSLT_OP_PARENT:
713 if ((node->type == XML_DOCUMENT_NODE) ||
714 (node->type == XML_HTML_DOCUMENT_NODE) ||
715 #ifdef LIBXML_DOCB_ENABLED
716 (node->type == XML_DOCB_DOCUMENT_NODE) ||
717 #endif
718 (node->type == XML_NAMESPACE_DECL))
719 goto rollback;
720 node = node->parent;
721 if (node == NULL)
722 goto rollback;
723 if (step->value == NULL)
724 continue;
725 if (step->value[0] != node->name[0])
726 goto rollback;
727 if (!xmlStrEqual(step->value, node->name))
728 goto rollback;
729 /* Namespace test */
730 if (node->ns == NULL) {
731 if (step->value2 != NULL)
732 goto rollback;
733 } else if (node->ns->href != NULL) {
734 if (step->value2 == NULL)
735 goto rollback;
736 if (!xmlStrEqual(step->value2, node->ns->href))
737 goto rollback;
738 }
739 continue;
740 case XSLT_OP_ANCESTOR:
741 /* TODO: implement coalescing of ANCESTOR/NODE ops */
742 if (step->value == NULL) {
743 step = &comp->steps[i+1];
744 if (step->op == XSLT_OP_ROOT)
745 goto found;
746 /* added NS, ID and KEY as a result of bug 168208 */
747 if ((step->op != XSLT_OP_ELEM) &&
748 (step->op != XSLT_OP_ALL) &&
749 (step->op != XSLT_OP_NS) &&
750 (step->op != XSLT_OP_ID) &&
751 (step->op != XSLT_OP_KEY))
752 goto rollback;
753 }
754 if (node == NULL)
755 goto rollback;
756 if ((node->type == XML_DOCUMENT_NODE) ||
757 (node->type == XML_HTML_DOCUMENT_NODE) ||
758 #ifdef LIBXML_DOCB_ENABLED
759 (node->type == XML_DOCB_DOCUMENT_NODE) ||
760 #endif
761 (node->type == XML_NAMESPACE_DECL))
762 goto rollback;
763 node = node->parent;
764 if ((step->op != XSLT_OP_ELEM) && step->op != XSLT_OP_ALL) {
765 xsltPatPushState(ctxt, &states, i, node);
766 continue;
767 }
768 i++;
769 if (step->value == NULL) {
770 xsltPatPushState(ctxt, &states, i - 1, node);
771 continue;
772 }
773 while (node != NULL) {
774 if ((node->type == XML_ELEMENT_NODE) &&
775 (step->value[0] == node->name[0]) &&
776 (xmlStrEqual(step->value, node->name))) {
777 /* Namespace test */
778 if (node->ns == NULL) {
779 if (step->value2 == NULL)
780 break;
781 } else if (node->ns->href != NULL) {
782 if ((step->value2 != NULL) &&
783 (xmlStrEqual(step->value2, node->ns->href)))
784 break;
785 }
786 }
787 node = node->parent;
788 }
789 if (node == NULL)
790 goto rollback;
791 xsltPatPushState(ctxt, &states, i - 1, node);
792 continue;
793 case XSLT_OP_ID: {
794 /* TODO Handle IDs decently, must be done differently */
795 xmlAttrPtr id;
796
797 if (node->type != XML_ELEMENT_NODE)
798 goto rollback;
799
800 id = xmlGetID(node->doc, step->value);
801 if ((id == NULL) || (id->parent != node))
802 goto rollback;
803 break;
804 }
805 case XSLT_OP_KEY: {
806 xmlNodeSetPtr list;
807 int indx;
808
809 list = xsltGetKey(ctxt, step->value,
810 step->value3, step->value2);
811 if (list == NULL)
812 goto rollback;
813 for (indx = 0;indx < list->nodeNr;indx++)
814 if (list->nodeTab[indx] == node)
815 break;
816 if (indx >= list->nodeNr)
817 goto rollback;
818 break;
819 }
820 case XSLT_OP_NS:
821 if (node->type != XML_ELEMENT_NODE)
822 goto rollback;
823 if (node->ns == NULL) {
824 if (step->value != NULL)
825 goto rollback;
826 } else if (node->ns->href != NULL) {
827 if (step->value == NULL)
828 goto rollback;
829 if (!xmlStrEqual(step->value, node->ns->href))
830 goto rollback;
831 }
832 break;
833 case XSLT_OP_ALL:
834 if (node->type != XML_ELEMENT_NODE)
835 goto rollback;
836 break;
837 case XSLT_OP_PREDICATE: {
838 xmlNodePtr oldNode;
839 xmlDocPtr doc;
840 int oldCS, oldCP;
841 int pos = 0, len = 0;
842 int isRVT;
843
844 /*
845 * when there is cascading XSLT_OP_PREDICATE, then use a
846 * direct computation approach. It's not done directly
847 * at the beginning of the routine to filter out as much
848 * as possible this costly computation.
849 */
850 if (comp->direct) {
851 if (states.states != NULL) {
852 /* Free the rollback states */
853 xmlFree(states.states);
854 }
855 return(xsltTestCompMatchDirect(ctxt, comp, node,
856 comp->nsList, comp->nsNr));
857 }
858
859 doc = node->doc;
860 if (XSLT_IS_RES_TREE_FRAG(doc))
861 isRVT = 1;
862 else
863 isRVT = 0;
864
865 /*
866 * Depending on the last selection, one may need to
867 * recompute contextSize and proximityPosition.
868 */
869 oldCS = ctxt->xpathCtxt->contextSize;
870 oldCP = ctxt->xpathCtxt->proximityPosition;
871 if ((sel != NULL) &&
872 (sel->op == XSLT_OP_ELEM) &&
873 (sel->value != NULL) &&
874 (node->type == XML_ELEMENT_NODE) &&
875 (node->parent != NULL)) {
876 xmlNodePtr previous;
877 int nocache = 0;
878
879 previous = (xmlNodePtr)
880 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
881 if ((previous != NULL) &&
882 (previous->parent == node->parent)) {
883 /*
884 * just walk back to adjust the index
885 */
886 int indx = 0;
887 xmlNodePtr sibling = node;
888
889 while (sibling != NULL) {
890 if (sibling == previous)
891 break;
892 if ((sibling->type == XML_ELEMENT_NODE) &&
893 (previous->name != NULL) &&
894 (sibling->name != NULL) &&
895 (previous->name[0] == sibling->name[0]) &&
896 (xmlStrEqual(previous->name, sibling->name)))
897 {
898 if ((sel->value2 == NULL) ||
899 ((sibling->ns != NULL) &&
900 (xmlStrEqual(sel->value2,
901 sibling->ns->href))))
902 indx++;
903 }
904 sibling = sibling->prev;
905 }
906 if (sibling == NULL) {
907 /* hum going backward in document order ... */
908 indx = 0;
909 sibling = node;
910 while (sibling != NULL) {
911 if (sibling == previous)
912 break;
913 if ((sibling->type == XML_ELEMENT_NODE) &&
914 (previous->name != NULL) &&
915 (sibling->name != NULL) &&
916 (previous->name[0] == sibling->name[0]) &&
917 (xmlStrEqual(previous->name, sibling->name)))
918 {
919 if ((sel->value2 == NULL) ||
920 ((sibling->ns != NULL) &&
921 (xmlStrEqual(sel->value2,
922 sibling->ns->href))))
923 {
924 indx--;
925 }
926 }
927 sibling = sibling->next;
928 }
929 }
930 if (sibling != NULL) {
931 pos = XSLT_RUNTIME_EXTRA(ctxt,
932 sel->indexExtra, ival) + indx;
933 /*
934 * If the node is in a Value Tree we need to
935 * save len, but cannot cache the node!
936 * (bugs 153137 and 158840)
937 */
938 if (node->doc != NULL) {
939 len = XSLT_RUNTIME_EXTRA(ctxt,
940 sel->lenExtra, ival);
941 if (!isRVT) {
942 XSLT_RUNTIME_EXTRA(ctxt,
943 sel->previousExtra, ptr) = node;
944 XSLT_RUNTIME_EXTRA(ctxt,
945 sel->indexExtra, ival) = pos;
946 }
947 }
948 } else
949 pos = 0;
950 } else {
951 /*
952 * recompute the index
953 */
954 xmlNodePtr parent = node->parent;
955 xmlNodePtr siblings = NULL;
956
957 if (parent) siblings = parent->children;
958
959 while (siblings != NULL) {
960 if (siblings->type == XML_ELEMENT_NODE) {
961 if (siblings == node) {
962 len++;
963 pos = len;
964 } else if ((node->name != NULL) &&
965 (siblings->name != NULL) &&
966 (node->name[0] == siblings->name[0]) &&
967 (xmlStrEqual(node->name, siblings->name))) {
968 if ((sel->value2 == NULL) ||
969 ((siblings->ns != NULL) &&
970 (xmlStrEqual(sel->value2,
971 siblings->ns->href))))
972 len++;
973 }
974 }
975 siblings = siblings->next;
976 }
977 if ((parent == NULL) || (node->doc == NULL))
978 nocache = 1;
979 else {
980 while (parent->parent != NULL)
981 parent = parent->parent;
982 if (((parent->type != XML_DOCUMENT_NODE) &&
983 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
984 (parent != (xmlNodePtr) node->doc))
985 nocache = 1;
986 }
987 }
988 if (pos != 0) {
989 ctxt->xpathCtxt->contextSize = len;
990 ctxt->xpathCtxt->proximityPosition = pos;
991 /*
992 * If the node is in a Value Tree we cannot
993 * cache it !
994 */
995 if ((!isRVT) && (node->doc != NULL) &&
996 (nocache == 0)) {
997 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
998 node;
999 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
1000 pos;
1001 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival) =
1002 len;
1003 }
1004 }
1005 } else if ((sel != NULL) && (sel->op == XSLT_OP_ALL) &&
1006 (node->type == XML_ELEMENT_NODE)) {
1007 xmlNodePtr previous;
1008 int nocache = 0;
1009
1010 previous = (xmlNodePtr)
1011 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
1012 if ((previous != NULL) &&
1013 (previous->parent == node->parent)) {
1014 /*
1015 * just walk back to adjust the index
1016 */
1017 int indx = 0;
1018 xmlNodePtr sibling = node;
1019
1020 while (sibling != NULL) {
1021 if (sibling == previous)
1022 break;
1023 if (sibling->type == XML_ELEMENT_NODE)
1024 indx++;
1025 sibling = sibling->prev;
1026 }
1027 if (sibling == NULL) {
1028 /* hum going backward in document order ... */
1029 indx = 0;
1030 sibling = node;
1031 while (sibling != NULL) {
1032 if (sibling == previous)
1033 break;
1034 if (sibling->type == XML_ELEMENT_NODE)
1035 indx--;
1036 sibling = sibling->next;
1037 }
1038 }
1039 if (sibling != NULL) {
1040 pos = XSLT_RUNTIME_EXTRA(ctxt,
1041 sel->indexExtra, ival) + indx;
1042 /*
1043 * If the node is in a Value Tree we cannot
1044 * cache it !
1045 */
1046 if ((node->doc != NULL) && !isRVT) {
1047 len = XSLT_RUNTIME_EXTRA(ctxt,
1048 sel->lenExtra, ival);
1049 XSLT_RUNTIME_EXTRA(ctxt,
1050 sel->previousExtra, ptr) = node;
1051 XSLT_RUNTIME_EXTRA(ctxt,
1052 sel->indexExtra, ival) = pos;
1053 }
1054 } else
1055 pos = 0;
1056 } else {
1057 /*
1058 * recompute the index
1059 */
1060 xmlNodePtr parent = node->parent;
1061 xmlNodePtr siblings = NULL;
1062
1063 if (parent) siblings = parent->children;
1064
1065 while (siblings != NULL) {
1066 if (siblings->type == XML_ELEMENT_NODE) {
1067 len++;
1068 if (siblings == node) {
1069 pos = len;
1070 }
1071 }
1072 siblings = siblings->next;
1073 }
1074 if ((parent == NULL) || (node->doc == NULL))
1075 nocache = 1;
1076 else {
1077 while (parent->parent != NULL)
1078 parent = parent->parent;
1079 if (((parent->type != XML_DOCUMENT_NODE) &&
1080 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
1081 (parent != (xmlNodePtr) node->doc))
1082 nocache = 1;
1083 }
1084 }
1085 if (pos != 0) {
1086 ctxt->xpathCtxt->contextSize = len;
1087 ctxt->xpathCtxt->proximityPosition = pos;
1088 /*
1089 * If the node is in a Value Tree we cannot
1090 * cache it !
1091 */
1092 if ((node->doc != NULL) && (nocache == 0) && !isRVT) {
1093 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
1094 node;
1095 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
1096 pos;
1097 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival) =
1098 len;
1099 }
1100 }
1101 }
1102 oldNode = ctxt->node;
1103 ctxt->node = node;
1104
1105 if (step->value == NULL)
1106 goto wrong_index;
1107 if (step->comp == NULL)
1108 goto wrong_index;
1109
1110 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
1111 comp->nsNr))
1112 goto wrong_index;
1113
1114 if (pos != 0) {
1115 ctxt->xpathCtxt->contextSize = oldCS;
1116 ctxt->xpathCtxt->proximityPosition = oldCP;
1117 }
1118 ctxt->node = oldNode;
1119 break;
1120 wrong_index:
1121 if (pos != 0) {
1122 ctxt->xpathCtxt->contextSize = oldCS;
1123 ctxt->xpathCtxt->proximityPosition = oldCP;
1124 }
1125 ctxt->node = oldNode;
1126 goto rollback;
1127 }
1128 case XSLT_OP_PI:
1129 if (node->type != XML_PI_NODE)
1130 goto rollback;
1131 if (step->value != NULL) {
1132 if (!xmlStrEqual(step->value, node->name))
1133 goto rollback;
1134 }
1135 break;
1136 case XSLT_OP_COMMENT:
1137 if (node->type != XML_COMMENT_NODE)
1138 goto rollback;
1139 break;
1140 case XSLT_OP_TEXT:
1141 if ((node->type != XML_TEXT_NODE) &&
1142 (node->type != XML_CDATA_SECTION_NODE))
1143 goto rollback;
1144 break;
1145 case XSLT_OP_NODE:
1146 switch (node->type) {
1147 case XML_ELEMENT_NODE:
1148 case XML_CDATA_SECTION_NODE:
1149 case XML_PI_NODE:
1150 case XML_COMMENT_NODE:
1151 case XML_TEXT_NODE:
1152 break;
1153 default:
1154 goto rollback;
1155 }
1156 break;
1157 }
1158 }
1159 found:
1160 if (states.states != NULL) {
1161 /* Free the rollback states */
1162 xmlFree(states.states);
1163 }
1164 return(1);
1165 rollback:
1166 /* got an error try to rollback */
1167 if (states.states == NULL)
1168 return(0);
1169 if (states.nbstates <= 0) {
1170 xmlFree(states.states);
1171 return(0);
1172 }
1173 states.nbstates--;
1174 i = states.states[states.nbstates].step;
1175 node = states.states[states.nbstates].node;
1176 #if 0
1177 fprintf(stderr, "Pop: %d, %s\n", i, node->name);
1178 #endif
1179 goto restart;
1180 }
1181
1182 /**
1183 * xsltTestCompMatchList:
1184 * @ctxt: a XSLT process context
1185 * @node: a node
1186 * @comp: the precompiled pattern list
1187 *
1188 * Test whether the node matches one of the patterns in the list
1189 *
1190 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
1191 */
1192 int
1193 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
1194 xsltCompMatchPtr comp) {
1195 int ret;
1196
1197 if ((ctxt == NULL) || (node == NULL))
1198 return(-1);
1199 while (comp != NULL) {
1200 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
1201 if (ret == 1)
1202 return(1);
1203 comp = comp->next;
1204 }
1205 return(0);
1206 }
1207
1208 /************************************************************************
1209 * *
1210 * Dedicated parser for templates *
1211 * *
1212 ************************************************************************/
1213
1214 #define CUR (*ctxt->cur)
1215 #define SKIP(val) ctxt->cur += (val)
1216 #define NXT(val) ctxt->cur[(val)]
1217 #define CUR_PTR ctxt->cur
1218
1219 #define SKIP_BLANKS \
1220 while (IS_BLANK_CH(CUR)) NEXT
1221
1222 #define CURRENT (*ctxt->cur)
1223 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1224
1225
1226 #define PUSH(op, val, val2, novar) \
1227 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2), (novar))) goto error;
1228
1229 #define SWAP() \
1230 xsltSwapTopCompMatch(ctxt->comp);
1231
1232 #define XSLT_ERROR(X) \
1233 { xsltError(ctxt, __FILE__, __LINE__, X); \
1234 ctxt->error = (X); return; }
1235
1236 #define XSLT_ERROR0(X) \
1237 { xsltError(ctxt, __FILE__, __LINE__, X); \
1238 ctxt->error = (X); return(0); }
1239
1240 /**
1241 * xsltScanLiteral:
1242 * @ctxt: the XPath Parser context
1243 *
1244 * Parse an XPath Litteral:
1245 *
1246 * [29] Literal ::= '"' [^"]* '"'
1247 * | "'" [^']* "'"
1248 *
1249 * Returns the Literal parsed or NULL
1250 */
1251
1252 static xmlChar *
1253 xsltScanLiteral(xsltParserContextPtr ctxt) {
1254 const xmlChar *q, *cur;
1255 xmlChar *ret = NULL;
1256 int val, len;
1257
1258 SKIP_BLANKS;
1259 if (CUR == '"') {
1260 NEXT;
1261 cur = q = CUR_PTR;
1262 val = xmlStringCurrentChar(NULL, cur, &len);
1263 while ((IS_CHAR(val)) && (val != '"')) {
1264 cur += len;
1265 val = xmlStringCurrentChar(NULL, cur, &len);
1266 }
1267 if (!IS_CHAR(val)) {
1268 ctxt->error = 1;
1269 return(NULL);
1270 } else {
1271 ret = xmlStrndup(q, cur - q);
1272 }
1273 cur += len;
1274 CUR_PTR = cur;
1275 } else if (CUR == '\'') {
1276 NEXT;
1277 cur = q = CUR_PTR;
1278 val = xmlStringCurrentChar(NULL, cur, &len);
1279 while ((IS_CHAR(val)) && (val != '\'')) {
1280 cur += len;
1281 val = xmlStringCurrentChar(NULL, cur, &len);
1282 }
1283 if (!IS_CHAR(val)) {
1284 ctxt->error = 1;
1285 return(NULL);
1286 } else {
1287 ret = xmlStrndup(q, cur - q);
1288 }
1289 cur += len;
1290 CUR_PTR = cur;
1291 } else {
1292 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1293 ctxt->error = 1;
1294 return(NULL);
1295 }
1296 return(ret);
1297 }
1298
1299 /**
1300 * xsltScanNCName:
1301 * @ctxt: the XPath Parser context
1302 *
1303 * Parses a non qualified name
1304 *
1305 * Returns the Name parsed or NULL
1306 */
1307
1308 static xmlChar *
1309 xsltScanNCName(xsltParserContextPtr ctxt) {
1310 const xmlChar *q, *cur;
1311 xmlChar *ret = NULL;
1312 int val, len;
1313
1314 SKIP_BLANKS;
1315
1316 cur = q = CUR_PTR;
1317 val = xmlStringCurrentChar(NULL, cur, &len);
1318 if (!IS_LETTER(val) && (val != '_'))
1319 return(NULL);
1320
1321 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1322 (val == '.') || (val == '-') ||
1323 (val == '_') ||
1324 (IS_COMBINING(val)) ||
1325 (IS_EXTENDER(val))) {
1326 cur += len;
1327 val = xmlStringCurrentChar(NULL, cur, &len);
1328 }
1329 ret = xmlStrndup(q, cur - q);
1330 CUR_PTR = cur;
1331 return(ret);
1332 }
1333
1334 /*
1335 * xsltCompileIdKeyPattern:
1336 * @ctxt: the compilation context
1337 * @name: a preparsed name
1338 * @aid: whether id/key are allowed there
1339 * @novar: flag to prohibit xslt var
1340 *
1341 * Compile the XSLT LocationIdKeyPattern
1342 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1343 * | 'key' '(' Literal ',' Literal ')'
1344 *
1345 * also handle NodeType and PI from:
1346 *
1347 * [7] NodeTest ::= NameTest
1348 * | NodeType '(' ')'
1349 * | 'processing-instruction' '(' Literal ')'
1350 */
1351 static void
1352 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name,
1353 int aid, int novar, xsltAxis axis) {
1354 xmlChar *lit = NULL;
1355 xmlChar *lit2 = NULL;
1356
1357 if (CUR != '(') {
1358 xsltTransformError(NULL, NULL, NULL,
1359 "xsltCompileIdKeyPattern : ( expected\n");
1360 ctxt->error = 1;
1361 return;
1362 }
1363 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1364 if (axis != 0) {
1365 xsltTransformError(NULL, NULL, NULL,
1366 "xsltCompileIdKeyPattern : NodeTest expected\n");
1367 ctxt->error = 1;
1368 return;
1369 }
1370 NEXT;
1371 SKIP_BLANKS;
1372 lit = xsltScanLiteral(ctxt);
1373 if (ctxt->error) {
1374 xsltTransformError(NULL, NULL, NULL,
1375 "xsltCompileIdKeyPattern : Literal expected\n");
1376 return;
1377 }
1378 SKIP_BLANKS;
1379 if (CUR != ')') {
1380 xsltTransformError(NULL, NULL, NULL,
1381 "xsltCompileIdKeyPattern : ) expected\n");
1382 xmlFree(lit);
1383 ctxt->error = 1;
1384 return;
1385 }
1386 NEXT;
1387 PUSH(XSLT_OP_ID, lit, NULL, novar);
1388 lit = NULL;
1389 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1390 if (axis != 0) {
1391 xsltTransformError(NULL, NULL, NULL,
1392 "xsltCompileIdKeyPattern : NodeTest expected\n");
1393 ctxt->error = 1;
1394 return;
1395 }
1396 NEXT;
1397 SKIP_BLANKS;
1398 lit = xsltScanLiteral(ctxt);
1399 if (ctxt->error) {
1400 xsltTransformError(NULL, NULL, NULL,
1401 "xsltCompileIdKeyPattern : Literal expected\n");
1402 return;
1403 }
1404 SKIP_BLANKS;
1405 if (CUR != ',') {
1406 xsltTransformError(NULL, NULL, NULL,
1407 "xsltCompileIdKeyPattern : , expected\n");
1408 ctxt->error = 1;
1409 return;
1410 }
1411 NEXT;
1412 SKIP_BLANKS;
1413 lit2 = xsltScanLiteral(ctxt);
1414 if (ctxt->error) {
1415 xsltTransformError(NULL, NULL, NULL,
1416 "xsltCompileIdKeyPattern : Literal expected\n");
1417 xmlFree(lit);
1418 return;
1419 }
1420 SKIP_BLANKS;
1421 if (CUR != ')') {
1422 xsltTransformError(NULL, NULL, NULL,
1423 "xsltCompileIdKeyPattern : ) expected\n");
1424 xmlFree(lit);
1425 xmlFree(lit2);
1426 ctxt->error = 1;
1427 return;
1428 }
1429 NEXT;
1430 /* URGENT TODO: support namespace in keys */
1431 PUSH(XSLT_OP_KEY, lit, lit2, novar);
1432 lit = NULL;
1433 lit2 = NULL;
1434 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1435 NEXT;
1436 SKIP_BLANKS;
1437 if (CUR != ')') {
1438 lit = xsltScanLiteral(ctxt);
1439 if (ctxt->error) {
1440 xsltTransformError(NULL, NULL, NULL,
1441 "xsltCompileIdKeyPattern : Literal expected\n");
1442 return;
1443 }
1444 SKIP_BLANKS;
1445 if (CUR != ')') {
1446 xsltTransformError(NULL, NULL, NULL,
1447 "xsltCompileIdKeyPattern : ) expected\n");
1448 ctxt->error = 1;
1449 return;
1450 }
1451 }
1452 NEXT;
1453 PUSH(XSLT_OP_PI, lit, NULL, novar);
1454 lit = NULL;
1455 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1456 NEXT;
1457 SKIP_BLANKS;
1458 if (CUR != ')') {
1459 xsltTransformError(NULL, NULL, NULL,
1460 "xsltCompileIdKeyPattern : ) expected\n");
1461 ctxt->error = 1;
1462 return;
1463 }
1464 NEXT;
1465 PUSH(XSLT_OP_TEXT, NULL, NULL, novar);
1466 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1467 NEXT;
1468 SKIP_BLANKS;
1469 if (CUR != ')') {
1470 xsltTransformError(NULL, NULL, NULL,
1471 "xsltCompileIdKeyPattern : ) expected\n");
1472 ctxt->error = 1;
1473 return;
1474 }
1475 NEXT;
1476 PUSH(XSLT_OP_COMMENT, NULL, NULL, novar);
1477 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1478 NEXT;
1479 SKIP_BLANKS;
1480 if (CUR != ')') {
1481 xsltTransformError(NULL, NULL, NULL,
1482 "xsltCompileIdKeyPattern : ) expected\n");
1483 ctxt->error = 1;
1484 return;
1485 }
1486 NEXT;
1487 if (axis == AXIS_ATTRIBUTE) {
1488 PUSH(XSLT_OP_ATTR, NULL, NULL, novar);
1489 }
1490 else {
1491 PUSH(XSLT_OP_NODE, NULL, NULL, novar);
1492 }
1493 } else if (aid) {
1494 xsltTransformError(NULL, NULL, NULL,
1495 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1496 ctxt->error = 1;
1497 return;
1498 } else {
1499 xsltTransformError(NULL, NULL, NULL,
1500 "xsltCompileIdKeyPattern : node type\n");
1501 ctxt->error = 1;
1502 return;
1503 }
1504 error:
1505 return;
1506 }
1507
1508 /**
1509 * xsltCompileStepPattern:
1510 * @ctxt: the compilation context
1511 * @token: a posible precompiled name
1512 * @novar: flag to prohibit xslt variables from pattern
1513 *
1514 * Compile the XSLT StepPattern and generates a precompiled
1515 * form suitable for fast matching.
1516 *
1517 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1518 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1519 * | ('child' | 'attribute') '::'
1520 * from XPath
1521 * [7] NodeTest ::= NameTest
1522 * | NodeType '(' ')'
1523 * | 'processing-instruction' '(' Literal ')'
1524 * [8] Predicate ::= '[' PredicateExpr ']'
1525 * [9] PredicateExpr ::= Expr
1526 * [13] AbbreviatedAxisSpecifier ::= '@'?
1527 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1528 */
1529
1530 static void
1531 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token, int novar) {
1532 xmlChar *name = NULL;
1533 const xmlChar *URI = NULL;
1534 xmlChar *URL = NULL;
1535 int level;
1536 xsltAxis axis = 0;
1537
1538 SKIP_BLANKS;
1539 if ((token == NULL) && (CUR == '@')) {
1540 NEXT;
1541 axis = AXIS_ATTRIBUTE;
1542 }
1543 parse_node_test:
1544 if (token == NULL)
1545 token = xsltScanNCName(ctxt);
1546 if (token == NULL) {
1547 if (CUR == '*') {
1548 NEXT;
1549 if (axis == AXIS_ATTRIBUTE) {
1550 PUSH(XSLT_OP_ATTR, NULL, NULL, novar);
1551 }
1552 else {
1553 PUSH(XSLT_OP_ALL, NULL, NULL, novar);
1554 }
1555 goto parse_predicate;
1556 } else {
1557 xsltTransformError(NULL, NULL, NULL,
1558 "xsltCompileStepPattern : Name expected\n");
1559 ctxt->error = 1;
1560 goto error;
1561 }
1562 }
1563
1564
1565 SKIP_BLANKS;
1566 if (CUR == '(') {
1567 xsltCompileIdKeyPattern(ctxt, token, 0, novar, axis);
1568 xmlFree(token);
1569 token = NULL;
1570 if (ctxt->error)
1571 goto error;
1572 } else if (CUR == ':') {
1573 NEXT;
1574 if (CUR != ':') {
1575 xmlChar *prefix = token;
1576 xmlNsPtr ns;
1577
1578 /*
1579 * This is a namespace match
1580 */
1581 token = xsltScanNCName(ctxt);
1582 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1583 if (ns == NULL) {
1584 xsltTransformError(NULL, NULL, NULL,
1585 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1586 prefix);
1587 xmlFree(prefix);
1588 prefix=NULL;
1589 ctxt->error = 1;
1590 goto error;
1591 } else {
1592 URL = xmlStrdup(ns->href);
1593 }
1594 xmlFree(prefix);
1595 prefix=NULL;
1596 if (token == NULL) {
1597 if (CUR == '*') {
1598 NEXT;
1599 if (axis == AXIS_ATTRIBUTE) {
1600 PUSH(XSLT_OP_ATTR, NULL, URL, novar);
1601 URL = NULL;
1602 }
1603 else {
1604 PUSH(XSLT_OP_NS, URL, NULL, novar);
1605 URL = NULL;
1606 }
1607 } else {
1608 xsltTransformError(NULL, NULL, NULL,
1609 "xsltCompileStepPattern : Name expected\n");
1610 ctxt->error = 1;
1611 goto error;
1612 }
1613 } else {
1614 if (axis == AXIS_ATTRIBUTE) {
1615 PUSH(XSLT_OP_ATTR, token, URL, novar);
1616 token = NULL;
1617 URL = NULL;
1618 }
1619 else {
1620 PUSH(XSLT_OP_ELEM, token, URL, novar);
1621 token = NULL;
1622 URL = NULL;
1623 }
1624 }
1625 } else {
1626 if (axis != 0) {
1627 xsltTransformError(NULL, NULL, NULL,
1628 "xsltCompileStepPattern : NodeTest expected\n");
1629 ctxt->error = 1;
1630 goto error;
1631 }
1632 NEXT;
1633 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1634 axis = AXIS_CHILD;
1635 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1636 axis = AXIS_ATTRIBUTE;
1637 } else {
1638 xsltTransformError(NULL, NULL, NULL,
1639 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1640 ctxt->error = 1;
1641 goto error;
1642 }
1643 xmlFree(token);
1644 token = NULL;
1645 SKIP_BLANKS;
1646 token = xsltScanNCName(ctxt);
1647 goto parse_node_test;
1648 }
1649 } else {
1650 URI = xsltGetQNameURI(ctxt->elem, &token);
1651 if (token == NULL) {
1652 ctxt->error = 1;
1653 goto error;
1654 }
1655 if (URI != NULL)
1656 URL = xmlStrdup(URI);
1657 if (axis == AXIS_ATTRIBUTE) {
1658 PUSH(XSLT_OP_ATTR, token, URL, novar);
1659 token = NULL;
1660 URL = NULL;
1661 }
1662 else {
1663 PUSH(XSLT_OP_ELEM, token, URL, novar);
1664 token = NULL;
1665 URL = NULL;
1666 }
1667 }
1668 parse_predicate:
1669 SKIP_BLANKS;
1670 level = 0;
1671 while (CUR == '[') {
1672 const xmlChar *q;
1673 xmlChar *ret = NULL;
1674
1675 level++;
1676 NEXT;
1677 q = CUR_PTR;
1678 while (CUR != 0) {
1679 /* Skip over nested predicates */
1680 if (CUR == '[')
1681 level++;
1682 else if (CUR == ']') {
1683 level--;
1684 if (level == 0)
1685 break;
1686 } else if (CUR == '"') {
1687 NEXT;
1688 while ((CUR != 0) && (CUR != '"'))
1689 NEXT;
1690 } else if (CUR == '\'') {
1691 NEXT;
1692 while ((CUR != 0) && (CUR != '\''))
1693 NEXT;
1694 }
1695 NEXT;
1696 }
1697 if (CUR == 0) {
1698 xsltTransformError(NULL, NULL, NULL,
1699 "xsltCompileStepPattern : ']' expected\n");
1700 ctxt->error = 1;
1701 return;
1702 }
1703 ret = xmlStrndup(q, CUR_PTR - q);
1704 PUSH(XSLT_OP_PREDICATE, ret, NULL, novar);
1705 ret = NULL;
1706 /* push the predicate lower than local test */
1707 SWAP();
1708 NEXT;
1709 SKIP_BLANKS;
1710 }
1711 return;
1712 error:
1713 if (token != NULL)
1714 xmlFree(token);
1715 if (name != NULL)
1716 xmlFree(name);
1717 }
1718
1719 /**
1720 * xsltCompileRelativePathPattern:
1721 * @comp: the compilation context
1722 * @token: a posible precompiled name
1723 * @novar: flag to prohibit xslt variables
1724 *
1725 * Compile the XSLT RelativePathPattern and generates a precompiled
1726 * form suitable for fast matching.
1727 *
1728 * [4] RelativePathPattern ::= StepPattern
1729 * | RelativePathPattern '/' StepPattern
1730 * | RelativePathPattern '//' StepPattern
1731 */
1732 static void
1733 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token, int novar) {
1734 xsltCompileStepPattern(ctxt, token, novar);
1735 if (ctxt->error)
1736 goto error;
1737 SKIP_BLANKS;
1738 while ((CUR != 0) && (CUR != '|')) {
1739 if ((CUR == '/') && (NXT(1) == '/')) {
1740 PUSH(XSLT_OP_ANCESTOR, NULL, NULL, novar);
1741 NEXT;
1742 NEXT;
1743 SKIP_BLANKS;
1744 xsltCompileStepPattern(ctxt, NULL, novar);
1745 } else if (CUR == '/') {
1746 PUSH(XSLT_OP_PARENT, NULL, NULL, novar);
1747 NEXT;
1748 SKIP_BLANKS;
1749 if ((CUR != 0) && (CUR != '|')) {
1750 xsltCompileRelativePathPattern(ctxt, NULL, novar);
1751 }
1752 } else {
1753 ctxt->error = 1;
1754 }
1755 if (ctxt->error)
1756 goto error;
1757 SKIP_BLANKS;
1758 }
1759 error:
1760 return;
1761 }
1762
1763 /**
1764 * xsltCompileLocationPathPattern:
1765 * @ctxt: the compilation context
1766 * @novar: flag to prohibit xslt variables
1767 *
1768 * Compile the XSLT LocationPathPattern and generates a precompiled
1769 * form suitable for fast matching.
1770 *
1771 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1772 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1773 * | '//'? RelativePathPattern
1774 */
1775 static void
1776 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt, int novar) {
1777 SKIP_BLANKS;
1778 if ((CUR == '/') && (NXT(1) == '/')) {
1779 /*
1780 * since we reverse the query
1781 * a leading // can be safely ignored
1782 */
1783 NEXT;
1784 NEXT;
1785 ctxt->comp->priority = 0.5; /* '//' means not 0 priority */
1786 xsltCompileRelativePathPattern(ctxt, NULL, novar);
1787 } else if (CUR == '/') {
1788 /*
1789 * We need to find root as the parent
1790 */
1791 NEXT;
1792 SKIP_BLANKS;
1793 PUSH(XSLT_OP_ROOT, NULL, NULL, novar);
1794 if ((CUR != 0) && (CUR != '|')) {
1795 PUSH(XSLT_OP_PARENT, NULL, NULL, novar);
1796 xsltCompileRelativePathPattern(ctxt, NULL, novar);
1797 }
1798 } else if (CUR == '*') {
1799 xsltCompileRelativePathPattern(ctxt, NULL, novar);
1800 } else if (CUR == '@') {
1801 xsltCompileRelativePathPattern(ctxt, NULL, novar);
1802 } else {
1803 xmlChar *name;
1804 name = xsltScanNCName(ctxt);
1805 if (name == NULL) {
1806 xsltTransformError(NULL, NULL, NULL,
1807 "xsltCompileLocationPathPattern : Name expected\n");
1808 ctxt->error = 1;
1809 return;
1810 }
1811 SKIP_BLANKS;
1812 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1813 xsltCompileIdKeyPattern(ctxt, name, 1, novar, 0);
1814 xmlFree(name);
1815 name = NULL;
1816 if ((CUR == '/') && (NXT(1) == '/')) {
1817 PUSH(XSLT_OP_ANCESTOR, NULL, NULL, novar);
1818 NEXT;
1819 NEXT;
1820 SKIP_BLANKS;
1821 xsltCompileRelativePathPattern(ctxt, NULL, novar);
1822 } else if (CUR == '/') {
1823 PUSH(XSLT_OP_PARENT, NULL, NULL, novar);
1824 NEXT;
1825 SKIP_BLANKS;
1826 xsltCompileRelativePathPattern(ctxt, NULL, novar);
1827 }
1828 return;
1829 }
1830 xsltCompileRelativePathPattern(ctxt, name, novar);
1831 }
1832 error:
1833 return;
1834 }
1835
1836 /**
1837 * xsltCompilePatternInternal:
1838 * @pattern: an XSLT pattern
1839 * @doc: the containing document
1840 * @node: the containing element
1841 * @style: the stylesheet
1842 * @runtime: the transformation context, if done at run-time
1843 * @novar: flag to prohibit xslt variables
1844 *
1845 * Compile the XSLT pattern and generates a list of precompiled form suitable
1846 * for fast matching.
1847 *
1848 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1849 *
1850 * Returns the generated pattern list or NULL in case of failure
1851 */
1852
1853 static xsltCompMatchPtr
1854 xsltCompilePatternInternal(const xmlChar *pattern, xmlDocPtr doc,
1855 xmlNodePtr node, xsltStylesheetPtr style,
1856 xsltTransformContextPtr runtime, int novar) {
1857 xsltParserContextPtr ctxt = NULL;
1858 xsltCompMatchPtr element, first = NULL, previous = NULL;
1859 int current, start, end, level, j;
1860
1861 if (pattern == NULL) {
1862 xsltTransformError(NULL, NULL, node,
1863 "xsltCompilePattern : NULL pattern\n");
1864 return(NULL);
1865 }
1866
1867 ctxt = xsltNewParserContext(style, runtime);
1868 if (ctxt == NULL)
1869 return(NULL);
1870 ctxt->doc = doc;
1871 ctxt->elem = node;
1872 current = end = 0;
1873 while (pattern[current] != 0) {
1874 start = current;
1875 while (IS_BLANK_CH(pattern[current]))
1876 current++;
1877 end = current;
1878 level = 0;
1879 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1880 if (pattern[end] == '[')
1881 level++;
1882 else if (pattern[end] == ']')
1883 level--;
1884 else if (pattern[end] == '\'') {
1885 end++;
1886 while ((pattern[end] != 0) && (pattern[end] != '\''))
1887 end++;
1888 } else if (pattern[end] == '"') {
1889 end++;
1890 while ((pattern[end] != 0) && (pattern[end] != '"'))
1891 end++;
1892 }
1893 if (pattern[end] == 0)
1894 break;
1895 end++;
1896 }
1897 if (current == end) {
1898 xsltTransformError(NULL, NULL, node,
1899 "xsltCompilePattern : NULL pattern\n");
1900 goto error;
1901 }
1902 element = xsltNewCompMatch();
1903 if (element == NULL) {
1904 goto error;
1905 }
1906 if (first == NULL)
1907 first = element;
1908 else if (previous != NULL)
1909 previous->next = element;
1910 previous = element;
1911
1912 ctxt->comp = element;
1913 ctxt->base = xmlStrndup(&pattern[start], end - start);
1914 if (ctxt->base == NULL)
1915 goto error;
1916 ctxt->cur = &(ctxt->base)[current - start];
1917 element->pattern = ctxt->base;
1918 element->nsList = xmlGetNsList(doc, node);
1919 j = 0;
1920 if (element->nsList != NULL) {
1921 while (element->nsList[j] != NULL)
1922 j++;
1923 }
1924 element->nsNr = j;
1925
1926
1927 #ifdef WITH_XSLT_DEBUG_PATTERN
1928 xsltGenericDebug(xsltGenericDebugContext,
1929 "xsltCompilePattern : parsing '%s'\n",
1930 element->pattern);
1931 #endif
1932 /*
1933 Preset default priority to be zero.
1934 This may be changed by xsltCompileLocationPathPattern.
1935 */
1936 element->priority = 0;
1937 xsltCompileLocationPathPattern(ctxt, novar);
1938 if (ctxt->error) {
1939 xsltTransformError(NULL, style, node,
1940 "xsltCompilePattern : failed to compile '%s'\n",
1941 element->pattern);
1942 if (style != NULL) style->errors++;
1943 goto error;
1944 }
1945
1946 /*
1947 * Reverse for faster interpretation.
1948 */
1949 xsltReverseCompMatch(ctxt, element);
1950
1951 /*
1952 * Set-up the priority
1953 */
1954 if (element->priority == 0) { /* if not yet determined */
1955 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1956 (element->steps[0].op == XSLT_OP_ATTR) ||
1957 (element->steps[0].op == XSLT_OP_PI)) &&
1958 (element->steps[0].value != NULL) &&
1959 (element->steps[1].op == XSLT_OP_END)) {
1960 ; /* previously preset */
1961 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1962 (element->steps[0].value2 != NULL) &&
1963 (element->steps[1].op == XSLT_OP_END)) {
1964 element->priority = -0.25;
1965 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1966 (element->steps[0].value != NULL) &&
1967 (element->steps[1].op == XSLT_OP_END)) {
1968 element->priority = -0.25;
1969 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1970 (element->steps[0].value == NULL) &&
1971 (element->steps[0].value2 == NULL) &&
1972 (element->steps[1].op == XSLT_OP_END)) {
1973 element->priority = -0.5;
1974 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1975 (element->steps[0].op == XSLT_OP_TEXT) ||
1976 (element->steps[0].op == XSLT_OP_ALL) ||
1977 (element->steps[0].op == XSLT_OP_NODE) ||
1978 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1979 (element->steps[1].op == XSLT_OP_END)) {
1980 element->priority = -0.5;
1981 } else {
1982 element->priority = 0.5;
1983 }
1984 }
1985 #ifdef WITH_XSLT_DEBUG_PATTERN
1986 xsltGenericDebug(xsltGenericDebugContext,
1987 "xsltCompilePattern : parsed %s, default priority %f\n",
1988 element->pattern, element->priority);
1989 #endif
1990 if (pattern[end] == '|')
1991 end++;
1992 current = end;
1993 }
1994 if (end == 0) {
1995 xsltTransformError(NULL, style, node,
1996 "xsltCompilePattern : NULL pattern\n");
1997 if (style != NULL) style->errors++;
1998 goto error;
1999 }
2000
2001 xsltFreeParserContext(ctxt);
2002 return(first);
2003
2004 error:
2005 if (ctxt != NULL)
2006 xsltFreeParserContext(ctxt);
2007 if (first != NULL)
2008 xsltFreeCompMatchList(first);
2009 return(NULL);
2010 }
2011
2012 /**
2013 * xsltCompilePattern:
2014 * @pattern: an XSLT pattern
2015 * @doc: the containing document
2016 * @node: the containing element
2017 * @style: the stylesheet
2018 * @runtime: the transformation context, if done at run-time
2019 *
2020 * Compile the XSLT pattern and generates a list of precompiled form suitable
2021 * for fast matching.
2022 *
2023 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
2024 *
2025 * Returns the generated pattern list or NULL in case of failure
2026 */
2027
2028 xsltCompMatchPtr
2029 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
2030 xmlNodePtr node, xsltStylesheetPtr style,
2031 xsltTransformContextPtr runtime) {
2032 return (xsltCompilePatternInternal(pattern, doc, node, style, runtime, 0));
2033 }
2034
2035 /************************************************************************
2036 * *
2037 * Module interfaces *
2038 * *
2039 ************************************************************************/
2040
2041 /**
2042 * xsltAddTemplate:
2043 * @style: an XSLT stylesheet
2044 * @cur: an XSLT template
2045 * @mode: the mode name or NULL
2046 * @modeURI: the mode URI or NULL
2047 *
2048 * Register the XSLT pattern associated to @cur
2049 *
2050 * Returns -1 in case of error, 0 otherwise
2051 */
2052 int
2053 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
2054 const xmlChar *mode, const xmlChar *modeURI) {
2055 xsltCompMatchPtr pat, list, next;
2056 /*
2057 * 'top' will point to style->xxxMatch ptr - declaring as 'void'
2058 * avoids gcc 'type-punned pointer' warning.
2059 */
2060 void **top = NULL;
2061 const xmlChar *name = NULL;
2062 float priority; /* the priority */
2063
2064 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
2065 return(-1);
2066
2067 priority = cur->priority;
2068 pat = xsltCompilePatternInternal(cur->match, style->doc, cur->elem,
2069 style, NULL, 1);
2070 if (pat == NULL)
2071 return(-1);
2072 while (pat) {
2073 next = pat->next;
2074 pat->next = NULL;
2075 name = NULL;
2076
2077 pat->template = cur;
2078 if (mode != NULL)
2079 pat->mode = xmlDictLookup(style->dict, mode, -1);
2080 if (modeURI != NULL)
2081 pat->modeURI = xmlDictLookup(style->dict, modeURI, -1);
2082 if (priority != XSLT_PAT_NO_PRIORITY)
2083 pat->priority = priority;
2084
2085 /*
2086 * insert it in the hash table list corresponding to its lookup name
2087 */
2088 switch (pat->steps[0].op) {
2089 case XSLT_OP_ATTR:
2090 if (pat->steps[0].value != NULL)
2091 name = pat->steps[0].value;
2092 else
2093 top = &(style->attrMatch);
2094 break;
2095 case XSLT_OP_PARENT:
2096 case XSLT_OP_ANCESTOR:
2097 top = &(style->elemMatch);
2098 break;
2099 case XSLT_OP_ROOT:
2100 top = &(style->rootMatch);
2101 break;
2102 case XSLT_OP_KEY:
2103 top = &(style->keyMatch);
2104 break;
2105 case XSLT_OP_ID:
2106 /* TODO optimize ID !!! */
2107 case XSLT_OP_NS:
2108 case XSLT_OP_ALL:
2109 top = &(style->elemMatch);
2110 break;
2111 case XSLT_OP_END:
2112 case XSLT_OP_PREDICATE:
2113 xsltTransformError(NULL, style, NULL,
2114 "xsltAddTemplate: invalid compiled pattern\n");
2115 xsltFreeCompMatch(pat);
2116 return(-1);
2117 /*
2118 * TODO: some flags at the top level about type based patterns
2119 * would be faster than inclusion in the hash table.
2120 */
2121 case XSLT_OP_PI:
2122 if (pat->steps[0].value != NULL)
2123 name = pat->steps[0].value;
2124 else
2125 top = &(style->piMatch);
2126 break;
2127 case XSLT_OP_COMMENT:
2128 top = &(style->commentMatch);
2129 break;
2130 case XSLT_OP_TEXT:
2131 top = &(style->textMatch);
2132 break;
2133 case XSLT_OP_ELEM:
2134 case XSLT_OP_NODE:
2135 if (pat->steps[0].value != NULL)
2136 name = pat->steps[0].value;
2137 else
2138 top = &(style->elemMatch);
2139 break;
2140 }
2141 if (name != NULL) {
2142 if (style->templatesHash == NULL) {
2143 style->templatesHash = xmlHashCreate(1024);
2144 if (style->templatesHash == NULL) {
2145 xsltFreeCompMatch(pat);
2146 return(-1);
2147 }
2148 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
2149 } else {
2150 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
2151 name, mode, modeURI);
2152 if (list == NULL) {
2153 xmlHashAddEntry3(style->templatesHash, name,
2154 mode, modeURI, pat);
2155 } else {
2156 /*
2157 * Note '<=' since one must choose among the matching
2158 * template rules that are left, the one that occurs
2159 * last in the stylesheet
2160 */
2161 if (list->priority <= pat->priority) {
2162 pat->next = list;
2163 xmlHashUpdateEntry3(style->templatesHash, name,
2164 mode, modeURI, pat, NULL);
2165 } else {
2166 while (list->next != NULL) {
2167 if (list->next->priority <= pat->priority)
2168 break;
2169 list = list->next;
2170 }
2171 pat->next = list->next;
2172 list->next = pat;
2173 }
2174 }
2175 }
2176 } else if (top != NULL) {
2177 list = *top;
2178 if (list == NULL) {
2179 *top = pat;
2180 pat->next = NULL;
2181 } else if (list->priority <= pat->priority) {
2182 pat->next = list;
2183 *top = pat;
2184 } else {
2185 while (list->next != NULL) {
2186 if (list->next->priority <= pat->priority)
2187 break;
2188 list = list->next;
2189 }
2190 pat->next = list->next;
2191 list->next = pat;
2192 }
2193 } else {
2194 xsltTransformError(NULL, style, NULL,
2195 "xsltAddTemplate: invalid compiled pattern\n");
2196 xsltFreeCompMatch(pat);
2197 return(-1);
2198 }
2199 #ifdef WITH_XSLT_DEBUG_PATTERN
2200 if (mode)
2201 xsltGenericDebug(xsltGenericDebugContext,
2202 "added pattern : '%s' mode '%s' priority %f\n",
2203 pat->pattern, pat->mode, pat->priority);
2204 else
2205 xsltGenericDebug(xsltGenericDebugContext,
2206 "added pattern : '%s' priority %f\n",
2207 pat->pattern, pat->priority);
2208 #endif
2209
2210 pat = next;
2211 }
2212 return(0);
2213 }
2214
2215 static int
2216 xsltComputeAllKeys(xsltTransformContextPtr ctxt, xmlNodePtr contextNode)
2217 {
2218 if ((ctxt == NULL) || (contextNode == NULL)) {
2219 xsltTransformError(ctxt, NULL, ctxt->inst,
2220 "Internal error in xsltComputeAllKeys(): "
2221 "Bad arguments.\n");
2222 return(-1);
2223 }
2224
2225 if (ctxt->document == NULL) {
2226 /*
2227 * The document info will only be NULL if we have a RTF.
2228 */
2229 if (contextNode->doc->_private != NULL)
2230 goto doc_info_mismatch;
2231 /*
2232 * On-demand creation of the document info (needed for keys).
2233 */
2234 ctxt->document = xsltNewDocument(ctxt, contextNode->doc);
2235 if (ctxt->document == NULL)
2236 return(-1);
2237 }
2238 return xsltInitAllDocKeys(ctxt);
2239
2240 doc_info_mismatch:
2241 xsltTransformError(ctxt, NULL, ctxt->inst,
2242 "Internal error in xsltComputeAllKeys(): "
2243 "The context's document info doesn't match the "
2244 "document info of the current result tree.\n");
2245 ctxt->state = XSLT_STATE_STOPPED;
2246 return(-1);
2247 }
2248
2249 /**
2250 * xsltGetTemplate:
2251 * @ctxt: a XSLT process context
2252 * @node: the node being processed
2253 * @style: the current style
2254 *
2255 * Finds the template applying to this node, if @style is non-NULL
2256 * it means one needs to look for the next imported template in scope.
2257 *
2258 * Returns the xsltTemplatePtr or NULL if not found
2259 */
2260 xsltTemplatePtr
2261 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2262 xsltStylesheetPtr style)
2263 {
2264 xsltStylesheetPtr curstyle;
2265 xsltTemplatePtr ret = NULL;
2266 const xmlChar *name = NULL;
2267 xsltCompMatchPtr list = NULL;
2268 float priority;
2269 int keyed = 0;
2270
2271 if ((ctxt == NULL) || (node == NULL))
2272 return(NULL);
2273
2274 if (style == NULL) {
2275 curstyle = ctxt->style;
2276 } else {
2277 curstyle = xsltNextImport(style);
2278 }
2279
2280 while ((curstyle != NULL) && (curstyle != style)) {
2281 priority = XSLT_PAT_NO_PRIORITY;
2282 /* TODO : handle IDs/keys here ! */
2283 if (curstyle->templatesHash != NULL) {
2284 /*
2285 * Use the top name as selector
2286 */
2287 switch (node->type) {
2288 case XML_ELEMENT_NODE:
2289 if (node->name[0] == ' ')
2290 break;
2291 case XML_ATTRIBUTE_NODE:
2292 case XML_PI_NODE:
2293 name = node->name;
2294 break;
2295 case XML_DOCUMENT_NODE:
2296 case XML_HTML_DOCUMENT_NODE:
2297 case XML_TEXT_NODE:
2298 case XML_CDATA_SECTION_NODE:
2299 case XML_COMMENT_NODE:
2300 case XML_ENTITY_REF_NODE:
2301 case XML_ENTITY_NODE:
2302 case XML_DOCUMENT_TYPE_NODE:
2303 case XML_DOCUMENT_FRAG_NODE:
2304 case XML_NOTATION_NODE:
2305 case XML_DTD_NODE:
2306 case XML_ELEMENT_DECL:
2307 case XML_ATTRIBUTE_DECL:
2308 case XML_ENTITY_DECL:
2309 case XML_NAMESPACE_DECL:
2310 case XML_XINCLUDE_START:
2311 case XML_XINCLUDE_END:
2312 break;
2313 default:
2314 return(NULL);
2315
2316 }
2317 }
2318 if (name != NULL) {
2319 /*
2320 * find the list of applicable expressions based on the name
2321 */
2322 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2323 name, ctxt->mode, ctxt->modeURI);
2324 } else
2325 list = NULL;
2326 while (list != NULL) {
2327 if (xsltTestCompMatch(ctxt, list, node,
2328 ctxt->mode, ctxt->modeURI)) {
2329 ret = list->template;
2330 priority = list->priority;
2331 break;
2332 }
2333 list = list->next;
2334 }
2335 list = NULL;
2336
2337 /*
2338 * find alternate generic matches
2339 */
2340 switch (node->type) {
2341 case XML_ELEMENT_NODE:
2342 if (node->name[0] == ' ')
2343 list = curstyle->rootMatch;
2344 else
2345 list = curstyle->elemMatch;
2346 if (node->psvi != NULL) keyed = 1;
2347 break;
2348 case XML_ATTRIBUTE_NODE: {
2349 xmlAttrPtr attr;
2350
2351 list = curstyle->attrMatch;
2352 attr = (xmlAttrPtr) node;
2353 if (attr->psvi != NULL) keyed = 1;
2354 break;
2355 }
2356 case XML_PI_NODE:
2357 list = curstyle->piMatch;
2358 if (node->psvi != NULL) keyed = 1;
2359 break;
2360 case XML_DOCUMENT_NODE:
2361 case XML_HTML_DOCUMENT_NODE: {
2362 xmlDocPtr doc;
2363
2364 list = curstyle->rootMatch;
2365 doc = (xmlDocPtr) node;
2366 if (doc->psvi != NULL) keyed = 1;
2367 break;
2368 }
2369 case XML_TEXT_NODE:
2370 case XML_CDATA_SECTION_NODE:
2371 list = curstyle->textMatch;
2372 if (node->psvi != NULL) keyed = 1;
2373 break;
2374 case XML_COMMENT_NODE:
2375 list = curstyle->commentMatch;
2376 if (node->psvi != NULL) keyed = 1;
2377 break;
2378 case XML_ENTITY_REF_NODE:
2379 case XML_ENTITY_NODE:
2380 case XML_DOCUMENT_TYPE_NODE:
2381 case XML_DOCUMENT_FRAG_NODE:
2382 case XML_NOTATION_NODE:
2383 case XML_DTD_NODE:
2384 case XML_ELEMENT_DECL:
2385 case XML_ATTRIBUTE_DECL:
2386 case XML_ENTITY_DECL:
2387 case XML_NAMESPACE_DECL:
2388 case XML_XINCLUDE_START:
2389 case XML_XINCLUDE_END:
2390 break;
2391 default:
2392 break;
2393 }
2394 while ((list != NULL) &&
2395 ((ret == NULL) || (list->priority > priority))) {
2396 if (xsltTestCompMatch(ctxt, list, node,
2397 ctxt->mode, ctxt->modeURI)) {
2398 ret = list->template;
2399 priority = list->priority;
2400 break;
2401 }
2402 list = list->next;
2403 }
2404 /*
2405 * Some of the tests for elements can also apply to documents
2406 */
2407 if ((node->type == XML_DOCUMENT_NODE) ||
2408 (node->type == XML_HTML_DOCUMENT_NODE) ||
2409 (node->type == XML_TEXT_NODE)) {
2410 list = curstyle->elemMatch;
2411 while ((list != NULL) &&
2412 ((ret == NULL) || (list->priority > priority))) {
2413 if (xsltTestCompMatch(ctxt, list, node,
2414 ctxt->mode, ctxt->modeURI)) {
2415 ret = list->template;
2416 priority = list->priority;
2417 break;
2418 }
2419 list = list->next;
2420 }
2421 } else if ((node->type == XML_PI_NODE) ||
2422 (node->type == XML_COMMENT_NODE)) {
2423 list = curstyle->elemMatch;
2424 while ((list != NULL) &&
2425 ((ret == NULL) || (list->priority > priority))) {
2426 if (xsltTestCompMatch(ctxt, list, node,
2427 ctxt->mode, ctxt->modeURI)) {
2428 ret = list->template;
2429 priority = list->priority;
2430 break;
2431 }
2432 list = list->next;
2433 }
2434 }
2435
2436 keyed_match:
2437 if (keyed) {
2438 list = curstyle->keyMatch;
2439 while ((list != NULL) &&
2440 ((ret == NULL) || (list->priority > priority))) {
2441 if (xsltTestCompMatch(ctxt, list, node,
2442 ctxt->mode, ctxt->modeURI)) {
2443 ret = list->template;
2444 priority = list->priority;
2445 break;
2446 }
2447 list = list->next;
2448 }
2449 }
2450 else if (ctxt->hasTemplKeyPatterns &&
2451 ((ctxt->document == NULL) ||
2452 (ctxt->document->nbKeysComputed < ctxt->nbKeys)))
2453 {
2454 /*
2455 * Compute all remaining keys for this document.
2456 *
2457 * REVISIT TODO: I think this could be further optimized.
2458 */
2459 if (xsltComputeAllKeys(ctxt, node) == -1)
2460 goto error;
2461
2462 switch (node->type) {
2463 case XML_ELEMENT_NODE:
2464 if (node->psvi != NULL) keyed = 1;
2465 break;
2466 case XML_ATTRIBUTE_NODE:
2467 if (((xmlAttrPtr) node)->psvi != NULL) keyed = 1;
2468 break;
2469 case XML_TEXT_NODE:
2470 case XML_CDATA_SECTION_NODE:
2471 case XML_COMMENT_NODE:
2472 case XML_PI_NODE:
2473 if (node->psvi != NULL) keyed = 1;
2474 break;
2475 case XML_DOCUMENT_NODE:
2476 case XML_HTML_DOCUMENT_NODE:
2477 if (((xmlDocPtr) node)->psvi != NULL) keyed = 1;
2478 break;
2479 default:
2480 break;
2481 }
2482 if (keyed)
2483 goto keyed_match;
2484 }
2485 if (ret != NULL)
2486 return(ret);
2487
2488 /*
2489 * Cycle on next curstylesheet import.
2490 */
2491 curstyle = xsltNextImport(curstyle);
2492 }
2493
2494 error:
2495 return(NULL);
2496 }
2497
2498 /**
2499 * xsltCleanupTemplates:
2500 * @style: an XSLT stylesheet
2501 *
2502 * Cleanup the state of the templates used by the stylesheet and
2503 * the ones it imports.
2504 */
2505 void
2506 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2507 }
2508
2509 /**
2510 * xsltFreeTemplateHashes:
2511 * @style: an XSLT stylesheet
2512 *
2513 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2514 */
2515 void
2516 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2517 if (style->templatesHash != NULL)
2518 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2519 (xmlHashDeallocator) xsltFreeCompMatchList);
2520 if (style->rootMatch != NULL)
2521 xsltFreeCompMatchList(style->rootMatch);
2522 if (style->keyMatch != NULL)
2523 xsltFreeCompMatchList(style->keyMatch);
2524 if (style->elemMatch != NULL)
2525 xsltFreeCompMatchList(style->elemMatch);
2526 if (style->attrMatch != NULL)
2527 xsltFreeCompMatchList(style->attrMatch);
2528 if (style->parentMatch != NULL)
2529 xsltFreeCompMatchList(style->parentMatch);
2530 if (style->textMatch != NULL)
2531 xsltFreeCompMatchList(style->textMatch);
2532 if (style->piMatch != NULL)
2533 xsltFreeCompMatchList(style->piMatch);
2534 if (style->commentMatch != NULL)
2535 xsltFreeCompMatchList(style->commentMatch);
2536 }
2537