-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlUnit.pas
More file actions
570 lines (500 loc) · 11.3 KB
/
XmlUnit.pas
File metadata and controls
570 lines (500 loc) · 11.3 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
unit XmlUnit;
interface
uses
Classes;
type
c_XmlElement = class(TObject)
private
{: Inserts a "\" in front of any """. }
class function Escape(
const p_str: string
): string;
{: Deletes a "\" in front of andy "\"". }
class function Epacse(
const p_str: string
): string;
public
m_strName: string;
m_strValue: string;
m_strlAttribute: TStringList;
m_strlElement: TStringList;
private
function LoadElement(
): boolean;
procedure WriteToStream(
const p_stm: TStream;
const p_str: string
);
public
function AddElement(
const p_strName: string
): c_XmlElement;
procedure SetAttribute(
const p_strName: string;
const p_strValue: string
);
function GetAttribute(
const p_strName: string
): string;
function GetElement(
const p_i: integer
): c_XmlElement;
constructor Create(
const p_strName: string
);
destructor Destroy(
); override;
procedure SaveToStream(
const p_stm: TStream;
const p_strIndent: string
);
procedure LoadFromStream(
const p_stm: TStream
);
function AsString(
): string;
end;
c_XmlAttribute = class(TObject)
private
m_strValue: string;
public
function GetValue(
): string;
procedure SetValue(
const p_str: string
);
end;
implementation
uses
SysUtils;
var
u_st: TStream;
u_ch: char;
u_str: string;
function QryCharacter(
): char;
begin
result := u_ch;
end;
function GetCharacter(
): char;
begin
result := QryCharacter();
u_ch := #$00;
if (u_st.Position < u_st.Size) then begin
u_st.ReadBuffer(u_ch, sizeof(u_ch));
end;
end;
function EOF(
): boolean;
begin
result := (#$00 = QryCharacter());
end;
procedure Bing(
);
begin
u_str := u_str + GetCharacter();
end;
function QryWord(
): string;
begin
result := u_str;
end;
function QryQuote(
): boolean;
begin
result := ((length(QryWord()) > 0) and ('"' = QryWord()[1]));
end;
function QryAlphaNumeric(
): boolean;
begin
result := ((length(QryWord()) > 0) and (QryWord()[1] in ['_', 'A'..'Z', 'a'..
'z']));
end;
function GetWord(
): string;
begin
result := QryWord();
u_str := '';
while (QryCharacter() in [#$0A, #$0D, ' ']) do begin
GetCharacter();
end;
if ('<' = QryCharacter()) then begin
Bing();
if ('/' <> QryCharacter()) then begin
exit;
end;
Bing();
exit;
end;
if ('/' = QryCharacter()) then begin
Bing();
if ('>' <> QryCharacter()) then begin
exit;
end;
Bing();
exit
end;
if ('>' = QryCharacter()) then begin
Bing();
exit;
end;
if ('=' = QryCharacter()) then begin
Bing();
exit;
end;
if ('"' = QryCharacter()) then begin
repeat
Bing();
if (EOF()) then begin
exit;
end;
until ('"' = QryCharacter);
Bing();
exit;
end;
if (QryCharacter() in ['_', 'A'..'Z', 'a'..'z']) then begin
repeat
Bing();
until (not (QryCharacter() in ['_', '0'..'9', 'A'..'Z', 'a'..'z']));
exit;
end;
if (QryCharacter() in ['0'..'9']) then begin
repeat
Bing();
until (not (QryCharacter() in ['0'..'9']));
exit;
end;
end;
function c_XmlElement.AddElement(
const p_strName: string
): c_XmlElement;
begin
result := c_XmlElement.Create(p_strName);
try
m_strlElement.AddObject(p_strName, result);
except
FreeAndNil(result);
raise;
end;
end;
constructor c_XmlElement.Create(
const p_strName: string
);
begin
m_strName := p_strName;
m_strlAttribute := TStringList.Create();
// m_strlAttribute.Sorted := true;
m_strlElement := TStringList.Create();
m_strlElement.Duplicates := dupAccept;
// m_strlElement.Sorted := true;
end;
destructor c_XmlElement.Destroy(
);
var
l_i: integer;
l_strl: TStringList;
begin
l_strl := m_strlElement;
for l_i := 0 to l_strl.Count - 1 do begin
l_strl.Objects[l_i].Free();
end;
FreeAndNil(m_strlElement);
l_strl := m_strlAttribute;
for l_i := 0 to l_strl.Count - 1 do begin
l_strl.Objects[l_i].Free();
end;
FreeAndNil(m_strlAttribute);
inherited;
end;
function c_XmlElement.GetAttribute(
const p_strName: string
): string;
var
l_i: integer;
l_strl: TStringList;
begin
l_strl := m_strlAttribute;
l_i := l_strl.IndexOf(p_strName);
if (- 1 = l_i) then begin
result := '';
exit;
end;
result := c_XmlAttribute(l_strl.Objects[l_i]).GetValue();
end;
function c_XmlElement.GetElement(
const p_i: integer
): c_XmlElement;
var
l_strl: TStringList;
begin
l_strl := m_strlElement;
if ((p_i < 0) or (p_i >= l_strl.Count)) then begin
result := nil;
exit;
end;
result := c_XmlElement(l_strl.Objects[p_i]);
end;
function c_XmlElement.LoadElement(
): boolean;
var
l_strElementName: string;
l_strAttributeName: string;
l_xe: c_XmlElement;
l_str: string;
begin
result := false;
if ('<' <> QryWord()) then begin
exit;
end;
GetWord();
if (not QryAlphaNumeric()) then begin
exit;
end;
l_strElementName := GetWord();
m_strName := l_strElementName;
while (QryAlphaNumeric()) do begin
l_strAttributeName := GetWord();
if ('=' <> QryWord()) then begin
exit;
end;
GetWord();
if (not QryQuote()) then begin
exit;
end;
l_str := GetWord();
delete(l_str, 1, 1);
delete(l_str, length(l_str), 1);
SetAttribute(l_strAttributeName, Epacse(l_str));
end;
if ('/>' = QryWord()) then begin
GetWord();
result := true;
exit;
end;
if ('>' <> QryWord()) then begin
exit;
end;
GetWord();
// either we get the value here are an open tag.
while ('<' = QryWord()) do begin
l_xe := AddElement('');
if (not l_xe.LoadElement()) then begin
exit;
end;
end;
if ('</' <> QryWord()) then begin
exit;
end;
GetWord();
if (QryWord() <> l_strElementName) then begin
exit;
end;
GetWord();
if ('>' <> QryWord()) then begin
exit;
end;
GetWord();
result := true;
end;
procedure c_XmlElement.LoadFromStream(
const p_stm: TStream
);
begin
u_st := p_stm;
GetCharacter();
GetWord();
LoadElement();
end;
procedure c_XmlElement.SaveToStream(
const p_stm: TStream;
const p_strIndent: string
);
var
l_i: integer;
begin
WriteToStream(p_stm, p_strIndent);
WriteToStream(p_stm, '<');
WriteToStream(p_stm, m_strName);
for l_i := 0 to m_strlAttribute.Count - 1 do begin
WriteToStream(p_stm, ' ');
WriteToStream(p_stm, m_strlAttribute.Strings[l_i]);
WriteToStream(p_stm, '="');
WriteToStream(p_stm, Escape(c_XMLAttribute(m_strlAttribute.Objects[
l_i]).GetValue()));
WriteToStream(p_stm, '"');
end;
if (0 = m_strlElement.Count) then begin
WriteToStream(p_stm, ' />'#$D#$A);
exit;
end;
WriteToStream(p_stm, '>'#$D#$A);
for l_i := 0 to m_strlElement.Count - 1 do begin
c_XmlElement(m_strlElement.Objects[l_i]).SaveToStream(p_stm, p_strIndent +
' ');
end;
WriteToStream(p_stm, p_strIndent);
WriteToStream(p_stm, '</');
WriteToStream(p_stm, m_strName);
WriteToStream(p_stm, '>'#$D#$A);
end;
procedure c_XmlElement.SetAttribute(
const p_strName: string;
const p_strValue: string
);
var
l_i: integer;
l_strl: TStringList;
begin
l_strl := m_strlAttribute;
l_i := l_strl.IndexOf(p_strName);
if (- 1 = l_i) then begin
l_i := l_strl.AddObject(p_strName, c_XmlAttribute.Create());
end;
c_XmlAttribute(l_strl.Objects[l_i]).SetValue(p_strValue);
end;
function c_XmlAttribute.GetValue(
): string;
begin
result := m_strValue;
end;
procedure c_XmlAttribute.SetValue(
const p_str: string
);
begin
m_strValue := p_str;
end;
procedure c_XmlElement.WriteToStream(
const p_stm: TStream;
const p_str: string
);
begin
p_stm.WriteBuffer(pchar(p_str)^, length(p_str));
end;
class function c_XmlElement.Escape(
const p_str: string
): string;
var
l_i: integer;
l_j: integer;
l_c: cardinal; // Count of characters in string.
l_d: cardinal; // Count of """'s in string.
begin
{: Find out how many """'s and "%"'s we need to escape. }
l_c := length(p_str);
l_d := 0;
for l_i := 1 to l_c do begin
if (p_str[l_i] in ['%', '"', '<', '>', '&']) then begin
inc(l_d);
end;
end;
{: Make our resulting string the size of the original string plus the number
of extra characters we need, which is two for each """ and "%". }
setlength(result, l_c + l_d * 2);
{: Copy the original string to the resulting string adding "%xx"'s where
needed. }
l_j := 1;
for l_i := 1 to l_c do begin
if ('"' = p_str[l_i]) then begin
result[l_j] := '%';
inc(l_j);
result[l_j] := '2';
inc(l_j);
result[l_j] := '2';
inc(l_j);
end else if ('%' = p_str[l_i]) then begin
result[l_j] := '%';
inc(l_j);
result[l_j] := '2';
inc(l_j);
result[l_j] := '5';
inc(l_j);
end else if ('<' = p_str[l_i]) then begin
result[l_j] := '%';
inc(l_j);
result[l_j] := '3';
inc(l_j);
result[l_j] := 'C';
inc(l_j);
end else if ('>' = p_str[l_i]) then begin
result[l_j] := '%';
inc(l_j);
result[l_j] := '3';
inc(l_j);
result[l_j] := 'E';
inc(l_j);
end else if ('&' = p_str[l_i]) then begin
result[l_j] := '%';
inc(l_j);
result[l_j] := '2';
inc(l_j);
result[l_j] := '6';
inc(l_j);
end else begin
result[l_j] := p_str[l_i];
inc(l_j);
end;
end;
end;
class function c_XmlElement.Epacse(
const p_str: string
): string;
function HexDigitToByte(
const p_chr: char
): byte;
begin
if (p_chr in ['a'..'z']) then begin
result := byte(p_chr) - ord('a') + ord(#$A);
end else if (p_chr in ['A'..'Z']) then begin
result := byte(p_chr) - ord('A') + ord(#$A);
end else if (p_chr in ['0'..'9']) then begin
result := byte(p_chr) - ord('0');
end else begin
raise Exception.Create('Invalid hexidecimal digit.');
end;
end;
var
l_c: cardinal;
l_i: cardinal;
l_j: cardinal;
begin
l_c := length(p_str);
setlength(result, l_c);
l_i := 1;
l_j := 0;
while (l_i <= l_c) do begin
if ('%' = p_str[l_i]) then begin
inc(l_i);
if (l_i >= l_c) then begin
raise Exception.Create('Invalid hexidecimal digit.');
end;
inc(l_j);
result[l_j] := char(HexDigitToByte(p_str[l_i]) * 16 + HexDigitToByte(
p_str[l_i + 1]));
inc(l_i, 2);
end else begin
inc(l_j);
result[l_j] := p_str[l_i];
inc(l_i);
end;
end;
setlength(result, l_j);
end;
function c_XmlElement.AsString(
): string;
var
l_ms: TMemoryStream;
l_cb: integer;
begin
l_ms := TMemoryStream.Create();
try
SaveToStream(l_ms, '');
l_ms.Position := 0;
l_cb := l_ms.Size;
setlength(result, l_cb);
l_ms.ReadBuffer(pchar(result)^, l_cb);
finally
FreeAndNil(l_ms);
end;
end;
end.