XPathSequence.cs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  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. get { return ctx.ContextManager.CurrentContext; }
  55. }
  56. public virtual int Count {
  57. get {
  58. if (countCache >= 0)
  59. return countCache;
  60. XPathSequence clone = Clone ();
  61. while (clone.MoveNext ())
  62. ;
  63. countCache = clone.Position;
  64. return countCache;
  65. }
  66. }
  67. public XPathItem Current {
  68. get {
  69. if (Position == 0)
  70. throw new InvalidOperationException ("XQuery internal error (should not happen)");
  71. return CurrentCore;
  72. }
  73. }
  74. public abstract XPathItem CurrentCore { get; }
  75. // Returns 0 if not started, otherwise returns XPath positional integer.
  76. public virtual int Position {
  77. get { return position; }
  78. }
  79. public virtual bool MoveNext ()
  80. {
  81. if (!MoveNextCore ())
  82. return false;
  83. position++;
  84. return true;
  85. }
  86. protected abstract bool MoveNextCore ();
  87. public abstract XPathSequence Clone ();
  88. object ICloneable.Clone ()
  89. {
  90. return this.Clone ();
  91. }
  92. public virtual IEnumerator GetEnumerator ()
  93. {
  94. while (MoveNext ())
  95. yield return CurrentCore;
  96. }
  97. }
  98. // empty iterator (still required since it contains XQueryContext)
  99. class XPathEmptySequence : XPathSequence
  100. {
  101. internal XPathEmptySequence (XQueryContext ctx)
  102. : base (ctx)
  103. {
  104. }
  105. public override int Count {
  106. get { return 0; }
  107. }
  108. protected override bool MoveNextCore ()
  109. {
  110. return false;
  111. }
  112. public override XPathItem CurrentCore {
  113. get { throw new InvalidOperationException ("Should not happen. In XPathEmptySequence.Current."); }
  114. }
  115. // Don't return clone. It's waste of resource.
  116. public override XPathSequence Clone ()
  117. {
  118. return this;
  119. }
  120. }
  121. // single item iterator
  122. internal class SingleItemIterator : XPathSequence
  123. {
  124. XPathItem item;
  125. XPathItem current;
  126. // for XQuery execution start point
  127. internal SingleItemIterator (XPathItem item, XQueryContext ctx)
  128. : base (ctx)
  129. {
  130. this.item = item;
  131. }
  132. private SingleItemIterator (SingleItemIterator other)
  133. : base (other)
  134. {
  135. this.item = other.item;
  136. this.current = other.current;
  137. }
  138. public override XPathSequence Clone ()
  139. {
  140. return new SingleItemIterator (this);
  141. }
  142. protected override bool MoveNextCore ()
  143. {
  144. if (current == null) {
  145. current = item;
  146. return true;
  147. }
  148. return false;
  149. }
  150. public override XPathItem CurrentCore {
  151. get {
  152. return current;
  153. }
  154. }
  155. }
  156. // RangeExpr iterator
  157. internal class IntegerRangeIterator : XPathSequence
  158. {
  159. static XmlSchemaSimpleType intType = XmlSchemaType.GetBuiltInSimpleType (new XmlQualifiedName ("int", XmlSchema.Namespace));
  160. int start;
  161. int end;
  162. int next;
  163. XPathItem current;
  164. public IntegerRangeIterator (XQueryContext ctx, int start, int end)
  165. : base (ctx)
  166. {
  167. this.start = start;
  168. this.end = end;
  169. }
  170. private IntegerRangeIterator (IntegerRangeIterator other)
  171. : base (other)
  172. {
  173. this.start = other.start;
  174. this.end = other.end;
  175. this.next = other.next;
  176. this.current = other.current;
  177. }
  178. public override XPathSequence Clone ()
  179. {
  180. return new IntegerRangeIterator (this);
  181. }
  182. protected override bool MoveNextCore ()
  183. {
  184. if (current == null)
  185. next = start;
  186. if (next > end)
  187. return false;
  188. current = new XPathAtomicValue (next++, intType);
  189. return true;
  190. }
  191. public override XPathItem CurrentCore {
  192. get {
  193. return current;
  194. }
  195. }
  196. }
  197. // Slash iterator
  198. // <copy original='System.Xml.XPath/Iterator.cs,
  199. // System.Xml.XPath/XPathComparer.cs'>
  200. internal class PathStepIterator : XPathSequence
  201. {
  202. XPathSequence left;
  203. XPathSequence right;
  204. PathStepExpr step;
  205. ArrayList nodeStore;
  206. SortedList storedIterators;
  207. bool finished;
  208. XPathSequence nextRight;
  209. public PathStepIterator (XPathSequence iter, PathStepExpr source)
  210. : base (iter.Context)
  211. {
  212. left = iter;
  213. step = source;
  214. }
  215. private PathStepIterator (PathStepIterator other)
  216. : base (other)
  217. {
  218. left = other.left.Clone ();
  219. step = other.step;
  220. if (other.right != null)
  221. right = other.right.Clone ();
  222. if (other.nodeStore != null)
  223. nodeStore = (ArrayList) other.nodeStore.Clone ();
  224. if (other.storedIterators != null)
  225. storedIterators = (SortedList) other.storedIterators.Clone ();
  226. if (other.nextRight != null)
  227. nextRight = other.nextRight.Clone ();
  228. }
  229. public override XPathSequence Clone ()
  230. {
  231. return new PathStepIterator (this);
  232. }
  233. protected override bool MoveNextCore ()
  234. {
  235. if (finished)
  236. return false;
  237. if (step.RequireSorting) {
  238. // Mainly '//' ('/descendant-or-self::node()/')
  239. if (nodeStore == null) {
  240. CollectResults ();
  241. if (nodeStore.Count == 0) {
  242. finished = true;
  243. return false;
  244. } else
  245. // Initially it must not go to
  246. // the while loop below
  247. // (.Position -1 is -1).
  248. return true;
  249. }
  250. if (nodeStore.Count == Position) {
  251. finished = true;
  252. return false;
  253. }
  254. while (nodeStore.Count > Position) {
  255. if (((XPathNavigator) nodeStore [Position]).ComparePosition (
  256. (XPathNavigator) nodeStore [Position - 1]) == XmlNodeOrder.Same)
  257. nodeStore.RemoveAt (Position);
  258. else
  259. break;
  260. }
  261. return true;
  262. } else { // Sorting not required
  263. if (right == null) { // First time
  264. if (!left.MoveNext ())
  265. return false;
  266. right = step.Next.Evaluate (left);
  267. storedIterators = new SortedList (XPathSequenceComparer.Instance);
  268. }
  269. while (true) {
  270. while (!right.MoveNext ()) {
  271. if (storedIterators.Count > 0) {
  272. int last = storedIterators.Count - 1;
  273. XPathSequence tmpIter = (XPathSequence) storedIterators.GetByIndex (last);
  274. storedIterators.RemoveAt (last);
  275. switch (((XPathNavigator) tmpIter.Current).ComparePosition ((XPathNavigator) right.Current)) {
  276. case XmlNodeOrder.Same:
  277. case XmlNodeOrder.Before:
  278. right = tmpIter;
  279. continue;
  280. default:
  281. right = tmpIter;
  282. break;
  283. }
  284. break;
  285. } else if (nextRight != null) {
  286. right = nextRight;
  287. nextRight = null;
  288. break;
  289. } else if (!left.MoveNext ()) {
  290. finished = true;
  291. return false;
  292. }
  293. else
  294. right = step.Next.Evaluate (left);
  295. }
  296. bool loop = true;
  297. while (loop) {
  298. loop = false;
  299. if (nextRight == null) {
  300. bool noMoreNext = false;
  301. while (nextRight == null || !nextRight.MoveNext ()) {
  302. if(left.MoveNext ())
  303. nextRight = step.Next.Evaluate (left);
  304. else {
  305. noMoreNext = true;
  306. break;
  307. }
  308. }
  309. if (noMoreNext)
  310. nextRight = null; // FIXME: More efficient code. Maybe making noMoreNext class scope would be better.
  311. }
  312. if (nextRight != null) {
  313. switch (((XPathNavigator) right.Current).ComparePosition ((XPathNavigator) nextRight.Current)) {
  314. case XmlNodeOrder.After:
  315. storedIterators.Add (storedIterators.Count, right);
  316. right = nextRight;
  317. nextRight = null;
  318. loop = true;
  319. break;
  320. case XmlNodeOrder.Same:
  321. if (!nextRight.MoveNext ())
  322. nextRight = null;
  323. else {
  324. int last = storedIterators.Count;
  325. if (last > 0) {
  326. storedIterators.Add (last, nextRight);
  327. nextRight = (XPathSequence) storedIterators.GetByIndex (last);
  328. storedIterators.RemoveAt (last);
  329. }
  330. }
  331. loop = true;
  332. break;
  333. }
  334. }
  335. }
  336. return true;
  337. }
  338. }
  339. }
  340. private void CollectResults ()
  341. {
  342. if (nodeStore != null)
  343. return;
  344. nodeStore = new ArrayList ();
  345. while (true) {
  346. while (right == null || !right.MoveNext ()) {
  347. if (!left.MoveNext ()) {
  348. nodeStore.Sort (XPathNavigatorComparer2.Instance);
  349. return;
  350. }
  351. right = step.Next.Evaluate (left);
  352. }
  353. XPathNavigator nav = (XPathNavigator) right.Current;
  354. nodeStore.Add (nav);
  355. }
  356. }
  357. public override XPathItem CurrentCore {
  358. get {
  359. if (Position <= 0) return null;
  360. if (step.RequireSorting) {
  361. return (XPathNavigator) nodeStore [Position - 1];
  362. } else {
  363. return right.Current;
  364. }
  365. }
  366. }
  367. public override int Count {
  368. get {
  369. if (nodeStore == null)
  370. return base.Count;
  371. else
  372. return nodeStore.Count;
  373. }
  374. }
  375. internal class XPathSequenceComparer : IComparer
  376. {
  377. public static XPathSequenceComparer Instance = new XPathSequenceComparer ();
  378. private XPathSequenceComparer ()
  379. {
  380. }
  381. public int Compare (object o1, object o2)
  382. {
  383. XPathSequence nav1 = o1 as XPathSequence;
  384. XPathSequence nav2 = o2 as XPathSequence;
  385. if (nav1 == null)
  386. return -1;
  387. if (nav2 == null)
  388. return 1;
  389. switch (((XPathNavigator) nav1.Current).ComparePosition ((XPathNavigator) nav2.Current)) {
  390. case XmlNodeOrder.Same:
  391. return 0;
  392. case XmlNodeOrder.After:
  393. return -1;
  394. default:
  395. return 1;
  396. }
  397. }
  398. }
  399. internal class XPathNavigatorComparer2 : IComparer
  400. {
  401. public static XPathNavigatorComparer2 Instance = new XPathNavigatorComparer2 ();
  402. private XPathNavigatorComparer2 ()
  403. {
  404. }
  405. public int Compare (object o1, object o2)
  406. {
  407. XPathNavigator nav1 = o1 as XPathNavigator;
  408. XPathNavigator nav2 = o2 as XPathNavigator;
  409. if (nav1 == null)
  410. return -1;
  411. if (nav2 == null)
  412. return 1;
  413. switch (nav1.ComparePosition (nav2)) {
  414. case XmlNodeOrder.Same:
  415. return 0;
  416. case XmlNodeOrder.After:
  417. return 1;
  418. default:
  419. return -1;
  420. }
  421. }
  422. }
  423. }
  424. // </copy>
  425. // Filter step iterator
  426. internal class FilteredIterator : XPathSequence
  427. {
  428. XPathSequence left;
  429. ExprSequence filter;
  430. public FilteredIterator (XPathSequence iter, FilterStepExpr source)
  431. : base (iter.Context)
  432. {
  433. left = source.Expr.Evaluate (iter);
  434. filter = source.Predicate;
  435. }
  436. private FilteredIterator (FilteredIterator other)
  437. : base (other)
  438. {
  439. left = other.left.Clone ();
  440. filter = other.filter;
  441. }
  442. public override XPathSequence Clone ()
  443. {
  444. return new FilteredIterator (this);
  445. }
  446. protected override bool MoveNextCore ()
  447. {
  448. // FIXME: as for numeric predicates, it is MUCH faster
  449. // when it skips apparent non-candidates, with possible
  450. // method implementation "XPathSequence.SkipTo (int)".
  451. // When it comes true, iteration won't be done first.
  452. while (left.MoveNext ()) {
  453. bool doesntPass = true;
  454. // Treat as OK if any of filter expr passed.
  455. // FIXME: handle numeric predicate.
  456. foreach (ExprSingle single in filter) {
  457. XPathAtomicValue av = single.EvaluateAsAtomic (left);
  458. if (av == null)
  459. continue;
  460. if (SequenceType.IsNumeric (av.XmlType.TypeCode)) {
  461. // numeric filter
  462. if (av.ValueAsInt32 == left.Position) {
  463. doesntPass = false;
  464. break;
  465. }
  466. }
  467. else if (single.EvaluateAsBoolean (left)) {
  468. doesntPass = false;
  469. break;
  470. }
  471. }
  472. if (doesntPass)
  473. continue;
  474. return true;
  475. }
  476. return false;
  477. }
  478. public override XPathItem CurrentCore {
  479. get { return left.Current; }
  480. }
  481. }
  482. // AxisIterator
  483. internal class AxisIterator : XPathSequence
  484. {
  485. NodeIterator iter;
  486. AxisStepExpr source;
  487. public AxisIterator (NodeIterator iter, AxisStepExpr source)
  488. : base (iter.Context)
  489. {
  490. this.iter = iter;
  491. this.source = source;
  492. }
  493. private AxisIterator (AxisIterator other)
  494. : base (other)
  495. {
  496. iter = (NodeIterator) other.iter.Clone ();
  497. source = other.source;
  498. }
  499. public override XPathSequence Clone ()
  500. {
  501. return new AxisIterator (this);
  502. }
  503. protected override bool MoveNextCore ()
  504. {
  505. while (iter.MoveNext ()) {
  506. if (source.Matches (iter.Current as XPathNavigator))
  507. return true;
  508. }
  509. return false;
  510. }
  511. public override XPathItem CurrentCore {
  512. get { return iter.Current; }
  513. }
  514. }
  515. internal abstract class NodeIterator : XPathSequence
  516. {
  517. XPathNavigator node;
  518. XPathNavigator current;
  519. bool emptyInput;
  520. public NodeIterator (XPathNavigator nav, XQueryContext ctx)
  521. : base (ctx)
  522. {
  523. this.node = nav.Clone ();
  524. }
  525. internal NodeIterator (NodeIterator other, bool cloneFlag)
  526. : base (other)
  527. {
  528. if (other.emptyInput)
  529. emptyInput = true;
  530. else
  531. node = other.node.Clone ();
  532. }
  533. internal XPathNavigator Node {
  534. get { return node; }
  535. }
  536. public override bool MoveNext ()
  537. {
  538. if (emptyInput)
  539. return false;
  540. if (!base.MoveNext ())
  541. return false;
  542. current = null;
  543. return true;
  544. }
  545. public override XPathItem CurrentCore {
  546. get {
  547. if (current == null)
  548. current = node.Clone ();
  549. return current;
  550. }
  551. }
  552. public virtual bool ReverseAxis {
  553. get { return false; }
  554. }
  555. }
  556. // <copy original='System.Xml.XPath/Iterator.cs'>
  557. internal class SelfIterator : NodeIterator
  558. {
  559. public SelfIterator (XPathNavigator nav, XQueryContext ctx)
  560. : base (nav, ctx)
  561. {
  562. }
  563. private SelfIterator (SelfIterator other, bool cloneFlag)
  564. : base (other, true)
  565. {
  566. }
  567. public override XPathSequence Clone ()
  568. {
  569. return new SelfIterator (this, true);
  570. }
  571. protected override bool MoveNextCore ()
  572. {
  573. if (Position == 0)
  574. return true;
  575. return false;
  576. }
  577. }
  578. internal class ParentIterator : NodeIterator
  579. {
  580. public ParentIterator (XPathNavigator nav, XQueryContext ctx)
  581. : base (nav, ctx)
  582. {
  583. }
  584. private ParentIterator (ParentIterator other, bool cloneFlag)
  585. : base (other, true)
  586. {
  587. }
  588. public override XPathSequence Clone ()
  589. {
  590. return new ParentIterator (this, true);
  591. }
  592. protected override bool MoveNextCore ()
  593. {
  594. if (Position == 0 && Node.MoveToParent ())
  595. return true;
  596. return false;
  597. }
  598. public override bool ReverseAxis {
  599. get { return true; }
  600. }
  601. }
  602. internal class ChildIterator : NodeIterator
  603. {
  604. public ChildIterator (XPathNavigator nav, XQueryContext ctx)
  605. : base (nav, ctx)
  606. {
  607. }
  608. private ChildIterator (ChildIterator other, bool cloneFlag)
  609. : base (other, true)
  610. {
  611. }
  612. public override XPathSequence Clone ()
  613. {
  614. return new ChildIterator (this, true);
  615. }
  616. protected override bool MoveNextCore ()
  617. {
  618. if (Position == 0)
  619. return Node.MoveToFirstChild ();
  620. else
  621. return Node.MoveToNext ();
  622. }
  623. }
  624. internal class FollowingSiblingIterator : NodeIterator
  625. {
  626. public FollowingSiblingIterator (XPathNavigator nav, XQueryContext ctx)
  627. : base (nav, ctx)
  628. {
  629. }
  630. private FollowingSiblingIterator (FollowingSiblingIterator other, bool cloneFlag)
  631. : base (other, true)
  632. {
  633. }
  634. public override XPathSequence Clone ()
  635. {
  636. return new FollowingSiblingIterator (this, true);
  637. }
  638. protected override bool MoveNextCore ()
  639. {
  640. return Node.MoveToNext ();
  641. }
  642. }
  643. internal class PrecedingSiblingIterator : NodeIterator
  644. {
  645. bool finished;
  646. bool started;
  647. XPathNavigator startPosition;
  648. public PrecedingSiblingIterator (XPathNavigator nav, XQueryContext ctx)
  649. : base (nav, ctx)
  650. {
  651. startPosition = Node.Clone ();
  652. }
  653. private PrecedingSiblingIterator (PrecedingSiblingIterator other, bool cloneFlag)
  654. : base (other, true)
  655. {
  656. startPosition = other.startPosition;
  657. started = other.started;
  658. finished = other.finished;
  659. }
  660. public override XPathSequence Clone ()
  661. {
  662. return new PrecedingSiblingIterator (this, true);
  663. }
  664. protected override bool MoveNextCore ()
  665. {
  666. if (finished)
  667. return false;
  668. if (!started) {
  669. started = true;
  670. Node.MoveToFirst ();
  671. } else {
  672. Node.MoveToNext ();
  673. }
  674. if (Node.ComparePosition (startPosition) == XmlNodeOrder.Same) {
  675. finished = true;
  676. return false;
  677. }
  678. else
  679. return true;
  680. }
  681. public override bool ReverseAxis {
  682. get { return true; }
  683. }
  684. }
  685. internal class AncestorIterator : NodeIterator
  686. {
  687. bool finished;
  688. ArrayList nodes = new ArrayList ();
  689. public AncestorIterator (XPathNavigator nav, XQueryContext ctx)
  690. : base (nav, ctx)
  691. {
  692. }
  693. private AncestorIterator (AncestorIterator other, bool cloneFlag)
  694. : base (other, true)
  695. {
  696. finished = other.finished;
  697. nodes = other.nodes;
  698. }
  699. public override XPathSequence Clone ()
  700. {
  701. return new AncestorIterator (this, true);
  702. }
  703. protected override bool MoveNextCore ()
  704. {
  705. if (finished)
  706. return false;
  707. if (nodes != null) {
  708. nodes = new ArrayList ();
  709. while (Node.MoveToParent () && Node.NodeType != XPathNodeType.Root)
  710. nodes.Add (Node.Clone ());
  711. nodes.Reverse ();
  712. }
  713. if (nodes.Count >= Position)
  714. return false;
  715. Node.MoveTo (nodes [Position] as XPathNavigator);
  716. return true;
  717. }
  718. public override bool ReverseAxis {
  719. get { return true; }
  720. }
  721. public override int Count {
  722. get {
  723. if (Position == 0)
  724. return base.Count;
  725. return nodes.Count;
  726. }
  727. }
  728. }
  729. internal class AncestorOrSelfIterator : NodeIterator
  730. {
  731. bool finished;
  732. ArrayList nodes = new ArrayList ();
  733. public AncestorOrSelfIterator (XPathNavigator nav, XQueryContext ctx)
  734. : base (nav, ctx)
  735. {
  736. }
  737. private AncestorOrSelfIterator (AncestorOrSelfIterator other, bool cloneFlag)
  738. : base (other, true)
  739. {
  740. finished = other.finished;
  741. nodes = other.nodes;
  742. }
  743. public override XPathSequence Clone ()
  744. {
  745. return new AncestorOrSelfIterator (this, true);
  746. }
  747. protected override bool MoveNextCore ()
  748. {
  749. if (finished)
  750. return false;
  751. if (nodes != null) {
  752. nodes = new ArrayList ();
  753. do {
  754. nodes.Add (Node.Clone ());
  755. } while (Node.MoveToParent () && Node.NodeType != XPathNodeType.Root);
  756. nodes.Reverse ();
  757. }
  758. if (nodes.Count >= Position)
  759. return false;
  760. Node.MoveTo (nodes [Position] as XPathNavigator);
  761. return true;
  762. }
  763. public override bool ReverseAxis {
  764. get { return true; }
  765. }
  766. public override int Count {
  767. get {
  768. if (Position == 0)
  769. return base.Count;
  770. return nodes.Count;
  771. }
  772. }
  773. }
  774. internal class DescendantIterator : NodeIterator
  775. {
  776. private int depth;
  777. private bool finished;
  778. public DescendantIterator (XPathNavigator nav, XQueryContext ctx)
  779. : base (nav, ctx)
  780. {
  781. }
  782. private DescendantIterator (DescendantIterator other, bool cloneFlag)
  783. : base (other, true)
  784. {
  785. finished = other.finished;
  786. depth = other.depth;
  787. }
  788. public override XPathSequence Clone ()
  789. {
  790. return new DescendantIterator (this, true);
  791. }
  792. protected override bool MoveNextCore ()
  793. {
  794. if (finished)
  795. return false;
  796. if (Node.MoveToFirstChild ()) {
  797. depth ++;
  798. return true;
  799. }
  800. while (depth != 0) {
  801. if (Node.MoveToNext ())
  802. return true;
  803. if (!Node.MoveToParent ()) // should NEVER fail!
  804. throw new XmlQueryException ("There seems some bugs on the XPathNavigator implementation class.");
  805. depth --;
  806. }
  807. finished = true;
  808. return false;
  809. }
  810. }
  811. internal class DescendantOrSelfIterator : NodeIterator
  812. {
  813. protected int depth;
  814. private bool finished;
  815. public DescendantOrSelfIterator (XPathNavigator nav, XQueryContext ctx)
  816. : base (nav, ctx)
  817. {
  818. }
  819. protected DescendantOrSelfIterator (DescendantOrSelfIterator other, bool cloneFlag)
  820. : base (other, true)
  821. {
  822. depth = other.depth;
  823. finished = other.finished;
  824. }
  825. public override XPathSequence Clone ()
  826. {
  827. return new DescendantOrSelfIterator (this, true);
  828. }
  829. protected override bool MoveNextCore ()
  830. {
  831. if (finished)
  832. return false;
  833. if (Position == 0)
  834. return true; // Self
  835. if (Node.MoveToFirstChild ()) {
  836. depth ++;
  837. return true;
  838. }
  839. while (depth != 0) {
  840. if (Node.MoveToNext ())
  841. return true;
  842. if (!Node.MoveToParent ()) // should NEVER fail!
  843. throw new XmlQueryException ("There seems some bugs on the XPathNavigator implementation class.");
  844. depth --;
  845. }
  846. finished = true;
  847. return false;
  848. }
  849. }
  850. internal class FollowingIterator : NodeIterator
  851. {
  852. private bool finished;
  853. public FollowingIterator (XPathNavigator nav, XQueryContext ctx)
  854. : base (nav, ctx)
  855. {
  856. }
  857. protected FollowingIterator (FollowingIterator other, bool cloneFlag)
  858. : base (other, true)
  859. {
  860. finished = other.finished;
  861. }
  862. public override XPathSequence Clone ()
  863. {
  864. return new FollowingIterator (this, true);
  865. }
  866. protected override bool MoveNextCore ()
  867. {
  868. if (finished)
  869. return false;
  870. if (Position == 0) {
  871. // At first, it should not iterate children.
  872. if (Node.MoveToNext ())
  873. return true;
  874. else {
  875. while (Node.MoveToParent ())
  876. if (Node.MoveToNext ())
  877. return true;
  878. }
  879. } else {
  880. if (Node.MoveToFirstChild ())
  881. return true;
  882. do {
  883. if (Node.MoveToNext ())
  884. return true;
  885. } while (Node.MoveToParent ());
  886. }
  887. finished = true;
  888. return false;
  889. }
  890. }
  891. internal class PrecedingIterator : NodeIterator
  892. {
  893. bool finished;
  894. bool started;
  895. XPathNavigator startPosition;
  896. public PrecedingIterator (XPathNavigator nav, XQueryContext ctx)
  897. : base (nav, ctx)
  898. {
  899. startPosition = Node.Clone ();
  900. }
  901. private PrecedingIterator (PrecedingIterator other, bool cloneFlag)
  902. : base (other, true)
  903. {
  904. startPosition = other.startPosition;
  905. started = other.started;
  906. finished = other.finished;
  907. }
  908. public override XPathSequence Clone ()
  909. {
  910. return new PrecedingIterator (this, true);
  911. }
  912. protected override bool MoveNextCore ()
  913. {
  914. if (finished)
  915. return false;
  916. if (!started) {
  917. started = true;
  918. Node.MoveToRoot ();
  919. }
  920. bool loop = true;
  921. while (loop) {
  922. while (!Node.MoveToFirstChild ()) {
  923. while (!Node.MoveToNext ()) {
  924. if (!Node.MoveToParent ()) { // Should not finish, at least before startPosition.
  925. finished = true;
  926. return false;
  927. }
  928. }
  929. break;
  930. }
  931. if (Node.IsDescendant (startPosition))
  932. continue;
  933. loop = false;
  934. break;
  935. }
  936. if (Node.ComparePosition (startPosition) != XmlNodeOrder.Before) {
  937. // Note that if _nav contains only 1 node, it won't be Same.
  938. finished = true;
  939. return false;
  940. }
  941. else
  942. return true;
  943. }
  944. public override bool ReverseAxis {
  945. get { return true; }
  946. }
  947. }
  948. internal class NamespaceIterator : NodeIterator
  949. {
  950. public NamespaceIterator (XPathNavigator nav, XQueryContext ctx)
  951. : base (nav, ctx)
  952. {
  953. }
  954. private NamespaceIterator (NamespaceIterator other, bool cloneFlag)
  955. : base (other, true)
  956. {
  957. }
  958. public override XPathSequence Clone ()
  959. {
  960. return new NamespaceIterator (this, true);
  961. }
  962. protected override bool MoveNextCore ()
  963. {
  964. if (Position == 0) {
  965. if (Node.MoveToFirstNamespace ())
  966. return true;
  967. }
  968. else if (Node.MoveToNextNamespace ())
  969. return true;
  970. return false;
  971. }
  972. public override bool ReverseAxis { get { return true; } }
  973. }
  974. internal class AttributeIterator : NodeIterator
  975. {
  976. public AttributeIterator (XPathNavigator nav, XQueryContext ctx)
  977. : base (nav, ctx)
  978. {
  979. }
  980. private AttributeIterator (AttributeIterator other, bool cloneFlag)
  981. : base (other, true)
  982. {
  983. }
  984. public override XPathSequence Clone ()
  985. {
  986. return new AttributeIterator (this, true);
  987. }
  988. protected override bool MoveNextCore ()
  989. {
  990. if (Position == 0) {
  991. if (Node.MoveToFirstAttribute ())
  992. return true;
  993. }
  994. else if (Node.MoveToNextAttribute ())
  995. return true;
  996. return false;
  997. }
  998. }
  999. // </copy>
  1000. internal class ExprSequenceIterator : XPathSequence
  1001. {
  1002. XPathSequence contextSequence;
  1003. XPathSequence iter;
  1004. ExprSequence expr;
  1005. int currentExprIndex;
  1006. public ExprSequenceIterator (XPathSequence iter, ExprSequence expr)
  1007. : base (iter.Context)
  1008. {
  1009. contextSequence = iter;
  1010. this.expr = expr;
  1011. }
  1012. private ExprSequenceIterator (ExprSequenceIterator other)
  1013. : base (other)
  1014. {
  1015. if (other.iter != null)
  1016. iter = other.iter.Clone ();
  1017. expr = other.expr;
  1018. contextSequence = other.contextSequence;
  1019. currentExprIndex = other.currentExprIndex;
  1020. }
  1021. public override XPathSequence Clone ()
  1022. {
  1023. return new ExprSequenceIterator (this);
  1024. }
  1025. protected override bool MoveNextCore ()
  1026. {
  1027. if (iter != null && iter.MoveNext ())
  1028. return true;
  1029. while (currentExprIndex < expr.Count) {
  1030. iter = expr [currentExprIndex++].Evaluate (contextSequence);
  1031. if (iter.MoveNext ())
  1032. return true;
  1033. }
  1034. return false;
  1035. }
  1036. public override XPathItem CurrentCore {
  1037. get { return iter.Current; }
  1038. }
  1039. }
  1040. // FLWOR - Order By
  1041. internal class FLWORIterator : XPathSequence
  1042. {
  1043. XPathSequence contextSequence;
  1044. FLWORExpr expr;
  1045. ArrayList forStack = new ArrayList ();
  1046. IEnumerator en;
  1047. bool finished;
  1048. public FLWORIterator (XPathSequence iter, FLWORExpr expr)
  1049. : base (iter.Context)
  1050. {
  1051. this.contextSequence = iter;
  1052. this.expr = expr;
  1053. }
  1054. private FLWORIterator (FLWORIterator other)
  1055. : base (other)
  1056. {
  1057. contextSequence = other.contextSequence;
  1058. expr = other.expr;
  1059. forStack = other.forStack.Clone () as ArrayList;
  1060. if (en != null)
  1061. en = ((ICloneable) other.en).Clone () as IEnumerator;
  1062. finished = other.finished;
  1063. }
  1064. public override XPathSequence Clone ()
  1065. {
  1066. return new FLWORIterator (this);
  1067. }
  1068. protected override bool MoveNextCore ()
  1069. {
  1070. if (en == null)
  1071. en = GetEnumerator ();
  1072. return en.MoveNext ();
  1073. }
  1074. public override IEnumerator GetEnumerator ()
  1075. {
  1076. return EvaluateRemainingForLet (0);
  1077. }
  1078. private IEnumerator EvaluateRemainingForLet (int flcPosition)
  1079. {
  1080. // Prepare iteration stack
  1081. if (flcPosition < expr.ForLetClauses.Count) {
  1082. IEnumerator items = EvaluateRemainingSingleItem (flcPosition, 0);
  1083. while (items.MoveNext ())
  1084. yield return items.Current;
  1085. } else {
  1086. bool passedFilter = expr.WhereClause == null;
  1087. if (!passedFilter)
  1088. passedFilter = expr.WhereClause.EvaluateAsBoolean (contextSequence);
  1089. if (passedFilter) {
  1090. IEnumerator ie = expr.ReturnExpr.Evaluate (contextSequence).GetEnumerator ();
  1091. while (ie.MoveNext ())
  1092. yield return (XPathItem) ie.Current;
  1093. }
  1094. }
  1095. }
  1096. private IEnumerator EvaluateRemainingSingleItem (int flcPosition, int singlePosition)
  1097. {
  1098. if (singlePosition < expr.ForLetClauses [flcPosition].Count) {
  1099. ForLetSingleBody sb = expr.ForLetClauses [flcPosition] [singlePosition];
  1100. ForSingleBody fsb = sb as ForSingleBody;
  1101. if (fsb != null) {
  1102. XPathSequence backup = contextSequence;
  1103. contextSequence = fsb.Expression.Evaluate (Context.CurrentSequence);
  1104. Context.ContextManager.PushCurrentSequence (contextSequence);
  1105. while (contextSequence.MoveNext ()) {
  1106. XPathItem forItem = (XPathItem) contextSequence.Current;
  1107. Context.PushVariable (fsb.PositionalVar, contextSequence.Position);
  1108. Context.PushVariable (sb.VarName, forItem);
  1109. // recurse here (including following bindings)
  1110. IEnumerator items = EvaluateRemainingSingleItem (flcPosition, singlePosition + 1);
  1111. while (items.MoveNext ())
  1112. yield return (XPathItem) items.Current;
  1113. Context.PopVariable ();
  1114. Context.PopVariable ();
  1115. }
  1116. Context.ContextManager.PopCurrentSequence ();
  1117. contextSequence = backup;
  1118. } else {
  1119. Context.PushVariable (sb.VarName, sb.Expression.Evaluate (contextSequence));
  1120. // recurse here (including following bindings)
  1121. IEnumerator items = EvaluateRemainingSingleItem (flcPosition, singlePosition + 1);
  1122. while (items.MoveNext ())
  1123. yield return (XPathItem) items.Current;
  1124. Context.PopVariable ();
  1125. }
  1126. } else {
  1127. // evaluate next binding
  1128. IEnumerator items = EvaluateRemainingForLet (flcPosition + 1);
  1129. while (items.MoveNext ())
  1130. yield return (XPathItem) items.Current;
  1131. }
  1132. }
  1133. public override XPathItem CurrentCore {
  1134. get { return (XPathItem) en.Current; }
  1135. }
  1136. }
  1137. internal class GroupIterator : XPathSequence
  1138. {
  1139. GroupExpr expr;
  1140. XPathSequence lseq;
  1141. XPathSequence rseq;
  1142. bool started;
  1143. bool left;
  1144. bool leftFinished;
  1145. bool rightFinished;
  1146. public GroupIterator (XPathSequence iter, GroupExpr expr)
  1147. : base (iter.Context)
  1148. {
  1149. this.expr = expr;
  1150. left = true;
  1151. lseq = expr.Left.EvaluateOrdered (iter);
  1152. rseq = expr.Right.EvaluateOrdered (iter);
  1153. }
  1154. private GroupIterator (GroupIterator other)
  1155. : base (other)
  1156. {
  1157. this.expr = other.expr;
  1158. this.started = other.started;
  1159. this.left = other.left;
  1160. this.leftFinished = other.leftFinished;
  1161. this.rightFinished = other.rightFinished;
  1162. this.lseq = other.lseq.Clone ();
  1163. this.rseq = other.rseq.Clone ();
  1164. }
  1165. public override XPathSequence Clone ()
  1166. {
  1167. return new GroupIterator (this);
  1168. }
  1169. protected override bool MoveNextCore ()
  1170. {
  1171. if (leftFinished && rightFinished)
  1172. return false;
  1173. bool proceeded = false;
  1174. if (started) {
  1175. if (left) {
  1176. if (!leftFinished && lseq.MoveNext ())
  1177. proceeded = true;
  1178. else
  1179. leftFinished = true;
  1180. } else {
  1181. if (rightFinished && rseq.MoveNext ())
  1182. proceeded = true;
  1183. else
  1184. rightFinished = true;
  1185. }
  1186. } else {
  1187. started = true;
  1188. if (!lseq.MoveNext ()) {
  1189. leftFinished = true;
  1190. if (!rseq.MoveNext ()) {
  1191. rightFinished = true;
  1192. return false;
  1193. }
  1194. left = false;
  1195. return true;
  1196. }
  1197. proceeded = true;
  1198. if (!rseq.MoveNext ()) {
  1199. rightFinished = true;
  1200. return true;
  1201. }
  1202. }
  1203. if (!proceeded) {
  1204. if (expr.AggregationType == AggregationType.Intersect)
  1205. return false;
  1206. left = !leftFinished;
  1207. return !leftFinished || !rightFinished;
  1208. }
  1209. XPathNavigator lnav = lseq.Current as XPathNavigator;
  1210. XPathNavigator rnav = rseq.Current as XPathNavigator;
  1211. if (lnav == null || rnav == null)
  1212. throw new XmlQueryException ("XP0006: Evaluation against union, intersect, except expressions must result in nodes.");
  1213. XmlNodeOrder order = lnav.ComparePosition (rnav);
  1214. switch (order) {
  1215. case XmlNodeOrder.Same:
  1216. switch (expr.AggregationType) {
  1217. case AggregationType.Union:
  1218. left = false;
  1219. if (!lseq.MoveNext ())
  1220. leftFinished = true;
  1221. return true;
  1222. case AggregationType.Intersect:
  1223. return true;
  1224. case AggregationType.Except:
  1225. default:
  1226. return MoveNext ();
  1227. }
  1228. case XmlNodeOrder.Before:
  1229. left = true;
  1230. if (expr.AggregationType == AggregationType.Intersect)
  1231. return MoveNext ();
  1232. return true;
  1233. default: // After, Unknown
  1234. left = false;
  1235. if (expr.AggregationType == AggregationType.Intersect)
  1236. return MoveNext ();
  1237. return true;
  1238. }
  1239. }
  1240. public override XPathItem CurrentCore {
  1241. get { return left ? lseq.Current : rseq.Current; }
  1242. }
  1243. }
  1244. internal class AtomizingIterator : XPathSequence
  1245. {
  1246. XPathSequence iter;
  1247. public AtomizingIterator (XPathSequence iter)
  1248. : base (iter.Context)
  1249. {
  1250. this.iter = iter;
  1251. }
  1252. private AtomizingIterator (AtomizingIterator other)
  1253. : base (other)
  1254. {
  1255. iter = other.iter.Clone ();
  1256. }
  1257. public override XPathSequence Clone ()
  1258. {
  1259. return new AtomizingIterator (this);
  1260. }
  1261. protected override bool MoveNextCore ()
  1262. {
  1263. return iter.MoveNext ();
  1264. }
  1265. public override XPathItem CurrentCore {
  1266. get {
  1267. XPathNavigator nav = iter.Current as XPathNavigator;
  1268. if (nav == null)
  1269. return (XPathAtomicValue) iter.Current;
  1270. if (nav.SchemaInfo != null)
  1271. return new XPathAtomicValue (
  1272. nav.TypedValue,
  1273. nav.SchemaInfo.SchemaType);
  1274. else
  1275. return new XPathAtomicValue (nav.Value, null);
  1276. }
  1277. }
  1278. }
  1279. internal class ConvertingIterator : XPathSequence
  1280. {
  1281. XPathSequence iter;
  1282. SequenceType type;
  1283. public ConvertingIterator (XPathSequence iter, SequenceType type)
  1284. : base (iter.Context)
  1285. {
  1286. this.iter = iter;
  1287. this.type = type;
  1288. }
  1289. private ConvertingIterator (ConvertingIterator other)
  1290. : base (other)
  1291. {
  1292. iter = other.iter.Clone ();
  1293. type = other.type;
  1294. }
  1295. public override XPathSequence Clone ()
  1296. {
  1297. return new ConvertingIterator (this);
  1298. }
  1299. protected override bool MoveNextCore ()
  1300. {
  1301. return iter.MoveNext ();
  1302. }
  1303. public override XPathItem CurrentCore {
  1304. get { return type.Convert (iter.Current); }
  1305. }
  1306. }
  1307. internal class TracingIterator : XPathSequence
  1308. {
  1309. XPathSequence iter;
  1310. string format;
  1311. public TracingIterator (XPathSequence iter, string format)
  1312. : base (iter.Context)
  1313. {
  1314. this.iter = iter;
  1315. this.format = format;
  1316. }
  1317. private TracingIterator (TracingIterator other)
  1318. : base (other)
  1319. {
  1320. iter = other.iter.Clone ();
  1321. format = other.format;
  1322. }
  1323. public override XPathSequence Clone ()
  1324. {
  1325. return new TracingIterator (this);
  1326. }
  1327. protected override bool MoveNextCore ()
  1328. {
  1329. if (!iter.MoveNext ())
  1330. return false;
  1331. // FIXME: use OnMessageEvent
  1332. string output = String.Format (format, iter.Current.TypedValue);
  1333. Context.StaticContext.OnMessageEvent (iter.Current, new TraceEventArgs (output));
  1334. return true;
  1335. }
  1336. public override XPathItem CurrentCore {
  1337. get { return iter.Current; }
  1338. }
  1339. internal class TraceEventArgs : QueryEventArgs
  1340. {
  1341. string message;
  1342. internal TraceEventArgs (string message)
  1343. {
  1344. this.message = message;
  1345. }
  1346. public override string Message {
  1347. get { return message; }
  1348. }
  1349. }
  1350. }
  1351. internal class ListIterator : XPathSequence
  1352. {
  1353. IList list;
  1354. public ListIterator (XPathSequence iter, IList list)
  1355. : base (iter.Context)
  1356. {
  1357. if (list is ICloneable)
  1358. this.list = list;
  1359. else
  1360. throw new InvalidOperationException (String.Format ("XQuery internal error: target list is not cloneable. List is {0}.", list != null ? list.GetType ().ToString () : "null argument"));
  1361. }
  1362. private ListIterator (ListIterator other)
  1363. : base (other)
  1364. {
  1365. this.list = (IList) ((ICloneable) other.list).Clone ();
  1366. }
  1367. public override XPathSequence Clone ()
  1368. {
  1369. return new ListIterator (this);
  1370. }
  1371. protected override bool MoveNextCore ()
  1372. {
  1373. return (Position < list.Count);
  1374. }
  1375. public override XPathItem CurrentCore {
  1376. get { return (XPathItem) list [Position - 1]; }
  1377. }
  1378. }
  1379. internal class EnumeratorIterator : XPathSequence
  1380. {
  1381. IEnumerator list;
  1382. public EnumeratorIterator (XQueryContext ctx, IEnumerable en)
  1383. : base (ctx)
  1384. {
  1385. list = en.GetEnumerator ();
  1386. if (list is ICloneable)
  1387. this.list = list;
  1388. else
  1389. throw new InvalidOperationException (String.Format ("XQuery internal error: target list's enumerator is not cloneable. List is {0}.", en != null ? en.GetType ().ToString () : "null argument"));
  1390. }
  1391. private EnumeratorIterator (EnumeratorIterator other)
  1392. : base (other)
  1393. {
  1394. this.list = (IEnumerator) ((ICloneable) other.list).Clone ();
  1395. }
  1396. public override XPathSequence Clone ()
  1397. {
  1398. return new EnumeratorIterator (this);
  1399. }
  1400. protected override bool MoveNextCore ()
  1401. {
  1402. return list.MoveNext ();
  1403. }
  1404. public override XPathItem CurrentCore {
  1405. get { return (XPathItem) list.Current; }
  1406. }
  1407. }
  1408. }
  1409. #endif