| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | |
| | | 5 | | namespace Kestrun.Localization; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Parses PowerShell-style string table files containing key=value pairs. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class StringTableParser |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Parses a string table file and returns a dictionary of key/value pairs. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="path">The file path to parse.</param> |
| | | 16 | | /// <returns>A dictionary containing parsed key/value pairs.</returns> |
| | | 17 | | public static IReadOnlyDictionary<string, string> ParseFile(string path) |
| | | 18 | | { |
| | 9 | 19 | | ArgumentNullException.ThrowIfNull(path); |
| | | 20 | | |
| | 9 | 21 | | if (!File.Exists(path)) |
| | | 22 | | { |
| | 0 | 23 | | throw new FileNotFoundException("Localization file not found.", path); |
| | | 24 | | } |
| | | 25 | | |
| | 9 | 26 | | var map = new Dictionary<string, string>(StringComparer.Ordinal); |
| | 9 | 27 | | var prefixStack = new Stack<string>(); |
| | 9 | 28 | | using var reader = new StreamReader( |
| | 9 | 29 | | path, |
| | 9 | 30 | | new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: false), |
| | 9 | 31 | | detectEncodingFromByteOrderMarks: true); |
| | | 32 | | |
| | | 33 | | string? line; |
| | 57 | 34 | | while ((line = reader.ReadLine()) != null) |
| | | 35 | | { |
| | 48 | 36 | | var trimmed = line.Trim(); |
| | | 37 | | |
| | 48 | 38 | | if (ShouldSkipLine(trimmed)) |
| | | 39 | | { |
| | | 40 | | continue; |
| | | 41 | | } |
| | | 42 | | |
| | 36 | 43 | | if (string.Equals(trimmed, "}", StringComparison.Ordinal)) |
| | | 44 | | { |
| | 14 | 45 | | ProcessClosingBrace(prefixStack); |
| | 14 | 46 | | continue; |
| | | 47 | | } |
| | | 48 | | |
| | 22 | 49 | | ProcessKeyValueLine(trimmed, prefixStack, map); |
| | | 50 | | } |
| | | 51 | | |
| | 9 | 52 | | return map; |
| | 9 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Parses a JSON string table file and returns a dictionary of key/value pairs. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="path">The JSON file path to parse.</param> |
| | | 59 | | /// <returns>A dictionary containing parsed key/value pairs.</returns> |
| | | 60 | | public static IReadOnlyDictionary<string, string> ParseJsonFile(string path) |
| | | 61 | | { |
| | 2 | 62 | | ArgumentNullException.ThrowIfNull(path); |
| | | 63 | | |
| | 2 | 64 | | if (!File.Exists(path)) |
| | | 65 | | { |
| | 0 | 66 | | throw new FileNotFoundException("Localization file not found.", path); |
| | | 67 | | } |
| | | 68 | | |
| | 2 | 69 | | using var stream = File.OpenRead(path); |
| | 2 | 70 | | using var document = JsonDocument.Parse(stream, new JsonDocumentOptions |
| | 2 | 71 | | { |
| | 2 | 72 | | AllowTrailingCommas = true, |
| | 2 | 73 | | CommentHandling = JsonCommentHandling.Skip |
| | 2 | 74 | | }); |
| | | 75 | | |
| | 2 | 76 | | var map = new Dictionary<string, string>(StringComparer.Ordinal); |
| | 2 | 77 | | if (document.RootElement.ValueKind == JsonValueKind.Object) |
| | | 78 | | { |
| | 2 | 79 | | FlattenJsonElement(document.RootElement, prefix: null, map); |
| | | 80 | | } |
| | | 81 | | |
| | 2 | 82 | | return map; |
| | 2 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Determines whether a line should be skipped during parsing. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="trimmed">The trimmed line content.</param> |
| | | 89 | | /// <returns>True if the line should be skipped; otherwise false.</returns> |
| | | 90 | | private static bool ShouldSkipLine(string trimmed) => |
| | 48 | 91 | | trimmed.Length == 0 || |
| | 48 | 92 | | string.Equals(trimmed, "@{", StringComparison.Ordinal) || |
| | 48 | 93 | | trimmed.StartsWith('#') || |
| | 48 | 94 | | trimmed.StartsWith("//", StringComparison.Ordinal); |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Processes a closing brace by popping the prefix stack. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="prefixStack">The prefix stack tracking nested hashtables.</param> |
| | | 100 | | private static void ProcessClosingBrace(Stack<string> prefixStack) |
| | | 101 | | { |
| | 14 | 102 | | if (prefixStack.Count > 0) |
| | | 103 | | { |
| | 5 | 104 | | _ = prefixStack.Pop(); |
| | | 105 | | } |
| | 14 | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Processes a key=value line and adds it to the map. |
| | | 110 | | /// </summary> |
| | | 111 | | /// <param name="trimmed">The trimmed line content.</param> |
| | | 112 | | /// <param name="prefixStack">The prefix stack tracking nested hashtables.</param> |
| | | 113 | | /// <param name="map">The dictionary to populate.</param> |
| | | 114 | | private static void ProcessKeyValueLine(string trimmed, Stack<string> prefixStack, Dictionary<string, string> map) |
| | | 115 | | { |
| | 22 | 116 | | var equalsIndex = trimmed.IndexOf('='); |
| | 22 | 117 | | if (equalsIndex < 0) |
| | | 118 | | { |
| | 1 | 119 | | return; |
| | | 120 | | } |
| | | 121 | | |
| | 21 | 122 | | var key = trimmed[..equalsIndex].Trim(); |
| | 21 | 123 | | if (key.Length == 0) |
| | | 124 | | { |
| | 0 | 125 | | return; |
| | | 126 | | } |
| | | 127 | | |
| | 21 | 128 | | var value = trimmed[(equalsIndex + 1)..].Trim(); |
| | 21 | 129 | | if (string.Equals(value, "@{", StringComparison.Ordinal)) |
| | | 130 | | { |
| | 5 | 131 | | var prefix = prefixStack.Count == 0 ? key : string.Concat(prefixStack.Peek(), ".", key); |
| | 5 | 132 | | prefixStack.Push(prefix); |
| | 5 | 133 | | return; |
| | | 134 | | } |
| | | 135 | | |
| | 16 | 136 | | value = UnquoteValue(value); |
| | 16 | 137 | | var fullKey = prefixStack.Count == 0 ? key : string.Concat(prefixStack.Peek(), ".", key); |
| | 16 | 138 | | map[fullKey] = value; |
| | 16 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Flattens a JSON element into dot-delimited keys. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <param name="element">The JSON element to flatten.</param> |
| | | 145 | | /// <param name="prefix">The key prefix.</param> |
| | | 146 | | /// <param name="map">The dictionary to populate.</param> |
| | | 147 | | private static void FlattenJsonElement(JsonElement element, string? prefix, Dictionary<string, string> map) |
| | | 148 | | { |
| | 9 | 149 | | if (element.ValueKind == JsonValueKind.Object) |
| | | 150 | | { |
| | 22 | 151 | | foreach (var property in element.EnumerateObject()) |
| | | 152 | | { |
| | 7 | 153 | | var nextPrefix = BuildJsonKey(prefix, property.Name); |
| | 7 | 154 | | FlattenJsonElement(property.Value, nextPrefix, map); |
| | | 155 | | } |
| | | 156 | | |
| | 4 | 157 | | return; |
| | | 158 | | } |
| | | 159 | | |
| | 5 | 160 | | if (element.ValueKind == JsonValueKind.Array) |
| | | 161 | | { |
| | 0 | 162 | | var index = 0; |
| | 0 | 163 | | foreach (var item in element.EnumerateArray()) |
| | | 164 | | { |
| | 0 | 165 | | var nextPrefix = BuildJsonKey(prefix, index.ToString(CultureInfo.InvariantCulture)); |
| | 0 | 166 | | FlattenJsonElement(item, nextPrefix, map); |
| | 0 | 167 | | index++; |
| | | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | return; |
| | | 171 | | } |
| | | 172 | | |
| | 5 | 173 | | if (prefix is not null) |
| | | 174 | | { |
| | 5 | 175 | | map[prefix] = ConvertJsonValue(element); |
| | | 176 | | } |
| | 5 | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Builds a dot-delimited JSON key. |
| | | 181 | | /// </summary> |
| | | 182 | | /// <param name="prefix">The existing prefix.</param> |
| | | 183 | | /// <param name="name">The next segment name.</param> |
| | | 184 | | /// <returns>The combined key.</returns> |
| | | 185 | | private static string BuildJsonKey(string? prefix, string name) => |
| | 7 | 186 | | string.IsNullOrWhiteSpace(prefix) ? name : string.Concat(prefix, ".", name); |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Converts a JSON element to a string value for the string table. |
| | | 190 | | /// </summary> |
| | | 191 | | /// <param name="element">The JSON element.</param> |
| | | 192 | | /// <returns>The string value.</returns> |
| | | 193 | | private static string ConvertJsonValue(JsonElement element) |
| | | 194 | | { |
| | 5 | 195 | | return element.ValueKind switch |
| | 5 | 196 | | { |
| | 5 | 197 | | JsonValueKind.String => element.GetString() ?? string.Empty, |
| | 0 | 198 | | JsonValueKind.Null => string.Empty, |
| | 0 | 199 | | _ => element.GetRawText() |
| | 5 | 200 | | }; |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | /// <summary> |
| | | 204 | | /// Removes surrounding quotes from a value if present. |
| | | 205 | | /// </summary> |
| | | 206 | | /// <param name="value">The value to unquote.</param> |
| | | 207 | | /// <returns>The unquoted value.</returns> |
| | | 208 | | private static string UnquoteValue(string value) |
| | | 209 | | { |
| | 16 | 210 | | if (value.Length >= 2) |
| | | 211 | | { |
| | 16 | 212 | | var quote = value[0]; |
| | 16 | 213 | | if ((quote == '"' || quote == '\'') && value[^1] == quote) |
| | | 214 | | { |
| | 16 | 215 | | return value[1..^1]; |
| | | 216 | | } |
| | | 217 | | } |
| | | 218 | | |
| | 0 | 219 | | return value; |
| | | 220 | | } |
| | | 221 | | } |