Exemple #1
0
        public static ContentMediaSubtype Parse(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (value == string.Empty)
            {
                throw new FormatException("The content media subtype value can not be empty.");
            }

            if (value.Length > MaxLength)
            {
                throw new FormatException($"The content media subtype value can not exceed {MaxLength} characters.");
            }

            if (value.Any(tokenCharacter => !TokenCharacter.IsAcceptable(tokenCharacter)))
            {
                throw new FormatException(
                          $"The content media subtype value must not contain spaces, control characters nor one of the unacceptable token characters {string.Join(", ", TokenCharacter.UnacceptableCharacters.Select(candidate => "'" + candidate + "'"))}");
            }

            return(new ContentMediaSubtype(value));
        }
Exemple #2
0
        public static bool TryParse(string value, out ContentMediaType parsed)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (value.Length > MaxLength)
            {
                parsed = default;
                return(false);
            }

            if (value.StartsWith("X-"))
            {
                if (value.Length == 2 ||
                    value.Skip(2).Any(tokenCharacter => !TokenCharacter.IsAcceptable(tokenCharacter)))
                {
                    parsed = default;
                    return(false);
                }
            }
            else if (!Array.Exists(
                         KnownMediaTypes,
                         candidate => string.Equals(candidate, value, StringComparison.InvariantCulture)))
            {
                parsed = default;
                return(false);
            }

            parsed = new ContentMediaType(value);
            return(true);
        }
Exemple #3
0
        public static ContentMediaType Parse(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (value == string.Empty)
            {
                throw new FormatException("The content media type value can not be empty.");
            }

            if (value.Length > MaxLength)
            {
                throw new FormatException($"The content media type value can not exceed {MaxLength} characters.");
            }

            if (value.StartsWith("X-"))
            {
                if (value.Length == 2)
                {
                    throw new FormatException(
                              "The custom content media type value must be longer than 2 characters.");
                }
                if (value.Skip(2).Any(tokenCharacter => !TokenCharacter.IsAcceptable(tokenCharacter)))
                {
                    throw new FormatException(
                              $"The custom content media type value must not contain spaces, control characters nor one of the unacceptable token characters {string.Join(", ", TokenCharacter.UnacceptableCharacters.Select(candidate => "'" + candidate + "'"))}.");
                }
            }
            else if (!Array.Exists(KnownMediaTypes, candidate => string.Equals(candidate, value, StringComparison.InvariantCulture)))
            {
                throw new FormatException(
                          $"The content media type value must be either a private media type starting with 'X-' or one of {string.Join(", ", KnownMediaTypes.Select(candidate => "'" + candidate + "'"))}.");
            }

            return(new ContentMediaType(value));
        }
Exemple #4
0
        public static bool TryParse(string value, out ContentMediaSubtype parsed)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (value == string.Empty || value.Length > MaxLength || value.Any(tokenCharacter => !TokenCharacter.IsAcceptable(tokenCharacter)))
            {
                parsed = default;
                return(false);
            }

            parsed = new ContentMediaSubtype(value);
            return(true);
        }