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