|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Globalization; |
| 3 | + |
| 4 | +namespace MauiNativePdfView.Abstractions; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// TypeConverter for converting strings to PdfSource types in XAML. |
| 8 | +/// </summary> |
| 9 | +/// <remarks> |
| 10 | +/// Supports the following string formats: |
| 11 | +/// <list type="bullet"> |
| 12 | +/// <item><description>URLs (http:// or https://) → UriPdfSource</description></item> |
| 13 | +/// <item><description>Asset paths (asset://) → AssetPdfSource</description></item> |
| 14 | +/// <item><description>File URIs (file://) → FilePdfSource</description></item> |
| 15 | +/// <item><description>Simple filenames (no path separators) → AssetPdfSource</description></item> |
| 16 | +/// <item><description>Other strings → FilePdfSource</description></item> |
| 17 | +/// </list> |
| 18 | +/// </remarks> |
| 19 | +public class PdfSourceTypeConverter : TypeConverter |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// Returns whether this converter can convert from the specified type. |
| 23 | + /// </summary> |
| 24 | + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) |
| 25 | + => sourceType == typeof(string) || sourceType == typeof(Uri) || base.CanConvertFrom(context, sourceType); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Returns whether this converter can convert to the specified type. |
| 29 | + /// </summary> |
| 30 | + public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) |
| 31 | + => destinationType == typeof(string) || base.CanConvertTo(context, destinationType); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Converts from a string or Uri to a PdfSource. |
| 35 | + /// </summary> |
| 36 | + public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) |
| 37 | + { |
| 38 | + if (value is Uri uri) |
| 39 | + { |
| 40 | + return new UriPdfSource(uri); |
| 41 | + } |
| 42 | + |
| 43 | + if (value is string stringValue) |
| 44 | + { |
| 45 | + return ParseString(stringValue); |
| 46 | + } |
| 47 | + |
| 48 | + return base.ConvertFrom(context, culture, value); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Converts a PdfSource to a string representation. |
| 53 | + /// </summary> |
| 54 | + public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) |
| 55 | + { |
| 56 | + if (destinationType == typeof(string)) |
| 57 | + { |
| 58 | + return value switch |
| 59 | + { |
| 60 | + UriPdfSource uriSource => uriSource.Uri.ToString(), |
| 61 | + FilePdfSource fileSource => fileSource.FilePath, |
| 62 | + AssetPdfSource assetSource => $"asset://{assetSource.AssetName}", |
| 63 | + _ => null |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + return base.ConvertTo(context, culture, value, destinationType); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Converts a string to the appropriate PdfSource type based on the string format. |
| 72 | + /// </summary> |
| 73 | + private static PdfSource ParseString(string value) |
| 74 | + { |
| 75 | + if (string.IsNullOrWhiteSpace(value)) |
| 76 | + { |
| 77 | + throw new InvalidOperationException($"Cannot convert empty or null string to {nameof(PdfSource)}"); |
| 78 | + } |
| 79 | + |
| 80 | + var trimmedValue = value.Trim(); |
| 81 | + |
| 82 | + // Check for HTTP/HTTPS URLs |
| 83 | + if (trimmedValue.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || |
| 84 | + trimmedValue.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) |
| 85 | + { |
| 86 | + return new UriPdfSource(new Uri(trimmedValue)); |
| 87 | + } |
| 88 | + |
| 89 | + // Check for asset:// prefix (custom scheme for embedded assets) |
| 90 | + if (trimmedValue.StartsWith("asset://", StringComparison.OrdinalIgnoreCase)) |
| 91 | + { |
| 92 | + var assetName = trimmedValue["asset://".Length..]; |
| 93 | + return new AssetPdfSource(assetName); |
| 94 | + } |
| 95 | + |
| 96 | + // Check for file:// URI scheme |
| 97 | + if (trimmedValue.StartsWith("file://", StringComparison.OrdinalIgnoreCase)) |
| 98 | + { |
| 99 | + var uri = new Uri(trimmedValue); |
| 100 | + return new FilePdfSource(uri.LocalPath); |
| 101 | + } |
| 102 | + |
| 103 | + // Default: treat as a file path or asset name |
| 104 | + // If it looks like a relative path without extension or with common asset extensions, |
| 105 | + // and doesn't contain path separators at the start, treat as asset |
| 106 | + if (!trimmedValue.StartsWith("/") && |
| 107 | + !trimmedValue.StartsWith("\\") && |
| 108 | + !trimmedValue.Contains(":/") && |
| 109 | + !trimmedValue.Contains(":\\") && |
| 110 | + (trimmedValue.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase) || |
| 111 | + !trimmedValue.Contains(Path.DirectorySeparatorChar) && |
| 112 | + !trimmedValue.Contains(Path.AltDirectorySeparatorChar))) |
| 113 | + { |
| 114 | + // Could be an asset name like "sample.pdf" or a simple filename |
| 115 | + // We'll treat simple names as assets, full paths as files |
| 116 | + if (!Path.IsPathRooted(trimmedValue) && |
| 117 | + !trimmedValue.Contains(Path.DirectorySeparatorChar) && |
| 118 | + !trimmedValue.Contains(Path.AltDirectorySeparatorChar)) |
| 119 | + { |
| 120 | + return new AssetPdfSource(trimmedValue); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Treat as file path |
| 125 | + return new FilePdfSource(trimmedValue); |
| 126 | + } |
| 127 | +} |
0 commit comments