Iterator.cs 31 KB

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