Ejemplo n.º 1
0
        public static object GetTypedValue(this XPathItem item)
        {
            XPathNavigator nav = item as XPathNavigator;

            if (nav == null)
            {
                return(item.TypedValue);
            }
            IXmlSchemaInfo schemaInfo = nav.SchemaInfo;

            if (schemaInfo == null || schemaInfo.SchemaType == null)
            {
                switch (nav.NodeType)
                {
                case XPathNodeType.Comment:
                case XPathNodeType.ProcessingInstruction:
                case XPathNodeType.Namespace:
                    return(nav.Value);

                default:
                    return(new UntypedAtomic(nav.Value));
                }
            }
            XmlTypeCode typeCode = schemaInfo.SchemaType.TypeCode;

            if (typeCode == XmlTypeCode.AnyAtomicType && schemaInfo.MemberType != null)
            {
                typeCode = schemaInfo.MemberType.TypeCode;
            }
            switch (typeCode)
            {
            case XmlTypeCode.UntypedAtomic:
                return(new UntypedAtomic(nav.Value));

            case XmlTypeCode.Integer:
            case XmlTypeCode.PositiveInteger:
            case XmlTypeCode.NegativeInteger:
            case XmlTypeCode.NonPositiveInteger:
                return((Integer)(decimal)nav.TypedValue);

            case XmlTypeCode.Date:
                return(DateValue.Parse(nav.Value));

            case XmlTypeCode.DateTime:
                return(DateTimeValue.Parse(nav.Value));

            case XmlTypeCode.Time:
                return(TimeValue.Parse(nav.Value));

            case XmlTypeCode.Duration:
                return(DurationValue.Parse(nav.Value));

            case XmlTypeCode.DayTimeDuration:
                return(new DayTimeDurationValue((TimeSpan)nav.TypedValue));

            case XmlTypeCode.YearMonthDuration:
                return(new YearMonthDurationValue((TimeSpan)nav.TypedValue));

            case XmlTypeCode.GDay:
                return(GDayValue.Parse(nav.Value));

            case XmlTypeCode.GMonth:
                return(GMonthValue.Parse(nav.Value));

            case XmlTypeCode.GMonthDay:
                return(GMonthDayValue.Parse(nav.Value));

            case XmlTypeCode.GYear:
                return(GYearValue.Parse(nav.Value));

            case XmlTypeCode.GYearMonth:
                return(GYearMonthValue.Parse(nav.Value));

            case XmlTypeCode.QName:
            case XmlTypeCode.Notation:
            {
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
                ExtFuncs.ScanLocalNamespaces(nsmgr, nav.Clone(), true);
                if (schemaInfo.SchemaType.TypeCode == XmlTypeCode.Notation)
                {
                    return(NotationValue.Parse(nav.Value, nsmgr));
                }
                else
                {
                    return(QNameValue.Parse(nav.Value, nsmgr));
                }
            }

            case XmlTypeCode.AnyUri:
                return(new AnyUriValue(nav.Value));

            case XmlTypeCode.HexBinary:
                return(new HexBinaryValue((byte[])nav.TypedValue));

            case XmlTypeCode.Base64Binary:
                return(new Base64BinaryValue((byte[])nav.TypedValue));

            case XmlTypeCode.Idref:
                if (schemaInfo.SchemaType == SequenceType.XmlSchema.IDREFS)
                {
                    return(new IDREFSValue((string[])nav.TypedValue));
                }
                goto default;

            case XmlTypeCode.NmToken:
                if (schemaInfo.SchemaType == SequenceType.XmlSchema.NMTOKENS)
                {
                    return(new NMTOKENSValue((string[])nav.TypedValue));
                }
                goto default;

            case XmlTypeCode.Entity:
                if (schemaInfo.SchemaType == SequenceType.XmlSchema.ENTITIES)
                {
                    return(new ENTITIESValue((string[])nav.TypedValue));
                }
                goto default;

            default:
                return(nav.TypedValue);
            }
        }
Ejemplo n.º 2
0
        public static object ChangeType(XmlSchemaType xmlType, object value, SequenceType type,
                                        XmlNameTable nameTable, XmlNamespaceManager nsmgr)
        {
            if (type.TypeCode == XmlTypeCode.AnyAtomicType || xmlType.TypeCode == type.TypeCode)
            {
                return(value);
            }
            try
            {
                switch (xmlType.TypeCode)
                {
                case XmlTypeCode.String:
                case XmlTypeCode.UntypedAtomic:
                    switch (type.TypeCode)
                    {
                    case XmlTypeCode.UntypedAtomic:
                        return(new UntypedAtomic(value.ToString()));

                    case XmlTypeCode.String:
                        return(value.ToString());

                    case XmlTypeCode.DateTime:
                        return(DateTimeValue.Parse(value.ToString()));

                    case XmlTypeCode.Date:
                        return(DateValue.Parse(value.ToString()));

                    case XmlTypeCode.Time:
                        return(TimeValue.Parse(value.ToString()));

                    case XmlTypeCode.GYearMonth:
                        return(GYearMonthValue.Parse(value.ToString()));

                    case XmlTypeCode.GYear:
                        return(GYearValue.Parse(value.ToString()));

                    case XmlTypeCode.GMonth:
                        return(GMonthValue.Parse(value.ToString()));

                    case XmlTypeCode.GMonthDay:
                        return(GMonthDayValue.Parse(value.ToString()));

                    case XmlTypeCode.GDay:
                        return(GDayValue.Parse(value.ToString()));

                    case XmlTypeCode.Duration:
                        return(DurationValue.Parse(value.ToString()));

                    case XmlTypeCode.QName:
                        if (xmlType.TypeCode == XmlTypeCode.UntypedAtomic)
                        {
                            throw new XPath2Exception("XPTY0004", Properties.Resources.XPTY0004,
                                                      new SequenceType(xmlType, XmlTypeCardinality.One, null), type);
                        }
                        return(QNameValue.Parse(value.ToString(), nsmgr));

                    case XmlTypeCode.Notation:
                        return(NotationValue.Parse(value.ToString(), nsmgr));

                    case XmlTypeCode.AnyUri:
                        return(new AnyUriValue(value.ToString()));

                    default:
                    {
                        string text = value.ToString();
                        object res  = type.SchemaType.Datatype.ParseValue(text, nameTable, nsmgr);
                        switch (type.TypeCode)
                        {
                        case XmlTypeCode.Integer:
                        case XmlTypeCode.PositiveInteger:
                        case XmlTypeCode.NegativeInteger:
                        case XmlTypeCode.NonPositiveInteger:
                        case XmlTypeCode.NonNegativeInteger:
                            return((Integer)Convert.ToDecimal(res));

                        case XmlTypeCode.DayTimeDuration:
                            return(new DayTimeDurationValue((TimeSpan)res));

                        case XmlTypeCode.YearMonthDuration:
                            return(new YearMonthDurationValue((TimeSpan)res));

                        case XmlTypeCode.HexBinary:
                            return(new HexBinaryValue((byte[])res));

                        case XmlTypeCode.Base64Binary:
                            if (text.EndsWith("==") && (text.Length < 3 || "AQgw".IndexOf(text[text.Length - 3]) == -1))
                            {
                                throw new XPath2Exception("FORG0001", Properties.Resources.FORG0001, value);
                            }
                            return(new Base64BinaryValue((byte[])res));

                        case XmlTypeCode.Idref:
                            if (type.SchemaType == SequenceType.XmlSchema.IDREFS)
                            {
                                return(new IDREFSValue((string[])res));
                            }
                            goto default;

                        case XmlTypeCode.NmToken:
                            if (type.SchemaType == SequenceType.XmlSchema.NMTOKENS)
                            {
                                return(new NMTOKENSValue((string[])res));
                            }
                            goto default;

                        case XmlTypeCode.Entity:
                            if (type.SchemaType == SequenceType.XmlSchema.ENTITIES)
                            {
                                return(new ENTITIESValue((string[])res));
                            }
                            goto default;

                        default:
                            return(res);
                        }
                    }
                    }

                case XmlTypeCode.Boolean:
                    switch (type.TypeCode)
                    {
                    case XmlTypeCode.Decimal:
                    case XmlTypeCode.Float:
                    case XmlTypeCode.Double:
                    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:
                        return(ChangeType(value, type.ItemType));

                    case XmlTypeCode.String:
                        return(XPath2Convert.ToString((bool)value));

                    case XmlTypeCode.UntypedAtomic:
                        return(new UntypedAtomic(XPath2Convert.ToString((bool)value)));
                    }
                    break;

                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:
                case XmlTypeCode.Decimal:
                case XmlTypeCode.Float:
                case XmlTypeCode.Double:
                    switch (type.TypeCode)
                    {
                    case XmlTypeCode.String:
                        return(ToString(value));

                    case XmlTypeCode.UntypedAtomic:
                        return(new UntypedAtomic(ToString(value)));

                    case XmlTypeCode.Boolean:
                        return(CoreFuncs.BooleanValue(value));

                    case XmlTypeCode.AnyUri:
                        throw new XPath2Exception("XPTY0004", Properties.Resources.XPTY0004,
                                                  new SequenceType(xmlType, XmlTypeCardinality.One, null), type);

                    default:
                        return(ChangeType(value, type.ItemType));
                    }

                default:
                {
                    IXmlConvertable convert = value as IXmlConvertable;
                    if (convert != null)
                    {
                        return(convert.ValueAs(type, nsmgr));
                    }
                    if (type.TypeCode == XmlTypeCode.String)
                    {
                        return(ToString(value));
                    }
                    if (type.TypeCode == XmlTypeCode.UntypedAtomic)
                    {
                        return(new UntypedAtomic(ToString(value)));
                    }
                    return(type.SchemaType.Datatype.ChangeType(value, type.ValueType));
                }
                }
            }
            catch (XmlSchemaException ex)
            {
                throw new XPath2Exception(ex.Message, ex);
            }
            catch (InvalidCastException)
            {
                throw new XPath2Exception("XPTY0004", Properties.Resources.XPTY0004,
                                          new SequenceType(xmlType, XmlTypeCardinality.One, null), type);
            }
            catch (FormatException)
            {
                throw new XPath2Exception("XPTY0004", Properties.Resources.XPTY0004,
                                          new SequenceType(xmlType, XmlTypeCardinality.One, null), type);
            }
            throw new XPath2Exception("XPTY0004", Properties.Resources.XPTY0004,
                                      new SequenceType(xmlType, XmlTypeCardinality.One, null), type);
        }