Iterator.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. //
  2. // System.Xml.XPath.BaseIterator
  3. //
  4. // Author:
  5. // Piers Haken ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C) 2002 Piers Haken
  9. // (C) 2003 Atsushi Enomoto
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Xml;
  34. using System.Xml.XPath;
  35. using System.Xml.Xsl;
  36. #if NET_2_0
  37. using NSResolver = System.Xml.IXmlNamespaceResolver;
  38. #else
  39. using NSResolver = System.Xml.XmlNamespaceManager;
  40. #endif
  41. namespace System.Xml.XPath
  42. {
  43. internal abstract class BaseIterator : XPathNodeIterator
  44. {
  45. NSResolver _nsm;
  46. int position;
  47. internal BaseIterator (BaseIterator other)
  48. {
  49. _nsm = other._nsm;
  50. position = other.position;
  51. }
  52. internal BaseIterator (NSResolver nsm)
  53. {
  54. _nsm = nsm;
  55. }
  56. public NSResolver NamespaceManager
  57. {
  58. get { return _nsm; }
  59. set { _nsm = value; }
  60. }
  61. public virtual bool ReverseAxis {
  62. get { return false; }
  63. }
  64. public abstract bool RequireSorting { get; }
  65. public virtual int ComparablePosition {
  66. get {
  67. if (ReverseAxis) {
  68. int diff = Count - CurrentPosition + 1;
  69. return diff < 1 ? 1 : diff;
  70. }
  71. else
  72. return CurrentPosition;
  73. }
  74. }
  75. public override int CurrentPosition {
  76. get { return position; }
  77. }
  78. public override bool MoveNext ()
  79. {
  80. if (!MoveNextCore ())
  81. return false;
  82. position++;
  83. return true;
  84. }
  85. public abstract bool MoveNextCore ();
  86. public override string ToString ()
  87. {
  88. if (Current != null)
  89. return Current.NodeType.ToString () + "[" + CurrentPosition + "] : " + Current.Name + " = " + Current.Value;
  90. else
  91. return this.GetType().ToString () + "[" + CurrentPosition + "]";
  92. }
  93. }
  94. internal class WrapperIterator : BaseIterator
  95. {
  96. XPathNodeIterator iter;
  97. public WrapperIterator (XPathNodeIterator iter, NSResolver nsm)
  98. : base (nsm)
  99. {
  100. this.iter = iter;
  101. }
  102. private WrapperIterator (WrapperIterator other)
  103. : base (other)
  104. {
  105. iter = other.iter.Clone ();
  106. }
  107. public override XPathNodeIterator Clone ()
  108. {
  109. return new WrapperIterator (this);
  110. }
  111. public override bool MoveNextCore ()
  112. {
  113. return iter.MoveNext ();
  114. }
  115. public override XPathNavigator Current {
  116. get { return iter.Current; }
  117. }
  118. public override bool RequireSorting {
  119. get { return true; }
  120. }
  121. }
  122. internal abstract class SimpleIterator : BaseIterator
  123. {
  124. protected readonly BaseIterator _iter;
  125. protected readonly XPathNavigator _nav;
  126. protected XPathNavigator _current;
  127. public SimpleIterator (BaseIterator iter) : base (iter.NamespaceManager)
  128. {
  129. _iter = iter;
  130. _nav = iter.Current.Clone ();
  131. }
  132. protected SimpleIterator (SimpleIterator other, bool clone) : base (other)
  133. {
  134. if (other._nav == null)
  135. _iter = (BaseIterator) other._iter.Clone ();
  136. else
  137. _nav = other._nav.Clone ();
  138. }
  139. public SimpleIterator (XPathNavigator nav, NSResolver nsm) : base (nsm)
  140. {
  141. _nav = nav.Clone ();
  142. }
  143. public override XPathNavigator Current {
  144. get {
  145. if (_current == null) // position == 0
  146. _current = _nav.Clone ();
  147. return _current;
  148. }
  149. }
  150. }
  151. internal class SelfIterator : SimpleIterator
  152. {
  153. public SelfIterator (BaseIterator iter) : base (iter) {}
  154. public SelfIterator (XPathNavigator nav, NSResolver nsm) : base (nav, nsm) {}
  155. protected SelfIterator (SelfIterator other, bool clone) : base (other, true)
  156. {
  157. if (other._current != null)
  158. _current = _nav;
  159. }
  160. public override XPathNodeIterator Clone () { return new SelfIterator (this, true); }
  161. public override bool MoveNextCore ()
  162. {
  163. if (CurrentPosition == 0)
  164. {
  165. _current = _nav;
  166. return true;
  167. }
  168. return false;
  169. }
  170. public override bool RequireSorting { get { return false; } }
  171. }
  172. internal class NullIterator : SelfIterator
  173. {
  174. public NullIterator (BaseIterator iter) : base (iter) {}
  175. public NullIterator (XPathNavigator nav) : this (nav, null) {}
  176. public NullIterator (XPathNavigator nav, NSResolver nsm) : base (nav, nsm) {}
  177. private NullIterator (NullIterator other) : base (other, true) {}
  178. public override XPathNodeIterator Clone () { return new NullIterator (this); }
  179. public override bool MoveNextCore ()
  180. {
  181. return false;
  182. }
  183. }
  184. internal class ParensIterator : BaseIterator
  185. {
  186. BaseIterator _iter;
  187. public ParensIterator (BaseIterator iter) : base (iter.NamespaceManager)
  188. {
  189. _iter = iter;
  190. }
  191. private ParensIterator (ParensIterator other) : base (other)
  192. {
  193. _iter = (BaseIterator) other._iter.Clone ();
  194. }
  195. public override XPathNodeIterator Clone () { return new ParensIterator (this); }
  196. public override bool MoveNextCore ()
  197. {
  198. return _iter.MoveNext ();
  199. }
  200. public override XPathNavigator Current { get { return _iter.Current; }}
  201. public override bool RequireSorting { get { return _iter.RequireSorting; } }
  202. public override int Count { get { return _iter.Count; } }
  203. }
  204. internal class ParentIterator : SimpleIterator
  205. {
  206. public ParentIterator (BaseIterator iter) : base (iter) {}
  207. private ParentIterator (ParentIterator other) : base (other, true)
  208. {
  209. if (other._current != null)
  210. _current = _nav;
  211. }
  212. public ParentIterator (XPathNavigator nav, NSResolver nsm) : base (nav, nsm) {}
  213. public override XPathNodeIterator Clone () { return new ParentIterator (this); }
  214. public override bool MoveNextCore ()
  215. {
  216. if (CurrentPosition == 0 && _nav.MoveToParent ())
  217. {
  218. _current = _nav;
  219. return true;
  220. }
  221. return false;
  222. }
  223. public override bool ReverseAxis { get { return true; } }
  224. public override bool RequireSorting { get { return false; } }
  225. }
  226. internal class ChildIterator : SimpleIterator
  227. {
  228. public ChildIterator (BaseIterator iter) : base (iter) {}
  229. private ChildIterator (ChildIterator other) : base (other, true) {}
  230. public override XPathNodeIterator Clone () { return new ChildIterator (this); }
  231. public override bool MoveNextCore ()
  232. {
  233. bool fSuccess = (CurrentPosition == 0) ? _nav.MoveToFirstChild () : _nav.MoveToNext ();
  234. if (fSuccess) {
  235. _current = null;
  236. }
  237. return fSuccess;
  238. }
  239. public override bool RequireSorting { get { return false; } }
  240. }
  241. internal class FollowingSiblingIterator : SimpleIterator
  242. {
  243. public FollowingSiblingIterator (BaseIterator iter) : base (iter) {}
  244. private FollowingSiblingIterator (FollowingSiblingIterator other) : base (other, true) {}
  245. public override XPathNodeIterator Clone () { return new FollowingSiblingIterator (this); }
  246. public override bool MoveNextCore ()
  247. {
  248. switch (_nav.NodeType) {
  249. case XPathNodeType.Attribute:
  250. case XPathNodeType.Namespace:
  251. // They have no siblings.
  252. return false;
  253. }
  254. if (_nav.MoveToNext ())
  255. {
  256. _current = null;
  257. return true;
  258. }
  259. return false;
  260. }
  261. public override bool RequireSorting { get { return false; } }
  262. }
  263. internal class PrecedingSiblingIterator : SimpleIterator
  264. {
  265. bool finished;
  266. bool started;
  267. XPathNavigator startPosition;
  268. public PrecedingSiblingIterator (BaseIterator iter) : base (iter)
  269. {
  270. startPosition = iter.Current.Clone ();
  271. }
  272. private PrecedingSiblingIterator (PrecedingSiblingIterator other) : base (other, true)
  273. {
  274. startPosition = other.startPosition;
  275. started = other.started;
  276. finished = other.finished;
  277. }
  278. public override XPathNodeIterator Clone () { return new PrecedingSiblingIterator (this); }
  279. public override bool MoveNextCore ()
  280. {
  281. if (finished)
  282. return false;
  283. if (!started) {
  284. started = true;
  285. switch (_nav.NodeType) {
  286. case XPathNodeType.Attribute:
  287. case XPathNodeType.Namespace:
  288. // They have no siblings.
  289. finished = true;
  290. return false;
  291. }
  292. _nav.MoveToFirst ();
  293. if (!_nav.IsSamePosition (startPosition)) {
  294. _current = null;
  295. return true;
  296. }
  297. } else {
  298. if (!_nav.MoveToNext ()) {
  299. finished = true;
  300. return false;
  301. }
  302. }
  303. if (_nav.ComparePosition (startPosition) != XmlNodeOrder.Before) {
  304. // Note that if _nav contains only 1 node, it won't be Same.
  305. finished = true;
  306. return false;
  307. } else {
  308. _current = null;
  309. return true;
  310. }
  311. }
  312. public override bool ReverseAxis {
  313. get { return true; }
  314. }
  315. public override bool RequireSorting { get { return true; } }
  316. }
  317. internal class AncestorIterator : SimpleIterator
  318. {
  319. int currentPosition;
  320. ArrayList navigators;
  321. XPathNavigator startPosition;
  322. public AncestorIterator (BaseIterator iter) : base (iter)
  323. {
  324. startPosition = iter.Current.Clone ();
  325. }
  326. private AncestorIterator (AncestorIterator other)
  327. : base (other, true)
  328. {
  329. startPosition = other.startPosition;
  330. if (other.navigators != null)
  331. navigators = (ArrayList) other.navigators.Clone ();
  332. currentPosition = other.currentPosition;
  333. }
  334. public override XPathNodeIterator Clone ()
  335. {
  336. return new AncestorIterator (this);
  337. }
  338. private void CollectResults ()
  339. {
  340. navigators = new ArrayList ();
  341. XPathNavigator ancestors = startPosition.Clone ();
  342. if (!ancestors.MoveToParent ())
  343. return;
  344. while (ancestors.NodeType != XPathNodeType.Root) {
  345. navigators.Add (ancestors.Clone ());
  346. ancestors.MoveToParent ();
  347. }
  348. currentPosition = navigators.Count;
  349. }
  350. public override bool MoveNextCore ()
  351. {
  352. if (navigators == null) {
  353. CollectResults ();
  354. if (startPosition.NodeType != XPathNodeType.Root) {
  355. // First time it returns Root
  356. _nav.MoveToRoot ();
  357. _current = null;
  358. return true;
  359. }
  360. }
  361. if (currentPosition == 0)
  362. return false;
  363. _nav.MoveTo ((XPathNavigator) navigators [--currentPosition]);
  364. _current = null;
  365. return true;
  366. }
  367. public override bool ReverseAxis {
  368. get { return true; }
  369. }
  370. public override bool RequireSorting { get { return true; } }
  371. public override int Count {
  372. get {
  373. if (navigators == null)
  374. CollectResults ();
  375. return navigators.Count;
  376. }
  377. }
  378. }
  379. internal class AncestorOrSelfIterator : SimpleIterator
  380. {
  381. int currentPosition;
  382. ArrayList navigators;
  383. XPathNavigator startPosition;
  384. public AncestorOrSelfIterator (BaseIterator iter) : base (iter)
  385. {
  386. startPosition = iter.Current.Clone ();
  387. }
  388. private AncestorOrSelfIterator (AncestorOrSelfIterator other)
  389. : base (other, true)
  390. {
  391. startPosition = other.startPosition;
  392. if (other.navigators != null)
  393. navigators = (ArrayList) other.navigators.Clone ();
  394. currentPosition = other.currentPosition;
  395. }
  396. public override XPathNodeIterator Clone ()
  397. {
  398. return new AncestorOrSelfIterator (this);
  399. }
  400. private void CollectResults ()
  401. {
  402. navigators = new ArrayList ();
  403. XPathNavigator ancestors = startPosition.Clone ();
  404. if (!ancestors.MoveToParent ())
  405. return;
  406. while (ancestors.NodeType != XPathNodeType.Root) {
  407. navigators.Add (ancestors.Clone ());
  408. ancestors.MoveToParent ();
  409. }
  410. currentPosition = navigators.Count;
  411. }
  412. public override bool MoveNextCore ()
  413. {
  414. if (navigators == null) {
  415. CollectResults ();
  416. if (startPosition.NodeType != XPathNodeType.Root) {
  417. // First time it returns Root
  418. _nav.MoveToRoot ();
  419. _current = null;
  420. return true;
  421. }
  422. }
  423. if (currentPosition == -1)
  424. return false;
  425. if (currentPosition-- == 0) {
  426. _nav.MoveTo (startPosition);
  427. _current = null;
  428. return true; // returns self.
  429. }
  430. _nav.MoveTo ((XPathNavigator) navigators [currentPosition]);
  431. _current = null;
  432. return true;
  433. }
  434. public override bool ReverseAxis {
  435. get { return true; }
  436. }
  437. public override bool RequireSorting { get { return true; } }
  438. public override int Count {
  439. get {
  440. if (navigators == null)
  441. CollectResults ();
  442. return navigators.Count + 1;
  443. }
  444. }
  445. }
  446. internal class DescendantIterator : SimpleIterator
  447. {
  448. private int _depth;
  449. private bool _finished;
  450. public DescendantIterator (BaseIterator iter) : base (iter) {}
  451. private DescendantIterator (DescendantIterator other) : base (other)
  452. {
  453. _depth = other._depth;
  454. _finished = other._finished;
  455. }
  456. public override XPathNodeIterator Clone () { return new DescendantIterator (this); }
  457. public override bool MoveNextCore ()
  458. {
  459. if (_finished)
  460. return false;
  461. if (_nav.MoveToFirstChild ())
  462. {
  463. _depth ++;
  464. _current = null;
  465. return true;
  466. }
  467. while (_depth != 0)
  468. {
  469. if (_nav.MoveToNext ())
  470. {
  471. _current = null;
  472. return true;
  473. }
  474. if (!_nav.MoveToParent ()) // should NEVER fail!
  475. throw new XPathException ("There seems some bugs on the XPathNavigator implementation class.");
  476. _depth --;
  477. }
  478. _finished = true;
  479. return false;
  480. }
  481. public override bool RequireSorting { get { return false; } }
  482. }
  483. internal class DescendantOrSelfIterator : SimpleIterator
  484. {
  485. private int _depth;
  486. private bool _finished;
  487. public DescendantOrSelfIterator (BaseIterator iter) : base (iter) {}
  488. private DescendantOrSelfIterator (DescendantOrSelfIterator other) : base (other, true)
  489. {
  490. _depth = other._depth;
  491. }
  492. public override XPathNodeIterator Clone () { return new DescendantOrSelfIterator (this); }
  493. public override bool MoveNextCore ()
  494. {
  495. if (_finished)
  496. return false;
  497. if (CurrentPosition == 0)
  498. {
  499. // self
  500. _current = null;
  501. return true;
  502. }
  503. if (_nav.MoveToFirstChild ())
  504. {
  505. _depth ++;
  506. _current = null;
  507. return true;
  508. }
  509. while (_depth != 0)
  510. {
  511. if (_nav.MoveToNext ())
  512. {
  513. _current = null;
  514. return true;
  515. }
  516. if (!_nav.MoveToParent ()) // should NEVER fail!
  517. throw new XPathException ("There seems some bugs on the XPathNavigator implementation class.");
  518. _depth --;
  519. }
  520. _finished = true;
  521. return false;
  522. }
  523. public override bool RequireSorting { get { return false; } }
  524. }
  525. internal class FollowingIterator : SimpleIterator
  526. {
  527. private bool _finished = false;
  528. public FollowingIterator (BaseIterator iter) : base (iter) {}
  529. private FollowingIterator (FollowingIterator other) : base (other, true) {}
  530. public override XPathNodeIterator Clone () { return new FollowingIterator (this); }
  531. public override bool MoveNextCore ()
  532. {
  533. if (_finished)
  534. return false;
  535. 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 = null;
  549. return true;
  550. } else {
  551. while (_nav.MoveToParent ()) {
  552. if (_nav.MoveToNext ()) {
  553. _current = null;
  554. return true;
  555. }
  556. }
  557. }
  558. break;
  559. }
  560. }
  561. if (checkChildren)
  562. {
  563. if (_nav.MoveToFirstChild ())
  564. {
  565. _current = null;
  566. return true;
  567. }
  568. do
  569. {
  570. if (_nav.MoveToNext ())
  571. {
  572. _current = null;
  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 = null;
  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 = null;
  649. return true;
  650. }
  651. }
  652. else if (_nav.MoveToNextNamespace ())
  653. {
  654. _current = null;
  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 = null;
  674. return true;
  675. }
  676. }
  677. else if (_nav.MoveToNextAttribute ())
  678. {
  679. _current = null;
  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) : base (iter.NamespaceManager)
  748. {
  749. _iterLeft = iter;
  750. _expr = expr;
  751. if (_iterLeft.RequireSorting || _expr.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. BaseIterator tmpIter = (BaseIterator) _iterList.GetByIndex (last);
  800. _iterList.RemoveAt (last);
  801. switch (tmpIter.Current.ComparePosition (_iterRight.Current)) {
  802. case XmlNodeOrder.Same:
  803. case XmlNodeOrder.Before:
  804. _iterRight = tmpIter;
  805. continue;
  806. default:
  807. _iterRight = tmpIter;
  808. break;
  809. }
  810. break;
  811. } else if (_nextIterRight != null) {
  812. _iterRight = _nextIterRight;
  813. _nextIterRight = null;
  814. break;
  815. } else if (!_iterLeft.MoveNext ()) {
  816. _finished = true;
  817. return false;
  818. }
  819. else
  820. _iterRight = _expr.EvaluateNodeSet (_iterLeft);
  821. }
  822. bool loop = true;
  823. while (loop) {
  824. loop = false;
  825. if (_nextIterRight == null) {
  826. bool noMoreNext = false;
  827. while (_nextIterRight == null || !_nextIterRight.MoveNext ()) {
  828. if(_iterLeft.MoveNext ())
  829. _nextIterRight = _expr.EvaluateNodeSet (_iterLeft);
  830. else {
  831. noMoreNext = true;
  832. break;
  833. }
  834. }
  835. if (noMoreNext)
  836. _nextIterRight = null; // FIXME: More efficient code. Maybe making noMoreNext class scope would be better.
  837. }
  838. if (_nextIterRight != null) {
  839. switch (_iterRight.Current.ComparePosition (_nextIterRight.Current)) {
  840. case XmlNodeOrder.After:
  841. _iterList.Add (_iterList.Count, _iterRight);
  842. _iterRight = _nextIterRight;
  843. _nextIterRight = null;
  844. loop = true;
  845. break;
  846. case XmlNodeOrder.Same:
  847. if (!_nextIterRight.MoveNext ())
  848. _nextIterRight = null;
  849. else {
  850. int last = _iterList.Count;
  851. if (last > 0) {
  852. _iterList.Add (last, _nextIterRight);
  853. _nextIterRight = (BaseIterator) _iterList.GetByIndex (last);
  854. _iterList.RemoveAt (last);
  855. }
  856. }
  857. loop = true;
  858. break;
  859. }
  860. }
  861. }
  862. return true;
  863. }
  864. }
  865. private void CollectResults ()
  866. {
  867. _navStore = new ArrayList ();
  868. while (true) {
  869. while (_iterRight == null || !_iterRight.MoveNext ()) {
  870. if (!_iterLeft.MoveNext ()) {
  871. _navStore.Sort (XPathNavigatorComparer.Instance);
  872. return;
  873. }
  874. _iterRight = _expr.EvaluateNodeSet (_iterLeft);
  875. }
  876. XPathNavigator nav = _iterRight.Current;
  877. _navStore.Add (nav);
  878. }
  879. }
  880. public override XPathNavigator Current {
  881. get {
  882. if (CurrentPosition <= 0) return null;
  883. if (_navStore != null) {
  884. return (XPathNavigator) _navStore [CurrentPosition - 1];
  885. } else {
  886. return _iterRight.Current;
  887. }
  888. }
  889. }
  890. public override bool RequireSorting {
  891. // It always does not need to be sorted.
  892. get { return false; }
  893. }
  894. public override int Count { get { return _navStore == null ? base.Count : _navStore.Count; } }
  895. }
  896. internal class PredicateIterator : BaseIterator
  897. {
  898. private BaseIterator _iter;
  899. private Expression _pred;
  900. private XPathResultType resType;
  901. private bool finished;
  902. public PredicateIterator (BaseIterator iter, Expression pred) : base (iter.NamespaceManager)
  903. {
  904. _iter = iter;
  905. _pred = pred;
  906. resType = pred.GetReturnType (iter);
  907. }
  908. private PredicateIterator (PredicateIterator other) : base (other)
  909. {
  910. _iter = (BaseIterator) other._iter.Clone ();
  911. _pred = other._pred;
  912. resType = other.resType;
  913. finished = other.finished;
  914. }
  915. public override XPathNodeIterator Clone () { return new PredicateIterator (this); }
  916. public override bool MoveNextCore ()
  917. {
  918. if (finished)
  919. return false;
  920. while (_iter.MoveNext ())
  921. {
  922. switch (resType) {
  923. case XPathResultType.Number:
  924. if (_pred.EvaluateNumber (_iter) != _iter.ComparablePosition)
  925. continue;
  926. finished = true;
  927. break;
  928. case XPathResultType.Any: {
  929. object result = _pred.Evaluate (_iter);
  930. if (result is double)
  931. {
  932. if ((double) result != _iter.ComparablePosition)
  933. continue;
  934. finished = true;
  935. }
  936. else if (!XPathFunctions.ToBoolean (result))
  937. continue;
  938. }
  939. break;
  940. default:
  941. if (!_pred.EvaluateBoolean (_iter))
  942. continue;
  943. break;
  944. }
  945. return true;
  946. }
  947. return false;
  948. }
  949. public override XPathNavigator Current { get { return _iter.Current; }}
  950. public override bool ReverseAxis {
  951. get { return _iter.ReverseAxis; }
  952. }
  953. public override bool RequireSorting { get { return _iter.RequireSorting || _pred.RequireSorting; } }
  954. }
  955. internal class ListIterator : BaseIterator
  956. {
  957. private IList _list;
  958. bool _requireSorting;
  959. public ListIterator (BaseIterator iter, IList list, bool requireSorting) : base (iter.NamespaceManager)
  960. {
  961. if (!(list is ICloneable))
  962. throw new ArgumentException ("Target enumerator must be cloneable.");
  963. _list = list;
  964. _requireSorting = requireSorting;
  965. }
  966. public ListIterator (IList list, NSResolver nsm, bool requireSorting) : base (nsm)
  967. {
  968. if (!(list is ICloneable))
  969. throw new ArgumentException ("Target enumerator must be cloneable.");
  970. _list = list;
  971. _requireSorting = requireSorting;
  972. }
  973. private ListIterator (ListIterator other) : base (other)
  974. {
  975. ICloneable listClone = other._list as ICloneable;
  976. _list = (IList) listClone.Clone ();
  977. _requireSorting = other._requireSorting;
  978. }
  979. public override XPathNodeIterator Clone () { return new ListIterator (this); }
  980. public override bool MoveNextCore ()
  981. {
  982. if (CurrentPosition >= _list.Count)
  983. return false;
  984. return true;
  985. }
  986. public override XPathNavigator Current {
  987. get {
  988. if (_list.Count == 0)
  989. return null;
  990. return (XPathNavigator) _list [CurrentPosition - 1];
  991. }
  992. }
  993. public override bool RequireSorting { get { return _requireSorting; } }
  994. public override int Count { get { return _list.Count; } }
  995. }
  996. internal class UnionIterator : BaseIterator
  997. {
  998. private BaseIterator _left, _right;
  999. private bool keepLeft;
  1000. private bool keepRight;
  1001. private bool useRight;
  1002. public UnionIterator (BaseIterator iter, BaseIterator left, BaseIterator right) : base (iter.NamespaceManager)
  1003. {
  1004. _left = left;
  1005. _right = right;
  1006. }
  1007. private UnionIterator (UnionIterator other) : base (other)
  1008. {
  1009. _left = (BaseIterator) other._left.Clone ();
  1010. _right = (BaseIterator) other._right.Clone ();
  1011. keepLeft = other.keepLeft;
  1012. keepRight = other.keepRight;
  1013. useRight = other.useRight;
  1014. }
  1015. public override XPathNodeIterator Clone () { return new UnionIterator (this); }
  1016. public override bool MoveNextCore ()
  1017. {
  1018. if (!keepLeft)
  1019. keepLeft = _left.MoveNext ();
  1020. if (!keepRight)
  1021. keepRight = _right.MoveNext ();
  1022. if (!keepLeft && !keepRight)
  1023. return false;
  1024. if (!keepRight) {
  1025. keepLeft = useRight = false;
  1026. return true;
  1027. } else if (!keepLeft) {
  1028. keepRight = false;
  1029. useRight = true;
  1030. return true;
  1031. }
  1032. switch (_left.Current.ComparePosition (_right.Current)) {
  1033. case XmlNodeOrder.Same:
  1034. // consume both. i.e. don't output duplicate result.
  1035. keepLeft = keepRight = false;
  1036. useRight = true;
  1037. return true;
  1038. case XmlNodeOrder.Before:
  1039. case XmlNodeOrder.Unknown: // Maybe happen because of "document(a) | document(b)"
  1040. keepLeft = useRight = false;
  1041. return true;
  1042. case XmlNodeOrder.After:
  1043. keepRight = false;
  1044. useRight = true;
  1045. return true;
  1046. default:
  1047. throw new InvalidOperationException ("Should not happen.");
  1048. }
  1049. }
  1050. public override XPathNavigator Current
  1051. {
  1052. get
  1053. {
  1054. if (CurrentPosition == 0)
  1055. return null;
  1056. if (useRight)
  1057. return _right.Current;
  1058. else
  1059. return _left.Current;
  1060. }
  1061. }
  1062. public override bool RequireSorting { get { return _left.RequireSorting || _right.RequireSorting; } }
  1063. }
  1064. }