DefaultContext.cs 24 KB

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