XPathSequence.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. //
  2. // XPathSequence.cs - represents XPath sequence iterator
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Collections;
  32. using System.Xml;
  33. using System.Xml.Schema;
  34. using System.Xml.Query;
  35. using System.Xml.XPath;
  36. namespace Mono.Xml.XPath2
  37. {
  38. public abstract class XPathSequence : IEnumerable, ICloneable
  39. {
  40. XQueryContext ctx;
  41. int countCache = -1;
  42. int position = 0;
  43. internal XPathSequence (XQueryContext ctx)
  44. {
  45. this.ctx = ctx;
  46. }
  47. internal XPathSequence (XPathSequence original)
  48. {
  49. ctx = original.ctx;
  50. position = original.position;
  51. }
  52. internal XQueryContext Context {
  53. get { return ctx; }
  54. }
  55. public virtual int Count {
  56. get {
  57. if (countCache >= 0)
  58. return countCache;
  59. XPathSequence clone = Clone ();
  60. countCache = 0;
  61. while (clone.MoveNext ())
  62. countCache++;
  63. return countCache;
  64. }
  65. }
  66. public XPathItem Current {
  67. get {
  68. if (Position == 0)
  69. throw new InvalidOperationException ("XQuery internal error (should not happen)");
  70. return CurrentCore;
  71. }
  72. }
  73. public abstract XPathItem CurrentCore { get; }
  74. // Returns 0 if not started, otherwise returns XPath positional integer.
  75. public int Position {
  76. get { return position; }
  77. }
  78. public bool MoveNext ()
  79. {
  80. if (!MoveNextCore ())
  81. return false;
  82. position++;
  83. return true;
  84. }
  85. public abstract bool MoveNextCore ();
  86. public abstract XPathSequence Clone ();
  87. object ICloneable.Clone ()
  88. {
  89. return this.Clone ();
  90. }
  91. public virtual IEnumerator GetEnumerator ()
  92. {
  93. while (MoveNext ())
  94. yield return CurrentCore;
  95. }
  96. }
  97. // empty iterator (still required since it contains XQueryContext)
  98. class XPathEmptySequence : XPathSequence
  99. {
  100. internal XPathEmptySequence (XPathSequence iter)
  101. : base (iter.Context)
  102. {
  103. }
  104. public override int Count {
  105. get { return 0; }
  106. }
  107. public override bool MoveNextCore ()
  108. {
  109. return false;
  110. }
  111. public override XPathItem CurrentCore {
  112. get { throw new InvalidOperationException ("Should not happen. In XPathEmptySequence.Current."); }
  113. }
  114. // Don't return clone
  115. public override XPathSequence Clone ()
  116. {
  117. return this;
  118. }
  119. }
  120. // single item iterator
  121. internal class SingleItemIterator : XPathSequence
  122. {
  123. XPathItem item;
  124. XPathItem current;
  125. public SingleItemIterator (XPathItem item, XPathSequence iter)
  126. : this (item, iter.Context)
  127. {
  128. }
  129. // for XQuery execution start point
  130. internal SingleItemIterator (XPathItem item, XQueryContext ctx)
  131. : base (ctx)
  132. {
  133. this.item = item;
  134. }
  135. private SingleItemIterator (SingleItemIterator other)
  136. : base (other)
  137. {
  138. this.item = other.item;
  139. this.current = other.current;
  140. }
  141. public override XPathSequence Clone ()
  142. {
  143. return new SingleItemIterator (this);
  144. }
  145. public override bool MoveNextCore ()
  146. {
  147. if (item != null) {
  148. current = item;
  149. item = null;
  150. return true;
  151. }
  152. return false;
  153. }
  154. public override XPathItem CurrentCore {
  155. get {
  156. return current;
  157. }
  158. }
  159. }
  160. // RangeExpr iterator
  161. internal class IntegerRangeIterator : XPathSequence
  162. {
  163. static XmlSchemaSimpleType intType = XmlSchemaType.GetBuiltInSimpleType (new XmlQualifiedName ("int", XmlSchema.Namespace));
  164. int start;
  165. int end;
  166. int next;
  167. XPathItem current;
  168. public IntegerRangeIterator (XPathSequence iter, int start, int end)
  169. : base (iter.Context)
  170. {
  171. this.start = start;
  172. this.end = end;
  173. }
  174. private IntegerRangeIterator (IntegerRangeIterator other)
  175. : base (other)
  176. {
  177. this.start = other.start;
  178. this.end = other.end;
  179. this.next = other.next;
  180. this.current = other.current;
  181. }
  182. public override XPathSequence Clone ()
  183. {
  184. return new IntegerRangeIterator (this);
  185. }
  186. public override bool MoveNextCore ()
  187. {
  188. if (current == null)
  189. next = start;
  190. if (next > end)
  191. return false;
  192. current = new XPathAtomicValue (next++, intType);
  193. return true;
  194. }
  195. public override XPathItem CurrentCore {
  196. get {
  197. return current;
  198. }
  199. }
  200. }
  201. // Slash iterator
  202. internal class ChildPathIterator : XPathSequence
  203. {
  204. XPathSequence left;
  205. ExprSingle child;
  206. public ChildPathIterator (XPathSequence iter, PathChildExpr source)
  207. : base (iter.Context)
  208. {
  209. left = source.Left.Evaluate (iter);
  210. child = source.Next;
  211. }
  212. private ChildPathIterator (ChildPathIterator other)
  213. : base (other)
  214. {
  215. left = other.left.Clone ();
  216. child = other.child;
  217. }
  218. public override XPathSequence Clone ()
  219. {
  220. return new ChildPathIterator (this);
  221. }
  222. public override bool MoveNextCore ()
  223. {
  224. throw new NotImplementedException ();
  225. }
  226. public override XPathItem CurrentCore {
  227. get { return left.Current; }
  228. }
  229. }
  230. // Slash2 iterator
  231. internal class DescendantPathIterator : XPathSequence
  232. {
  233. XPathSequence left;
  234. ExprSingle descendant;
  235. public DescendantPathIterator (XPathSequence iter, PathDescendantExpr source)
  236. : base (iter.Context)
  237. {
  238. left = source.Left.Evaluate (iter);
  239. descendant = source.Descendant;
  240. }
  241. private DescendantPathIterator (DescendantPathIterator other)
  242. : base (other)
  243. {
  244. left = other.left.Clone ();
  245. descendant = other.descendant;
  246. }
  247. public override XPathSequence Clone ()
  248. {
  249. return new DescendantPathIterator (this);
  250. }
  251. public override bool MoveNextCore ()
  252. {
  253. throw new NotImplementedException ();
  254. }
  255. public override XPathItem CurrentCore {
  256. get { return left.Current; }
  257. }
  258. }
  259. // Filter step iterator
  260. internal class FilteredIterator : XPathSequence
  261. {
  262. XPathSequence left;
  263. PredicateList filter;
  264. public FilteredIterator (XPathSequence iter, FilterStepExpr source)
  265. : base (iter.Context)
  266. {
  267. left = source.Expr.Evaluate (iter);
  268. filter = source.Predicates;
  269. }
  270. private FilteredIterator (FilteredIterator other)
  271. : base (other)
  272. {
  273. left = other.left.Clone ();
  274. filter = other.filter;
  275. }
  276. public override XPathSequence Clone ()
  277. {
  278. return new FilteredIterator (this);
  279. }
  280. public override bool MoveNextCore ()
  281. {
  282. throw new NotImplementedException ();
  283. }
  284. public override XPathItem CurrentCore {
  285. get { return left.Current; }
  286. }
  287. }
  288. // AxisIterator
  289. internal class AxisIterator : XPathSequence
  290. {
  291. XPathSequence iter;
  292. AxisStepExpr source;
  293. public AxisIterator (XPathSequence iter, AxisStepExpr source)
  294. : base (iter.Context)
  295. {
  296. this.iter = iter;
  297. this.source = source;
  298. }
  299. private AxisIterator (AxisIterator other)
  300. : base (other)
  301. {
  302. iter = other.iter.Clone ();
  303. source = other.source;
  304. }
  305. public override XPathSequence Clone ()
  306. {
  307. return new AxisIterator (this);
  308. }
  309. public override bool MoveNextCore ()
  310. {
  311. throw new NotImplementedException ();
  312. }
  313. public override XPathItem CurrentCore {
  314. get { throw new NotImplementedException (); }
  315. }
  316. }
  317. /*
  318. // Node test iterator
  319. internal class NodeKindTestIterator : XPathSequence
  320. {
  321. XPathSequence iter;
  322. NodeKindTestExpression source;
  323. public NodeKindTestIterator (XPathSequence iter, NodeKindTestExpression source)
  324. : base (iter.Context)
  325. {
  326. this.iter = iter;
  327. this.source = source;
  328. }
  329. private NodeKindTestIterator (NodeKindTestIterator other)
  330. : base (other)
  331. {
  332. iter = other.iter.Clone ();
  333. source = other.source;
  334. }
  335. public override XPathSequence Clone ()
  336. {
  337. return new NodeKindTestIterator (this);
  338. }
  339. public override bool MoveNextCore ()
  340. {
  341. throw new NotImplementedException ();
  342. }
  343. public override XPathItem CurrentCore {
  344. get { throw new NotImplementedException (); }
  345. }
  346. }
  347. */
  348. internal class ExprSequenceIterator : XPathSequence
  349. {
  350. XPathSequence contextSequence;
  351. XPathSequence iter;
  352. ExprSequence expr;
  353. int currentExprIndex;
  354. public ExprSequenceIterator (XPathSequence iter, ExprSequence expr)
  355. : base (iter.Context)
  356. {
  357. contextSequence = iter;
  358. this.expr = expr;
  359. }
  360. private ExprSequenceIterator (ExprSequenceIterator other)
  361. : base (other)
  362. {
  363. if (other.iter != null)
  364. iter = other.iter.Clone ();
  365. expr = other.expr;
  366. currentExprIndex = other.currentExprIndex;
  367. }
  368. public override XPathSequence Clone ()
  369. {
  370. return new ExprSequenceIterator (this);
  371. }
  372. public override bool MoveNextCore ()
  373. {
  374. if (iter != null && iter.MoveNext ())
  375. return true;
  376. for (; currentExprIndex < expr.Count; currentExprIndex++) {
  377. iter = expr [currentExprIndex].Evaluate (contextSequence);
  378. if (iter.MoveNext ())
  379. return true;
  380. }
  381. return false;
  382. }
  383. public override XPathItem CurrentCore {
  384. get { return iter.Current; }
  385. }
  386. }
  387. internal class FLWORIterator : XPathSequence
  388. {
  389. XPathSequence contextSequence;
  390. FLWORExpr expr;
  391. public FLWORIterator (XPathSequence iter, FLWORExpr expr)
  392. : base (iter.Context)
  393. {
  394. this.contextSequence = iter;
  395. this.expr = expr;
  396. }
  397. private FLWORIterator (FLWORIterator other)
  398. : base (other)
  399. {
  400. contextSequence = other.contextSequence;
  401. expr = other.expr;
  402. throw new NotImplementedException ();
  403. }
  404. public override XPathSequence Clone ()
  405. {
  406. return new FLWORIterator (this);
  407. }
  408. public override bool MoveNextCore ()
  409. {
  410. throw new NotImplementedException ();
  411. }
  412. public override XPathItem CurrentCore {
  413. get { throw new NotImplementedException (); }
  414. }
  415. }
  416. internal class AtomizingIterator : XPathSequence
  417. {
  418. XPathSequence iter;
  419. public AtomizingIterator (XPathSequence iter)
  420. : base (iter.Context)
  421. {
  422. this.iter = iter;
  423. }
  424. private AtomizingIterator (AtomizingIterator other)
  425. : base (other)
  426. {
  427. iter = other.iter.Clone ();
  428. }
  429. public override XPathSequence Clone ()
  430. {
  431. return new AtomizingIterator (this);
  432. }
  433. public override bool MoveNextCore ()
  434. {
  435. return iter.MoveNext ();
  436. }
  437. public override XPathItem CurrentCore {
  438. get {
  439. XPathNavigator nav = iter.Current as XPathNavigator;
  440. if (nav == null)
  441. return (XPathAtomicValue) iter.Current;
  442. if (nav.SchemaInfo != null)
  443. return new XPathAtomicValue (
  444. nav.TypedValue,
  445. nav.SchemaInfo.SchemaType);
  446. else
  447. return new XPathAtomicValue (nav.Value, null);
  448. }
  449. }
  450. }
  451. internal class ConvertingIterator : XPathSequence
  452. {
  453. XPathSequence iter;
  454. SequenceType type;
  455. public ConvertingIterator (XPathSequence iter, SequenceType type)
  456. : base (iter.Context)
  457. {
  458. this.iter = iter;
  459. type = type;
  460. }
  461. private ConvertingIterator (ConvertingIterator other)
  462. : base (other)
  463. {
  464. iter = other.iter.Clone ();
  465. type = other.type;
  466. }
  467. public override XPathSequence Clone ()
  468. {
  469. return new ConvertingIterator (this);
  470. }
  471. public override bool MoveNextCore ()
  472. {
  473. return iter.MoveNext ();
  474. }
  475. public override XPathItem CurrentCore {
  476. get { return type.Convert (iter.Current); }
  477. }
  478. }
  479. internal class TracingIterator : XPathSequence
  480. {
  481. XPathSequence iter;
  482. string format;
  483. public TracingIterator (XPathSequence iter, string format)
  484. : base (iter.Context)
  485. {
  486. this.iter = iter;
  487. this.format = format;
  488. }
  489. private TracingIterator (TracingIterator other)
  490. : base (other)
  491. {
  492. iter = other.iter.Clone ();
  493. format = other.format;
  494. }
  495. public override XPathSequence Clone ()
  496. {
  497. return new TracingIterator (this);
  498. }
  499. public override bool MoveNextCore ()
  500. {
  501. if (!iter.MoveNext ())
  502. return false;
  503. // FIXME: use OnMessageEvent
  504. // Context.ErrorOutput.Write (format, iter.Current.TypedValue);
  505. throw new NotImplementedException ();
  506. return true;
  507. }
  508. public override XPathItem CurrentCore {
  509. get { return iter.Current; }
  510. }
  511. }
  512. internal class ListIterator : XPathSequence
  513. {
  514. IList list;
  515. public ListIterator (XPathSequence iter, IList list)
  516. : base (iter.Context)
  517. {
  518. if (list is ICloneable)
  519. this.list = list;
  520. else
  521. throw new InvalidOperationException (String.Format ("XQuery internal error: target list is not cloneable. List is {0}.", list != null ? list.GetType ().ToString () : "null argument"));
  522. }
  523. private ListIterator (ListIterator other)
  524. : base (other)
  525. {
  526. this.list = (IList) ((ICloneable) other.list).Clone ();
  527. }
  528. public override XPathSequence Clone ()
  529. {
  530. return new ListIterator (this);
  531. }
  532. public override bool MoveNextCore ()
  533. {
  534. return (Position < list.Count);
  535. }
  536. public override XPathItem CurrentCore {
  537. get { return (XPathItem) list [Position - 1]; }
  538. }
  539. }
  540. }
  541. #endif