Iterator.cs 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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.MoveTo (_nav);
  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.MoveTo (_nav);
  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.MoveTo (_nav);
  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.MoveTo (_nav);
  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.MoveTo (_nav);
  358. return true;
  359. }
  360. }
  361. if (currentPosition == 0)
  362. return false;
  363. _nav.MoveTo ((XPathNavigator) navigators [--currentPosition]);
  364. Current.MoveTo (_nav);
  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.MoveTo (_nav);
  420. return true;
  421. }
  422. }
  423. if (currentPosition == -1)
  424. return false;
  425. if (currentPosition-- == 0) {
  426. _nav.MoveTo (startPosition);
  427. Current.MoveTo (_nav);
  428. return true; // returns self.
  429. }
  430. _nav.MoveTo ((XPathNavigator) navigators [currentPosition]);
  431. Current.MoveTo (_nav);
  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.MoveTo (_nav);
  465. return true;
  466. }
  467. while (_depth != 0)
  468. {
  469. if (_nav.MoveToNext ())
  470. {
  471. Current.MoveTo (_nav);
  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.MoveTo (_nav);
  501. return true;
  502. }
  503. if (_nav.MoveToFirstChild ())
  504. {
  505. _depth ++;
  506. Current.MoveTo (_nav);
  507. return true;
  508. }
  509. while (_depth != 0)
  510. {
  511. if (_nav.MoveToNext ())
  512. {
  513. Current.MoveTo (_nav);
  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. bool checkChildren = true;
  536. if (CurrentPosition == 0)
  537. {
  538. checkChildren = false;
  539. switch (_nav.NodeType) {
  540. case XPathNodeType.Attribute:
  541. case XPathNodeType.Namespace:
  542. _nav.MoveToParent ();
  543. checkChildren = true;
  544. break;
  545. default:
  546. if (_nav.MoveToNext ())
  547. {
  548. Current.MoveTo (_nav);
  549. return true;
  550. } else {
  551. while (_nav.MoveToParent ()) {
  552. if (_nav.MoveToNext ()) {
  553. Current.MoveTo (_nav);
  554. return true;
  555. }
  556. }
  557. }
  558. break;
  559. }
  560. }
  561. if (checkChildren)
  562. {
  563. if (_nav.MoveToFirstChild ())
  564. {
  565. Current.MoveTo (_nav);
  566. return true;
  567. }
  568. do
  569. {
  570. if (_nav.MoveToNext ())
  571. {
  572. Current.MoveTo (_nav);
  573. return true;
  574. }
  575. }
  576. while (_nav.MoveToParent ());
  577. }
  578. _finished = true;
  579. return false;
  580. }
  581. public override bool RequireSorting { get { return false; } }
  582. }
  583. internal class PrecedingIterator : SimpleIterator
  584. {
  585. bool finished;
  586. bool started;
  587. XPathNavigator startPosition;
  588. public PrecedingIterator (BaseIterator iter) : base (iter)
  589. {
  590. startPosition = iter.Current.Clone ();
  591. }
  592. private PrecedingIterator (PrecedingIterator other) : base (other, true)
  593. {
  594. startPosition = other.startPosition;
  595. started = other.started;
  596. finished = other.finished;
  597. }
  598. public override XPathNodeIterator Clone () { return new PrecedingIterator (this); }
  599. public override bool MoveNextCore ()
  600. {
  601. if (finished)
  602. return false;
  603. if (!started) {
  604. started = true;
  605. _nav.MoveToRoot ();
  606. }
  607. bool loop = true;
  608. while (loop) {
  609. while (!_nav.MoveToFirstChild ()) {
  610. while (!_nav.MoveToNext ()) {
  611. if (!_nav.MoveToParent ()) { // Should not finish, at least before startPosition.
  612. finished = true;
  613. return false;
  614. }
  615. }
  616. break;
  617. }
  618. if (_nav.IsDescendant (startPosition))
  619. continue;
  620. loop = false;
  621. break;
  622. }
  623. if (_nav.ComparePosition (startPosition) != XmlNodeOrder.Before) {
  624. // Note that if _nav contains only 1 node, it won't be Same.
  625. finished = true;
  626. return false;
  627. } else {
  628. Current.MoveTo (_nav);
  629. return true;
  630. }
  631. }
  632. public override bool ReverseAxis {
  633. get { return true; }
  634. }
  635. public override bool RequireSorting { get { return true; } }
  636. }
  637. internal class NamespaceIterator : SimpleIterator
  638. {
  639. public NamespaceIterator (BaseIterator iter) : base (iter) {}
  640. private NamespaceIterator (NamespaceIterator other) : base (other, true) {}
  641. public override XPathNodeIterator Clone () { return new NamespaceIterator (this); }
  642. public override bool MoveNextCore ()
  643. {
  644. if (CurrentPosition == 0)
  645. {
  646. if (_nav.MoveToFirstNamespace ())
  647. {
  648. Current.MoveTo (_nav);
  649. return true;
  650. }
  651. }
  652. else if (_nav.MoveToNextNamespace ())
  653. {
  654. Current.MoveTo (_nav);
  655. return true;
  656. }
  657. return false;
  658. }
  659. public override bool ReverseAxis { get { return true; } }
  660. public override bool RequireSorting { get { return false; } }
  661. }
  662. internal class AttributeIterator : SimpleIterator
  663. {
  664. public AttributeIterator (BaseIterator iter) : base (iter) {}
  665. private AttributeIterator (AttributeIterator other) : base (other, true) {}
  666. public override XPathNodeIterator Clone () { return new AttributeIterator (this); }
  667. public override bool MoveNextCore ()
  668. {
  669. if (CurrentPosition == 0)
  670. {
  671. if (_nav.MoveToFirstAttribute ())
  672. {
  673. Current.MoveTo (_nav);
  674. return true;
  675. }
  676. }
  677. else if (_nav.MoveToNextAttribute ())
  678. {
  679. Current.MoveTo (_nav);
  680. return true;
  681. }
  682. return false;
  683. }
  684. public override bool RequireSorting { get { return true; } }
  685. }
  686. internal class AxisIterator : BaseIterator
  687. {
  688. private SimpleIterator _iter;
  689. private NodeTest _test;
  690. string name, ns;
  691. XPathNodeType matchType;
  692. public AxisIterator (SimpleIterator iter, NodeTest test) : base (iter.NamespaceManager)
  693. {
  694. _iter = iter;
  695. _test = test;
  696. test.GetInfo (out name, out ns, out matchType, NamespaceManager);
  697. // if (name != null)
  698. // name = Current.NameTable.Add (name);
  699. // if (ns != null)
  700. // ns = Current.NameTable.Add (ns);
  701. }
  702. private AxisIterator (AxisIterator other) : base (other)
  703. {
  704. _iter = (SimpleIterator) other._iter.Clone ();
  705. _test = other._test;
  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 MoveNextCore ()
  712. {
  713. while (_iter.MoveNext ())
  714. {
  715. if (_test.Match (NamespaceManager, Current))
  716. {
  717. return true;
  718. }
  719. }
  720. return false;
  721. }
  722. public override XPathNavigator Current { get { return _iter.Current; }}
  723. bool Match ()
  724. {
  725. if (Current.NodeType != matchType && matchType != XPathNodeType.All)
  726. return false;
  727. if (ns == null)
  728. return name == null || (object)name == (object)Current.LocalName;
  729. else
  730. return (object)ns == (object)Current.NamespaceURI &&
  731. (name == null || (object)name == (object)Current.LocalName);
  732. }
  733. public override bool ReverseAxis {
  734. get { return _iter.ReverseAxis; }
  735. }
  736. public override bool RequireSorting { get { return _iter.RequireSorting; } }
  737. }
  738. internal class SlashIterator : BaseIterator
  739. {
  740. private BaseIterator _iterLeft;
  741. private BaseIterator _iterRight;
  742. private NodeSet _expr;
  743. ArrayList _navStore;
  744. SortedList _iterList;
  745. bool _finished;
  746. BaseIterator _nextIterRight;
  747. public SlashIterator (BaseIterator iter, NodeSet expr, bool requireSorting) : base (iter.NamespaceManager)
  748. {
  749. _iterLeft = iter;
  750. _expr = expr;
  751. if (requireSorting)
  752. CollectResults ();
  753. }
  754. private SlashIterator (SlashIterator other) : base (other)
  755. {
  756. _iterLeft = (BaseIterator) other._iterLeft.Clone ();
  757. if (other._iterRight != null)
  758. _iterRight = (BaseIterator) other._iterRight.Clone ();
  759. _expr = other._expr;
  760. if (other._iterList != null)
  761. _iterList = (SortedList) other._iterList.Clone ();
  762. if (other._navStore != null)
  763. _navStore = (ArrayList) other._navStore.Clone ();
  764. _finished = other._finished;
  765. if (other._nextIterRight != null)
  766. _nextIterRight = (BaseIterator) other._nextIterRight.Clone ();
  767. }
  768. public override XPathNodeIterator Clone () { return new SlashIterator (this); }
  769. public override bool MoveNextCore ()
  770. {
  771. if (_finished)
  772. return false;
  773. if (_navStore != null) {
  774. // Which requires sorting::
  775. if (_navStore.Count < CurrentPosition + 1) {
  776. _finished = true;
  777. return false;
  778. }
  779. while (_navStore.Count > CurrentPosition + 1) {
  780. if (((XPathNavigator) _navStore [CurrentPosition + 1]).IsSamePosition (
  781. (XPathNavigator) _navStore [CurrentPosition]))
  782. _navStore.RemoveAt (CurrentPosition + 1);
  783. else
  784. break;
  785. }
  786. return true;
  787. }
  788. // Which does not require sorting::
  789. if (_iterRight == null) { // First time
  790. if (!_iterLeft.MoveNext ())
  791. return false;
  792. _iterRight = _expr.EvaluateNodeSet (_iterLeft);
  793. _iterList = new SortedList (XPathIteratorComparer.Instance);
  794. }
  795. while (true) {
  796. while (!_iterRight.MoveNext ()) {
  797. if (_iterList.Count > 0) {
  798. int last = _iterList.Count - 1;
  799. _iterRight = (BaseIterator) _iterList.GetByIndex (last);
  800. _iterList.RemoveAt (last);
  801. break;
  802. } else if (_nextIterRight != null) {
  803. _iterRight = _nextIterRight;
  804. _nextIterRight = null;
  805. break;
  806. } else if (!_iterLeft.MoveNext ()) {
  807. _finished = true;
  808. return false;
  809. }
  810. else
  811. _iterRight = _expr.EvaluateNodeSet (_iterLeft);
  812. }
  813. bool loop = true;
  814. while (loop) {
  815. loop = false;
  816. if (_nextIterRight == null) {
  817. bool noMoreNext = false;
  818. while (_nextIterRight == null || !_nextIterRight.MoveNext ()) {
  819. if(_iterLeft.MoveNext ())
  820. _nextIterRight = _expr.EvaluateNodeSet (_iterLeft);
  821. else {
  822. noMoreNext = true;
  823. break;
  824. }
  825. }
  826. if (noMoreNext)
  827. _nextIterRight = null; // FIXME: More efficient code. Maybe making noMoreNext class scope would be better.
  828. }
  829. if (_nextIterRight != null) {
  830. switch (_iterRight.Current.ComparePosition (_nextIterRight.Current)) {
  831. case XmlNodeOrder.After:
  832. _iterList [_iterRight] = _iterRight;
  833. _iterRight = _nextIterRight;
  834. _nextIterRight = null;
  835. loop = true;
  836. break;
  837. case XmlNodeOrder.Same:
  838. if (!_nextIterRight.MoveNext ())
  839. _nextIterRight = null;
  840. else {
  841. int last = _iterList.Count;
  842. _iterList [_nextIterRight] = _nextIterRight;
  843. if (last != _iterList.Count) {
  844. _nextIterRight = (BaseIterator) _iterList.GetByIndex (last);
  845. _iterList.RemoveAt (last);
  846. }
  847. }
  848. loop = true;
  849. break;
  850. }
  851. }
  852. }
  853. return true;
  854. }
  855. }
  856. private void CollectResults ()
  857. {
  858. _navStore = new ArrayList ();
  859. while (true) {
  860. while (_iterRight == null || !_iterRight.MoveNext ()) {
  861. if (!_iterLeft.MoveNext ()) {
  862. _navStore.Sort (XPathNavigatorComparer.Instance);
  863. return;
  864. }
  865. _iterRight = _expr.EvaluateNodeSet (_iterLeft);
  866. }
  867. XPathNavigator nav = _iterRight.Current;
  868. _navStore.Add (nav.Clone ());
  869. }
  870. }
  871. public override XPathNavigator Current {
  872. get {
  873. if (CurrentPosition <= 0) return null;
  874. if (_navStore != null) {
  875. return (XPathNavigator) _navStore [CurrentPosition - 1];
  876. } else {
  877. return _iterRight.Current;
  878. }
  879. }
  880. }
  881. public override bool RequireSorting {
  882. // It always does not need to be sorted.
  883. get { return false; }
  884. }
  885. public override int Count { get { return _navStore == null ? base.Count : _navStore.Count; } }
  886. }
  887. internal class PredicateIterator : BaseIterator
  888. {
  889. private BaseIterator _iter;
  890. private Expression _pred;
  891. private XPathResultType resType;
  892. private bool finished;
  893. public PredicateIterator (BaseIterator iter, Expression pred) : base (iter.NamespaceManager)
  894. {
  895. _iter = iter;
  896. _pred = pred;
  897. resType = pred.GetReturnType (iter);
  898. }
  899. private PredicateIterator (PredicateIterator other) : base (other)
  900. {
  901. _iter = (BaseIterator) other._iter.Clone ();
  902. _pred = other._pred;
  903. resType = other.resType;
  904. finished = other.finished;
  905. }
  906. public override XPathNodeIterator Clone () { return new PredicateIterator (this); }
  907. public override bool MoveNextCore ()
  908. {
  909. if (finished)
  910. return false;
  911. while (_iter.MoveNext ())
  912. {
  913. switch (resType) {
  914. case XPathResultType.Number:
  915. if (_pred.EvaluateNumber (_iter) != _iter.ComparablePosition)
  916. continue;
  917. finished = true;
  918. break;
  919. case XPathResultType.Any: {
  920. object result = _pred.Evaluate (_iter);
  921. if (result is double)
  922. {
  923. if ((double) result != _iter.ComparablePosition)
  924. continue;
  925. finished = true;
  926. }
  927. else if (!XPathFunctions.ToBoolean (result))
  928. continue;
  929. }
  930. break;
  931. default:
  932. if (!_pred.EvaluateBoolean (_iter))
  933. continue;
  934. break;
  935. }
  936. return true;
  937. }
  938. return false;
  939. }
  940. public override XPathNavigator Current { get { return _iter.Current; }}
  941. public override bool ReverseAxis {
  942. get { return _iter.ReverseAxis; }
  943. }
  944. public override bool RequireSorting { get { return _iter.RequireSorting || _pred.RequireSorting; } }
  945. }
  946. internal class ListIterator : BaseIterator
  947. {
  948. private IList _list;
  949. bool _requireSorting;
  950. public ListIterator (BaseIterator iter, IList list, bool requireSorting) : base (iter.NamespaceManager)
  951. {
  952. if (!(list is ICloneable))
  953. throw new ArgumentException ("Target enumerator must be cloneable.");
  954. _list = list;
  955. _requireSorting = requireSorting;
  956. }
  957. public ListIterator (IList list, NSResolver nsm, bool requireSorting) : base (nsm)
  958. {
  959. if (!(list is ICloneable))
  960. throw new ArgumentException ("Target enumerator must be cloneable.");
  961. _list = list;
  962. _requireSorting = requireSorting;
  963. }
  964. private ListIterator (ListIterator other) : base (other)
  965. {
  966. ICloneable listClone = other._list as ICloneable;
  967. _list = (IList) listClone.Clone ();
  968. _requireSorting = other._requireSorting;
  969. }
  970. public override XPathNodeIterator Clone () { return new ListIterator (this); }
  971. public override bool MoveNextCore ()
  972. {
  973. if (CurrentPosition >= _list.Count)
  974. return false;
  975. return true;
  976. }
  977. public override XPathNavigator Current {
  978. get {
  979. if (_list.Count == 0)
  980. return null;
  981. return (XPathNavigator) _list [CurrentPosition - 1];
  982. }
  983. }
  984. public override bool RequireSorting { get { return _requireSorting; } }
  985. public override int Count { get { return _list.Count; } }
  986. }
  987. internal class UnionIterator : BaseIterator
  988. {
  989. private BaseIterator _left, _right;
  990. private bool keepLeft;
  991. private bool keepRight;
  992. XPathNavigator _current;
  993. public UnionIterator (BaseIterator iter, BaseIterator left, BaseIterator right) : base (iter.NamespaceManager)
  994. {
  995. _left = left;
  996. _right = right;
  997. }
  998. private UnionIterator (UnionIterator other) : base (other)
  999. {
  1000. _left = (BaseIterator) other._left.Clone ();
  1001. _right = (BaseIterator) other._right.Clone ();
  1002. keepLeft = other.keepLeft;
  1003. keepRight = other.keepRight;
  1004. if (other._current != null)
  1005. _current = other._current.Clone ();
  1006. }
  1007. public override XPathNodeIterator Clone () { return new UnionIterator (this); }
  1008. public override bool MoveNextCore ()
  1009. {
  1010. if (!keepLeft)
  1011. keepLeft = _left.MoveNext ();
  1012. if (!keepRight)
  1013. keepRight = _right.MoveNext ();
  1014. if (!keepLeft && !keepRight)
  1015. return false;
  1016. if (!keepRight) {
  1017. keepLeft = false;
  1018. SetCurrent (_left);
  1019. return true;
  1020. } else if (!keepLeft) {
  1021. keepRight = false;
  1022. SetCurrent (_right);
  1023. return true;
  1024. }
  1025. switch (_left.Current.ComparePosition (_right.Current)) {
  1026. case XmlNodeOrder.Same:
  1027. // consume both. i.e. don't output duplicate result.
  1028. keepLeft = keepRight = false;
  1029. SetCurrent (_right);
  1030. return true;
  1031. case XmlNodeOrder.Before:
  1032. case XmlNodeOrder.Unknown: // Maybe happen because of "document(a) | document(b)"
  1033. keepLeft = false;
  1034. SetCurrent (_left);
  1035. return true;
  1036. case XmlNodeOrder.After:
  1037. keepRight = false;
  1038. SetCurrent (_right);
  1039. return true;
  1040. default:
  1041. throw new InvalidOperationException ("Should not happen.");
  1042. }
  1043. }
  1044. private void SetCurrent (XPathNodeIterator iter)
  1045. {
  1046. if (_current == null)
  1047. _current = iter.Current.Clone ();
  1048. else
  1049. _current.MoveTo (iter.Current);
  1050. }
  1051. public override XPathNavigator Current
  1052. {
  1053. get { return _current; }
  1054. }
  1055. public override bool RequireSorting { get { return _left.RequireSorting || _right.RequireSorting; } }
  1056. }
  1057. }