DefaultContext.cs 24 KB

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