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
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Xml.Writer;
{$SCOPEDENUMS ON}
interface
uses
Character,
Character.Encoding,
Collection.Stack,
Exception,
Integer._32,
Stream.Output,
Text,
Uri,
Xml.CharacterReference,
Xml.Namespace;
type
TXmlWriterState = class(TObject)
protected
Stream: TOutputStream;
Encoding: TCharacterEncoding;
procedure Write(const Value: TText);
procedure WriteCharacter(Character: TCharacterCodePoint);
public
constructor Create(Stream: TOutputStream; Encoding: TCharacterEncoding); reintroduce; virtual;
procedure CharacterData(const Value: TText); virtual; abstract;
end;
TXmlElementContentType = (CharacterData, Element);
TXmlElementContentTypes = set of TXmlElementContentType;
TXmlWriterElementState = class(TXmlWriterState)
protected
FTag: TText;
public
ContentTypes: TXmlElementContentTypes;
constructor Create(Stream: TOutputStream; Encoding: TCharacterEncoding; const Tag: TText); reintroduce; virtual;
procedure CharacterData(const Value: TText); override;
property Tag: TText read FTag;
end;
TXmlWriterAttributeState = class(TXmlWriterState)
public
procedure CharacterData(const Value: TText); override;
end;
TXmlEndTag = (Optional, Always);
TXmlWriter = class(TObject)
protected
Stream: TOutputStream;
States: TStack<TXmlWriterState>;
FEncoding: TCharacterEncoding;
procedure WriteCharacter(Character: TCharacterCodePoint);
procedure Write(const Value: TText);
procedure WriteBeginOfLine();
procedure WriteLine(Line: TText);
public
IndentSize: TInteger32;
EndOfLine: TText;
constructor Create(Stream: TOutputStream; Encoding: TCharacterEncoding);
destructor Destroy(); override;
procedure WriteHeader();
procedure StartElement(const Name: TText); overload;
procedure StartElement(Namespace: TXmlNamespace; const Name: TText); overload;
procedure WriteElement(const Name: TText); overload;
procedure WriteElement(const Name: TText; const CharacterData: TText); overload;
procedure WriteElement(Namespace: TXmlNamespace; const Name: TText); overload;
procedure EndElement(EndTag: TXmlEndTag = TXmlEndTag.Optional);
procedure StartAttribute(const Name: TText); overload;
procedure StartAttribute(const Namespace: TText; const Name: TText); overload;
procedure WriteAttribute(const Name: TText; const Value: TText);
procedure EndAttribute();
procedure CharacterData(const Value: TText);
procedure WriteCharacterEntity(Code: TInteger32);
procedure WriteCharacterReference(Reference: TCharacterReference);
procedure WriteCdata(const Value: TText);
procedure WriteComment(const Comment: TText);
procedure StartDtd(Name: TText; PublicId: TText; SystemId: TUri);
procedure EndDtd();
property Encoding: TCharacterEncoding read FEncoding;
end;
EXmlWriterInvalidState = class(EException)
public
constructor Create(State: TXmlWriterState); reintroduce; virtual;
end;
implementation
uses
Character.Set_.Unicode,
Collection.Stack.List,
Integer._32.TextFormat,
Object_.Reference.Owner,
Stream.Input;
{ TXmlWriter }
procedure TXmlWriter.WriteAttribute(const Name: TText; const Value: TText);
begin
StartAttribute(Name);
CharacterData(Value);
EndAttribute();
end;
procedure TXmlWriter.WriteBeginOfLine();
var
i: TInteger32;
begin
for i := 1 to States.Count * IndentSize do
WriteCharacter(TUnicode.SPACE);
end;
constructor TXmlWriter.Create(Stream: TOutputStream; Encoding: TCharacterEncoding);
begin
inherited Create();
States := TListStack<TXmlWriterState>.Create();
States.ElementManager := TReferenceOwner<TXmlWriterState>.Create();
Self.Stream := Stream;
FEncoding := Encoding;
end;
destructor TXmlWriter.Destroy();
begin
States.Free();
inherited Destroy();
end;
procedure TXmlWriter.EndAttribute();
begin
WriteCharacter(TUnicode.QUOTATION_MARK);
States.Pop();
end;
procedure TXmlWriter.EndDtd();
begin
WriteCharacter(TUnicode.GREATER_THAN_SIGN);
Write(EndOfLine);
end;
procedure TXmlWriter.StartAttribute(const Name: TText);
begin
WriteCharacter(TUnicode.SPACE);
Write(Name);
WriteCharacter(TUnicode.EQUALS_SIGN);
WriteCharacter(TUnicode.QUOTATION_MARK);
States.Add(TXmlWriterAttributeState.Create(Stream, Encoding));
end;
procedure TXmlWriter.StartAttribute(const Namespace: TText; const Name: TText);
begin
StartAttribute(Namespace + ':' + Name);
end;
procedure TXmlWriter.StartDtd(Name: TText; PublicId: TText; SystemId: TUri);
begin
Write('<!DOCTYPE ');
Write(Name);
if not SystemId.IsEmpty() then
begin
if PublicId.Count > 0 then
begin
Write(' PUBLIC ');
WriteCharacter(TUnicode.QUOTATION_MARK);
States.Add(TXmlWriterAttributeState.Create(Stream, Encoding));
CharacterData(PublicId);
States.Pop();
WriteCharacter(TUnicode.QUOTATION_MARK);
WriteCharacter(TUnicode.SPACE);
end
else
Write(' SYSTEM ');
WriteCharacter(TUnicode.QUOTATION_MARK);
States.Add(TXmlWriterAttributeState.Create(Stream, Encoding));
CharacterData(SystemId.ToText());
States.Pop();
WriteCharacter(TUnicode.QUOTATION_MARK);
end;
end;
procedure TXmlWriter.StartElement(const Name: TText);
var
ElementState: TXmlWriterElementState;
begin
if (States.Count > 0) and (States.Top is TXmlWriterElementState) then
begin
ElementState := States.Top as TXmlWriterElementState;
if ElementState.ContentTypes = [] then
begin
WriteCharacter(TUnicode.GREATER_THAN_SIGN);
Write(EndOfLine);
end;
ElementState.ContentTypes := ElementState.ContentTypes + [TXmlElementContentType.Element];
end;
WriteBeginOfLine();
WriteCharacter(TUnicode.LESS_THAN_SIGN);
Write(Name);
States.Add(TXmlWriterElementState.Create(Stream, Encoding, Name));
end;
procedure TXmlWriter.WriteLine(Line: TText);
begin
WriteBeginOfLine();
Write(Line);
Write(EndOfLine);
end;
procedure TXmlWriter.Write(const Value: TText);
var
Input: TInputStream;
begin
if Value.Count > 0 then
begin
if Value.Encoding = Encoding then
Value.Write(Stream)
else
begin
Input := Value.CreateInputStream();
while not Input.AtEnd() do
Encoding.Encode(Stream, Value.Encoding.Decode(Input));
Input.Free();
end;
end;
end;
procedure TXmlWriter.WriteCdata(const Value: TText);
var
ElementState: TXmlWriterElementState;
begin
if (States.Count > 0) and (States.Top is TXmlWriterElementState) then
begin
ElementState := States.Top as TXmlWriterElementState;
WriteCharacter(TUnicode.GREATER_THAN_SIGN);
Write(EndOfLine);
ElementState.ContentTypes := ElementState.ContentTypes + [TXmlElementContentType.CharacterData];
end;
// TODO: We have to properly escape Value for embedded occurrences of ]]>
Write('<![CDATA[');
Write(Value);
Write(']]>');
end;
procedure TXmlWriter.CharacterData(const Value: TText);
var
ElementState: TXmlWriterElementState;
begin
if (States.Count > 0) and (States.Top is TXmlWriterElementState) then
begin
ElementState := States.Top as TXmlWriterElementState;
if ElementState.ContentTypes = [] then
WriteCharacter(TUnicode.GREATER_THAN_SIGN);
ElementState.ContentTypes := ElementState.ContentTypes + [TXmlElementContentType.CharacterData];
end;
if States.Count > 0 then
States.Top.CharacterData(Value)
else
begin
States.Add(TXmlWriterElementState.Create(Stream, Encoding, 'dummy'));
States.Top.CharacterData(Value);
States.Pop();
end;
end;
procedure TXmlWriter.WriteCharacter(Character: TCharacterCodePoint);
begin
Encoding.Encode(Stream, Character);
end;
procedure TXmlWriter.WriteCharacterEntity(Code: TInteger32);
begin
Write('&#');
Write(Integer32ToText(Code));
WriteCharacter(TUnicode.SEMICOLON);
end;
procedure TXmlWriter.WriteCharacterReference(Reference: TCharacterReference);
begin
WriteCharacter(TUnicode.AMPERSAND);
Write(CharacterReferenceToText(Reference));
WriteCharacter(TUnicode.SEMICOLON);
end;
procedure TXmlWriter.WriteComment(const Comment: TText);
var
ElementState: TXmlWriterElementState;
begin
if (States.Count > 0) and (States.Top is TXmlWriterElementState) then
begin
ElementState := States.Top as TXmlWriterElementState;
if ElementState.ContentTypes = [] then
begin
WriteCharacter(TUnicode.GREATER_THAN_SIGN);
Write(EndOfLine);
ElementState.ContentTypes := ElementState.ContentTypes + [TXmlElementContentType.CharacterData];
end;
end;
WriteBeginOfLine();
Write('<!--');
Write(Comment); // TODO: We have to properly escape the comment contents
Write('-->');
Write(EndOfLine);
end;
procedure TXmlWriter.WriteElement(const Name: TText);
begin
StartElement(Name);
EndElement();
end;
procedure TXmlWriter.WriteElement(Namespace: TXmlNamespace; const Name: TText);
begin
StartElement(Namespace, Name);
EndElement();
end;
procedure TXmlWriter.EndElement(EndTag: TXmlEndTag = TXmlEndTag.Optional);
var
State: TXmlWriterElementState;
begin
if States.Top is TXmlWriterElementState then
State := States.ExtractTop() as TXmlWriterElementState
else
raise EXmlWriterInvalidState.Create(States.Top);
if State.ContentTypes = [] then
begin
case EndTag of
TXmlEndTag.Optional:
begin
WriteCharacter(TUnicode.SOLIDUS);
WriteCharacter(TUnicode.GREATER_THAN_SIGN);
end;
TXmlEndTag.Always:
begin
WriteCharacter(TUnicode.GREATER_THAN_SIGN);
WriteCharacter(TUnicode.LESS_THAN_SIGN);
WriteCharacter(TUnicode.SOLIDUS);
Write(State.Tag);
WriteCharacter(TUnicode.GREATER_THAN_SIGN);
end;
else
raise EException.Create('Unknown end tag');
end;
end
else
begin
if State.ContentTypes <> [TXmlElementContentType.CharacterData] then
WriteBeginOfLine();
WriteCharacter(TUnicode.LESS_THAN_SIGN);
WriteCharacter(TUnicode.SOLIDUS);
Write(State.Tag);
WriteCharacter(TUnicode.GREATER_THAN_SIGN);
end;
Write(EndOfLine);
State.Free();
end;
procedure TXmlWriter.WriteHeader();
begin
WriteLine('<?xml version="1.0" encoding="' + Encoding.Identifier + '"?>');
end;
procedure TXmlWriter.StartElement(Namespace: TXmlNamespace; const Name: TText);
begin
StartElement(Namespace.Prefix + ':' + Name);
end;
procedure TXmlWriter.WriteElement(const Name: TText; const CharacterData: TText);
begin
StartElement(Name);
Self.CharacterData(CharacterData);
EndElement();
end;
{ TXmlWriterElementState }
procedure TXmlWriterElementState.CharacterData(const Value: TText);
var
Character: TCharacterCodePoint;
begin
for Character in Value do
begin
case Character of
TUnicode.LESS_THAN_SIGN:
Write('<');
TUnicode.GREATER_THAN_SIGN:
Write('>');
TUnicode.AMPERSAND:
Write('&');
else
WriteCharacter(Character);
end;
end;
end;
constructor TXmlWriterElementState.Create(Stream: TOutputStream; Encoding: TCharacterEncoding; const Tag: TText);
begin
inherited Create(Stream, Encoding);
FTag := Tag;
end;
{ TXmlWriterState }
constructor TXmlWriterState.Create(Stream: TOutputStream; Encoding: TCharacterEncoding);
begin
inherited Create();
Self.Stream := Stream;
Self.Encoding := Encoding;
end;
procedure TXmlWriterState.Write(const Value: TText);
var
Input: TInputStream;
begin
if Value.Count > 0 then
begin
if Value.Encoding = Encoding then
Value.Write(Stream)
else
begin
Input := Value.CreateInputStream();
while not Input.AtEnd() do
Encoding.Encode(Stream, Value.Encoding.Decode(Input));
Input.Free();
end;
end;
end;
procedure TXmlWriterState.WriteCharacter(Character: TCharacterCodePoint);
begin
Encoding.Encode(Stream, Character);
end;
{ EXmlWriterInvalidState }
constructor EXmlWriterInvalidState.Create(State: TXmlWriterState);
begin
inherited Create('Invalid xml writer state: ' + State.ClassName);
end;
{ TXmlWriterAttributeState }
procedure TXmlWriterAttributeState.CharacterData(const Value: TText);
var
Character: TCharacterCodePoint;
begin
for Character in Value do
begin
case Character of
TUnicode.LESS_THAN_SIGN:
Write('<');
TUnicode.GREATER_THAN_SIGN:
Write('>');
TUnicode.AMPERSAND:
Write('&');
TUnicode.QUOTATION_MARK:
Write('"');
else
WriteCharacter(Character);
end;
end;
end;
end.
|