| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.IO; |
| | 7 | | using System.Text; |
| | 8 | | using GDX.DataTables.DataBinding.Formats; |
| | 9 | |
|
| | 10 | | namespace GDX.DataTables.DataBinding |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Data translation functionality for <see cref="DataTableBase" />. |
| | 14 | | /// </summary> |
| | 15 | | public static class DataBindingProvider |
| | 16 | | { |
| 1 | 17 | | static readonly List<FormatBase> k_KnownFormats = new List<FormatBase>(3); |
| 1 | 18 | | static readonly CommaSeperatedValueFormat k_CommaSeperatedValueFormat = new CommaSeperatedValueFormat(); |
| | 19 | |
|
| 1 | 20 | | static readonly JavaScriptObjectNotationFormat k_JavaScriptObjectNotationFormat = |
| | 21 | | new JavaScriptObjectNotationFormat(); |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Export the content of a given <see cref="DataTableBase" /> to a target format. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="dataTable">The <see cref="DataTableBase" /></param> |
| | 27 | | /// <param name="uri">The output path/uri where to send the data, absolute if on disk</param> |
| | 28 | | /// <param name="jsonFallback">If the format cannot be determined by the uri, fallback to JSON.</param> |
| | 29 | | public static void Export(DataTableBase dataTable, string uri, bool jsonFallback = true) |
| 0 | 30 | | { |
| 0 | 31 | | SerializableTable serializableTable = new SerializableTable(dataTable); |
| 0 | 32 | | FormatBase format = GetFormatFromUri(uri); |
| 0 | 33 | | if (format == null && jsonFallback) |
| 0 | 34 | | { |
| 0 | 35 | | format = k_JavaScriptObjectNotationFormat; |
| 0 | 36 | | } |
| | 37 | |
|
| 0 | 38 | | format?.Push(uri, serializableTable); |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | public static FormatBase[] GetFormats() |
| 4 | 42 | | { |
| 4 | 43 | | return k_KnownFormats.ToArray(); |
| 4 | 44 | | } |
| | 45 | |
|
| | 46 | | public static FormatBase GetFormatFromFile(string filePath) |
| 0 | 47 | | { |
| 0 | 48 | | using FileStream fileStream = new FileStream(filePath, FileMode.Open); |
| | 49 | | const int k_HeaderSize = 25; |
| | 50 | |
|
| 0 | 51 | | byte[] chunk = new byte[k_HeaderSize]; |
| 0 | 52 | | int read = fileStream.Read(chunk, 0, k_HeaderSize); |
| 0 | 53 | | return read == k_HeaderSize |
| | 54 | | ? GetFormatFromFileHeader(Encoding.ASCII.GetString(chunk, 0, k_HeaderSize)) |
| | 55 | | : null; |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | static FormatBase GetFormatFromFileHeader(string header) |
| 0 | 59 | | { |
| 0 | 60 | | for (int i = 0; i < k_KnownFormats.Count; i++) |
| 0 | 61 | | { |
| 0 | 62 | | if (k_KnownFormats[i].IsFileHeader(header)) |
| 0 | 63 | | { |
| 0 | 64 | | return k_KnownFormats[i]; |
| | 65 | | } |
| 0 | 66 | | } |
| | 67 | |
|
| 0 | 68 | | return null; |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public static FormatBase GetFormatFromUri(string uri) |
| 0 | 72 | | { |
| 0 | 73 | | for (int i = 0; i < k_KnownFormats.Count; i++) |
| 0 | 74 | | { |
| 0 | 75 | | if (k_KnownFormats[i].IsUri(uri)) |
| 0 | 76 | | { |
| 0 | 77 | | return k_KnownFormats[i]; |
| | 78 | | } |
| 0 | 79 | | } |
| | 80 | |
|
| 0 | 81 | | return null; |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | public static string[] GetImportDialogExtensions() |
| 0 | 85 | | { |
| 0 | 86 | | List<string> returnData = new List<string>(4); |
| 0 | 87 | | returnData.Add("Auto"); |
| 0 | 88 | | returnData.Add("*"); |
| 0 | 89 | | for (int i = 0; i < k_KnownFormats.Count; i++) |
| 0 | 90 | | { |
| 0 | 91 | | string[] extensions = k_KnownFormats[i].GetImportDialogExtensions(); |
| 0 | 92 | | if (extensions != null) |
| 0 | 93 | | { |
| 0 | 94 | | returnData.AddRange(extensions); |
| 0 | 95 | | } |
| 0 | 96 | | } |
| | 97 | |
|
| 0 | 98 | | return returnData.ToArray(); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Update the <see cref="DataTableBase" /> with the data found in the given file. |
| | 103 | | /// </summary> |
| | 104 | | /// <remarks> |
| | 105 | | /// It's important that the Row Identifier column remains unchanged, no structural changes have occured, and |
| | 106 | | /// no changes of column order were made. Object references will be maintained during update, only values wi |
| | 107 | | /// be updated. |
| | 108 | | /// </remarks> |
| | 109 | | /// <param name="dataTable">The target <see cref="DataTableBase" /> to apply changes to.</param> |
| | 110 | | /// <param name="uri">The resource path to load data from, absolute if on disk.</param> |
| | 111 | | /// <param name="removeRowIfNotFound">Should rows that are not found in the file content be removed?</param> |
| | 112 | | /// <param name="jsonFallback"> |
| | 113 | | /// If the importer is unable to determine the format based on the URI, fallback to assuming its |
| | 114 | | /// JSON. |
| | 115 | | /// </param> |
| | 116 | | /// <returns>Was the import successful?</returns> |
| | 117 | | public static bool Import(DataTableBase dataTable, string uri, bool removeRowIfNotFound = true, |
| | 118 | | bool jsonFallback = true) |
| 0 | 119 | | { |
| 0 | 120 | | SerializableTable serializableTable = null; |
| 0 | 121 | | FormatBase format = GetFormatFromUri(uri); |
| 0 | 122 | | if (format == null && jsonFallback) |
| 0 | 123 | | { |
| 0 | 124 | | format = k_JavaScriptObjectNotationFormat; |
| 0 | 125 | | } |
| | 126 | |
|
| 0 | 127 | | if (format != null) |
| 0 | 128 | | { |
| 0 | 129 | | serializableTable = format.Pull(uri, dataTable.GetDataVersion(), dataTable.GetStructureVersion()); |
| 0 | 130 | | } |
| | 131 | |
|
| 0 | 132 | | return serializableTable != null && serializableTable.Update(dataTable, removeRowIfNotFound); |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | public static void RegisterFormat(FormatBase format) |
| 2 | 136 | | { |
| 2 | 137 | | if (!k_KnownFormats.Contains(format)) |
| 2 | 138 | | { |
| 2 | 139 | | k_KnownFormats.Add(format); |
| 2 | 140 | | } |
| 2 | 141 | | } |
| | 142 | |
|
| | 143 | | public static void UnregisterFormat(FormatBase format) |
| 0 | 144 | | { |
| 0 | 145 | | k_KnownFormats.Remove(format); |
| 0 | 146 | | } |
| | 147 | | } |
| | 148 | | } |