Iterator.cs 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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. switch (resType) {
  935. case XPathResultType.Number:
  936. if (_pred.EvaluateNumber (_iter) != _iter.ComparablePosition)
  937. continue;
  938. break;
  939. case XPathResultType.Any: {
  940. object result = _pred.Evaluate (_iter);
  941. if (result is double)
  942. {
  943. if ((double) result != _iter.ComparablePosition)
  944. continue;
  945. }
  946. else if (!XPathFunctions.ToBoolean (result))
  947. continue;
  948. }
  949. break;
  950. default:
  951. if (!_pred.EvaluateBoolean (_iter))
  952. continue;
  953. break;
  954. }
  955. _pos ++;
  956. return true;
  957. }
  958. return false;
  959. }
  960. public override XPathNavigator Current { get { return _iter.Current; }}
  961. public override int CurrentPosition { get { return _pos; }}
  962. public override bool ReverseAxis {
  963. get { return _iter.ReverseAxis; }
  964. }
  965. public override bool RequireSorting { get { return true; } }
  966. }
  967. // WARNING: Before using be sure about IEnumerator argument, because
  968. internal class EnumeratorIterator : BaseIterator
  969. {
  970. protected IEnumerator _enum;
  971. protected int _pos;
  972. public EnumeratorIterator (BaseIterator iter, IEnumerator enumerator) : base (iter)
  973. {
  974. if (!(enumerator is ICloneable))
  975. throw new ArgumentException ("Target enumerator must be cloneable.");
  976. _enum = enumerator;
  977. }
  978. public EnumeratorIterator (IEnumerator enumerator, XmlNamespaceManager nsm) : base (nsm)
  979. {
  980. if (!(enumerator is ICloneable))
  981. throw new ArgumentException ("Target enumerator must be cloneable.");
  982. _enum = enumerator;
  983. }
  984. protected EnumeratorIterator (EnumeratorIterator other) : base (other)
  985. {
  986. ICloneable enumClone = other._enum as ICloneable;
  987. _enum = (IEnumerator) enumClone.Clone ();
  988. _pos = other._pos;
  989. }
  990. public override XPathNodeIterator Clone () { return new EnumeratorIterator (this); }
  991. public override bool MoveNext ()
  992. {
  993. if (!_enum.MoveNext ())
  994. return false;
  995. _pos++;
  996. return true;
  997. }
  998. public override XPathNavigator Current { get { return (XPathNavigator) _enum.Current; }}
  999. public override int CurrentPosition { get { return _pos; }}
  1000. public override bool RequireSorting { get { return true; } }
  1001. }
  1002. internal class UnionIterator : BaseIterator
  1003. {
  1004. protected BaseIterator _left, _right;
  1005. private int _pos;
  1006. private bool keepLeft;
  1007. private bool keepRight;
  1008. private bool useRight;
  1009. public UnionIterator (BaseIterator iter, BaseIterator left, BaseIterator right) : base (iter)
  1010. {
  1011. _left = left;
  1012. _right = right;
  1013. }
  1014. protected UnionIterator (UnionIterator other) : base (other)
  1015. {
  1016. _left = (BaseIterator) other._left.Clone ();
  1017. _right = (BaseIterator) other._right.Clone ();
  1018. _pos = other._pos;
  1019. keepLeft = other.keepLeft;
  1020. keepRight = other.keepRight;
  1021. useRight = other.useRight;
  1022. }
  1023. public override XPathNodeIterator Clone () { return new UnionIterator (this); }
  1024. public override bool MoveNext ()
  1025. {
  1026. if (!keepLeft)
  1027. keepLeft = _left.MoveNext ();
  1028. if (!keepRight)
  1029. keepRight = _right.MoveNext ();
  1030. if (!keepLeft && !keepRight)
  1031. return false;
  1032. _pos ++;
  1033. if (!keepRight) {
  1034. keepLeft = useRight = false;
  1035. return true;
  1036. } else if (!keepLeft) {
  1037. keepRight = false;
  1038. useRight = true;
  1039. return true;
  1040. }
  1041. switch (_left.Current.ComparePosition (_right.Current)) {
  1042. case XmlNodeOrder.Same:
  1043. // consume both. i.e. don't output duplicate result.
  1044. keepLeft = keepRight = false;
  1045. useRight = true;
  1046. return true;
  1047. case XmlNodeOrder.Before:
  1048. case XmlNodeOrder.Unknown: // Maybe happen because of "document(a) | document(b)"
  1049. keepLeft = useRight = false;
  1050. return true;
  1051. case XmlNodeOrder.After:
  1052. keepRight = false;
  1053. useRight = true;
  1054. return true;
  1055. default:
  1056. throw new InvalidOperationException ("Should not happen.");
  1057. }
  1058. }
  1059. public override XPathNavigator Current
  1060. {
  1061. get
  1062. {
  1063. if (_pos == 0)
  1064. return null;
  1065. if (useRight)
  1066. return _right.Current;
  1067. else
  1068. return _left.Current;
  1069. }
  1070. }
  1071. public override int CurrentPosition { get { return _pos; }}
  1072. public override bool RequireSorting { get { return _left.RequireSorting || _right.RequireSorting; } }
  1073. }
  1074. }