2 * pattern.c: Implemetation of selectors for nodes
5 * http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/
7 * http://www.w3.org/TR/1999/REC-xml-19991116
9 * See Copyright for the status of this software.
16 * - compilation flags to check for specific syntaxes
17 * using flags of xmlPatterncompile()
18 * - making clear how pattern starting with / or . need to be handled,
19 * currently push(NULL, NULL) means a reset of the streaming context
20 * and indicating we are on / (the document node), probably need
21 * something similar for .
22 * - get rid of the "compile" starting with lowercase
23 * - DONE (2006-05-16): get rid of the Strdup/Strndup in case of dictionary
30 #include <libxml/xmlmemory.h>
31 #include <libxml/tree.h>
32 #include <libxml/hash.h>
33 #include <libxml/dict.h>
34 #include <libxml/xmlerror.h>
35 #include <libxml/parserInternals.h>
36 #include <libxml/pattern.h>
38 #ifdef LIBXML_PATTERN_ENABLED
40 /* #define DEBUG_STREAMING */
45 #define ERROR(a, b, c, d)
46 #define ERROR5(a, b, c, d, e)
48 #define XML_STREAM_STEP_DESC 1
49 #define XML_STREAM_STEP_FINAL 2
50 #define XML_STREAM_STEP_ROOT 4
51 #define XML_STREAM_STEP_ATTR 8
52 #define XML_STREAM_STEP_NODE 16
53 #define XML_STREAM_STEP_IN_SET 32
56 * NOTE: Those private flags (XML_STREAM_xxx) are used
57 * in _xmlStreamCtxt->flag. They extend the public
58 * xmlPatternFlags, so be carefull not to interfere with the
59 * reserved values for xmlPatternFlags.
61 #define XML_STREAM_FINAL_IS_ANY_NODE 1<<14
62 #define XML_STREAM_FROM_ROOT 1<<15
63 #define XML_STREAM_DESC 1<<16
66 * XML_STREAM_ANY_NODE is used for comparison against
67 * xmlElementType enums, to indicate a node of any type.
69 #define XML_STREAM_ANY_NODE 100
71 #define XML_PATTERN_NOTPATTERN (XML_PATTERN_XPATH | \
75 #define XML_STREAM_XS_IDC(c) ((c)->flags & \
76 (XML_PATTERN_XSSEL | XML_PATTERN_XSFIELD))
78 #define XML_STREAM_XS_IDC_SEL(c) ((c)->flags & XML_PATTERN_XSSEL)
80 #define XML_STREAM_XS_IDC_FIELD(c) ((c)->flags & XML_PATTERN_XSFIELD)
82 #define XML_PAT_COPY_NSNAME(c, r, nsname) \
83 if ((c)->comp->dict) \
84 r = (xmlChar *) xmlDictLookup((c)->comp->dict, BAD_CAST nsname, -1); \
85 else r = xmlStrdup(BAD_CAST nsname);
87 #define XML_PAT_FREE_STRING(c, r) if ((c)->comp->dict == NULL) xmlFree(r);
89 typedef struct _xmlStreamStep xmlStreamStep
;
90 typedef xmlStreamStep
*xmlStreamStepPtr
;
91 struct _xmlStreamStep
{
92 int flags
; /* properties of that step */
93 const xmlChar
*name
; /* first string value if NULL accept all */
94 const xmlChar
*ns
; /* second string value */
95 int nodeType
; /* type of node */
98 typedef struct _xmlStreamComp xmlStreamComp
;
99 typedef xmlStreamComp
*xmlStreamCompPtr
;
100 struct _xmlStreamComp
{
101 xmlDict
*dict
; /* the dictionary if any */
102 int nbStep
; /* number of steps in the automata */
103 int maxStep
; /* allocated number of steps */
104 xmlStreamStepPtr steps
; /* the array of steps */
108 struct _xmlStreamCtxt
{
109 struct _xmlStreamCtxt
*next
;/* link to next sub pattern if | */
110 xmlStreamCompPtr comp
; /* the compiled stream */
111 int nbState
; /* number of states in the automata */
112 int maxState
; /* allocated number of states */
113 int level
; /* how deep are we ? */
114 int *states
; /* the array of step indexes */
115 int flags
; /* validation options */
119 static void xmlFreeStreamComp(xmlStreamCompPtr comp
);
138 typedef struct _xmlStepState xmlStepState
;
139 typedef xmlStepState
*xmlStepStatePtr
;
140 struct _xmlStepState
{
145 typedef struct _xmlStepStates xmlStepStates
;
146 typedef xmlStepStates
*xmlStepStatesPtr
;
147 struct _xmlStepStates
{
150 xmlStepStatePtr states
;
153 typedef struct _xmlStepOp xmlStepOp
;
154 typedef xmlStepOp
*xmlStepOpPtr
;
157 const xmlChar
*value
;
158 const xmlChar
*value2
; /* The namespace name */
161 #define PAT_FROM_ROOT (1<<8)
162 #define PAT_FROM_CUR (1<<9)
165 void *data
; /* the associated template */
166 xmlDictPtr dict
; /* the optional dictionary */
167 struct _xmlPattern
*next
; /* next pattern if | is used */
168 const xmlChar
*pattern
; /* the pattern */
169 int flags
; /* flags */
172 xmlStepOpPtr steps
; /* ops for computation */
173 xmlStreamCompPtr stream
; /* the streaming data if any */
176 typedef struct _xmlPatParserContext xmlPatParserContext
;
177 typedef xmlPatParserContext
*xmlPatParserContextPtr
;
178 struct _xmlPatParserContext
{
179 const xmlChar
*cur
; /* the current char being parsed */
180 const xmlChar
*base
; /* the full expression */
181 int error
; /* error code */
182 xmlDictPtr dict
; /* the dictionary if any */
183 xmlPatternPtr comp
; /* the result */
184 xmlNodePtr elem
; /* the current node if any */
185 const xmlChar
**namespaces
; /* the namespaces definitions */
186 int nb_namespaces
; /* the number of namespaces */
189 /************************************************************************
193 ************************************************************************/
198 * Create a new XSLT Pattern
200 * Returns the newly allocated xmlPatternPtr or NULL in case of error
203 xmlNewPattern(void) {
206 cur
= (xmlPatternPtr
) xmlMalloc(sizeof(xmlPattern
));
208 ERROR(NULL
, NULL
, NULL
,
209 "xmlNewPattern : malloc failed\n");
212 memset(cur
, 0, sizeof(xmlPattern
));
214 cur
->steps
= (xmlStepOpPtr
) xmlMalloc(cur
->maxStep
* sizeof(xmlStepOp
));
215 if (cur
->steps
== NULL
) {
217 ERROR(NULL
, NULL
, NULL
,
218 "xmlNewPattern : malloc failed\n");
226 * @comp: an XSLT comp
228 * Free up the memory allocated by @comp
231 xmlFreePattern(xmlPatternPtr comp
) {
237 if (comp
->next
!= NULL
)
238 xmlFreePattern(comp
->next
);
239 if (comp
->stream
!= NULL
)
240 xmlFreeStreamComp(comp
->stream
);
241 if (comp
->pattern
!= NULL
)
242 xmlFree((xmlChar
*)comp
->pattern
);
243 if (comp
->steps
!= NULL
) {
244 if (comp
->dict
== NULL
) {
245 for (i
= 0;i
< comp
->nbStep
;i
++) {
246 op
= &comp
->steps
[i
];
247 if (op
->value
!= NULL
)
248 xmlFree((xmlChar
*) op
->value
);
249 if (op
->value2
!= NULL
)
250 xmlFree((xmlChar
*) op
->value2
);
253 xmlFree(comp
->steps
);
255 if (comp
->dict
!= NULL
)
256 xmlDictFree(comp
->dict
);
258 memset(comp
, -1, sizeof(xmlPattern
));
263 * xmlFreePatternList:
264 * @comp: an XSLT comp list
266 * Free up the memory allocated by all the elements of @comp
269 xmlFreePatternList(xmlPatternPtr comp
) {
272 while (comp
!= NULL
) {
281 * xmlNewPatParserContext:
282 * @pattern: the pattern context
283 * @dict: the inherited dictionary or NULL
284 * @namespaces: the prefix definitions, array of [URI, prefix] terminated
285 * with [NULL, NULL] or NULL if no namespace is used
287 * Create a new XML pattern parser context
289 * Returns the newly allocated xmlPatParserContextPtr or NULL in case of error
291 static xmlPatParserContextPtr
292 xmlNewPatParserContext(const xmlChar
*pattern
, xmlDictPtr dict
,
293 const xmlChar
**namespaces
) {
294 xmlPatParserContextPtr cur
;
299 cur
= (xmlPatParserContextPtr
) xmlMalloc(sizeof(xmlPatParserContext
));
301 ERROR(NULL
, NULL
, NULL
,
302 "xmlNewPatParserContext : malloc failed\n");
305 memset(cur
, 0, sizeof(xmlPatParserContext
));
309 if (namespaces
!= NULL
) {
311 for (i
= 0;namespaces
[2 * i
] != NULL
;i
++)
313 cur
->nb_namespaces
= i
;
315 cur
->nb_namespaces
= 0;
317 cur
->namespaces
= namespaces
;
322 * xmlFreePatParserContext:
323 * @ctxt: an XSLT parser context
325 * Free up the memory allocated by @ctxt
328 xmlFreePatParserContext(xmlPatParserContextPtr ctxt
) {
331 memset(ctxt
, -1, sizeof(xmlPatParserContext
));
337 * @comp: the compiled match expression
339 * @value: the first value
340 * @value2: the second value
342 * Add a step to an XSLT Compiled Match
344 * Returns -1 in case of failure, 0 otherwise.
347 xmlPatternAdd(xmlPatParserContextPtr ctxt ATTRIBUTE_UNUSED
,
349 xmlPatOp op
, xmlChar
* value
, xmlChar
* value2
)
351 if (comp
->nbStep
>= comp
->maxStep
) {
353 temp
= (xmlStepOpPtr
) xmlRealloc(comp
->steps
, comp
->maxStep
* 2 *
356 ERROR(ctxt
, NULL
, NULL
,
357 "xmlPatternAdd: realloc failed\n");
363 comp
->steps
[comp
->nbStep
].op
= op
;
364 comp
->steps
[comp
->nbStep
].value
= value
;
365 comp
->steps
[comp
->nbStep
].value2
= value2
;
372 * xsltSwapTopPattern:
373 * @comp: the compiled match expression
375 * reverse the two top steps.
378 xsltSwapTopPattern(xmlPatternPtr comp
) {
380 int j
= comp
->nbStep
- 1;
383 register const xmlChar
*tmp
;
384 register xmlPatOp op
;
386 tmp
= comp
->steps
[i
].value
;
387 comp
->steps
[i
].value
= comp
->steps
[j
].value
;
388 comp
->steps
[j
].value
= tmp
;
389 tmp
= comp
->steps
[i
].value2
;
390 comp
->steps
[i
].value2
= comp
->steps
[j
].value2
;
391 comp
->steps
[j
].value2
= tmp
;
392 op
= comp
->steps
[i
].op
;
393 comp
->steps
[i
].op
= comp
->steps
[j
].op
;
394 comp
->steps
[j
].op
= op
;
401 * @comp: the compiled match expression
403 * reverse all the stack of expressions
405 * returns 0 in case of success and -1 in case of error.
408 xmlReversePattern(xmlPatternPtr comp
) {
412 * remove the leading // for //a or .//a
414 if ((comp
->nbStep
> 0) && (comp
->steps
[0].op
== XML_OP_ANCESTOR
)) {
415 for (i
= 0, j
= 1;j
< comp
->nbStep
;i
++,j
++) {
416 comp
->steps
[i
].value
= comp
->steps
[j
].value
;
417 comp
->steps
[i
].value2
= comp
->steps
[j
].value2
;
418 comp
->steps
[i
].op
= comp
->steps
[j
].op
;
422 if (comp
->nbStep
>= comp
->maxStep
) {
424 temp
= (xmlStepOpPtr
) xmlRealloc(comp
->steps
, comp
->maxStep
* 2 *
427 ERROR(ctxt
, NULL
, NULL
,
428 "xmlReversePattern: realloc failed\n");
435 j
= comp
->nbStep
- 1;
437 register const xmlChar
*tmp
;
438 register xmlPatOp op
;
439 tmp
= comp
->steps
[i
].value
;
440 comp
->steps
[i
].value
= comp
->steps
[j
].value
;
441 comp
->steps
[j
].value
= tmp
;
442 tmp
= comp
->steps
[i
].value2
;
443 comp
->steps
[i
].value2
= comp
->steps
[j
].value2
;
444 comp
->steps
[j
].value2
= tmp
;
445 op
= comp
->steps
[i
].op
;
446 comp
->steps
[i
].op
= comp
->steps
[j
].op
;
447 comp
->steps
[j
].op
= op
;
451 comp
->steps
[comp
->nbStep
].value
= NULL
;
452 comp
->steps
[comp
->nbStep
].value2
= NULL
;
453 comp
->steps
[comp
->nbStep
++].op
= XML_OP_END
;
457 /************************************************************************
459 * The interpreter for the precompiled patterns *
461 ************************************************************************/
464 xmlPatPushState(xmlStepStates
*states
, int step
, xmlNodePtr node
) {
465 if ((states
->states
== NULL
) || (states
->maxstates
<= 0)) {
466 states
->maxstates
= 4;
467 states
->nbstates
= 0;
468 states
->states
= xmlMalloc(4 * sizeof(xmlStepState
));
470 else if (states
->maxstates
<= states
->nbstates
) {
473 tmp
= (xmlStepStatePtr
) xmlRealloc(states
->states
,
474 2 * states
->maxstates
* sizeof(xmlStepState
));
477 states
->states
= tmp
;
478 states
->maxstates
*= 2;
480 states
->states
[states
->nbstates
].step
= step
;
481 states
->states
[states
->nbstates
++].node
= node
;
483 fprintf(stderr
, "Push: %d, %s\n", step
, node
->name
);
490 * @comp: the precompiled pattern
493 * Test whether the node matches the pattern
495 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
498 xmlPatMatch(xmlPatternPtr comp
, xmlNodePtr node
) {
501 xmlStepStates states
= {0, 0, NULL
}; /* // may require backtrack */
503 if ((comp
== NULL
) || (node
== NULL
)) return(-1);
506 for (;i
< comp
->nbStep
;i
++) {
507 step
= &comp
->steps
[i
];
512 if (node
->type
== XML_NAMESPACE_DECL
)
515 if ((node
->type
== XML_DOCUMENT_NODE
) ||
516 #ifdef LIBXML_DOCB_ENABLED
517 (node
->type
== XML_DOCB_DOCUMENT_NODE
) ||
519 (node
->type
== XML_HTML_DOCUMENT_NODE
))
523 if (node
->type
!= XML_ELEMENT_NODE
)
525 if (step
->value
== NULL
)
527 if (step
->value
[0] != node
->name
[0])
529 if (!xmlStrEqual(step
->value
, node
->name
))
533 if (node
->ns
== NULL
) {
534 if (step
->value2
!= NULL
)
536 } else if (node
->ns
->href
!= NULL
) {
537 if (step
->value2
== NULL
)
539 if (!xmlStrEqual(step
->value2
, node
->ns
->href
))
546 if ((node
->type
!= XML_ELEMENT_NODE
) &&
547 (node
->type
!= XML_DOCUMENT_NODE
) &&
548 #ifdef LIBXML_DOCB_ENABLED
549 (node
->type
!= XML_DOCB_DOCUMENT_NODE
) &&
551 (node
->type
!= XML_HTML_DOCUMENT_NODE
))
554 lst
= node
->children
;
556 if (step
->value
!= NULL
) {
557 while (lst
!= NULL
) {
558 if ((lst
->type
== XML_ELEMENT_NODE
) &&
559 (step
->value
[0] == lst
->name
[0]) &&
560 (xmlStrEqual(step
->value
, lst
->name
)))
570 if (node
->type
!= XML_ATTRIBUTE_NODE
)
572 if (step
->value
!= NULL
) {
573 if (step
->value
[0] != node
->name
[0])
575 if (!xmlStrEqual(step
->value
, node
->name
))
579 if (node
->ns
== NULL
) {
580 if (step
->value2
!= NULL
)
582 } else if (step
->value2
!= NULL
) {
583 if (!xmlStrEqual(step
->value2
, node
->ns
->href
))
588 if ((node
->type
== XML_DOCUMENT_NODE
) ||
589 (node
->type
== XML_HTML_DOCUMENT_NODE
) ||
590 #ifdef LIBXML_DOCB_ENABLED
591 (node
->type
== XML_DOCB_DOCUMENT_NODE
) ||
593 (node
->type
== XML_NAMESPACE_DECL
))
598 if (step
->value
== NULL
)
600 if (step
->value
[0] != node
->name
[0])
602 if (!xmlStrEqual(step
->value
, node
->name
))
605 if (node
->ns
== NULL
) {
606 if (step
->value2
!= NULL
)
608 } else if (node
->ns
->href
!= NULL
) {
609 if (step
->value2
== NULL
)
611 if (!xmlStrEqual(step
->value2
, node
->ns
->href
))
615 case XML_OP_ANCESTOR
:
616 /* TODO: implement coalescing of ANCESTOR/NODE ops */
617 if (step
->value
== NULL
) {
619 step
= &comp
->steps
[i
];
620 if (step
->op
== XML_OP_ROOT
)
622 if (step
->op
!= XML_OP_ELEM
)
624 if (step
->value
== NULL
)
629 if ((node
->type
== XML_DOCUMENT_NODE
) ||
630 (node
->type
== XML_HTML_DOCUMENT_NODE
) ||
631 #ifdef LIBXML_DOCB_ENABLED
632 (node
->type
== XML_DOCB_DOCUMENT_NODE
) ||
634 (node
->type
== XML_NAMESPACE_DECL
))
637 while (node
!= NULL
) {
638 if ((node
->type
== XML_ELEMENT_NODE
) &&
639 (step
->value
[0] == node
->name
[0]) &&
640 (xmlStrEqual(step
->value
, node
->name
))) {
642 if (node
->ns
== NULL
) {
643 if (step
->value2
== NULL
)
645 } else if (node
->ns
->href
!= NULL
) {
646 if ((step
->value2
!= NULL
) &&
647 (xmlStrEqual(step
->value2
, node
->ns
->href
)))
656 * prepare a potential rollback from here
657 * for ancestors of that node.
659 if (step
->op
== XML_OP_ANCESTOR
)
660 xmlPatPushState(&states
, i
, node
);
662 xmlPatPushState(&states
, i
- 1, node
);
665 if (node
->type
!= XML_ELEMENT_NODE
)
667 if (node
->ns
== NULL
) {
668 if (step
->value
!= NULL
)
670 } else if (node
->ns
->href
!= NULL
) {
671 if (step
->value
== NULL
)
673 if (!xmlStrEqual(step
->value
, node
->ns
->href
))
678 if (node
->type
!= XML_ELEMENT_NODE
)
684 if (states
.states
!= NULL
) {
685 /* Free the rollback states */
686 xmlFree(states
.states
);
690 /* got an error try to rollback */
691 if (states
.states
== NULL
)
693 if (states
.nbstates
<= 0) {
694 xmlFree(states
.states
);
698 i
= states
.states
[states
.nbstates
].step
;
699 node
= states
.states
[states
.nbstates
].node
;
701 fprintf(stderr
, "Pop: %d, %s\n", i
, node
->name
);
706 /************************************************************************
708 * Dedicated parser for templates *
710 ************************************************************************/
713 xmlGenericError(xmlGenericErrorContext, \
714 "Unimplemented block at %s:%d\n", \
716 #define CUR (*ctxt->cur)
717 #define SKIP(val) ctxt->cur += (val)
718 #define NXT(val) ctxt->cur[(val)]
719 #define PEEKPREV(val) ctxt->cur[-(val)]
720 #define CUR_PTR ctxt->cur
722 #define SKIP_BLANKS \
723 while (IS_BLANK_CH(CUR)) NEXT
725 #define CURRENT (*ctxt->cur)
726 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
729 #define PUSH(op, val, val2) \
730 if (xmlPatternAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
732 #define XSLT_ERROR(X) \
733 { xsltError(ctxt, __FILE__, __LINE__, X); \
734 ctxt->error = (X); return; }
736 #define XSLT_ERROR0(X) \
737 { xsltError(ctxt, __FILE__, __LINE__, X); \
738 ctxt->error = (X); return(0); }
743 * @ctxt: the XPath Parser context
745 * Parse an XPath Litteral:
747 * [29] Literal ::= '"' [^"]* '"'
750 * Returns the Literal parsed or NULL
754 xmlPatScanLiteral(xmlPatParserContextPtr ctxt
) {
755 const xmlChar
*q
, *cur
;
763 val
= xmlStringCurrentChar(NULL
, cur
, &len
);
764 while ((IS_CHAR(val
)) && (val
!= '"')) {
766 val
= xmlStringCurrentChar(NULL
, cur
, &len
);
773 ret
= (xmlChar
*) xmlDictLookup(ctxt
->dict
, q
, cur
- q
);
775 ret
= xmlStrndup(q
, cur
- q
);
779 } else if (CUR
== '\'') {
782 val
= xmlStringCurrentChar(NULL
, cur
, &len
);
783 while ((IS_CHAR(val
)) && (val
!= '\'')) {
785 val
= xmlStringCurrentChar(NULL
, cur
, &len
);
792 ret
= (xmlChar
*) xmlDictLookup(ctxt
->dict
, q
, cur
- q
);
794 ret
= xmlStrndup(q
, cur
- q
);
799 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
809 * @ctxt: the XPath Parser context
811 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
812 * CombiningChar | Extender
814 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
816 * [6] Names ::= Name (S Name)*
818 * Returns the Name parsed or NULL
822 xmlPatScanName(xmlPatParserContextPtr ctxt
) {
823 const xmlChar
*q
, *cur
;
830 val
= xmlStringCurrentChar(NULL
, cur
, &len
);
831 if (!IS_LETTER(val
) && (val
!= '_') && (val
!= ':'))
834 while ((IS_LETTER(val
)) || (IS_DIGIT(val
)) ||
835 (val
== '.') || (val
== '-') ||
837 (IS_COMBINING(val
)) ||
838 (IS_EXTENDER(val
))) {
840 val
= xmlStringCurrentChar(NULL
, cur
, &len
);
843 ret
= (xmlChar
*) xmlDictLookup(ctxt
->dict
, q
, cur
- q
);
845 ret
= xmlStrndup(q
, cur
- q
);
852 * @ctxt: the XPath Parser context
854 * Parses a non qualified name
856 * Returns the Name parsed or NULL
860 xmlPatScanNCName(xmlPatParserContextPtr ctxt
) {
861 const xmlChar
*q
, *cur
;
868 val
= xmlStringCurrentChar(NULL
, cur
, &len
);
869 if (!IS_LETTER(val
) && (val
!= '_'))
872 while ((IS_LETTER(val
)) || (IS_DIGIT(val
)) ||
873 (val
== '.') || (val
== '-') ||
875 (IS_COMBINING(val
)) ||
876 (IS_EXTENDER(val
))) {
878 val
= xmlStringCurrentChar(NULL
, cur
, &len
);
881 ret
= (xmlChar
*) xmlDictLookup(ctxt
->dict
, q
, cur
- q
);
883 ret
= xmlStrndup(q
, cur
- q
);
891 * @ctxt: the XPath Parser context
892 * @prefix: the place to store the prefix
894 * Parse a qualified name
896 * Returns the Name parsed or NULL
900 xmlPatScanQName(xmlPatParserContextPtr ctxt
, xmlChar
**prefix
) {
904 ret
= xmlPatScanNCName(ctxt
);
908 ret
= xmlPatScanNCName(ctxt
);
915 * xmlCompileAttributeTest:
916 * @ctxt: the compilation context
918 * Compile an attribute test.
921 xmlCompileAttributeTest(xmlPatParserContextPtr ctxt
) {
922 xmlChar
*token
= NULL
;
923 xmlChar
*name
= NULL
;
927 name
= xmlPatScanNCName(ctxt
);
930 PUSH(XML_OP_ATTR
, NULL
, NULL
);
933 ERROR(NULL
, NULL
, NULL
,
934 "xmlCompileAttributeTest : Name expected\n");
941 xmlChar
*prefix
= name
;
945 if (IS_BLANK_CH(CUR
)) {
946 ERROR5(NULL
, NULL
, NULL
, "Invalid QName.\n", NULL
);
947 XML_PAT_FREE_STRING(ctxt
, prefix
);
952 * This is a namespace match
954 token
= xmlPatScanName(ctxt
);
955 if ((prefix
[0] == 'x') &&
956 (prefix
[1] == 'm') &&
957 (prefix
[2] == 'l') &&
960 XML_PAT_COPY_NSNAME(ctxt
, URL
, XML_XML_NAMESPACE
);
962 for (i
= 0;i
< ctxt
->nb_namespaces
;i
++) {
963 if (xmlStrEqual(ctxt
->namespaces
[2 * i
+ 1], prefix
)) {
964 XML_PAT_COPY_NSNAME(ctxt
, URL
, ctxt
->namespaces
[2 * i
])
968 if (i
>= ctxt
->nb_namespaces
) {
969 ERROR5(NULL
, NULL
, NULL
,
970 "xmlCompileAttributeTest : no namespace bound to prefix %s\n",
976 XML_PAT_FREE_STRING(ctxt
, prefix
);
980 PUSH(XML_OP_ATTR
, NULL
, URL
);
982 ERROR(NULL
, NULL
, NULL
,
983 "xmlCompileAttributeTest : Name expected\n");
988 PUSH(XML_OP_ATTR
, token
, URL
);
991 PUSH(XML_OP_ATTR
, name
, NULL
);
996 XML_PAT_FREE_STRING(ctxt
, URL
)
998 XML_PAT_FREE_STRING(ctxt
, token
);
1002 * xmlCompileStepPattern:
1003 * @ctxt: the compilation context
1005 * Compile the Step Pattern and generates a precompiled
1006 * form suitable for fast matching.
1008 * [3] Step ::= '.' | NameTest
1009 * [4] NameTest ::= QName | '*' | NCName ':' '*'
1013 xmlCompileStepPattern(xmlPatParserContextPtr ctxt
) {
1014 xmlChar
*token
= NULL
;
1015 xmlChar
*name
= NULL
;
1016 xmlChar
*URL
= NULL
;
1025 PUSH(XML_OP_ELEM
, NULL
, NULL
);
1032 if (XML_STREAM_XS_IDC_SEL(ctxt
->comp
)) {
1033 ERROR5(NULL
, NULL
, NULL
,
1034 "Unexpected attribute axis in '%s'.\n", ctxt
->base
);
1039 xmlCompileAttributeTest(ctxt
);
1040 if (ctxt
->error
!= 0)
1044 name
= xmlPatScanNCName(ctxt
);
1048 PUSH(XML_OP_ALL
, NULL
, NULL
);
1051 ERROR(NULL
, NULL
, NULL
,
1052 "xmlCompileStepPattern : Name expected\n");
1057 if (IS_BLANK_CH(CUR
)) {
1064 xmlChar
*prefix
= name
;
1067 if (hasBlanks
|| IS_BLANK_CH(CUR
)) {
1068 ERROR5(NULL
, NULL
, NULL
, "Invalid QName.\n", NULL
);
1073 * This is a namespace match
1075 token
= xmlPatScanName(ctxt
);
1076 if ((prefix
[0] == 'x') &&
1077 (prefix
[1] == 'm') &&
1078 (prefix
[2] == 'l') &&
1081 XML_PAT_COPY_NSNAME(ctxt
, URL
, XML_XML_NAMESPACE
)
1083 for (i
= 0;i
< ctxt
->nb_namespaces
;i
++) {
1084 if (xmlStrEqual(ctxt
->namespaces
[2 * i
+ 1], prefix
)) {
1085 XML_PAT_COPY_NSNAME(ctxt
, URL
, ctxt
->namespaces
[2 * i
])
1089 if (i
>= ctxt
->nb_namespaces
) {
1090 ERROR5(NULL
, NULL
, NULL
,
1091 "xmlCompileStepPattern : no namespace bound to prefix %s\n",
1097 XML_PAT_FREE_STRING(ctxt
, prefix
);
1099 if (token
== NULL
) {
1102 PUSH(XML_OP_NS
, URL
, NULL
);
1104 ERROR(NULL
, NULL
, NULL
,
1105 "xmlCompileStepPattern : Name expected\n");
1110 PUSH(XML_OP_ELEM
, token
, URL
);
1114 if (xmlStrEqual(name
, (const xmlChar
*) "child")) {
1115 XML_PAT_FREE_STRING(ctxt
, name
);
1116 name
= xmlPatScanName(ctxt
);
1120 PUSH(XML_OP_ALL
, NULL
, NULL
);
1123 ERROR(NULL
, NULL
, NULL
,
1124 "xmlCompileStepPattern : QName expected\n");
1130 xmlChar
*prefix
= name
;
1134 if (IS_BLANK_CH(CUR
)) {
1135 ERROR5(NULL
, NULL
, NULL
, "Invalid QName.\n", NULL
);
1140 * This is a namespace match
1142 token
= xmlPatScanName(ctxt
);
1143 if ((prefix
[0] == 'x') &&
1144 (prefix
[1] == 'm') &&
1145 (prefix
[2] == 'l') &&
1148 XML_PAT_COPY_NSNAME(ctxt
, URL
, XML_XML_NAMESPACE
)
1150 for (i
= 0;i
< ctxt
->nb_namespaces
;i
++) {
1151 if (xmlStrEqual(ctxt
->namespaces
[2 * i
+ 1], prefix
)) {
1152 XML_PAT_COPY_NSNAME(ctxt
, URL
, ctxt
->namespaces
[2 * i
])
1156 if (i
>= ctxt
->nb_namespaces
) {
1157 ERROR5(NULL
, NULL
, NULL
,
1158 "xmlCompileStepPattern : no namespace bound "
1159 "to prefix %s\n", prefix
);
1164 XML_PAT_FREE_STRING(ctxt
, prefix
);
1166 if (token
== NULL
) {
1169 PUSH(XML_OP_NS
, URL
, NULL
);
1171 ERROR(NULL
, NULL
, NULL
,
1172 "xmlCompileStepPattern : Name expected\n");
1177 PUSH(XML_OP_CHILD
, token
, URL
);
1180 PUSH(XML_OP_CHILD
, name
, NULL
);
1182 } else if (xmlStrEqual(name
, (const xmlChar
*) "attribute")) {
1183 XML_PAT_FREE_STRING(ctxt
, name
)
1185 if (XML_STREAM_XS_IDC_SEL(ctxt
->comp
)) {
1186 ERROR5(NULL
, NULL
, NULL
,
1187 "Unexpected attribute axis in '%s'.\n", ctxt
->base
);
1191 xmlCompileAttributeTest(ctxt
);
1192 if (ctxt
->error
!= 0)
1196 ERROR5(NULL
, NULL
, NULL
,
1197 "The 'element' or 'attribute' axis is expected.\n", NULL
);
1202 } else if (CUR
== '*') {
1208 PUSH(XML_OP_ALL
, token
, NULL
);
1210 PUSH(XML_OP_ELEM
, name
, NULL
);
1215 XML_PAT_FREE_STRING(ctxt
, URL
)
1217 XML_PAT_FREE_STRING(ctxt
, token
)
1219 XML_PAT_FREE_STRING(ctxt
, name
)
1223 * xmlCompilePathPattern:
1224 * @ctxt: the compilation context
1226 * Compile the Path Pattern and generates a precompiled
1227 * form suitable for fast matching.
1229 * [5] Path ::= ('.//')? ( Step '/' )* ( Step | '@' NameTest )
1232 xmlCompilePathPattern(xmlPatParserContextPtr ctxt
) {
1235 ctxt
->comp
->flags
|= PAT_FROM_ROOT
;
1236 } else if ((CUR
== '.') || (ctxt
->comp
->flags
& XML_PATTERN_NOTPATTERN
)) {
1237 ctxt
->comp
->flags
|= PAT_FROM_CUR
;
1240 if ((CUR
== '/') && (NXT(1) == '/')) {
1241 PUSH(XML_OP_ANCESTOR
, NULL
, NULL
);
1244 } else if ((CUR
== '.') && (NXT(1) == '/') && (NXT(2) == '/')) {
1245 PUSH(XML_OP_ANCESTOR
, NULL
, NULL
);
1249 /* Check for incompleteness. */
1252 ERROR5(NULL
, NULL
, NULL
,
1253 "Incomplete expression '%s'.\n", ctxt
->base
);
1260 xmlCompileAttributeTest(ctxt
);
1262 /* TODO: check for incompleteness */
1264 xmlCompileStepPattern(ctxt
);
1265 if (ctxt
->error
!= 0)
1270 PUSH(XML_OP_ROOT
, NULL
, NULL
);
1272 /* Check for incompleteness. */
1275 ERROR5(NULL
, NULL
, NULL
,
1276 "Incomplete expression '%s'.\n", ctxt
->base
);
1281 xmlCompileStepPattern(ctxt
);
1282 if (ctxt
->error
!= 0)
1285 while (CUR
== '/') {
1286 if (NXT(1) == '/') {
1287 PUSH(XML_OP_ANCESTOR
, NULL
, NULL
);
1291 xmlCompileStepPattern(ctxt
);
1292 if (ctxt
->error
!= 0)
1295 PUSH(XML_OP_PARENT
, NULL
, NULL
);
1299 ERROR5(NULL
, NULL
, NULL
,
1300 "Incomplete expression '%s'.\n", ctxt
->base
);
1304 xmlCompileStepPattern(ctxt
);
1305 if (ctxt
->error
!= 0)
1311 ERROR5(NULL
, NULL
, NULL
,
1312 "Failed to compile pattern %s\n", ctxt
->base
);
1320 * xmlCompileIDCXPathPath:
1321 * @ctxt: the compilation context
1323 * Compile the Path Pattern and generates a precompiled
1324 * form suitable for fast matching.
1326 * [5] Path ::= ('.//')? ( Step '/' )* ( Step | '@' NameTest )
1329 xmlCompileIDCXPathPath(xmlPatParserContextPtr ctxt
) {
1332 ERROR5(NULL
, NULL
, NULL
,
1333 "Unexpected selection of the document root in '%s'.\n",
1337 ctxt
->comp
->flags
|= PAT_FROM_CUR
;
1340 /* "." - "self::node()" */
1345 * Selection of the context node.
1347 PUSH(XML_OP_ELEM
, NULL
, NULL
);
1351 /* TODO: A more meaningful error message. */
1352 ERROR5(NULL
, NULL
, NULL
,
1353 "Unexpected token after '.' in '%s'.\n", ctxt
->base
);
1356 /* "./" - "self::node()/" */
1360 if (IS_BLANK_CH(PEEKPREV(1))) {
1364 ERROR5(NULL
, NULL
, NULL
,
1365 "Unexpected '/' token in '%s'.\n", ctxt
->base
);
1368 /* ".//" - "self:node()/descendant-or-self::node()/" */
1369 PUSH(XML_OP_ANCESTOR
, NULL
, NULL
);
1374 goto error_unfinished
;
1380 xmlCompileStepPattern(ctxt
);
1381 if (ctxt
->error
!= 0)
1386 PUSH(XML_OP_PARENT
, NULL
, NULL
);
1391 * Disallow subsequent '//'.
1393 ERROR5(NULL
, NULL
, NULL
,
1394 "Unexpected subsequent '//' in '%s'.\n",
1399 goto error_unfinished
;
1404 ERROR5(NULL
, NULL
, NULL
,
1405 "Failed to compile expression '%s'.\n", ctxt
->base
);
1415 ERROR5(NULL
, NULL
, NULL
,
1416 "Unfinished expression '%s'.\n", ctxt
->base
);
1420 /************************************************************************
1422 * The streaming code *
1424 ************************************************************************/
1426 #ifdef DEBUG_STREAMING
1428 xmlDebugStreamComp(xmlStreamCompPtr stream
) {
1431 if (stream
== NULL
) {
1432 printf("Stream: NULL\n");
1435 printf("Stream: %d steps\n", stream
->nbStep
);
1436 for (i
= 0;i
< stream
->nbStep
;i
++) {
1437 if (stream
->steps
[i
].ns
!= NULL
) {
1438 printf("{%s}", stream
->steps
[i
].ns
);
1440 if (stream
->steps
[i
].name
== NULL
) {
1443 printf("%s ", stream
->steps
[i
].name
);
1445 if (stream
->steps
[i
].flags
& XML_STREAM_STEP_ROOT
)
1447 if (stream
->steps
[i
].flags
& XML_STREAM_STEP_DESC
)
1449 if (stream
->steps
[i
].flags
& XML_STREAM_STEP_FINAL
)
1455 xmlDebugStreamCtxt(xmlStreamCtxtPtr ctxt
, int match
) {
1459 printf("Stream: NULL\n");
1462 printf("Stream: level %d, %d states: ", ctxt
->level
, ctxt
->nbState
);
1464 printf("matches\n");
1467 for (i
= 0;i
< ctxt
->nbState
;i
++) {
1468 if (ctxt
->states
[2 * i
] < 0)
1469 printf(" %d: free\n", i
);
1471 printf(" %d: step %d, level %d", i
, ctxt
->states
[2 * i
],
1472 ctxt
->states
[(2 * i
) + 1]);
1473 if (ctxt
->comp
->steps
[ctxt
->states
[2 * i
]].flags
&
1474 XML_STREAM_STEP_DESC
)
1484 * @size: the number of expected steps
1486 * build a new compiled pattern for streaming
1488 * Returns the new structure or NULL in case of error.
1490 static xmlStreamCompPtr
1491 xmlNewStreamComp(int size
) {
1492 xmlStreamCompPtr cur
;
1497 cur
= (xmlStreamCompPtr
) xmlMalloc(sizeof(xmlStreamComp
));
1499 ERROR(NULL
, NULL
, NULL
,
1500 "xmlNewStreamComp: malloc failed\n");
1503 memset(cur
, 0, sizeof(xmlStreamComp
));
1504 cur
->steps
= (xmlStreamStepPtr
) xmlMalloc(size
* sizeof(xmlStreamStep
));
1505 if (cur
->steps
== NULL
) {
1507 ERROR(NULL
, NULL
, NULL
,
1508 "xmlNewStreamComp: malloc failed\n");
1512 cur
->maxStep
= size
;
1517 * xmlFreeStreamComp:
1518 * @comp: the compiled pattern for streaming
1520 * Free the compiled pattern for streaming
1523 xmlFreeStreamComp(xmlStreamCompPtr comp
) {
1525 if (comp
->steps
!= NULL
)
1526 xmlFree(comp
->steps
);
1527 if (comp
->dict
!= NULL
)
1528 xmlDictFree(comp
->dict
);
1534 * xmlStreamCompAddStep:
1535 * @comp: the compiled pattern for streaming
1536 * @name: the first string, the name, or NULL for *
1537 * @ns: the second step, the namespace name
1538 * @flags: the flags for that step
1540 * Add a new step to the compiled pattern
1542 * Returns -1 in case of error or the step index if successful
1545 xmlStreamCompAddStep(xmlStreamCompPtr comp
, const xmlChar
*name
,
1546 const xmlChar
*ns
, int nodeType
, int flags
) {
1547 xmlStreamStepPtr cur
;
1549 if (comp
->nbStep
>= comp
->maxStep
) {
1550 cur
= (xmlStreamStepPtr
) xmlRealloc(comp
->steps
,
1551 comp
->maxStep
* 2 * sizeof(xmlStreamStep
));
1553 ERROR(NULL
, NULL
, NULL
,
1554 "xmlNewStreamComp: malloc failed\n");
1560 cur
= &comp
->steps
[comp
->nbStep
++];
1564 cur
->nodeType
= nodeType
;
1565 return(comp
->nbStep
- 1);
1570 * @comp: the precompiled pattern
1572 * Tries to stream compile a pattern
1574 * Returns -1 in case of failure and 0 in case of success.
1577 xmlStreamCompile(xmlPatternPtr comp
) {
1578 xmlStreamCompPtr stream
;
1579 int i
, s
= 0, root
= 0, flags
= 0, prevs
= -1;
1582 if ((comp
== NULL
) || (comp
->steps
== NULL
))
1585 * special case for .
1587 if ((comp
->nbStep
== 1) &&
1588 (comp
->steps
[0].op
== XML_OP_ELEM
) &&
1589 (comp
->steps
[0].value
== NULL
) &&
1590 (comp
->steps
[0].value2
== NULL
)) {
1591 stream
= xmlNewStreamComp(0);
1594 /* Note that the stream will have no steps in this case. */
1595 stream
->flags
|= XML_STREAM_FINAL_IS_ANY_NODE
;
1596 comp
->stream
= stream
;
1600 stream
= xmlNewStreamComp((comp
->nbStep
/ 2) + 1);
1603 if (comp
->dict
!= NULL
) {
1604 stream
->dict
= comp
->dict
;
1605 xmlDictReference(stream
->dict
);
1609 if (comp
->flags
& PAT_FROM_ROOT
)
1610 stream
->flags
|= XML_STREAM_FROM_ROOT
;
1612 for (;i
< comp
->nbStep
;i
++) {
1613 step
= comp
->steps
[i
];
1623 s
= xmlStreamCompAddStep(stream
, NULL
, step
.value
,
1624 XML_ELEMENT_NODE
, flags
);
1631 flags
|= XML_STREAM_STEP_ATTR
;
1633 s
= xmlStreamCompAddStep(stream
,
1634 step
.value
, step
.value2
, XML_ATTRIBUTE_NODE
, flags
);
1640 if ((step
.value
== NULL
) && (step
.value2
== NULL
)) {
1642 * We have a "." or "self::node()" here.
1643 * Eliminate redundant self::node() tests like in "/./."
1645 * The only case we won't eliminate is "//.", i.e. if
1646 * self::node() is the last node test and we had
1647 * continuation somewhere beforehand.
1649 if ((comp
->nbStep
== i
+ 1) &&
1650 (flags
& XML_STREAM_STEP_DESC
)) {
1652 * Mark the special case where the expression resolves
1653 * to any type of node.
1655 if (comp
->nbStep
== i
+ 1) {
1656 stream
->flags
|= XML_STREAM_FINAL_IS_ANY_NODE
;
1658 flags
|= XML_STREAM_STEP_NODE
;
1659 s
= xmlStreamCompAddStep(stream
, NULL
, NULL
,
1660 XML_STREAM_ANY_NODE
, flags
);
1665 * If there was a previous step, mark it to be added to
1666 * the result node-set; this is needed since only
1667 * the last step will be marked as "final" and only
1668 * "final" nodes are added to the resulting set.
1671 stream
->steps
[prevs
].flags
|= XML_STREAM_STEP_IN_SET
;
1677 /* Just skip this one. */
1681 /* An element node. */
1682 s
= xmlStreamCompAddStep(stream
, step
.value
, step
.value2
,
1683 XML_ELEMENT_NODE
, flags
);
1690 /* An element node child. */
1691 s
= xmlStreamCompAddStep(stream
, step
.value
, step
.value2
,
1692 XML_ELEMENT_NODE
, flags
);
1699 s
= xmlStreamCompAddStep(stream
, NULL
, NULL
,
1700 XML_ELEMENT_NODE
, flags
);
1708 case XML_OP_ANCESTOR
:
1709 /* Skip redundant continuations. */
1710 if (flags
& XML_STREAM_STEP_DESC
)
1712 flags
|= XML_STREAM_STEP_DESC
;
1714 * Mark the expression as having "//".
1716 if ((stream
->flags
& XML_STREAM_DESC
) == 0)
1717 stream
->flags
|= XML_STREAM_DESC
;
1721 if ((! root
) && (comp
->flags
& XML_PATTERN_NOTPATTERN
) == 0) {
1723 * If this should behave like a real pattern, we will mark
1724 * the first step as having "//", to be reentrant on every
1727 if ((stream
->flags
& XML_STREAM_DESC
) == 0)
1728 stream
->flags
|= XML_STREAM_DESC
;
1730 if (stream
->nbStep
> 0) {
1731 if ((stream
->steps
[0].flags
& XML_STREAM_STEP_DESC
) == 0)
1732 stream
->steps
[0].flags
|= XML_STREAM_STEP_DESC
;
1735 if (stream
->nbStep
<= s
)
1737 stream
->steps
[s
].flags
|= XML_STREAM_STEP_FINAL
;
1739 stream
->steps
[0].flags
|= XML_STREAM_STEP_ROOT
;
1740 #ifdef DEBUG_STREAMING
1741 xmlDebugStreamComp(stream
);
1743 comp
->stream
= stream
;
1746 xmlFreeStreamComp(stream
);
1752 * @size: the number of expected states
1754 * build a new stream context
1756 * Returns the new structure or NULL in case of error.
1758 static xmlStreamCtxtPtr
1759 xmlNewStreamCtxt(xmlStreamCompPtr stream
) {
1760 xmlStreamCtxtPtr cur
;
1762 cur
= (xmlStreamCtxtPtr
) xmlMalloc(sizeof(xmlStreamCtxt
));
1764 ERROR(NULL
, NULL
, NULL
,
1765 "xmlNewStreamCtxt: malloc failed\n");
1768 memset(cur
, 0, sizeof(xmlStreamCtxt
));
1769 cur
->states
= (int *) xmlMalloc(4 * 2 * sizeof(int));
1770 if (cur
->states
== NULL
) {
1772 ERROR(NULL
, NULL
, NULL
,
1773 "xmlNewStreamCtxt: malloc failed\n");
1780 cur
->blockLevel
= -1;
1785 * xmlFreeStreamCtxt:
1786 * @stream: the stream context
1788 * Free the stream context
1791 xmlFreeStreamCtxt(xmlStreamCtxtPtr stream
) {
1792 xmlStreamCtxtPtr next
;
1794 while (stream
!= NULL
) {
1795 next
= stream
->next
;
1796 if (stream
->states
!= NULL
)
1797 xmlFree(stream
->states
);
1804 * xmlStreamCtxtAddState:
1805 * @comp: the stream context
1806 * @idx: the step index for that streaming state
1808 * Add a new state to the stream context
1810 * Returns -1 in case of error or the state index if successful
1813 xmlStreamCtxtAddState(xmlStreamCtxtPtr comp
, int idx
, int level
) {
1815 for (i
= 0;i
< comp
->nbState
;i
++) {
1816 if (comp
->states
[2 * i
] < 0) {
1817 comp
->states
[2 * i
] = idx
;
1818 comp
->states
[2 * i
+ 1] = level
;
1822 if (comp
->nbState
>= comp
->maxState
) {
1825 cur
= (int *) xmlRealloc(comp
->states
,
1826 comp
->maxState
* 4 * sizeof(int));
1828 ERROR(NULL
, NULL
, NULL
,
1829 "xmlNewStreamCtxt: malloc failed\n");
1833 comp
->maxState
*= 2;
1835 comp
->states
[2 * comp
->nbState
] = idx
;
1836 comp
->states
[2 * comp
->nbState
++ + 1] = level
;
1837 return(comp
->nbState
- 1);
1841 * xmlStreamPushInternal:
1842 * @stream: the stream context
1843 * @name: the current name
1844 * @ns: the namespace name
1845 * @nodeType: the type of the node
1847 * Push new data onto the stream. NOTE: if the call xmlPatterncompile()
1848 * indicated a dictionary, then strings for name and ns will be expected
1849 * to come from the dictionary.
1850 * Both @name and @ns being NULL means the / i.e. the root of the document.
1851 * This can also act as a reset.
1853 * Returns: -1 in case of error, 1 if the current state in the stream is a
1854 * match and 0 otherwise.
1857 xmlStreamPushInternal(xmlStreamCtxtPtr stream
,
1858 const xmlChar
*name
, const xmlChar
*ns
,
1860 int ret
= 0, err
= 0, final
= 0, tmp
, i
, m
, match
, stepNr
, desc
;
1861 xmlStreamCompPtr comp
;
1863 #ifdef DEBUG_STREAMING
1864 xmlStreamCtxtPtr orig
= stream
;
1867 if ((stream
== NULL
) || (stream
->nbState
< 0))
1870 while (stream
!= NULL
) {
1871 comp
= stream
->comp
;
1873 if ((nodeType
== XML_ELEMENT_NODE
) &&
1874 (name
== NULL
) && (ns
== NULL
)) {
1875 /* We have a document node here (or a reset). */
1876 stream
->nbState
= 0;
1878 stream
->blockLevel
= -1;
1879 if (comp
->flags
& XML_STREAM_FROM_ROOT
) {
1880 if (comp
->nbStep
== 0) {
1881 /* TODO: We have a "/." here? */
1884 if ((comp
->nbStep
== 1) &&
1885 (comp
->steps
[0].nodeType
== XML_STREAM_ANY_NODE
) &&
1886 (comp
->steps
[0].flags
& XML_STREAM_STEP_DESC
))
1889 * In the case of "//." the document node will match
1893 } else if (comp
->steps
[0].flags
& XML_STREAM_STEP_ROOT
) {
1894 /* TODO: Do we need this ? */
1895 tmp
= xmlStreamCtxtAddState(stream
, 0, 0);
1901 stream
= stream
->next
;
1902 continue; /* while */
1906 * Fast check for ".".
1908 if (comp
->nbStep
== 0) {
1910 * / and . are handled at the XPath node set creation
1911 * level by checking min depth
1913 if (stream
->flags
& XML_PATTERN_XPATH
) {
1914 stream
= stream
->next
;
1915 continue; /* while */
1918 * For non-pattern like evaluation like XML Schema IDCs
1919 * or traditional XPath expressions, this will match if
1920 * we are at the first level only, otherwise on every level.
1922 if ((nodeType
!= XML_ATTRIBUTE_NODE
) &&
1923 (((stream
->flags
& XML_PATTERN_NOTPATTERN
) == 0) ||
1924 (stream
->level
== 0))) {
1930 if (stream
->blockLevel
!= -1) {
1932 * Skip blocked expressions.
1938 if ((nodeType
!= XML_ELEMENT_NODE
) &&
1939 (nodeType
!= XML_ATTRIBUTE_NODE
) &&
1940 ((comp
->flags
& XML_STREAM_FINAL_IS_ANY_NODE
) == 0)) {
1942 * No need to process nodes of other types if we don't
1943 * resolve to those types.
1944 * TODO: Do we need to block the context here?
1951 * Check evolution of existing states
1954 m
= stream
->nbState
;
1956 if ((comp
->flags
& XML_STREAM_DESC
) == 0) {
1958 * If there is no "//", then only the last
1959 * added state is of interest.
1961 stepNr
= stream
->states
[2 * (stream
->nbState
-1)];
1963 * TODO: Security check, should not happen, remove it.
1965 if (stream
->states
[(2 * (stream
->nbState
-1)) + 1] <
1974 * If there are "//", then we need to process every "//"
1975 * occuring in the states, plus any other state for this
1978 stepNr
= stream
->states
[2 * i
];
1980 /* TODO: should not happen anymore: dead states */
1984 tmp
= stream
->states
[(2 * i
) + 1];
1986 /* skip new states just added */
1987 if (tmp
> stream
->level
)
1990 /* skip states at ancestor levels, except if "//" */
1991 desc
= comp
->steps
[stepNr
].flags
& XML_STREAM_STEP_DESC
;
1992 if ((tmp
< stream
->level
) && (!desc
))
1996 * Check for correct node-type.
1998 step
= comp
->steps
[stepNr
];
1999 if (step
.nodeType
!= nodeType
) {
2000 if (step
.nodeType
== XML_ATTRIBUTE_NODE
) {
2002 * Block this expression for deeper evaluation.
2004 if ((comp
->flags
& XML_STREAM_DESC
) == 0)
2005 stream
->blockLevel
= stream
->level
+1;
2007 } else if (step
.nodeType
!= XML_STREAM_ANY_NODE
)
2011 * Compare local/namespace-name.
2014 if (step
.nodeType
== XML_STREAM_ANY_NODE
) {
2016 } else if (step
.name
== NULL
) {
2017 if (step
.ns
== NULL
) {
2019 * This lets through all elements/attributes.
2022 } else if (ns
!= NULL
)
2023 match
= xmlStrEqual(step
.ns
, ns
);
2024 } else if (((step
.ns
!= NULL
) == (ns
!= NULL
)) &&
2026 (step
.name
[0] == name
[0]) &&
2027 xmlStrEqual(step
.name
, name
) &&
2028 ((step
.ns
== ns
) || xmlStrEqual(step
.ns
, ns
)))
2034 * TODO: Pointer comparison won't work, since not guaranteed that the given
2035 * values are in the same dict; especially if it's the namespace name,
2036 * normally coming from ns->href. We need a namespace dict mechanism !
2038 } else if (comp
->dict
) {
2039 if (step
.name
== NULL
) {
2040 if (step
.ns
== NULL
)
2043 match
= (step
.ns
== ns
);
2045 match
= ((step
.name
== name
) && (step
.ns
== ns
));
2047 #endif /* if 0 ------------------------------------------------------- */
2049 final
= step
.flags
& XML_STREAM_STEP_FINAL
;
2054 /* descending match create a new state */
2055 xmlStreamCtxtAddState(stream
, stepNr
+ 1,
2062 xmlStreamCtxtAddState(stream
, stepNr
+ 1,
2066 if ((ret
!= 1) && (step
.flags
& XML_STREAM_STEP_IN_SET
)) {
2068 * Check if we have a special case like "foo/bar//.", where
2069 * "foo" is selected as well.
2074 if (((comp
->flags
& XML_STREAM_DESC
) == 0) &&
2075 ((! match
) || final
)) {
2077 * Mark this expression as blocked for any evaluation at
2078 * deeper levels. Note that this includes "/foo"
2079 * expressions if the *pattern* behaviour is used.
2081 stream
->blockLevel
= stream
->level
+1;
2090 * Re/enter the expression.
2091 * Don't reenter if it's an absolute expression like "/foo",
2094 step
= comp
->steps
[0];
2095 if (step
.flags
& XML_STREAM_STEP_ROOT
)
2098 desc
= step
.flags
& XML_STREAM_STEP_DESC
;
2099 if (stream
->flags
& XML_PATTERN_NOTPATTERN
) {
2101 * Re/enter the expression if it is a "descendant" one,
2102 * or if we are at the 1st level of evaluation.
2105 if (stream
->level
== 1) {
2106 if (XML_STREAM_XS_IDC(stream
)) {
2108 * XS-IDC: The missing "self::node()" will always
2109 * match the first given node.
2116 * A "//" is always reentrant.
2122 * XS-IDC: Process the 2nd level, since the missing
2123 * "self::node()" is responsible for the 2nd level being
2124 * the real start level.
2126 if ((stream
->level
== 2) && XML_STREAM_XS_IDC(stream
))
2134 * Check expected node-type.
2136 if (step
.nodeType
!= nodeType
) {
2137 if (nodeType
== XML_ATTRIBUTE_NODE
)
2139 else if (step
.nodeType
!= XML_STREAM_ANY_NODE
)
2143 * Compare local/namespace-name.
2146 if (step
.nodeType
== XML_STREAM_ANY_NODE
) {
2148 } else if (step
.name
== NULL
) {
2149 if (step
.ns
== NULL
) {
2151 * This lets through all elements/attributes.
2154 } else if (ns
!= NULL
)
2155 match
= xmlStrEqual(step
.ns
, ns
);
2156 } else if (((step
.ns
!= NULL
) == (ns
!= NULL
)) &&
2158 (step
.name
[0] == name
[0]) &&
2159 xmlStrEqual(step
.name
, name
) &&
2160 ((step
.ns
== ns
) || xmlStrEqual(step
.ns
, ns
)))
2164 final
= step
.flags
& XML_STREAM_STEP_FINAL
;
2169 xmlStreamCtxtAddState(stream
, 1, stream
->level
);
2170 if ((ret
!= 1) && (step
.flags
& XML_STREAM_STEP_IN_SET
)) {
2172 * Check if we have a special case like "foo//.", where
2173 * "foo" is selected as well.
2178 if (((comp
->flags
& XML_STREAM_DESC
) == 0) &&
2179 ((! match
) || final
)) {
2181 * Mark this expression as blocked for any evaluation at
2184 stream
->blockLevel
= stream
->level
;
2188 stream
= stream
->next
;
2189 } /* while stream != NULL */
2193 #ifdef DEBUG_STREAMING
2194 xmlDebugStreamCtxt(orig
, ret
);
2201 * @stream: the stream context
2202 * @name: the current name
2203 * @ns: the namespace name
2205 * Push new data onto the stream. NOTE: if the call xmlPatterncompile()
2206 * indicated a dictionary, then strings for name and ns will be expected
2207 * to come from the dictionary.
2208 * Both @name and @ns being NULL means the / i.e. the root of the document.
2209 * This can also act as a reset.
2210 * Otherwise the function will act as if it has been given an element-node.
2212 * Returns: -1 in case of error, 1 if the current state in the stream is a
2213 * match and 0 otherwise.
2216 xmlStreamPush(xmlStreamCtxtPtr stream
,
2217 const xmlChar
*name
, const xmlChar
*ns
) {
2218 return (xmlStreamPushInternal(stream
, name
, ns
, (int) XML_ELEMENT_NODE
));
2222 * xmlStreamPushNode:
2223 * @stream: the stream context
2224 * @name: the current name
2225 * @ns: the namespace name
2226 * @nodeType: the type of the node being pushed
2228 * Push new data onto the stream. NOTE: if the call xmlPatterncompile()
2229 * indicated a dictionary, then strings for name and ns will be expected
2230 * to come from the dictionary.
2231 * Both @name and @ns being NULL means the / i.e. the root of the document.
2232 * This can also act as a reset.
2233 * Different from xmlStreamPush() this function can be fed with nodes of type:
2234 * element-, attribute-, text-, cdata-section-, comment- and
2235 * processing-instruction-node.
2237 * Returns: -1 in case of error, 1 if the current state in the stream is a
2238 * match and 0 otherwise.
2241 xmlStreamPushNode(xmlStreamCtxtPtr stream
,
2242 const xmlChar
*name
, const xmlChar
*ns
,
2245 return (xmlStreamPushInternal(stream
, name
, ns
,
2250 * xmlStreamPushAttr:
2251 * @stream: the stream context
2252 * @name: the current name
2253 * @ns: the namespace name
2255 * Push new attribute data onto the stream. NOTE: if the call xmlPatterncompile()
2256 * indicated a dictionary, then strings for name and ns will be expected
2257 * to come from the dictionary.
2258 * Both @name and @ns being NULL means the / i.e. the root of the document.
2259 * This can also act as a reset.
2260 * Otherwise the function will act as if it has been given an attribute-node.
2262 * Returns: -1 in case of error, 1 if the current state in the stream is a
2263 * match and 0 otherwise.
2266 xmlStreamPushAttr(xmlStreamCtxtPtr stream
,
2267 const xmlChar
*name
, const xmlChar
*ns
) {
2268 return (xmlStreamPushInternal(stream
, name
, ns
, (int) XML_ATTRIBUTE_NODE
));
2273 * @stream: the stream context
2275 * push one level from the stream.
2277 * Returns: -1 in case of error, 0 otherwise.
2280 xmlStreamPop(xmlStreamCtxtPtr stream
) {
2285 while (stream
!= NULL
) {
2287 * Reset block-level.
2289 if (stream
->blockLevel
== stream
->level
)
2290 stream
->blockLevel
= -1;
2293 * stream->level can be zero when XML_FINAL_IS_ANY_NODE is set
2294 * (see the thread at
2295 * http://mail.gnome.org/archives/xslt/2008-July/msg00027.html)
2300 * Check evolution of existing states
2302 for (i
= stream
->nbState
-1; i
>= 0; i
--) {
2303 /* discard obsoleted states */
2304 lev
= stream
->states
[(2 * i
) + 1];
2305 if (lev
> stream
->level
)
2307 if (lev
<= stream
->level
)
2310 stream
= stream
->next
;
2316 * xmlStreamWantsAnyNode:
2317 * @streamCtxt: the stream context
2319 * Query if the streaming pattern additionally needs to be fed with
2320 * text-, cdata-section-, comment- and processing-instruction-nodes.
2321 * If the result is 0 then only element-nodes and attribute-nodes
2322 * need to be pushed.
2324 * Returns: 1 in case of need of nodes of the above described types,
2325 * 0 otherwise. -1 on API errors.
2328 xmlStreamWantsAnyNode(xmlStreamCtxtPtr streamCtxt
)
2330 if (streamCtxt
== NULL
)
2332 while (streamCtxt
!= NULL
) {
2333 if (streamCtxt
->comp
->flags
& XML_STREAM_FINAL_IS_ANY_NODE
)
2335 streamCtxt
= streamCtxt
->next
;
2340 /************************************************************************
2342 * The public interfaces *
2344 ************************************************************************/
2347 * xmlPatterncompile:
2348 * @pattern: the pattern to compile
2349 * @dict: an optional dictionary for interned strings
2350 * @flags: compilation flags, see xmlPatternFlags
2351 * @namespaces: the prefix definitions, array of [URI, prefix] or NULL
2353 * Compile a pattern.
2355 * Returns the compiled form of the pattern or NULL in case of error
2358 xmlPatterncompile(const xmlChar
*pattern
, xmlDict
*dict
, int flags
,
2359 const xmlChar
**namespaces
) {
2360 xmlPatternPtr ret
= NULL
, cur
;
2361 xmlPatParserContextPtr ctxt
= NULL
;
2362 const xmlChar
*or, *start
;
2363 xmlChar
*tmp
= NULL
;
2367 if (pattern
== NULL
)
2374 while ((*or != 0) && (*or != '|')) or++;
2376 ctxt
= xmlNewPatParserContext(start
, dict
, namespaces
);
2378 tmp
= xmlStrndup(start
, or - start
);
2380 ctxt
= xmlNewPatParserContext(tmp
, dict
, namespaces
);
2384 if (ctxt
== NULL
) goto error
;
2385 cur
= xmlNewPattern();
2386 if (cur
== NULL
) goto error
;
2388 * Assign string dict.
2392 xmlDictReference(dict
);
2397 cur
->next
= ret
->next
;
2403 if (XML_STREAM_XS_IDC(cur
))
2404 xmlCompileIDCXPathPath(ctxt
);
2406 xmlCompilePathPattern(ctxt
);
2407 if (ctxt
->error
!= 0)
2409 xmlFreePatParserContext(ctxt
);
2415 type
= cur
->flags
& (PAT_FROM_ROOT
| PAT_FROM_CUR
);
2416 } else if (type
== PAT_FROM_ROOT
) {
2417 if (cur
->flags
& PAT_FROM_CUR
)
2419 } else if (type
== PAT_FROM_CUR
) {
2420 if (cur
->flags
& PAT_FROM_ROOT
)
2425 xmlStreamCompile(cur
);
2426 if (xmlReversePattern(cur
) < 0)
2434 if (streamable
== 0) {
2436 while (cur
!= NULL
) {
2437 if (cur
->stream
!= NULL
) {
2438 xmlFreeStreamComp(cur
->stream
);
2447 if (ctxt
!= NULL
) xmlFreePatParserContext(ctxt
);
2448 if (ret
!= NULL
) xmlFreePattern(ret
);
2449 if (tmp
!= NULL
) xmlFree(tmp
);
2455 * @comp: the precompiled pattern
2458 * Test whether the node matches the pattern
2460 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
2463 xmlPatternMatch(xmlPatternPtr comp
, xmlNodePtr node
)
2467 if ((comp
== NULL
) || (node
== NULL
))
2470 while (comp
!= NULL
) {
2471 ret
= xmlPatMatch(comp
, node
);
2480 * xmlPatternGetStreamCtxt:
2481 * @comp: the precompiled pattern
2483 * Get a streaming context for that pattern
2484 * Use xmlFreeStreamCtxt to free the context.
2486 * Returns a pointer to the context or NULL in case of failure
2489 xmlPatternGetStreamCtxt(xmlPatternPtr comp
)
2491 xmlStreamCtxtPtr ret
= NULL
, cur
;
2493 if ((comp
== NULL
) || (comp
->stream
== NULL
))
2496 while (comp
!= NULL
) {
2497 if (comp
->stream
== NULL
)
2499 cur
= xmlNewStreamCtxt(comp
->stream
);
2505 cur
->next
= ret
->next
;
2508 cur
->flags
= comp
->flags
;
2513 xmlFreeStreamCtxt(ret
);
2518 * xmlPatternStreamable:
2519 * @comp: the precompiled pattern
2521 * Check if the pattern is streamable i.e. xmlPatternGetStreamCtxt()
2524 * Returns 1 if streamable, 0 if not and -1 in case of error.
2527 xmlPatternStreamable(xmlPatternPtr comp
) {
2530 while (comp
!= NULL
) {
2531 if (comp
->stream
== NULL
)
2539 * xmlPatternMaxDepth:
2540 * @comp: the precompiled pattern
2542 * Check the maximum depth reachable by a pattern
2544 * Returns -2 if no limit (using //), otherwise the depth,
2545 * and -1 in case of error
2548 xmlPatternMaxDepth(xmlPatternPtr comp
) {
2552 while (comp
!= NULL
) {
2553 if (comp
->stream
== NULL
)
2555 for (i
= 0;i
< comp
->stream
->nbStep
;i
++)
2556 if (comp
->stream
->steps
[i
].flags
& XML_STREAM_STEP_DESC
)
2558 if (comp
->stream
->nbStep
> ret
)
2559 ret
= comp
->stream
->nbStep
;
2566 * xmlPatternMinDepth:
2567 * @comp: the precompiled pattern
2569 * Check the minimum depth reachable by a pattern, 0 mean the / or . are
2572 * Returns -1 in case of error otherwise the depth,
2576 xmlPatternMinDepth(xmlPatternPtr comp
) {
2580 while (comp
!= NULL
) {
2581 if (comp
->stream
== NULL
)
2583 if (comp
->stream
->nbStep
< ret
)
2584 ret
= comp
->stream
->nbStep
;
2593 * xmlPatternFromRoot:
2594 * @comp: the precompiled pattern
2596 * Check if the pattern must be looked at from the root.
2598 * Returns 1 if true, 0 if false and -1 in case of error
2601 xmlPatternFromRoot(xmlPatternPtr comp
) {
2604 while (comp
!= NULL
) {
2605 if (comp
->stream
== NULL
)
2607 if (comp
->flags
& PAT_FROM_ROOT
)
2615 #define bottom_pattern
2616 #include "elfgcchack.h"
2617 #endif /* LIBXML_PATTERN_ENABLED */