pattern.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530
  1. /*
  2. * pattern.c: Implementation of selectors for nodes
  3. *
  4. * Reference:
  5. * http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/
  6. * to some extent
  7. * http://www.w3.org/TR/1999/REC-xml-19991116
  8. *
  9. * See Copyright for the status of this software.
  10. *
  11. * daniel@veillard.com
  12. */
  13. /*
  14. * TODO:
  15. * - compilation flags to check for specific syntaxes
  16. * using flags of xmlPatterncompile()
  17. * - making clear how pattern starting with / or . need to be handled,
  18. * currently push(NULL, NULL) means a reset of the streaming context
  19. * and indicating we are on / (the document node), probably need
  20. * something similar for .
  21. * - get rid of the "compile" starting with lowercase
  22. * - DONE (2006-05-16): get rid of the Strdup/Strndup in case of dictionary
  23. */
  24. #define IN_LIBXML
  25. #include "libxml.h"
  26. #include <string.h>
  27. #include <libxml/pattern.h>
  28. #include <libxml/xmlmemory.h>
  29. #include <libxml/tree.h>
  30. #include <libxml/dict.h>
  31. #include <libxml/xmlerror.h>
  32. #include <libxml/parserInternals.h>
  33. #ifdef LIBXML_PATTERN_ENABLED
  34. #ifdef ERROR
  35. #undef ERROR
  36. #endif
  37. #define ERROR(a, b, c, d)
  38. #define ERROR5(a, b, c, d, e)
  39. #define XML_STREAM_STEP_DESC 1
  40. #define XML_STREAM_STEP_FINAL 2
  41. #define XML_STREAM_STEP_ROOT 4
  42. #define XML_STREAM_STEP_ATTR 8
  43. #define XML_STREAM_STEP_NODE 16
  44. #define XML_STREAM_STEP_IN_SET 32
  45. /*
  46. * NOTE: Those private flags (XML_STREAM_xxx) are used
  47. * in _xmlStreamCtxt->flag. They extend the public
  48. * xmlPatternFlags, so be careful not to interfere with the
  49. * reserved values for xmlPatternFlags.
  50. */
  51. #define XML_STREAM_FINAL_IS_ANY_NODE 1<<14
  52. #define XML_STREAM_FROM_ROOT 1<<15
  53. #define XML_STREAM_DESC 1<<16
  54. /*
  55. * XML_STREAM_ANY_NODE is used for comparison against
  56. * xmlElementType enums, to indicate a node of any type.
  57. */
  58. #define XML_STREAM_ANY_NODE 100
  59. #define XML_PATTERN_NOTPATTERN (XML_PATTERN_XPATH | \
  60. XML_PATTERN_XSSEL | \
  61. XML_PATTERN_XSFIELD)
  62. #define XML_STREAM_XS_IDC(c) ((c)->flags & \
  63. (XML_PATTERN_XSSEL | XML_PATTERN_XSFIELD))
  64. #define XML_STREAM_XS_IDC_SEL(c) ((c)->flags & XML_PATTERN_XSSEL)
  65. #define XML_STREAM_XS_IDC_FIELD(c) ((c)->flags & XML_PATTERN_XSFIELD)
  66. #define XML_PAT_COPY_NSNAME(c, r, nsname) \
  67. if ((c)->comp->dict) \
  68. r = (xmlChar *) xmlDictLookup((c)->comp->dict, BAD_CAST nsname, -1); \
  69. else r = xmlStrdup(BAD_CAST nsname);
  70. #define XML_PAT_FREE_STRING(c, r) if ((c)->comp->dict == NULL) xmlFree(r);
  71. typedef struct _xmlStreamStep xmlStreamStep;
  72. typedef xmlStreamStep *xmlStreamStepPtr;
  73. struct _xmlStreamStep {
  74. int flags; /* properties of that step */
  75. const xmlChar *name; /* first string value if NULL accept all */
  76. const xmlChar *ns; /* second string value */
  77. int nodeType; /* type of node */
  78. };
  79. typedef struct _xmlStreamComp xmlStreamComp;
  80. typedef xmlStreamComp *xmlStreamCompPtr;
  81. struct _xmlStreamComp {
  82. xmlDict *dict; /* the dictionary if any */
  83. int nbStep; /* number of steps in the automata */
  84. int maxStep; /* allocated number of steps */
  85. xmlStreamStepPtr steps; /* the array of steps */
  86. int flags;
  87. };
  88. struct _xmlStreamCtxt {
  89. struct _xmlStreamCtxt *next;/* link to next sub pattern if | */
  90. xmlStreamCompPtr comp; /* the compiled stream */
  91. int nbState; /* number of states in the automata */
  92. int maxState; /* allocated number of states */
  93. int level; /* how deep are we ? */
  94. int *states; /* the array of step indexes */
  95. int flags; /* validation options */
  96. int blockLevel;
  97. };
  98. static void xmlFreeStreamComp(xmlStreamCompPtr comp);
  99. /*
  100. * Types are private:
  101. */
  102. typedef enum {
  103. XML_OP_END=0,
  104. XML_OP_ROOT,
  105. XML_OP_ELEM,
  106. XML_OP_CHILD,
  107. XML_OP_ATTR,
  108. XML_OP_PARENT,
  109. XML_OP_ANCESTOR,
  110. XML_OP_NS,
  111. XML_OP_ALL
  112. } xmlPatOp;
  113. typedef struct _xmlStepState xmlStepState;
  114. typedef xmlStepState *xmlStepStatePtr;
  115. struct _xmlStepState {
  116. int step;
  117. xmlNodePtr node;
  118. };
  119. typedef struct _xmlStepStates xmlStepStates;
  120. typedef xmlStepStates *xmlStepStatesPtr;
  121. struct _xmlStepStates {
  122. int nbstates;
  123. int maxstates;
  124. xmlStepStatePtr states;
  125. };
  126. typedef struct _xmlStepOp xmlStepOp;
  127. typedef xmlStepOp *xmlStepOpPtr;
  128. struct _xmlStepOp {
  129. xmlPatOp op;
  130. const xmlChar *value;
  131. const xmlChar *value2; /* The namespace name */
  132. };
  133. #define PAT_FROM_ROOT (1<<8)
  134. #define PAT_FROM_CUR (1<<9)
  135. struct _xmlPattern {
  136. void *data; /* the associated template */
  137. xmlDictPtr dict; /* the optional dictionary */
  138. struct _xmlPattern *next; /* next pattern if | is used */
  139. const xmlChar *pattern; /* the pattern */
  140. int flags; /* flags */
  141. int nbStep;
  142. int maxStep;
  143. xmlStepOpPtr steps; /* ops for computation */
  144. xmlStreamCompPtr stream; /* the streaming data if any */
  145. };
  146. typedef struct _xmlPatParserContext xmlPatParserContext;
  147. typedef xmlPatParserContext *xmlPatParserContextPtr;
  148. struct _xmlPatParserContext {
  149. const xmlChar *cur; /* the current char being parsed */
  150. const xmlChar *base; /* the full expression */
  151. int error; /* error code */
  152. xmlDictPtr dict; /* the dictionary if any */
  153. xmlPatternPtr comp; /* the result */
  154. xmlNodePtr elem; /* the current node if any */
  155. const xmlChar **namespaces; /* the namespaces definitions */
  156. int nb_namespaces; /* the number of namespaces */
  157. };
  158. /************************************************************************
  159. * *
  160. * Type functions *
  161. * *
  162. ************************************************************************/
  163. /**
  164. * xmlNewPattern:
  165. *
  166. * Create a new XSLT Pattern
  167. *
  168. * Returns the newly allocated xmlPatternPtr or NULL in case of error
  169. */
  170. static xmlPatternPtr
  171. xmlNewPattern(void) {
  172. xmlPatternPtr cur;
  173. cur = (xmlPatternPtr) xmlMalloc(sizeof(xmlPattern));
  174. if (cur == NULL) {
  175. ERROR(NULL, NULL, NULL,
  176. "xmlNewPattern : malloc failed\n");
  177. return(NULL);
  178. }
  179. memset(cur, 0, sizeof(xmlPattern));
  180. cur->maxStep = 10;
  181. cur->steps = (xmlStepOpPtr) xmlMalloc(cur->maxStep * sizeof(xmlStepOp));
  182. if (cur->steps == NULL) {
  183. xmlFree(cur);
  184. ERROR(NULL, NULL, NULL,
  185. "xmlNewPattern : malloc failed\n");
  186. return(NULL);
  187. }
  188. return(cur);
  189. }
  190. /**
  191. * xmlFreePattern:
  192. * @comp: an XSLT comp
  193. *
  194. * Free up the memory allocated by @comp
  195. */
  196. void
  197. xmlFreePattern(xmlPatternPtr comp) {
  198. xmlFreePatternList(comp);
  199. }
  200. static void
  201. xmlFreePatternInternal(xmlPatternPtr comp) {
  202. xmlStepOpPtr op;
  203. int i;
  204. if (comp == NULL)
  205. return;
  206. if (comp->stream != NULL)
  207. xmlFreeStreamComp(comp->stream);
  208. if (comp->pattern != NULL)
  209. xmlFree((xmlChar *)comp->pattern);
  210. if (comp->steps != NULL) {
  211. if (comp->dict == NULL) {
  212. for (i = 0;i < comp->nbStep;i++) {
  213. op = &comp->steps[i];
  214. if (op->value != NULL)
  215. xmlFree((xmlChar *) op->value);
  216. if (op->value2 != NULL)
  217. xmlFree((xmlChar *) op->value2);
  218. }
  219. }
  220. xmlFree(comp->steps);
  221. }
  222. if (comp->dict != NULL)
  223. xmlDictFree(comp->dict);
  224. memset(comp, -1, sizeof(xmlPattern));
  225. xmlFree(comp);
  226. }
  227. /**
  228. * xmlFreePatternList:
  229. * @comp: an XSLT comp list
  230. *
  231. * Free up the memory allocated by all the elements of @comp
  232. */
  233. void
  234. xmlFreePatternList(xmlPatternPtr comp) {
  235. xmlPatternPtr cur;
  236. while (comp != NULL) {
  237. cur = comp;
  238. comp = comp->next;
  239. cur->next = NULL;
  240. xmlFreePatternInternal(cur);
  241. }
  242. }
  243. /**
  244. * xmlNewPatParserContext:
  245. * @pattern: the pattern context
  246. * @dict: the inherited dictionary or NULL
  247. * @namespaces: the prefix definitions, array of [URI, prefix] terminated
  248. * with [NULL, NULL] or NULL if no namespace is used
  249. *
  250. * Create a new XML pattern parser context
  251. *
  252. * Returns the newly allocated xmlPatParserContextPtr or NULL in case of error
  253. */
  254. static xmlPatParserContextPtr
  255. xmlNewPatParserContext(const xmlChar *pattern, xmlDictPtr dict,
  256. const xmlChar **namespaces) {
  257. xmlPatParserContextPtr cur;
  258. if (pattern == NULL)
  259. return(NULL);
  260. cur = (xmlPatParserContextPtr) xmlMalloc(sizeof(xmlPatParserContext));
  261. if (cur == NULL) {
  262. ERROR(NULL, NULL, NULL,
  263. "xmlNewPatParserContext : malloc failed\n");
  264. return(NULL);
  265. }
  266. memset(cur, 0, sizeof(xmlPatParserContext));
  267. cur->dict = dict;
  268. cur->cur = pattern;
  269. cur->base = pattern;
  270. if (namespaces != NULL) {
  271. int i;
  272. for (i = 0;namespaces[2 * i] != NULL;i++)
  273. ;
  274. cur->nb_namespaces = i;
  275. } else {
  276. cur->nb_namespaces = 0;
  277. }
  278. cur->namespaces = namespaces;
  279. return(cur);
  280. }
  281. /**
  282. * xmlFreePatParserContext:
  283. * @ctxt: an XSLT parser context
  284. *
  285. * Free up the memory allocated by @ctxt
  286. */
  287. static void
  288. xmlFreePatParserContext(xmlPatParserContextPtr ctxt) {
  289. if (ctxt == NULL)
  290. return;
  291. memset(ctxt, -1, sizeof(xmlPatParserContext));
  292. xmlFree(ctxt);
  293. }
  294. /**
  295. * xmlPatternAdd:
  296. * @comp: the compiled match expression
  297. * @op: an op
  298. * @value: the first value
  299. * @value2: the second value
  300. *
  301. * Add a step to an XSLT Compiled Match
  302. *
  303. * Returns -1 in case of failure, 0 otherwise.
  304. */
  305. static int
  306. xmlPatternAdd(xmlPatParserContextPtr ctxt ATTRIBUTE_UNUSED,
  307. xmlPatternPtr comp,
  308. xmlPatOp op, xmlChar * value, xmlChar * value2)
  309. {
  310. if (comp->nbStep >= comp->maxStep) {
  311. xmlStepOpPtr temp;
  312. temp = (xmlStepOpPtr) xmlRealloc(comp->steps, comp->maxStep * 2 *
  313. sizeof(xmlStepOp));
  314. if (temp == NULL) {
  315. ERROR(ctxt, NULL, NULL,
  316. "xmlPatternAdd: realloc failed\n");
  317. return (-1);
  318. }
  319. comp->steps = temp;
  320. comp->maxStep *= 2;
  321. }
  322. comp->steps[comp->nbStep].op = op;
  323. comp->steps[comp->nbStep].value = value;
  324. comp->steps[comp->nbStep].value2 = value2;
  325. comp->nbStep++;
  326. return (0);
  327. }
  328. #if 0
  329. /**
  330. * xsltSwapTopPattern:
  331. * @comp: the compiled match expression
  332. *
  333. * reverse the two top steps.
  334. */
  335. static void
  336. xsltSwapTopPattern(xmlPatternPtr comp) {
  337. int i;
  338. int j = comp->nbStep - 1;
  339. if (j > 0) {
  340. register const xmlChar *tmp;
  341. register xmlPatOp op;
  342. i = j - 1;
  343. tmp = comp->steps[i].value;
  344. comp->steps[i].value = comp->steps[j].value;
  345. comp->steps[j].value = tmp;
  346. tmp = comp->steps[i].value2;
  347. comp->steps[i].value2 = comp->steps[j].value2;
  348. comp->steps[j].value2 = tmp;
  349. op = comp->steps[i].op;
  350. comp->steps[i].op = comp->steps[j].op;
  351. comp->steps[j].op = op;
  352. }
  353. }
  354. #endif
  355. /**
  356. * xmlReversePattern:
  357. * @comp: the compiled match expression
  358. *
  359. * reverse all the stack of expressions
  360. *
  361. * returns 0 in case of success and -1 in case of error.
  362. */
  363. static int
  364. xmlReversePattern(xmlPatternPtr comp) {
  365. int i, j;
  366. /*
  367. * remove the leading // for //a or .//a
  368. */
  369. if ((comp->nbStep > 0) && (comp->steps[0].op == XML_OP_ANCESTOR)) {
  370. for (i = 0, j = 1;j < comp->nbStep;i++,j++) {
  371. comp->steps[i].value = comp->steps[j].value;
  372. comp->steps[i].value2 = comp->steps[j].value2;
  373. comp->steps[i].op = comp->steps[j].op;
  374. }
  375. comp->nbStep--;
  376. }
  377. if (comp->nbStep >= comp->maxStep) {
  378. xmlStepOpPtr temp;
  379. temp = (xmlStepOpPtr) xmlRealloc(comp->steps, comp->maxStep * 2 *
  380. sizeof(xmlStepOp));
  381. if (temp == NULL) {
  382. ERROR(ctxt, NULL, NULL,
  383. "xmlReversePattern: realloc failed\n");
  384. return (-1);
  385. }
  386. comp->steps = temp;
  387. comp->maxStep *= 2;
  388. }
  389. i = 0;
  390. j = comp->nbStep - 1;
  391. while (j > i) {
  392. register const xmlChar *tmp;
  393. register xmlPatOp op;
  394. tmp = comp->steps[i].value;
  395. comp->steps[i].value = comp->steps[j].value;
  396. comp->steps[j].value = tmp;
  397. tmp = comp->steps[i].value2;
  398. comp->steps[i].value2 = comp->steps[j].value2;
  399. comp->steps[j].value2 = tmp;
  400. op = comp->steps[i].op;
  401. comp->steps[i].op = comp->steps[j].op;
  402. comp->steps[j].op = op;
  403. j--;
  404. i++;
  405. }
  406. comp->steps[comp->nbStep].value = NULL;
  407. comp->steps[comp->nbStep].value2 = NULL;
  408. comp->steps[comp->nbStep++].op = XML_OP_END;
  409. return(0);
  410. }
  411. /************************************************************************
  412. * *
  413. * The interpreter for the precompiled patterns *
  414. * *
  415. ************************************************************************/
  416. static int
  417. xmlPatPushState(xmlStepStates *states, int step, xmlNodePtr node) {
  418. if ((states->states == NULL) || (states->maxstates <= 0)) {
  419. states->maxstates = 4;
  420. states->nbstates = 0;
  421. states->states = xmlMalloc(4 * sizeof(xmlStepState));
  422. }
  423. else if (states->maxstates <= states->nbstates) {
  424. xmlStepState *tmp;
  425. tmp = (xmlStepStatePtr) xmlRealloc(states->states,
  426. 2 * states->maxstates * sizeof(xmlStepState));
  427. if (tmp == NULL)
  428. return(-1);
  429. states->states = tmp;
  430. states->maxstates *= 2;
  431. }
  432. states->states[states->nbstates].step = step;
  433. states->states[states->nbstates++].node = node;
  434. #if 0
  435. fprintf(stderr, "Push: %d, %s\n", step, node->name);
  436. #endif
  437. return(0);
  438. }
  439. /**
  440. * xmlPatMatch:
  441. * @comp: the precompiled pattern
  442. * @node: a node
  443. *
  444. * Test whether the node matches the pattern
  445. *
  446. * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
  447. */
  448. static int
  449. xmlPatMatch(xmlPatternPtr comp, xmlNodePtr node) {
  450. int i;
  451. xmlStepOpPtr step;
  452. xmlStepStates states = {0, 0, NULL}; /* // may require backtrack */
  453. if ((comp == NULL) || (node == NULL)) return(-1);
  454. i = 0;
  455. restart:
  456. for (;i < comp->nbStep;i++) {
  457. step = &comp->steps[i];
  458. switch (step->op) {
  459. case XML_OP_END:
  460. goto found;
  461. case XML_OP_ROOT:
  462. if (node->type == XML_NAMESPACE_DECL)
  463. goto rollback;
  464. node = node->parent;
  465. if ((node->type == XML_DOCUMENT_NODE) ||
  466. (node->type == XML_HTML_DOCUMENT_NODE))
  467. continue;
  468. goto rollback;
  469. case XML_OP_ELEM:
  470. if (node->type != XML_ELEMENT_NODE)
  471. goto rollback;
  472. if (step->value == NULL)
  473. continue;
  474. if (step->value[0] != node->name[0])
  475. goto rollback;
  476. if (!xmlStrEqual(step->value, node->name))
  477. goto rollback;
  478. /* Namespace test */
  479. if (node->ns == NULL) {
  480. if (step->value2 != NULL)
  481. goto rollback;
  482. } else if (node->ns->href != NULL) {
  483. if (step->value2 == NULL)
  484. goto rollback;
  485. if (!xmlStrEqual(step->value2, node->ns->href))
  486. goto rollback;
  487. }
  488. continue;
  489. case XML_OP_CHILD: {
  490. xmlNodePtr lst;
  491. if ((node->type != XML_ELEMENT_NODE) &&
  492. (node->type != XML_DOCUMENT_NODE) &&
  493. (node->type != XML_HTML_DOCUMENT_NODE))
  494. goto rollback;
  495. lst = node->children;
  496. if (step->value != NULL) {
  497. while (lst != NULL) {
  498. if ((lst->type == XML_ELEMENT_NODE) &&
  499. (step->value[0] == lst->name[0]) &&
  500. (xmlStrEqual(step->value, lst->name)))
  501. break;
  502. lst = lst->next;
  503. }
  504. if (lst != NULL)
  505. continue;
  506. }
  507. goto rollback;
  508. }
  509. case XML_OP_ATTR:
  510. if (node->type != XML_ATTRIBUTE_NODE)
  511. goto rollback;
  512. if (step->value != NULL) {
  513. if (step->value[0] != node->name[0])
  514. goto rollback;
  515. if (!xmlStrEqual(step->value, node->name))
  516. goto rollback;
  517. }
  518. /* Namespace test */
  519. if (node->ns == NULL) {
  520. if (step->value2 != NULL)
  521. goto rollback;
  522. } else if (step->value2 != NULL) {
  523. if (!xmlStrEqual(step->value2, node->ns->href))
  524. goto rollback;
  525. }
  526. continue;
  527. case XML_OP_PARENT:
  528. if ((node->type == XML_DOCUMENT_NODE) ||
  529. (node->type == XML_HTML_DOCUMENT_NODE) ||
  530. (node->type == XML_NAMESPACE_DECL))
  531. goto rollback;
  532. node = node->parent;
  533. if (node == NULL)
  534. goto rollback;
  535. if (step->value == NULL)
  536. continue;
  537. if (step->value[0] != node->name[0])
  538. goto rollback;
  539. if (!xmlStrEqual(step->value, node->name))
  540. goto rollback;
  541. /* Namespace test */
  542. if (node->ns == NULL) {
  543. if (step->value2 != NULL)
  544. goto rollback;
  545. } else if (node->ns->href != NULL) {
  546. if (step->value2 == NULL)
  547. goto rollback;
  548. if (!xmlStrEqual(step->value2, node->ns->href))
  549. goto rollback;
  550. }
  551. continue;
  552. case XML_OP_ANCESTOR:
  553. /* TODO: implement coalescing of ANCESTOR/NODE ops */
  554. if (step->value == NULL) {
  555. i++;
  556. step = &comp->steps[i];
  557. if (step->op == XML_OP_ROOT)
  558. goto found;
  559. if (step->op != XML_OP_ELEM)
  560. goto rollback;
  561. if (step->value == NULL)
  562. return(-1);
  563. }
  564. if (node == NULL)
  565. goto rollback;
  566. if ((node->type == XML_DOCUMENT_NODE) ||
  567. (node->type == XML_HTML_DOCUMENT_NODE) ||
  568. (node->type == XML_NAMESPACE_DECL))
  569. goto rollback;
  570. node = node->parent;
  571. while (node != NULL) {
  572. if ((node->type == XML_ELEMENT_NODE) &&
  573. (step->value[0] == node->name[0]) &&
  574. (xmlStrEqual(step->value, node->name))) {
  575. /* Namespace test */
  576. if (node->ns == NULL) {
  577. if (step->value2 == NULL)
  578. break;
  579. } else if (node->ns->href != NULL) {
  580. if ((step->value2 != NULL) &&
  581. (xmlStrEqual(step->value2, node->ns->href)))
  582. break;
  583. }
  584. }
  585. node = node->parent;
  586. }
  587. if (node == NULL)
  588. goto rollback;
  589. /*
  590. * prepare a potential rollback from here
  591. * for ancestors of that node.
  592. */
  593. if (step->op == XML_OP_ANCESTOR)
  594. xmlPatPushState(&states, i, node);
  595. else
  596. xmlPatPushState(&states, i - 1, node);
  597. continue;
  598. case XML_OP_NS:
  599. if (node->type != XML_ELEMENT_NODE)
  600. goto rollback;
  601. if (node->ns == NULL) {
  602. if (step->value != NULL)
  603. goto rollback;
  604. } else if (node->ns->href != NULL) {
  605. if (step->value == NULL)
  606. goto rollback;
  607. if (!xmlStrEqual(step->value, node->ns->href))
  608. goto rollback;
  609. }
  610. break;
  611. case XML_OP_ALL:
  612. if (node->type != XML_ELEMENT_NODE)
  613. goto rollback;
  614. break;
  615. }
  616. }
  617. found:
  618. if (states.states != NULL) {
  619. /* Free the rollback states */
  620. xmlFree(states.states);
  621. }
  622. return(1);
  623. rollback:
  624. /* got an error try to rollback */
  625. if (states.states == NULL)
  626. return(0);
  627. if (states.nbstates <= 0) {
  628. xmlFree(states.states);
  629. return(0);
  630. }
  631. states.nbstates--;
  632. i = states.states[states.nbstates].step;
  633. node = states.states[states.nbstates].node;
  634. #if 0
  635. fprintf(stderr, "Pop: %d, %s\n", i, node->name);
  636. #endif
  637. goto restart;
  638. }
  639. /************************************************************************
  640. * *
  641. * Dedicated parser for templates *
  642. * *
  643. ************************************************************************/
  644. #define TODO \
  645. xmlGenericError(xmlGenericErrorContext, \
  646. "Unimplemented block at %s:%d\n", \
  647. __FILE__, __LINE__);
  648. #define CUR (*ctxt->cur)
  649. #define SKIP(val) ctxt->cur += (val)
  650. #define NXT(val) ctxt->cur[(val)]
  651. #define PEEKPREV(val) ctxt->cur[-(val)]
  652. #define CUR_PTR ctxt->cur
  653. #define SKIP_BLANKS \
  654. while (IS_BLANK_CH(CUR)) NEXT
  655. #define CURRENT (*ctxt->cur)
  656. #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
  657. #define PUSH(op, val, val2) \
  658. if (xmlPatternAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
  659. #define XSLT_ERROR(X) \
  660. { xsltError(ctxt, __FILE__, __LINE__, X); \
  661. ctxt->error = (X); return; }
  662. #define XSLT_ERROR0(X) \
  663. { xsltError(ctxt, __FILE__, __LINE__, X); \
  664. ctxt->error = (X); return(0); }
  665. #if 0
  666. /**
  667. * xmlPatScanLiteral:
  668. * @ctxt: the XPath Parser context
  669. *
  670. * Parse an XPath Literal:
  671. *
  672. * [29] Literal ::= '"' [^"]* '"'
  673. * | "'" [^']* "'"
  674. *
  675. * Returns the Literal parsed or NULL
  676. */
  677. static xmlChar *
  678. xmlPatScanLiteral(xmlPatParserContextPtr ctxt) {
  679. const xmlChar *q, *cur;
  680. xmlChar *ret = NULL;
  681. int val, len;
  682. SKIP_BLANKS;
  683. if (CUR == '"') {
  684. NEXT;
  685. cur = q = CUR_PTR;
  686. val = xmlStringCurrentChar(NULL, cur, &len);
  687. while ((IS_CHAR(val)) && (val != '"')) {
  688. cur += len;
  689. val = xmlStringCurrentChar(NULL, cur, &len);
  690. }
  691. if (!IS_CHAR(val)) {
  692. ctxt->error = 1;
  693. return(NULL);
  694. } else {
  695. if (ctxt->dict)
  696. ret = (xmlChar *) xmlDictLookup(ctxt->dict, q, cur - q);
  697. else
  698. ret = xmlStrndup(q, cur - q);
  699. }
  700. cur += len;
  701. CUR_PTR = cur;
  702. } else if (CUR == '\'') {
  703. NEXT;
  704. cur = q = CUR_PTR;
  705. val = xmlStringCurrentChar(NULL, cur, &len);
  706. while ((IS_CHAR(val)) && (val != '\'')) {
  707. cur += len;
  708. val = xmlStringCurrentChar(NULL, cur, &len);
  709. }
  710. if (!IS_CHAR(val)) {
  711. ctxt->error = 1;
  712. return(NULL);
  713. } else {
  714. if (ctxt->dict)
  715. ret = (xmlChar *) xmlDictLookup(ctxt->dict, q, cur - q);
  716. else
  717. ret = xmlStrndup(q, cur - q);
  718. }
  719. cur += len;
  720. CUR_PTR = cur;
  721. } else {
  722. /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
  723. ctxt->error = 1;
  724. return(NULL);
  725. }
  726. return(ret);
  727. }
  728. #endif
  729. /**
  730. * xmlPatScanName:
  731. * @ctxt: the XPath Parser context
  732. *
  733. * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
  734. * CombiningChar | Extender
  735. *
  736. * [5] Name ::= (Letter | '_' | ':') (NameChar)*
  737. *
  738. * [6] Names ::= Name (S Name)*
  739. *
  740. * Returns the Name parsed or NULL
  741. */
  742. static xmlChar *
  743. xmlPatScanName(xmlPatParserContextPtr ctxt) {
  744. const xmlChar *q, *cur;
  745. xmlChar *ret = NULL;
  746. int val, len;
  747. SKIP_BLANKS;
  748. cur = q = CUR_PTR;
  749. val = xmlStringCurrentChar(NULL, cur, &len);
  750. if (!IS_LETTER(val) && (val != '_') && (val != ':'))
  751. return(NULL);
  752. while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
  753. (val == '.') || (val == '-') ||
  754. (val == '_') ||
  755. (IS_COMBINING(val)) ||
  756. (IS_EXTENDER(val))) {
  757. cur += len;
  758. val = xmlStringCurrentChar(NULL, cur, &len);
  759. }
  760. if (ctxt->dict)
  761. ret = (xmlChar *) xmlDictLookup(ctxt->dict, q, cur - q);
  762. else
  763. ret = xmlStrndup(q, cur - q);
  764. CUR_PTR = cur;
  765. return(ret);
  766. }
  767. /**
  768. * xmlPatScanNCName:
  769. * @ctxt: the XPath Parser context
  770. *
  771. * Parses a non qualified name
  772. *
  773. * Returns the Name parsed or NULL
  774. */
  775. static xmlChar *
  776. xmlPatScanNCName(xmlPatParserContextPtr ctxt) {
  777. const xmlChar *q, *cur;
  778. xmlChar *ret = NULL;
  779. int val, len;
  780. SKIP_BLANKS;
  781. cur = q = CUR_PTR;
  782. val = xmlStringCurrentChar(NULL, cur, &len);
  783. if (!IS_LETTER(val) && (val != '_'))
  784. return(NULL);
  785. while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
  786. (val == '.') || (val == '-') ||
  787. (val == '_') ||
  788. (IS_COMBINING(val)) ||
  789. (IS_EXTENDER(val))) {
  790. cur += len;
  791. val = xmlStringCurrentChar(NULL, cur, &len);
  792. }
  793. if (ctxt->dict)
  794. ret = (xmlChar *) xmlDictLookup(ctxt->dict, q, cur - q);
  795. else
  796. ret = xmlStrndup(q, cur - q);
  797. CUR_PTR = cur;
  798. return(ret);
  799. }
  800. #if 0
  801. /**
  802. * xmlPatScanQName:
  803. * @ctxt: the XPath Parser context
  804. * @prefix: the place to store the prefix
  805. *
  806. * Parse a qualified name
  807. *
  808. * Returns the Name parsed or NULL
  809. */
  810. static xmlChar *
  811. xmlPatScanQName(xmlPatParserContextPtr ctxt, xmlChar **prefix) {
  812. xmlChar *ret = NULL;
  813. *prefix = NULL;
  814. ret = xmlPatScanNCName(ctxt);
  815. if (CUR == ':') {
  816. *prefix = ret;
  817. NEXT;
  818. ret = xmlPatScanNCName(ctxt);
  819. }
  820. return(ret);
  821. }
  822. #endif
  823. /**
  824. * xmlCompileAttributeTest:
  825. * @ctxt: the compilation context
  826. *
  827. * Compile an attribute test.
  828. */
  829. static void
  830. xmlCompileAttributeTest(xmlPatParserContextPtr ctxt) {
  831. xmlChar *token = NULL;
  832. xmlChar *name = NULL;
  833. xmlChar *URL = NULL;
  834. SKIP_BLANKS;
  835. name = xmlPatScanNCName(ctxt);
  836. if (name == NULL) {
  837. if (CUR == '*') {
  838. PUSH(XML_OP_ATTR, NULL, NULL);
  839. NEXT;
  840. } else {
  841. ERROR(NULL, NULL, NULL,
  842. "xmlCompileAttributeTest : Name expected\n");
  843. ctxt->error = 1;
  844. }
  845. return;
  846. }
  847. if (CUR == ':') {
  848. int i;
  849. xmlChar *prefix = name;
  850. NEXT;
  851. if (IS_BLANK_CH(CUR)) {
  852. ERROR5(NULL, NULL, NULL, "Invalid QName.\n", NULL);
  853. ctxt->error = 1;
  854. goto error;
  855. }
  856. /*
  857. * This is a namespace match
  858. */
  859. token = xmlPatScanName(ctxt);
  860. if ((prefix[0] == 'x') &&
  861. (prefix[1] == 'm') &&
  862. (prefix[2] == 'l') &&
  863. (prefix[3] == 0))
  864. {
  865. XML_PAT_COPY_NSNAME(ctxt, URL, XML_XML_NAMESPACE);
  866. } else {
  867. for (i = 0;i < ctxt->nb_namespaces;i++) {
  868. if (xmlStrEqual(ctxt->namespaces[2 * i + 1], prefix)) {
  869. XML_PAT_COPY_NSNAME(ctxt, URL, ctxt->namespaces[2 * i])
  870. break;
  871. }
  872. }
  873. if (i >= ctxt->nb_namespaces) {
  874. ERROR5(NULL, NULL, NULL,
  875. "xmlCompileAttributeTest : no namespace bound to prefix %s\n",
  876. prefix);
  877. ctxt->error = 1;
  878. goto error;
  879. }
  880. }
  881. XML_PAT_FREE_STRING(ctxt, name);
  882. name = NULL;
  883. if (token == NULL) {
  884. if (CUR == '*') {
  885. NEXT;
  886. PUSH(XML_OP_ATTR, NULL, URL);
  887. } else {
  888. ERROR(NULL, NULL, NULL,
  889. "xmlCompileAttributeTest : Name expected\n");
  890. ctxt->error = 1;
  891. goto error;
  892. }
  893. } else {
  894. PUSH(XML_OP_ATTR, token, URL);
  895. }
  896. } else {
  897. PUSH(XML_OP_ATTR, name, NULL);
  898. }
  899. return;
  900. error:
  901. if (name != NULL)
  902. XML_PAT_FREE_STRING(ctxt, name);
  903. if (URL != NULL)
  904. XML_PAT_FREE_STRING(ctxt, URL)
  905. if (token != NULL)
  906. XML_PAT_FREE_STRING(ctxt, token);
  907. }
  908. /**
  909. * xmlCompileStepPattern:
  910. * @ctxt: the compilation context
  911. *
  912. * Compile the Step Pattern and generates a precompiled
  913. * form suitable for fast matching.
  914. *
  915. * [3] Step ::= '.' | NameTest
  916. * [4] NameTest ::= QName | '*' | NCName ':' '*'
  917. */
  918. static void
  919. xmlCompileStepPattern(xmlPatParserContextPtr ctxt) {
  920. xmlChar *token = NULL;
  921. xmlChar *name = NULL;
  922. xmlChar *URL = NULL;
  923. int hasBlanks = 0;
  924. SKIP_BLANKS;
  925. if (CUR == '.') {
  926. /*
  927. * Context node.
  928. */
  929. NEXT;
  930. PUSH(XML_OP_ELEM, NULL, NULL);
  931. return;
  932. }
  933. if (CUR == '@') {
  934. /*
  935. * Attribute test.
  936. */
  937. if (XML_STREAM_XS_IDC_SEL(ctxt->comp)) {
  938. ERROR5(NULL, NULL, NULL,
  939. "Unexpected attribute axis in '%s'.\n", ctxt->base);
  940. ctxt->error = 1;
  941. return;
  942. }
  943. NEXT;
  944. xmlCompileAttributeTest(ctxt);
  945. if (ctxt->error != 0)
  946. goto error;
  947. return;
  948. }
  949. name = xmlPatScanNCName(ctxt);
  950. if (name == NULL) {
  951. if (CUR == '*') {
  952. NEXT;
  953. PUSH(XML_OP_ALL, NULL, NULL);
  954. return;
  955. } else {
  956. ERROR(NULL, NULL, NULL,
  957. "xmlCompileStepPattern : Name expected\n");
  958. ctxt->error = 1;
  959. return;
  960. }
  961. }
  962. if (IS_BLANK_CH(CUR)) {
  963. hasBlanks = 1;
  964. SKIP_BLANKS;
  965. }
  966. if (CUR == ':') {
  967. NEXT;
  968. if (CUR != ':') {
  969. xmlChar *prefix = name;
  970. int i;
  971. if (hasBlanks || IS_BLANK_CH(CUR)) {
  972. ERROR5(NULL, NULL, NULL, "Invalid QName.\n", NULL);
  973. ctxt->error = 1;
  974. goto error;
  975. }
  976. /*
  977. * This is a namespace match
  978. */
  979. token = xmlPatScanName(ctxt);
  980. if ((prefix[0] == 'x') &&
  981. (prefix[1] == 'm') &&
  982. (prefix[2] == 'l') &&
  983. (prefix[3] == 0))
  984. {
  985. XML_PAT_COPY_NSNAME(ctxt, URL, XML_XML_NAMESPACE)
  986. } else {
  987. for (i = 0;i < ctxt->nb_namespaces;i++) {
  988. if (xmlStrEqual(ctxt->namespaces[2 * i + 1], prefix)) {
  989. XML_PAT_COPY_NSNAME(ctxt, URL, ctxt->namespaces[2 * i])
  990. break;
  991. }
  992. }
  993. if (i >= ctxt->nb_namespaces) {
  994. ERROR5(NULL, NULL, NULL,
  995. "xmlCompileStepPattern : no namespace bound to prefix %s\n",
  996. prefix);
  997. ctxt->error = 1;
  998. goto error;
  999. }
  1000. }
  1001. XML_PAT_FREE_STRING(ctxt, prefix);
  1002. name = NULL;
  1003. if (token == NULL) {
  1004. if (CUR == '*') {
  1005. NEXT;
  1006. PUSH(XML_OP_NS, URL, NULL);
  1007. } else {
  1008. ERROR(NULL, NULL, NULL,
  1009. "xmlCompileStepPattern : Name expected\n");
  1010. ctxt->error = 1;
  1011. goto error;
  1012. }
  1013. } else {
  1014. PUSH(XML_OP_ELEM, token, URL);
  1015. }
  1016. } else {
  1017. NEXT;
  1018. if (xmlStrEqual(name, (const xmlChar *) "child")) {
  1019. XML_PAT_FREE_STRING(ctxt, name);
  1020. name = xmlPatScanName(ctxt);
  1021. if (name == NULL) {
  1022. if (CUR == '*') {
  1023. NEXT;
  1024. PUSH(XML_OP_ALL, NULL, NULL);
  1025. return;
  1026. } else {
  1027. ERROR(NULL, NULL, NULL,
  1028. "xmlCompileStepPattern : QName expected\n");
  1029. ctxt->error = 1;
  1030. goto error;
  1031. }
  1032. }
  1033. if (CUR == ':') {
  1034. xmlChar *prefix = name;
  1035. int i;
  1036. NEXT;
  1037. if (IS_BLANK_CH(CUR)) {
  1038. ERROR5(NULL, NULL, NULL, "Invalid QName.\n", NULL);
  1039. ctxt->error = 1;
  1040. goto error;
  1041. }
  1042. /*
  1043. * This is a namespace match
  1044. */
  1045. token = xmlPatScanName(ctxt);
  1046. if ((prefix[0] == 'x') &&
  1047. (prefix[1] == 'm') &&
  1048. (prefix[2] == 'l') &&
  1049. (prefix[3] == 0))
  1050. {
  1051. XML_PAT_COPY_NSNAME(ctxt, URL, XML_XML_NAMESPACE)
  1052. } else {
  1053. for (i = 0;i < ctxt->nb_namespaces;i++) {
  1054. if (xmlStrEqual(ctxt->namespaces[2 * i + 1], prefix)) {
  1055. XML_PAT_COPY_NSNAME(ctxt, URL, ctxt->namespaces[2 * i])
  1056. break;
  1057. }
  1058. }
  1059. if (i >= ctxt->nb_namespaces) {
  1060. ERROR5(NULL, NULL, NULL,
  1061. "xmlCompileStepPattern : no namespace bound "
  1062. "to prefix %s\n", prefix);
  1063. ctxt->error = 1;
  1064. goto error;
  1065. }
  1066. }
  1067. XML_PAT_FREE_STRING(ctxt, prefix);
  1068. name = NULL;
  1069. if (token == NULL) {
  1070. if (CUR == '*') {
  1071. NEXT;
  1072. PUSH(XML_OP_NS, URL, NULL);
  1073. } else {
  1074. ERROR(NULL, NULL, NULL,
  1075. "xmlCompileStepPattern : Name expected\n");
  1076. ctxt->error = 1;
  1077. goto error;
  1078. }
  1079. } else {
  1080. PUSH(XML_OP_CHILD, token, URL);
  1081. }
  1082. } else
  1083. PUSH(XML_OP_CHILD, name, NULL);
  1084. return;
  1085. } else if (xmlStrEqual(name, (const xmlChar *) "attribute")) {
  1086. XML_PAT_FREE_STRING(ctxt, name)
  1087. name = NULL;
  1088. if (XML_STREAM_XS_IDC_SEL(ctxt->comp)) {
  1089. ERROR5(NULL, NULL, NULL,
  1090. "Unexpected attribute axis in '%s'.\n", ctxt->base);
  1091. ctxt->error = 1;
  1092. goto error;
  1093. }
  1094. xmlCompileAttributeTest(ctxt);
  1095. if (ctxt->error != 0)
  1096. goto error;
  1097. return;
  1098. } else {
  1099. ERROR5(NULL, NULL, NULL,
  1100. "The 'element' or 'attribute' axis is expected.\n", NULL);
  1101. ctxt->error = 1;
  1102. goto error;
  1103. }
  1104. }
  1105. } else if (CUR == '*') {
  1106. if (name != NULL) {
  1107. ctxt->error = 1;
  1108. goto error;
  1109. }
  1110. NEXT;
  1111. PUSH(XML_OP_ALL, token, NULL);
  1112. } else {
  1113. PUSH(XML_OP_ELEM, name, NULL);
  1114. }
  1115. return;
  1116. error:
  1117. if (URL != NULL)
  1118. XML_PAT_FREE_STRING(ctxt, URL)
  1119. if (token != NULL)
  1120. XML_PAT_FREE_STRING(ctxt, token)
  1121. if (name != NULL)
  1122. XML_PAT_FREE_STRING(ctxt, name)
  1123. }
  1124. /**
  1125. * xmlCompilePathPattern:
  1126. * @ctxt: the compilation context
  1127. *
  1128. * Compile the Path Pattern and generates a precompiled
  1129. * form suitable for fast matching.
  1130. *
  1131. * [5] Path ::= ('.//')? ( Step '/' )* ( Step | '@' NameTest )
  1132. */
  1133. static void
  1134. xmlCompilePathPattern(xmlPatParserContextPtr ctxt) {
  1135. SKIP_BLANKS;
  1136. if (CUR == '/') {
  1137. ctxt->comp->flags |= PAT_FROM_ROOT;
  1138. } else if ((CUR == '.') || (ctxt->comp->flags & XML_PATTERN_NOTPATTERN)) {
  1139. ctxt->comp->flags |= PAT_FROM_CUR;
  1140. }
  1141. if ((CUR == '/') && (NXT(1) == '/')) {
  1142. PUSH(XML_OP_ANCESTOR, NULL, NULL);
  1143. NEXT;
  1144. NEXT;
  1145. } else if ((CUR == '.') && (NXT(1) == '/') && (NXT(2) == '/')) {
  1146. PUSH(XML_OP_ANCESTOR, NULL, NULL);
  1147. NEXT;
  1148. NEXT;
  1149. NEXT;
  1150. /* Check for incompleteness. */
  1151. SKIP_BLANKS;
  1152. if (CUR == 0) {
  1153. ERROR5(NULL, NULL, NULL,
  1154. "Incomplete expression '%s'.\n", ctxt->base);
  1155. ctxt->error = 1;
  1156. goto error;
  1157. }
  1158. }
  1159. if (CUR == '@') {
  1160. NEXT;
  1161. xmlCompileAttributeTest(ctxt);
  1162. SKIP_BLANKS;
  1163. /* TODO: check for incompleteness */
  1164. if (CUR != 0) {
  1165. xmlCompileStepPattern(ctxt);
  1166. if (ctxt->error != 0)
  1167. goto error;
  1168. }
  1169. } else {
  1170. if (CUR == '/') {
  1171. PUSH(XML_OP_ROOT, NULL, NULL);
  1172. NEXT;
  1173. /* Check for incompleteness. */
  1174. SKIP_BLANKS;
  1175. if (CUR == 0) {
  1176. ERROR5(NULL, NULL, NULL,
  1177. "Incomplete expression '%s'.\n", ctxt->base);
  1178. ctxt->error = 1;
  1179. goto error;
  1180. }
  1181. }
  1182. xmlCompileStepPattern(ctxt);
  1183. if (ctxt->error != 0)
  1184. goto error;
  1185. SKIP_BLANKS;
  1186. while (CUR == '/') {
  1187. if (NXT(1) == '/') {
  1188. PUSH(XML_OP_ANCESTOR, NULL, NULL);
  1189. NEXT;
  1190. NEXT;
  1191. SKIP_BLANKS;
  1192. xmlCompileStepPattern(ctxt);
  1193. if (ctxt->error != 0)
  1194. goto error;
  1195. } else {
  1196. PUSH(XML_OP_PARENT, NULL, NULL);
  1197. NEXT;
  1198. SKIP_BLANKS;
  1199. if (CUR == 0) {
  1200. ERROR5(NULL, NULL, NULL,
  1201. "Incomplete expression '%s'.\n", ctxt->base);
  1202. ctxt->error = 1;
  1203. goto error;
  1204. }
  1205. xmlCompileStepPattern(ctxt);
  1206. if (ctxt->error != 0)
  1207. goto error;
  1208. }
  1209. }
  1210. }
  1211. if (CUR != 0) {
  1212. ERROR5(NULL, NULL, NULL,
  1213. "Failed to compile pattern %s\n", ctxt->base);
  1214. ctxt->error = 1;
  1215. }
  1216. error:
  1217. return;
  1218. }
  1219. /**
  1220. * xmlCompileIDCXPathPath:
  1221. * @ctxt: the compilation context
  1222. *
  1223. * Compile the Path Pattern and generates a precompiled
  1224. * form suitable for fast matching.
  1225. *
  1226. * [5] Path ::= ('.//')? ( Step '/' )* ( Step | '@' NameTest )
  1227. */
  1228. static void
  1229. xmlCompileIDCXPathPath(xmlPatParserContextPtr ctxt) {
  1230. SKIP_BLANKS;
  1231. if (CUR == '/') {
  1232. ERROR5(NULL, NULL, NULL,
  1233. "Unexpected selection of the document root in '%s'.\n",
  1234. ctxt->base);
  1235. goto error;
  1236. }
  1237. ctxt->comp->flags |= PAT_FROM_CUR;
  1238. if (CUR == '.') {
  1239. /* "." - "self::node()" */
  1240. NEXT;
  1241. SKIP_BLANKS;
  1242. if (CUR == 0) {
  1243. /*
  1244. * Selection of the context node.
  1245. */
  1246. PUSH(XML_OP_ELEM, NULL, NULL);
  1247. return;
  1248. }
  1249. if (CUR != '/') {
  1250. /* TODO: A more meaningful error message. */
  1251. ERROR5(NULL, NULL, NULL,
  1252. "Unexpected token after '.' in '%s'.\n", ctxt->base);
  1253. goto error;
  1254. }
  1255. /* "./" - "self::node()/" */
  1256. NEXT;
  1257. SKIP_BLANKS;
  1258. if (CUR == '/') {
  1259. if (IS_BLANK_CH(PEEKPREV(1))) {
  1260. /*
  1261. * Disallow "./ /"
  1262. */
  1263. ERROR5(NULL, NULL, NULL,
  1264. "Unexpected '/' token in '%s'.\n", ctxt->base);
  1265. goto error;
  1266. }
  1267. /* ".//" - "self:node()/descendant-or-self::node()/" */
  1268. PUSH(XML_OP_ANCESTOR, NULL, NULL);
  1269. NEXT;
  1270. SKIP_BLANKS;
  1271. }
  1272. if (CUR == 0)
  1273. goto error_unfinished;
  1274. }
  1275. /*
  1276. * Process steps.
  1277. */
  1278. do {
  1279. xmlCompileStepPattern(ctxt);
  1280. if (ctxt->error != 0)
  1281. goto error;
  1282. SKIP_BLANKS;
  1283. if (CUR != '/')
  1284. break;
  1285. PUSH(XML_OP_PARENT, NULL, NULL);
  1286. NEXT;
  1287. SKIP_BLANKS;
  1288. if (CUR == '/') {
  1289. /*
  1290. * Disallow subsequent '//'.
  1291. */
  1292. ERROR5(NULL, NULL, NULL,
  1293. "Unexpected subsequent '//' in '%s'.\n",
  1294. ctxt->base);
  1295. goto error;
  1296. }
  1297. if (CUR == 0)
  1298. goto error_unfinished;
  1299. } while (CUR != 0);
  1300. if (CUR != 0) {
  1301. ERROR5(NULL, NULL, NULL,
  1302. "Failed to compile expression '%s'.\n", ctxt->base);
  1303. ctxt->error = 1;
  1304. }
  1305. return;
  1306. error:
  1307. ctxt->error = 1;
  1308. return;
  1309. error_unfinished:
  1310. ctxt->error = 1;
  1311. ERROR5(NULL, NULL, NULL,
  1312. "Unfinished expression '%s'.\n", ctxt->base);
  1313. return;
  1314. }
  1315. /************************************************************************
  1316. * *
  1317. * The streaming code *
  1318. * *
  1319. ************************************************************************/
  1320. /**
  1321. * xmlNewStreamComp:
  1322. * @size: the number of expected steps
  1323. *
  1324. * build a new compiled pattern for streaming
  1325. *
  1326. * Returns the new structure or NULL in case of error.
  1327. */
  1328. static xmlStreamCompPtr
  1329. xmlNewStreamComp(int size) {
  1330. xmlStreamCompPtr cur;
  1331. if (size < 4)
  1332. size = 4;
  1333. cur = (xmlStreamCompPtr) xmlMalloc(sizeof(xmlStreamComp));
  1334. if (cur == NULL) {
  1335. ERROR(NULL, NULL, NULL,
  1336. "xmlNewStreamComp: malloc failed\n");
  1337. return(NULL);
  1338. }
  1339. memset(cur, 0, sizeof(xmlStreamComp));
  1340. cur->steps = (xmlStreamStepPtr) xmlMalloc(size * sizeof(xmlStreamStep));
  1341. if (cur->steps == NULL) {
  1342. xmlFree(cur);
  1343. ERROR(NULL, NULL, NULL,
  1344. "xmlNewStreamComp: malloc failed\n");
  1345. return(NULL);
  1346. }
  1347. cur->nbStep = 0;
  1348. cur->maxStep = size;
  1349. return(cur);
  1350. }
  1351. /**
  1352. * xmlFreeStreamComp:
  1353. * @comp: the compiled pattern for streaming
  1354. *
  1355. * Free the compiled pattern for streaming
  1356. */
  1357. static void
  1358. xmlFreeStreamComp(xmlStreamCompPtr comp) {
  1359. if (comp != NULL) {
  1360. if (comp->steps != NULL)
  1361. xmlFree(comp->steps);
  1362. if (comp->dict != NULL)
  1363. xmlDictFree(comp->dict);
  1364. xmlFree(comp);
  1365. }
  1366. }
  1367. /**
  1368. * xmlStreamCompAddStep:
  1369. * @comp: the compiled pattern for streaming
  1370. * @name: the first string, the name, or NULL for *
  1371. * @ns: the second step, the namespace name
  1372. * @flags: the flags for that step
  1373. *
  1374. * Add a new step to the compiled pattern
  1375. *
  1376. * Returns -1 in case of error or the step index if successful
  1377. */
  1378. static int
  1379. xmlStreamCompAddStep(xmlStreamCompPtr comp, const xmlChar *name,
  1380. const xmlChar *ns, int nodeType, int flags) {
  1381. xmlStreamStepPtr cur;
  1382. if (comp->nbStep >= comp->maxStep) {
  1383. cur = (xmlStreamStepPtr) xmlRealloc(comp->steps,
  1384. comp->maxStep * 2 * sizeof(xmlStreamStep));
  1385. if (cur == NULL) {
  1386. ERROR(NULL, NULL, NULL,
  1387. "xmlNewStreamComp: malloc failed\n");
  1388. return(-1);
  1389. }
  1390. comp->steps = cur;
  1391. comp->maxStep *= 2;
  1392. }
  1393. cur = &comp->steps[comp->nbStep++];
  1394. cur->flags = flags;
  1395. cur->name = name;
  1396. cur->ns = ns;
  1397. cur->nodeType = nodeType;
  1398. return(comp->nbStep - 1);
  1399. }
  1400. /**
  1401. * xmlStreamCompile:
  1402. * @comp: the precompiled pattern
  1403. *
  1404. * Tries to stream compile a pattern
  1405. *
  1406. * Returns -1 in case of failure and 0 in case of success.
  1407. */
  1408. static int
  1409. xmlStreamCompile(xmlPatternPtr comp) {
  1410. xmlStreamCompPtr stream;
  1411. int i, s = 0, root = 0, flags = 0, prevs = -1;
  1412. xmlStepOp step;
  1413. if ((comp == NULL) || (comp->steps == NULL))
  1414. return(-1);
  1415. /*
  1416. * special case for .
  1417. */
  1418. if ((comp->nbStep == 1) &&
  1419. (comp->steps[0].op == XML_OP_ELEM) &&
  1420. (comp->steps[0].value == NULL) &&
  1421. (comp->steps[0].value2 == NULL)) {
  1422. stream = xmlNewStreamComp(0);
  1423. if (stream == NULL)
  1424. return(-1);
  1425. /* Note that the stream will have no steps in this case. */
  1426. stream->flags |= XML_STREAM_FINAL_IS_ANY_NODE;
  1427. comp->stream = stream;
  1428. return(0);
  1429. }
  1430. stream = xmlNewStreamComp((comp->nbStep / 2) + 1);
  1431. if (stream == NULL)
  1432. return(-1);
  1433. if (comp->dict != NULL) {
  1434. stream->dict = comp->dict;
  1435. xmlDictReference(stream->dict);
  1436. }
  1437. i = 0;
  1438. if (comp->flags & PAT_FROM_ROOT)
  1439. stream->flags |= XML_STREAM_FROM_ROOT;
  1440. for (;i < comp->nbStep;i++) {
  1441. step = comp->steps[i];
  1442. switch (step.op) {
  1443. case XML_OP_END:
  1444. break;
  1445. case XML_OP_ROOT:
  1446. if (i != 0)
  1447. goto error;
  1448. root = 1;
  1449. break;
  1450. case XML_OP_NS:
  1451. s = xmlStreamCompAddStep(stream, NULL, step.value,
  1452. XML_ELEMENT_NODE, flags);
  1453. if (s < 0)
  1454. goto error;
  1455. prevs = s;
  1456. flags = 0;
  1457. break;
  1458. case XML_OP_ATTR:
  1459. flags |= XML_STREAM_STEP_ATTR;
  1460. prevs = -1;
  1461. s = xmlStreamCompAddStep(stream,
  1462. step.value, step.value2, XML_ATTRIBUTE_NODE, flags);
  1463. flags = 0;
  1464. if (s < 0)
  1465. goto error;
  1466. break;
  1467. case XML_OP_ELEM:
  1468. if ((step.value == NULL) && (step.value2 == NULL)) {
  1469. /*
  1470. * We have a "." or "self::node()" here.
  1471. * Eliminate redundant self::node() tests like in "/./."
  1472. * or "//./"
  1473. * The only case we won't eliminate is "//.", i.e. if
  1474. * self::node() is the last node test and we had
  1475. * continuation somewhere beforehand.
  1476. */
  1477. if ((comp->nbStep == i + 1) &&
  1478. (flags & XML_STREAM_STEP_DESC)) {
  1479. /*
  1480. * Mark the special case where the expression resolves
  1481. * to any type of node.
  1482. */
  1483. if (comp->nbStep == i + 1) {
  1484. stream->flags |= XML_STREAM_FINAL_IS_ANY_NODE;
  1485. }
  1486. flags |= XML_STREAM_STEP_NODE;
  1487. s = xmlStreamCompAddStep(stream, NULL, NULL,
  1488. XML_STREAM_ANY_NODE, flags);
  1489. if (s < 0)
  1490. goto error;
  1491. flags = 0;
  1492. /*
  1493. * If there was a previous step, mark it to be added to
  1494. * the result node-set; this is needed since only
  1495. * the last step will be marked as "final" and only
  1496. * "final" nodes are added to the resulting set.
  1497. */
  1498. if (prevs != -1) {
  1499. stream->steps[prevs].flags |= XML_STREAM_STEP_IN_SET;
  1500. prevs = -1;
  1501. }
  1502. break;
  1503. } else {
  1504. /* Just skip this one. */
  1505. continue;
  1506. }
  1507. }
  1508. /* An element node. */
  1509. s = xmlStreamCompAddStep(stream, step.value, step.value2,
  1510. XML_ELEMENT_NODE, flags);
  1511. if (s < 0)
  1512. goto error;
  1513. prevs = s;
  1514. flags = 0;
  1515. break;
  1516. case XML_OP_CHILD:
  1517. /* An element node child. */
  1518. s = xmlStreamCompAddStep(stream, step.value, step.value2,
  1519. XML_ELEMENT_NODE, flags);
  1520. if (s < 0)
  1521. goto error;
  1522. prevs = s;
  1523. flags = 0;
  1524. break;
  1525. case XML_OP_ALL:
  1526. s = xmlStreamCompAddStep(stream, NULL, NULL,
  1527. XML_ELEMENT_NODE, flags);
  1528. if (s < 0)
  1529. goto error;
  1530. prevs = s;
  1531. flags = 0;
  1532. break;
  1533. case XML_OP_PARENT:
  1534. break;
  1535. case XML_OP_ANCESTOR:
  1536. /* Skip redundant continuations. */
  1537. if (flags & XML_STREAM_STEP_DESC)
  1538. break;
  1539. flags |= XML_STREAM_STEP_DESC;
  1540. /*
  1541. * Mark the expression as having "//".
  1542. */
  1543. if ((stream->flags & XML_STREAM_DESC) == 0)
  1544. stream->flags |= XML_STREAM_DESC;
  1545. break;
  1546. }
  1547. }
  1548. if ((! root) && (comp->flags & XML_PATTERN_NOTPATTERN) == 0) {
  1549. /*
  1550. * If this should behave like a real pattern, we will mark
  1551. * the first step as having "//", to be reentrant on every
  1552. * tree level.
  1553. */
  1554. if ((stream->flags & XML_STREAM_DESC) == 0)
  1555. stream->flags |= XML_STREAM_DESC;
  1556. if (stream->nbStep > 0) {
  1557. if ((stream->steps[0].flags & XML_STREAM_STEP_DESC) == 0)
  1558. stream->steps[0].flags |= XML_STREAM_STEP_DESC;
  1559. }
  1560. }
  1561. if (stream->nbStep <= s)
  1562. goto error;
  1563. stream->steps[s].flags |= XML_STREAM_STEP_FINAL;
  1564. if (root)
  1565. stream->steps[0].flags |= XML_STREAM_STEP_ROOT;
  1566. comp->stream = stream;
  1567. return(0);
  1568. error:
  1569. xmlFreeStreamComp(stream);
  1570. return(0);
  1571. }
  1572. /**
  1573. * xmlNewStreamCtxt:
  1574. * @size: the number of expected states
  1575. *
  1576. * build a new stream context
  1577. *
  1578. * Returns the new structure or NULL in case of error.
  1579. */
  1580. static xmlStreamCtxtPtr
  1581. xmlNewStreamCtxt(xmlStreamCompPtr stream) {
  1582. xmlStreamCtxtPtr cur;
  1583. cur = (xmlStreamCtxtPtr) xmlMalloc(sizeof(xmlStreamCtxt));
  1584. if (cur == NULL) {
  1585. ERROR(NULL, NULL, NULL,
  1586. "xmlNewStreamCtxt: malloc failed\n");
  1587. return(NULL);
  1588. }
  1589. memset(cur, 0, sizeof(xmlStreamCtxt));
  1590. cur->states = (int *) xmlMalloc(4 * 2 * sizeof(int));
  1591. if (cur->states == NULL) {
  1592. xmlFree(cur);
  1593. ERROR(NULL, NULL, NULL,
  1594. "xmlNewStreamCtxt: malloc failed\n");
  1595. return(NULL);
  1596. }
  1597. cur->nbState = 0;
  1598. cur->maxState = 4;
  1599. cur->level = 0;
  1600. cur->comp = stream;
  1601. cur->blockLevel = -1;
  1602. return(cur);
  1603. }
  1604. /**
  1605. * xmlFreeStreamCtxt:
  1606. * @stream: the stream context
  1607. *
  1608. * Free the stream context
  1609. */
  1610. void
  1611. xmlFreeStreamCtxt(xmlStreamCtxtPtr stream) {
  1612. xmlStreamCtxtPtr next;
  1613. while (stream != NULL) {
  1614. next = stream->next;
  1615. if (stream->states != NULL)
  1616. xmlFree(stream->states);
  1617. xmlFree(stream);
  1618. stream = next;
  1619. }
  1620. }
  1621. /**
  1622. * xmlStreamCtxtAddState:
  1623. * @comp: the stream context
  1624. * @idx: the step index for that streaming state
  1625. *
  1626. * Add a new state to the stream context
  1627. *
  1628. * Returns -1 in case of error or the state index if successful
  1629. */
  1630. static int
  1631. xmlStreamCtxtAddState(xmlStreamCtxtPtr comp, int idx, int level) {
  1632. int i;
  1633. for (i = 0;i < comp->nbState;i++) {
  1634. if (comp->states[2 * i] < 0) {
  1635. comp->states[2 * i] = idx;
  1636. comp->states[2 * i + 1] = level;
  1637. return(i);
  1638. }
  1639. }
  1640. if (comp->nbState >= comp->maxState) {
  1641. int *cur;
  1642. cur = (int *) xmlRealloc(comp->states,
  1643. comp->maxState * 4 * sizeof(int));
  1644. if (cur == NULL) {
  1645. ERROR(NULL, NULL, NULL,
  1646. "xmlNewStreamCtxt: malloc failed\n");
  1647. return(-1);
  1648. }
  1649. comp->states = cur;
  1650. comp->maxState *= 2;
  1651. }
  1652. comp->states[2 * comp->nbState] = idx;
  1653. comp->states[2 * comp->nbState++ + 1] = level;
  1654. return(comp->nbState - 1);
  1655. }
  1656. /**
  1657. * xmlStreamPushInternal:
  1658. * @stream: the stream context
  1659. * @name: the current name
  1660. * @ns: the namespace name
  1661. * @nodeType: the type of the node
  1662. *
  1663. * Push new data onto the stream. NOTE: if the call xmlPatterncompile()
  1664. * indicated a dictionary, then strings for name and ns will be expected
  1665. * to come from the dictionary.
  1666. * Both @name and @ns being NULL means the / i.e. the root of the document.
  1667. * This can also act as a reset.
  1668. *
  1669. * Returns: -1 in case of error, 1 if the current state in the stream is a
  1670. * match and 0 otherwise.
  1671. */
  1672. static int
  1673. xmlStreamPushInternal(xmlStreamCtxtPtr stream,
  1674. const xmlChar *name, const xmlChar *ns,
  1675. int nodeType) {
  1676. int ret = 0, err = 0, final = 0, tmp, i, m, match, stepNr, desc;
  1677. xmlStreamCompPtr comp;
  1678. xmlStreamStep step;
  1679. if ((stream == NULL) || (stream->nbState < 0))
  1680. return(-1);
  1681. while (stream != NULL) {
  1682. comp = stream->comp;
  1683. if ((nodeType == XML_ELEMENT_NODE) &&
  1684. (name == NULL) && (ns == NULL)) {
  1685. /* We have a document node here (or a reset). */
  1686. stream->nbState = 0;
  1687. stream->level = 0;
  1688. stream->blockLevel = -1;
  1689. if (comp->flags & XML_STREAM_FROM_ROOT) {
  1690. if (comp->nbStep == 0) {
  1691. /* TODO: We have a "/." here? */
  1692. ret = 1;
  1693. } else {
  1694. if ((comp->nbStep == 1) &&
  1695. (comp->steps[0].nodeType == XML_STREAM_ANY_NODE) &&
  1696. (comp->steps[0].flags & XML_STREAM_STEP_DESC))
  1697. {
  1698. /*
  1699. * In the case of "//." the document node will match
  1700. * as well.
  1701. */
  1702. ret = 1;
  1703. } else if (comp->steps[0].flags & XML_STREAM_STEP_ROOT) {
  1704. /* TODO: Do we need this ? */
  1705. tmp = xmlStreamCtxtAddState(stream, 0, 0);
  1706. if (tmp < 0)
  1707. err++;
  1708. }
  1709. }
  1710. }
  1711. stream = stream->next;
  1712. continue; /* while */
  1713. }
  1714. /*
  1715. * Fast check for ".".
  1716. */
  1717. if (comp->nbStep == 0) {
  1718. /*
  1719. * / and . are handled at the XPath node set creation
  1720. * level by checking min depth
  1721. */
  1722. if (stream->flags & XML_PATTERN_XPATH) {
  1723. stream = stream->next;
  1724. continue; /* while */
  1725. }
  1726. /*
  1727. * For non-pattern like evaluation like XML Schema IDCs
  1728. * or traditional XPath expressions, this will match if
  1729. * we are at the first level only, otherwise on every level.
  1730. */
  1731. if ((nodeType != XML_ATTRIBUTE_NODE) &&
  1732. (((stream->flags & XML_PATTERN_NOTPATTERN) == 0) ||
  1733. (stream->level == 0))) {
  1734. ret = 1;
  1735. }
  1736. stream->level++;
  1737. goto stream_next;
  1738. }
  1739. if (stream->blockLevel != -1) {
  1740. /*
  1741. * Skip blocked expressions.
  1742. */
  1743. stream->level++;
  1744. goto stream_next;
  1745. }
  1746. if ((nodeType != XML_ELEMENT_NODE) &&
  1747. (nodeType != XML_ATTRIBUTE_NODE) &&
  1748. ((comp->flags & XML_STREAM_FINAL_IS_ANY_NODE) == 0)) {
  1749. /*
  1750. * No need to process nodes of other types if we don't
  1751. * resolve to those types.
  1752. * TODO: Do we need to block the context here?
  1753. */
  1754. stream->level++;
  1755. goto stream_next;
  1756. }
  1757. /*
  1758. * Check evolution of existing states
  1759. */
  1760. i = 0;
  1761. m = stream->nbState;
  1762. while (i < m) {
  1763. if ((comp->flags & XML_STREAM_DESC) == 0) {
  1764. /*
  1765. * If there is no "//", then only the last
  1766. * added state is of interest.
  1767. */
  1768. stepNr = stream->states[2 * (stream->nbState -1)];
  1769. /*
  1770. * TODO: Security check, should not happen, remove it.
  1771. */
  1772. if (stream->states[(2 * (stream->nbState -1)) + 1] <
  1773. stream->level) {
  1774. return (-1);
  1775. }
  1776. desc = 0;
  1777. /* loop-stopper */
  1778. i = m;
  1779. } else {
  1780. /*
  1781. * If there are "//", then we need to process every "//"
  1782. * occurring in the states, plus any other state for this
  1783. * level.
  1784. */
  1785. stepNr = stream->states[2 * i];
  1786. /* TODO: should not happen anymore: dead states */
  1787. if (stepNr < 0)
  1788. goto next_state;
  1789. tmp = stream->states[(2 * i) + 1];
  1790. /* skip new states just added */
  1791. if (tmp > stream->level)
  1792. goto next_state;
  1793. /* skip states at ancestor levels, except if "//" */
  1794. desc = comp->steps[stepNr].flags & XML_STREAM_STEP_DESC;
  1795. if ((tmp < stream->level) && (!desc))
  1796. goto next_state;
  1797. }
  1798. /*
  1799. * Check for correct node-type.
  1800. */
  1801. step = comp->steps[stepNr];
  1802. if (step.nodeType != nodeType) {
  1803. if (step.nodeType == XML_ATTRIBUTE_NODE) {
  1804. /*
  1805. * Block this expression for deeper evaluation.
  1806. */
  1807. if ((comp->flags & XML_STREAM_DESC) == 0)
  1808. stream->blockLevel = stream->level +1;
  1809. goto next_state;
  1810. } else if (step.nodeType != XML_STREAM_ANY_NODE)
  1811. goto next_state;
  1812. }
  1813. /*
  1814. * Compare local/namespace-name.
  1815. */
  1816. match = 0;
  1817. if (step.nodeType == XML_STREAM_ANY_NODE) {
  1818. match = 1;
  1819. } else if (step.name == NULL) {
  1820. if (step.ns == NULL) {
  1821. /*
  1822. * This lets through all elements/attributes.
  1823. */
  1824. match = 1;
  1825. } else if (ns != NULL)
  1826. match = xmlStrEqual(step.ns, ns);
  1827. } else if (((step.ns != NULL) == (ns != NULL)) &&
  1828. (name != NULL) &&
  1829. (step.name[0] == name[0]) &&
  1830. xmlStrEqual(step.name, name) &&
  1831. ((step.ns == ns) || xmlStrEqual(step.ns, ns)))
  1832. {
  1833. match = 1;
  1834. }
  1835. #if 0
  1836. /*
  1837. * TODO: Pointer comparison won't work, since not guaranteed that the given
  1838. * values are in the same dict; especially if it's the namespace name,
  1839. * normally coming from ns->href. We need a namespace dict mechanism !
  1840. */
  1841. } else if (comp->dict) {
  1842. if (step.name == NULL) {
  1843. if (step.ns == NULL)
  1844. match = 1;
  1845. else
  1846. match = (step.ns == ns);
  1847. } else {
  1848. match = ((step.name == name) && (step.ns == ns));
  1849. }
  1850. #endif /* if 0 ------------------------------------------------------- */
  1851. if (match) {
  1852. final = step.flags & XML_STREAM_STEP_FINAL;
  1853. if (final) {
  1854. ret = 1;
  1855. } else {
  1856. xmlStreamCtxtAddState(stream, stepNr + 1,
  1857. stream->level + 1);
  1858. }
  1859. if ((ret != 1) && (step.flags & XML_STREAM_STEP_IN_SET)) {
  1860. /*
  1861. * Check if we have a special case like "foo/bar//.", where
  1862. * "foo" is selected as well.
  1863. */
  1864. ret = 1;
  1865. }
  1866. }
  1867. if (((comp->flags & XML_STREAM_DESC) == 0) &&
  1868. ((! match) || final)) {
  1869. /*
  1870. * Mark this expression as blocked for any evaluation at
  1871. * deeper levels. Note that this includes "/foo"
  1872. * expressions if the *pattern* behaviour is used.
  1873. */
  1874. stream->blockLevel = stream->level +1;
  1875. }
  1876. next_state:
  1877. i++;
  1878. }
  1879. stream->level++;
  1880. /*
  1881. * Re/enter the expression.
  1882. * Don't reenter if it's an absolute expression like "/foo",
  1883. * except "//foo".
  1884. */
  1885. step = comp->steps[0];
  1886. if (step.flags & XML_STREAM_STEP_ROOT)
  1887. goto stream_next;
  1888. desc = step.flags & XML_STREAM_STEP_DESC;
  1889. if (stream->flags & XML_PATTERN_NOTPATTERN) {
  1890. /*
  1891. * Re/enter the expression if it is a "descendant" one,
  1892. * or if we are at the 1st level of evaluation.
  1893. */
  1894. if (stream->level == 1) {
  1895. if (XML_STREAM_XS_IDC(stream)) {
  1896. /*
  1897. * XS-IDC: The missing "self::node()" will always
  1898. * match the first given node.
  1899. */
  1900. goto stream_next;
  1901. } else
  1902. goto compare;
  1903. }
  1904. /*
  1905. * A "//" is always reentrant.
  1906. */
  1907. if (desc)
  1908. goto compare;
  1909. /*
  1910. * XS-IDC: Process the 2nd level, since the missing
  1911. * "self::node()" is responsible for the 2nd level being
  1912. * the real start level.
  1913. */
  1914. if ((stream->level == 2) && XML_STREAM_XS_IDC(stream))
  1915. goto compare;
  1916. goto stream_next;
  1917. }
  1918. compare:
  1919. /*
  1920. * Check expected node-type.
  1921. */
  1922. if (step.nodeType != nodeType) {
  1923. if (nodeType == XML_ATTRIBUTE_NODE)
  1924. goto stream_next;
  1925. else if (step.nodeType != XML_STREAM_ANY_NODE)
  1926. goto stream_next;
  1927. }
  1928. /*
  1929. * Compare local/namespace-name.
  1930. */
  1931. match = 0;
  1932. if (step.nodeType == XML_STREAM_ANY_NODE) {
  1933. match = 1;
  1934. } else if (step.name == NULL) {
  1935. if (step.ns == NULL) {
  1936. /*
  1937. * This lets through all elements/attributes.
  1938. */
  1939. match = 1;
  1940. } else if (ns != NULL)
  1941. match = xmlStrEqual(step.ns, ns);
  1942. } else if (((step.ns != NULL) == (ns != NULL)) &&
  1943. (name != NULL) &&
  1944. (step.name[0] == name[0]) &&
  1945. xmlStrEqual(step.name, name) &&
  1946. ((step.ns == ns) || xmlStrEqual(step.ns, ns)))
  1947. {
  1948. match = 1;
  1949. }
  1950. final = step.flags & XML_STREAM_STEP_FINAL;
  1951. if (match) {
  1952. if (final)
  1953. ret = 1;
  1954. else
  1955. xmlStreamCtxtAddState(stream, 1, stream->level);
  1956. if ((ret != 1) && (step.flags & XML_STREAM_STEP_IN_SET)) {
  1957. /*
  1958. * Check if we have a special case like "foo//.", where
  1959. * "foo" is selected as well.
  1960. */
  1961. ret = 1;
  1962. }
  1963. }
  1964. if (((comp->flags & XML_STREAM_DESC) == 0) &&
  1965. ((! match) || final)) {
  1966. /*
  1967. * Mark this expression as blocked for any evaluation at
  1968. * deeper levels.
  1969. */
  1970. stream->blockLevel = stream->level;
  1971. }
  1972. stream_next:
  1973. stream = stream->next;
  1974. } /* while stream != NULL */
  1975. if (err > 0)
  1976. ret = -1;
  1977. return(ret);
  1978. }
  1979. /**
  1980. * xmlStreamPush:
  1981. * @stream: the stream context
  1982. * @name: the current name
  1983. * @ns: the namespace name
  1984. *
  1985. * Push new data onto the stream. NOTE: if the call xmlPatterncompile()
  1986. * indicated a dictionary, then strings for name and ns will be expected
  1987. * to come from the dictionary.
  1988. * Both @name and @ns being NULL means the / i.e. the root of the document.
  1989. * This can also act as a reset.
  1990. * Otherwise the function will act as if it has been given an element-node.
  1991. *
  1992. * Returns: -1 in case of error, 1 if the current state in the stream is a
  1993. * match and 0 otherwise.
  1994. */
  1995. int
  1996. xmlStreamPush(xmlStreamCtxtPtr stream,
  1997. const xmlChar *name, const xmlChar *ns) {
  1998. return (xmlStreamPushInternal(stream, name, ns, XML_ELEMENT_NODE));
  1999. }
  2000. /**
  2001. * xmlStreamPushNode:
  2002. * @stream: the stream context
  2003. * @name: the current name
  2004. * @ns: the namespace name
  2005. * @nodeType: the type of the node being pushed
  2006. *
  2007. * Push new data onto the stream. NOTE: if the call xmlPatterncompile()
  2008. * indicated a dictionary, then strings for name and ns will be expected
  2009. * to come from the dictionary.
  2010. * Both @name and @ns being NULL means the / i.e. the root of the document.
  2011. * This can also act as a reset.
  2012. * Different from xmlStreamPush() this function can be fed with nodes of type:
  2013. * element-, attribute-, text-, cdata-section-, comment- and
  2014. * processing-instruction-node.
  2015. *
  2016. * Returns: -1 in case of error, 1 if the current state in the stream is a
  2017. * match and 0 otherwise.
  2018. */
  2019. int
  2020. xmlStreamPushNode(xmlStreamCtxtPtr stream,
  2021. const xmlChar *name, const xmlChar *ns,
  2022. int nodeType)
  2023. {
  2024. return (xmlStreamPushInternal(stream, name, ns,
  2025. nodeType));
  2026. }
  2027. /**
  2028. * xmlStreamPushAttr:
  2029. * @stream: the stream context
  2030. * @name: the current name
  2031. * @ns: the namespace name
  2032. *
  2033. * Push new attribute data onto the stream. NOTE: if the call xmlPatterncompile()
  2034. * indicated a dictionary, then strings for name and ns will be expected
  2035. * to come from the dictionary.
  2036. * Both @name and @ns being NULL means the / i.e. the root of the document.
  2037. * This can also act as a reset.
  2038. * Otherwise the function will act as if it has been given an attribute-node.
  2039. *
  2040. * Returns: -1 in case of error, 1 if the current state in the stream is a
  2041. * match and 0 otherwise.
  2042. */
  2043. int
  2044. xmlStreamPushAttr(xmlStreamCtxtPtr stream,
  2045. const xmlChar *name, const xmlChar *ns) {
  2046. return (xmlStreamPushInternal(stream, name, ns, XML_ATTRIBUTE_NODE));
  2047. }
  2048. /**
  2049. * xmlStreamPop:
  2050. * @stream: the stream context
  2051. *
  2052. * push one level from the stream.
  2053. *
  2054. * Returns: -1 in case of error, 0 otherwise.
  2055. */
  2056. int
  2057. xmlStreamPop(xmlStreamCtxtPtr stream) {
  2058. int i, lev;
  2059. if (stream == NULL)
  2060. return(-1);
  2061. while (stream != NULL) {
  2062. /*
  2063. * Reset block-level.
  2064. */
  2065. if (stream->blockLevel == stream->level)
  2066. stream->blockLevel = -1;
  2067. /*
  2068. * stream->level can be zero when XML_FINAL_IS_ANY_NODE is set
  2069. * (see the thread at
  2070. * http://mail.gnome.org/archives/xslt/2008-July/msg00027.html)
  2071. */
  2072. if (stream->level)
  2073. stream->level--;
  2074. /*
  2075. * Check evolution of existing states
  2076. */
  2077. for (i = stream->nbState -1; i >= 0; i--) {
  2078. /* discard obsoleted states */
  2079. lev = stream->states[(2 * i) + 1];
  2080. if (lev > stream->level)
  2081. stream->nbState--;
  2082. if (lev <= stream->level)
  2083. break;
  2084. }
  2085. stream = stream->next;
  2086. }
  2087. return(0);
  2088. }
  2089. /**
  2090. * xmlStreamWantsAnyNode:
  2091. * @streamCtxt: the stream context
  2092. *
  2093. * Query if the streaming pattern additionally needs to be fed with
  2094. * text-, cdata-section-, comment- and processing-instruction-nodes.
  2095. * If the result is 0 then only element-nodes and attribute-nodes
  2096. * need to be pushed.
  2097. *
  2098. * Returns: 1 in case of need of nodes of the above described types,
  2099. * 0 otherwise. -1 on API errors.
  2100. */
  2101. int
  2102. xmlStreamWantsAnyNode(xmlStreamCtxtPtr streamCtxt)
  2103. {
  2104. if (streamCtxt == NULL)
  2105. return(-1);
  2106. while (streamCtxt != NULL) {
  2107. if (streamCtxt->comp->flags & XML_STREAM_FINAL_IS_ANY_NODE)
  2108. return(1);
  2109. streamCtxt = streamCtxt->next;
  2110. }
  2111. return(0);
  2112. }
  2113. /************************************************************************
  2114. * *
  2115. * The public interfaces *
  2116. * *
  2117. ************************************************************************/
  2118. /**
  2119. * xmlPatterncompile:
  2120. * @pattern: the pattern to compile
  2121. * @dict: an optional dictionary for interned strings
  2122. * @flags: compilation flags, see xmlPatternFlags
  2123. * @namespaces: the prefix definitions, array of [URI, prefix] or NULL
  2124. *
  2125. * Compile a pattern.
  2126. *
  2127. * Returns the compiled form of the pattern or NULL in case of error
  2128. */
  2129. xmlPatternPtr
  2130. xmlPatterncompile(const xmlChar *pattern, xmlDict *dict, int flags,
  2131. const xmlChar **namespaces) {
  2132. xmlPatternPtr ret = NULL, cur;
  2133. xmlPatParserContextPtr ctxt = NULL;
  2134. const xmlChar *or, *start;
  2135. xmlChar *tmp = NULL;
  2136. int type = 0;
  2137. int streamable = 1;
  2138. if (pattern == NULL)
  2139. return(NULL);
  2140. start = pattern;
  2141. or = start;
  2142. while (*or != 0) {
  2143. tmp = NULL;
  2144. while ((*or != 0) && (*or != '|')) or++;
  2145. if (*or == 0)
  2146. ctxt = xmlNewPatParserContext(start, dict, namespaces);
  2147. else {
  2148. tmp = xmlStrndup(start, or - start);
  2149. if (tmp != NULL) {
  2150. ctxt = xmlNewPatParserContext(tmp, dict, namespaces);
  2151. }
  2152. or++;
  2153. }
  2154. if (ctxt == NULL) goto error;
  2155. cur = xmlNewPattern();
  2156. if (cur == NULL) goto error;
  2157. /*
  2158. * Assign string dict.
  2159. */
  2160. if (dict) {
  2161. cur->dict = dict;
  2162. xmlDictReference(dict);
  2163. }
  2164. if (ret == NULL)
  2165. ret = cur;
  2166. else {
  2167. cur->next = ret->next;
  2168. ret->next = cur;
  2169. }
  2170. cur->flags = flags;
  2171. ctxt->comp = cur;
  2172. if (XML_STREAM_XS_IDC(cur))
  2173. xmlCompileIDCXPathPath(ctxt);
  2174. else
  2175. xmlCompilePathPattern(ctxt);
  2176. if (ctxt->error != 0)
  2177. goto error;
  2178. xmlFreePatParserContext(ctxt);
  2179. ctxt = NULL;
  2180. if (streamable) {
  2181. if (type == 0) {
  2182. type = cur->flags & (PAT_FROM_ROOT | PAT_FROM_CUR);
  2183. } else if (type == PAT_FROM_ROOT) {
  2184. if (cur->flags & PAT_FROM_CUR)
  2185. streamable = 0;
  2186. } else if (type == PAT_FROM_CUR) {
  2187. if (cur->flags & PAT_FROM_ROOT)
  2188. streamable = 0;
  2189. }
  2190. }
  2191. if (streamable)
  2192. xmlStreamCompile(cur);
  2193. if (xmlReversePattern(cur) < 0)
  2194. goto error;
  2195. if (tmp != NULL) {
  2196. xmlFree(tmp);
  2197. tmp = NULL;
  2198. }
  2199. start = or;
  2200. }
  2201. if (streamable == 0) {
  2202. cur = ret;
  2203. while (cur != NULL) {
  2204. if (cur->stream != NULL) {
  2205. xmlFreeStreamComp(cur->stream);
  2206. cur->stream = NULL;
  2207. }
  2208. cur = cur->next;
  2209. }
  2210. }
  2211. return(ret);
  2212. error:
  2213. if (ctxt != NULL) xmlFreePatParserContext(ctxt);
  2214. if (ret != NULL) xmlFreePattern(ret);
  2215. if (tmp != NULL) xmlFree(tmp);
  2216. return(NULL);
  2217. }
  2218. /**
  2219. * xmlPatternMatch:
  2220. * @comp: the precompiled pattern
  2221. * @node: a node
  2222. *
  2223. * Test whether the node matches the pattern
  2224. *
  2225. * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
  2226. */
  2227. int
  2228. xmlPatternMatch(xmlPatternPtr comp, xmlNodePtr node)
  2229. {
  2230. int ret = 0;
  2231. if ((comp == NULL) || (node == NULL))
  2232. return(-1);
  2233. while (comp != NULL) {
  2234. ret = xmlPatMatch(comp, node);
  2235. if (ret != 0)
  2236. return(ret);
  2237. comp = comp->next;
  2238. }
  2239. return(ret);
  2240. }
  2241. /**
  2242. * xmlPatternGetStreamCtxt:
  2243. * @comp: the precompiled pattern
  2244. *
  2245. * Get a streaming context for that pattern
  2246. * Use xmlFreeStreamCtxt to free the context.
  2247. *
  2248. * Returns a pointer to the context or NULL in case of failure
  2249. */
  2250. xmlStreamCtxtPtr
  2251. xmlPatternGetStreamCtxt(xmlPatternPtr comp)
  2252. {
  2253. xmlStreamCtxtPtr ret = NULL, cur;
  2254. if ((comp == NULL) || (comp->stream == NULL))
  2255. return(NULL);
  2256. while (comp != NULL) {
  2257. if (comp->stream == NULL)
  2258. goto failed;
  2259. cur = xmlNewStreamCtxt(comp->stream);
  2260. if (cur == NULL)
  2261. goto failed;
  2262. if (ret == NULL)
  2263. ret = cur;
  2264. else {
  2265. cur->next = ret->next;
  2266. ret->next = cur;
  2267. }
  2268. cur->flags = comp->flags;
  2269. comp = comp->next;
  2270. }
  2271. return(ret);
  2272. failed:
  2273. xmlFreeStreamCtxt(ret);
  2274. return(NULL);
  2275. }
  2276. /**
  2277. * xmlPatternStreamable:
  2278. * @comp: the precompiled pattern
  2279. *
  2280. * Check if the pattern is streamable i.e. xmlPatternGetStreamCtxt()
  2281. * should work.
  2282. *
  2283. * Returns 1 if streamable, 0 if not and -1 in case of error.
  2284. */
  2285. int
  2286. xmlPatternStreamable(xmlPatternPtr comp) {
  2287. if (comp == NULL)
  2288. return(-1);
  2289. while (comp != NULL) {
  2290. if (comp->stream == NULL)
  2291. return(0);
  2292. comp = comp->next;
  2293. }
  2294. return(1);
  2295. }
  2296. /**
  2297. * xmlPatternMaxDepth:
  2298. * @comp: the precompiled pattern
  2299. *
  2300. * Check the maximum depth reachable by a pattern
  2301. *
  2302. * Returns -2 if no limit (using //), otherwise the depth,
  2303. * and -1 in case of error
  2304. */
  2305. int
  2306. xmlPatternMaxDepth(xmlPatternPtr comp) {
  2307. int ret = 0, i;
  2308. if (comp == NULL)
  2309. return(-1);
  2310. while (comp != NULL) {
  2311. if (comp->stream == NULL)
  2312. return(-1);
  2313. for (i = 0;i < comp->stream->nbStep;i++)
  2314. if (comp->stream->steps[i].flags & XML_STREAM_STEP_DESC)
  2315. return(-2);
  2316. if (comp->stream->nbStep > ret)
  2317. ret = comp->stream->nbStep;
  2318. comp = comp->next;
  2319. }
  2320. return(ret);
  2321. }
  2322. /**
  2323. * xmlPatternMinDepth:
  2324. * @comp: the precompiled pattern
  2325. *
  2326. * Check the minimum depth reachable by a pattern, 0 mean the / or . are
  2327. * part of the set.
  2328. *
  2329. * Returns -1 in case of error otherwise the depth,
  2330. *
  2331. */
  2332. int
  2333. xmlPatternMinDepth(xmlPatternPtr comp) {
  2334. int ret = 12345678;
  2335. if (comp == NULL)
  2336. return(-1);
  2337. while (comp != NULL) {
  2338. if (comp->stream == NULL)
  2339. return(-1);
  2340. if (comp->stream->nbStep < ret)
  2341. ret = comp->stream->nbStep;
  2342. if (ret == 0)
  2343. return(0);
  2344. comp = comp->next;
  2345. }
  2346. return(ret);
  2347. }
  2348. /**
  2349. * xmlPatternFromRoot:
  2350. * @comp: the precompiled pattern
  2351. *
  2352. * Check if the pattern must be looked at from the root.
  2353. *
  2354. * Returns 1 if true, 0 if false and -1 in case of error
  2355. */
  2356. int
  2357. xmlPatternFromRoot(xmlPatternPtr comp) {
  2358. if (comp == NULL)
  2359. return(-1);
  2360. while (comp != NULL) {
  2361. if (comp->stream == NULL)
  2362. return(-1);
  2363. if (comp->flags & PAT_FROM_ROOT)
  2364. return(1);
  2365. comp = comp->next;
  2366. }
  2367. return(0);
  2368. }
  2369. #endif /* LIBXML_PATTERN_ENABLED */