DefaultContext.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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 ((double) arg).ToString ("R", System.Globalization.NumberFormatInfo.InvariantInfo);
  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.NodeSet; }}
  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. BaseIterator iter = (args.Length == 1) ? ((BaseIterator) args [0]) : new SelfIterator (docContext, xsltContext);
  280. if (iter == null || !iter.MoveNext ())
  281. return "";
  282. return iter.Current.Name;
  283. }
  284. public override string Name { get { return "name"; }}
  285. }
  286. internal class XPathFunctionString : XPathFunction
  287. {
  288. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  289. public override int Minargs { get { return 0; }}
  290. public override int Maxargs { get { return 1; }}
  291. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Any }; }}
  292. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  293. {
  294. return XPathFunctions.ToString (args [0]);
  295. }
  296. public override string Name { get { return "string"; }}
  297. }
  298. internal class XPathFunctionConcat : XPathFunction
  299. {
  300. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  301. public override int Minargs { get { return 2; }}
  302. public override int Maxargs { get { return int.MaxValue; }}
  303. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String, XPathResultType.String, XPathResultType.String }; }}
  304. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  305. {
  306. String str = "";
  307. foreach (string strArg in args)
  308. str += strArg;
  309. return str;
  310. }
  311. public override string Name { get { return "concat"; }}
  312. }
  313. internal class XPathFunctionStartsWith : XPathFunction
  314. {
  315. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  316. public override int Minargs { get { return 2; }}
  317. public override int Maxargs { get { return 2; }}
  318. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String, XPathResultType.String }; }}
  319. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  320. {
  321. string str1 = (string) args [0];
  322. string str2 = (string) args [1];
  323. return str1.StartsWith (str2);
  324. }
  325. public override string Name { get { return "starts-with"; }}
  326. }
  327. internal class XPathFunctionContains : XPathFunction
  328. {
  329. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  330. public override int Minargs { get { return 2; }}
  331. public override int Maxargs { get { return 2; }}
  332. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String, XPathResultType.String }; }}
  333. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  334. {
  335. string str1 = (string) args [0];
  336. string str2 = (string) args [1];
  337. return str1.IndexOf (str2) != -1;
  338. }
  339. public override string Name { get { return "contains"; }}
  340. }
  341. internal class XPathFunctionSubstringBefore : XPathFunction
  342. {
  343. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  344. public override int Minargs { get { return 2; }}
  345. public override int Maxargs { get { return 2; }}
  346. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String, XPathResultType.String }; }}
  347. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  348. {
  349. string str1 = (string) args [0];
  350. string str2 = (string) args [1];
  351. int ich = str1.IndexOf (str2);
  352. if (ich <= 0)
  353. return "";
  354. return str1.Substring (0, ich);
  355. }
  356. public override string Name { get { return "substring-before"; }}
  357. }
  358. internal class XPathFunctionSubstringAfter : XPathFunction
  359. {
  360. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  361. public override int Minargs { get { return 2; }}
  362. public override int Maxargs { get { return 2; }}
  363. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String, XPathResultType.String }; }}
  364. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  365. {
  366. string str1 = (string) args [0];
  367. string str2 = (string) args [1];
  368. int ich = str1.IndexOf (str2);
  369. if (ich <= 0)
  370. return "";
  371. return str1.Substring (ich + str2.Length);
  372. }
  373. public override string Name { get { return "substring-after"; }}
  374. }
  375. internal class XPathFunctionSubstring : XPathFunction
  376. {
  377. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  378. public override int Minargs { get { return 2; }}
  379. public override int Maxargs { get { return int.MaxValue; }}
  380. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String, XPathResultType.String, XPathResultType.String }; }}
  381. [MonoTODO]
  382. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  383. {
  384. // TODO: check this, what the hell were they smoking?
  385. string str = (string) args [0];
  386. double ich = Math.Round ((double) args [1]) - 1;
  387. if (Double.IsNaN (ich) || ich >= (double) str.Length)
  388. return "";
  389. if (args.Length == 2)
  390. {
  391. if (ich < 0)
  392. ich = 0.0;
  393. return str.Substring ((int) ich);
  394. }
  395. else
  396. {
  397. double cch = Math.Round ((double) args [2]);
  398. if (Double.IsNaN (cch))
  399. return "";
  400. if (ich < 0.0 || cch < 0.0)
  401. {
  402. cch = ich + cch;
  403. if (cch <= 0.0)
  404. return "";
  405. ich = 0.0;
  406. }
  407. double cchMax = (double) str.Length - ich;
  408. if (cch > cchMax)
  409. cch = cchMax;
  410. return str.Substring ((int) ich, (int) cch);
  411. }
  412. }
  413. public override string Name { get { return "substring"; }}
  414. }
  415. internal class XPathFunctionStringLength : XPathFunction
  416. {
  417. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  418. public override int Minargs { get { return 0; }}
  419. public override int Maxargs { get { return 1; }}
  420. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String }; }}
  421. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  422. {
  423. string str;
  424. if (args.Length == 1)
  425. str = (string) args [0];
  426. else
  427. str = docContext.Value;
  428. return (double) str.Length;
  429. }
  430. public override string Name { get { return "string-length"; }}
  431. }
  432. internal class XPathFunctionNormalizeSpace : XPathFunction
  433. {
  434. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  435. public override int Minargs { get { return 0; }}
  436. public override int Maxargs { get { return 1; }}
  437. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String }; }}
  438. [MonoTODO]
  439. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  440. {
  441. string str;
  442. if (args.Length == 1)
  443. str = (string) args [0];
  444. else
  445. str = docContext.Value;
  446. System.Text.StringBuilder sb = new System.Text.StringBuilder ();
  447. bool fSpace = false;
  448. foreach (char ch in str)
  449. {
  450. if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n')
  451. {
  452. fSpace = true;
  453. }
  454. else
  455. {
  456. if (fSpace)
  457. {
  458. fSpace = false;
  459. if (sb.Length > 0)
  460. sb.Append (' ');
  461. }
  462. sb.Append (ch);
  463. }
  464. }
  465. return sb.ToString ();
  466. }
  467. public override string Name { get { return "normalize-space"; }}
  468. }
  469. internal class XPathFunctionTranslate : XPathFunction
  470. {
  471. public override XPathResultType ReturnType { get { return XPathResultType.String; }}
  472. public override int Minargs { get { return 3; }}
  473. public override int Maxargs { get { return 3; }}
  474. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String, XPathResultType.String, XPathResultType.String }; }}
  475. [MonoTODO]
  476. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  477. {
  478. throw new NotImplementedException ();
  479. }
  480. public override string Name { get { return "translate"; }}
  481. }
  482. internal class XPathFunctionBoolean : XPathFunction
  483. {
  484. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  485. public override int Minargs { get { return 1; }}
  486. public override int Maxargs { get { return 1; }}
  487. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Any }; }}
  488. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  489. {
  490. return XPathFunctions.ToBoolean (args [0]);
  491. }
  492. public override string Name { get { return "boolean"; }}
  493. }
  494. internal class XPathFunctionNot : XPathFunction
  495. {
  496. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  497. public override int Minargs { get { return 1; }}
  498. public override int Maxargs { get { return 1; }}
  499. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Any }; }}
  500. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  501. {
  502. return !(XPathFunctions.ToBoolean (args [0]));
  503. }
  504. public override string Name { get { return "not"; }}
  505. }
  506. internal class XPathFunctionTrue : XPathFunction
  507. {
  508. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  509. public override int Minargs { get { return 0; }}
  510. public override int Maxargs { get { return 0; }}
  511. public override XPathResultType [] ArgTypes { get { return null; }}
  512. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  513. {
  514. return true;
  515. }
  516. public override string Name { get { return "true"; }}
  517. }
  518. internal class XPathFunctionFalse : XPathFunction
  519. {
  520. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  521. public override int Minargs { get { return 0; }}
  522. public override int Maxargs { get { return 0; }}
  523. public override XPathResultType [] ArgTypes { get { return null; }}
  524. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  525. {
  526. return false;
  527. }
  528. public override string Name { get { return "false"; }}
  529. }
  530. internal class XPathFunctionLang : XPathFunction
  531. {
  532. public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
  533. public override int Minargs { get { return 1; }}
  534. public override int Maxargs { get { return 1; }}
  535. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.String }; }}
  536. [MonoTODO]
  537. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  538. {
  539. throw new NotImplementedException ();
  540. }
  541. public override string Name { get { return "lang"; }}
  542. }
  543. internal class XPathFunctionNumber : XPathFunction
  544. {
  545. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  546. public override int Minargs { get { return 0; }}
  547. public override int Maxargs { get { return 1; }}
  548. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Any }; }}
  549. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  550. {
  551. return XPathFunctions.ToNumber (args [0]);
  552. }
  553. public override string Name { get { return "number"; }}
  554. }
  555. internal class XPathFunctionSum : XPathFunction
  556. {
  557. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  558. public override int Minargs { get { return 1; }}
  559. public override int Maxargs { get { return 1; }}
  560. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.NodeSet }; }}
  561. [MonoTODO]
  562. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  563. {
  564. throw new NotImplementedException ();
  565. }
  566. public override string Name { get { return "sum"; }}
  567. }
  568. internal class XPathFunctionFloor : XPathFunction
  569. {
  570. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  571. public override int Minargs { get { return 1; }}
  572. public override int Maxargs { get { return 1; }}
  573. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Number }; }}
  574. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  575. {
  576. return Math.Floor ((double) args [0]);
  577. }
  578. public override string Name { get { return "floor"; }}
  579. }
  580. internal class XPathFunctionCeil : XPathFunction
  581. {
  582. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  583. public override int Minargs { get { return 1; }}
  584. public override int Maxargs { get { return 1; }}
  585. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Number }; }}
  586. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  587. {
  588. return Math.Ceiling ((double) args [0]);
  589. }
  590. public override string Name { get { return "ceil"; }}
  591. }
  592. internal class XPathFunctionRound : XPathFunction
  593. {
  594. public override XPathResultType ReturnType { get { return XPathResultType.Number; }}
  595. public override int Minargs { get { return 1; }}
  596. public override int Maxargs { get { return 1; }}
  597. public override XPathResultType [] ArgTypes { get { return new XPathResultType [] { XPathResultType.Number }; }}
  598. public override object TypesafeInvoke (XsltContext xsltContext, object[] args, XPathNavigator docContext)
  599. {
  600. double arg = (double) args [0];
  601. if (arg < -0.5 || arg > 0)
  602. return Math.Floor (arg + 0.5);
  603. return Math.Round (arg);
  604. }
  605. public override string Name { get { return "round"; }}
  606. }
  607. }