DefaultContext.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. //
  2. // System.Xml.XPath.DefaultContext & support classes
  3. //
  4. // Author:
  5. // Piers Haken ([email protected])
  6. //
  7. // (C) 2002 Piers Haken
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Xml;
  12. using System.Xml.XPath;
  13. using System.Xml.Xsl;
  14. using System.Text;
  15. namespace System.Xml.XPath
  16. {
  17. internal class XPathFunctions
  18. {
  19. public static bool ToBoolean (object arg)
  20. {
  21. if (arg == null)
  22. throw new ArgumentNullException ();
  23. if (arg is bool)
  24. return (bool) arg;
  25. if (arg is double)
  26. {
  27. double dArg = (double) arg;
  28. return (dArg != 0.0 && !double.IsNaN (dArg));
  29. }
  30. if (arg is string)
  31. return ((string) arg).Length != 0;
  32. if (arg is BaseIterator)
  33. {
  34. BaseIterator iter = (BaseIterator) arg;
  35. return iter.MoveNext ();
  36. }
  37. throw new ArgumentException ();
  38. }
  39. [MonoTODO]
  40. public static string ToString (object arg)
  41. {
  42. if (arg == null)
  43. throw new ArgumentNullException ();
  44. if (arg is string)
  45. return (string) arg;
  46. if (arg is bool)
  47. return ((bool) arg) ? "true" : "false";
  48. if (arg is double)
  49. return ((double) arg).ToString ("R", System.Globalization.NumberFormatInfo.InvariantInfo);
  50. if (arg is BaseIterator)
  51. {
  52. BaseIterator iter = (BaseIterator) arg;
  53. if (!iter.MoveNext ())
  54. return "";
  55. return iter.Current.Value;
  56. }
  57. throw new ArgumentException ();
  58. }
  59. [MonoTODO]
  60. public static double ToNumber (object arg)
  61. {
  62. if (arg == null)
  63. throw new ArgumentNullException ();
  64. if (arg is string)
  65. {
  66. try
  67. {
  68. return XmlConvert.ToDouble ((string) arg); // TODO: spec? convert string to number
  69. }
  70. catch (System.FormatException)
  71. {
  72. return double.NaN;
  73. }
  74. }
  75. if (arg is BaseIterator)
  76. arg = ToString (arg); // follow on
  77. if (arg is double)
  78. return (double) arg;
  79. if (arg is bool)
  80. return Convert.ToDouble ((bool) arg);
  81. throw new ArgumentException ();
  82. }
  83. public static double ToNumber (string arg)
  84. {
  85. try {
  86. return XmlConvert.ToDouble ((string) arg); // TODO: spec? convert string to number
  87. } catch (System.OverflowException) {
  88. return double.NaN;
  89. } catch (System.FormatException) {
  90. return double.NaN;
  91. }
  92. }
  93. }
  94. internal abstract class XPathFunction : Expression
  95. {
  96. public XPathFunction (FunctionArguments args) {}
  97. }
  98. internal class XPathFunctionLast : XPathFunction
  99. {
  100. public XPathFunctionLast (FunctionArguments args) : base (args)
  101. {
  102. if (args != null)
  103. throw new XPathException ("last takes 0 args");
  104. }
  105. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  106. public override object Evaluate (BaseIterator iter)
  107. {
  108. return (double) iter.Count;
  109. }
  110. }
  111. internal class XPathFunctionPosition : XPathFunction
  112. {
  113. public XPathFunctionPosition (FunctionArguments args) : base (args)
  114. {
  115. if (args != null)
  116. throw new XPathException ("position takes 0 args");
  117. }
  118. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  119. public override object Evaluate (BaseIterator iter)
  120. {
  121. return (double) iter.CurrentPosition;
  122. }
  123. }
  124. internal class XPathFunctionCount : XPathFunction
  125. {
  126. Expression arg0;
  127. public XPathFunctionCount (FunctionArguments args) : base (args)
  128. {
  129. if (args == null || args.Tail != null)
  130. throw new XPathException ("count takes 1 arg");
  131. arg0 = args.Arg;
  132. }
  133. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  134. public override object Evaluate (BaseIterator iter)
  135. {
  136. return (double) arg0.EvaluateNodeSet (iter).Count;
  137. }
  138. public override bool EvaluateBoolean (BaseIterator iter)
  139. {
  140. if (arg0.GetReturnType (iter) == XPathResultType.NodeSet)
  141. return arg0.EvaluateBoolean (iter);
  142. return arg0.EvaluateNodeSet (iter).MoveNext ();
  143. }
  144. }
  145. internal class XPathFunctionId : XPathFunction
  146. {
  147. Expression arg0;
  148. public XPathFunctionId (FunctionArguments args) : base (args)
  149. {
  150. if (args == null || args.Tail != null)
  151. throw new XPathException ("id takes 1 arg");
  152. arg0 = args.Arg;
  153. }
  154. public Expression Id { get { return arg0; } }
  155. private static char [] rgchWhitespace = {' ', '\t', '\r', '\n'};
  156. public override XPathResultType ReturnType { get { return XPathResultType.NodeSet; }}
  157. [MonoTODO]
  158. public override object Evaluate (BaseIterator iter)
  159. {
  160. String strArgs;
  161. object val = arg0.Evaluate (iter);
  162. BaseIterator valItr = val as BaseIterator;
  163. if (valItr != null)
  164. {
  165. strArgs = "";
  166. while (valItr.MoveNext ())
  167. strArgs += valItr.Current.Value + " ";
  168. }
  169. else
  170. strArgs = XPathFunctions.ToString (val);
  171. XPathNavigator n = iter.Current.Clone ();
  172. ArrayList rgNodes = new ArrayList ();
  173. foreach (string strArg in strArgs.Split (rgchWhitespace))
  174. {
  175. if (n.MoveToId (strArg)) {
  176. rgNodes.Add (n.Clone ());
  177. }
  178. }
  179. rgNodes.Sort (XPathNavigatorComparer.Instance);
  180. return new EnumeratorIterator (iter, rgNodes.GetEnumerator ());
  181. }
  182. }
  183. internal class XPathFunctionLocalName : XPathFunction
  184. {
  185. Expression arg0;
  186. public XPathFunctionLocalName (FunctionArguments args) : base (args)
  187. {
  188. if (args != null) {
  189. arg0 = args.Arg;
  190. if (args.Tail != null)
  191. throw new XPathException ("local-name takes 1 or zero args");
  192. }
  193. }
  194. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  195. public override object Evaluate (BaseIterator iter)
  196. {
  197. if (arg0 == null)
  198. return iter.Current.LocalName;
  199. BaseIterator argNs = arg0.EvaluateNodeSet (iter);
  200. if (argNs == null || !argNs.MoveNext ())
  201. return "";
  202. return argNs.Current.LocalName;
  203. }
  204. }
  205. internal class XPathFunctionNamespaceUri : XPathFunction
  206. {
  207. Expression arg0;
  208. public XPathFunctionNamespaceUri (FunctionArguments args) : base (args)
  209. {
  210. if (args != null) {
  211. arg0 = args.Arg;
  212. if (args.Tail != null)
  213. throw new XPathException ("namespace-uri takes 1 or zero args");
  214. }
  215. }
  216. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  217. public override object Evaluate (BaseIterator iter)
  218. {
  219. if (arg0 == null)
  220. return iter.Current.NamespaceURI;
  221. BaseIterator argNs = arg0.EvaluateNodeSet (iter);
  222. if (argNs == null || !argNs.MoveNext ())
  223. return "";
  224. return argNs.Current.NamespaceURI;
  225. }
  226. }
  227. internal class XPathFunctionName : XPathFunction
  228. {
  229. Expression arg0;
  230. public XPathFunctionName (FunctionArguments args) : base (args)
  231. {
  232. if (args != null) {
  233. arg0 = args.Arg;
  234. if (args.Tail != null)
  235. throw new XPathException ("name takes 1 or zero args");
  236. }
  237. }
  238. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  239. public override object Evaluate (BaseIterator iter)
  240. {
  241. if (arg0 == null)
  242. return iter.Current.Name;
  243. BaseIterator argNs = arg0.EvaluateNodeSet (iter);
  244. if (argNs == null || !argNs.MoveNext ())
  245. return "";
  246. return argNs.Current.Name;
  247. }
  248. }
  249. internal class XPathFunctionString : XPathFunction
  250. {
  251. Expression arg0;
  252. public XPathFunctionString (FunctionArguments args) : base (args)
  253. {
  254. if (args != null) {
  255. arg0 = args.Arg;
  256. if (args.Tail != null)
  257. throw new XPathException ("boolean takes 1 or zero args");
  258. }
  259. }
  260. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  261. public override object Evaluate (BaseIterator iter)
  262. {
  263. if (arg0 == null)
  264. return iter.Current.Value;
  265. return arg0.EvaluateString (iter);
  266. }
  267. }
  268. internal class XPathFunctionConcat : XPathFunction
  269. {
  270. ArrayList rgs;
  271. public XPathFunctionConcat (FunctionArguments args) : base (args)
  272. {
  273. if (args == null || args.Tail == null)
  274. throw new XPathException ("concat takes 2 or more args");
  275. args.ToArrayList (rgs = new ArrayList ());
  276. }
  277. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  278. public override object Evaluate (BaseIterator iter)
  279. {
  280. StringBuilder sb = new StringBuilder ();
  281. int len = rgs.Count;
  282. for (int i = 0; i < len; i++)
  283. sb.Append (((Expression)rgs[i]).EvaluateString (iter));
  284. return sb.ToString ();
  285. }
  286. }
  287. internal class XPathFunctionStartsWith : XPathFunction
  288. {
  289. Expression arg0, arg1;
  290. public XPathFunctionStartsWith (FunctionArguments args) : base (args)
  291. {
  292. if (args == null || args.Tail == null || args.Tail.Tail != null)
  293. throw new XPathException ("starts-with takes 2 args");
  294. arg0 = args.Arg;
  295. arg1 = args.Tail.Arg;
  296. }
  297. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  298. public override object Evaluate (BaseIterator iter)
  299. {
  300. return arg0.EvaluateString (iter).StartsWith (arg1.EvaluateString (iter));
  301. }
  302. }
  303. internal class XPathFunctionContains : XPathFunction
  304. {
  305. Expression arg0, arg1;
  306. public XPathFunctionContains (FunctionArguments args) : base (args)
  307. {
  308. if (args == null || args.Tail == null || args.Tail.Tail != null)
  309. throw new XPathException ("contains takes 2 args");
  310. arg0 = args.Arg;
  311. arg1 = args.Tail.Arg;
  312. }
  313. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  314. public override object Evaluate (BaseIterator iter)
  315. {
  316. return arg0.EvaluateString (iter).IndexOf (arg1.EvaluateString (iter)) != -1;
  317. }
  318. }
  319. internal class XPathFunctionSubstringBefore : XPathFunction
  320. {
  321. Expression arg0, arg1;
  322. public XPathFunctionSubstringBefore (FunctionArguments args) : base (args)
  323. {
  324. if (args == null || args.Tail == null || args.Tail.Tail != null)
  325. throw new XPathException ("substring-before takes 2 args");
  326. arg0 = args.Arg;
  327. arg1 = args.Tail.Arg;
  328. }
  329. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  330. public override object Evaluate (BaseIterator iter)
  331. {
  332. string str1 = arg0.EvaluateString (iter);
  333. string str2 = arg1.EvaluateString (iter);
  334. int ich = str1.IndexOf (str2);
  335. if (ich <= 0)
  336. return "";
  337. return str1.Substring (0, ich);
  338. }
  339. }
  340. internal class XPathFunctionSubstringAfter : XPathFunction
  341. {
  342. Expression arg0, arg1;
  343. public XPathFunctionSubstringAfter (FunctionArguments args) : base (args)
  344. {
  345. if (args == null || args.Tail == null || args.Tail.Tail != null)
  346. throw new XPathException ("substring-after takes 2 args");
  347. arg0 = args.Arg;
  348. arg1 = args.Tail.Arg;
  349. }
  350. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  351. public override object Evaluate (BaseIterator iter)
  352. {
  353. string str1 = arg0.EvaluateString (iter);
  354. string str2 = arg1.EvaluateString (iter);
  355. int ich = str1.IndexOf (str2);
  356. if (ich < 0)
  357. return "";
  358. return str1.Substring (ich + str2.Length);
  359. }
  360. }
  361. internal class XPathFunctionSubstring : XPathFunction
  362. {
  363. Expression arg0, arg1, arg2;
  364. public XPathFunctionSubstring (FunctionArguments args) : base (args)
  365. {
  366. if (args == null || args.Tail == null || (args.Tail.Tail != null && args.Tail.Tail.Tail != null))
  367. throw new XPathException ("substring takes 2 or 3 args");
  368. arg0 = args.Arg;
  369. arg1 = args.Tail.Arg;
  370. if (args.Tail.Tail != null)
  371. arg2= args.Tail.Tail.Arg;
  372. }
  373. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  374. [MonoTODO]
  375. public override object Evaluate (BaseIterator iter)
  376. {
  377. // TODO: check this, what the hell were they smoking?
  378. string str = arg0.EvaluateString (iter);
  379. double ich = Math.Round (arg1.EvaluateNumber (iter)) - 1;
  380. if (Double.IsNaN (ich) || ich >= (double) str.Length)
  381. return "";
  382. if (arg2 == null)
  383. {
  384. if (ich < 0)
  385. ich = 0.0;
  386. return str.Substring ((int) ich);
  387. }
  388. else
  389. {
  390. double cch = Math.Round (arg2.EvaluateNumber (iter));
  391. if (Double.IsNaN (cch))
  392. return "";
  393. if (ich < 0.0 || cch < 0.0)
  394. {
  395. cch = ich + cch;
  396. if (cch <= 0.0)
  397. return "";
  398. ich = 0.0;
  399. }
  400. double cchMax = (double) str.Length - ich;
  401. if (cch > cchMax)
  402. cch = cchMax;
  403. return str.Substring ((int) ich, (int) cch);
  404. }
  405. }
  406. }
  407. internal class XPathFunctionStringLength : XPathFunction
  408. {
  409. Expression arg0;
  410. public XPathFunctionStringLength (FunctionArguments args) : base (args)
  411. {
  412. if (args != null) {
  413. arg0 = args.Arg;
  414. if (args.Tail != null)
  415. throw new XPathException ("string-length takes 1 or zero args");
  416. }
  417. }
  418. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  419. public override object Evaluate (BaseIterator iter)
  420. {
  421. string str;
  422. if (arg0 != null)
  423. str = arg0.EvaluateString (iter);
  424. else
  425. str = iter.Current.Value;
  426. return (double) str.Length;
  427. }
  428. }
  429. internal class XPathFunctionNormalizeSpace : XPathFunction
  430. {
  431. Expression arg0;
  432. public XPathFunctionNormalizeSpace (FunctionArguments args) : base (args)
  433. {
  434. if (args != null) {
  435. arg0 = args.Arg;
  436. if (args.Tail != null)
  437. throw new XPathException ("string-length takes 1 or zero args");
  438. }
  439. }
  440. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  441. [MonoTODO]
  442. public override object Evaluate (BaseIterator iter)
  443. {
  444. string str;
  445. if (arg0 == null)
  446. str = arg0.EvaluateString (iter);
  447. else
  448. str = iter.Current.Value;
  449. System.Text.StringBuilder sb = new System.Text.StringBuilder ();
  450. bool fSpace = false;
  451. foreach (char ch in str)
  452. {
  453. if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n')
  454. {
  455. fSpace = true;
  456. }
  457. else
  458. {
  459. if (fSpace)
  460. {
  461. fSpace = false;
  462. if (sb.Length > 0)
  463. sb.Append (' ');
  464. }
  465. sb.Append (ch);
  466. }
  467. }
  468. return sb.ToString ();
  469. }
  470. }
  471. internal class XPathFunctionTranslate : XPathFunction
  472. {
  473. Expression arg0, arg1, arg2;
  474. public XPathFunctionTranslate (FunctionArguments args) : base (args)
  475. {
  476. if (args == null || args.Tail == null || args.Tail.Tail == null || args.Tail.Tail.Tail != null)
  477. throw new XPathException ("translate takes 3 args");
  478. arg0 = args.Arg;
  479. arg1 = args.Tail.Arg;
  480. arg2= args.Tail.Tail.Arg;
  481. }
  482. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  483. public override object Evaluate (BaseIterator iter)
  484. {
  485. string s0 = arg0.EvaluateString (iter);
  486. string s1 = arg1.EvaluateString (iter);
  487. string s2 = arg2.EvaluateString (iter);
  488. StringBuilder ret = new StringBuilder (s0.Length);
  489. int pos = 0, len = s0.Length, s2len = s2.Length;
  490. while (pos < len) {
  491. int idx = s1.IndexOf (s0 [pos]);
  492. if (idx != -1) {
  493. if (idx < s2len)
  494. ret.Append (s2 [idx]);
  495. }
  496. else
  497. ret.Append (s0 [pos]);
  498. pos++;
  499. }
  500. return ret.ToString ();
  501. }
  502. }
  503. internal class XPathFunctionBoolean : XPathFunction
  504. {
  505. Expression arg0;
  506. public XPathFunctionBoolean (FunctionArguments args) : base (args)
  507. {
  508. if (args != null) {
  509. arg0 = args.Arg;
  510. if (args.Tail != null)
  511. throw new XPathException ("boolean takes 1 or zero args");
  512. }
  513. }
  514. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  515. public override object Evaluate (BaseIterator iter)
  516. {
  517. if (arg0 == null)
  518. return XPathFunctions.ToBoolean (iter.Current.Value);
  519. return arg0.EvaluateBoolean (iter);
  520. }
  521. }
  522. internal class XPathFunctionNot : XPathFunction
  523. {
  524. Expression arg0;
  525. public XPathFunctionNot (FunctionArguments args) : base (args)
  526. {
  527. if (args == null || args.Tail != null)
  528. throw new XPathException ("not takes one arg");
  529. arg0 = args.Arg;
  530. }
  531. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  532. public override object Evaluate (BaseIterator iter)
  533. {
  534. return !arg0.EvaluateBoolean (iter);
  535. }
  536. }
  537. internal class XPathFunctionTrue : XPathFunction
  538. {
  539. public XPathFunctionTrue (FunctionArguments args) : base (args)
  540. {
  541. if (args != null)
  542. throw new XPathException ("true takes 0 args");
  543. }
  544. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  545. public override object Evaluate (BaseIterator iter)
  546. {
  547. return true;
  548. }
  549. }
  550. internal class XPathFunctionFalse : XPathFunction
  551. {
  552. public XPathFunctionFalse (FunctionArguments args) : base (args)
  553. {
  554. if (args != null)
  555. throw new XPathException ("false takes 0 args");
  556. }
  557. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  558. public override object Evaluate (BaseIterator iter)
  559. {
  560. return false;
  561. }
  562. }
  563. internal class XPathFunctionLang : XPathFunction
  564. {
  565. Expression arg0;
  566. public XPathFunctionLang (FunctionArguments args) : base (args)
  567. {
  568. if (args == null || args.Tail != null)
  569. throw new XPathException ("lang takes one arg");
  570. arg0 = args.Arg;
  571. }
  572. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  573. public override object Evaluate (BaseIterator iter)
  574. {
  575. string lang = arg0.EvaluateString (iter).ToLower ();
  576. string actualLang = iter.Current.XmlLang.ToLower ();
  577. return lang == actualLang || lang == (actualLang.Split ('-')[0]);
  578. }
  579. }
  580. internal class XPathFunctionNumber : XPathFunction
  581. {
  582. Expression arg0;
  583. public XPathFunctionNumber (FunctionArguments args) : base (args)
  584. {
  585. if (args != null) {
  586. arg0 = args.Arg;
  587. if (args.Tail != null)
  588. throw new XPathException ("number takes 1 or zero args");
  589. }
  590. }
  591. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  592. public override object Evaluate (BaseIterator iter)
  593. {
  594. if (arg0 == null)
  595. return XPathFunctions.ToNumber (iter.Current.Value);
  596. return arg0.EvaluateNumber (iter);
  597. }
  598. }
  599. internal class XPathFunctionSum : XPathFunction
  600. {
  601. Expression arg0;
  602. public XPathFunctionSum (FunctionArguments args) : base (args)
  603. {
  604. if (args == null || args.Tail != null)
  605. throw new XPathException ("sum takes one arg");
  606. arg0 = args.Arg;
  607. }
  608. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  609. public override object Evaluate (BaseIterator iter)
  610. {
  611. XPathNodeIterator itr = arg0.EvaluateNodeSet (iter);
  612. double sum = 0;
  613. while (itr.MoveNext ())
  614. sum += XPathFunctions.ToNumber (itr.Current.Value);
  615. return sum;
  616. }
  617. }
  618. internal class XPathFunctionFloor : XPathFunction
  619. {
  620. Expression arg0;
  621. public XPathFunctionFloor (FunctionArguments args) : base (args)
  622. {
  623. if (args == null || args.Tail != null)
  624. throw new XPathException ("floor takes one arg");
  625. arg0 = args.Arg;
  626. }
  627. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  628. public override object Evaluate (BaseIterator iter)
  629. {
  630. return Math.Floor (arg0.EvaluateNumber (iter));
  631. }
  632. }
  633. internal class XPathFunctionCeil : XPathFunction
  634. {
  635. Expression arg0;
  636. public XPathFunctionCeil (FunctionArguments args) : base (args)
  637. {
  638. if (args == null || args.Tail != null)
  639. throw new XPathException ("ceil takes one arg");
  640. arg0 = args.Arg;
  641. }
  642. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  643. public override object Evaluate (BaseIterator iter)
  644. {
  645. return Math.Ceiling (arg0.EvaluateNumber (iter));
  646. }
  647. }
  648. internal class XPathFunctionRound : XPathFunction
  649. {
  650. Expression arg0;
  651. public XPathFunctionRound (FunctionArguments args) : base (args)
  652. {
  653. if (args == null || args.Tail != null)
  654. throw new XPathException ("round takes one arg");
  655. arg0 = args.Arg;
  656. }
  657. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  658. public override object Evaluate (BaseIterator iter)
  659. {
  660. double arg = arg0.EvaluateNumber (iter);
  661. if (arg < -0.5 || arg > 0)
  662. return Math.Floor (arg + 0.5);
  663. return Math.Round (arg);
  664. }
  665. }
  666. }