-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoFixDemo.pas
More file actions
740 lines (642 loc) · 19.4 KB
/
AutoFixDemo.pas
File metadata and controls
740 lines (642 loc) · 19.4 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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
unit AutoFixDemo;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
Vcl.ComCtrls, System.Generics.Collections, System.IniFiles, System.JSON, JSONHelpers,
ConfigValidator, ValidationDialog;
type
TConfigFixDemoForm = class(TForm)
PageControl1: TPageControl;
tsINI: TTabSheet;
tsJSON: TTabSheet;
pnlINITop: TPanel;
pnlINIContent: TPanel;
pnlJSONTop: TPanel;
pnlJSONContent: TPanel;
btnValidateINI: TButton;
sgINI: TStringGrid;
memJSON: TMemo;
btnValidateJSON: TButton;
StatusBar: TStatusBar;
btnAddSampleData: TButton;
btnResetINI: TButton;
btnFixINI: TButton;
btnResetJSON: TButton;
btnFixJSON: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnValidateINIClick(Sender: TObject);
procedure btnValidateJSONClick(Sender: TObject);
procedure btnAddSampleDataClick(Sender: TObject);
procedure btnResetINIClick(Sender: TObject);
procedure btnFixINIClick(Sender: TObject);
procedure btnResetJSONClick(Sender: TObject);
procedure btnFixJSONClick(Sender: TObject);
private
FValidator: TConfigValidator;
FValidationDialog: TfrmValidation;
FINIFile: TMemIniFile;
FJSONObject: TJSONObject;
procedure InitializeValidator;
procedure InitializeINIGrid;
procedure LoadSampleINIData;
procedure LoadSampleJSONData;
procedure DisplayValidationResults;
procedure SelectProperty(const Path, Name: string);
procedure FixProperty(const Path, Name: string);
function CreateINIPropertyPath(const Section, Key: string): string;
procedure UpdateINIGrid;
procedure UpdateJSONDisplay;
public
{ Public declarations }
end;
var
ConfigFixDemoForm: TConfigFixDemoForm;
implementation
{$R *.dfm}
procedure TConfigFixDemoForm.FormCreate(Sender: TObject);
begin
// 创建验证器
FValidator := TConfigValidator.Create;
InitializeValidator;
// 创建验证对话框
FValidationDialog := TfrmValidation.Create(Self);
FValidationDialog.OnSelectProperty := SelectProperty;
FValidationDialog.OnFixProperty := FixProperty;
// 初始化INI设置
FINIFile := TMemIniFile.Create('');
InitializeINIGrid;
// 初始化JSON对象
FJSONObject := TJSONObject.Create;
// 显示初始状态
StatusBar.SimpleText := '就绪。点击"添加样例数据"加载测试数据,然后点击"验证"按钮检查配置。';
end;
procedure TConfigFixDemoForm.FormDestroy(Sender: TObject);
begin
FValidator.Free;
FValidationDialog.Free;
FINIFile.Free;
FJSONObject.Free;
end;
procedure TConfigFixDemoForm.InitializeValidator;
begin
// 添加INI验证规则
FValidator.AddRequiredRule('General/Name', 'Name', '名称不能为空');
FValidator.AddNumericRule('General/Age', 'Age', '年龄必须是数字');
FValidator.AddRangeRule('General/Age', 'Age', 0, 120, '年龄必须在0到120之间');
FValidator.AddEmailRule('General/Email', 'Email', '必须是有效的电子邮件地址');
FValidator.AddURLRule('Network/Website', 'Website', '必须是有效的网站地址');
FValidator.AddIPAddressRule('Network/IPAddress', 'IPAddress', '必须是有效的IP地址');
FValidator.AddPasswordRule('Security/Password', 'Password', 8, True, True, True,
'密码至少需要8个字符,且必须包含大写字母、数字和特殊字符');
FValidator.AddColorCodeRule('UI/BackgroundColor', 'BackgroundColor', '必须是有效的颜色代码(#RRGGBB)');
FValidator.AddDateTimeRule('Schedule/StartDate', 'StartDate', 'yyyy-mm-dd', '必须是有效的日期(YYYY-MM-DD)');
// 添加JSON验证规则
FValidator.AddRequiredRule('user/name', 'name', '用户名不能为空');
FValidator.AddEmailRule('user/email', 'email', '必须是有效的电子邮件地址');
FValidator.AddJSONRule('user/preferences', 'preferences', '必须是有效的JSON对象');
FValidator.AddURLRule('links/homepage', 'homepage', '必须是有效的URL');
FValidator.AddIPAddressRule('network/ip', 'ip', '必须是有效的IP地址');
end;
procedure TConfigFixDemoForm.InitializeINIGrid;
begin
// 设置INI表格
with sgINI do
begin
ColCount := 4;
RowCount := 2;
FixedRows := 1;
// 设置列标题
Cells[0, 0] := '节';
Cells[1, 0] := '键';
Cells[2, 0] := '值';
Cells[3, 0] := '状态';
// 设置列宽
ColWidths[0] := 120;
ColWidths[1] := 120;
ColWidths[2] := 200;
ColWidths[3] := 100;
end;
end;
procedure TConfigFixDemoForm.btnAddSampleDataClick(Sender: TObject);
begin
// 加载示例数据
LoadSampleINIData;
LoadSampleJSONData;
StatusBar.SimpleText := '已加载样例数据。点击"验证"按钮检查配置。';
end;
procedure TConfigFixDemoForm.LoadSampleINIData;
begin
// 清空当前数据
FINIFile.Clear;
// 添加有效数据
FINIFile.WriteString('General', 'Name', 'John Doe');
FINIFile.WriteString('General', 'Age', '30');
FINIFile.WriteString('General', 'Email', 'john.doe@example.com');
// 添加无效数据
FINIFile.WriteString('Network', 'Website', 'example.com'); // 缺少http://
FINIFile.WriteString('Network', 'IPAddress', '192.168.1.300'); // IP段值超出范围
FINIFile.WriteString('Security', 'Password', 'password'); // 简单密码
FINIFile.WriteString('UI', 'BackgroundColor', 'FF0000'); // 缺少#
FINIFile.WriteString('Schedule', 'StartDate', '2023-13-01'); // 无效月份
// 更新表格
UpdateINIGrid;
end;
procedure TConfigFixDemoForm.LoadSampleJSONData;
begin
// 清空当前数据
FJSONObject.Free;
FJSONObject := TJSONObject.Create;
// 添加用户信息
var User := TJSONObject.Create;
User.AddPair('name', 'Jane Doe');
User.AddPair('email', 'not-an-email'); // 无效邮箱
User.AddPair('preferences', '{color: "blue"}'); // 无效JSON格式
// 添加链接信息
var Links := TJSONObject.Create;
Links.AddPair('homepage', 'www.example.com'); // 缺少http://
// 添加网络信息
var Network := TJSONObject.Create;
Network.AddPair('ip', '256.0.0.1'); // 无效IP
// 添加到主对象
FJSONObject.AddPair('user', User);
FJSONObject.AddPair('links', Links);
FJSONObject.AddPair('network', Network);
// 更新显示
UpdateJSONDisplay;
end;
procedure TConfigFixDemoForm.btnResetINIClick(Sender: TObject);
begin
// 清空INI数据
FINIFile.Clear;
UpdateINIGrid;
StatusBar.SimpleText := 'INI数据已重置。';
end;
procedure TConfigFixDemoForm.btnResetJSONClick(Sender: TObject);
begin
// 清空JSON数据
FJSONObject.Free;
FJSONObject := TJSONObject.Create;
UpdateJSONDisplay;
StatusBar.SimpleText := 'JSON数据已重置。';
end;
procedure TConfigFixDemoForm.btnValidateINIClick(Sender: TObject);
var
Section, Key, Value: string;
Row: Integer;
HasErrors: Boolean;
begin
// 清除验证结果
FValidator.ClearResults;
// 重置状态列
for Row := 1 to sgINI.RowCount - 1 do
sgINI.Cells[3, Row] := '';
// 验证所有INI值
HasErrors := False;
for Row := 1 to sgINI.RowCount - 1 do
begin
if (sgINI.Cells[0, Row] <> '') and (sgINI.Cells[1, Row] <> '') then
begin
Section := sgINI.Cells[0, Row];
Key := sgINI.Cells[1, Row];
Value := sgINI.Cells[2, Row];
if not FValidator.ValidateINI(Section, Key, Value) then
begin
// 标记错误
sgINI.Cells[3, Row] := '错误';
HasErrors := True;
end
else
begin
// 标记正确
sgINI.Cells[3, Row] := '正确';
end;
end;
end;
// 显示验证结果
if HasErrors then
begin
StatusBar.SimpleText := '验证完成,发现错误。';
DisplayValidationResults;
end
else
begin
StatusBar.SimpleText := '验证完成,未发现错误。';
ShowMessage('所有配置项验证通过!');
end;
end;
procedure TConfigFixDemoForm.btnValidateJSONClick(Sender: TObject);
var
JsonStr: string;
JSONRoot, ChildObj: TJSONObject;
Pair: TJSONPair;
I: Integer;
HasErrors: Boolean;
begin
// 清除验证结果
FValidator.ClearResults;
// 从文本框获取JSON
JsonStr := memJSON.Text;
// 更新FJSONObject
try
JSONRoot := TJSONObject.ParseJSONValue(JsonStr) as TJSONObject;
if Assigned(JSONRoot) then
begin
FJSONObject.Free;
FJSONObject := JSONRoot;
end
else
begin
ShowMessage('JSON解析错误');
Exit;
end;
except
on E: Exception do
begin
ShowMessage('JSON解析错误: ' + E.Message);
Exit;
end;
end;
// 验证所有JSON值
HasErrors := False;
// 递归函数验证JSON对象
function ValidateJSONObject(const BasePath: string; Obj: TJSONObject): Boolean;
var
I: Integer;
Pair: TJSONPair;
ChildPath: string;
Result: Boolean;
begin
Result := True;
for I := 0 to Obj.Count - 1 do
begin
Pair := Obj.Pairs[I];
if BasePath = '' then
ChildPath := Pair.JsonString.Value
else
ChildPath := BasePath + '/' + Pair.JsonString.Value;
// 如果是子对象,递归验证
if Pair.JsonValue is TJSONObject then
begin
if not ValidateJSONObject(ChildPath, Pair.JsonValue as TJSONObject) then
Result := False;
end
else
begin
// 验证值
var ValueStr := '';
if Pair.JsonValue is TJSONString then
ValueStr := TJSONString(Pair.JsonValue).Value
else
ValueStr := Pair.JsonValue.ToString;
if not FValidator.ValidateINI(ChildPath, Pair.JsonString.Value, ValueStr) then
Result := False;
end;
end;
Result := Result;
end;
// 开始验证JSON
if not ValidateJSONObject('', FJSONObject) then
HasErrors := True;
// 显示验证结果
if HasErrors then
begin
StatusBar.SimpleText := '验证完成,发现错误。';
DisplayValidationResults;
end
else
begin
StatusBar.SimpleText := '验证完成,未发现错误。';
ShowMessage('所有配置项验证通过!');
end;
end;
procedure TConfigFixDemoForm.btnFixINIClick(Sender: TObject);
var
Section, Key, Value, Path, FixedValue: string;
Row: Integer;
FixCount: Integer;
begin
FixCount := 0;
// 尝试修复所有INI值
for Row := 1 to sgINI.RowCount - 1 do
begin
if (sgINI.Cells[0, Row] <> '') and (sgINI.Cells[1, Row] <> '') then
begin
Section := sgINI.Cells[0, Row];
Key := sgINI.Cells[1, Row];
Value := sgINI.Cells[2, Row];
Path := CreateINIPropertyPath(Section, Key);
// 尝试自动修复
if FValidator.TryAutoFix(Path, Key, Value, FixedValue) then
begin
// 更新值
sgINI.Cells[2, Row] := FixedValue;
FINIFile.WriteString(Section, Key, FixedValue);
sgINI.Cells[3, Row] := '已修复';
Inc(FixCount);
end;
end;
end;
// 更新状态
if FixCount > 0 then
StatusBar.SimpleText := Format('已自动修复 %d 个配置项。点击"验证"按钮检查结果。', [FixCount])
else
StatusBar.SimpleText := '没有配置项可以自动修复。';
end;
procedure TConfigFixDemoForm.btnFixJSONClick(Sender: TObject);
var
FixCount: Integer;
// 递归函数修复JSON对象
function FixJSONObject(const BasePath: string; Obj: TJSONObject): Integer;
var
I: Integer;
Pair: TJSONPair;
ChildPath: string;
FixedValue: string;
FixCounter: Integer;
begin
FixCounter := 0;
for I := 0 to Obj.Count - 1 do
begin
Pair := Obj.Pairs[I];
if BasePath = '' then
ChildPath := Pair.JsonString.Value
else
ChildPath := BasePath + '/' + Pair.JsonString.Value;
// 如果是子对象,递归修复
if Pair.JsonValue is TJSONObject then
begin
FixCounter := FixCounter + FixJSONObject(ChildPath, Pair.JsonValue as TJSONObject);
end
else
begin
// 尝试自动修复
var ValueStr := '';
if Pair.JsonValue is TJSONString then
ValueStr := TJSONString(Pair.JsonValue).Value
else
ValueStr := Pair.JsonValue.ToString;
if FValidator.TryAutoFix(ChildPath, Pair.JsonString.Value, ValueStr, FixedValue) then
begin
// 更新值
if Pair.JsonValue is TJSONString then
begin
Obj.RemovePair(Pair.JsonString.Value).Free;
Obj.AddPair(Pair.JsonString.Value, FixedValue);
end;
Inc(FixCounter);
end;
end;
end;
Result := FixCounter;
end;
begin
// 开始修复JSON
FixCount := FixJSONObject('', FJSONObject);
// 更新显示
UpdateJSONDisplay;
// 更新状态
if FixCount > 0 then
StatusBar.SimpleText := Format('已自动修复 %d 个配置项。点击"验证"按钮检查结果。', [FixCount])
else
StatusBar.SimpleText := '没有配置项可以自动修复。';
end;
procedure TConfigFixDemoForm.DisplayValidationResults;
var
I: Integer;
Results: TValidationResultList;
Result: TValidationResult;
begin
// 创建验证结果列表
Results := TValidationResultList.Create;
// 添加验证结果
for I := 0 to FValidator.Results.Count - 1 do
begin
var ValidationResult := FValidator.Results[I];
var CanFix := False;
var FixedValue: string;
// 检查是否可以自动修复
CanFix := FValidator.TryAutoFix(ValidationResult.PropertyPath, ValidationResult.PropertyName, '', FixedValue);
// 添加到结果列表
Results.AddErrorWithFix(
ValidationResult.PropertyPath,
ValidationResult.PropertyName,
ValidationResult.ErrorMessage,
ValidationResult.FixSuggestion,
CanFix
);
end;
// 显示验证对话框
FValidationDialog.ShowResults(Results);
// 清理
Results.Free;
end;
procedure TConfigFixDemoForm.SelectProperty(const Path, Name: string);
var
Section, Key: string;
Row: Integer;
Pos: Integer;
begin
// 解析路径
Pos := Path.IndexOf('/');
if Pos > 0 then
begin
Section := Path.Substring(0, Pos);
Key := Path.Substring(Pos + 1);
end
else
begin
Section := Path;
Key := Name;
end;
// 对于INI值,在表格中查找并选择
if PageControl1.ActivePage = tsINI then
begin
for Row := 1 to sgINI.RowCount - 1 do
begin
if (sgINI.Cells[0, Row] = Section) and (sgINI.Cells[1, Row] = Key) then
begin
sgINI.Row := Row;
Break;
end;
end;
end
else if PageControl1.ActivePage = tsJSON then
begin
// 对于JSON,目前只能提示用户
StatusBar.SimpleText := Format('请在JSON中查找并修正: %s -> %s', [Path, Name]);
end;
end;
procedure TConfigFixDemoForm.FixProperty(const Path, Name: string);
var
Section, Key, Value, FixedValue: string;
Row: Integer;
Pos: Integer;
Fixed: Boolean;
begin
Fixed := False;
// 解析路径
Pos := Path.IndexOf('/');
if Pos > 0 then
begin
Section := Path.Substring(0, Pos);
Key := Path.Substring(Pos + 1);
end
else
begin
Section := Path;
Key := Name;
end;
// 对于INI值,在表格中查找并修复
if PageControl1.ActivePage = tsINI then
begin
for Row := 1 to sgINI.RowCount - 1 do
begin
if (sgINI.Cells[0, Row] = Section) and (sgINI.Cells[1, Row] = Key) then
begin
Value := sgINI.Cells[2, Row];
// 尝试自动修复
if FValidator.TryAutoFix(Path, Name, Value, FixedValue) then
begin
// 更新值
sgINI.Cells[2, Row] := FixedValue;
FINIFile.WriteString(Section, Key, FixedValue);
sgINI.Cells[3, Row] := '已修复';
Fixed := True;
end;
Break;
end;
end;
end
else if PageControl1.ActivePage = tsJSON then
begin
// 对于JSON,尝试查找并修复对应的值
// 递归函数修复JSON对象中的特定值
function TryFixJSONProperty(const BasePath: string; Obj: TJSONObject; const TargetPath, TargetName: string): Boolean;
var
I: Integer;
Pair: TJSONPair;
ChildPath: string;
begin
Result := False;
for I := 0 to Obj.Count - 1 do
begin
Pair := Obj.Pairs[I];
if BasePath = '' then
ChildPath := Pair.JsonString.Value
else
ChildPath := BasePath + '/' + Pair.JsonString.Value;
// 检查是否是目标路径
if (ChildPath = TargetPath) and (Pair.JsonString.Value = TargetName) then
begin
// 找到了目标,尝试修复
var ValueStr := '';
if Pair.JsonValue is TJSONString then
ValueStr := TJSONString(Pair.JsonValue).Value
else
ValueStr := Pair.JsonValue.ToString;
if FValidator.TryAutoFix(TargetPath, TargetName, ValueStr, FixedValue) then
begin
// 更新值
if Pair.JsonValue is TJSONString then
begin
Obj.RemovePair(Pair.JsonString.Value).Free;
Obj.AddPair(Pair.JsonString.Value, FixedValue);
Result := True;
end;
end;
Exit;
end
// 如果是子对象,递归查找
else if Pair.JsonValue is TJSONObject then
begin
if TryFixJSONProperty(ChildPath, Pair.JsonValue as TJSONObject, TargetPath, TargetName) then
begin
Result := True;
Exit;
end;
end;
end;
end;
// 尝试修复
if TryFixJSONProperty('', FJSONObject, Path, Name) then
begin
// 修复成功,更新显示
UpdateJSONDisplay;
Fixed := True;
end;
end;
// 更新状态
if Fixed then
StatusBar.SimpleText := '已自动修复配置项。点击"验证"按钮检查结果。'
else
StatusBar.SimpleText := '无法自动修复该配置项。';
end;
function TConfigFixDemoForm.CreateINIPropertyPath(const Section, Key: string): string;
begin
Result := Section + '/' + Key;
end;
procedure TConfigFixDemoForm.UpdateINIGrid;
var
Sections: TStringList;
Keys: TStringList;
I, J, Row: Integer;
begin
// 清空表格数据(保留标题行)
sgINI.RowCount := 2;
for I := 0 to sgINI.ColCount - 1 do
sgINI.Cells[I, 1] := '';
// 获取所有节
Sections := TStringList.Create;
try
FINIFile.ReadSections(Sections);
// 如果没有数据,直接返回
if Sections.Count = 0 then
Exit;
// 计算需要的行数
Row := 1;
for I := 0 to Sections.Count - 1 do
begin
Keys := TStringList.Create;
try
FINIFile.ReadSection(Sections[I], Keys);
Row := Row + Keys.Count;
finally
Keys.Free;
end;
end;
// 设置行数
sgINI.RowCount := Row;
// 填充表格
Row := 1;
for I := 0 to Sections.Count - 1 do
begin
Keys := TStringList.Create;
try
FINIFile.ReadSection(Sections[I], Keys);
for J := 0 to Keys.Count - 1 do
begin
sgINI.Cells[0, Row] := Sections[I];
sgINI.Cells[1, Row] := Keys[J];
sgINI.Cells[2, Row] := FINIFile.ReadString(Sections[I], Keys[J], '');
sgINI.Cells[3, Row] := '';
Inc(Row);
end;
finally
Keys.Free;
end;
end;
finally
Sections.Free;
end;
end;
procedure TConfigFixDemoForm.UpdateJSONDisplay;
begin
// 更新JSON显示
if Assigned(FJSONObject) then
memJSON.Text := FJSONObject.Format(2)
else
memJSON.Text := '';
end;
end.