DefaultContext.cs 23 KB

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