Iterator.cs 29 KB

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