DefaultContext.cs 24 KB

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