DefaultContext.cs 24 KB

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