ValueAs() public method

public ValueAs ( Type returnType ) : object
returnType System.Type
return object
Exemplo n.º 1
0
        public bool Match(XPathItem item, XQueryContext context)
        {
            switch (TypeCode)
            {
                case XmlTypeCode.None:
                    return false;

                case XmlTypeCode.Item:
                    return true;

                case XmlTypeCode.Node:
                    return item.IsNode;

                case XmlTypeCode.AnyAtomicType:
                    return !item.IsNode;

                case XmlTypeCode.UntypedAtomic:
                    return !item.IsNode && item.XmlType == XmlSchema.UntypedAtomic;

                case XmlTypeCode.Document:
                    {
                        XPathNavigator nav = item as XPathNavigator;
                        if (nav != null)
                        {
                            if (nav.NodeType == XPathNodeType.Root)
                            {
                                XPathNavigator cur = nav.Clone();                                
                                if (SchemaElement == null)
                                {
                                    if (cur.MoveToChild(XPathNodeType.Element) && MatchName(cur, context))
                                    {
                                        if (SchemaType == null || SchemaType == XmlSchema.UntypedAtomic)
                                            return true;
                                        IXmlSchemaInfo schemaInfo = cur.SchemaInfo;
                                        if (schemaInfo != null)
                                        {
                                            if (XmlSchemaType.IsDerivedFrom(schemaInfo.SchemaType, SchemaType, XmlSchemaDerivationMethod.Empty))
                                                return !schemaInfo.IsNil || Nillable;
                                        }
                                        else
                                            return XmlSchemaType.IsDerivedFrom(XmlSchema.UntypedAtomic, SchemaType, XmlSchemaDerivationMethod.Empty);
                                    }
                                }
                                else
                                {
                                    if (!cur.MoveToChild(XPathNodeType.Element))
                                        return false;
                                    IXmlSchemaInfo schemaInfo = cur.SchemaInfo;
                                    if (schemaInfo != null)
                                        return schemaInfo.SchemaElement.QualifiedName == SchemaElement.QualifiedName;
                                }
                            }
                        }
                    }
                    break;

                case XmlTypeCode.Element:
                    {
                        XPathNavigator nav = item as XPathNavigator;
                        if (nav != null && nav.NodeType == XPathNodeType.Element)
                        {
                            if (SchemaElement == null)
                            {
                                if (MatchName(nav, context))
                                {
                                    if (SchemaType == null || SchemaType == XmlSchema.UntypedAtomic)
                                        return true;
                                    IXmlSchemaInfo schemaInfo = nav.SchemaInfo;
                                    if (schemaInfo != null)
                                    {
                                        if (XmlSchemaType.IsDerivedFrom(schemaInfo.SchemaType, SchemaType, XmlSchemaDerivationMethod.Empty))
                                            return !schemaInfo.IsNil || Nillable;
                                    }
                                    else
                                        return XmlSchemaType.IsDerivedFrom(XmlSchema.UntypedAtomic, SchemaType, XmlSchemaDerivationMethod.Empty);
                                }
                            }
                            else
                            {
                                IXmlSchemaInfo schemaInfo = nav.SchemaInfo;
                                if (schemaInfo != null)
                                    return schemaInfo.SchemaElement.QualifiedName == SchemaElement.QualifiedName;
                            }
                        }
                    }
                    break;

                case XmlTypeCode.Attribute:
                    {
                        XPathNavigator nav = item as XPathNavigator;
                        if (nav != null && nav.NodeType == XPathNodeType.Attribute)
                        {
                            if (SchemaAttribute == null)
                            {
                                if (MatchName(nav, context))
                                {
                                    if (SchemaType == null || SchemaType == XmlSchema.UntypedAtomic) 
                                        return true;
                                    IXmlSchemaInfo schemaInfo = nav.SchemaInfo;
                                    if (schemaInfo == null)
                                        return XmlSchemaType.IsDerivedFrom(XmlSchema.UntypedAtomic, SchemaType, XmlSchemaDerivationMethod.Empty);
                                    else
                                        return XmlSchemaType.IsDerivedFrom(schemaInfo.SchemaType, SchemaType, XmlSchemaDerivationMethod.Empty);
                                }
                            }
                            else
                            {
                                IXmlSchemaInfo schemaInfo = nav.SchemaInfo;
                                if (schemaInfo != null)
                                    return schemaInfo.SchemaAttribute.QualifiedName == SchemaAttribute.QualifiedName;
                            }
                        }
                    }
                    break;

                case XmlTypeCode.ProcessingInstruction:
                    {
                        XPathNavigator nav = item as XPathNavigator;
                        if (nav != null)
                            return (nav.NodeType == XPathNodeType.ProcessingInstruction &&
                                (NameTest.IsNameWildcard || NameTest.Name == nav.Name));
                    }
                    break;

                case XmlTypeCode.Comment:
                    {
                        XPathNavigator nav = item as XPathNavigator;
                        if (nav != null)
                            return nav.NodeType == XPathNodeType.Comment;
                    }
                    break;

                case XmlTypeCode.Text:
                    {
                        XPathNavigator nav = item as XPathNavigator;
                        if (nav != null)
                            return nav.NodeType == XPathNodeType.Text ||
                                nav.NodeType == XPathNodeType.SignificantWhitespace;
                    }
                    break;                

                case XmlTypeCode.PositiveInteger:
                    switch (item.XmlType.TypeCode)
                    {
                        case XmlTypeCode.Byte:
                        case XmlTypeCode.Short:
                        case XmlTypeCode.Int:
                        case XmlTypeCode.Long:
                        case XmlTypeCode.Integer:
                            return (decimal)item.ValueAs(typeof(System.Decimal)) > 0;
                    }
                    break;

                case XmlTypeCode.NegativeInteger:
                    switch (item.XmlType.TypeCode)
                    {
                        case XmlTypeCode.Byte:
                        case XmlTypeCode.Short:
                        case XmlTypeCode.Int:
                        case XmlTypeCode.Long:
                        case XmlTypeCode.Integer:
                            return (decimal)item.ValueAs(typeof(System.Decimal)) < 0;
                    }
                    break;

                case XmlTypeCode.NonPositiveInteger:
                    switch (item.XmlType.TypeCode)
                    {
                        case XmlTypeCode.Byte:
                        case XmlTypeCode.Short:
                        case XmlTypeCode.Int:
                        case XmlTypeCode.Long:
                        case XmlTypeCode.Integer:
                            return (decimal)item.ValueAs(typeof(System.Decimal)) <= 0;
                    }
                    break;

                case XmlTypeCode.NonNegativeInteger:
                    switch (item.XmlType.TypeCode)
                    {
                        case XmlTypeCode.Byte:
                        case XmlTypeCode.Short:
                        case XmlTypeCode.Int:
                        case XmlTypeCode.Long:
                        case XmlTypeCode.Integer:
                            return (decimal)item.ValueAs(typeof(System.Decimal)) >= 0;

                        case XmlTypeCode.UnsignedByte:
                        case XmlTypeCode.UnsignedShort:
                        case XmlTypeCode.UnsignedInt:
                        case XmlTypeCode.UnsignedLong:
                            return true;
                    }
                    break;

                case XmlTypeCode.Integer:
                    switch (item.XmlType.TypeCode)
                    {
                        case XmlTypeCode.Byte:
                        case XmlTypeCode.Short:
                        case XmlTypeCode.Int:
                        case XmlTypeCode.Long:
                        case XmlTypeCode.Integer:
                        case XmlTypeCode.UnsignedByte:
                        case XmlTypeCode.UnsignedShort:
                        case XmlTypeCode.UnsignedInt:
                        case XmlTypeCode.UnsignedLong:
                            return true;

                        case XmlTypeCode.Decimal:
                            decimal value = (decimal)item.ValueAs(typeof(System.Decimal));
                            return value == Math.Truncate(value);
                    }
                    break;

                case XmlTypeCode.Entity:
                    return (item.XmlType.TypeCode == XmlTypeCode.String) ||
                           (item.XmlType.TypeCode == XmlTypeCode.Entity);

                default:
                    {
                        if (item.XmlType != null)
                            return XmlSchemaType.IsDerivedFrom(item.XmlType, SchemaType, XmlSchemaDerivationMethod.Empty);
                    }
                    break;
            }
            return false;
        }