XQueryFunctionCliImpl.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. //
  2. // XQueryFunctionCliImple.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. //
  30. // Runtime-level (native) implementation of XQuery 1.0 and XPath 2.0
  31. // Functions implementation. XQueryCliFunction
  32. // See XQuery 1.0 and XPath 2.0 Functions and Operators.
  33. //
  34. #if NET_2_0
  35. using System;
  36. using System.Collections;
  37. using System.Globalization;
  38. using System.IO;
  39. using System.Text.RegularExpressions;
  40. using System.Xml;
  41. using System.Xml.Query;
  42. using System.Xml.Schema;
  43. using System.Xml.XPath;
  44. namespace Mono.Xml.XPath2
  45. {
  46. public class XQueryFunctionCliImpl
  47. {
  48. internal static XmlSchemaType XmlTypeFromCliType (Type cliType)
  49. {
  50. switch (Type.GetTypeCode (cliType)) {
  51. case TypeCode.Int32:
  52. return XmlSchemaSimpleType.XsInt;
  53. case TypeCode.Decimal:
  54. return XmlSchemaSimpleType.XsDecimal;
  55. case TypeCode.Double:
  56. return XmlSchemaSimpleType.XsDouble;
  57. case TypeCode.Single:
  58. return XmlSchemaSimpleType.XsFloat;
  59. case TypeCode.Int64:
  60. return XmlSchemaSimpleType.XsLong;
  61. case TypeCode.Int16:
  62. return XmlSchemaSimpleType.XsShort;
  63. case TypeCode.UInt16:
  64. return XmlSchemaSimpleType.XsUnsignedShort;
  65. case TypeCode.UInt32:
  66. return XmlSchemaSimpleType.XsUnsignedInt;
  67. case TypeCode.String:
  68. return XmlSchemaSimpleType.XsString;
  69. case TypeCode.DateTime:
  70. return XmlSchemaSimpleType.XsDateTime;
  71. case TypeCode.Boolean:
  72. return XmlSchemaSimpleType.XsBoolean;
  73. }
  74. if (cliType == typeof (XmlQualifiedName))
  75. return XmlSchemaSimpleType.XsQName;
  76. return null;
  77. }
  78. private static XPathItem ToItem (object arg)
  79. {
  80. if (arg == null)
  81. return null;
  82. XPathItem item = arg as XPathItem;
  83. if (item != null)
  84. return item;
  85. XPathSequence seq = arg as XPathSequence;
  86. if (seq != null)
  87. return seq.MoveNext () ? seq.Current : null;
  88. return new XPathAtomicValue (arg, XmlTypeFromCliType (arg.GetType ()));
  89. }
  90. // Accessors
  91. public static XmlQualifiedName FnNodeName (XPathNavigator arg)
  92. {
  93. if (arg == null)
  94. return null;
  95. return arg.LocalName == String.Empty ?
  96. XmlQualifiedName.Empty :
  97. new XmlQualifiedName (arg.LocalName, arg.NamespaceURI);
  98. }
  99. public static bool FnNilled (XPathNavigator arg)
  100. {
  101. if (arg == null)
  102. throw new XmlQueryException ("Function nilled() does not allow empty sequence parameter.");
  103. IXmlSchemaInfo info = arg.NodeType == XPathNodeType.Element ? arg.SchemaInfo : null;
  104. return info != null && info.IsNil;
  105. }
  106. public static string FnString (XQueryContext context)
  107. {
  108. XPathItem item = context.CurrentItem;
  109. if (item == null)
  110. throw new ArgumentException ("FONC0001: undefined context item");
  111. return FnString (item);
  112. }
  113. [MonoTODO]
  114. public static string FnString (object arg)
  115. {
  116. if (arg == null)
  117. return String.Empty;
  118. XPathNavigator nav = arg as XPathNavigator;
  119. if (nav != null)
  120. return nav.Value;
  121. // FIXME: it should be exactly the same as "arg cast as xs:string"
  122. XPathItem item = ToItem (arg);
  123. return item != null ? XQueryConvert.ItemToString (item) : null;
  124. }
  125. [MonoTODO]
  126. public static XPathAtomicValue FnData (object arg)
  127. {
  128. // FIXME: parameter should be object []
  129. XPathNavigator nav = arg as XPathNavigator;
  130. if (nav != null) {
  131. XmlSchemaType st = nav.SchemaInfo != null ? nav.SchemaInfo.SchemaType : null;
  132. return new XPathAtomicValue (nav.TypedValue, st != null ? st : XmlSchemaComplexType.AnyType);
  133. }
  134. else
  135. return (XPathAtomicValue) arg;
  136. }
  137. public static string FnBaseUri (XPathNavigator nav)
  138. {
  139. return nav != null ? nav.BaseURI : null;
  140. }
  141. public static string FnDocumentUri (XPathNavigator nav)
  142. {
  143. if (nav == null)
  144. return null;
  145. XPathNavigator root = nav.Clone ();
  146. root.MoveToRoot ();
  147. return root.BaseURI;
  148. }
  149. // Error
  150. [MonoTODO]
  151. public static void FnError (object arg)
  152. {
  153. throw new NotImplementedException ();
  154. }
  155. // Trace
  156. [MonoTODO]
  157. public static object FnTrace (XQueryContext ctx, object value, string label)
  158. {
  159. if (value == null)
  160. return new XPathEmptySequence (ctx);
  161. XPathSequence seq = value as XPathSequence;
  162. if (seq == null) {
  163. IEnumerable list = value as IEnumerable;
  164. if (list != null)
  165. seq = new EnumeratorIterator (ctx, list);
  166. else {
  167. XPathAtomicValue av = value as XPathAtomicValue;
  168. if (av == null)
  169. av = new XPathAtomicValue (value,
  170. XmlSchemaType.GetBuiltInType (
  171. XPathAtomicValue.XmlTypeCodeFromRuntimeType (
  172. value.GetType (), true)));
  173. seq = new SingleItemIterator (av, ctx);
  174. }
  175. }
  176. return new TracingIterator (seq, label);
  177. }
  178. // Numeric Operation
  179. [MonoTODO]
  180. public static object FnAbs (object arg)
  181. {
  182. if (arg is int)
  183. return System.Math.Abs ((int) arg);
  184. if (arg is long)
  185. return System.Math.Abs ((long) arg);
  186. else if (arg is decimal)
  187. return System.Math.Abs ((decimal) arg);
  188. else if (arg is double)
  189. return System.Math.Abs ((double) arg);
  190. else if (arg is float)
  191. return System.Math.Abs ((float) arg);
  192. else if (arg is short)
  193. return System.Math.Abs ((short) arg);
  194. else if (arg is uint || arg is ulong || arg is ushort)
  195. return arg;
  196. return null;
  197. }
  198. [MonoTODO]
  199. public static object FnCeiling (object arg)
  200. {
  201. if (arg is decimal) {
  202. decimal d = (decimal) arg;
  203. decimal d2 = Decimal.Floor (d);
  204. return d2 != d ? d2 + 1 : d2;
  205. }
  206. else if (arg is double || arg is float)
  207. return System.Math.Ceiling ((double) arg);
  208. else if (arg is int || arg is long || arg is short || arg is uint || arg is ulong || arg is ushort)
  209. return arg;
  210. return null;
  211. }
  212. [MonoTODO]
  213. public static object FnFloor (object arg)
  214. {
  215. if (arg is decimal)
  216. return Decimal.Floor ((decimal) arg);
  217. else if (arg is double || arg is float)
  218. return System.Math.Floor ((double) arg);
  219. else if (arg is int || arg is long || arg is short || arg is uint || arg is ulong || arg is ushort)
  220. return arg;
  221. return null;
  222. }
  223. [MonoTODO]
  224. public static object FnRound (object arg)
  225. {
  226. if (arg is decimal)
  227. return Decimal.Round ((decimal) arg, 0);
  228. else if (arg is double || arg is float)
  229. return System.Math.Round ((double) arg);
  230. else if (arg is int || arg is long || arg is short || arg is uint || arg is ulong || arg is ushort)
  231. return arg;
  232. return null;
  233. }
  234. [MonoTODO]
  235. public static object FnRoundHalfToEven (object arg)
  236. {
  237. throw new NotImplementedException ();
  238. }
  239. [MonoTODO]
  240. public static string FnCodepointsToString (int [] arg)
  241. {
  242. throw new NotImplementedException ();
  243. }
  244. [MonoTODO]
  245. public static int [] FnStringToCodepoints (string arg)
  246. {
  247. throw new NotImplementedException ();
  248. }
  249. public static int FnCompare (XQueryContext ctx, string s1, string s2)
  250. {
  251. return FnCompare (s1, s2, ctx.DefaultCollation);
  252. }
  253. public static int FnCompare (XQueryContext ctx, string s1, string s2, string collation)
  254. {
  255. return FnCompare (s1, s2, ctx.GetCulture (collation));
  256. }
  257. private static int FnCompare (string s1, string s2, CultureInfo ci)
  258. {
  259. return ci.CompareInfo.Compare (s1, s2);
  260. }
  261. public static string FnConcat (object o1, object o2)
  262. {
  263. return String.Concat (o1, o2);
  264. }
  265. public static string FnStringJoin (string [] strings, string separator)
  266. {
  267. return String.Join (separator, strings);
  268. }
  269. public static string FnSubstring (string src, double loc)
  270. {
  271. return src.Substring ((int) loc);
  272. }
  273. public static string FnSubstring (string src, double loc, double length)
  274. {
  275. return src.Substring ((int) loc, (int) length);
  276. }
  277. public static int FnStringLength (XQueryContext ctx)
  278. {
  279. return FnString (ctx).Length;
  280. }
  281. public static int FnStringLength (string s)
  282. {
  283. return s.Length;
  284. }
  285. public static string FnNormalizeSpace (XQueryContext ctx)
  286. {
  287. return FnNormalizeSpace (FnString (ctx));
  288. }
  289. [MonoTODO]
  290. public static string FnNormalizeSpace (string s)
  291. {
  292. throw new NotImplementedException ();
  293. }
  294. public static string FnNormalizeUnicode (string arg)
  295. {
  296. return FnNormalizeUnicode (arg, "NFC");
  297. }
  298. [MonoTODO]
  299. public static string FnNormalizeUnicode (string arg, string normalizationForm)
  300. {
  301. throw new NotImplementedException ();
  302. }
  303. public static string FnUpperCase (string arg)
  304. {
  305. // FIXME: supply culture
  306. return arg.ToUpper ();
  307. }
  308. public static string FnLowerCase (string arg)
  309. {
  310. // FIXME: supply culture
  311. return arg.ToLower ();
  312. }
  313. public static string FnTranslate (string arg, string mapString, string transString)
  314. {
  315. return arg == null ? null : arg.Replace (mapString, transString);
  316. }
  317. [MonoTODO]
  318. public static string FnEscapeUri (string uriPart, bool escapeReserved)
  319. {
  320. throw new NotImplementedException ();
  321. }
  322. public static bool FnContains (XQueryContext ctx, string arg1, string arg2)
  323. {
  324. return FnContains (arg1, arg2, ctx.DefaultCollation);
  325. }
  326. public static bool FnContains (XQueryContext ctx, string arg1, string arg2, string collation)
  327. {
  328. return FnContains (arg1, arg2, ctx.GetCulture (collation));
  329. }
  330. private static bool FnContains (string arg1, string arg2, CultureInfo ci)
  331. {
  332. if (arg1 == null)
  333. arg1 = String.Empty;
  334. if (arg2 == null)
  335. arg2 = String.Empty;
  336. if (arg2 == String.Empty)
  337. return true;
  338. return ci.CompareInfo.IndexOf (arg1, arg2) >= 0;
  339. }
  340. public static bool FnStartsWith (XQueryContext ctx, string arg1, string arg2)
  341. {
  342. return FnStartsWith (arg1, arg2, ctx.DefaultCollation);
  343. }
  344. public static bool FnStartsWith (XQueryContext ctx, string arg1, string arg2, string collation)
  345. {
  346. return FnStartsWith (arg1, arg2, ctx.GetCulture (collation));
  347. }
  348. private static bool FnStartsWith (string arg1, string arg2, CultureInfo ci)
  349. {
  350. return ci.CompareInfo.IsPrefix (arg1, arg2);
  351. }
  352. public static bool FnEndsWith (XQueryContext ctx, string arg1, string arg2)
  353. {
  354. return FnEndsWith (arg1, arg2, ctx.DefaultCollation);
  355. }
  356. public static bool FnEndsWith (XQueryContext ctx, string arg1, string arg2, string collation)
  357. {
  358. return FnEndsWith (arg1, arg2, ctx.GetCulture (collation));
  359. }
  360. private static bool FnEndsWith (string arg1, string arg2, CultureInfo ci)
  361. {
  362. return ci.CompareInfo.IsSuffix (arg1, arg2);
  363. }
  364. public static string FnSubstringBefore (XQueryContext ctx, string arg1, string arg2)
  365. {
  366. return FnSubstringBefore (arg1, arg2, ctx.DefaultCollation);
  367. }
  368. public static string FnSubstringBefore (XQueryContext ctx, string arg1, string arg2, string collation)
  369. {
  370. return FnSubstringBefore (arg1, arg2, ctx.GetCulture (collation));
  371. }
  372. private static string FnSubstringBefore (string arg1, string arg2, CultureInfo ci)
  373. {
  374. int index = ci.CompareInfo.IndexOf (arg1, arg2);
  375. return arg1.Substring (0, index);
  376. }
  377. public static string FnSubstringAfter (XQueryContext ctx, string arg1, string arg2)
  378. {
  379. return FnSubstringAfter (arg1, arg2, ctx.DefaultCollation);
  380. }
  381. public static string FnSubstringAfter (XQueryContext ctx, string arg1, string arg2, string collation)
  382. {
  383. return FnSubstringAfter (arg1, arg2, ctx.GetCulture (collation));
  384. }
  385. private static string FnSubstringAfter (string arg1, string arg2, CultureInfo ci)
  386. {
  387. int index = ci.CompareInfo.IndexOf (arg1, arg2);
  388. return arg1.Substring (index);
  389. }
  390. public static bool FnMatches (string input, string pattern)
  391. {
  392. return new Regex (pattern).IsMatch (input);
  393. }
  394. [MonoTODO]
  395. public static bool FnMatches (string input, string pattern, string flags)
  396. {
  397. throw new NotImplementedException ();
  398. }
  399. public static string FnReplace (string input, string pattern, string replace)
  400. {
  401. return new Regex (pattern).Replace (input, replace);
  402. }
  403. [MonoTODO]
  404. public static string FnReplace (string input, string pattern, string replace, string flags)
  405. {
  406. throw new NotImplementedException ();
  407. }
  408. public static string [] FnTokenize (string input, string pattern)
  409. {
  410. return new Regex (pattern).Split (input);
  411. }
  412. [MonoTODO]
  413. public static string [] FnTokenize (string input, string pattern, string flags)
  414. {
  415. throw new NotImplementedException ();
  416. }
  417. public static string FnResolveUri (XQueryContext ctx, string relUri)
  418. {
  419. return new Uri (new Uri (ctx.StaticContext.BaseUri), relUri).ToString ();
  420. }
  421. public static string FnResolveUri (string relUri, string baseUri)
  422. {
  423. return new Uri (new Uri (baseUri), relUri).ToString ();
  424. }
  425. public static object FnTrue ()
  426. {
  427. return true;
  428. }
  429. public static object FnFalse ()
  430. {
  431. return false;
  432. }
  433. public static object FnNot (bool value)
  434. {
  435. return !value;
  436. }
  437. // FIXME: add a bunch of annoying datetime functions
  438. [MonoTODO]
  439. public static object FnResolveQName ()
  440. {
  441. throw new NotImplementedException ();
  442. }
  443. [MonoTODO]
  444. public static object FnExpandQName ()
  445. {
  446. throw new NotImplementedException ();
  447. }
  448. public static string FnLocalNameFromQName (XmlQualifiedName name)
  449. {
  450. return name != null ? name.Name : null;
  451. }
  452. public static object FnNamespaceUriFromQName (XmlQualifiedName name)
  453. {
  454. return name != null ? name.Namespace : null;
  455. }
  456. public static object FnNamespaceUriForPrefix (XQueryContext context, string prefix)
  457. {
  458. return prefix != null ? context.LookupNamespace (prefix) : null;
  459. }
  460. public static string [] FnInScopePrefixes (XQueryContext context)
  461. {
  462. IDictionary dict = context.GetNamespacesInScope (XmlNamespaceScope.ExcludeXml);
  463. ArrayList keys = new ArrayList (dict.Keys);
  464. return keys.ToArray (typeof (string)) as string [];
  465. }
  466. public static string FnName (XPathNavigator nav)
  467. {
  468. return nav != null ? nav.Name : null;
  469. }
  470. public static string FnLocalName (XPathNavigator nav)
  471. {
  472. return nav != null ? nav.LocalName : null;
  473. }
  474. public static string FnNamespaceUri (XPathNavigator nav)
  475. {
  476. return nav != null ? nav.NamespaceURI : null;
  477. }
  478. public static double FnNumber (XQueryContext ctx)
  479. {
  480. return FnNumber (ctx.CurrentItem);
  481. }
  482. public static double FnNumber (object arg)
  483. {
  484. if (arg == null)
  485. throw new XmlQueryException ("Context item could not be ndetermined during number() evaluation.");
  486. XPathItem item = ToItem (arg);
  487. return XQueryConvert.ItemToDouble (item);
  488. }
  489. public static bool FnLang (XQueryContext ctx, string testLang)
  490. {
  491. return FnLang (testLang, ctx.CurrentNode);
  492. }
  493. public static bool FnLang (string testLang, XPathNavigator node)
  494. {
  495. return testLang == node.XmlLang;
  496. }
  497. public static XPathNavigator FnRoot (XQueryContext ctx)
  498. {
  499. if (ctx.CurrentItem == null)
  500. throw new XmlQueryException ("FONC0001: Undefined context item.");
  501. if (ctx.CurrentNode == null)
  502. throw new XmlQueryException ("FOTY0011: Context item is not a node.");
  503. return FnRoot (ctx.CurrentNode);
  504. }
  505. public static XPathNavigator FnRoot (XPathNavigator node)
  506. {
  507. if (node == null)
  508. return null;
  509. XPathNavigator root = node.Clone ();
  510. root.MoveToRoot ();
  511. return root;
  512. }
  513. public static bool FnBoolean (IEnumerator e)
  514. {
  515. if (!e.MoveNext ())
  516. return false;
  517. XPathItem item = e.Current as XPathItem;
  518. if (e.MoveNext ())
  519. return true;
  520. return XQueryConvert.ItemToBoolean (item);
  521. }
  522. public static IEnumerable FnIndexOf (XQueryContext ctx, IEnumerable e, XPathItem item)
  523. {
  524. return FnIndexOf (e, item, ctx.DefaultCollation);
  525. }
  526. public static IEnumerable FnIndexOf (IEnumerable items, XPathItem item, CultureInfo ci)
  527. {
  528. ArrayList al = new ArrayList ();
  529. IEnumerator e = items.GetEnumerator ();
  530. for (int i = 0; e.MoveNext (); i++) {
  531. XPathItem iter = e.Current as XPathItem;
  532. if (iter.XmlType.TypeCode == XmlTypeCode.String) {
  533. if (ci.CompareInfo.Compare (iter.Value, item.Value) == 0)
  534. al.Add (i);
  535. }
  536. else {
  537. IComparable ic = (IComparable) iter.TypedValue;
  538. if (ic.CompareTo ((IComparable) item.TypedValue) == 0)
  539. al.Add (i);
  540. }
  541. }
  542. return al;
  543. }
  544. public static bool FnEmpty (IEnumerable e)
  545. {
  546. if (e is XPathEmptySequence)
  547. return true;
  548. return !e.GetEnumerator ().MoveNext ();
  549. }
  550. public static object FnExists (IEnumerable e)
  551. {
  552. if (e is XPathEmptySequence)
  553. return false;
  554. return e.GetEnumerator ().MoveNext ();
  555. }
  556. [MonoTODO]
  557. public static object FnDistinctValues (XQueryContext ctx, IEnumerable items)
  558. {
  559. throw new NotImplementedException ();
  560. }
  561. [MonoTODO]
  562. public static object FnDistinctValues (XQueryContext ctx, IEnumerable items, string collation)
  563. {
  564. throw new NotImplementedException ();
  565. }
  566. [MonoTODO]
  567. public static IEnumerable FnInsertBefore (IEnumerable target, int position, IEnumerable inserts)
  568. {
  569. throw new NotImplementedException ();
  570. }
  571. [MonoTODO]
  572. public static IEnumerable FnRemove (IEnumerable target, int position)
  573. {
  574. throw new NotImplementedException ();
  575. }
  576. [MonoTODO]
  577. public static IEnumerable FnReverse (IEnumerable arg)
  578. {
  579. throw new NotImplementedException ();
  580. }
  581. [MonoTODO]
  582. public static object FnSubsequence (IEnumerable sourceSeq, double startingLoc)
  583. {
  584. throw new NotImplementedException ();
  585. }
  586. [MonoTODO]
  587. public static object FnSubsequence (IEnumerable sourceSeq, double startingLoc, double length)
  588. {
  589. throw new NotImplementedException ();
  590. }
  591. [MonoTODO]
  592. // Basically it should be optimized by XQueryASTCompiler
  593. public static IEnumerable FnUnordered (IEnumerable e)
  594. {
  595. return e;
  596. }
  597. [MonoTODO]
  598. public static object FnZeroOrMore (IEnumerable e)
  599. {
  600. throw new NotImplementedException ();
  601. }
  602. [MonoTODO]
  603. public static object FnOneOrMore (IEnumerable e)
  604. {
  605. throw new NotImplementedException ();
  606. }
  607. [MonoTODO]
  608. public static object FnExactlyOne (IEnumerable e)
  609. {
  610. throw new NotImplementedException ();
  611. }
  612. [MonoTODO]
  613. public static object FnDeepEqual (XQueryContext ctx, IEnumerable p1, IEnumerable p2)
  614. {
  615. throw new NotImplementedException ();
  616. }
  617. [MonoTODO]
  618. public static object FnDeepEqual (XQueryContext ctx, IEnumerable p1, IEnumerable p2, string collation)
  619. {
  620. throw new NotImplementedException ();
  621. }
  622. public static int FnCount (IEnumerable e)
  623. {
  624. if (e == null)
  625. return 0;
  626. XPathSequence seq = e as XPathSequence;
  627. if (seq != null)
  628. return seq.Count;
  629. ICollection col = e as ICollection;
  630. if (col != null)
  631. return col.Count;
  632. int count = 0;
  633. IEnumerator en = e.GetEnumerator ();
  634. while (en.MoveNext ())
  635. count++;
  636. return count;
  637. }
  638. [MonoTODO]
  639. public static object FnAvg (IEnumerable e)
  640. {
  641. throw new NotImplementedException ();
  642. }
  643. [MonoTODO]
  644. public static object FnMax (XQueryContext ctx, IEnumerable e)
  645. {
  646. throw new NotImplementedException ();
  647. }
  648. [MonoTODO]
  649. public static object FnMax (XQueryContext ctx, IEnumerable e, string collation)
  650. {
  651. throw new NotImplementedException ();
  652. }
  653. [MonoTODO]
  654. public static object FnMin (XQueryContext ctx, IEnumerable e)
  655. {
  656. throw new NotImplementedException ();
  657. }
  658. [MonoTODO]
  659. public static object FnMin (XQueryContext ctx, IEnumerable e, string collation)
  660. {
  661. throw new NotImplementedException ();
  662. }
  663. [MonoTODO]
  664. public static object FnSum (IEnumerable e)
  665. {
  666. throw new NotImplementedException ();
  667. }
  668. [MonoTODO]
  669. public static object FnSum (IEnumerable e, XPathItem zero)
  670. {
  671. throw new NotImplementedException ();
  672. }
  673. public static XPathNavigator FnId (XQueryContext ctx, string id)
  674. {
  675. return FnId (id, ctx.CurrentNode);
  676. }
  677. public static XPathNavigator FnId (string id, XPathNavigator nav)
  678. {
  679. XPathNavigator node = nav.Clone ();
  680. return node.MoveToId (id) ? node : null;
  681. }
  682. [MonoTODO]
  683. public static object FnIdRef (XQueryContext ctx, string arg)
  684. {
  685. return FnIdRef (arg, ctx.CurrentNode);
  686. }
  687. [MonoTODO]
  688. public static object FnIdRef (string arg, XPathNavigator node)
  689. {
  690. throw new NotImplementedException ();
  691. }
  692. public static XPathNavigator FnDoc (XQueryContext ctx, string uri)
  693. {
  694. XmlResolver res = ctx.ContextManager.ExtDocResolver;
  695. string baseUriString = ctx.StaticContext.BaseUri;
  696. Uri baseUri = null;
  697. if (baseUriString != null && baseUriString != String.Empty)
  698. baseUri = new Uri (baseUriString);
  699. Uri relUri = res.ResolveUri (baseUri, uri);
  700. Stream s = res.GetEntity (relUri, null, typeof (Stream)) as Stream;
  701. try {
  702. XPathDocument doc = new XPathDocument (new XmlValidatingReader (new XmlTextReader (s)), XmlSpace.Preserve);
  703. return doc.CreateNavigator ();
  704. } finally {
  705. s.Close ();
  706. }
  707. }
  708. public static IEnumerable FnCollection (XQueryContext ctx, string name)
  709. {
  710. return ctx.ResolveCollection (name);
  711. }
  712. public static int FnPosition (XQueryContext ctx)
  713. {
  714. return ctx.CurrentSequence.Position;
  715. }
  716. public static int FnLast (XQueryContext ctx)
  717. {
  718. return ctx.CurrentSequence.Count;
  719. }
  720. public static DateTime FnCurrentDateTime ()
  721. {
  722. return DateTime.Now;
  723. }
  724. public static DateTime FnCurrentDate ()
  725. {
  726. return DateTime.Today;
  727. }
  728. public static DateTime FnCurrentTime ()
  729. {
  730. return new DateTime (DateTime.Now.TimeOfDay.Ticks);
  731. }
  732. [MonoTODO]
  733. public static object FnDefaultCollation ()
  734. {
  735. throw new NotImplementedException ();
  736. }
  737. [MonoTODO]
  738. public static object FnImplicitTimeZone ()
  739. {
  740. throw new NotImplementedException ();
  741. }
  742. }
  743. }
  744. #endif