Iterator.cs 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. //
  2. // System.Xml.XPath.BaseIterator
  3. //
  4. // Author:
  5. // Piers Haken ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C) 2002 Piers Haken
  9. // (C) 2003 Atsushi Enomoto
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Xml;
  34. using System.Xml.XPath;
  35. using System.Xml.Xsl;
  36. namespace System.Xml.XPath
  37. {
  38. internal abstract class BaseIterator : XPathNodeIterator
  39. {
  40. private XmlNamespaceManager _nsm;
  41. protected bool _needClone = true; // TODO: use this field in practice.
  42. internal BaseIterator (BaseIterator other)
  43. {
  44. _nsm = other._nsm;
  45. }
  46. internal BaseIterator (XmlNamespaceManager nsm)
  47. {
  48. _nsm = nsm;
  49. }
  50. public XmlNamespaceManager NamespaceManager
  51. {
  52. get { return _nsm; }
  53. set { _nsm = value; }
  54. }
  55. public virtual bool ReverseAxis {
  56. get { return false; }
  57. }
  58. public abstract bool RequireSorting { get; }
  59. public virtual int ComparablePosition {
  60. get {
  61. if (ReverseAxis) {
  62. int diff = Count - CurrentPosition + 1;
  63. return diff < 1 ? 1 : diff;
  64. }
  65. else
  66. return CurrentPosition;
  67. }
  68. }
  69. public override string ToString ()
  70. {
  71. if (Current != null)
  72. return Current.NodeType.ToString () + "[" + CurrentPosition + "] : " + Current.Name + " = " + Current.Value;
  73. else
  74. return this.GetType().ToString () + "[" + CurrentPosition + "]";
  75. }
  76. }
  77. internal abstract class SimpleIterator : BaseIterator
  78. {
  79. protected readonly BaseIterator _iter;
  80. protected readonly XPathNavigator _nav;
  81. protected XPathNavigator _current;
  82. protected int _pos;
  83. public SimpleIterator (BaseIterator iter) : base (iter)
  84. {
  85. _iter = iter;
  86. _nav = iter.Current.Clone ();
  87. _current = _nav.Clone ();
  88. }
  89. protected SimpleIterator (SimpleIterator other) : base (other)
  90. {
  91. if (other._nav == null)
  92. _iter = (BaseIterator) other._iter.Clone ();
  93. else
  94. _nav = other._nav.Clone ();
  95. _pos = other._pos;
  96. _current = other._current.Clone ();
  97. }
  98. public SimpleIterator (XPathNavigator nav, XmlNamespaceManager nsm) : base (nsm)
  99. {
  100. _nav = nav.Clone ();
  101. _current = nav.Clone ();
  102. }
  103. public override XPathNavigator Current { get { return _current; }}
  104. public override int CurrentPosition { get { return _pos; }}
  105. }
  106. internal class SelfIterator : SimpleIterator
  107. {
  108. public SelfIterator (BaseIterator iter) : base (iter) {}
  109. public SelfIterator (XPathNavigator nav, XmlNamespaceManager nsm) : base (nav, nsm) {}
  110. protected SelfIterator (SelfIterator other) : base (other) {}
  111. public override XPathNodeIterator Clone () { return new SelfIterator (this); }
  112. public override bool MoveNext ()
  113. {
  114. if (_pos == 0)
  115. {
  116. _pos = 1;
  117. _current = _needClone ? _nav.Clone () : _nav;
  118. return true;
  119. }
  120. return false;
  121. }
  122. public override bool RequireSorting { get { return false; } }
  123. }
  124. internal class NullIterator : SelfIterator
  125. {
  126. public NullIterator (BaseIterator iter) : base (iter) {}
  127. public NullIterator (XPathNavigator nav) : this (nav, null) {}
  128. public NullIterator (XPathNavigator nav, XmlNamespaceManager nsm) : base (nav, nsm) {}
  129. protected NullIterator (NullIterator other) : base (other) {}
  130. public override XPathNodeIterator Clone () { return new NullIterator (this); }
  131. public override bool MoveNext ()
  132. {
  133. return false;
  134. }
  135. }
  136. internal class ParensIterator : BaseIterator
  137. {
  138. BaseIterator _iter;
  139. public ParensIterator (BaseIterator iter) : base (iter)
  140. {
  141. _iter = iter;
  142. }
  143. protected ParensIterator (ParensIterator other) : base (other)
  144. {
  145. _iter = (BaseIterator) other._iter.Clone ();
  146. }
  147. public override XPathNodeIterator Clone () { return new ParensIterator (this); }
  148. public override bool MoveNext ()
  149. {
  150. return _iter.MoveNext ();
  151. }
  152. public override XPathNavigator Current { get { return _iter.Current; }}
  153. public override int CurrentPosition { get { return _iter.CurrentPosition; } }
  154. public override bool RequireSorting { get { return _iter.RequireSorting; } }
  155. public override int Count { get { return _iter.Count; } }
  156. }
  157. internal class ParentIterator : SimpleIterator
  158. {
  159. public ParentIterator (BaseIterator iter) : base (iter) {}
  160. protected ParentIterator (ParentIterator other) : base (other) {}
  161. public ParentIterator (XPathNavigator nav, XmlNamespaceManager nsm) : base (nav, nsm) {}
  162. public override XPathNodeIterator Clone () { return new ParentIterator (this); }
  163. public override bool MoveNext ()
  164. {
  165. if (_pos == 0 && _nav.MoveToParent ())
  166. {
  167. _pos = 1;
  168. _current = _needClone ? _nav.Clone () : _nav;
  169. return true;
  170. }
  171. return false;
  172. }
  173. public override bool RequireSorting { get { return true; } }
  174. }
  175. internal class ChildIterator : SimpleIterator
  176. {
  177. public ChildIterator (BaseIterator iter) : base (iter) {}
  178. protected ChildIterator (ChildIterator other) : base (other) {}
  179. public override XPathNodeIterator Clone () { return new ChildIterator (this); }
  180. public override bool MoveNext ()
  181. {
  182. bool fSuccess = (_pos == 0) ? _nav.MoveToFirstChild () : _nav.MoveToNext ();
  183. if (fSuccess) {
  184. _pos ++;
  185. // This clone cannot be omitted
  186. _current = _nav.Clone ();
  187. }
  188. return fSuccess;
  189. }
  190. public override bool RequireSorting { get { return false; } }
  191. }
  192. internal class FollowingSiblingIterator : SimpleIterator
  193. {
  194. public FollowingSiblingIterator (BaseIterator iter) : base (iter) {}
  195. protected FollowingSiblingIterator (FollowingSiblingIterator other) : base (other) {}
  196. public override XPathNodeIterator Clone () { return new FollowingSiblingIterator (this); }
  197. public override bool MoveNext ()
  198. {
  199. switch (_nav.NodeType) {
  200. case XPathNodeType.Attribute:
  201. case XPathNodeType.Namespace:
  202. // They have no siblings.
  203. return false;
  204. }
  205. if (_nav.MoveToNext ())
  206. {
  207. _pos ++;
  208. // This clone cannot be omitted
  209. _current = _nav.Clone ();
  210. return true;
  211. }
  212. return false;
  213. }
  214. public override bool RequireSorting { get { return false; } }
  215. }
  216. internal class PrecedingSiblingIterator : SimpleIterator
  217. {
  218. bool finished;
  219. bool started;
  220. XPathNavigator startPosition;
  221. public PrecedingSiblingIterator (BaseIterator iter) : base (iter)
  222. {
  223. startPosition = iter.Current.Clone ();
  224. _current = startPosition.Clone ();
  225. }
  226. protected PrecedingSiblingIterator (PrecedingSiblingIterator other) : base (other)
  227. {
  228. startPosition = other.startPosition;
  229. started = other.started;
  230. finished = other.finished;
  231. _current = other._current.Clone ();
  232. }
  233. public override XPathNodeIterator Clone () { return new PrecedingSiblingIterator (this); }
  234. public override bool MoveNext ()
  235. {
  236. if (finished)
  237. return false;
  238. if (!started) {
  239. started = true;
  240. switch (_nav.NodeType) {
  241. case XPathNodeType.Attribute:
  242. case XPathNodeType.Namespace:
  243. // They have no siblings.
  244. finished = true;
  245. return false;
  246. }
  247. _nav.MoveToFirst ();
  248. if (_nav.ComparePosition (startPosition) != XmlNodeOrder.Same) {
  249. _pos++;
  250. // This clone cannot be omitted
  251. _current = _nav.Clone ();
  252. return true;
  253. }
  254. } else {
  255. if (!_nav.MoveToNext ()) {
  256. finished = true;
  257. return false;
  258. }
  259. }
  260. if (_nav.ComparePosition (startPosition) != XmlNodeOrder.Before) {
  261. // Note that if _nav contains only 1 node, it won't be Same.
  262. finished = true;
  263. return false;
  264. } else {
  265. _pos ++;
  266. // This clone cannot be omitted
  267. _current = _nav.Clone ();
  268. return true;
  269. }
  270. }
  271. public override bool ReverseAxis {
  272. get { return true; }
  273. }
  274. public override bool RequireSorting { get { return true; } }
  275. }
  276. internal class AncestorIterator : SimpleIterator
  277. {
  278. bool finished;
  279. bool started;
  280. ArrayList positions = new ArrayList ();
  281. XPathNavigator startPosition;
  282. int nextDepth;
  283. public AncestorIterator (BaseIterator iter) : base (iter)
  284. {
  285. startPosition = iter.Current.Clone ();
  286. _current = startPosition.Clone ();
  287. }
  288. protected AncestorIterator (AncestorIterator other) : base (other)
  289. {
  290. startPosition = other.startPosition;
  291. started = other.started;
  292. finished = other.finished;
  293. positions = (ArrayList) other.positions.Clone ();
  294. nextDepth = other.nextDepth;
  295. _current = other._current.Clone ();
  296. }
  297. public override XPathNodeIterator Clone () { return new AncestorIterator (this); }
  298. public override bool MoveNext ()
  299. {
  300. if (finished)
  301. return false;
  302. if (!started) {
  303. started = true;
  304. // This clone cannot be omitted
  305. XPathNavigator ancestors = startPosition.Clone ();
  306. ancestors.MoveToParent ();
  307. _nav.MoveToParent ();
  308. while (ancestors.NodeType != XPathNodeType.Root) {
  309. int i = 0;
  310. _nav.MoveToFirst ();
  311. while (_nav.ComparePosition (ancestors) == XmlNodeOrder.Before) {
  312. _nav.MoveToNext ();
  313. i++;
  314. }
  315. positions.Add (i);
  316. if (!ancestors.MoveToParent ())
  317. break; // It is for detached nodes under XmlDocumentNavigator
  318. _nav.MoveToParent ();
  319. }
  320. positions.Reverse ();
  321. if (startPosition.NodeType != XPathNodeType.Root) {
  322. // First time it returns Root
  323. _pos++;
  324. // This clone cannot be omitted
  325. _current = _nav.Clone ();
  326. return true;
  327. }
  328. }
  329. // Don't worry about node type of start position, like AncestorOrSelf.
  330. // It should be Element or Root.
  331. if (nextDepth < positions.Count) {
  332. int thisTimePos = (int) positions [nextDepth];
  333. _nav.MoveToFirstChild ();
  334. for (int i = 0; i < thisTimePos; i++)
  335. _nav.MoveToNext ();
  336. nextDepth++;
  337. _pos++;
  338. // This clone cannot be omitted
  339. _current = _nav.Clone ();
  340. return true;
  341. }
  342. finished = true;
  343. return false;
  344. }
  345. public override bool ReverseAxis {
  346. get { return true; }
  347. }
  348. public override bool RequireSorting { get { return true; } }
  349. public override int Count { get { return positions.Count; } }
  350. }
  351. internal class AncestorOrSelfIterator : SimpleIterator
  352. {
  353. bool finished;
  354. bool started;
  355. ArrayList positions = new ArrayList ();
  356. XPathNavigator startPosition;
  357. int nextDepth;
  358. public AncestorOrSelfIterator (BaseIterator iter) : base (iter)
  359. {
  360. startPosition = iter.Current.Clone ();
  361. _current = startPosition.Clone ();
  362. }
  363. protected AncestorOrSelfIterator (AncestorOrSelfIterator other) : base (other)
  364. {
  365. startPosition = other.startPosition;
  366. started = other.started;
  367. finished = other.finished;
  368. positions = (ArrayList) other.positions.Clone ();
  369. nextDepth = other.nextDepth;
  370. _current = other._current.Clone ();
  371. }
  372. public override XPathNodeIterator Clone () { return new AncestorOrSelfIterator (this); }
  373. public override bool MoveNext ()
  374. {
  375. bool initialIteration = false;
  376. if (finished)
  377. return false;
  378. if (!started) {
  379. initialIteration = true;
  380. started = true;
  381. // This clone cannot be omitted
  382. XPathNavigator ancestors = startPosition.Clone ();
  383. do {
  384. int i = 0;
  385. _nav.MoveToFirst ();
  386. while (_nav.ComparePosition (ancestors) == XmlNodeOrder.Before) {
  387. _nav.MoveToNext ();
  388. i++;
  389. }
  390. positions.Add (i);
  391. if (!ancestors.MoveToParent ())
  392. break; // for detached nodes under XmlDocumentNavigator.
  393. _nav.MoveToParent ();
  394. } while (ancestors.NodeType != XPathNodeType.Root);
  395. positions.Reverse ();
  396. }
  397. if (initialIteration && startPosition.NodeType != XPathNodeType.Root) {
  398. // This clone cannot be omitted
  399. _current = _nav.Clone ();
  400. return true;
  401. } else if (nextDepth + 1 == positions.Count) {
  402. nextDepth++;
  403. _pos++;
  404. _nav.MoveTo (startPosition);
  405. // This clone cannot be omitted
  406. _current = _nav.Clone ();
  407. return true;
  408. }
  409. else if (nextDepth < positions.Count) {
  410. int thisTimePos = (int) positions [nextDepth];
  411. _nav.MoveToFirstChild ();
  412. for (int i = 0; i < thisTimePos; i++)
  413. _nav.MoveToNext ();
  414. nextDepth++;
  415. _pos++;
  416. // This clone cannot be omitted
  417. _current = _nav.Clone ();
  418. return true;
  419. }
  420. finished = true;
  421. return false;
  422. }
  423. public override bool ReverseAxis {
  424. get { return true; }
  425. }
  426. public override bool RequireSorting { get { return true; } }
  427. public override int Count { get { return positions.Count; } }
  428. }
  429. internal class DescendantIterator : SimpleIterator
  430. {
  431. protected int _depth;
  432. private bool _finished;
  433. public DescendantIterator (BaseIterator iter) : base (iter) {}
  434. protected DescendantIterator (DescendantIterator other) : base (other)
  435. {
  436. _depth = other._depth;
  437. _current = other._current.Clone ();
  438. }
  439. public override XPathNodeIterator Clone () { return new DescendantIterator (this); }
  440. public override bool MoveNext ()
  441. {
  442. if (_finished)
  443. return false;
  444. if (_nav.MoveToFirstChild ())
  445. {
  446. _depth ++;
  447. _pos ++;
  448. // This clone cannot be omitted
  449. _current = _nav.Clone ();
  450. return true;
  451. }
  452. while (_depth != 0)
  453. {
  454. if (_nav.MoveToNext ())
  455. {
  456. _pos ++;
  457. // This clone cannot be omitted
  458. _current = _nav.Clone ();
  459. return true;
  460. }
  461. if (!_nav.MoveToParent ()) // should NEVER fail!
  462. throw new XPathException ("There seems some bugs on the XPathNavigator implementation class.");
  463. _depth --;
  464. }
  465. _finished = true;
  466. return false;
  467. }
  468. public override bool RequireSorting { get { return false; } }
  469. }
  470. internal class DescendantOrSelfIterator : SimpleIterator
  471. {
  472. protected int _depth;
  473. private bool _finished;
  474. public DescendantOrSelfIterator (BaseIterator iter) : base (iter) {}
  475. protected DescendantOrSelfIterator (DescendantOrSelfIterator other) : base (other)
  476. {
  477. _depth = other._depth;
  478. _current = other._current.Clone ();
  479. }
  480. public override XPathNodeIterator Clone () { return new DescendantOrSelfIterator (this); }
  481. public override bool MoveNext ()
  482. {
  483. if (_finished)
  484. return false;
  485. if (_pos == 0)
  486. {
  487. // self
  488. _pos ++;
  489. // This clone cannot be omitted
  490. _current = _nav.Clone ();
  491. return true;
  492. }
  493. if (_nav.MoveToFirstChild ())
  494. {
  495. _depth ++;
  496. _pos ++;
  497. // This clone cannot be omitted
  498. _current = _nav.Clone ();
  499. return true;
  500. }
  501. while (_depth != 0)
  502. {
  503. if (_nav.MoveToNext ())
  504. {
  505. _pos ++;
  506. // This clone cannot be omitted
  507. _current = _nav.Clone ();
  508. return true;
  509. }
  510. if (!_nav.MoveToParent ()) // should NEVER fail!
  511. throw new XPathException ("There seems some bugs on the XPathNavigator implementation class.");
  512. _depth --;
  513. }
  514. _finished = true;
  515. return false;
  516. }
  517. public override bool RequireSorting { get { return false; } }
  518. }
  519. internal class FollowingIterator : SimpleIterator
  520. {
  521. private bool _finished = false;
  522. public FollowingIterator (BaseIterator iter) : base (iter) {}
  523. protected FollowingIterator (FollowingIterator other) : base (other) {}
  524. public override XPathNodeIterator Clone () { return new FollowingIterator (this); }
  525. public override bool MoveNext ()
  526. {
  527. if (_finished)
  528. return false;
  529. if (_pos == 0)
  530. {
  531. if (_nav.MoveToNext ())
  532. {
  533. _pos ++;
  534. // This clone cannot be omitted
  535. _current = _nav.Clone ();
  536. return true;
  537. } else {
  538. while (_nav.MoveToParent ()) {
  539. if (_nav.MoveToNext ()) {
  540. _pos ++;
  541. // This clone cannot be omitted
  542. _current = _nav.Clone ();
  543. return true;
  544. }
  545. }
  546. }
  547. }
  548. else
  549. {
  550. if (_nav.MoveToFirstChild ())
  551. {
  552. _pos ++;
  553. // This clone cannot be omitted
  554. _current = _nav.Clone ();
  555. return true;
  556. }
  557. do
  558. {
  559. if (_nav.MoveToNext ())
  560. {
  561. _pos ++;
  562. // This clone cannot be omitted
  563. _current = _nav.Clone ();
  564. return true;
  565. }
  566. }
  567. while (_nav.MoveToParent ());
  568. }
  569. _finished = true;
  570. return false;
  571. }
  572. public override bool RequireSorting { get { return false; } }
  573. }
  574. internal class PrecedingIterator : SimpleIterator
  575. {
  576. bool finished;
  577. bool started;
  578. XPathNavigator startPosition;
  579. public PrecedingIterator (BaseIterator iter) : base (iter)
  580. {
  581. startPosition = iter.Current.Clone ();
  582. _current = startPosition.Clone ();
  583. }
  584. protected PrecedingIterator (PrecedingIterator other) : base (other)
  585. {
  586. startPosition = other.startPosition;
  587. started = other.started;
  588. finished = other.finished;
  589. _current = other._current.Clone ();
  590. }
  591. public override XPathNodeIterator Clone () { return new PrecedingIterator (this); }
  592. public override bool MoveNext ()
  593. {
  594. if (finished)
  595. return false;
  596. if (!started) {
  597. started = true;
  598. _nav.MoveToRoot ();
  599. }
  600. bool loop = true;
  601. while (loop) {
  602. while (!_nav.MoveToFirstChild ()) {
  603. while (!_nav.MoveToNext ()) {
  604. if (!_nav.MoveToParent ()) { // Should not finish, at least before startPosition.
  605. finished = true;
  606. return false;
  607. }
  608. }
  609. break;
  610. }
  611. if (_nav.IsDescendant (startPosition))
  612. continue;
  613. loop = false;
  614. break;
  615. }
  616. if (_nav.ComparePosition (startPosition) != XmlNodeOrder.Before) {
  617. // Note that if _nav contains only 1 node, it won't be Same.
  618. finished = true;
  619. return false;
  620. } else {
  621. _pos ++;
  622. // This cannot be omitted
  623. _current = _nav.Clone ();
  624. return true;
  625. }
  626. }
  627. public override bool ReverseAxis {
  628. get { return true; }
  629. }
  630. public override bool RequireSorting { get { return true; } }
  631. }
  632. internal class NamespaceIterator : SimpleIterator
  633. {
  634. public NamespaceIterator (BaseIterator iter) : base (iter) {}
  635. protected NamespaceIterator (NamespaceIterator other) : base (other) {}
  636. public override XPathNodeIterator Clone () { return new NamespaceIterator (this); }
  637. public override bool MoveNext ()
  638. {
  639. if (_pos == 0)
  640. {
  641. if (_nav.MoveToFirstNamespace ())
  642. {
  643. _pos ++;
  644. // This clone cannot be omitted
  645. _current = _nav.Clone ();
  646. return true;
  647. }
  648. }
  649. else if (_nav.MoveToNextNamespace ())
  650. {
  651. _pos ++;
  652. // This clone cannot be omitted
  653. _current = _nav.Clone ();
  654. return true;
  655. }
  656. return false;
  657. }
  658. public override bool ReverseAxis { get { return true; } }
  659. public override bool RequireSorting { get { return false; } }
  660. }
  661. internal class AttributeIterator : SimpleIterator
  662. {
  663. public AttributeIterator (BaseIterator iter) : base (iter) {}
  664. protected AttributeIterator (AttributeIterator other) : base (other) {}
  665. public override XPathNodeIterator Clone () { return new AttributeIterator (this); }
  666. public override bool MoveNext ()
  667. {
  668. if (_pos == 0)
  669. {
  670. if (_nav.MoveToFirstAttribute ())
  671. {
  672. _pos += 1;
  673. // This clone cannot be omitted
  674. _current = _nav.Clone ();
  675. return true;
  676. }
  677. }
  678. else if (_nav.MoveToNextAttribute ())
  679. {
  680. _pos ++;
  681. // This clone cannot be omitted
  682. _current = _nav.Clone ();
  683. return true;
  684. }
  685. return false;
  686. }
  687. public override bool RequireSorting { get { return false; } }
  688. }
  689. internal class AxisIterator : BaseIterator
  690. {
  691. protected SimpleIterator _iter;
  692. protected NodeTest _test;
  693. protected int _pos;
  694. string name, ns;
  695. XPathNodeType matchType;
  696. public AxisIterator (SimpleIterator iter, NodeTest test) : base (iter)
  697. {
  698. _iter = iter;
  699. _test = test;
  700. test.GetInfo (out name, out ns, out matchType, NamespaceManager);
  701. // if (name != null)
  702. // name = Current.NameTable.Add (name);
  703. // if (ns != null)
  704. // ns = Current.NameTable.Add (ns);
  705. }
  706. protected AxisIterator (AxisIterator other) : base (other)
  707. {
  708. _iter = (SimpleIterator) other._iter.Clone ();
  709. _test = other._test;
  710. _pos = other._pos;
  711. name = other.name;
  712. ns = other.ns;
  713. matchType = other.matchType;
  714. }
  715. public override XPathNodeIterator Clone () { return new AxisIterator (this); }
  716. public override bool MoveNext ()
  717. {
  718. while (_iter.MoveNext ())
  719. {
  720. if (_test.Match (NamespaceManager, Current))
  721. {
  722. _pos ++;
  723. return true;
  724. }
  725. }
  726. return false;
  727. }
  728. public override XPathNavigator Current { get { return _iter.Current; }}
  729. public override int CurrentPosition { get { return _pos; }}
  730. bool Match ()
  731. {
  732. if (Current.NodeType != matchType && matchType != XPathNodeType.All)
  733. return false;
  734. if (ns == null)
  735. return name == null || (object)name == (object)Current.LocalName;
  736. else
  737. return (object)ns == (object)Current.NamespaceURI &&
  738. (name == null || (object)name == (object)Current.LocalName);
  739. }
  740. public override bool ReverseAxis {
  741. get { return _iter.ReverseAxis; }
  742. }
  743. public override bool RequireSorting { get { return _iter.RequireSorting; } }
  744. }
  745. internal class SlashIterator : BaseIterator
  746. {
  747. protected BaseIterator _iterLeft;
  748. protected BaseIterator _iterRight;
  749. protected NodeSet _expr;
  750. protected int _pos;
  751. ArrayList _navStore;
  752. SortedList _iterList;
  753. bool _finished;
  754. BaseIterator _nextIterRight;
  755. public SlashIterator (BaseIterator iter, NodeSet expr) : base (iter)
  756. {
  757. _iterLeft = iter;
  758. _expr = expr;
  759. }
  760. protected SlashIterator (SlashIterator other) : base (other)
  761. {
  762. _iterLeft = (BaseIterator) other._iterLeft.Clone ();
  763. if (other._iterRight != null)
  764. _iterRight = (BaseIterator) other._iterRight.Clone ();
  765. _expr = other._expr;
  766. _pos = other._pos;
  767. if (other._iterList != null)
  768. _iterList = (SortedList) other._iterList.Clone ();
  769. if (other._navStore != null)
  770. _navStore = (ArrayList) other._navStore.Clone ();
  771. _finished = other._finished;
  772. if (other._nextIterRight != null)
  773. _nextIterRight = (BaseIterator) other._nextIterRight.Clone ();
  774. }
  775. public override XPathNodeIterator Clone () { return new SlashIterator (this); }
  776. public override bool MoveNext ()
  777. {
  778. if (_finished)
  779. return false;
  780. if (RequireSorting) {
  781. if (_navStore == null) {
  782. CollectResults ();
  783. if (_navStore.Count == 0) {
  784. _finished = true;
  785. return false;
  786. }
  787. }
  788. _pos++;
  789. if (_navStore.Count < _pos) {
  790. _finished = true;
  791. _pos--;
  792. return false;
  793. }
  794. while (_navStore.Count > _pos) {
  795. if (((XPathNavigator) _navStore [_pos]).ComparePosition (
  796. (XPathNavigator) _navStore [_pos - 1]) == XmlNodeOrder.Same)
  797. _navStore.RemoveAt (_pos);
  798. else
  799. break;
  800. }
  801. return true;
  802. } else {
  803. if (_iterRight == null) { // First time
  804. if (!_iterLeft.MoveNext ())
  805. return false;
  806. _iterRight = _expr.EvaluateNodeSet (_iterLeft);
  807. _iterList = new SortedList (XPathIteratorComparer.Instance);
  808. }
  809. while (true) {
  810. while (!_iterRight.MoveNext ()) {
  811. if (_iterList.Count > 0) {
  812. int last = _iterList.Count - 1;
  813. BaseIterator tmpIter = (BaseIterator) _iterList.GetByIndex (last);
  814. _iterList.RemoveAt (last);
  815. switch (tmpIter.Current.ComparePosition (_iterRight.Current)) {
  816. case XmlNodeOrder.Same:
  817. case XmlNodeOrder.Before:
  818. _iterRight = tmpIter;
  819. continue;
  820. default:
  821. _iterRight = tmpIter;
  822. break;
  823. }
  824. break;
  825. } else if (_nextIterRight != null) {
  826. _iterRight = _nextIterRight;
  827. _nextIterRight = null;
  828. break;
  829. } else if (!_iterLeft.MoveNext ()) {
  830. _finished = true;
  831. return false;
  832. }
  833. else
  834. _iterRight = _expr.EvaluateNodeSet (_iterLeft);
  835. }
  836. bool loop = true;
  837. while (loop) {
  838. loop = false;
  839. if (_nextIterRight == null) {
  840. bool noMoreNext = false;
  841. while (_nextIterRight == null || !_nextIterRight.MoveNext ()) {
  842. if(_iterLeft.MoveNext ())
  843. _nextIterRight = _expr.EvaluateNodeSet (_iterLeft);
  844. else {
  845. noMoreNext = true;
  846. break;
  847. }
  848. }
  849. if (noMoreNext)
  850. _nextIterRight = null; // FIXME: More efficient code. Maybe making noMoreNext class scope would be better.
  851. }
  852. if (_nextIterRight != null) {
  853. switch (_iterRight.Current.ComparePosition (_nextIterRight.Current)) {
  854. case XmlNodeOrder.After:
  855. _iterList.Add (_iterList.Count, _iterRight);
  856. _iterRight = _nextIterRight;
  857. _nextIterRight = null;
  858. loop = true;
  859. break;
  860. case XmlNodeOrder.Same:
  861. if (!_nextIterRight.MoveNext ())
  862. _nextIterRight = null;
  863. else {
  864. int last = _iterList.Count;
  865. if (last > 0) {
  866. _iterList.Add (last, _nextIterRight);
  867. _nextIterRight = (BaseIterator) _iterList.GetByIndex (last);
  868. _iterList.RemoveAt (last);
  869. }
  870. }
  871. loop = true;
  872. break;
  873. }
  874. }
  875. }
  876. _pos ++;
  877. return true;
  878. }
  879. }
  880. }
  881. private void CollectResults ()
  882. {
  883. if (_navStore != null)
  884. return;
  885. _navStore = new ArrayList ();
  886. while (true) {
  887. while (_iterRight == null || !_iterRight.MoveNext ()) {
  888. if (!_iterLeft.MoveNext ()) {
  889. _navStore.Sort (XPathNavigatorComparer.Instance);
  890. return;
  891. }
  892. _iterRight = _expr.EvaluateNodeSet (_iterLeft);
  893. }
  894. XPathNavigator nav = _iterRight.Current;
  895. _navStore.Add (_needClone ? nav.Clone () : nav);
  896. }
  897. }
  898. public override XPathNavigator Current {
  899. get {
  900. if (_pos <= 0) return null;
  901. if (RequireSorting) {
  902. return (XPathNavigator) _navStore [_pos - 1];
  903. } else {
  904. return _iterRight.Current;
  905. }
  906. }
  907. }
  908. public override int CurrentPosition { get { return _pos; }}
  909. public override bool RequireSorting {
  910. get {
  911. return _iterLeft.RequireSorting || _expr.RequireSorting;
  912. }
  913. }
  914. public override int Count { get { return _navStore == null ? base.Count : _navStore.Count; } }
  915. }
  916. internal class PredicateIterator : BaseIterator
  917. {
  918. protected BaseIterator _iter;
  919. protected Expression _pred;
  920. protected int _pos;
  921. protected XPathResultType resType;
  922. public PredicateIterator (BaseIterator iter, Expression pred) : base (iter)
  923. {
  924. _iter = iter;
  925. _pred = pred;
  926. resType = pred.GetReturnType (iter);
  927. }
  928. protected PredicateIterator (PredicateIterator other) : base (other)
  929. {
  930. _iter = (BaseIterator) other._iter.Clone ();
  931. _pred = other._pred;
  932. _pos = other._pos;
  933. resType = other.resType;
  934. }
  935. public override XPathNodeIterator Clone () { return new PredicateIterator (this); }
  936. public override bool MoveNext ()
  937. {
  938. while (_iter.MoveNext ())
  939. {
  940. switch (resType) {
  941. case XPathResultType.Number:
  942. if (_pred.EvaluateNumber (_iter) != _iter.ComparablePosition)
  943. continue;
  944. break;
  945. case XPathResultType.Any: {
  946. object result = _pred.Evaluate (_iter);
  947. if (result is double)
  948. {
  949. if ((double) result != _iter.ComparablePosition)
  950. continue;
  951. }
  952. else if (!XPathFunctions.ToBoolean (result))
  953. continue;
  954. }
  955. break;
  956. default:
  957. if (!_pred.EvaluateBoolean (_iter))
  958. continue;
  959. break;
  960. }
  961. _pos ++;
  962. return true;
  963. }
  964. return false;
  965. }
  966. public override XPathNavigator Current { get { return _iter.Current; }}
  967. public override int CurrentPosition { get { return _pos; }}
  968. public override bool ReverseAxis {
  969. get { return _iter.ReverseAxis; }
  970. }
  971. public override bool RequireSorting { get { return true; } }
  972. }
  973. /*
  974. internal class EnumeratorIterator : BaseIterator
  975. {
  976. protected IEnumerator _enum;
  977. protected int _pos;
  978. bool _requireSorting;
  979. public EnumeratorIterator (BaseIterator iter, IEnumerator enumerator, bool requireSorting) : base (iter)
  980. {
  981. if (!(enumerator is ICloneable))
  982. throw new ArgumentException ("Target enumerator must be cloneable.");
  983. _enum = enumerator;
  984. _requireSorting = requireSorting;
  985. }
  986. public EnumeratorIterator (IEnumerator enumerator, XmlNamespaceManager nsm, bool requireSorting) : base (nsm)
  987. {
  988. if (!(enumerator is ICloneable))
  989. throw new ArgumentException ("Target enumerator must be cloneable.");
  990. _enum = enumerator;
  991. _requireSorting = requireSorting;
  992. }
  993. protected EnumeratorIterator (EnumeratorIterator other) : base (other)
  994. {
  995. ICloneable enumClone = other._enum as ICloneable;
  996. _enum = (IEnumerator) enumClone.Clone ();
  997. _pos = other._pos;
  998. _requireSorting = other._requireSorting;
  999. }
  1000. public override XPathNodeIterator Clone () { return new EnumeratorIterator (this); }
  1001. public override bool MoveNext ()
  1002. {
  1003. if (!_enum.MoveNext ())
  1004. return false;
  1005. _pos++;
  1006. return true;
  1007. }
  1008. public override XPathNavigator Current { get { return (XPathNavigator) _enum.Current; }}
  1009. public override int CurrentPosition { get { return _pos; }}
  1010. public override bool RequireSorting { get { return _requireSorting; } }
  1011. }
  1012. */
  1013. internal class ListIterator : BaseIterator
  1014. {
  1015. protected IList _list;
  1016. protected int _pos;
  1017. bool _requireSorting;
  1018. public ListIterator (BaseIterator iter, IList list, bool requireSorting) : base (iter)
  1019. {
  1020. if (!(list is ICloneable))
  1021. throw new ArgumentException ("Target enumerator must be cloneable.");
  1022. _list = list;
  1023. _requireSorting = requireSorting;
  1024. }
  1025. public ListIterator (IList list, XmlNamespaceManager nsm, bool requireSorting) : base (nsm)
  1026. {
  1027. if (!(list is ICloneable))
  1028. throw new ArgumentException ("Target enumerator must be cloneable.");
  1029. _list = list;
  1030. _requireSorting = requireSorting;
  1031. }
  1032. protected ListIterator (ListIterator other) : base (other)
  1033. {
  1034. ICloneable listClone = other._list as ICloneable;
  1035. _list = (IList) listClone.Clone ();
  1036. _pos = other._pos;
  1037. _requireSorting = other._requireSorting;
  1038. }
  1039. public override XPathNodeIterator Clone () { return new ListIterator (this); }
  1040. public override bool MoveNext ()
  1041. {
  1042. if (_pos >= _list.Count)
  1043. return false;
  1044. _pos++;
  1045. return true;
  1046. }
  1047. public override XPathNavigator Current {
  1048. get {
  1049. if (_list.Count == 0)
  1050. return null;
  1051. return (XPathNavigator) _list [_pos - 1];
  1052. }
  1053. }
  1054. public override int CurrentPosition { get { return _pos; }}
  1055. public override bool RequireSorting { get { return _requireSorting; } }
  1056. public override int Count { get { return _list.Count; } }
  1057. }
  1058. internal class UnionIterator : BaseIterator
  1059. {
  1060. protected BaseIterator _left, _right;
  1061. private int _pos;
  1062. private bool keepLeft;
  1063. private bool keepRight;
  1064. private bool useRight;
  1065. public UnionIterator (BaseIterator iter, BaseIterator left, BaseIterator right) : base (iter)
  1066. {
  1067. _left = left;
  1068. _right = right;
  1069. }
  1070. protected UnionIterator (UnionIterator other) : base (other)
  1071. {
  1072. _left = (BaseIterator) other._left.Clone ();
  1073. _right = (BaseIterator) other._right.Clone ();
  1074. _pos = other._pos;
  1075. keepLeft = other.keepLeft;
  1076. keepRight = other.keepRight;
  1077. useRight = other.useRight;
  1078. }
  1079. public override XPathNodeIterator Clone () { return new UnionIterator (this); }
  1080. public override bool MoveNext ()
  1081. {
  1082. if (!keepLeft)
  1083. keepLeft = _left.MoveNext ();
  1084. if (!keepRight)
  1085. keepRight = _right.MoveNext ();
  1086. if (!keepLeft && !keepRight)
  1087. return false;
  1088. _pos ++;
  1089. if (!keepRight) {
  1090. keepLeft = useRight = false;
  1091. return true;
  1092. } else if (!keepLeft) {
  1093. keepRight = false;
  1094. useRight = true;
  1095. return true;
  1096. }
  1097. switch (_left.Current.ComparePosition (_right.Current)) {
  1098. case XmlNodeOrder.Same:
  1099. // consume both. i.e. don't output duplicate result.
  1100. keepLeft = keepRight = false;
  1101. useRight = true;
  1102. return true;
  1103. case XmlNodeOrder.Before:
  1104. case XmlNodeOrder.Unknown: // Maybe happen because of "document(a) | document(b)"
  1105. keepLeft = useRight = false;
  1106. return true;
  1107. case XmlNodeOrder.After:
  1108. keepRight = false;
  1109. useRight = true;
  1110. return true;
  1111. default:
  1112. throw new InvalidOperationException ("Should not happen.");
  1113. }
  1114. }
  1115. public override XPathNavigator Current
  1116. {
  1117. get
  1118. {
  1119. if (_pos == 0)
  1120. return null;
  1121. if (useRight)
  1122. return _right.Current;
  1123. else
  1124. return _left.Current;
  1125. }
  1126. }
  1127. public override int CurrentPosition { get { return _pos; }}
  1128. public override bool RequireSorting { get { return _left.RequireSorting || _right.RequireSorting; } }
  1129. }
  1130. }