DefaultContext.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. namespace System.Xml.XPath
  15. {
  16. /// <summary>
  17. /// Summary description for DefaultContext.
  18. /// </summary>
  19. internal class DefaultContext : XsltContext
  20. {
  21. protected static Hashtable _htFunctions = new Hashtable ();
  22. static DefaultContext()
  23. {
  24. Add (new XPathFunctionLast ());
  25. Add (new XPathFunctionPosition ());
  26. Add (new XPathFunctionCount ());
  27. Add (new XPathFunctionId ());
  28. Add (new XPathFunctionLocalName ());
  29. Add (new XPathFunctionNamespaceUri ());
  30. Add (new XPathFunctionName ());
  31. Add (new XPathFunctionString ());
  32. Add (new XPathFunctionConcat ());
  33. Add (new XPathFunctionStartsWith ());
  34. Add (new XPathFunctionContains ());
  35. Add (new XPathFunctionSubstringBefore ());
  36. Add (new XPathFunctionSubstringAfter ());
  37. Add (new XPathFunctionSubstring ());
  38. Add (new XPathFunctionStringLength ());
  39. Add (new XPathFunctionNormalizeSpace ());
  40. Add (new XPathFunctionTranslate ());
  41. Add (new XPathFunctionBoolean ());
  42. Add (new XPathFunctionNot ());
  43. Add (new XPathFunctionTrue ());
  44. Add (new XPathFunctionFalse ());
  45. Add (new XPathFunctionLang ());
  46. Add (new XPathFunctionNumber ());
  47. Add (new XPathFunctionSum ());
  48. Add (new XPathFunctionFloor ());
  49. Add (new XPathFunctionCeil ());
  50. Add (new XPathFunctionRound ());
  51. }
  52. [MonoTODO]
  53. public override IXsltContextFunction ResolveFunction (string prefix, string name, XPathResultType[] ArgTypes)
  54. {
  55. // match the prefix
  56. if (prefix != null && prefix != "") // TODO: should we allow some namespaces here?
  57. return null;
  58. // match the function name
  59. XPathFunction fn = (XPathFunction) _htFunctions [name];
  60. if (fn == null)
  61. return null;
  62. // check the number of arguments
  63. int cArgs = ArgTypes.Length;
  64. if (cArgs < fn.Minargs || cArgs > fn.Maxargs)
  65. return null;
  66. // check the types of the arguments
  67. XPathResultType [] rgTypes = fn.ArgTypes;
  68. if (rgTypes == null)
  69. {
  70. if (cArgs != 0)
  71. return null;
  72. }
  73. else
  74. {
  75. int cTypes = rgTypes.Length;
  76. for (int iArg = 0; iArg < cArgs; iArg ++)
  77. {
  78. XPathResultType typeRequested = ArgTypes [iArg];
  79. XPathResultType typeDefined = (iArg >= cTypes) ? rgTypes [cTypes - 1] : rgTypes [iArg];
  80. if (typeDefined != XPathResultType.Any && typeDefined != typeRequested)
  81. return null;
  82. }
  83. }
  84. return fn;
  85. }
  86. public override IXsltContextVariable ResolveVariable (string prefix, string name)
  87. {
  88. return null;
  89. }
  90. [MonoTODO]
  91. public override int CompareDocument (string baseUri, string nextBaseUri) { throw new NotImplementedException (); }
  92. [MonoTODO]
  93. public override bool PreserveWhitespace (XPathNavigator nav) { throw new NotImplementedException (); }
  94. [MonoTODO]
  95. public override bool Whitespace { get { throw new NotImplementedException (); }}
  96. protected static void Add (XPathFunction fn)
  97. {
  98. _htFunctions.Add (fn.Name, fn);
  99. }
  100. }
  101. internal class XPathFunctions
  102. {
  103. public static bool ToBoolean (object arg)
  104. {
  105. if (arg == null)
  106. throw new ArgumentNullException ();
  107. if (arg is bool)
  108. return (bool) arg;
  109. if (arg is double)
  110. {
  111. double dArg = (double) arg;
  112. return (dArg != 0.0 && !double.IsNaN (dArg));
  113. }
  114. if (arg is string)
  115. return ((string) arg).Length != 0;
  116. if (arg is BaseIterator)
  117. {
  118. BaseIterator iter = (BaseIterator) arg;
  119. return iter.MoveNext ();
  120. }
  121. throw new ArgumentException ();
  122. }
  123. [MonoTODO]
  124. public static string ToString (object arg)
  125. {
  126. if (arg == null)
  127. throw new ArgumentNullException ();
  128. if (arg is string)
  129. return (string) arg;
  130. if (arg is bool)
  131. return ((bool) arg) ? "true" : "false";
  132. if (arg is double)
  133. return (string) XmlConvert.ToString ((double) arg); // TODO: spec? convert number to string
  134. if (arg is BaseIterator)
  135. {
  136. BaseIterator iter = (BaseIterator) arg;
  137. if (!iter.MoveNext ())
  138. return "";
  139. return iter.Current.Value;
  140. }
  141. throw new ArgumentException ();
  142. }
  143. [MonoTODO]
  144. public static double ToNumber (object arg)
  145. {
  146. if (arg == null)
  147. throw new ArgumentNullException ();
  148. if (arg is BaseIterator)
  149. arg = ToString (arg); // follow on
  150. if (arg is string)
  151. return XmlConvert.ToDouble ((string) arg); // TODO: spec? convert string to number
  152. if (arg is double)
  153. return (double) arg;
  154. if (arg is bool)
  155. return Convert.ToDouble ((bool) arg);
  156. throw new ArgumentException ();
  157. }
  158. }
  159. internal abstract class XPathFunction : IXsltContextFunction
  160. {
  161. public abstract XPathResultType ReturnType { get; }
  162. public abstract int Minargs { get; }
  163. public abstract int Maxargs { get; }
  164. public abstract XPathResultType [] ArgTypes { get; }
  165. public object Invoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  166. {
  167. return TypesafeInvoke (xsltContext, args, docContext);
  168. }
  169. public abstract string Name { get; }
  170. public abstract object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext);
  171. }
  172. internal class XPathFunctionLast : XPathFunction
  173. {
  174. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  175. public override int Minargs { get { return 0; }}
  176. public override int Maxargs { get { return 0; }}
  177. public override XPathResultType [] ArgTypes { get { return null; }}
  178. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  179. {
  180. throw new NotImplementedException (); // special-cased
  181. }
  182. public override string Name { get { return "last"; }}
  183. }
  184. internal class XPathFunctionPosition : XPathFunction
  185. {
  186. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  187. public override int Minargs { get { return 0; }}
  188. public override int Maxargs { get { return 0; }}
  189. public override XPathResultType [] ArgTypes { get { return null; }}
  190. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  191. {
  192. throw new NotImplementedException (); // special-cased
  193. }
  194. public override string Name { get { return "position"; }}
  195. }
  196. internal class XPathFunctionCount : XPathFunction
  197. {
  198. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  199. public override int Minargs { get { return 1; }}
  200. public override int Maxargs { get { return 1; }}
  201. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.NodeSet }; }}
  202. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  203. {
  204. return ((BaseIterator) args [0]).Count;
  205. }
  206. public override string Name { get { return "count"; }}
  207. }
  208. internal class XPathFunctionId : XPathFunction
  209. {
  210. private static char [] rgchWhitespace = {' ', '\t', '\r', '\n'};
  211. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  212. public override int Minargs { get { return 1; }}
  213. public override int Maxargs { get { return 1; }}
  214. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Any }; }}
  215. [MonoTODO]
  216. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  217. {
  218. String strArgs;
  219. BaseIterator iter = args [0] as BaseIterator;
  220. if (iter != null)
  221. {
  222. strArgs = "";
  223. while (!iter.MoveNext ())
  224. strArgs += iter.Current.Value + " ";
  225. }
  226. else
  227. strArgs = XPathFunctions.ToString (args [0]);
  228. string [] rgstrArgs = strArgs.Split (rgchWhitespace);
  229. ArrayList rgNodes = new ArrayList ();
  230. foreach (string strArg in rgstrArgs)
  231. {
  232. if (docContext.MoveToId (strArg))
  233. rgNodes.Add (docContext.Clone ());
  234. }
  235. return new ArrayListIterator (iter, rgNodes);
  236. }
  237. public override string Name { get { return "id"; }}
  238. }
  239. internal class XPathFunctionLocalName : XPathFunction
  240. {
  241. public override XPathResultType ReturnType { get { return XPathResultType.NodeSet; }}
  242. public override int Minargs { get { return 0; }}
  243. public override int Maxargs { get { return 1; }}
  244. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.NodeSet }; }}
  245. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  246. {
  247. BaseIterator iter = (args.Length == 1) ? ((BaseIterator) args [0]) : new SelfIterator (docContext, xsltContext);
  248. if (iter == null || !iter.MoveNext ())
  249. return "";
  250. return iter.Current.LocalName;
  251. }
  252. public override string Name { get { return "local-name"; }}
  253. }
  254. internal class XPathFunctionNamespaceUri : XPathFunction
  255. {
  256. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  257. public override int Minargs { get { return 0; }}
  258. public override int Maxargs { get { return 1; }}
  259. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.NodeSet }; }}
  260. [MonoTODO]
  261. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  262. {
  263. BaseIterator iter = (args.Length == 1) ? ((BaseIterator) args [0]) : new SelfIterator (docContext, xsltContext);
  264. if (iter == null || !iter.MoveNext ())
  265. return "";
  266. return iter.Current.NamespaceURI; // TODO: should the namespace be expanded wrt. the given context?
  267. }
  268. public override string Name { get { return "namespace-uri"; }}
  269. }
  270. internal class XPathFunctionName : XPathFunction
  271. {
  272. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  273. public override int Minargs { get { return 0; }}
  274. public override int Maxargs { get { return 1; }}
  275. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.NodeSet }; }}
  276. [MonoTODO]
  277. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  278. {
  279. throw new NotImplementedException ();
  280. }
  281. public override string Name { get { return "name"; }}
  282. }
  283. internal class XPathFunctionString : XPathFunction
  284. {
  285. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  286. public override int Minargs { get { return 0; }}
  287. public override int Maxargs { get { return 1; }}
  288. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Any }; }}
  289. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  290. {
  291. return XPathFunctions.ToString (args [0]);
  292. }
  293. public override string Name { get { return "string"; }}
  294. }
  295. internal class XPathFunctionConcat : XPathFunction
  296. {
  297. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  298. public override int Minargs { get { return 2; }}
  299. public override int Maxargs { get { return int.MaxValue; }}
  300. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String, XPathResultType.String, XPathResultType.String }; }}
  301. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  302. {
  303. String str = "";
  304. foreach (string strArg in args)
  305. str += strArg;
  306. return str;
  307. }
  308. public override string Name { get { return "concat"; }}
  309. }
  310. internal class XPathFunctionStartsWith : XPathFunction
  311. {
  312. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  313. public override int Minargs { get { return 2; }}
  314. public override int Maxargs { get { return 2; }}
  315. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String, XPathResultType.String }; }}
  316. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  317. {
  318. string str1 = (string) args [0];
  319. string str2 = (string) args [1];
  320. return str1.StartsWith (str2);
  321. }
  322. public override string Name { get { return "starts-with"; }}
  323. }
  324. internal class XPathFunctionContains : XPathFunction
  325. {
  326. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  327. public override int Minargs { get { return 2; }}
  328. public override int Maxargs { get { return 2; }}
  329. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String, XPathResultType.String }; }}
  330. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  331. {
  332. string str1 = (string) args [0];
  333. string str2 = (string) args [1];
  334. return str1.IndexOf (str2) != -1;
  335. }
  336. public override string Name { get { return "contains"; }}
  337. }
  338. internal class XPathFunctionSubstringBefore : XPathFunction
  339. {
  340. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  341. public override int Minargs { get { return 2; }}
  342. public override int Maxargs { get { return 2; }}
  343. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String, XPathResultType.String }; }}
  344. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  345. {
  346. string str1 = (string) args [0];
  347. string str2 = (string) args [1];
  348. int ich = str1.IndexOf (str2);
  349. if (ich <= 0)
  350. return "";
  351. return str1.Substring (0, ich);
  352. }
  353. public override string Name { get { return "substring-before"; }}
  354. }
  355. internal class XPathFunctionSubstringAfter : XPathFunction
  356. {
  357. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  358. public override int Minargs { get { return 2; }}
  359. public override int Maxargs { get { return 2; }}
  360. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String, XPathResultType.String }; }}
  361. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  362. {
  363. string str1 = (string) args [0];
  364. string str2 = (string) args [1];
  365. int ich = str1.IndexOf (str2);
  366. if (ich <= 0)
  367. return "";
  368. return str1.Substring (ich + str2.Length);
  369. }
  370. public override string Name { get { return "substring-after"; }}
  371. }
  372. internal class XPathFunctionSubstring : XPathFunction
  373. {
  374. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  375. public override int Minargs { get { return 2; }}
  376. public override int Maxargs { get { return int.MaxValue; }}
  377. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String, XPathResultType.String, XPathResultType.String }; }}
  378. [MonoTODO]
  379. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  380. {
  381. // TODO: check this, what the hell were they smoking?
  382. string str = (string) args [0];
  383. double ich = Math.Round ((double) args [1]) - 1;
  384. if (Double.IsNaN (ich) || ich >= (double) str.Length)
  385. return "";
  386. if (args.Length == 2)
  387. {
  388. if (ich < 0)
  389. ich = 0.0;
  390. return str.Substring ((int) ich);
  391. }
  392. else
  393. {
  394. double cch = Math.Round ((double) args [2]);
  395. if (Double.IsNaN (cch))
  396. return "";
  397. if (ich < 0.0 || cch < 0.0)
  398. {
  399. cch = ich + cch;
  400. if (cch <= 0.0)
  401. return "";
  402. ich = 0.0;
  403. }
  404. double cchMax = (double) str.Length - ich;
  405. if (cch > cchMax)
  406. cch = cchMax;
  407. return str.Substring ((int) ich, (int) cch);
  408. }
  409. }
  410. public override string Name { get { return "substring"; }}
  411. }
  412. internal class XPathFunctionStringLength : XPathFunction
  413. {
  414. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  415. public override int Minargs { get { return 0; }}
  416. public override int Maxargs { get { return 1; }}
  417. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String }; }}
  418. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  419. {
  420. string str;
  421. if (args.Length == 1)
  422. str = (string) args [0];
  423. else
  424. str = docContext.Value;
  425. return (double) str.Length;
  426. }
  427. public override string Name { get { return "string-length"; }}
  428. }
  429. internal class XPathFunctionNormalizeSpace : XPathFunction
  430. {
  431. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  432. public override int Minargs { get { return 0; }}
  433. public override int Maxargs { get { return 1; }}
  434. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String }; }}
  435. [MonoTODO]
  436. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  437. {
  438. string str;
  439. if (args.Length == 1)
  440. str = (string) args [0];
  441. else
  442. str = docContext.Value;
  443. System.Text.StringBuilder sb = new System.Text.StringBuilder ();
  444. bool fSpace = false;
  445. foreach (char ch in str)
  446. {
  447. if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n')
  448. {
  449. fSpace = true;
  450. }
  451. else
  452. {
  453. if (fSpace)
  454. {
  455. fSpace = false;
  456. if (sb.Length > 0)
  457. sb.Append (' ');
  458. }
  459. sb.Append (ch);
  460. }
  461. }
  462. return sb.ToString ();
  463. }
  464. public override string Name { get { return "normalize-space"; }}
  465. }
  466. internal class XPathFunctionTranslate : XPathFunction
  467. {
  468. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  469. public override int Minargs { get { return 3; }}
  470. public override int Maxargs { get { return 3; }}
  471. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String, XPathResultType.String, XPathResultType.String }; }}
  472. [MonoTODO]
  473. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  474. {
  475. throw new NotImplementedException ();
  476. }
  477. public override string Name { get { return "translate"; }}
  478. }
  479. internal class XPathFunctionBoolean : XPathFunction
  480. {
  481. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  482. public override int Minargs { get { return 1; }}
  483. public override int Maxargs { get { return 1; }}
  484. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Any }; }}
  485. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  486. {
  487. return XPathFunctions.ToBoolean (args [0]);
  488. }
  489. public override string Name { get { return "boolean"; }}
  490. }
  491. internal class XPathFunctionNot : XPathFunction
  492. {
  493. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  494. public override int Minargs { get { return 1; }}
  495. public override int Maxargs { get { return 1; }}
  496. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Boolean }; }}
  497. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  498. {
  499. return !((bool) args [0]);
  500. }
  501. public override string Name { get { return "not"; }}
  502. }
  503. internal class XPathFunctionTrue : XPathFunction
  504. {
  505. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  506. public override int Minargs { get { return 0; }}
  507. public override int Maxargs { get { return 0; }}
  508. public override XPathResultType [] ArgTypes { get { return null; }}
  509. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  510. {
  511. return true;
  512. }
  513. public override string Name { get { return "true"; }}
  514. }
  515. internal class XPathFunctionFalse : XPathFunction
  516. {
  517. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  518. public override int Minargs { get { return 0; }}
  519. public override int Maxargs { get { return 0; }}
  520. public override XPathResultType [] ArgTypes { get { return null; }}
  521. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  522. {
  523. return false;
  524. }
  525. public override string Name { get { return "false"; }}
  526. }
  527. internal class XPathFunctionLang : XPathFunction
  528. {
  529. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  530. public override int Minargs { get { return 1; }}
  531. public override int Maxargs { get { return 1; }}
  532. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String }; }}
  533. [MonoTODO]
  534. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  535. {
  536. throw new NotImplementedException ();
  537. }
  538. public override string Name { get { return "lang"; }}
  539. }
  540. internal class XPathFunctionNumber : XPathFunction
  541. {
  542. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  543. public override int Minargs { get { return 0; }}
  544. public override int Maxargs { get { return 1; }}
  545. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Any }; }}
  546. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  547. {
  548. return XPathFunctions.ToNumber (args [0]);
  549. }
  550. public override string Name { get { return "number"; }}
  551. }
  552. internal class XPathFunctionSum : XPathFunction
  553. {
  554. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  555. public override int Minargs { get { return 1; }}
  556. public override int Maxargs { get { return 1; }}
  557. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.NodeSet }; }}
  558. [MonoTODO]
  559. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  560. {
  561. throw new NotImplementedException ();
  562. }
  563. public override string Name { get { return "sum"; }}
  564. }
  565. internal class XPathFunctionFloor : XPathFunction
  566. {
  567. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  568. public override int Minargs { get { return 1; }}
  569. public override int Maxargs { get { return 1; }}
  570. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Number }; }}
  571. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  572. {
  573. return Math.Floor ((double) args [0]);
  574. }
  575. public override string Name { get { return "floor"; }}
  576. }
  577. internal class XPathFunctionCeil : XPathFunction
  578. {
  579. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  580. public override int Minargs { get { return 1; }}
  581. public override int Maxargs { get { return 1; }}
  582. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Number }; }}
  583. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  584. {
  585. return Math.Ceiling ((double) args [0]);
  586. }
  587. public override string Name { get { return "ceil"; }}
  588. }
  589. internal class XPathFunctionRound : XPathFunction
  590. {
  591. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  592. public override int Minargs { get { return 1; }}
  593. public override int Maxargs { get { return 1; }}
  594. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Number }; }}
  595. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  596. {
  597. double arg = (double) args [0];
  598. if (arg < -0.5 || arg > 0)
  599. return Math.Floor (arg + 0.5);
  600. return Math.Round (arg);
  601. }
  602. public override string Name { get { return "round"; }}
  603. }
  604. }