Exemple #1
0
        public int Compare(object x, object y)
        {
            switch (_dataType)
            {
            case XmlDataType.Text:
                string s1     = Convert.ToString(x, _cinfo);
                string s2     = Convert.ToString(y, _cinfo);
                int    result = _cinfo.CompareInfo.Compare(s1, s2, _caseOrder != XmlCaseOrder.None ? CompareOptions.IgnoreCase : CompareOptions.None);

                if (result != 0 || _caseOrder == XmlCaseOrder.None)
                {
                    return((_order == XmlSortOrder.Ascending) ? result : -result);
                }

                // If we came this far, it means that strings s1 and s2 are
                // equal to each other when case is ignored. Now it's time to check
                // and see if they differ in case only and take into account the user
                // requested case order for sorting purposes.
                result = _cinfo.CompareInfo.Compare(s1, s2);
                return((_caseOrder == XmlCaseOrder.LowerFirst) ? result : -result);

            case XmlDataType.Number:
                double r1 = XmlConvertEx.ToXPathDouble(x);
                double r2 = XmlConvertEx.ToXPathDouble(y);
                result = r1.CompareTo(r2);
                return((_order == XmlSortOrder.Ascending) ? result : -result);

            default:
                // dataType doesn't support any other value
                throw new InvalidOperationException(SR.Xml_InvalidOperation);
            }
        } // Compare ()
Exemple #2
0
 public override object Evaluate(XPathNodeIterator nodeIterator)
 {
     return(GetValue(_op,
                     XmlConvertEx.ToXPathDouble(_opnd1.Evaluate(nodeIterator)),
                     XmlConvertEx.ToXPathDouble(_opnd2.Evaluate(nodeIterator))
                     ));
 }
        private double Number(XPathNodeIterator nodeIterator)
        {
            if (arg == null)
            {
                return(XmlConvertEx.ToXPathDouble(nodeIterator.Current.Value));
            }
            object argVal = arg.Evaluate(nodeIterator);

            switch (GetXPathType(argVal))
            {
            case XPathResultType.NodeSet:
                XPathNavigator value = arg.Advance();
                if (value != null)
                {
                    return(Number(value.Value));
                }
                break;

            case XPathResultType.String:
                return(Number((string)argVal));

            case XPathResultType.Boolean:
                return(Number((bool)argVal));

            case XPathResultType.Number:
                return((double)argVal);

            case XPathResultType_Navigator:
                return(Number(((XPathNavigator)argVal).Value));
            }
            return(double.NaN);
        }
        private string Normalize(XPathNodeIterator nodeIterator)
        {
            string str1;

            if (argList.Count > 0)
            {
                str1 = argList[0].Evaluate(nodeIterator).ToString();
            }
            else
            {
                str1 = nodeIterator.Current.Value;
            }
            str1 = XmlConvertEx.TrimString(str1);
            int           count       = 0;
            StringBuilder str2        = new StringBuilder();
            bool          FirstSpace  = true;
            XmlCharType   xmlCharType = XmlCharType.Instance;

            while (count < str1.Length)
            {
                if (!xmlCharType.IsWhiteSpace(str1[count]))
                {
                    FirstSpace = true;
                    str2.Append(str1[count]);
                }
                else if (FirstSpace)
                {
                    FirstSpace = false;
                    str2.Append(' ');
                }
                count++;
            }
            return(str2.ToString());
        }
Exemple #5
0
 void ProcessIds(XPathNavigator contextNode, string val)
 {
     string[] ids = XmlConvertEx.SplitString(val);
     for (int idx = 0; idx < ids.Length; idx++)
     {
         if (contextNode.MoveToId(ids[idx]))
         {
             Insert(outputBuffer, contextNode);
         }
     }
 }
Exemple #6
0
        private double ScanFraction()
        {
            Debug.Assert(XmlCharType.IsDigit(this.CurrentChar));
            int start = _xpathExprIndex - 2;

            Debug.Assert(0 <= start && _xpathExpr[start] == '.');
            int len = 1; // '.'

            while (XmlCharType.IsDigit(this.CurrentChar))
            {
                NextChar(); len++;
            }
            return(XmlConvertEx.ToXPathDouble(_xpathExpr.Substring(start, len)));
        }
        private static T ReadValue <T>(string value, bool ignoreCase = false)
        {
            var type = typeof(T);

            if (type == typeof(Uri))
            {
                return((T)(object)new Uri(value));
            }

            type = Nullable.GetUnderlyingType(type) ?? type;
            if (type.GetTypeInfo().IsEnum)
            {
                return((T)EnumEx.Parse(type, value, ignoreCase));
            }

            return(XmlConvertEx.FromString <T>(value));
        }
Exemple #8
0
        private double ScanNumber()
        {
            Debug.Assert(this.CurrentChar == '.' || XmlCharType.IsDigit(this.CurrentChar));
            int start = _xpathExprIndex - 1;
            int len   = 0;

            while (XmlCharType.IsDigit(this.CurrentChar))
            {
                NextChar(); len++;
            }
            if (this.CurrentChar == '.')
            {
                NextChar(); len++;
                while (XmlCharType.IsDigit(this.CurrentChar))
                {
                    NextChar(); len++;
                }
            }
            return(XmlConvertEx.ToXPathDouble(_xpathExpr.Substring(start, len)));
        }
        private string Substring(XPathNodeIterator nodeIterator)
        {
            string str1 = argList[0].Evaluate(nodeIterator).ToString();
            double num  = XmlConvertEx.XPathRound(XmlConvertEx.ToXPathDouble(argList[1].Evaluate(nodeIterator))) - 1;

            if (Double.IsNaN(num) || str1.Length <= num)
            {
                return(string.Empty);
            }
            if (argList.Count == 3)
            {
                double num1 = XmlConvertEx.XPathRound(XmlConvertEx.ToXPathDouble(argList[2].Evaluate(nodeIterator)));
                if (Double.IsNaN(num1))
                {
                    return(string.Empty);
                }
                if (num < 0 || num1 < 0)
                {
                    num1 = num + num1;
                    // NOTE: condition is true for NaN
                    if (!(num1 > 0))
                    {
                        return(string.Empty);
                    }
                    num = 0;
                }
                double maxlength = str1.Length - num;
                if (num1 > maxlength)
                {
                    num1 = maxlength;
                }
                return(str1.Substring((int)num, (int)num1));
            }
            if (num < 0)
            {
                num = 0;
            }
            return(str1.Substring((int)num));
        }
 internal static double Number(string arg)
 {
     return(XmlConvertEx.ToXPathDouble(arg));
 }
        private double Round(XPathNodeIterator nodeIterator)
        {
            double n = XmlConvertEx.ToXPathDouble(arg.Evaluate(nodeIterator));

            return(XmlConvertEx.XPathRound(n));
        }
Exemple #12
0
        public static object ChangeType(string value, Type destinationType, IXmlNamespaceResolver nsResolver)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (destinationType == null)
            {
                throw new ArgumentNullException(nameof(destinationType));
            }

            if (destinationType == BooleanType)
            {
                return(XmlConvert.ToBoolean((string)value));
            }
            if (destinationType == ByteType)
            {
                return(Int32ToByte(XmlConvert.ToInt32((string)value)));
            }
            if (destinationType == ByteArrayType)
            {
                return(StringToBase64Binary((string)value));
            }
            if (destinationType == DateTimeType)
            {
                return(UntypedAtomicToDateTime((string)value));
            }
            if (destinationType == DateTimeOffsetType)
            {
                return(XmlConvert.ToDateTimeOffset((string)value));
            }
            if (destinationType == DecimalType)
            {
                return(XmlConvert.ToDecimal((string)value));
            }
            if (destinationType == DoubleType)
            {
                return(XmlConvert.ToDouble((string)value));
            }
            if (destinationType == Int16Type)
            {
                return(Int32ToInt16(XmlConvert.ToInt32((string)value)));
            }
            if (destinationType == Int32Type)
            {
                return(XmlConvert.ToInt32((string)value));
            }
            if (destinationType == Int64Type)
            {
                return(XmlConvert.ToInt64((string)value));
            }
            if (destinationType == SByteType)
            {
                return(Int32ToSByte(XmlConvert.ToInt32((string)value)));
            }
            if (destinationType == SingleType)
            {
                return(XmlConvert.ToSingle((string)value));
            }
            if (destinationType == TimeSpanType)
            {
                return(XmlConvert.ToTimeSpan((string)value));
            }
            if (destinationType == UInt16Type)
            {
                return(Int32ToUInt16(XmlConvert.ToInt32((string)value)));
            }
            if (destinationType == UInt32Type)
            {
                return(Int64ToUInt32(XmlConvert.ToInt64((string)value)));
            }
            if (destinationType == UInt64Type)
            {
                return(DecimalToUInt64(XmlConvert.ToDecimal((string)value)));
            }
            if (destinationType == UriType)
            {
                return(XmlConvertEx.ToUri((string)value));
            }
            if (destinationType == XmlQualifiedNameType)
            {
                return(StringToQName((string)value, nsResolver));
            }
            if (destinationType == StringType)
            {
                return((string)value);
            }

            throw new InvalidCastException(SR.Format(SR.XmlConvert_TypeFromString, destinationType.Name));
        }
Exemple #13
0
 private static byte[] StringToBase64Binary(string value)
 {
     return(Convert.FromBase64String(XmlConvertEx.TrimString(value)));
 }