-
Notifications
You must be signed in to change notification settings - Fork 0
/
UXmlNode.pas
537 lines (464 loc) · 13.1 KB
/
UXmlNode.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
unit UXmlNode;
interface
uses
SysUtils, Classes, System.Zip,
UMyMemoryStream;
type
KXmlAttr = class
Name: string;
Value: string;
end;
KXmlNode = class
ChildNodes: array of KXmlNode;
Attrs: array of KXmlAttr;
Value: string;
Name: string; // = '': Text
destructor Destroy; override;
function AppendChildNode(Name_: string; Value_: string = ''): KXmlNode; overload;
function AppendChildNode(Name_: string; Value_: integer): KXmlNode; overload;
function AddChild(Name_: string; Value_: string = ''): KXmlNode;
procedure InsertChildNode(Index: integer; Child_: KXmlNode);
procedure AppendChildNode(Child_: KXmlNode); Overload;
function ChildNodesCount: integer;
procedure AppendAttr(Name_, Value_: string); overload;
procedure AppendAttr(Name_: string; Value_: integer); overload;
function SaveToXmlFile(const FileName: string; Header: string = ''): boolean;
function SaveToStream(const Header: string): TMyMemoryStream;
function SaveToMsczFile(FileName: string): boolean;
procedure BuildStream(Stream: TMyMemoryStream; Level: integer; Wln: boolean);
procedure RemoveChild(Child: KXmlNode);
function GetChildIndex(Child: KXmlNode): integer;
procedure PurgeChild(Index: integer);
function HasChild(Name_: string): KXmlNode;
function DeleteAttribute(Attribute: string): boolean;
function AttributeIdx(Attribute: string): integer;
function HasAttribute(Attribute: string): boolean;
function GetAttribute(const Idx: string): string;
procedure SetAttributes(const Idx: string; const Value: string);
function LastNode: KXmlNode;
function GetXmlValue: AnsiString;
function GetChild(Name: string; var Child: KXmlNode): boolean;
//MuseScore
procedure MergeStaff(var Staff3: KXmlNode);
function ExtractVoice(VoiceIndex: integer; StaffId: integer): KXmlNode;
class function BuildMemoryStream(Root: KXmlNode): TMyMemoryStream;
property Attributes[const Name: string]: string read GetAttribute write SetAttributes;
property Count: integer read ChildNodesCount;
property XmlValue: AnsiString read GetXmlValue;
end;
const
// Unicode
// https://de.wikipedia.org/wiki/Unicodeblock_Notenschriftzeichen
dotRest = 119149;
doubleWholeRest = 119098;
wholeRest = 119099;
halfRest = 119100;
quarterRest = 119101;
wholeNoteHead = 119133;
halfNoteHead = 119127;
quarterNoteHead = 119128;
function NewXmlAttr(Name_: string; Value_: string = ''): KXmlAttr;
function NewXmlNode(Name_: string; Value_: string = ''): KXmlNode;
implementation
function NewXmlAttr(Name_: string; Value_: string = ''): KXmlAttr;
begin
result := KXmlAttr.Create;
result.Name := Name_;
result.Value := Value_;
end;
destructor KXmlNode.Destroy;
var
i: integer;
begin
for i := 0 to Length(ChildNodes)-1 do
ChildNodes[i].Free;
Value := '';
Name := '';
inherited;
end;
function NewXmlNode(Name_: string; Value_: string = ''): KXmlNode;
begin
result := KXmlNode.Create;
SetLength(result.ChildNodes, 0);
SetLength(result.Attrs, 0);
result.Value := Value_;
result.Name := Name_;
end;
procedure KXmlNode.InsertChildNode(Index: integer; Child_: KXmlNode);
var
i: integer;
begin
if (Index >= 0) and (Index <= Count) then
begin
SetLength(ChildNodes, Length(ChildNodes)+1);
for i := Length(ChildNodes)-2 downto Index do
ChildNodes[i+1] := ChildNodes[i];
ChildNodes[Index] := Child_;
end;
end;
procedure KXmlNode.AppendChildNode(Child_: KXmlNode);
begin
InsertChildNode(Count, Child_);
end;
function KXmlNode.ChildNodesCount: integer;
begin
result := Length(ChildNodes);
end;
function KXmlNode.AppendChildNode(Name_: string; Value_: string = ''): KXmlNode;
begin
result := NewXmlNode(Name_, Value_);
SetLength(ChildNodes, Length(ChildNodes) + 1);
ChildNodes[Length(ChildNodes)-1] :=result;
end;
function KXmlNode.AppendChildNode(Name_: string; Value_: integer): KXmlNode;
begin
result := AppendChildNode(Name_, IntToStr(Value_));
end;
function KXmlNode.AddChild(Name_: string; Value_: string = ''): KXmlNode;
begin
result := AppendChildNode(Name_, Value_);
end;
procedure KXmlNode.AppendAttr(Name_, Value_: string);
var
Attr_: KXmlAttr;
begin
Attr_ := NewXmlAttr(Name_, Value_);
SetLength(Attrs, Length(Attrs)+1);
Attrs[Length(Attrs)-1] := Attr_;
end;
procedure KXmlNode.AppendAttr(Name_: string; Value_: integer);
begin
AppendAttr(Name_, IntToStr(Value_));
end;
function KXmlNode.GetChildIndex(Child: KXmlNode): integer;
begin
if Child = nil then
begin
result := -1;
exit;
end;
result := Length(ChildNodes)-1;
while (result >= 0) and (ChildNodes[result] <> Child) do
dec(result);
end;
procedure KXmlNode.RemoveChild(Child: KXmlNode);
begin
PurgeChild(GetChildIndex(Child));
end;
procedure KXmlNode.PurgeChild(Index: integer);
var
i: integer;
begin
if (0 <= Index) and (Index < Count) then
begin
ChildNodes[Index].Free;
for i := Index+1 to Count-1 do
ChildNodes[i-1] := ChildNodes[i];
SetLength(ChildNodes, Count-1);
end;
end;
function KXmlNode.HasChild(Name_: string): KXmlNode;
var
i: integer;
begin
result := nil;
for i := 0 to Count-1 do
begin
if ChildNodes[i].Name = Name_ then
begin
result := ChildNodes[i];
break;
end;
end;
end;
function KXmlNode.AttributeIdx(Attribute: string): integer;
begin
result := Length(Attrs)-1;
while (result >= 0) and (Attrs[result].Name <> Attribute) do
dec(result);
end;
function KXmlNode.HasAttribute(Attribute: string): boolean;
begin
result := AttributeIdx(Attribute) >= 0;
end;
function KXmlNode.GetAttribute(const Idx: string): string;
var
i: integer;
begin
result := '';
i := AttributeIdx(Idx);
if i >= 0 then
result := Attrs[i].Value;
end;
function KXmlNode.DeleteAttribute(Attribute: string): boolean;
var
i: integer;
begin
result := false;
i := 0;
while (i < Length(Attrs)) and not result do
if Attrs[i].Name = Attribute then
begin
Attrs[i].Free;
result := true;
while i < Length(Attrs)-1 do
begin
Attrs[i] := Attrs[i+1];
inc(i);
end;
SetLength(Attrs, Length(Attrs)-1);
end else
inc(i);
end;
procedure KXmlNode.SetAttributes(const Idx: string; const Value: string);
var
i: integer;
begin
i := AttributeIdx(Idx);
if i >= 0 then
Attrs[i].Value := Value
else
AppendAttr(Idx, Value);
end;
function KXmlNode.LastNode: KXmlNode;
begin
result := nil;
if High(ChildNodes) >= 0 then
result := ChildNodes[High(ChildNodes)];
end;
procedure KXmlNode.BuildStream(Stream: TMyMemoryStream; Level: integer; Wln: boolean);
var
i: integer;
function Special: boolean;
begin
result := (name <> 'text') and (name <> 'appoggiatura');
end;
begin
if Wln then
for i := 0 to Level-1 do
Stream.WriteString(' ');
Stream.WriteString('<');
if Name = '' then
begin
Stream.WriteString('!-- ' + Value + ' -->');
if Wln then
Stream.Writeln;
end else begin
Stream.WriteString(Name);
for i := 0 to Length(Attrs)-1 do
begin
Stream.WriteString(' ');
Stream.WriteString(Attrs[i].Name);
Stream.WriteString('="');
Stream.WriteString(Attrs[i].Value);
Stream.WriteString('"');
end;
if (Length(ChildNodes) > 0) or (Value <> '') {or
((Length(Attrs) = 0) and (Name <> 'startRepeat'))} then
begin
Stream.WriteString('>');
if Wln and (Value = '') and Special then
Stream.Writeln;
for i := 0 to Length(ChildNodes)-1 do
begin
ChildNodes[i].BuildStream(Stream, Level+1, Wln and Special);
end;
if (Value <> '') then
Stream.WriteString(Value)
else
if Wln and Special then
for i := 0 to Level do
Stream.WriteString(' ');
Stream.WriteString('</');
Stream.WriteString(Name);
end else
Stream.WriteString('/');
Stream.WriteString('>');
if Wln then
Stream.Writeln;
end;
end;
class function KXmlNode.BuildMemoryStream(Root: KXmlNode): TMyMemoryStream;
var
Stream: TMyMemoryStream;
begin
result := TMyMemoryStream.Create;
Stream := result;
Stream.Size := 10000000;
while (Root.Name = '') and (Root.Count > 0) do
Root := Root.ChildNodes[0];
Root.BuildStream(Stream, 0, true);
Stream.Size := Stream.Position;
end;
function KXmlNode.SaveToXmlFile(const FileName: string; Header: string): boolean;
var
Stream: TMyMemoryStream;
begin
result := false;
Stream := SaveToStream(Header);
if Stream <> nil then
begin
Stream.SaveToFile(FileName);
Stream.Free;
result := true;
end;
end;
function KXmlNode.SaveToStream(const Header: string): TMyMemoryStream;
var
i, l: integer;
begin
result := BuildMemoryStream(self);
if result = nil then
exit;
l := Length(Header);
if l > 0 then
begin
result.Size := result.Size + l;
for i := result.Size - 1 downto 0 do
PAnsiChar(result.Memory)[i+l] := PAnsiChar(result.Memory)[i];
for i := 1 to l do
PAnsiChar(result.Memory)[i-1] := AnsiChar(Header[i]);
end;
end;
{
<?xml version="1.0" encoding="UTF-8"?>
<container>
<rootfiles>
<rootfile full-path="%s.mscx">
</rootfile>
</rootfiles>
</container>
}
function KXmlNode.SaveToMsczFile(FileName: string): boolean;
var
container, child: KXmlNode;
Stream: TMyMemoryStream;
conStr: TMyMemoryStream;
Zip: TZipFile;
begin
result := false;
Stream := BuildMemoryStream(self);
if Stream <> nil then
begin
SetLength(FileName, Length(FileName)-Length(ExtractFileExt(FileName)));
container := KXmlNode.Create;
container.Name := 'container';
child := container.AppendChildNode('rootfiles');
child := child.AppendChildNode('rootfile');
child.AppendAttr('full-path', ExtractFileName(FileName) + '.mscx');
conStr := container.SaveToStream('<?xml version="1.0" encoding="UTF-8"?>'#13#10);
Zip := TZipFile.Create;
Zip.Open(FileName + '.mscz', zmWrite);
Zip.Add(Stream.MakeBytes, ExtractFileName(FileName) + '.mscx');
Zip.Add(conStr.MakeBytes, 'META-INF/container.xml');
Zip.Free;
Stream.Free;
conStr.Free;
result := true;
end;
end;
function KXmlNode.GetXmlValue: AnsiString;
var
s: string;
procedure Change(from, to_: string);
var
p: integer;
begin
repeat
p := Pos(from, s);
if p > 0 then
begin
Delete(s, p, Length(from));
Insert(to_, s, p);
end;
until p = 0;
end;
begin
s := Value;
Change('&', '&');
Change('<', '<');
Change('>', '>');
result := UTF8encode(s);
end;
function KXmlNode.GetChild(Name: string; var Child: KXmlNode): boolean;
var
k: integer;
begin
k := 0;
result := false;
Child := nil;
while not result and (k < Count) do
begin
Child := ChildNodes[k];
inc(k);
result := Child.Name = Name;
end;
end;
///////////////////////////// MuseScore ////////////////////////////////////////
procedure KXmlNode.MergeStaff(var Staff3: KXmlNode);
var
mea1, mea3, p: integer;
Child: KXmlNode;
begin
if (Name <> 'Staff') or (Staff3.Name <> 'Staff') then
exit;
mea1 := -1;
mea3 := -1;
while (mea1 < Count) and (mea3 < Staff3.Count) do
begin
inc(mea1);
while (mea1 < Count) and (ChildNodes[mea1].Name <> 'Measure') do
inc(mea1);
inc(mea3);
while (mea3 < Staff3.Count) and (Staff3.ChildNodes[mea3].Name <> 'Measure') do
inc(mea3);
if (mea1 < Count) and (mea3 < Staff3.Count) then
begin
Child := Staff3.ChildNodes[mea3]; // measure
// startRepeat und endRepeat überspringen
p := Child.Count-1;
while (p > 0) and (Child.ChildNodes[p].Name <> 'voice') do
dec(p);
ChildNodes[mea1].AppendChildNode(Child.ChildNodes[p]);
Child.ChildNodes[p] := nil;
end;
end;
end;
function KXmlNode.ExtractVoice(VoiceIndex: integer; StaffId: integer): KXmlNode;
var
i, iMeasure: integer;
j, iVoice: integer;
Voice: KXmlNode;
Mea: KXmlNode;
Ok: boolean;
begin
Ok := false;
result := NewXmlNode('Staff');
result.AppendAttr('id', IntToStr(StaffId));
iMeasure := 1;
for i := 0 to Count-1 do
if ChildNodes[i].Name = 'Measure' then
begin
result.AppendChildNode('', 'Measure ' + IntToStr(iMeasure));
mea := result.AppendChildNode('Measure');
inc(iMeasure);
iVoice := -1;
for j := 0 to ChildNodes[i].Count-1 do
begin
Voice := ChildNodes[i].ChildNodes[j];
if Voice.Name = 'voice' then
inc(iVoice);
if iVoice = VoiceIndex then
begin
Mea.AppendChildNode(Voice);
ChildNodes[i].ChildNodes[j] := nil;
ChildNodes[i].PurgeChild(j);
Ok := true;
break;
end;
end;
end;
if not Ok then
FreeAndNil(result);
end;
end.