Example #1
0
        /// <summary>
        /// Parse the given uri text.
        /// Try to parse with a specific Uri literal parser registered for the target EdmType.
        /// If no parser is registered, try to parse with the general parsers.
        /// This method is public because of the Interface, but the Singleton instance in internal so it could not be accessed by clients.
        /// </summary>
        /// <param name="text">Part of the Uri which has to be parsed to a value of EdmType <paramref name="targetType"/></param>
        /// <param name="targetType">The type which the uri text has to be parsed to</param>
        /// <param name="parsingException">Assign the exception only in case the text could be parsed to the <paramref name="targetType"/> but failed during the parsing process</param>
        /// <returns>If parsing process has succeeded, returns the parsed object, otherwise returns 'Null'</returns>
        public object ParseUriStringToType(string text, IEdmTypeReference targetType, out UriLiteralParsingException parsingException)
        {
            IUriLiteralParser uriLiteralParserForEdmType = CustomUriLiteralParsers.GetUriLiteralParserByEdmType(targetType);

            // Search for Uri literal parser which is registered for the given EdmType
            if (uriLiteralParserForEdmType != null)
            {
                return(uriLiteralParserForEdmType.ParseUriStringToType(text, targetType, out parsingException));
            }

            // Parse with all the general parsers
            // Stop when a parser succeeded parsing the text.
            IUriLiteralParser[] localCustomUriLiteralParsers = CustomUriLiteralParsers.customUriLiteralParsers;
            foreach (IUriLiteralParser customUriLiteralParser in localCustomUriLiteralParsers)
            {
                // Try to parse
                object targetValue = customUriLiteralParser.ParseUriStringToType(text, targetType, out parsingException);

                // The uriCustomParser could parse the given targetType but failed during the parsing process
                if (parsingException != null)
                {
                    return(null);
                }

                // In case of no exception and no value - The parse cannot parse the given text
                if (targetValue != null)
                {
                    return(targetValue);
                }
            }

            // No uriCustomParser could parse the requested uri text.
            parsingException = null;
            return(null);
        }
Example #2
0
        /// <summary>
        /// Add a custom 'IUriLiteralParser' which will be called to parse a value of the given EdmType during the UriParsing process.
        /// </summary>
        /// <param name="edmTypeReference">The EdmType the Uri literal parser can parse.</param>
        /// <param name="customUriLiteralParser">The custom uri type parser to add.</param>
        /// <exception cref="ArgumentNullException"><paramref name="customUriLiteralParser"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="edmTypeReference"/> is null.</exception>
        /// <exception cref="ODataException">Another Uri literal parser is already registered for the given EdmType</exception>
        public static void AddCustomUriLiteralParser(IEdmTypeReference edmTypeReference, IUriLiteralParser customUriLiteralParser)
        {
            ExceptionUtils.CheckArgumentNotNull(edmTypeReference, "edmTypeReference");
            ExceptionUtils.CheckArgumentNotNull(customUriLiteralParser, "customUriLiteralParser");

            lock (CustomUriLiteralParsers.Locker)
            {
                if (CustomUriLiteralParsers.IsEdmTypeAlreadyRegistered(edmTypeReference))
                {
                    throw new ODataException(ODataErrorStrings.UriCustomTypeParsers_AddCustomUriTypeParserEdmTypeExists(edmTypeReference.FullName()));
                }

                CustomUriLiteralParsers.customUriLiteralParserPerEdmType = CustomUriLiteralParsers.customUriLiteralParserPerEdmType.Concat(
                    new UriLiteralParserPerEdmType[]
                {
                    new UriLiteralParserPerEdmType
                    {
                        EdmTypeOfUriParser = edmTypeReference,
                        UriLiteralParser   = customUriLiteralParser
                    }
                })
                                                                           .ToArray();
            }
        }