-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathRegexColumnizer.cs
More file actions
521 lines (447 loc) · 21.9 KB
/
RegexColumnizer.cs
File metadata and controls
521 lines (447 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
using System.Globalization;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using ColumnizerLib;
using LogExpert.Core.Helpers;
using Newtonsoft.Json;
[assembly: SupportedOSPlatform("windows")]
namespace RegexColumnizer;
/// <summary>
/// Provides a base class for columnizing log lines using regular expressions, supporting configurable column
/// definitions and integration with log line memory interfaces.
/// </summary>
/// <remarks>This abstract class implements core logic for splitting log lines into columns based on regular
/// expression group matches. It supports configuration loading and saving, and is intended to be extended by concrete
/// columnizer implementations. The class ensures that columnized output always matches the expected column count, and
/// provides mechanisms for both memory-efficient and string-based log line processing. Thread safety is not guaranteed;
/// instances should not be shared across threads without external synchronization.</remarks>
public abstract class BaseRegexColumnizer : ILogLineMemoryColumnizer, IColumnizerConfiguratorMemory
{
#region Fields
private readonly XmlSerializer _xml = new(typeof(RegexColumnizerConfig));
private string[] _columns;
private RegexColumnizerConfig _config;
#endregion
#region Properties
public Regex Regex { get; private set; }
#endregion
#region Public methods
/// <summary>
/// Gets the configured name, or a default name if no configuration is set.
/// </summary>
/// <returns>A string containing the configured name if available; otherwise, a default name.</returns>
public string GetName ()
{
return string.IsNullOrWhiteSpace(_config?.Name)
? GetNameInternal()
: _config.Name;
}
/// <summary>
/// Gets the custom name if specified; otherwise, returns the default name.
/// </summary>
/// <returns>A string containing the custom name if it is set and not empty; otherwise, the default name.</returns>
public string GetCustomName ()
{
return string.IsNullOrWhiteSpace(_config?.CustomName)
? GetNameInternal()
: _config.CustomName;
}
/// <summary>
/// Gets a localized description of the regular expression columnizer.
/// </summary>
/// <returns>A string containing the localized description text for the regular expression columnizer.</returns>
public string GetDescription () => Resources.RegexColumnizer_Description;
/// <summary>
/// Gets the number of columns in the collection.
/// </summary>
/// <returns>The total number of columns contained in the collection.</returns>
public int GetColumnCount () => _columns.Length;
/// <summary>
/// Returns the names of all columns in the current schema.
/// </summary>
/// <returns>An array of strings containing the names of the columns. The array will be empty if no columns are defined.</returns>
public string[] GetColumnNames () => _columns;
/// <summary>
/// Returns a slice of the specified memory corresponding to the matched group, or an empty memory if the group was
/// not successful or has zero length.
/// </summary>
/// <remarks>This method avoids allocating a new string by returning a memory slice over the original
/// input. The returned memory is valid as long as the underlying memory of <paramref name="lineMemory"/> remains
/// valid.</remarks>
/// <param name="lineMemory">The memory region containing the original input text from which the group was matched.</param>
/// <param name="group">The regular expression group whose matched value is to be extracted from the memory. The group must have been
/// matched against the input represented by <paramref name="lineMemory"/>.</param>
/// <returns>A <see cref="ReadOnlyMemory{Char}"/> representing the portion of <paramref name="lineMemory"/> matched by
/// <paramref name="group"/>. Returns <see cref="ReadOnlyMemory{Char}.Empty"/> if the group was not successful or
/// has zero length.</returns>
private static ReadOnlyMemory<char> GetGroupMemory (ReadOnlyMemory<char> lineMemory, Group group)
{
if (!group.Success || group.Length == 0)
{
return ReadOnlyMemory<char>.Empty;
}
// Use group's Index and Length to slice original memory
// This avoids allocating a new string for the group value
return lineMemory.Slice(group.Index, group.Length);
}
/// <summary>
/// Splits the specified log line into columns according to the configured columnization logic.
/// </summary>
/// <remarks>If the log line does not match the configured regular expression, the entire line is placed
/// in the last column and other columns are set to empty. The method ensures that the returned object always
/// contains the expected number of columns, avoiding null values in the column array.</remarks>
/// <param name="callback">A callback interface used to provide additional context or services during columnization. Cannot be null.</param>
/// <param name="logLine">The log line to be split into columns. Cannot be null.</param>
/// <returns>An object representing the columnized version of the input log line, with each column containing its
/// corresponding value.</returns>
public IColumnizedLogLineMemory SplitLine (ILogLineMemoryColumnizerCallback callback, ILogLineMemory logLine)
{
ArgumentNullException.ThrowIfNull(logLine, nameof(logLine));
ArgumentNullException.ThrowIfNull(callback, nameof(callback));
var columnizedLogLine = new ColumnizedLogLine
{
LogLine = logLine
};
if (Regex != null)
{
if (Regex.IsMatch(logLine.FullLine.Span))
{
// To extract regex group captures, we must convert to string.
// This is an unavoidable allocation - .NET Regex doesn't provide
// a way to get group capture positions from ReadOnlySpan<char>.
// However, GetGroupMemory() will slice the original ReadOnlyMemory,
// so we avoid allocating strings for each captured group.
var lineString = logLine.FullLine.ToString();
var match = Regex.Match(lineString);
var groupCount = Math.Min(match.Groups.Count - 1, _columns.Length);
var cols = Column.CreateColumns(_columns.Length, columnizedLogLine);
for (var i = 0; i < groupCount; i++)
{
cols[i].FullValue = GetGroupMemory(logLine.FullLine, match.Groups[i + 1]);
}
columnizedLogLine.ColumnValues = [.. cols.Select(c => c as IColumnMemory)];
}
else
{
//Fill other columns with empty string to avoid null pointer exceptions in unexpected places
var columns = Column.CreateColumns(_columns.Length, columnizedLogLine);
// Set last column to full line
columns[^1].FullValue = logLine.FullLine;
//Move non matching lines in the last column
columnizedLogLine.ColumnValues = [.. columns.Select(c => c as IColumnMemory)];
}
}
else //Regex is null, just put the full line in the first column
{
var cols = Column.CreateColumns(_columns.Length, columnizedLogLine);
cols[0].FullValue = logLine.FullLine;
columnizedLogLine.ColumnValues = [.. cols.Select(c => c as IColumnMemory)];
}
return columnizedLogLine;
}
/// <summary>
/// Determines whether timeshift functionality is implemented.
/// </summary>
/// <returns><see langword="true"/> if timeshift is implemented; otherwise, <see langword="false"/>.</returns>
public bool IsTimeshiftImplemented () => false;
/// <summary>
/// Sets the time offset, in milliseconds, to be applied to time calculations or operations.
/// </summary>
/// <param name="msecOffset">The time offset, in milliseconds. Positive values indicate a forward offset; negative values indicate a backward
/// offset.</param>
/// <exception cref="NotImplementedException">The method is not implemented.</exception>
public void SetTimeOffset (int msecOffset)
{
throw new NotImplementedException();
}
/// <summary>
/// Gets the time offset, in seconds, between the local system time and Coordinated Universal Time (UTC).
/// </summary>
/// <returns>The number of seconds that the local time is offset from UTC. A positive value indicates the local time is ahead
/// of UTC; a negative value indicates it is behind.</returns>
/// <exception cref="NotImplementedException">Thrown in all cases. This method is not implemented.</exception>
public int GetTimeOffset ()
{
throw new NotImplementedException();
}
/// <summary>
/// Loads the configuration for the columnizer from the specified directory, using either a JSON or XML
/// configuration file if available.
/// </summary>
/// <remarks>If both JSON and XML configuration files are missing or invalid, a default configuration is
/// created. The method displays an error message if deserialization fails.</remarks>
/// <param name="configDir">The path to the directory containing the configuration files. Must not be null or empty.</param>
public void LoadConfig (string configDir)
{
var configFile = GetConfigFileJSON(configDir);
if (!File.Exists(configFile))
{
configFile = GetConfigFileXML(configDir);
if (!File.Exists(configFile))
{
_config = new RegexColumnizerConfig
{
Name = GetName()
};
}
else
{
try
{
using var reader = new StreamReader(configFile);
_config = _xml.Deserialize(reader) as RegexColumnizerConfig;
}
catch (Exception ex) when (ex is InvalidOperationException or
IOException or
ArgumentException or
ArgumentNullException or
FileNotFoundException or
DirectoryNotFoundException)
{
_ = MessageBox.Show(ex.Message, Resources.RegexColumnizer_UI_Title_Deserialize);
_config = new RegexColumnizerConfig
{
Name = GetName()
};
}
}
}
else
{
try
{
string jsonContent = File.ReadAllText(configFile);
_config = JsonConvert.DeserializeObject<RegexColumnizerConfig>(jsonContent) ?? new RegexColumnizerConfig { Name = GetName() };
}
catch (JsonException ex)
{
_ = MessageBox.Show(ex.Message, Resources.RegexColumnizer_UI_Title_Deserialize);
_config = new RegexColumnizerConfig
{
Name = GetName()
};
}
}
Init();
}
/// <summary>
/// Validates that the columnizer name contains no path separators or invalid characters
/// to prevent path traversal attacks (SEC-02)
/// </summary>
private static void ValidateColumnizerName (string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new InvalidOperationException(Resources.RegexColumnizer_Error_Message_ColumnizerNameCannotBeNullOrEmpty);
}
// Check for path separators (both Windows and Unix)
if (name.Contains(Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) ||
name.Contains(Path.AltDirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) ||
name.Contains('/', StringComparison.OrdinalIgnoreCase) ||
name.Contains('\\', StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException(Resources.RegexColumnizer_Error_Message_ColumnizerNameNameContainsPathSeparatorsWhichAreNotAllowed);
}
// Check for invalid filename characters
char[] invalidChars = Path.GetInvalidFileNameChars();
if (name.IndexOfAny(invalidChars) >= 0)
{
throw new InvalidOperationException(Resources.RegexColumnizer_Error_Message_ColumnizerNameNameContainsInvalidFilenameCharacters);
}
// Check for path traversal patterns
if (name.Contains("..", StringComparison.OrdinalIgnoreCase) || name.Contains('~', StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException(Resources.RegexColumnizer_Error_Message_ColumnizerNameNameContainsPathTraversalPatternsWhichAreNotAllowed);
}
}
[Obsolete("XML Configuration is deprecated, use JSON instead")]
private string GetConfigFileXML (string configDir)
{
var name = GetType().Name;
ValidateColumnizerName(name);
var configPath = Path.Join(configDir, name);
configPath = Path.ChangeExtension(configPath, "xml");
return configPath;
}
private string GetConfigFileJSON (string configDir)
{
var name = GetType().Name;
ValidateColumnizerName(name);
var configPath = Path.Join(configDir, name);
configPath = Path.ChangeExtension(configPath, "json");
return configPath;
}
#endregion
/// <summary>
/// ToString, this is displayed in the columnizer picker combobox only in the FilterSelectionDialog
/// </summary>
/// <returns></returns>
public override string ToString ()
{
return GetName();
}
#region Private Methods
protected abstract string GetNameInternal ();
public void Init ()
{
try
{
Regex = RegexHelper.GetOrCreateCached(_config.Expression, RegexOptions.Compiled);
var skip = Regex.GetGroupNames().Length == 1
? 0
: 1;
_columns = [.. Regex.GetGroupNames().Skip(skip)];
}
catch (Exception ex) when (ex is ArgumentException or
ArgumentNullException or
OverflowException or
RegexParseException)
{
Regex = null;
_columns = ["text"];
}
}
/// <summary>
/// Retrieves the timestamp associated with the specified log line.
/// </summary>
/// <param name="callback">An object that provides callback methods for columnizer operations. Used to access additional context or
/// services required during timestamp extraction.</param>
/// <param name="logLine">The log line from which to extract the timestamp. Cannot be null.</param>
/// <returns>A DateTime value representing the timestamp of the specified log line.</returns>
/// <exception cref="NotImplementedException">The method is not implemented.</exception>
public DateTime GetTimestamp (ILogLineMemoryColumnizerCallback callback, ILogLineMemory logLine)
{
throw new NotImplementedException();
}
/// <summary>
/// Notifies the callback of a value change for a specific column.
/// </summary>
/// <param name="callback">The callback interface to receive the value change notification. Cannot be null.</param>
/// <param name="column">The zero-based index of the column whose value has changed.</param>
/// <param name="value">The new value to be associated with the specified column.</param>
/// <param name="oldValue">The previous value of the specified column before the change.</param>
/// <exception cref="NotImplementedException">The method is not implemented.</exception>
public void PushValue (ILogLineMemoryColumnizerCallback callback, int column, string value, string oldValue)
{
throw new NotImplementedException();
}
/// <summary>
/// Displays a configuration dialog for the columnizer and saves the updated settings to the specified configuration
/// directory.
/// </summary>
/// <remarks>If the specified configuration directory does not exist, the method attempts to create it. If
/// the user cancels the configuration dialog, no changes are made. Any errors encountered during directory creation
/// or file saving are displayed to the user via a message box. The callback parameter is not used in the current
/// implementation.</remarks>
/// <param name="callback">A callback interface for columnizer memory operations. This parameter is reserved for future use and can be
/// null.</param>
/// <param name="configDir">The path to the directory where the configuration file will be stored. Cannot be null, empty, or consist only of
/// white-space characters.</param>
/// <exception cref="ArgumentException">Thrown if configDir is null, empty, or consists only of white-space characters.</exception>
/// <exception cref="InvalidOperationException">Thrown if the columnizer name is null or empty.</exception>
public void Configure (ILogLineMemoryColumnizerCallback callback, string configDir)
{
// Validate inputs
if (string.IsNullOrWhiteSpace(configDir))
{
throw new ArgumentException(Resources.RegexColumnizer_Configuration_DirectoryCannotBeNullOrEmpty, nameof(configDir));
}
string name = GetName();
if (string.IsNullOrWhiteSpace(name))
{
throw new InvalidOperationException(Resources.RegexColumnizer_Error_Message_ColumnizerNameCannotBeNullOrEmpty);
}
// Ensure directory exists
if (!Directory.Exists(configDir))
{
try
{
_ = Directory.CreateDirectory(configDir);
}
catch (Exception ex) when (ex is IOException or
UnauthorizedAccessException)
{
_ = MessageBox.Show(string.Format(CultureInfo.InvariantCulture, Resources.RegexColumnizer_UI_Message_FailedToCreateConfigurationDirectory, ex.Message),
Resources.RegexColumnizer_UI_Title_Error,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
}
string filePath = Path.Join(configDir, $"{name}Columnizer.json");
_config ??= new RegexColumnizerConfig
{
Name = GetName()
};
RegexColumnizerConfigDialog dlg = new(_config);
if (dlg.ShowDialog() == DialogResult.OK)
{
try
{
// Only validate regex if expression is provided (empty is allowed and uses default)
if (!string.IsNullOrWhiteSpace(dlg.Config.Expression))
{
// Test regex compilation to catch errors early
_ = RegexHelper.CreateSafeRegex(dlg.Config.Expression);
}
// Save configuration
string json = JsonConvert.SerializeObject(dlg.Config, Formatting.Indented);
File.WriteAllText(filePath, json);
_config = dlg.Config;
Init();
}
catch (RegexMatchTimeoutException ex)
{
_ = MessageBox.Show(string.Format(CultureInfo.InvariantCulture, Resources.RegexColumnizer_UI_Message_RegexTimeout, ex.Message), Resources.RegexColumnizer_UI_Title_Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (ArgumentException ex)
{
_ = MessageBox.Show(string.Format(CultureInfo.InvariantCulture, Resources.RegexColumnizer_UI_Message_InvalidRegexPattern, ex.Message), Resources.RegexColumnizer_UI_Title_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
_ = MessageBox.Show(string.Format(CultureInfo.InvariantCulture, Resources.RegexColumnizer_UI_Message_FailedToSaveConfiguration, ex.Message), Resources.RegexColumnizer_UI_Title_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
#endregion
}
#region RegexColumnizer Implementations
public class Regex1Columnizer : BaseRegexColumnizer
{
protected override string GetNameInternal () => "Regex1";
}
public class Regex2Columnizer : BaseRegexColumnizer
{
protected override string GetNameInternal () => "Regex2";
}
public class Regex3Columnizer : BaseRegexColumnizer
{
protected override string GetNameInternal () => "Regex3";
}
public class Regex4Columnizer : BaseRegexColumnizer
{
protected override string GetNameInternal () => "Regex4";
}
public class Regex5Columnizer : BaseRegexColumnizer
{
protected override string GetNameInternal () => "Regex5";
}
public class Regex6Columnizer : BaseRegexColumnizer
{
protected override string GetNameInternal () => "Regex6";
}
public class Regex7Columnizer : BaseRegexColumnizer
{
protected override string GetNameInternal () => "Regex7";
}
public class Regex8Columnizer : BaseRegexColumnizer
{
protected override string GetNameInternal () => "Regex8";
}
public class Regex9Columnizer : BaseRegexColumnizer
{
protected override string GetNameInternal () => "Regex9";
}
#endregion