forked from HemulGM/WindowsNotificationManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFMX.Win.Notification.XML.pas
More file actions
438 lines (358 loc) · 9.76 KB
/
FMX.Win.Notification.XML.pas
File metadata and controls
438 lines (358 loc) · 9.76 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
unit FMX.Win.Notification.XML;
interface
uses
System.SysUtils, System.Classes, System.Types, System.Math,
// Winapi
Winapi.Windows,
// Windows RT (Runtime)
Winapi.Winrt, Winapi.DataRT;
type
TXMLInterface = Xml_Dom_IXmlDocument;
// WinXML custom document management
TWinXMLNodes = class;
TWinXMLAttributes = class;
TWinXMLNode = class(TObject)
private
FTagName: string;
FParent: TWinXMLNode;
FContents: string; // content of the node, pre-other nodes
FNodes: TWinXMLNodes;
FAttributes: TWinXMLAttributes;
procedure SetTagName(const Value: string);
public
property TagName: string read FTagName write SetTagName;
property Parent: TWinXMLNode read FParent;
property Contents: string read FContents write FContents;
property Nodes: TWinXMLNodes read FNodes write FNodes;
property Attributes: TWinXMLAttributes read FAttributes write FAttributes;
procedure Delete;
procedure Detach;
function OuterXML: string;
function InnerXML: string;
function ToString: string; override;
constructor Create; virtual;
destructor Destroy; override;
end;
TWinXMLAttribute = record
Tag: string;
Value: string;
end;
TWinXMLNodes = class
private
FNodes: TArray<TWinXMLNode>;
FNodeManager: TWinXMLNode;
function GetNode(Index: integer): TWinXMLNode;
public
function Count: integer;
function DetachNode(Index: integer): boolean; overload;
function DetachNode(Node: TWinXMLNode): boolean; overload;
procedure AttachNode(Node: TWinXMLNode); // add existing node class
function FindNode(TagName: string): integer; overload;
function FindNode(Node: TWinXMLNode): integer; overload;
function HasNode(TagName: string): boolean;
function DeleteNode(Index: integer): boolean; overload;
function DeleteNode(Node: TWinXMLNode): boolean; overload;
function AddNode(TagName: string): TWinXMLNode; overload;
procedure Clear;
property Nodes[Index: integer]: TWinXMLNode read GetNode; default;
constructor Create(ForNode: TWinXMLNode);
destructor Destroy; override;
end;
TWinXMLAttributes = class
private
FAttributes: TArray<TWinXMLAttribute>;
public
function Count: integer;
function FindAttribute(ATag: string): integer;
function HasAttribute(ATag: string): boolean;
function DeleteAttribute(Index: integer): boolean; overload;
function DeleteAttribute(ATag: string): boolean; overload;
function GetAttribute(Tag: string): string; overload;
function GetAttribute(Index: integer): string; overload;
function GetAttributeTag(Index: integer): string;
procedure SetAttribute(Tag: string; const Value: string);
property Attributes[Tag: string]: string read GetAttribute write SetAttribute; default;
constructor Create;
end;
TWinXMLDocument = class(TWinXMLNode)
constructor Create; override;
end;
// DomXMLDocument
TDomXMLDocument = class(TObject)
public
DomXML: TXMLInterface;
procedure Parse(XMLDocument: string);
function Format: string;
constructor Create; overload;
constructor Create(FromString: string); overload;
constructor Create(FromInterface: TXMLInterface); overload;
destructor Destroy; override;
end;
function CreateNewXMLInterface: TXMLInterface;
implementation
uses
Win.WinRT;
function CreateNewXMLInterface: TXMLInterface;
var
instance: IInspectable;
xmlDoc: Xml_Dom_IXmlDocument;
HRes: HRESULT;
begin
// Initialize output to nil
xmlDoc := nil;
// Activate the XmlDocument instance
HRes := RoActivateInstance(TWindowsString.Create('Windows.Data.Xml.Dom.XmlDocument'), instance);
if Failed(HRes) then
raise Exception.CreateFmt('Failed to activate instance: 0x%.8x', [HRes]);
// Query for the IXmlDocument interface
xmlDoc := Xml_Dom_IXmlDocument(instance);
HRes := instance.QueryInterface(Xml_Dom_IXmlDocument, xmlDoc);
if Failed(HRes) then
raise Exception.CreateFmt('Failed to query IXmlDocument interface: 0x%.8x', [HRes]);
Result := xmlDoc;
end;
{ TDomXMLDocument }
constructor TDomXMLDocument.Create;
begin
DomXML := CreateNewXMLInterface;
end;
constructor TDomXMLDocument.Create(FromString: string);
begin
inherited Create;
Parse(FromString);
end;
constructor TDomXMLDocument.Create(FromInterface: TXMLInterface);
begin
DomXML := FromInterface;
end;
destructor TDomXMLDocument.Destroy;
begin
DomXML := nil;
inherited;
end;
function TDomXMLDocument.Format: string;
begin
var HStr := (DomXML.DocumentElement as Xml_Dom_IXmlNodeSerializer).GetXml;
try
Result := HStr.ToString;
finally
WindowsDeleteString(HStr);
end;
end;
procedure TDomXMLDocument.Parse(XMLDocument: string);
begin
(DomXML as Xml_Dom_IXmlDocumentIO).LoadXml(TWindowsString.Create(XMLDocument));
end;
{ TWinXMLBranch }
constructor TWinXMLNode.Create;
begin
inherited;
FTagName := 'node';
FNodes := TWinXMLNodes.Create(Self);
FAttributes := TWinXMLAttributes.Create;
end;
procedure TWinXMLNode.Delete;
begin
if Parent <> nil then
Parent.Nodes.DeleteNode(Self);
end;
destructor TWinXMLNode.Destroy;
begin
FAttributes.Free;
FNodes.Free;
inherited;
end;
procedure TWinXMLNode.Detach;
begin
if Parent <> nil then
Parent.Nodes.DetachNode(Self);
end;
function TWinXMLNode.InnerXML: string;
begin
Result := Contents;
for var I := 0 to Nodes.Count - 1 do
Result := Result + Nodes[I].ToString;
end;
function TWinXMLNode.OuterXML: string;
var
Attrib, Interior: string;
begin
// Inner XML
Interior := InnerXML;
// Attribute
Attrib := '';
for var I := 0 to Attributes.Count - 1 do
Attrib := Format('%S %S="%S"', [Attrib, Attributes.GetAttributeTag(I), Attributes.GetAttribute(I)]);
// First tag
if Interior = '' then
Attrib := Attrib + '/';
Result := Format('<%S%S>', [TagName, Attrib]);
// Interior + Closing tag
if Interior <> '' then
Result := Result + Interior + Format('</%S>', [TagName]);
end;
procedure TWinXMLNode.SetTagName(const Value: string);
begin
if Value = '' then
raise Exception.Create('Tag name cannot be empty.');
FTagName := Value;
end;
function TWinXMLNode.ToString: string;
begin
Result := OuterXML;
end;
{ TWinXMLNodes }
function TWinXMLNodes.AddNode(TagName: string): TWinXMLNode;
begin
Result := TWinXMLNode.Create;
Result.TagName := TagName;
AttachNode(Result);
end;
procedure TWinXMLNodes.AttachNode(Node: TWinXMLNode);
begin
// Detach if attached
Node.Detach;
// Allocate space
const Index = Count;
SetLength(FNodes, Index + 1);
Node.FParent := FNodeManager;
// Attach
FNodes[Index] := Node;
end;
procedure TWinXMLNodes.Clear;
begin
for var I := Count - 1 downto 0 do
FNodes[I].Free;
SetLength(FNodes, 0);
end;
function TWinXMLNodes.Count: integer;
begin
Result := length(FNodes);
end;
constructor TWinXMLNodes.Create(ForNode: TWinXMLNode);
begin
inherited Create;
FNodeManager := ForNode;
FNodes := [];
end;
function TWinXMLNodes.DeleteNode(Node: TWinXMLNode): boolean;
begin
Result := DeleteNode(FindNode(Node));
end;
destructor TWinXMLNodes.Destroy;
begin
Clear;
inherited;
end;
function TWinXMLNodes.DetachNode(Node: TWinXMLNode): boolean;
begin
Result := DetachNode(FindNode(Node));
end;
function TWinXMLNodes.DetachNode(Index: integer): boolean;
begin
Result := InRange(Index, 0, Count - 1);
if not Result then
Exit;
FNodes[Index].FParent := nil;
for var I := Index to Count - 2 do
FNodes[I] := FNodes[I + 1];
SetLength(FNodes, Count - 1);
end;
function TWinXMLNodes.FindNode(Node: TWinXMLNode): integer;
begin
for var I := 0 to High(FNodes) do
if FNodes[I] = Node then
Exit(I);
Exit(-1);
end;
function TWinXMLNodes.DeleteNode(Index: integer): boolean;
begin
Result := InRange(Index, 0, Count - 1);
if not Result then
Exit;
FNodes[Index].Free;
DetachNode(Index);
end;
function TWinXMLNodes.FindNode(TagName: string): integer;
begin
for var I := 0 to High(FNodes) do
if FNodes[I].TagName = TagName then
Exit(I);
Exit(-1);
end;
function TWinXMLNodes.GetNode(Index: integer): TWinXMLNode;
begin
Result := FNodes[Index];
end;
function TWinXMLNodes.HasNode(TagName: string): boolean;
begin
Result := FindNode(TagName) <> -1;
end;
{ TWinXMLAttributes }
function TWinXMLAttributes.Count: integer;
begin
Result := length(FAttributes);
end;
constructor TWinXMLAttributes.Create;
begin
FAttributes := [];
end;
function TWinXMLAttributes.DeleteAttribute(ATag: string): boolean;
begin
Result := DeleteAttribute(FindAttribute(ATag));
end;
function TWinXMLAttributes.DeleteAttribute(Index: integer): boolean;
begin
Result := InRange(Index, 0, Count - 1);
if not Result then
Exit;
for var I := Index to Count - 2 do
FAttributes[I] := FAttributes[I + 1];
SetLength(FAttributes, Count - 1);
end;
function TWinXMLAttributes.FindAttribute(ATag: string): integer;
begin
for var I := 0 to Count - 1 do
if FAttributes[I].Tag = ATag then
Exit(I);
Exit(-1);
end;
function TWinXMLAttributes.GetAttribute(Index: integer): string;
begin
Result := FAttributes[Index].Value;
end;
function TWinXMLAttributes.GetAttributeTag(Index: integer): string;
begin
Result := FAttributes[Index].Tag;
end;
function TWinXMLAttributes.GetAttribute(Tag: string): string;
begin
const Index = FindAttribute(Tag);
Result := FAttributes[Index].Value;
end;
function TWinXMLAttributes.HasAttribute(ATag: string): boolean;
begin
Result := FindAttribute(ATag) <> -1;
end;
procedure TWinXMLAttributes.SetAttribute(Tag: string; const Value: string);
var
Index: integer;
begin
// Get Index
Index := FindAttribute(Tag);
if Index = -1 then
begin
Index := Count;
SetLength(FAttributes, Index + 1);
FAttributes[Index].Tag := Tag;
end;
// Set
FAttributes[Index].Value := Value;
end;
{ TWinXMLDocument }
constructor TWinXMLDocument.Create;
begin
inherited Create;
FTagName := 'xml';
end;
end.