-
Notifications
You must be signed in to change notification settings - Fork 0
/
UXmlParser.pas
610 lines (521 loc) · 13.7 KB
/
UXmlParser.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
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
unit UXmlParser;
interface
uses
SysUtils, System.Zip,
UMyMemoryStream, UXmlNode, Classes;
type
// xml Grammar:
//
// body ::= [comment] "<" Name (attribute)* ( "/>" | ">" (body)* "</" Name ">")
// attribute ::= AttributeId "=" """" Value """"
// alternativ "comment": <!-- comment -->
KXmlParser = class(TMyMemoryStream)
private
stream: string;
mActualPos: integer; // actual parser position
mErrorPos: integer; // error position in mStream
public
mRecursionDepth: integer; // recursion depth within xml stream
mStatusOk: boolean; // parser status
// GetAttribute results
// GetAttribute parses the xml stream
// Attr="Value"
// ^ ^- mValueLen
// +------- mAttribute
mValuePos: integer;
mValueLen: integer;
mAttribute: string;
function SubString(Len: integer): string;
constructor Create;
procedure GetAttribute();
procedure GetTagName(var TagName: string);
procedure SetError(ErrPos: integer);
// init. parser
procedure Initialize();
// parses header of the form:
// <?xml version="1.0" encoding="UTF-8"?> analysieren.
function ParseXmlHeader(): boolean;
// the character at the parser position might not be:
// 0, '/', '>', or '<'
function IsActualPositionValid: boolean;
function GetIdentifier(var Id: string): integer;
// skips blanks, line feeds and tabulator characters
function SkipSpaces(): char;
// skips comments
// if the result is true: the next parsing character is an '<'
function StartNewBody(XmlNode: KXmlNode = nil): boolean;
// EinBody sollte mit "</BodyName>" abgeschlossen sein.
procedure TestBodyEnd(TagName: string);
// it expects Char at the parser position (all other characters are an
// error)
procedure NeedChar(c: Char);
// true, if Char is at the parser position
function TestChar(c: Char): boolean;
// "/>": there is not a subbody
// ">": there is a subbody
function HasSubBody(): boolean;
function GenerateFromXml(parent: KXmlNode): boolean;
function Parse(Root: KXmlNode): boolean;
function ParseStream_(Root: KXmlNode): boolean;
class function ParseFile(FileName: string; var Root: KXmlNode): boolean;
class function ParseStream(const Stream: TBytes; var Root: KXmlNode): boolean;
end;
implementation
function IsFirstIdentChar(c: char): boolean;
begin
result := CharInSet(c, ['_', 'a'..'z', 'A'..'Z']);
end;
function IsIdentifierChar(c: char): boolean;
begin
result := CharInSet(c, ['_', 'a'..'z', 'A'..'Z', '0'..'9', '-']);
end;
////////////////////////////////////////////////////////////////////////////////
//
// KXmlParser
//
////////////////////////////////////////////////////////////////////////////////
function KXmlParser.SubString(Len: integer): string;
begin
result := '';
if mActualPos + Len <= Length(Stream) then
result := Copy(Stream, mActualPos, Len);
end;
constructor KXmlParser.Create;
begin
Initialize();
end;
procedure KXmlParser.Initialize();
begin
mAttribute := '';
// if there is no stream: it is an error
mStatusOk := true;
mActualPos := 1;
mRecursionDepth := 0;
mErrorPos := 0;
mValuePos := 1;
mValueLen := 0;
end;
procedure KXmlParser.GetAttribute();
var
loop: boolean;
begin
if not mStatusOk then
exit;
// attribute ::= AttributeId "=" """" Value """"
GetIdentifier(mAttribute);
NeedChar('=');
NeedChar('"');
mValuePos := mActualPos;
mValueLen := 0;
if (mStatusOk) then
begin
Loop := true;
repeat
case Stream[mActualPos] of
'"': begin
inc(mActualPos);
Loop := false;
end; // alles i.o.
#0,
'>',
'<': begin
SetError(mValuePos);
Loop := false;
end;
else begin
inc(mValueLen);
inc(mActualPos);
end;
end;
until not Loop;
end;
end;
procedure KXmlParser.GetTagName(var TagName: string);
begin
NeedChar('<');
SkipSpaces();
GetIdentifier(TagName);
end;
function KXmlParser.IsActualPositionValid: boolean;
begin
result := false;
if mStatusOk then
result := not CharInSet(SkipSpaces(), [#0, '<', '/', '>']);
end;
function KXmlParser.GetIdentifier(var Id: string): integer;
begin
result := 0;
Id := '';
if mStatusOk then
begin
SkipSpaces();
if (IsFirstIdentChar(Stream[mActualPos])) then
begin
while (IsIdentifierChar(Stream[mActualPos])) do
begin
SetLength(Id, Length(Id)+1);
Id[Length(Id)] := Stream[mActualPos];
inc(result);
inc(mActualPos);
end;
end;
if result = 0 then
SetError(-1);
end;
end;
function KXmlParser.ParseXmlHeader(): boolean;
var
Pos: integer;
begin
result := false;
if not mStatusOk or (Stream[mActualPos] = #0) then
exit;
// UTF-8 BOM (byte order mark)
if SubString(3) = #$EF#$BB#$BF then
inc(mActualPos, 3);
if (Stream[mActualPos] = '<') and
(Stream[mActualPos + 1] = '?') then
begin
NeedChar('<');
NeedChar('?');
if (mStatusOk) then
begin
SkipSpaces();
if SubString(3) = 'xml' then
inc(mActualPos, 3)
else
SetError(-1);
end;
if (mStatusOk) then
begin
Pos := mActualPos;
while (Stream[mActualPos] <> #0) and (Stream[mActualPos] <> '?') do
inc(mActualPos);
if (Stream[mActualPos] = '?') then
inc(mActualPos)
else
SetError(Pos);
end;
NeedChar('>');
end;
SkipSpaces();
if (SubString(2) = '<!') and
(SubString(4) <> '!--') then // DOCTYPE
begin
while not CharInSet(Stream[mActualPos], [#0, '>']) do
inc(mActualPos);
if Stream[mActualPos] = '>' then
inc(mActualPos);
end;
result := StartNewBody(nil);
end;
procedure KXmlParser.SetError(ErrPos: integer);
begin
if (mStatusOk) then
begin
mStatusOk := false;
if (ErrPos >= 0) then
mErrorPos := ErrPos
else
mErrorPos := mActualPos;
end;
end;
function KXmlParser.SkipSpaces(): char;
begin
result := #0;
if (mStatusOk) then
begin
result := Stream[mActualPos];
while CharInSet(result, [#1..' ']) do
begin
inc(mActualPos);
result := Stream[mActualPos];
end;
end;
end;
function KXmlParser.StartNewBody(XmlNode: KXmlNode): boolean;
var
IsComment: boolean;
c: char;
Pos: integer;
begin
result := false;
if not mStatusOk then
exit;
repeat
// [comment] skip comment
Pos := mActualPos;
while not CharInSet(Stream[mActualPos], [#0, '<', '>']) do
inc(mActualPos);
// "<" Name (attribute)* ... a tag starts with "<"
if (Stream[mActualPos] <> '<') then
begin
mActualPos := Pos;
exit; // '<' is missing (it is an error)
end;
// Stream[mActualPos] = '<'
IsComment := false;
// <!-- Sample XML file -->
if SubString(4) = '<!--' then
begin
inc(mActualPos, 4);
SkipSpaces();
Pos := mActualPos;
repeat
if SubString(3) = '-->' then
begin
XmlNode.AppendChildNode('').Value := trim(Copy(Stream, Pos, mActualPos-Pos));
inc(mActualPos, 3);
IsComment := true;
break;
end;
inc(mActualPos);
until Stream[mActualPos] = #0;
if not IsComment then
exit; // end of comment is missing (it is an error)
end;
until not IsComment;
// test for body end:
// if there is "< /": jump back
Pos := mActualPos;
inc(mActualPos);
c := SkipSpaces();
mActualPos := Pos;
if c = '/' then
begin
exit; // body end found
end;
// there is:
// itsXmlParser.mActualPos[0] = '<'
result := mStatusOk;
end;
procedure KXmlParser.TestBodyEnd(TagName: string);
var
EndTag: string;
ErrPos: integer;
begin
// test for "Body" end: </TagName>
NeedChar('<');
NeedChar('/');
if (mStatusOk) then
begin
SkipSpaces();
ErrPos := mActualPos;
GetIdentifier(EndTag);
if TagName <> EndTag then
SetError(ErrPos);
end;
NeedChar('>');
end;
procedure KXmlParser.NeedChar(c: char);
begin
if not TestChar(c) then
SetError(-1);
end;
function KXmlParser.TestChar(c: char): boolean;
var
ch: char;
begin
result := false;
if (mStatusOk) then
begin
ch := SkipSpaces();
if (ch = c) then
begin
inc(mActualPos);
result := true;
end
end;
end;
function KXmlParser.HasSubBody(): boolean;
var
NoSubBody: boolean;
begin
// v will be tested
// "<" Name (attribute)* "/>" there is no subbody
//
// "<" Name (attribute)* ">" (body)* "</" Name ">"
// ^ will be tested
NoSubBody := TestChar('/');
NeedChar('>');
result := mStatusOk and not NoSubBody;
end;
function KXmlParser.GenerateFromXml(Parent: KXmlNode): boolean;
var
TagName: string;
Val: string;
NewNode: KXmlNode;
StartPos, EndPos: integer;
i, MeasureNr: integer;
begin
result := false;
if not mStatusOk then
exit;
inc(mRecursionDepth);
GetTagName(TagName);
if mRecursionDepth = 1 then
begin
NewNode := Parent;
NewNode.Name := TagName;
end else
NewNode := Parent.AppendChildNode(TagName);
while IsActualPositionValid do
begin
GetAttribute();
if (NewNode <> nil) then
begin
Val := Copy(Stream, mValuePos, mValueLen);
NewNode.AppendAttr(mAttribute, Val);
end;
end;
if (HasSubBody()) then
begin
SkipSpaces();
StartPos := mActualPos;
while (StartNewBody(NewNode)) do
begin
GenerateFromXml(NewNode);
StartPos := mActualPos;
end;
if Stream[mActualPos] = '<' then
begin
EndPos := mActualPos-1;
while (EndPos >= StartPos) and
(#0 < Stream[EndPos]) and (Stream[EndPos] <= ' ') do
dec(EndPos);
if (EndPos >= StartPos) then
begin
NewNode.Value := (Copy(Stream, StartPos, mActualPos - StartPos));
if NewNode.Value <> trim(NewNode.Value) then
NewNode.Value := NewNode.Value;
if Copy(NewNode.Value, 1, 4) = '<!--' then
NewNode.Value := '';
end;
end;
if NewNode.Name = 'Staff' then
begin
MeasureNr := 1;
for i := 0 to NewNode.Count-1 do
begin
if NewNode.ChildNodes[i].Name = 'Measure' then
begin
NewNode.ChildNodes[i].AppendAttr('id', IntToStr(MeasureNr));
inc(MeasureNr);
end;
end;
end;
TestBodyEnd(TagName);
end;
dec(mRecursionDepth);
result := mStatusOk;
end;
function KXmlParser.Parse(Root: KXmlNode): boolean;
var
i: integer;
MyStream: TMyMemoryStream;
err: string;
begin
Initialize();
ParseXmlHeader();
GenerateFromXml(Root);
result := mStatusOk;
if not Result and
(0 <= mErrorPos) and (mErrorPos <= Length(Stream)) then
begin
err := 'xml-parser error pos: ';
i := 0;
while (i < 80) and (mErrorPos + i <= Length(Stream)) do
begin
err := err + Stream[mErrorPos + i];
inc(i);
end;
{$ifdef CONSOLE}
system.writeln(err);
{$else}
// Application.MessageBox(PChar(err), 'Error', MB_OK);
{$endif}
end else begin
MyStream := KXmlNode.BuildMemoryStream(Root);
{$ifdef TEST}
if MyStream <> nil then
MyStream.SaveToFile('test_.mscx');
{$endif}
MyStream.Free;
end;
end;
function KXmlParser.ParseStream_(Root: KXmlNode): boolean;
var
Utf8: AnsiString;
begin
SetLength(Utf8, Size);
Move(PByte(Memory)[0], Utf8[1], Size);
stream := UTF8ToString(utf8);
result := Parse(Root);
end;
class function KXmlParser.ParseFile(FileName: string; var Root: KXmlNode): boolean;
var
Parser: KXmlParser;
Zip_: TZipFile;
outp: TBytes;
ext: string;
i: integer;
begin
result := false;
if not FileExists(FileName) then
exit;
Root := KXmlNode.Create;
Parser := KXmlParser.Create;
try
ext := ExtractFileExt(FileName);
if (ext <> '.mscz') and (ext <> '.mxl') then
begin
Parser.LoadFromFile(FileName);
result := Parser.ParseStream_(Root);
end else begin
Zip_ := TZipFile.Create;
try
Zip_.Open(Filename, zmRead);
if ext = '.mscz' then
ext := '.mscx'
else
ext := '.xml';
for i := 0 to Length(Zip_.FileNames)-1 do
if ExtractFileExt(Zip_.FileNames[i]) = ext then
begin
FileName := Zip_.FileNames[i];
result := true;
break;
end;
if result then
Zip_.Read(FileName, Outp);
finally
Zip_.Free;
end;
if result then
result := Parser.ParseStream(Outp, Root);
SetLength(Outp, 0);
end;
finally
Parser.Free;
if not result then
FreeAndNil(Root);
end;
end;
class function KXmlParser.ParseStream(const Stream: TBytes; var Root: KXmlNode): boolean;
var
Parser: KXmlParser;
begin
result := false;
Root := KXmlNode.Create;
Parser := KXmlParser.Create;
try
Parser.Size := Length(Stream);
Move(Stream[0], PByte(Parser.Memory)[0], Length(Stream));
result := Parser.ParseStream_(Root);
finally
Parser.Free;
if not result then
FreeAndNil(Root);
end;
end;
end.