private static bool CreateDataUri(ParseResult result, out DataUri uri) { uri = null; if (string.IsNullOrEmpty(result.Data)) { result.SetFailure(ParseFailureKind.Format, UnrecognizedFormat, failureArgumentName: "uriString"); return(false); } if (string.IsNullOrEmpty(result.Encoding)) { uri = new TextDataUri(result.MediaType, result.Parameters, result.Data); return(true); } if (string.Equals(result.Encoding, "base64", StringComparison.CurrentCultureIgnoreCase)) { try { uri = new Base64DataUri(result.MediaType, result.Parameters, Convert.FromBase64String(result.Data)); return(true); } catch (FormatException ex) { result.SetFailure(ParseFailureKind.FormatWithInnerException, UnrecognizedFormat, failureArgumentName: "uriString", innerException: ex); return(false); } catch (Exception ex) { result.SetFailure(ParseFailureKind.NativeException, null, innerException: ex); return(false); } } result.SetFailure(ParseFailureKind.Format, string.Format("Unrecognized data uri for unknown data encoding: {0}.", result.Encoding), failureArgumentName: "uriString"); return(false); }
/// <summary> /// Converts the string representation of a uri to the equivalent data uri. /// </summary> /// <param name="uriString">The uri string to parse.</param> /// <param name="uri"> /// The data uri 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 will be <see langword="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); }
/// <summary> /// Determines if this data uri is equal to another data uri. /// </summary> /// <param name="other"><see cref="DataUri"/> to compare with for equality.</param> /// <returns><c>true</c> if the data uris are equal. <c>false</c> if the data uris are not equal.</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); }
/// <summary> /// Determines if this data uri is equal to another data uri. /// </summary> /// <param name="other"><see cref="DataUri"/> to compare with for equality.</param> /// <returns><c>true</c> if the data uris are equal. <c>false</c> if the data uris are not equal.</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)); }
/// <summary> /// Determines if this data uri is equal to another data uri. /// </summary> /// <param name="other"><see cref="DataUri"/> to compare with for equality.</param> /// <returns><c>true</c> if the data uris are equal. <c>false</c> if the data uris are not equal.</returns> protected virtual bool Equals(DataUri other) { if (!string.Equals(other.MediaType, MediaType, StringComparison.CurrentCultureIgnoreCase) || Parameters.Count != other.Parameters.Count) { return(false); } foreach (string parameterName in Parameters) { if (Parameters[parameterName] != other.Parameters[parameterName]) { return(false); } } return(true); }