DefaultContext.cs 18 KB

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