DefaultContext.cs 19 KB

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