//------------------------------------------------------------------------ // ToBoolean (internal type to internal type) //------------------------------------------------------------------------ public static bool ToBoolean(XPathItem item) { XsltLibrary.CheckXsltValue(item); if (item.IsNode) return true; Type itemType = item.ValueType; if (itemType == StringType) { return item.Value.Length != 0; } else if (itemType == DoubleType) { // (x < 0 || 0 < x) == (x != 0) && !Double.IsNaN(x) double dbl = item.ValueAsDouble; return dbl < 0 || 0 < dbl; } else { Debug.Assert(itemType == BooleanType, "Unexpected type of atomic sequence " + itemType.ToString()); return item.ValueAsBoolean; } }
internal XQueryContextManager (XQueryStaticContext ctx, XPathItem input, XmlWriter writer, XmlResolver resolver, XmlArgumentList args) { this.input = input; this.staticContext = ctx; this.args = args; currentWriter = writer; this.extDocResolver = resolver; namespaceManager = new XmlNamespaceManager (ctx.NameTable); foreach (DictionaryEntry de in ctx.NSResolver.GetNamespacesInScope (XmlNamespaceScope.ExcludeXml)) namespaceManager.AddNamespace (de.Key.ToString (), de.Value.ToString ()); namespaceManager.PushScope (); currentContext = new XQueryContext (this, null, new Hashtable ()); if (input != null) { currentSequence = new SingleItemIterator (input, currentContext); currentSequence.MoveNext (); } currentContext = new XQueryContext (this, currentSequence, new Hashtable ()); }
public void Add(XPathItem item) { XQueryNavigator nav = item as XQueryNavigator; if (nav != null && nav.NodeType == XPathNodeType.Element) { if (current == null || current.document != nav.Document) { current = new ElementsSegment(nav.Document); segments.Add(current); } current.Add(nav.Position); } else { if (current == null || current.document != null) { current = new DataSegment(); segments.Add(current); } current.Add(item.Clone()); } count++; }
private bool ItemEqual(XPathItem item1, XPathItem item2) { object res; object x = item1.TypedValue; if (x is UntypedAtomic || x is AnyUriValue) x = x.ToString(); object y = item2.TypedValue; if (y is UntypedAtomic || y is AnyUriValue) y = x.ToString(); if (x is Single && Single.IsNaN((float)x) || x is Double && Double.IsNaN((double)x)) x = Double.NaN; if (y is Single && Single.IsNaN((float)y) || y is Double && Double.IsNaN((double)y)) y = Double.NaN; if (x.Equals(y)) return true; if (ValueProxy.Eq(x, y, out res)) { if (res != null) return true; } return false; }
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 object FnSum (XPathSequence e, XPathItem zero) { throw new NotImplementedException (); }
public static DateTime ItemToTime (XPathItem value) { return XmlConvert.ToDateTime (value.Value); }
public static XmlQualifiedName ItemToQName (XPathItem value) { return (XmlQualifiedName) value.TypedValue; }
public static decimal ItemToNonPositiveInteger (XPathItem value) { return XmlConvert.ToDecimal (value.Value); }
public static long ItemToInteger (XPathItem value) { return XmlConvert.ToInt64 (value.Value); }
public static string ItemToAnyUri (XPathItem value) { return value.Value; }
public static decimal ItemToUnsignedLong (XPathItem value) { return XmlConvert.ToInt32 (value.Value); }
/// <summary> /// Create an XmlQueryType that represents the type of "item". /// </summary> private XmlQueryType CreateXmlType(XPathItem item) { if (item.IsNode) { // Rtf RtfNavigator rtf = item as RtfNavigator; if (rtf != null) return XmlQueryTypeFactory.Node; // Node XPathNavigator nav = (XPathNavigator) item; switch (nav.NodeType) { case XPathNodeType.Root: case XPathNodeType.Element: if (nav.XmlType == null) return XmlQueryTypeFactory.Type(nav.NodeType, XmlQualifiedNameTest.New(nav.LocalName, nav.NamespaceURI), XmlSchemaComplexType.UntypedAnyType, false); return XmlQueryTypeFactory.Type(nav.NodeType, XmlQualifiedNameTest.New(nav.LocalName, nav.NamespaceURI), nav.XmlType, nav.SchemaInfo.SchemaElement.IsNillable); case XPathNodeType.Attribute: if (nav.XmlType == null) return XmlQueryTypeFactory.Type(nav.NodeType, XmlQualifiedNameTest.New(nav.LocalName, nav.NamespaceURI), DatatypeImplementation.UntypedAtomicType, false); return XmlQueryTypeFactory.Type(nav.NodeType, XmlQualifiedNameTest.New(nav.LocalName, nav.NamespaceURI), nav.XmlType, false); } return XmlQueryTypeFactory.Type(nav.NodeType, XmlQualifiedNameTest.Wildcard, XmlSchemaComplexType.AnyType, false); } // Atomic value return XmlQueryTypeFactory.Type((XmlSchemaSimpleType)item.XmlType, true); }
/// <summary> /// Return true if the type of "item" is a subtype of the type identified by "code". /// </summary> public bool MatchesXmlType(XPathItem item, XmlTypeCode code) { // All atomic type codes appear after AnyAtomicType if (code > XmlTypeCode.AnyAtomicType) return !item.IsNode && item.XmlType.TypeCode == code; // Handle node code and AnyAtomicType switch (code) { case XmlTypeCode.AnyAtomicType: return !item.IsNode; case XmlTypeCode.Node: return item.IsNode; case XmlTypeCode.Item: return true; default: if (!item.IsNode) return false; switch (((XPathNavigator) item).NodeType) { case XPathNodeType.Root: return code == XmlTypeCode.Document; case XPathNodeType.Element: return code == XmlTypeCode.Element; case XPathNodeType.Attribute: return code == XmlTypeCode.Attribute; case XPathNodeType.Namespace: return code == XmlTypeCode.Namespace; case XPathNodeType.Text: return code == XmlTypeCode.Text; case XPathNodeType.SignificantWhitespace: return code == XmlTypeCode.Text; case XPathNodeType.Whitespace: return code == XmlTypeCode.Text; case XPathNodeType.ProcessingInstruction: return code == XmlTypeCode.ProcessingInstruction; case XPathNodeType.Comment: return code == XmlTypeCode.Comment; } break; } Debug.Fail("XmlTypeCode " + code + " was not fully handled."); return false; }
/// <summary> /// Return true if the type of "item" matches the xml type identified by "idxType". /// </summary> public bool MatchesXmlType(XPathItem item, int indexType) { return CreateXmlType(item).IsSubtypeOf(GetXmlType(indexType)); }
public static byte [] ItemToHexBinary (XPathItem value) { return XmlConvert.FromBinHexString (value.Value); }
public static int ItemToInt (XPathItem value) { return XmlConvert.ToInt32 (value.Value); }
public static byte [] ItemToBase64Binary (XPathItem value) { return Convert.FromBase64String (value.Value); }
public static XPathItem ItemToItem (XPathItem value, XmlSchemaType schemaTypeDest) { return new XPathAtomicValue (value.Value, schemaTypeDest); }
public static bool ItemToBoolean (XPathItem value) { return XmlConvert.ToBoolean (value.Value); }
// See XQuery & XPath 2.0 functions & operators section 17. public static bool CanConvert (XPathItem item, XmlSchemaType schemaTypeDest) { if (item == null) throw new ArgumentNullException ("item"); if (schemaTypeDest == null) throw new ArgumentNullException ("schemaTypeDest"); XmlTypeCode src = item.XmlType.TypeCode; XmlTypeCode dst = schemaTypeDest.TypeCode; // Notation cannot be converted from other than Notation if (src == XmlTypeCode.Notation && dst != XmlTypeCode.Notation) return false; // untypedAtomic and string are convertable unless source type is QName. switch (dst) { case XmlTypeCode.UntypedAtomic: case XmlTypeCode.String: return src != XmlTypeCode.QName; } switch (src) { case XmlTypeCode.None: case XmlTypeCode.Item: case XmlTypeCode.Node: case XmlTypeCode.Document: case XmlTypeCode.Element: case XmlTypeCode.Attribute: case XmlTypeCode.Namespace: case XmlTypeCode.ProcessingInstruction: case XmlTypeCode.Comment: case XmlTypeCode.Text: throw new NotImplementedException (); // FIXME: check what happens case XmlTypeCode.AnyAtomicType: throw new NotImplementedException (); // FIXME: check what happens case XmlTypeCode.UntypedAtomic: case XmlTypeCode.String: // 'M' throw new NotImplementedException (); // FIXME: check what happens case XmlTypeCode.Boolean: case XmlTypeCode.Decimal: switch (dst) { case XmlTypeCode.Float: case XmlTypeCode.Double: case XmlTypeCode.Decimal: case XmlTypeCode.Boolean: return true; } return false; case XmlTypeCode.Float: case XmlTypeCode.Double: if (dst == XmlTypeCode.Decimal) // 'M' throw new NotImplementedException (); // FIXME: check what happens goto case XmlTypeCode.Decimal; case XmlTypeCode.Duration: switch (dst) { case XmlTypeCode.Duration: case XmlTypeCode.YearMonthDuration: case XmlTypeCode.DayTimeDuration: return true; } return false; case XmlTypeCode.DateTime: switch (dst) { case XmlTypeCode.DateTime: case XmlTypeCode.Time: case XmlTypeCode.Date: case XmlTypeCode.GYearMonth: case XmlTypeCode.GYear: case XmlTypeCode.GMonthDay: case XmlTypeCode.GDay: case XmlTypeCode.GMonth: return true; } return false; case XmlTypeCode.Time: switch (dst) { case XmlTypeCode.Time: case XmlTypeCode.Date: return true; } return false; case XmlTypeCode.Date: if (dst == XmlTypeCode.Time) return false; goto case XmlTypeCode.DateTime; case XmlTypeCode.GYearMonth: case XmlTypeCode.GYear: case XmlTypeCode.GMonthDay: case XmlTypeCode.GDay: case XmlTypeCode.GMonth: return src == dst; case XmlTypeCode.HexBinary: case XmlTypeCode.Base64Binary: if (src == dst) return true; switch (dst) { case XmlTypeCode.HexBinary: case XmlTypeCode.Base64Binary: return true; } return false; case XmlTypeCode.AnyUri: case XmlTypeCode.QName: case XmlTypeCode.Notation: return src == dst; case XmlTypeCode.NormalizedString: case XmlTypeCode.Token: case XmlTypeCode.Language: case XmlTypeCode.NmToken: case XmlTypeCode.Name: case XmlTypeCode.NCName: case XmlTypeCode.Id: case XmlTypeCode.Idref: case XmlTypeCode.Entity: case XmlTypeCode.Integer: case XmlTypeCode.NonPositiveInteger: case XmlTypeCode.NegativeInteger: case XmlTypeCode.Long: case XmlTypeCode.Int: case XmlTypeCode.Short: case XmlTypeCode.Byte: case XmlTypeCode.NonNegativeInteger: case XmlTypeCode.UnsignedLong: case XmlTypeCode.UnsignedInt: case XmlTypeCode.UnsignedShort: case XmlTypeCode.UnsignedByte: case XmlTypeCode.PositiveInteger: throw new NotImplementedException (); // xdt:* case XmlTypeCode.YearMonthDuration: if (dst == XmlTypeCode.DayTimeDuration) return false; goto case XmlTypeCode.Duration; case XmlTypeCode.DayTimeDuration: if (dst == XmlTypeCode.YearMonthDuration) return false; goto case XmlTypeCode.Duration; } throw new NotImplementedException (); }
public static TimeSpan ItemToDayTimeDuration (XPathItem value) { return XmlConvert.ToTimeSpan (value.Value); }
public static string ItemToString (XPathItem value) { if (value.ValueType == typeof (DateTime)) return XmlConvert.ToString ((DateTime) value.TypedValue); if (value.TypedValue is XmlQualifiedName) throw new ArgumentException ("Invalid cast from schema QName type to string type."); return value.Value; }
public static decimal ItemToDecimal (XPathItem value) { return XmlConvert.ToDecimal (value.Value); }
public static long ItemToUnsignedInt (XPathItem value) { return XmlConvert.ToInt32 (value.Value); }
public static double ItemToDouble (XPathItem value) { return XmlConvert.ToDouble (value.Value); }
public static XPathSequence FnIndexOf (XQueryContext ctx, XPathSequence items, XPathItem item) { return FnIndexOf (ctx, items, item, ctx.DefaultCollation); }
public static string ItemToDuration (XPathItem value) { return XmlConvert.ToString (XmlConvert.ToTimeSpan (value.Value)); }
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 float ItemToFloat (XPathItem value) { return XmlConvert.ToSingle (value.Value); }