示例#1
0
 private static bool CreateDataUri(ParseResult result, out DataUri uri)
 {
     uri = null;
     if (string.IsNullOrEmpty(result.Data))
     {
         result.SetFailure(ParseFailureKind.Format, Strings.DataUri_Invalid_Format, failureArgumentName: "uriString");
         return(false);
     }
     if (string.IsNullOrEmpty(result.Encoding))
     {
         uri = new TextDataUri(result.MediaType, result.Parameters, HttpUtility.UrlDecode(result.Data));
         return(true);
     }
     if (string.Equals(result.Encoding, "base64", StringComparison.InvariantCultureIgnoreCase))
     {
         try
         {
             uri = new Base64DataUri(result.MediaType, result.Parameters, Convert.FromBase64String(result.Data));
             return(true);
         }
         catch (FormatException ex)
         {
             result.SetFailure(ParseFailureKind.FormatWithInnerException, Strings.DataUri_Invalid_Format, failureArgumentName: "uriString", innerException: ex);
             return(false);
         }
         catch (Exception ex)
         {
             result.SetFailure(ParseFailureKind.NativeException, null, innerException: ex);
             return(false);
         }
     }
     result.SetFailure(ParseFailureKind.Format, string.Format(Strings.DataUri_Unknown_Encoding, result.Encoding), failureArgumentName: "uriString");
     return(false);
 }
示例#2
0
        /// <summary>
        /// Converts the string to the equivalent data URI.
        /// </summary>
        /// <param name="uriString">The string to convert.</param>
        /// <param name="uri">The instance that will contain the parsed value. If the method returns <c>true</c>, result contains a valid data URI. If the method returns <c>false</c>, result is null.</param>
        /// <returns><c>true</c> if the parse operation was successful; otherwise, <c>false</c>.</returns>
        public static bool TryParse(string uriString, out DataUri uri)
        {
            ParseResult result = new ParseResult(false);

            if (TryParse(uriString, result) && CreateDataUri(result, out uri))
            {
                return(true);
            }
            uri = null;
            return(false);
        }
示例#3
0
        /// <summary>
        /// Returns a value indicating whether this instance and a specified <see cref="DataUri"/> object represent the same value.
        /// </summary>
        /// <param name="other">An object to compare to this instance.</param>
        /// <returns><c>true</c> if <paramref name="other"/> is a <see cref="TextDataUri"/> and equal to this instance; otherwise, <c>false</c>.</returns>
        protected override bool Equals(DataUri other)
        {
            if (!base.Equals(other))
            {
                return(false);
            }
            var uri = other as TextDataUri;

            if (uri == null)
            {
                return(false);
            }
            return(Content == uri.Content);
        }
示例#4
0
        /// <summary>
        /// Returns a value indicating whether this instance and a specified <see cref="DataUri"/> object represent the same value.
        /// </summary>
        /// <param name="other">An object to compare to this instance.</param>
        /// <returns><c>true</c> if <paramref name="other"/> is a <see cref="Base64DataUri"/> and equal to this instance; otherwise, <c>false</c>.</returns>
        protected override bool Equals(DataUri other)
        {
            if (!base.Equals(other))
            {
                return(false);
            }
            var uri = other as Base64DataUri;

            if (uri == null)
            {
                return(false);
            }
            return(Content.SequenceEqual(uri.Content));
        }
示例#5
0
 /// <summary>
 /// Returns a value indicating whether this instance and a specified <see cref="DataUri"/> object represent the same value.
 /// </summary>
 /// <param name="other">An object to compare to this instance.</param>
 /// <returns><c>true</c> if <paramref name="other"/> is equal to this instance; otherwise, <c>false</c>.</returns>
 protected virtual bool Equals(DataUri other)
 {
     if (!string.Equals(other.MediaType, MediaType, StringComparison.InvariantCultureIgnoreCase) || Parameters.Count != other.Parameters.Count)
     {
         return(false);
     }
     foreach (string parameterName in Parameters)
     {
         if (Parameters[parameterName] != other.Parameters[parameterName])
         {
             return(false);
         }
     }
     return(true);
 }