| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974 |
- //
- // XQueryFunctionCliImple.cs
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- //
- // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- //
- // Runtime-level (native) implementation of XQuery 1.0 and XPath 2.0
- // Functions implementation. XQueryCliFunction
- // See XQuery 1.0 and XPath 2.0 Functions and Operators.
- //
- #if NET_2_0
- using System;
- using System.Collections;
- using System.Globalization;
- using System.IO;
- using System.Text.RegularExpressions;
- using System.Xml;
- using System.Xml.Query;
- using System.Xml.Schema;
- using System.Xml.XPath;
- namespace Mono.Xml.XPath2
- {
- public class XQueryFunctionCliImpl
- {
- internal static XmlSchemaType XmlTypeFromCliType (Type cliType)
- {
- switch (Type.GetTypeCode (cliType)) {
- case TypeCode.Int32:
- return XmlSchemaSimpleType.XsInt;
- case TypeCode.Decimal:
- return XmlSchemaSimpleType.XsDecimal;
- case TypeCode.Double:
- return XmlSchemaSimpleType.XsDouble;
- case TypeCode.Single:
- return XmlSchemaSimpleType.XsFloat;
- case TypeCode.Int64:
- return XmlSchemaSimpleType.XsLong;
- case TypeCode.Int16:
- return XmlSchemaSimpleType.XsShort;
- case TypeCode.UInt16:
- return XmlSchemaSimpleType.XsUnsignedShort;
- case TypeCode.UInt32:
- return XmlSchemaSimpleType.XsUnsignedInt;
- case TypeCode.String:
- return XmlSchemaSimpleType.XsString;
- case TypeCode.DateTime:
- return XmlSchemaSimpleType.XsDateTime;
- case TypeCode.Boolean:
- return XmlSchemaSimpleType.XsBoolean;
- }
- if (cliType == typeof (XmlQualifiedName))
- return XmlSchemaSimpleType.XsQName;
- return null;
- }
- private static XPathItem ToItem (object arg)
- {
- if (arg == null)
- return null;
- XPathItem item = arg as XPathItem;
- if (item != null)
- return item;
- XPathSequence seq = arg as XPathSequence;
- if (seq != null)
- return seq.MoveNext () ? seq.Current : null;
- return new XPathAtomicValue (arg, XmlTypeFromCliType (arg.GetType ()));
- }
- // Accessors
- public static XmlQualifiedName FnNodeName (XPathNavigator arg)
- {
- if (arg == null)
- return null;
- return arg.LocalName == String.Empty ?
- XmlQualifiedName.Empty :
- new XmlQualifiedName (arg.LocalName, arg.NamespaceURI);
- }
- public static bool FnNilled (XPathNavigator arg)
- {
- if (arg == null)
- throw new XmlQueryException ("Function nilled() does not allow empty sequence parameter.");
- IXmlSchemaInfo info = arg.NodeType == XPathNodeType.Element ? arg.SchemaInfo : null;
- return info != null && info.IsNil;
- }
- public static string FnString (XQueryContext context)
- {
- XPathItem item = context.CurrentItem;
- if (item == null)
- throw new ArgumentException ("FONC0001: undefined context item");
- return FnString (item);
- }
- [MonoTODO]
- public static string FnString (object arg)
- {
- if (arg == null)
- return String.Empty;
- XPathNavigator nav = arg as XPathNavigator;
- if (nav != null)
- return nav.Value;
- // FIXME: it should be exactly the same as "arg cast as xs:string"
- XPathItem item = ToItem (arg);
- return item != null ? XQueryConvert.ItemToString (item) : null;
- }
- [MonoTODO]
- public static XPathAtomicValue FnData (object arg)
- {
- // FIXME: parameter should be object []
- XPathNavigator nav = arg as XPathNavigator;
- if (nav != null) {
- XmlSchemaType st = nav.SchemaInfo != null ? nav.SchemaInfo.SchemaType : null;
- return new XPathAtomicValue (nav.TypedValue, st != null ? st : XmlSchemaComplexType.AnyType);
- }
- else
- return (XPathAtomicValue) arg;
- }
- public static string FnBaseUri (XPathNavigator nav)
- {
- return nav != null ? nav.BaseURI : null;
- }
- public static string FnDocumentUri (XPathNavigator nav)
- {
- if (nav == null)
- return null;
- XPathNavigator root = nav.Clone ();
- root.MoveToRoot ();
- return root.BaseURI;
- }
- // Error
- [MonoTODO]
- public static void FnError (object arg)
- {
- throw new NotImplementedException ();
- }
- // Trace
- [MonoTODO]
- public static object FnTrace (XQueryContext ctx, object value, string label)
- {
- if (value == null)
- return new XPathEmptySequence (ctx);
- XPathSequence seq = value as XPathSequence;
- if (seq == null) {
- XPathAtomicValue av = value as XPathAtomicValue;
- if (av == null)
- av = new XPathAtomicValue (value,
- XmlSchemaType.GetBuiltInType (
- XPathAtomicValue.XmlTypeCodeFromRuntimeType (
- value.GetType (), true)));
- seq = new SingleItemIterator (av, ctx);
- }
- return new TracingIterator (seq, label);
- }
- // Numeric Operation
- [MonoTODO]
- public static object FnAbs (object arg)
- {
- if (arg is int)
- return System.Math.Abs ((int) arg);
- if (arg is long)
- return System.Math.Abs ((long) arg);
- else if (arg is decimal)
- return System.Math.Abs ((decimal) arg);
- else if (arg is double)
- return System.Math.Abs ((double) arg);
- else if (arg is float)
- return System.Math.Abs ((float) arg);
- else if (arg is short)
- return System.Math.Abs ((short) arg);
- else if (arg is uint || arg is ulong || arg is ushort)
- return arg;
- return null;
- }
- [MonoTODO]
- public static object FnCeiling (object arg)
- {
- if (arg is decimal) {
- decimal d = (decimal) arg;
- decimal d2 = Decimal.Floor (d);
- return d2 != d ? d2 + 1 : d2;
- }
- else if (arg is double || arg is float)
- return System.Math.Ceiling ((double) arg);
- else if (arg is int || arg is long || arg is short || arg is uint || arg is ulong || arg is ushort)
- return arg;
- return null;
- }
- [MonoTODO]
- public static object FnFloor (object arg)
- {
- if (arg is decimal)
- return Decimal.Floor ((decimal) arg);
- else if (arg is double || arg is float)
- return System.Math.Floor ((double) arg);
- else if (arg is int || arg is long || arg is short || arg is uint || arg is ulong || arg is ushort)
- return arg;
- return null;
- }
- [MonoTODO]
- public static object FnRound (object arg)
- {
- if (arg is decimal)
- return Decimal.Round ((decimal) arg, 0);
- else if (arg is double || arg is float)
- return System.Math.Round ((double) arg);
- else if (arg is int || arg is long || arg is short || arg is uint || arg is ulong || arg is ushort)
- return arg;
- return null;
- }
- [MonoTODO]
- public static object FnRoundHalfToEven (object arg)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static string FnCodepointsToString (int [] arg)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static int [] FnStringToCodepoints (string arg)
- {
- throw new NotImplementedException ();
- }
- public static int FnCompare (XQueryContext ctx, string s1, string s2)
- {
- return FnCompare (s1, s2, ctx.DefaultCollation);
- }
- public static int FnCompare (XQueryContext ctx, string s1, string s2, string collation)
- {
- return FnCompare (s1, s2, ctx.GetCulture (collation));
- }
- private static int FnCompare (string s1, string s2, CultureInfo ci)
- {
- return ci.CompareInfo.Compare (s1, s2);
- }
- public static string FnConcat (object o1, object o2)
- {
- return String.Concat (o1, o2);
- }
- public static string FnStringJoin (string [] strings, string separator)
- {
- return String.Join (separator, strings);
- }
- public static string FnSubstring (string src, double loc)
- {
- return src.Substring ((int) loc);
- }
- public static string FnSubstring (string src, double loc, double length)
- {
- return src.Substring ((int) loc, (int) length);
- }
- public static int FnStringLength (XQueryContext ctx)
- {
- return FnString (ctx).Length;
- }
- public static int FnStringLength (string s)
- {
- return s.Length;
- }
- public static string FnNormalizeSpace (XQueryContext ctx)
- {
- return FnNormalizeSpace (FnString (ctx));
- }
- [MonoTODO]
- public static string FnNormalizeSpace (string s)
- {
- throw new NotImplementedException ();
- }
- public static string FnNormalizeUnicode (string arg)
- {
- return FnNormalizeUnicode (arg, "NFC");
- }
- [MonoTODO]
- public static string FnNormalizeUnicode (string arg, string normalizationForm)
- {
- throw new NotImplementedException ();
- }
- public static string FnUpperCase (string arg)
- {
- // FIXME: supply culture
- return arg.ToUpper ();
- }
- public static string FnLowerCase (string arg)
- {
- // FIXME: supply culture
- return arg.ToLower ();
- }
- public static string FnTranslate (string arg, string mapString, string transString)
- {
- return arg == null ? null : arg.Replace (mapString, transString);
- }
- [MonoTODO]
- public static string FnEscapeUri (string uriPart, bool escapeReserved)
- {
- throw new NotImplementedException ();
- }
- public static bool FnContains (XQueryContext ctx, string arg1, string arg2)
- {
- return FnContains (arg1, arg2, ctx.DefaultCollation);
- }
- public static bool FnContains (XQueryContext ctx, string arg1, string arg2, string collation)
- {
- return FnContains (arg1, arg2, ctx.GetCulture (collation));
- }
- private static bool FnContains (string arg1, string arg2, CultureInfo ci)
- {
- if (arg1 == null)
- arg1 = String.Empty;
- if (arg2 == null)
- arg2 = String.Empty;
- if (arg2 == String.Empty)
- return true;
- return ci.CompareInfo.IndexOf (arg1, arg2) >= 0;
- }
- public static bool FnStartsWith (XQueryContext ctx, string arg1, string arg2)
- {
- return FnStartsWith (arg1, arg2, ctx.DefaultCollation);
- }
- public static bool FnStartsWith (XQueryContext ctx, string arg1, string arg2, string collation)
- {
- return FnStartsWith (arg1, arg2, ctx.GetCulture (collation));
- }
- private static bool FnStartsWith (string arg1, string arg2, CultureInfo ci)
- {
- return ci.CompareInfo.IsPrefix (arg1, arg2);
- }
- public static bool FnEndsWith (XQueryContext ctx, string arg1, string arg2)
- {
- return FnEndsWith (arg1, arg2, ctx.DefaultCollation);
- }
- public static bool FnEndsWith (XQueryContext ctx, string arg1, string arg2, string collation)
- {
- return FnEndsWith (arg1, arg2, ctx.GetCulture (collation));
- }
- private static bool FnEndsWith (string arg1, string arg2, CultureInfo ci)
- {
- return ci.CompareInfo.IsSuffix (arg1, arg2);
- }
- public static string FnSubstringBefore (XQueryContext ctx, string arg1, string arg2)
- {
- return FnSubstringBefore (arg1, arg2, ctx.DefaultCollation);
- }
- public static string FnSubstringBefore (XQueryContext ctx, string arg1, string arg2, string collation)
- {
- return FnSubstringBefore (arg1, arg2, ctx.GetCulture (collation));
- }
- private static string FnSubstringBefore (string arg1, string arg2, CultureInfo ci)
- {
- int index = ci.CompareInfo.IndexOf (arg1, arg2);
- return arg1.Substring (0, index);
- }
- public static string FnSubstringAfter (XQueryContext ctx, string arg1, string arg2)
- {
- return FnSubstringAfter (arg1, arg2, ctx.DefaultCollation);
- }
- public static string FnSubstringAfter (XQueryContext ctx, string arg1, string arg2, string collation)
- {
- return FnSubstringAfter (arg1, arg2, ctx.GetCulture (collation));
- }
- private static string FnSubstringAfter (string arg1, string arg2, CultureInfo ci)
- {
- int index = ci.CompareInfo.IndexOf (arg1, arg2);
- return arg1.Substring (index);
- }
- public static bool FnMatches (string input, string pattern)
- {
- return new Regex (pattern).IsMatch (input);
- }
- [MonoTODO]
- public static bool FnMatches (string input, string pattern, string flags)
- {
- throw new NotImplementedException ();
- }
- public static string FnReplace (string input, string pattern, string replace)
- {
- return new Regex (pattern).Replace (input, replace);
- }
- [MonoTODO]
- public static string FnReplace (string input, string pattern, string replace, string flags)
- {
- throw new NotImplementedException ();
- }
- public static string [] FnTokenize (string input, string pattern)
- {
- return new Regex (pattern).Split (input);
- }
- [MonoTODO]
- public static string [] FnTokenize (string input, string pattern, string flags)
- {
- throw new NotImplementedException ();
- }
- public static string FnResolveUri (XQueryContext ctx, string relUri)
- {
- return new Uri (new Uri (ctx.StaticContext.BaseUri), relUri).ToString ();
- }
- public static string FnResolveUri (string relUri, string baseUri)
- {
- return new Uri (new Uri (baseUri), relUri).ToString ();
- }
- public static object FnTrue ()
- {
- return true;
- }
- public static object FnFalse ()
- {
- return false;
- }
- public static object FnNot (bool value)
- {
- return !value;
- }
- // FIXME: add a bunch of annoying datetime functions
- public static object FnResolveQName (string qname, XPathNavigator element)
- {
- if (qname == null)
- return null;
- int index = qname.IndexOf (':');
- string prefix = (index < 0) ? "" : qname.Substring (index);
- return new XmlQualifiedName (
- element.LookupNamespace (prefix),
- index < 0 ? qname : qname.Substring (index + 1));
- }
- public static object FnExpandQName (string ns, string local)
- {
- return new XmlQualifiedName (local, ns);
- }
- public static string FnLocalNameFromQName (XmlQualifiedName name)
- {
- return name != null ? name.Name : null;
- }
- public static object FnNamespaceUriFromQName (XmlQualifiedName name)
- {
- return name != null ? name.Namespace : null;
- }
- public static object FnNamespaceUriForPrefix (XQueryContext context, string prefix)
- {
- return prefix != null ? context.LookupNamespace (prefix) : null;
- }
- public static string [] FnInScopePrefixes (XQueryContext context)
- {
- IDictionary dict = context.GetNamespacesInScope (XmlNamespaceScope.ExcludeXml);
- ArrayList keys = new ArrayList (dict.Keys);
- return keys.ToArray (typeof (string)) as string [];
- }
- public static string FnName (XPathNavigator nav)
- {
- return nav != null ? nav.Name : null;
- }
- public static string FnLocalName (XPathNavigator nav)
- {
- return nav != null ? nav.LocalName : null;
- }
- public static string FnNamespaceUri (XPathNavigator nav)
- {
- return nav != null ? nav.NamespaceURI : null;
- }
- public static double FnNumber (XQueryContext ctx)
- {
- return FnNumber (ctx.CurrentItem);
- }
- public static double FnNumber (object arg)
- {
- if (arg == null)
- throw new XmlQueryException ("Context item could not be ndetermined during number() evaluation.");
- XPathItem item = ToItem (arg);
- return XQueryConvert.ItemToDouble (item);
- }
- public static bool FnLang (XQueryContext ctx, string testLang)
- {
- return FnLang (testLang, ctx.CurrentNode);
- }
- public static bool FnLang (string testLang, XPathNavigator node)
- {
- return testLang == node.XmlLang;
- }
- public static XPathNavigator FnRoot (XQueryContext ctx)
- {
- if (ctx.CurrentItem == null)
- throw new XmlQueryException ("FONC0001: Undefined context item.");
- if (ctx.CurrentNode == null)
- throw new XmlQueryException ("FOTY0011: Context item is not a node.");
- return FnRoot (ctx.CurrentNode);
- }
- public static XPathNavigator FnRoot (XPathNavigator node)
- {
- if (node == null)
- return null;
- XPathNavigator root = node.Clone ();
- root.MoveToRoot ();
- return root;
- }
- public static bool FnBoolean (IEnumerator e)
- {
- if (!e.MoveNext ())
- return false;
- XPathItem item = e.Current as XPathItem;
- if (e.MoveNext ())
- return true;
- return XQueryConvert.ItemToBoolean (item);
- }
- public static XPathSequence FnIndexOf (XQueryContext ctx, XPathSequence items, XPathItem item)
- {
- return FnIndexOf (ctx, items, item, ctx.DefaultCollation);
- }
- public static XPathSequence FnIndexOf (XQueryContext ctx, XPathSequence items, XPathItem item, CultureInfo ci)
- {
- ArrayList al = new ArrayList ();
- IEnumerator e = items.GetEnumerator ();
- for (int i = 0; e.MoveNext (); i++) {
- XPathItem iter = e.Current as XPathItem;
- if (iter.XmlType.TypeCode == XmlTypeCode.String) {
- if (ci.CompareInfo.Compare (iter.Value, item.Value) == 0)
- al.Add (i);
- }
- else {
- IComparable ic = (IComparable) iter.TypedValue;
- if (ic.CompareTo ((IComparable) item.TypedValue) == 0)
- al.Add (i);
- }
- }
- return new ListIterator (ctx, al);
- }
- public static bool FnEmpty (XPathSequence e)
- {
- if (e is XPathEmptySequence)
- return true;
- return !e.GetEnumerator ().MoveNext ();
- }
- public static bool FnExists (XPathSequence e)
- {
- if (e is XPathEmptySequence)
- return false;
- return e.MoveNext ();
- }
- public static XPathSequence FnDistinctValues (XQueryContext ctx, XPathSequence items)
- {
- return FnDistinctValuesImpl (ctx, items, ctx.DefaultCollation);
- }
- public static XPathSequence FnDistinctValues (XQueryContext ctx, XPathSequence items, string collation)
- {
- return FnDistinctValuesImpl (ctx, items, ctx.GetCulture (collation));
- }
- private static XPathSequence FnDistinctValuesImpl (XQueryContext ctx, XPathSequence items, CultureInfo collation)
- {
- return new DistinctValueIterator (ctx, items, collation);
- }
- public static XPathSequence FnInsertBefore (XPathSequence target, int position, XPathSequence inserts)
- {
- if (position < 1)
- position = 1;
- return new InsertingIterator (target, position, inserts);
- }
- public static XPathSequence FnRemove (XPathSequence target, int position)
- {
- if (position < 1)
- return target;
- return new RemovalIterator (target, position);
- }
- [MonoTODO ("optimize")]
- public static XPathSequence FnReverse (XPathSequence arg)
- {
- ArrayList al = new ArrayList ();
- while (arg.MoveNext ())
- al.Add (arg.Current);
- al.Reverse ();
- return new ListIterator (arg.Context, al);
- }
- public static object FnSubsequence (XPathSequence sourceSeq, double startingLoc)
- {
- return FnSubsequence (sourceSeq, startingLoc, double.MaxValue);
- }
- [MonoTODO]
- public static object FnSubsequence (XPathSequence sourceSeq, double startingLoc, double length)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- // Basically it should be optimized by XQueryASTCompiler
- public static XPathSequence FnUnordered (XPathSequence e)
- {
- return e;
- }
- public static XPathItem FnZeroOrOne (XPathSequence e)
- {
- if (!e.MoveNext ())
- return null;
- XPathItem item = e.Current;
- if (e.MoveNext ())
- throw new XmlQueryException ("zero-or-one() function detected that the argument sequence contains two or more items.");
- return item;
- }
- public static object FnOneOrMore (XPathSequence e)
- {
- if (!e.Clone ().MoveNext ())
- throw new XmlQueryException ("one-or-more() function detected that the argument sequence contains no items.");
- return e;
- }
- public static XPathItem FnExactlyOne (XPathSequence e)
- {
- if (!e.MoveNext ())
- throw new XmlQueryException ("exactly-one() function detected that the argument sequence contains no items.");
- XPathItem item = e.Current;
- if (e.MoveNext ())
- throw new XmlQueryException ("exactly-one() function detected that the argument sequence contains two or more items.");
- return item;
- }
- public static object FnDeepEqual (XQueryContext ctx, XPathSequence p1, XPathSequence p2)
- {
- return FnDeepEqualImpl (p1, p2, ctx.DefaultCollation);
- }
- public static object FnDeepEqual (XQueryContext ctx, XPathSequence p1, XPathSequence p2, string collation)
- {
- return FnDeepEqualImpl (p1, p2, ctx.GetCulture (collation));
- }
- public static bool FnDeepEqualImpl (XPathSequence p1, XPathSequence p2, CultureInfo collation)
- {
- // FIXME: use collation
- while (p1.MoveNext ()) {
- if (!p2.MoveNext ())
- return false;
- if (!FnDeepEqualItem (p1.Current, p2.Current, collation))
- return false;
- }
- if (p2.MoveNext ())
- return false;
- return true;
- }
- // FIXME: Actually ValueEQ() should consider collation.
- [MonoTODO]
- private static bool FnDeepEqualItem (XPathItem i1, XPathItem i2, CultureInfo collation)
- {
- XPathAtomicValue av1 = i1 as XPathAtomicValue;
- XPathAtomicValue av2 = i1 as XPathAtomicValue;
- if (av1 != null && av2 != null) {
- try {
- return XQueryComparisonOperator.ValueEQ (av1, av2);
- } catch (XmlQueryException) {
- // not-allowed comparison never raises
- // an error here, just return false.
- return false;
- }
- }
- else if (av1 != null || av2 != null)
- return false;
- XPathNavigator n1 = i1 as XPathNavigator;
- XPathNavigator n2 = i2 as XPathNavigator;
- if (n1.NodeType != n2.NodeType)
- return false;
- switch (n1.NodeType) {
- case XPathNodeType.Root:
- throw new NotImplementedException ();
- case XPathNodeType.Element:
- throw new NotImplementedException ();
- case XPathNodeType.Attribute:
- return n1.Name == n2.Name && n1.TypedValue == n2.TypedValue;
- case XPathNodeType.ProcessingInstruction:
- case XPathNodeType.Namespace:
- return n1.Name == n2.Name && n1.Value == n2.Value;
- case XPathNodeType.Text:
- case XPathNodeType.Comment:
- return n1.Value == n2.Value;
- }
- return false;
- }
- public static int FnCount (XPathSequence e)
- {
- if (e == null)
- return 0;
- return e.Count;
- }
- [MonoTODO]
- public static object FnAvg (XPathSequence e)
- {
- if (!e.MoveNext ())
- return null;
- switch (e.Current.XmlType.TypeCode) {
- case XmlTypeCode.DayTimeDuration:
- return FnAvgDayTimeDuration (e);
- case XmlTypeCode.YearMonthDuration:
- return FnAvgYearMonthDuration (e);
- case XmlTypeCode.Decimal:
- return FnAvgDecimal (e);
- case XmlTypeCode.Integer:
- return FnAvgInteger (e);
- case XmlTypeCode.Float:
- return FnAvgFloat (e);
- case XmlTypeCode.UntypedAtomic:
- case XmlTypeCode.Double:
- return FnAvgDouble (e);
- }
- throw new XmlQueryException ("avg() function detected that the sequence contains an item whose type is neither of dayTimeDuration, yearMonthDuration, decimal, integer, float, double, nor untypedAtomic.");
- }
- private static TimeSpan FnAvgDayTimeDuration (XPathSequence e)
- {
- throw new NotImplementedException ();
- }
- private static TimeSpan FnAvgYearMonthDuration (XPathSequence e)
- {
- throw new NotImplementedException ();
- }
- private static TimeSpan FnAvgDecimal (XPathSequence e)
- {
- throw new NotImplementedException ();
- }
- private static TimeSpan FnAvgInteger (XPathSequence e)
- {
- throw new NotImplementedException ();
- }
- private static TimeSpan FnAvgFloat (XPathSequence e)
- {
- throw new NotImplementedException ();
- }
- private static TimeSpan FnAvgDouble (XPathSequence e)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static object FnMax (XQueryContext ctx, XPathSequence e)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static object FnMax (XQueryContext ctx, XPathSequence e, string collation)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static object FnMin (XQueryContext ctx, XPathSequence e)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static object FnMin (XQueryContext ctx, XPathSequence e, string collation)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static object FnSum (XPathSequence e)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static object FnSum (XPathSequence e, XPathItem zero)
- {
- throw new NotImplementedException ();
- }
- public static XPathNavigator FnId (XQueryContext ctx, string id)
- {
- return FnId (id, ctx.CurrentNode);
- }
- public static XPathNavigator FnId (string id, XPathNavigator nav)
- {
- XPathNavigator node = nav.Clone ();
- return node.MoveToId (id) ? node : null;
- }
- [MonoTODO]
- public static object FnIdRef (XQueryContext ctx, string arg)
- {
- return FnIdRef (arg, ctx.CurrentNode);
- }
- [MonoTODO]
- public static object FnIdRef (string arg, XPathNavigator node)
- {
- throw new NotImplementedException ();
- }
- public static XPathNavigator FnDoc (XQueryContext ctx, string uri)
- {
- XmlResolver res = ctx.ContextManager.ExtDocResolver;
- string baseUriString = ctx.StaticContext.BaseUri;
- Uri baseUri = null;
- if (baseUriString != null && baseUriString != String.Empty)
- baseUri = new Uri (baseUriString);
- Uri relUri = res.ResolveUri (baseUri, uri);
- Stream s = res.GetEntity (relUri, null, typeof (Stream)) as Stream;
- try {
- XPathDocument doc = new XPathDocument (new XmlValidatingReader (new XmlTextReader (s)), XmlSpace.Preserve);
- return doc.CreateNavigator ();
- } finally {
- s.Close ();
- }
- }
- public static XPathSequence FnCollection (XQueryContext ctx, string name)
- {
- return ctx.ResolveCollection (name);
- }
- [XQueryFunctionContext]
- public static int FnPosition (XPathSequence current)
- {
- return current.Position;
- }
- [XQueryFunctionContext]
- public static int FnLast (XPathSequence current)
- {
- return current.Count;
- }
- public static DateTime FnCurrentDateTime ()
- {
- return DateTime.Now;
- }
- public static DateTime FnCurrentDate ()
- {
- return DateTime.Today;
- }
- public static DateTime FnCurrentTime ()
- {
- return new DateTime (DateTime.Now.TimeOfDay.Ticks);
- }
- [MonoTODO]
- public static object FnDefaultCollation ()
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static object FnImplicitTimeZone ()
- {
- throw new NotImplementedException ();
- }
- }
- }
- #endif
|