/
XMLReader.ceylon
1673 lines (1545 loc) · 58.4 KB
/
XMLReader.ceylon
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import ceylon.buffer.charset {
utf8,
Charset,
utf16
}
import ceylon.collection {
LinkedList,
Queue,
Stack,
ArrayList,
HashMap,
MutableMap,
MapMutator,
MutableList
}
"Error returned to signal a parsing error."
shared class ParseError(shared String msg){}
class ParseException(shared String msg) extends Exception(msg){}
class State of beforeProlog
| prologLt
| prologExclam
| prologQuest
| noXmlDecl
| noXmlDeclLt
| prologPI
| rootEl
| element
| content
| contentExclam
| contentPI
| elementTag
| endElementTag
| afterRoot
| finished
{
String s;
shared new beforeProlog{s="beforeProlog";}
shared new prologLt{s="prologLt";}
shared new prologExclam{s="prologExclam";}
shared new prologQuest{s="prologQuest";}
shared new noXmlDecl{s="noXmlDecl";}
shared new noXmlDeclLt{s="noXmlDeclLt";}
shared new prologPI{s="prologPI";}
shared new rootEl{s="rootEl";}
shared new element{s="element";}
shared new content{s="content";}
shared new contentExclam{s="contentComment";}
shared new contentPI{s="contentPI";}
shared new elementTag{s="elementTag";}
shared new endElementTag{s="endElementTag";}
shared new afterRoot{s="afterRoot";}
shared new finished{s="finished";}
shared actual String string => s;
}
shared String defaultXmlVersion = "1.0";
shared String defaultXmlEncoding = "UTF-8";
shared class ExpandedName(localName, namespaceName=null, prefix=null)
{
shared String localName;
shared String? namespaceName;
shared String? prefix;
shared actual Boolean equals(Object that)
{
if (!is ExpandedName that) {
return false;
}
if (that.localName != localName) {
return false;
}
if (exists namespaceName) {
return if (exists it = that.namespaceName) then it == namespaceName else false;
}
return that.namespaceName is Null;
}
shared actual Integer hash
{
return 31 * localName.hash + (if (exists it = namespaceName) then it.hash else 0);
}
shared actual String string
{
value sb = StringBuilder();
sb.appendCharacter('"');
if (exists it = prefix, !it.empty) {
sb.append(it).appendCharacter(':');
}
sb.append(localName);
if (exists it = namespaceName) {
sb.appendCharacter('{').append(it).appendCharacter('}');
}
sb.appendCharacter('"');
return sb.string;
}
}
class ElementInfo(name, namespaceDeclarations)
{
shared ExpandedName name;
shared Map<String, String>? namespaceDeclarations;
}
"""
Pull-parser for reading XML, offering XMLEvents to the calling application.
This is a(n incomplete) non-validating XML processor according to the W3C's [Extensible Markup Language (XML) 1.0 (Fifth Edition)]
(http://www.w3.org/TR/2008/REC-xml-20081126/). The major restriction is the missing support for DOCTYPE, so
entities (other than the document entity) cannot be declared.
The parser is optionally namespace-aware, supporting the W3C's [Namespaces in XML 1.0 (Third Edition)](http://www.w3.org/TR/2009/REC-xml-names-20091208/).
During initialization with an input Sequential<Byte>, the XMLEventReader tries to heuristically detect the input's character encoding. This detection
uses the encoding="..." pseudo-attribute of the XML declaration at the beginning of the document, if present. It
can be overridden using the forcedEncoding parameter, which is useful if some external protocol (like MIME) specifies the encoding of the XML document.
The encoding that the XMLEventReader uses can be queried via the `encoding` property.
After initialization, the `XMLEvent`s can be retrieved via the `Iterator` mechanism, which may return a `ParseError` in case of well-formedness error.
"""
by("Dirk Lattermann")
throws(`class ParseException`)
shared class XMLEventReader(namespaceAware, input, forcedEncoding = null)
satisfies Iterator<XMLEvent|ParseError>
{
"Controls if the parser shall be namespace aware."
shared Boolean namespaceAware;
"The bytes that will be parsed into XMLEvents."
{Byte*} input;
"If non-null, the encoding that will be used to convert the [[input]] bytes to characters.
If null, the encoding will be auto-detected from the input."
Charset? forcedEncoding;
throws(`class ParseException`)
[Charset, Iterator<Byte>] guessCharset(Iterator<Byte> headIterator)
{
value b0 = headIterator.next();
if (is Finished b0) {
throw AssertionError("input too short to be an XML document");
}
value b1 = headIterator.next();
if (is Finished b1) {
throw AssertionError("input too short to be an XML document");
}
value b2 = headIterator.next();
if (is Finished b2) {
throw AssertionError("input too short to be an XML document");
}
value b3 = headIterator.next();
if (is Finished b3) {
throw AssertionError("input too short to be an XML document");
}
Byte[4] head = [b0, b1, b2, b3];
AdHocEncoding guessedEncoding = guessEncoding(head);
Charset charset;
Iterator<Byte> newInput;
switch (guessedEncoding)
case (AdHocEncoding.utf8WithoutBOM) {
charset = utf8;
newInput = ChainingIterator({head.iterator(), headIterator});
}
case (AdHocEncoding.utf8WithBOM) {
charset = utf8;
newInput = headIterator;
}
case (AdHocEncoding.other) {
charset = utf8;
newInput = ChainingIterator({head.iterator(), headIterator});
}
case (AdHocEncoding.utf16beWithBOM) {
charset = utf16;
newInput = ChainingIterator({head.iterator(), headIterator});
}
case (AdHocEncoding.utf16leWithBOM) {
charset = utf16;
newInput = ChainingIterator({head.iterator(), headIterator});
}
return [charset, newInput];
}
Charset charset;
Iterator<Byte> newIterator;
if (exists forcedEncoding) {
charset = forcedEncoding;
newIterator = input.iterator();
}
else {
value guessResult = guessCharset(input.iterator());
charset = guessResult[0];
newIterator = guessResult[1];
}
"The character encoding used to read the input. Either auto-detected or taken from [[forcedEncoding]]."
shared String encoding = charset.name;
value source = PositionPushbackSource(charset.decode(input).iterator());
"The current reading position in the input, in characters, starting at 0."
shared Integer offset => source.offset;
"The line number of the current reading position, starting at 1."
shared Integer line => source.line;
"The column number in the current line, starting at 1."
shared Integer column => source.column;
MutableMap<String, String> internalEntities = HashMap<String, String>();
internalEntities.putAll(predefinedEntities);
internalEntities.put("ga", "aha<joi>mama</joi> ");
Stack<PositionPushbackSource> entityRefSource = LinkedList<PositionPushbackSource>();
variable State state = State.beforeProlog;
Queue<XMLEvent> parsedEvents = LinkedList<XMLEvent>();
NamespaceContextImpl namespaceContext = NamespaceContextImpl();
Stack<ElementInfo> elementPath = LinkedList<ElementInfo>();
Character|Finished nextChar()
{
while (exists s = entityRefSource.top) {
value c = s.nextChar();
if (is Finished c) {
entityRefSource.pop();
}
else {
return c;
}
}
return source.nextChar();
}
void pushbackChar(Character character)
{
if (exists s = entityRefSource.top) {
s.pushbackChar(character);
}
else {
source.pushbackChar(character);
}
}
"Reads the next portion of the input to return the next XMLEvent or ParseError. Finished after the end of the document."
shared actual XMLEvent|ParseError|Finished next()
{
try {
return internalNext();
}
catch (ParseException e) {
return ParseError(e.msg);
}
}
throws(`class ParseException`)
XMLEvent|Finished internalNext()
{
if (exists event = parsedEvents.accept()) {
return event;
}
while (true) {
switch (state)
case (State.beforeProlog) {
if (!is Finished c0 = source.nextChar()) {
Character c1;
if (isXmlWhitespace(c0)) {
source.pushbackChar(c0);
state = State.noXmlDecl;
return StartDocument.absent;
}
else {
c1 = c0;
}
if (c1 == '<') {
state = State.prologLt;
}
else {
throw ParseException("text content before root element or wrong encoding (detected ``charset``)");
}
}
else {
throw ParseException("no root element");
}
}
case (State.prologLt) {
if (!is Finished c = source.nextChar()) {
if (c == '!') {
state = State.prologExclam;
return StartDocument.absent;
}
else if (c == '?') {
state = State.prologQuest;
}
else if (isNameStartChar(c)) {
source.pushbackChar(c);
state = State.rootEl;
return StartDocument.absent;
}
else {
throw ParseException("invalid name start character");
}
}
else {
throw ParseException("EOF while reading prolog");
}
}
case (State.noXmlDecl) {
if (!is Finished c = source.nextChar()) {
Character c1;
String? ws;
if (isXmlWhitespace(c)) {
value res = gatherWhitespace(c);
ws = res[0];
value cc = res[1];
if (is Finished cc) {
throw ParseException("EOF after tag opening");
}
c1 = cc;
}
else {
ws = null;
c1 = c;
}
if (c1 == '<') {
state = State.noXmlDeclLt;
if (exists ws) {
return Characters(ws, true, true);
}
}
else {
throw ParseException("text content before root element");
}
}
else {
throw ParseException("no root element");
}
}
case (State.noXmlDeclLt) {
if (!is Finished c = source.nextChar()) {
if (c == '!') {
state = State.prologExclam;
}
else if (c == '?') {
state = State.prologPI;
}
else {
source.pushbackChar(c);
state = State.rootEl;
}
}
else {
throw ParseException("no root element");
}
}
case (State.prologExclam) {
if (!is Finished c = source.nextChar()) {
if (c == '-') {
value commentResult = readComment("-");
state = State.noXmlDecl;
return Comment(commentResult);
}
else if (c == 'D') {
throw ParseException("DOCTYPE not supported yet");
}
else {
throw ParseException("invalid tag beginning <!");
}
}
else {
throw ParseException("EOF while reading comment/doctype in prolog");
}
}
case (State.prologQuest) {
switch (it = checkWithPushbackOnFalse("xml"))
case (finished) {
throw ParseException("EOF in XML declaration");
}
case (false) {
state = State.prologPI;
}
case (true) {
value res = gatherWhitespace();
value c = res[1];
if (is Finished c) {
throw ParseException("EOF in XML declaration");
}
if (res[0].empty) {
pushbackChar(c);
pushbackChar('l');
pushbackChar('m');
pushbackChar('x');
}
else {
pushbackChar(c);
value startDocument = readXmlDeclaration();
state = State.noXmlDecl;
return startDocument;
}
}
}
case (State.prologPI) {
value pi = readProcessingInstruction();
state = State.noXmlDecl;
return ProcessingInstruction(pi[0], pi[1]);
}
case (State.rootEl) {
state = State.element;
}
case (State.element) {
value nameResult = gatherName();
value c0 = nameResult[1];
value rr = gatherAttributes(c0);
Character c1 = rr[1];
if (c1 == '/') {
switch (it = check(">"))
case (finished) {
throw ParseException("EOF in close empty tag");
}
case (false) {
throw ParseException("invalid empty tag close");
}
case (true) {
if (is Null parentEl = elementPath.top) {
state = State.afterRoot;
}
else {
state = State.content;
}
if (namespaceAware) {
value nsBindingDeclsAndAttribs = determineNsBindingDeclsAndAttribs(rr[0]);
value declarations = nsBindingDeclsAndAttribs[0];
declarations.each((binding) => namespaceContext.push(binding));
value expandedName = expandName(nameResult[0], true);
value attribsMap = map(nsBindingDeclsAndAttribs[1].map((e)=>expandName(e.key, false) -> e.item));
parsedEvents.offer(EndElement(expandedName, true));
value startElement = StartElement(expandedName, true, attribsMap, nsBindingDeclsAndAttribs[0]);
// FIXME after the StartElement event, but before the immediately following EndElement, the XMLReader's namespaceContext won't reflect the namespace declarations in this empty element tag
declarations.each((binding) => namespaceContext.pop(binding.key));
return startElement;
}
else {
value name = ExpandedName(nameResult[0]);
value attribsMap = map(rr[0].map((attrib) => ExpandedName(attrib.key) -> attrib.item));
parsedEvents.offer(EndElement(name, true));
return StartElement(name, true, attribsMap);
}
}
}
else if (c1 == '>') {
state = State.content;
if (namespaceAware) {
value nsBindingDeclsAndAttribs = determineNsBindingDeclsAndAttribs(rr[0]);
value declarations = nsBindingDeclsAndAttribs[0];
declarations.each((binding) => namespaceContext.push(binding));
value expandedName = expandName(nameResult[0], true);
value attribsMap = map(nsBindingDeclsAndAttribs[1].map((e)=>expandName(e.key, false) -> e.item));
elementPath.push(ElementInfo(expandedName, declarations));
return StartElement(expandedName, false, attribsMap, nsBindingDeclsAndAttribs[0]);
}
else {
value name = ExpandedName(nameResult[0]);
value attribsMap = map(rr[0].map((attrib) => ExpandedName(attrib.key) -> attrib.item));
elementPath.push(ElementInfo(name, null));
return StartElement(name, false, attribsMap);
}
}
else {
throw ParseException("invalid character in start element tag");
}
}
case (State.content) {
// more madness...
variable Boolean stay = true;
variable Boolean whitespace = true;
value sb = StringBuilder();
while (stay) {
stay = false;
value contentResult = gatherTextContent();
sb.append(contentResult[0]);
whitespace &&= contentResult[2];
if (contentResult[1] == '<') {
value c = nextChar();
if (is Finished c) {
throw ParseException("EOF in element start");
}
if (c == '!') {
state = State.contentExclam;
}
else if (c == '?') {
state = State.contentPI;
}
else {
pushbackChar(c);
state = State.elementTag;
}
}
else if (contentResult[1] == '&') {
value c0 = nextChar();
if (is Finished c0) {
throw ParseException("EOF in content");
}
if (c0 == '#') {
value cr = resolveCharacterReference(nextChar);
whitespace &&= isXmlWhitespace(cr);
sb.appendCharacter(cr);
}
else {
value ref = gatherName(c0);
if (ref[1] != ';') {
throw ParseException("entity reference does not finish with ;");
}
value replacementText = internalEntities[ref[0]];
if (exists replacementText) {
entityRefSource.push(PositionPushbackSource(replacementText.iterator()));
}
else {
throw ParseException("entity not defined");
}
}
// yet more madness...
stay = true;
}
}
if (!sb.empty) {
// fixme ignorable whitespace?!
return Characters(sb.string, whitespace, false);
}
}
case (State.contentExclam) {
value c1 = nextChar();
if (is Finished c1) {
throw ParseException("EOF in start comment/CDATA section");
}
if (c1 == '-') {
value commentResult = readComment("-");
state = State.content;
return Comment(commentResult);
}
else if (c1 == '[') {
value res = check("CDATA[");
switch (res)
case (finished | false) {
throw ParseException("invalid CDATA section start");
}
case (true) {
value cdataResult = gatherCData();
state = State.content;
return Characters(cdataResult[0], cdataResult[1], false);
}
}
else {
throw ParseException("invalid start comment/CDATA section");
}
}
case (State.contentPI) {
value pi = readProcessingInstruction();
state = State.content;
return ProcessingInstruction(pi[0], pi[1]);
}
case (State.elementTag) {
if (!is Finished c = nextChar()) {
if (c == '/') {
state = State.endElementTag;
}
else {
pushbackChar(c);
state = State.element;
}
}
else {
throw ParseException("EOF in tag");
}
}
case (State.endElementTag) {
value nameResult = gatherName();
value c0 = nameResult[1];
Character c1;
if (isXmlWhitespace(c0)) {
value [ws, c] = gatherWhitespace(c0);
if (is Finished c) {
throw ParseException("EOF in element tag");
}
c1 = c;
}
else {
c1 = c0;
}
if (c1 != '>') {
throw ParseException("Invalid end element tag");
}
//FIXME check name match
value expectedName = elementPath.pop();
assert (exists expectedName);
ExpandedName name;
if (namespaceAware) {
assert (exists expectedPrefix = expectedName.name.prefix);
name = expandName(nameResult[0], true);
assert (exists endedPrefix = name.prefix);
if (expectedName.name.localName != name.localName || expectedPrefix != endedPrefix) {
throw ParseException("end tag not matching start tag");
}
assert (exists declarations = expectedName.namespaceDeclarations);
declarations.each((decl) => namespaceContext.pop(decl.key));
}
else {
if (expectedName.name.localName != nameResult[0]) {
throw ParseException("end tag not matching start tag");
}
name = ExpandedName(nameResult[0]);
}
if (exists it = elementPath.top) {
state = State.content;
}
else {
state = State.afterRoot;
}
return EndElement(name);
}
case (State.afterRoot) {
if (!is Finished c = source.nextChar()) {
if (isXmlWhitespace(c)) {
value wsResult = gatherWhitespace(c);
if (!is Finished it = wsResult[1]) {
source.pushbackChar(it);
}
return Characters(wsResult[0], true, false);
}
if (c == '<') {
if (!is Finished c1 = source.nextChar()) {
if (c1 == '!') {
value commentResult = readComment("--");
return Comment(commentResult);
}
else if (c1 == '?') {
value pi = readProcessingInstruction();
return ProcessingInstruction(pi[0], pi[1]);
}
else {
throw ParseException("multiple root elements");
}
}
else {
throw ParseException("EOF in comment/processing instruction after root element");
}
}
else {
throw ParseException("invalid text after root element");
}
}
else {
state = State.finished;
return EndDocument();
}
}
case (State.finished) {
return finished;
}
else {
throw AssertionError("unknown state ``state``");
}
}
}
Boolean|Finished check(String rest) {
for (sc in rest) {
if (!is Finished c = nextChar()) {
if (c != sc) {
return false;
}
}
else {
return finished;
}
}
return true;
}
Boolean|Finished checkWithPushbackOnFalse(String rest) {
value sb = StringBuilder();
for (sc in rest) {
if (!is Finished c = nextChar()) {
if (c != sc) {
sb.reversed.each((pb)=>pushbackChar(pb));
return false;
}
}
else {
return finished;
}
}
return true;
}
[String, Character|Finished] gatherWhitespace(Character? first = null)
{
StringBuilder sb = StringBuilder();
if (exists first) {
sb.appendCharacter(first);
}
while (true) {
value c = nextChar();
if (!is Finished c) {
if (isXmlWhitespace(c)) {
sb.appendCharacter(c);
}
else {
return [sb.string, c];
}
}
else {
return [sb.string, c];
}
}
}
[String, Character] gatherVersionDecimal() {
value sb = StringBuilder();
while (true) {
value c = nextChar();
if (is Finished c) {
throw ParseException("EOF in XML version number");
}
if ('0' <= c <= '9') {
sb.appendCharacter(c);
}
else {
if (sb.empty) {
throw ParseException("non-digit in XML version number decimal place");
}
else {
return [sb.string, c];
}
}
}
}
[String, Character] gatherName(Character? first = null)
{
value firstChar = first else nextChar();
if (is Finished firstChar) {
throw ParseException("EOF in Name");
}
if (!isNameStartChar(firstChar)) {
throw ParseException("Invalid name starting character '``firstChar``'");
}
StringBuilder sb = StringBuilder().appendCharacter(firstChar);
while (true) {
value c = nextChar();
if (is Finished c) {
throw ParseException("EOF in Name");
}
if (isNameChar(c)) {
sb.appendCharacter(c);
}
else {
return [sb.string, c];
}
}
}
String gatherComment()
{
value sb = StringBuilder();
while (true) {
value c0 = nextChar();
if (is Finished c0) {
throw ParseException("EOF in comment");
}
if (c0 == '-') {
value c1 = nextChar();
if (is Finished c1) {
throw ParseException("EOF in comment");
}
if (c1 == '-') {
value c2 = nextChar();
if (is Finished c2) {
throw ParseException("EOF in comment");
}
if (c2 == '>') {
return sb.string;
}
throw ParseException("-- in comment");
}
if (isChar(c1)) {
sb.appendCharacter(c0).appendCharacter(c1);
}
else {
throw ParseException("invalid character in comment");
}
}
if (isChar(c0)) {
sb.appendCharacter(c0);
}
else {
throw ParseException("invalid character in comment");
}
}
}
[String, Boolean] gatherCData()
{
variable Boolean isWhitespace = true;
value sb = StringBuilder();
while (true) {
value c0 = nextChar();
if (is Finished c0) {
throw ParseException("EOF in comment");
}
if (c0 == ']') {
value c1 = nextChar();
if (is Finished c1) {
throw ParseException("EOF in comment");
}
if (c1 == ']') {
value c2 = nextChar();
if (is Finished c2) {
throw ParseException("EOF in comment");
}
if (c2 == '>') {
return [sb.string, isWhitespace];
}
throw ParseException("-- in comment");
}
if (isChar(c1)) {
isWhitespace &&= isXmlWhitespace(c1);
sb.appendCharacter(c0).appendCharacter(c1);
}
else {
throw ParseException("invalid character in comment");
}
}
if (isChar(c0)) {
isWhitespace &&= isXmlWhitespace(c0);
sb.appendCharacter(c0);
}
else {
throw ParseException("invalid character in comment");
}
}
}
[String, Character, Boolean, Boolean] gatherTextContent()
{
variable Boolean isWhitespace = true;
value sb = StringBuilder();
while (true) {
value c0 = nextChar();
if (is Finished c0) {
throw ParseException("EOF in text content");
}
if (c0 == '<' || c0 == '&') {
return [sb.string, c0, isWhitespace, false];
}
if (!isChar(c0)) {
throw ParseException("invalid character in text content");
}
if (c0 == ']') {
value c1 = nextChar();
if (is Finished c1) {
throw ParseException("EOF in text content");
}
if (c1 == ']') {
value c2 = nextChar();
if (is Finished c2) {
throw ParseException("EOF in text content");
}
if (c2 == '>') {
/* FIXME this is a hack, text content until here should be returned as
* Characters and the the next event fetch result in this error.
*/
throw ParseException("]]> in text content not allowed");
}
else {
pushbackChar(c2);
pushbackChar(c1);
isWhitespace = false;
sb.appendCharacter(c0);
}
}
else {
pushbackChar(c1);
isWhitespace = false;
sb.appendCharacter(c0);
}
}
else {
isWhitespace &&= isXmlWhitespace(c0);
sb.appendCharacter(c0);
}
}
}
String gatherUntilQuestGt(Character first)
{
variable Character|Finished crs = first;
StringBuilder sb = StringBuilder();
while (true) {
value c = crs;
if (is Finished c) {
throw ParseException("EOF in PI or XMLDecl");
}
if (c == '?') {
value c2 = nextChar();
if (is Finished c2) {
throw ParseException("EOF in PI or XMLDecl");
}
if (c2 == '>') {
return sb.string;
}
sb.appendCharacter(c);
sb.appendCharacter(c2);
}
else {
sb.appendCharacter(c);
}
crs = nextChar();
}
}
StartDocument readXmlDeclaration()
{
value versionName = check("version");
switch (versionName)
case (finished) {
throw ParseException("EOF in XML declaration");
}
case (false) {
throw ParseException("version must be specified in XML declaration");
}
case (true) {
checkAttrEq();
}
value ws = nextChar();
if (is Finished ws) {
throw ParseException("EOF in XML declaration");
}
Character qExpected;
if (isXmlWhitespace(ws)) {
value wsResult1 = gatherWhitespace(ws);
if (!is Finished it1 = wsResult1[1]) {
qExpected = it1;
}
else {
throw ParseException("EOF after attribute name");
}
}
else {
qExpected = ws;
}
if (qExpected != '"' && qExpected != '\'') {
throw ParseException("invalid attribute value delimiter in XML declaration");
}