Exemplo n.º 1
0
        /// <summary>
        /// Try to parse the given text to a Geometry object.
        /// </summary>
        /// <param name="text">Text to parse.</param>
        /// <param name="targetValue">Geometry to return.</param>
        /// <returns>True if succeeds, false if not.</returns>
        private static bool TryUriStringToGeometry(string text, out Geometry targetValue)
        {
            if (!TryRemoveLiteralPrefix(ExpressionConstants.LiteralPrefixGeometry, ref text))
            {
                targetValue = default(Geometry);
                return(false);
            }

            if (!TryRemoveQuotes(ref text))
            {
                targetValue = default(Geometry);
                return(false);
            }

            try
            {
                targetValue = LiteralUtils.ParseGeometry(text);
                return(true);
            }
            catch (ParseErrorException)
            {
                targetValue = default(Geometry);
                return(false);
            }
        }
Exemplo n.º 2
0
 private static bool TryUriStringToGeometry(string text, out Geometry targetValue)
 {
     if (!TryRemoveLiteralPrefix("geometry", ref text))
     {
         targetValue = null;
         return(false);
     }
     if (!TryRemoveQuotes(ref text))
     {
         targetValue = null;
         return(false);
     }
     try
     {
         targetValue = LiteralUtils.ParseGeometry(text);
         return(true);
     }
     catch (ParseErrorException)
     {
         targetValue = null;
         return(false);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Write the Uri string representation of the given CLR object literal to the given builder.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to write the <paramref name="clrLiteral"/> to.</param>
        /// <param name="clrLiteral">The object to write as literal.</param>
        private static void WriteClrLiteral(StringBuilder builder, object clrLiteral)
        {
            if (clrLiteral == null)
            {
                builder.Append(ExpressionConstants.KeywordNull);
                return;
            }

            TypeCode code = Type.GetTypeCode(clrLiteral.GetType());

            switch (code)
            {
            case TypeCode.Byte:
                builder.Append(((byte)clrLiteral).ToString(ODataUriBuilderUtils.IntegerFormat, CultureInfo.InvariantCulture));
                return;

            case TypeCode.Boolean:
                builder.Append((bool)clrLiteral ? ExpressionConstants.KeywordTrue : ExpressionConstants.KeywordFalse);
                return;

            case TypeCode.DateTime:
                builder.Append(ExpressionConstants.LiteralPrefixDateTime);
                builder.Append(ExpressionConstants.SymbolSingleQuote);
                builder.Append(((DateTime)clrLiteral).ToString(ODataUriBuilderUtils.DateTimeFormat, CultureInfo.InvariantCulture));
                builder.Append(ExpressionConstants.SymbolSingleQuote);
                return;

            case TypeCode.Decimal:
                builder.Append(((Decimal)clrLiteral).ToString(ODataUriBuilderUtils.DecimalFormatInfo));
                builder.Append(ExpressionConstants.LiteralSuffixDecimal);
                return;

            case TypeCode.Double:
                builder.Append(((Double)clrLiteral).ToString(ODataUriBuilderUtils.DoubleFormat, ODataUriBuilderUtils.DoubleFormatInfo));
                return;

            case TypeCode.Int16:
                builder.Append(((short)clrLiteral).ToString(ODataUriBuilderUtils.IntegerFormat, CultureInfo.InvariantCulture));
                return;

            case TypeCode.Int32:
                builder.Append(((Int32)clrLiteral).ToString(ODataUriBuilderUtils.IntegerFormat, CultureInfo.InvariantCulture));
                return;

            case TypeCode.Int64:
                builder.Append(((Int64)clrLiteral).ToString(ODataUriBuilderUtils.IntegerFormat, CultureInfo.InvariantCulture));
                builder.Append(ExpressionConstants.LiteralSuffixInt64);
                return;

            case TypeCode.SByte:
                builder.Append(((SByte)clrLiteral).ToString(ODataUriBuilderUtils.IntegerFormat, CultureInfo.InvariantCulture));
                return;

            case TypeCode.Single:
                builder.Append(((float)clrLiteral).ToString(ODataUriBuilderUtils.FloatFormat, CultureInfo.InvariantCulture));
                builder.Append(ExpressionConstants.LiteralSuffixSingle);
                return;

            case TypeCode.String:
                builder.Append(ExpressionConstants.SymbolSingleQuote);
                builder.Append(Uri.EscapeDataString(ODataUriBuilderUtils.Escape(clrLiteral.ToString())));
                builder.Append(ExpressionConstants.SymbolSingleQuote);
                return;

            default:
                break;
            }

            if (clrLiteral is DateTimeOffset)
            {
                builder.Append(ExpressionConstants.LiteralPrefixDateTimeOffset);
                builder.Append(ExpressionConstants.SymbolSingleQuote);
                builder.Append(((DateTimeOffset)clrLiteral).ToString(ODataUriBuilderUtils.DateTimeOffsetFormat, CultureInfo.InvariantCulture));
                builder.Append(ExpressionConstants.SymbolSingleQuote);
                return;
            }

            if (clrLiteral is TimeSpan)
            {
                builder.Append(ExpressionConstants.LiteralPrefixTime);
                builder.Append(ExpressionConstants.SymbolSingleQuote);
                builder.Append(XmlConvert.ToString((TimeSpan)clrLiteral));
                builder.Append(ExpressionConstants.SymbolSingleQuote);
                return;
            }

            if (clrLiteral is Guid)
            {
                builder.Append(ExpressionConstants.LiteralPrefixGuid);
                builder.Append(ExpressionConstants.SymbolSingleQuote);
                builder.Append(((Guid)clrLiteral).ToString(ODataUriBuilderUtils.IntegerFormat));
                builder.Append(ExpressionConstants.SymbolSingleQuote);
                return;
            }

            byte[] bytes = clrLiteral as byte[];
            if (bytes != null)
            {
                builder.Append(ExpressionConstants.LiteralPrefixBinary);
                builder.Append(ExpressionConstants.SymbolSingleQuote);
                foreach (byte @byte in bytes)
                {
                    builder.Append(@byte.ToString(ODataUriBuilderUtils.BinaryFormat, CultureInfo.InvariantCulture));
                }

                builder.Append(ExpressionConstants.SymbolSingleQuote);
                return;
            }

            Geography geography = clrLiteral as Geography;

            if (geography != null)
            {
                builder.Append(ExpressionConstants.LiteralPrefixGeography);
                builder.Append(ExpressionConstants.SymbolSingleQuote);

                builder.Append(LiteralUtils.ToWellKnownText(geography));

                builder.Append(ExpressionConstants.SymbolSingleQuote);
                return;
            }

            Geometry geometry = clrLiteral as Geometry;

            if (geometry != null)
            {
                builder.Append(ExpressionConstants.LiteralPrefixGeometry);
                builder.Append(ExpressionConstants.SymbolSingleQuote);

                builder.Append(LiteralUtils.ToWellKnownText(geometry));

                builder.Append(ExpressionConstants.SymbolSingleQuote);
                return;
            }

            ODataUriBuilderUtils.NotSupported(clrLiteral.GetType());
        }