DefaultContext.cs 18 KB

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