-
Notifications
You must be signed in to change notification settings - Fork 35
/
NtUtils.XmlLite.pas
334 lines (266 loc) · 8.48 KB
/
NtUtils.XmlLite.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
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
unit NtUtils.XmlLite;
interface
uses
Ntapi.WinNt, Ntapi.ObjIdl, Ntapi.xmllite, NtUtils;
type
TXmlxNode = record
NodeType: TXmlNodeType;
Depth: Cardinal;
IsEmpty: LongBool;
QualifiedName: String;
LocalName: String;
Prefix: String;
NamespaceUri: String;
end;
TXmlxFilter = reference to function (const Node: TXmlxNode): Boolean;
IXmlxReader = interface
// Iterators: while Reader.MoveToNext.Save(Result) do {...}
function MoveToNext: TNtxStatus;
function MoveToNextAttribute: TNtxStatus;
// Node state
function GetParents: TArray<TXmlxNode>;
function GetCurrent: TXmlxNode;
function GetValue(out Value: String): TNtxStatus;
function GetBaseUri(out BaseUri: String): TNtxStatus;
function GetAttributeCount(out AttributeCount: Cardinal): TNtxStatus;
function GetLineNumber(out LineNumber: Cardinal): TNtxStatus;
function GetLinePosition(out LinePosition: Cardinal): TNtxStatus;
function IsEOF: LongBool;
function IsDefault: LongBool;
property Parents: TArray<TXmlxNode> read GetParents;
property Current: TXmlxNode read GetCurrent;
// Checks if the hierarchy of nodes up to the current one matches the filters.
// The current node is considered matching when each of its parents and the
// node itself satisfy the corresponding filters. I.e., the parent at depth
// 0 satisfies the filter #0, a parent on depth 1 satisfies the filter #1,
// and so on.
function Matches(const Hierarchy: TArray<TXmlxFilter>): Boolean;
end;
{ Reader creation }
// Start reading an XML from a stream
function XmlxCreateReader(
out Reader: IXmlxReader;
const Input: IStream
): TNtxStatus;
// Start reading an XML from a file
function XmlxCreateReaderOnFile(
out Reader: IXmlxReader;
[Access(GENERIC_READ)] const FileName: String
): TNtxStatus;
{ Built-in filters }
function ByLocalName(const LocalName: String): TXmlxFilter;
function ByQualifiedName(const QualifiedName: String): TXmlxFilter;
implementation
uses
Ntapi.ntstatus, Ntapi.Shlwapi, Ntapi.ntioapi, NtUtils.SysUtils;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
type
IXmlxReaderWrapper = class (TInterfacedObject, IXmlxReader)
private
FReader: IXmlReader;
FNodeStack: TArray<TXmlxNode>;
function SaveNodeData: TNtxStatus;
public
constructor Create(const Reader: IXmlReader);
function MoveToNext: TNtxStatus;
function MoveToNextAttribute: TNtxStatus;
function GetParents: TArray<TXmlxNode>;
function GetCurrent: TXmlxNode;
function GetValue(out Value: String): TNtxStatus;
function GetBaseUri(out BaseUri: String): TNtxStatus;
function GetAttributeCount(out AttributeCount: Cardinal): TNtxStatus;
function GetLineNumber(out LineNumber: Cardinal): TNtxStatus;
function GetLinePosition(out LinePosition: Cardinal): TNtxStatus;
function IsEOF: LongBool;
function IsDefault: LongBool;
function Matches(const Hierarchy: TArray<TXmlxFilter>): Boolean;
end;
{ IXmlxReaderWrapper }
constructor IXmlxReaderWrapper.Create;
begin
inherited Create;
FReader := Reader;
end;
function IXmlxReaderWrapper.GetAttributeCount;
begin
Result.Location := 'IXmlReader::GetAttributeCount';
Result.HResult := FReader.GetAttributeCount(AttributeCount)
end;
function IXmlxReaderWrapper.GetBaseUri;
var
Buffer: PWideChar;
BufferLength: Cardinal;
begin
Result.Location := 'IXmlReader::GetBaseUri';
Result.HResult := FReader.GetBaseUri(Buffer, @BufferLength);
if Result.IsSuccess then
SetString(BaseUri, Buffer, BufferLength);
end;
function IXmlxReaderWrapper.GetCurrent;
begin
if Length(FNodeStack) > 0 then
Result := FNodeStack[High(FNodeStack)]
else
Result := Default(TXmlxNode);
end;
function IXmlxReaderWrapper.GetLineNumber;
begin
Result.Location := 'IXmlReader::GetLineNumber';
Result.HResult := FReader.GetLineNumber(LineNumber);
end;
function IXmlxReaderWrapper.GetLinePosition;
begin
Result.Location := 'IXmlReader::GetLinePosition';
Result.HResult := FReader.GetLinePosition(LinePosition);
end;
function IXmlxReaderWrapper.GetParents;
begin
// Copy all items except for the last
Result := Copy(FNodeStack, 0, Length(FNodeStack) - 1);
end;
function IXmlxReaderWrapper.GetValue;
var
Buffer: PWideChar;
BufferLength: Cardinal;
begin
Result.Location := 'IXmlReader::GetValue';
Result.HResult := FReader.GetValue(Buffer, @BufferLength);
if Result.IsSuccess then
SetString(Value, Buffer, BufferLength);
end;
function IXmlxReaderWrapper.IsDefault;
begin
Result := FReader.IsDefault;
end;
function IXmlxReaderWrapper.IsEOF;
begin
Result := FReader.IsEOF;
end;
function IXmlxReaderWrapper.Matches;
var
i: Integer;
begin
Result := False;
// Check if the depth is right
if Length(FNodeStack) <> Length(Hierarchy) then
Exit;
// Check all nodes starting from the root
for i := 0 to High(Hierarchy) do
if Assigned(Hierarchy[i]) and not Hierarchy[i](FNodeStack[i]) then
Exit;
// All tests passed, that's a match
Result := True;
end;
function IXmlxReaderWrapper.MoveToNext;
var
NextNodeType: TXmlNodeType;
begin
// Advance the reader skipping attributes
Result.Location := 'IXmlReader::Read';
Result.HResultAllowFalse := FReader.Read(NextNodeType);
if Result.HResult = S_FALSE then
Result.Status := STATUS_NO_MORE_ENTRIES;
// Save the new node on the stack
if Result.IsSuccess then
Result := SaveNodeData;
end;
function IXmlxReaderWrapper.MoveToNextAttribute;
begin
// Advance the reader considering attributes
Result.Location := 'IXmlReader::MoveToNextAttribute';
Result.HResultAllowFalse := FReader.MoveToNextAttribute;
if Result.HResult = S_FALSE then
Result.Status := STATUS_NO_MORE_ENTRIES;
// Save the new node on the stack
if Result.IsSuccess then
Result := SaveNodeData;
end;
function IXmlxReaderWrapper.SaveNodeData;
var
Node: TXmlxNode;
Buffer: PWideChar;
BufferLength: Cardinal;
begin
Node.IsEmpty := FReader.IsEmptyElement;
// Save node type
Result.Location := 'IXmlReader::GetNodeType';
Result.HResult := FReader.GetNodeType(Node.NodeType);
if not Result.IsSuccess then
Exit;
// Save depth
Result.Location := 'IXmlReader::GetDepth';
Result.HResult := FReader.GetDepth(Node.Depth);
if not Result.IsSuccess then
Exit;
// Save qualified name
Result.Location := 'IXmlReader::GetQualifiedName';
Result.HResult := FReader.GetQualifiedName(Buffer, @BufferLength);
if not Result.IsSuccess then
Exit;
SetString(Node.QualifiedName, Buffer, BufferLength);
// Save local name
Result.Location := 'IXmlReader::GetLocalName';
Result.HResult := FReader.GetLocalName(Buffer, @BufferLength);
if not Result.IsSuccess then
Exit;
SetString(Node.LocalName, Buffer, BufferLength);
// Save prefix
Result.Location := 'IXmlReader::GetPrefix';
Result.HResult := FReader.GetPrefix(Buffer, @BufferLength);
if not Result.IsSuccess then
Exit;
SetString(Node.Prefix, Buffer, BufferLength);
// Save namespace URI
Result.Location := 'IXmlReader::GetNamespaceUri';
Result.HResult := FReader.GetNamespaceUri(Buffer, @BufferLength);
if not Result.IsSuccess then
Exit;
SetString(Node.NamespaceUri, Buffer, BufferLength);
// Update the node stack. Depending on the new depth, we will either truncate
// by one and replace the last, replace the last, or append.
SetLength(FNodeStack, Node.Depth + 1);
FNodeStack[Node.Depth] := Node;
end;
{ Creation functions }
function XmlxCreateReader;
var
ReaderRaw: IXmlReader;
begin
Result.Location := 'CreateXmlReader';
Result.HResult := CreateXmlReader(IXmlReader, ReaderRaw, nil);
if not Result.IsSuccess then
Exit;
Result.Location := 'IXmlReader::SetInput';
Result.HResult := ReaderRaw.SetInput(Input);
if Result.IsSuccess then
Reader := IXmlxReaderWrapper.Create(ReaderRaw);
end;
function XmlxCreateReaderOnFile;
var
Stream: IStream;
begin
Result.Location := 'SHCreateStreamOnFileEx';
Result.LastCall.OpensForAccess<TIoFileAccessMask>(GENERIC_READ);
Result.HResult := SHCreateStreamOnFileEx(PWideChar(FileName), STGM_READ, 0,
False, nil, Stream);
if Result.IsSuccess then
Result := XmlxCreateReader(Reader, Stream);
end;
{ Filters }
function ByLocalName;
begin
Result := function (const Node: TXmlxNode): Boolean
begin
Result := RtlxEqualStrings(Node.LocalName, LocalName);
end;
end;
function ByQualifiedName;
begin
Result := function (const Node: TXmlxNode): Boolean
begin
Result := RtlxEqualStrings(Node.QualifiedName, QualifiedName);
end;
end;
end.