-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSystem.MHT.RecordingXML.pas
225 lines (192 loc) · 6.66 KB
/
System.MHT.RecordingXML.pas
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
unit System.MHT.RecordingXML;
interface
uses
System.SysUtils,
System.RegularExpressions,
Xml.XMLIntf;
type
TEachAction = record
ActionNumber: Integer;
Time: string;
FileVersion: string;
FileName: string;
CommandLine: string;
Description: string;
Action: string;
ScreenshotFileName: string;
end;
IMHTRecordingXmlParser = interface(IInvokable)
['{11D71D02-D51E-4746-AC7C-11B070069B0A}']
function Xml(): string; overload;
function Xml(const Value: string): IMHTRecordingXmlParser; overload;
function Execute(): TArray<TEachAction>;
end;
TMHTRecordingXmlParser = class(TInterfacedObject, IMHTRecordingXmlParser)
strict private
FXml: string;
FXmlDoc: IXMLDocument;
private
function GetRecursiveFirstSearchNode(BaseNode: IXMLNode; const SearchNodeName: string): IXMLNode;
function ReplaceInvalidXmlCharacters(const Match: TMatch): string;
procedure OpenXmlDoc();
public
constructor Create();
destructor Destroy(); override;
function Xml(): string; overload;
function Xml(const Value: string): IMHTRecordingXmlParser; overload;
function Execute(): TArray<TEachAction>;
end;
implementation
uses
{$IFDEF MSWINDOWS}Winapi.ActiveX,{$ENDIF}
Xml.XMLDoc,
Xml.xmldom;
{ TMHTRecordingXmlParser }
constructor TMHTRecordingXmlParser.Create;
begin
inherited;
{$IFDEF MSWINDOWS}CoInitialize(nil);{$ENDIF}
FXmlDoc := TXMLDocument.Create(nil);
end;
destructor TMHTRecordingXmlParser.Destroy;
begin
FXmlDoc.Active := False;
FXmlDoc := nil;
{$IFDEF MSWINDOWS}CoUninitialize;{$ENDIF}
inherited;
end;
function TMHTRecordingXmlParser.Xml(const Value: string): IMHTRecordingXmlParser;
begin
FXml := Value;
Result := Self;
end;
function TMHTRecordingXmlParser.Xml: string;
begin
Result := FXml;
end;
function TMHTRecordingXmlParser.GetRecursiveFirstSearchNode(BaseNode: IXMLNode; const SearchNodeName: string): IXMLNode;
begin
Result := nil;
if string(BaseNode.NodeName).StartsWith(SearchNodeName) then
begin
Exit(BaseNode);
end
else
if (not Assigned(BaseNode.ChildNodes)) then
begin
Exit(nil);
end;
for var Idx: Integer := 0 to BaseNode.ChildNodes.Count - 1 do
begin
Result := GetRecursiveFirstSearchNode(BaseNode.ChildNodes[Idx], SearchNodeName);
if Assigned(Result) then
Exit;
end;
end;
function TMHTRecordingXmlParser.Execute: TArray<TEachAction>;
const
UserActionData = 'UserActionData';
RecordSession = 'RecordSession';
Description = 'Description';
Action = 'Action';
ScreenshotFileName = 'ScreenshotFileName';
var
InnerRoot: IXMLNode;
UserActionDataNode: IXMLNode;
RecordSessionNode: IXMLNode;
EachActionNodes: IXMLNode;
AnyNode: IXMLNode;
Actions: Integer;
InnerFunc: TFunc<IXMLNode, string, string>;
i: Integer;
begin
OpenXmlDoc;
InnerRoot := FXmlDoc.DocumentElement;
UserActionDataNode := GetRecursiveFirstSearchNode(InnerRoot, UserActionData);
if Assigned(UserActionDataNode) then
begin
RecordSessionNode := GetRecursiveFirstSearchNode(UserActionDataNode, RecordSession);
Actions := string(RecordSessionNode.Attributes['ActionCount']).ToInteger();
SetLength(Result, Actions);
for var Idx: Integer := 0 to RecordSessionNode.ChildNodes.Count - 1 do
begin
if Idx = 98 then
i := idx;
try
Result[Idx].ActionNumber := string(RecordSessionNode.ChildNodes[Idx].Attributes['ActionNumber']).ToInteger();
Result[Idx].Time := string(RecordSessionNode.ChildNodes[Idx].Attributes['Time']);
if RecordSessionNode.ChildNodes[Idx].HasAttribute('FileVersion') then
Result[Idx].FileVersion := string(RecordSessionNode.ChildNodes[Idx].Attributes['FileVersion']);
Result[Idx].FileName := string(RecordSessionNode.ChildNodes[Idx].Attributes['FileName']);
Result[Idx].CommandLine := string(RecordSessionNode.ChildNodes[Idx].Attributes['CommandLine']);
EachActionNodes := RecordSessionNode.ChildNodes[Idx];
InnerFunc :=
function(FromNodes: IXMLNode; ToSearch: string): string
begin
var InnerNode := GetRecursiveFirstSearchNode(FromNodes, ToSearch);
if (Assigned(InnerNode) and InnerNode.IsTextElement) then
begin
Result := InnerNode.Text;
end
else
Exit(string.Empty);
end;
Result[Idx].Description := InnerFunc(EachActionNodes, Description);
Result[Idx].Action := InnerFunc(EachActionNodes, Action);
Result[Idx].ScreenshotFileName := InnerFunc(EachActionNodes, ScreenshotFileName);
except
i := Idx;
end;
// AnyNode := GetRecursiveFirstSearchNode(EachActionNodes, Description);
// if (Assigned(AnyNode) and AnyNode.IsTextElement) then
// begin
// Result[Idx].Description := AnyNode.Text;
// end;
//
// AnyNode := GetRecursiveFirstSearchNode(EachActionNodes, Action);
// if (Assigned(AnyNode) and AnyNode.IsTextElement) then
// begin
// Result[Idx].Action := AnyNode.Text;
// end;
//
// AnyNode := GetRecursiveFirstSearchNode(EachActionNodes, ScreenshotFileName);
// if (Assigned(AnyNode) and AnyNode.IsTextElement) then
// begin
// Result[Idx].ScreenshotFileName := AnyNode.Text;
// end;
end;
end;
// LBase64: string;
// LTBytes: TBytes;
// ChildNode := ChildNode.ChildNodes.FindNode('BinaryBase64Object', 'schema');
// if ChildNode <> nil then
// begin
// LBase64 := ChildNode.Text;
// LTBytes := TNetEncoding.Base64.DecodeStringToBytes(LBase64);
// TFile.WriteAllBytes(TPath.ChangeExtension(AFilename, '.xslt'), LTBytes);
// end
end;
procedure TMHTRecordingXmlParser.OpenXmlDoc;
begin
FXmlDoc.Active := False;
if FXml.IsEmpty then
Exit;
// https://dvteclipse.com/documentation/svlinter/How_to_use_special_characters_in_XML.3F.html
FXml := TRegEx.Replace(FXml, 'name=\"[^\"]*\"', ReplaceInvalidXmlCharacters);
FXmlDoc.LoadFromXML(FXml);
FXmlDoc.Options := FXmlDoc.Options + [doNodeAutoIndent];
FXmlDoc.Active := True;
end;
function TMHTRecordingXmlParser.ReplaceInvalidXmlCharacters(const Match: TMatch): string;
begin
// Found symbol files with invalid characters in attribute, sample:
// <symbol name="{System.Generics.Collections}TList<System.Tether.Manager.TTetheringProfileInfo>.GetItem"/>
Result := Match
.Value
.Replace('<', '<')
.Replace('>', '>')
.Replace('&', '&');
// .Replace('"', '"')
// .Replace('''', ''');
end;
end.