-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathAsyncByteBufferScanner.java
4094 lines (3880 loc) · 150 KB
/
AsyncByteBufferScanner.java
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
/* Aalto XML processor
*
* Copyright (c) 2006- Tatu Saloranta, tatu.saloranta@iki.fi
*
* Licensed under the License specified in the file LICENSE which is
* included with the source code.
* You may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fasterxml.aalto.async;
import java.nio.ByteBuffer;
import javax.xml.stream.XMLStreamException;
import com.fasterxml.aalto.*;
import com.fasterxml.aalto.in.*;
import com.fasterxml.aalto.util.DataUtil;
//import com.fasterxml.aalto.util.XmlConsts;
import com.fasterxml.aalto.util.XmlCharTypes;
/**
* This is the base class for asynchronous (non-blocking) XML
* scanners. Due to basic complexity of async approach, character-based
* doesn't make much sense, so only byte-based input is supported.
*/
public class AsyncByteBufferScanner
extends AsyncByteScanner
implements AsyncByteBufferFeeder
{
/*
/**********************************************************************
/* Input buffer handling
/**********************************************************************
*/
/**
* This buffer is actually provided by caller
*/
protected ByteBuffer _inputBuffer;
/**
* In addition to current buffer pointer, and end pointer,
* we will also need to know number of bytes originally
* contained. This is needed to correctly update location
* information when the block has been completed.
*/
protected int _origBufferLen;
/*
/**********************************************************************
/* Instance construction
/**********************************************************************
*/
public AsyncByteBufferScanner(ReaderConfig cfg)
{
super(cfg);
// must start by checking if there's XML declaration...
_state = STATE_PROLOG_INITIAL;
_currToken = EVENT_INCOMPLETE;
}
@Override
public String toString() {
return "asyncScanner; curr="+_currToken+" next="+_nextEvent+", state = "+_state;
}
/*
/**********************************************************************
/* Implementation for low-level accessors
/**********************************************************************
*/
@Override
protected final byte _currentByte() throws XMLStreamException {
return _inputBuffer.get(_inputPtr);
}
@Override
protected final byte _nextByte() throws XMLStreamException {
return _inputBuffer.get(_inputPtr++);
}
@Override
protected final byte _prevByte() throws XMLStreamException {
return _inputBuffer.get(_inputPtr-1);
}
/*
/**********************************************************************
/* Parsing, comments
/**********************************************************************
*/
protected int parseCommentContents() throws XMLStreamException
{
// Left-overs from last input block?
if (_pendingInput != 0) { // CR, multi-byte, or '-'?
int result = handleCommentPending();
// If there's not enough input, or if we completed, can leave
if (result != 0) {
return result;
}
// otherwise we should be good to continue
}
char[] outputBuffer = _textBuilder.getBufferWithoutReset();
int outPtr = _textBuilder.getCurrentLength();
final int[] TYPES = _charTypes.OTHER_CHARS;
ByteBuffer inputBuffer = _inputBuffer;
main_loop:
while (true) {
int c;
// Then the tight ASCII non-funny-char loop:
ascii_loop:
while (true) {
if (_inputPtr >= _inputEnd) {
break main_loop;
}
if (outPtr >= outputBuffer.length) {
outputBuffer = _textBuilder.finishCurrentSegment();
outPtr = 0;
}
int max = _inputEnd;
{
int max2 = _inputPtr + (outputBuffer.length - outPtr);
if (max2 < max) {
max = max2;
}
}
while (_inputPtr < max) {
c = (int) inputBuffer.get(_inputPtr++) & 0xFF;
if (TYPES[c] != 0) {
break ascii_loop;
}
outputBuffer[outPtr++] = (char) c;
}
}
switch (TYPES[c]) {
case XmlCharTypes.CT_INVALID:
c = handleInvalidXmlChar(c);
case XmlCharTypes.CT_WS_CR:
{
if (_inputPtr >= _inputEnd) {
_pendingInput = PENDING_STATE_CR;
break main_loop;
}
if (inputBuffer.get(_inputPtr) == BYTE_LF) {
++_inputPtr;
}
markLF();
}
c = INT_LF;
break;
case XmlCharTypes.CT_WS_LF:
markLF();
break;
case XmlCharTypes.CT_MULTIBYTE_2:
if (_inputPtr >= _inputEnd) {
_pendingInput = c;
break main_loop;
}
c = decodeUtf8_2(c);
break;
case XmlCharTypes.CT_MULTIBYTE_3:
if ((_inputEnd - _inputPtr) < 2) {
if (_inputEnd > _inputPtr) { // 2 bytes available
int d = (int) _inputBuffer.get(_inputPtr++) & 0xFF;
c |= (d << 8);
}
_pendingInput = c;
break main_loop;
}
c = decodeUtf8_3(c);
break;
case XmlCharTypes.CT_MULTIBYTE_4:
if ((_inputEnd - _inputPtr) < 3) {
if (_inputEnd > _inputPtr) { // at least 2 bytes?
int d = (int) _inputBuffer.get(_inputPtr++) & 0xFF;
c |= (d << 8);
if (_inputEnd > _inputPtr) { // 3 bytes?
d = (int) _inputBuffer.get(_inputPtr++) & 0xFF;
c |= (d << 16);
}
}
_pendingInput = c;
break main_loop;
}
c = decodeUtf8_4(c);
// Let's add first part right away:
outputBuffer[outPtr++] = (char) (0xD800 | (c >> 10));
if (outPtr >= outputBuffer.length) {
outputBuffer = _textBuilder.finishCurrentSegment();
outPtr = 0;
}
c = 0xDC00 | (c & 0x3FF);
// And let the other char output down below
break;
case XmlCharTypes.CT_MULTIBYTE_N:
reportInvalidInitial(c);
case XmlCharTypes.CT_HYPHEN: // '-->'?
if (_inputPtr >= _inputEnd) {
_pendingInput = PENDING_STATE_COMMENT_HYPHEN1;
break main_loop;
}
if (_inputBuffer.get(_inputPtr) == BYTE_HYPHEN) { // ok, must be end then
++_inputPtr;
if (_inputPtr >= _inputEnd) {
_pendingInput = PENDING_STATE_COMMENT_HYPHEN2;
break main_loop;
}
if (_inputBuffer.get(_inputPtr++) != BYTE_GT) {
reportDoubleHyphenInComments();
}
_textBuilder.setCurrentLength(outPtr);
_state = STATE_DEFAULT;
_nextEvent = EVENT_INCOMPLETE;
return COMMENT;
}
break;
// default:
// Other types are not important here...
}
// Ok, can output the char (we know there's room for one more)
outputBuffer[outPtr++] = (char) c;
}
_textBuilder.setCurrentLength(outPtr);
return EVENT_INCOMPLETE;
}
/**
* @return EVENT_INCOMPLETE, if there's not enough input to
* handle pending char, COMMENT, if we handled complete
* "-->" end marker, or 0 to indicate something else
* was successfully handled.
*/
protected int handleCommentPending() throws XMLStreamException
{
if (_inputPtr >= _inputEnd) {
return EVENT_INCOMPLETE;
}
if (_pendingInput == PENDING_STATE_COMMENT_HYPHEN1) {
if (_inputBuffer.get(_inputPtr) != BYTE_HYPHEN) {
// can't be the end marker, just append '-' and go
_pendingInput = 0;
_textBuilder.append("-");
return 0;
}
++_inputPtr;
_pendingInput = PENDING_STATE_COMMENT_HYPHEN2;
if (_inputPtr >= _inputEnd) { // no more input?
return EVENT_INCOMPLETE;
}
// continue
}
if (_pendingInput == PENDING_STATE_COMMENT_HYPHEN2) {
_pendingInput = 0;
byte b = _inputBuffer.get(_inputPtr++);
if (b != BYTE_GT) {
reportDoubleHyphenInComments();
}
_state = STATE_DEFAULT;
_nextEvent = EVENT_INCOMPLETE;
return COMMENT;
}
// Otherwise can use default code
return handleAndAppendPending() ? 0 : EVENT_INCOMPLETE;
}
/*
/**********************************************************************
/* Parsing, PI
/**********************************************************************
*/
protected int parsePIData() throws XMLStreamException
{
// Left-overs from last input block?
if (_pendingInput != 0) { // CR, multi-byte, '?'
int result = handlePIPending();
// If there's not enough input, or if we completed, can leave
if (result != 0) {
return result;
}
// otherwise we should be good to continue
}
char[] outputBuffer = _textBuilder.getBufferWithoutReset();
int outPtr = _textBuilder.getCurrentLength();
final int[] TYPES = _charTypes.OTHER_CHARS;
ByteBuffer inputBuffer = _inputBuffer;
main_loop:
while (true) {
int c;
// Then the tight ASCII non-funny-char loop:
ascii_loop:
while (true) {
if (_inputPtr >= _inputEnd) {
break main_loop;
}
if (outPtr >= outputBuffer.length) {
outputBuffer = _textBuilder.finishCurrentSegment();
outPtr = 0;
}
int max = _inputEnd;
{
int max2 = _inputPtr + (outputBuffer.length - outPtr);
if (max2 < max) {
max = max2;
}
}
while (_inputPtr < max) {
c = (int) inputBuffer.get(_inputPtr++) & 0xFF;
if (TYPES[c] != 0) {
break ascii_loop;
}
outputBuffer[outPtr++] = (char) c;
}
}
switch (TYPES[c]) {
case XmlCharTypes.CT_INVALID:
c = handleInvalidXmlChar(c);
case XmlCharTypes.CT_WS_CR:
{
if (_inputPtr >= _inputEnd) {
_pendingInput = PENDING_STATE_CR;
break main_loop;
}
if (inputBuffer.get(_inputPtr) == BYTE_LF) {
++_inputPtr;
}
markLF();
}
c = INT_LF;
break;
case XmlCharTypes.CT_WS_LF:
markLF();
break;
case XmlCharTypes.CT_MULTIBYTE_2:
if (_inputPtr >= _inputEnd) {
_pendingInput = c;
break main_loop;
}
c = decodeUtf8_2(c);
break;
case XmlCharTypes.CT_MULTIBYTE_3:
if ((_inputEnd - _inputPtr) < 2) {
if (_inputEnd > _inputPtr) { // 2 bytes available
int d = (int) _inputBuffer.get(_inputPtr++) & 0xFF;
c |= (d << 8);
}
_pendingInput = c;
break main_loop;
}
c = decodeUtf8_3(c);
break;
case XmlCharTypes.CT_MULTIBYTE_4:
if ((_inputEnd - _inputPtr) < 3) {
if (_inputEnd > _inputPtr) { // at least 2 bytes?
int d = (int) _inputBuffer.get(_inputPtr++) & 0xFF;
c |= (d << 8);
if (_inputEnd > _inputPtr) { // 3 bytes?
d = (int) _inputBuffer.get(_inputPtr++) & 0xFF;
c |= (d << 16);
}
}
_pendingInput = c;
break main_loop;
}
c = decodeUtf8_4(c);
// Let's add first part right away:
outputBuffer[outPtr++] = (char) (0xD800 | (c >> 10));
if (outPtr >= outputBuffer.length) {
outputBuffer = _textBuilder.finishCurrentSegment();
outPtr = 0;
}
c = 0xDC00 | (c & 0x3FF);
// And let the other char output down below
break;
case XmlCharTypes.CT_MULTIBYTE_N:
reportInvalidInitial(c);
case XmlCharTypes.CT_QMARK:
if (_inputPtr >= _inputEnd) {
_pendingInput = PENDING_STATE_PI_QMARK;
break main_loop;
}
if (_inputBuffer.get(_inputPtr) == BYTE_GT) { // end
++_inputPtr;
_textBuilder.setCurrentLength(outPtr);
_state = STATE_DEFAULT;
_nextEvent = EVENT_INCOMPLETE;
return PROCESSING_INSTRUCTION;
}
// Not end mark, just need to reprocess the second char
break;
// default:
// Other types are not important here...
}
// Ok, can output the char (we know there's room for one more)
outputBuffer[outPtr++] = (char) c;
}
_textBuilder.setCurrentLength(outPtr);
return EVENT_INCOMPLETE;
}
/**
* @return EVENT_INCOMPLETE, if there's not enough input to
* handle pending char, PROCESSING_INSTRUCTION, if we handled complete
* "?>" end marker, or 0 to indicate something else
* was succesfully handled.
*/
protected int handlePIPending() throws XMLStreamException
{
// First, the special case, end marker:
if (_pendingInput == PENDING_STATE_PI_QMARK) {
if (_inputPtr >= _inputEnd) {
return EVENT_INCOMPLETE;
}
byte b = _inputBuffer.get(_inputPtr);
_pendingInput = 0;
if (b != BYTE_GT) {
// can't be the end marker, just append '-' and go
_textBuilder.append('?');
return 0;
}
++_inputPtr;
_state = STATE_DEFAULT;
_nextEvent = EVENT_INCOMPLETE;
return PROCESSING_INSTRUCTION;
}
// Otherwise can use default code
return handleAndAppendPending() ? 0 : EVENT_INCOMPLETE;
}
/*
/**********************************************************************
/* Parsing, internal DTD subset
/**********************************************************************
*/
@Override
protected final boolean handleDTDInternalSubset(boolean init) throws XMLStreamException
{
char[] outputBuffer;
int outPtr;
if (init) { // first time around
outputBuffer = _textBuilder.resetWithEmpty();
outPtr = 0;
_elemAttrQuote = 0;
_inDtdDeclaration = false;
} else {
if (_pendingInput != 0) {
if (!handleAndAppendPending()) {
return false;
}
}
outputBuffer = _textBuilder.getBufferWithoutReset();
outPtr = _textBuilder.getCurrentLength();
}
final int[] TYPES = _charTypes.DTD_CHARS;
ByteBuffer inputBuffer = _inputBuffer;
main_loop:
while (true) {
int c;
// Then the tight ASCII non-funny-char loop:
ascii_loop:
while (true) {
if (_inputPtr >= _inputEnd) {
break main_loop;
}
if (outPtr >= outputBuffer.length) {
outputBuffer = _textBuilder.finishCurrentSegment();
outPtr = 0;
}
int max = _inputEnd;
{
int max2 = _inputPtr + (outputBuffer.length - outPtr);
if (max2 < max) {
max = max2;
}
}
while (_inputPtr < max) {
c = (int) inputBuffer.get(_inputPtr++) & 0xFF;
if (TYPES[c] != 0) {
break ascii_loop;
}
outputBuffer[outPtr++] = (char) c;
}
}
switch (TYPES[c]) {
case XmlCharTypes.CT_INVALID:
c = handleInvalidXmlChar(c);
case XmlCharTypes.CT_WS_CR:
if (_inputPtr >= _inputEnd) {
_pendingInput = PENDING_STATE_CR;
break main_loop;
}
if (inputBuffer.get(_inputPtr) == BYTE_LF) {
++_inputPtr;
}
markLF();
c = INT_LF;
break;
case XmlCharTypes.CT_WS_LF:
markLF();
break;
case XmlCharTypes.CT_MULTIBYTE_2:
if (_inputPtr >= _inputEnd) {
_pendingInput = c;
break main_loop;
}
c = decodeUtf8_2(c);
break;
case XmlCharTypes.CT_MULTIBYTE_3:
if ((_inputEnd - _inputPtr) < 2) {
if (_inputEnd > _inputPtr) { // 2 bytes available
int d = (int) _inputBuffer.get(_inputPtr++) & 0xFF;
c |= (d << 8);
}
_pendingInput = c;
break main_loop;
}
c = decodeUtf8_3(c);
break;
case XmlCharTypes.CT_MULTIBYTE_4:
if ((_inputEnd - _inputPtr) < 3) {
if (_inputEnd > _inputPtr) { // at least 2 bytes?
int d = (int) _inputBuffer.get(_inputPtr++) & 0xFF;
c |= (d << 8);
if (_inputEnd > _inputPtr) { // 3 bytes?
d = (int) _inputBuffer.get(_inputPtr++) & 0xFF;
c |= (d << 16);
}
}
_pendingInput = c;
break main_loop;
}
c = decodeUtf8_4(c);
// Let's add first part right away:
outputBuffer[outPtr++] = (char) (0xD800 | (c >> 10));
if (outPtr >= outputBuffer.length) {
outputBuffer = _textBuilder.finishCurrentSegment();
outPtr = 0;
}
c = 0xDC00 | (c & 0x3FF);
// And let the other char output down below
break;
case XmlCharTypes.CT_MULTIBYTE_N:
reportInvalidInitial(c);
case XmlCharTypes.CT_DTD_QUOTE: // apos or quot
if (_elemAttrQuote == 0) {
_elemAttrQuote = (byte) c;
} else {
if (_elemAttrQuote == c) {
_elemAttrQuote = 0;
}
}
break;
case XmlCharTypes.CT_DTD_LT:
if (!_inDtdDeclaration) {
_inDtdDeclaration = true;
}
break;
case XmlCharTypes.CT_DTD_GT:
if (_elemAttrQuote == 0) {
_inDtdDeclaration = false;
}
break;
case XmlCharTypes.CT_DTD_RBRACKET:
if (!_inDtdDeclaration && _elemAttrQuote == 0) {
_textBuilder.setCurrentLength(outPtr);
return true;
}
break;
// default:
// Other types are not important here...
}
// Ok, can output the char (we know there's room for one more)
outputBuffer[outPtr++] = (char) c;
}
_textBuilder.setCurrentLength(outPtr);
return false;
}
/*
/**********************************************************************
/* Parsing, CDATA
/**********************************************************************
*/
protected final int parseCDataContents() throws XMLStreamException
{
// Left-overs from last input block?
if (_pendingInput != 0) { // CR, multi-byte, or ']'?
int result = handleCDataPending();
// If there's not enough input, or if we completed, can leave
if (result != 0) {
return result;
}
// otherwise we should be good to continue
}
char[] outputBuffer = _textBuilder.getBufferWithoutReset();
int outPtr = _textBuilder.getCurrentLength();
final int[] TYPES = _charTypes.OTHER_CHARS;
ByteBuffer inputBuffer = _inputBuffer;
main_loop:
while (true) {
int c;
// Then the tight ASCII non-funny-char loop:
ascii_loop:
while (true) {
if (_inputPtr >= _inputEnd) {
break main_loop;
}
if (outPtr >= outputBuffer.length) {
outputBuffer = _textBuilder.finishCurrentSegment();
outPtr = 0;
}
int max = _inputEnd;
{
int max2 = _inputPtr + (outputBuffer.length - outPtr);
if (max2 < max) {
max = max2;
}
}
while (_inputPtr < max) {
c = (int) inputBuffer.get(_inputPtr++) & 0xFF;
if (TYPES[c] != 0) {
break ascii_loop;
}
outputBuffer[outPtr++] = (char) c;
}
}
switch (TYPES[c]) {
case XmlCharTypes.CT_INVALID:
c = handleInvalidXmlChar(c);
case XmlCharTypes.CT_WS_CR:
{
if (_inputPtr >= _inputEnd) {
_pendingInput = PENDING_STATE_CR;
break main_loop;
}
if (inputBuffer.get(_inputPtr) == BYTE_LF) {
++_inputPtr;
}
markLF();
}
c = INT_LF;
break;
case XmlCharTypes.CT_WS_LF:
markLF();
break;
case XmlCharTypes.CT_MULTIBYTE_2:
if (_inputPtr >= _inputEnd) {
_pendingInput = c;
break main_loop;
}
c = decodeUtf8_2(c);
break;
case XmlCharTypes.CT_MULTIBYTE_3:
if ((_inputEnd - _inputPtr) < 2) {
if (_inputEnd > _inputPtr) { // 2 bytes available
int d = (int) _inputBuffer.get(_inputPtr++) & 0xFF;
c |= (d << 8);
}
_pendingInput = c;
break main_loop;
}
c = decodeUtf8_3(c);
break;
case XmlCharTypes.CT_MULTIBYTE_4:
if ((_inputEnd - _inputPtr) < 3) {
if (_inputEnd > _inputPtr) { // at least 2 bytes?
int d = (int) _inputBuffer.get(_inputPtr++) & 0xFF;
c |= (d << 8);
if (_inputEnd > _inputPtr) { // 3 bytes?
d = (int) _inputBuffer.get(_inputPtr++) & 0xFF;
c |= (d << 16);
}
}
_pendingInput = c;
break main_loop;
}
c = decodeUtf8_4(c);
// Let's add first part right away:
outputBuffer[outPtr++] = (char) (0xD800 | (c >> 10));
if (outPtr >= outputBuffer.length) {
outputBuffer = _textBuilder.finishCurrentSegment();
outPtr = 0;
}
c = 0xDC00 | (c & 0x3FF);
// And let the other char output down below
break;
case XmlCharTypes.CT_MULTIBYTE_N:
reportInvalidInitial(c);
case XmlCharTypes.CT_RBRACKET: // ']]>'?
if (_inputPtr >= _inputEnd) {
_pendingInput = PENDING_STATE_CDATA_BRACKET1;
break main_loop;
}
// Hmmh. This is more complex... so be it.
if (_inputBuffer.get(_inputPtr) == BYTE_RBRACKET) { // end might be nigh...
++_inputPtr;
while (true) {
if (_inputPtr >= _inputEnd) {
_pendingInput = PENDING_STATE_CDATA_BRACKET2;
break main_loop;
}
if (_inputBuffer.get(_inputPtr) == BYTE_GT) {
++_inputPtr;
_textBuilder.setCurrentLength(outPtr);
_state = STATE_DEFAULT;
_nextEvent = EVENT_INCOMPLETE;
return CDATA;
}
if (_inputBuffer.get(_inputPtr) != BYTE_RBRACKET) { // neither '>' nor ']'; push "]]" back
outputBuffer[outPtr++] = ']';
if (outPtr >= outputBuffer.length) {
outputBuffer = _textBuilder.finishCurrentSegment();
outPtr = 0;
}
outputBuffer[outPtr++] = ']';
continue main_loop;
}
// Got third bracket; push one back, keep on checking
++_inputPtr;
outputBuffer[outPtr++] = ']';
if (outPtr >= outputBuffer.length) {
outputBuffer = _textBuilder.finishCurrentSegment();
outPtr = 0;
}
}
}
break;
// default:
// Other types are not important here...
}
// Ok, can output the char (we know there's room for one more)
outputBuffer[outPtr++] = (char) c;
}
_textBuilder.setCurrentLength(outPtr);
return EVENT_INCOMPLETE;
}
/**
* @return EVENT_INCOMPLETE, if there's not enough input to
* handle pending char, CDATA, if we handled complete
* "]]>" end marker, or 0 to indicate something else
* was successfully handled.
*/
protected final int handleCDataPending() throws XMLStreamException
{
if (_pendingInput == PENDING_STATE_CDATA_BRACKET1) {
if (_inputPtr >= _inputEnd) {
return EVENT_INCOMPLETE;
}
if (_inputBuffer.get(_inputPtr) != BYTE_RBRACKET) {
// can't be the end marker, just append ']' and go
_textBuilder.append(']');
return (_pendingInput = 0);
}
++_inputPtr;
_pendingInput = PENDING_STATE_CDATA_BRACKET2;
if (_inputPtr >= _inputEnd) { // no more input?
return EVENT_INCOMPLETE;
}
// continue
}
while (_pendingInput == PENDING_STATE_CDATA_BRACKET2) {
if (_inputPtr >= _inputEnd) {
return EVENT_INCOMPLETE;
}
byte b = _inputBuffer.get(_inputPtr++);
if (b == BYTE_GT) {
_pendingInput = 0;
_state = STATE_DEFAULT;
_nextEvent = EVENT_INCOMPLETE;
return CDATA;
}
if (b != BYTE_RBRACKET) {
--_inputPtr;
_textBuilder.append("]]");
return (_pendingInput = 0);
}
_textBuilder.append(']');
}
// Otherwise can use default code
return handleAndAppendPending() ? 0 : EVENT_INCOMPLETE;
}
/**
* This method gets called, if the first character of a
* CHARACTERS event could not be fully read (multi-byte,
* split over buffer boundary). If so, there is some
* pending data to be handled.
*/
protected int startCharactersPending() throws XMLStreamException
{
// First, need to have at least one more byte:
if (_inputPtr >= _inputEnd) {
return EVENT_INCOMPLETE;
}
// K. So what was the type again?
int c = _pendingInput;
_pendingInput = 0;
// Possible \r\n linefeed?
if (c == PENDING_STATE_CR) {
if (_inputBuffer.get(_inputPtr) == BYTE_LF) {
++_inputPtr;
}
markLF();
_textBuilder.resetWithChar(CHAR_LF);
} else {
// Nah, a multi-byte UTF-8 char:
// Let's just retest the first pending byte (in LSB):
switch (_charTypes.TEXT_CHARS[c & 0xFF]) {
case XmlCharTypes.CT_MULTIBYTE_2:
// Easy: must have just one byte, did get another one:
_textBuilder.resetWithChar((char) decodeUtf8_2(c));
break;
case XmlCharTypes.CT_MULTIBYTE_3:
{
// Ok... so do we have one or two pending bytes?
int next = _inputBuffer.get(_inputPtr++) & 0xFF;
int c2 = (c >> 8);
if (c2 == 0) { // just one; need two more
if (_inputPtr >= _inputEnd) { // but got only one
_pendingInput = c | (next << 8);
return EVENT_INCOMPLETE;
}
int c3 = _inputBuffer.get(_inputPtr++) & 0xFF;
c = decodeUtf8_3(c, next, c3);
} else { // had two, got one, bueno:
c = decodeUtf8_3((c & 0xFF), c2, next);
}
_textBuilder.resetWithChar((char) c);
}
break;
case XmlCharTypes.CT_MULTIBYTE_4:
{
int next = (int) _inputBuffer.get(_inputPtr++) & 0xFF;
// Only had one?
if ((c >> 8) == 0) { // ok, so need 3 more
if (_inputPtr >= _inputEnd) { // just have 1
_pendingInput = c | (next << 8);
return EVENT_INCOMPLETE;
}
int c2 = _inputBuffer.get(_inputPtr++) & 0xFF;
if (_inputPtr >= _inputEnd) { // almost, got 2
_pendingInput = c | (next << 8) | (c2 << 16);
return EVENT_INCOMPLETE;
}
int c3 = _inputBuffer.get(_inputPtr++) & 0xFF;
c = decodeUtf8_4(c, next, c2, c3);
} else { // had two or three
int c2 = (c >> 8) & 0xFF;
int c3 = (c >> 16);
if (c3 == 0) { // just two
if (_inputPtr >= _inputEnd) { // one short
_pendingInput = c | (next << 16);
return EVENT_INCOMPLETE;
}
c3 = _inputBuffer.get(_inputPtr++) & 0xFF;
c = decodeUtf8_4((c & 0xFF), c2, next, c3);
} else { // had three, got last
c = decodeUtf8_4((c & 0xFF), c2, c3, next);
}
}
}
// Need a surrogate pair, have to call from here:
_textBuilder.resetWithSurrogate(c);
return (_currToken = CHARACTERS);
default: // should never occur:
throwInternal();
}
}
// Great, we got it. Is that enough?
if (_cfgCoalescing && !_cfgLazyParsing) {
// In eager coalescing mode, must read it all
return finishCharactersCoalescing();
}
_currToken = CHARACTERS;
if (_cfgLazyParsing) {
_tokenIncomplete = true;
} else {
finishCharacters();
}
return _currToken;
}
/**
* TODO: Method not yet implemented
*/
protected final int finishCharactersCoalescing() throws XMLStreamException
{
// First things first: any pending partial multi-bytes?
if (_pendingInput != 0) {
if (!handleAndAppendPending()) {
return EVENT_INCOMPLETE;
}
}
throw new UnsupportedOperationException();
// !!! TBI
// return 0;
}
/*
/**********************************************************************
/* Async input, methods to feed (push) content to parse
/**********************************************************************
*/
@Override
public final boolean needMoreInput() {
return (_inputPtr >=_inputEnd) && !_endOfInput;
}
@Override
public void feedInput(ByteBuffer buffer) throws XMLStreamException
{
// Must not have remaining input
if (_inputPtr < _inputEnd) {
throw new XMLStreamException("Still have "+(_inputEnd - _inputPtr)+" unread bytes");
}
// and shouldn't have been marked as end-of-input
if (_endOfInput) {
throw new XMLStreamException("Already closed, can not feed more input");
}
// Time to update pointers first
_pastBytesOrChars += _origBufferLen;
_rowStartOffset -= _origBufferLen;
int start = buffer.position();
int end = buffer.limit();
// And then update buffer settings
_inputBuffer = buffer;
_inputPtr = start;
_inputEnd = end;
_origBufferLen = end-start;
}
/*
/**********************************************************************
/* Implementation of parsing API
/**********************************************************************
*/
@Override
public int nextFromTree() throws XMLStreamException
{
// Had a fully complete event? Need to reset state:
if (_currToken != EVENT_INCOMPLETE) {
/* First, need to handle some complications arising from
* empty elements, and namespace binding/unbinding:
*/
if (_currToken == START_ELEMENT) {
if (_isEmptyTag) {
--_depth;
// Important: do NOT overwrite start location, same as with START_ELEMENT
return (_currToken = END_ELEMENT);
}
} else if (_currToken == END_ELEMENT) {
_currElem = _currElem.getParent();
// Any namespace declarations that need to be unbound?
while (_lastNsDecl != null && _lastNsDecl.getLevel() >= _depth) {
_lastNsDecl = _lastNsDecl.unbind();