-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathByteXmlWriter.java
1737 lines (1579 loc) · 56.7 KB
/
ByteXmlWriter.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
/* Woodstox Lite ("wool") XML processor
*
* Copyright (c) 2006- Tatu Saloranta, [email protected]
*
* 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.out;
import java.io.*;
import javax.xml.stream.*;
import org.codehaus.stax2.ri.typed.AsciiValueEncoder;
import com.fasterxml.aalto.impl.ErrorConsts;
import com.fasterxml.aalto.util.XmlCharTypes;
import com.fasterxml.aalto.util.XmlChars;
import com.fasterxml.aalto.util.XmlConsts;
import static com.fasterxml.aalto.out.OutputCharTypes.*;
/**
* This abstract base class (partial implementation of {@link XmlWriter})
* is used if the destination is byte-based {@link java.io.OutputStream}.
*<p>
* Further, all existing implementations are for encodings that
* are 7-bit ascii compatible. This is important since this means
* that marker and separator characters are identical independent
* of actual encoding. This would not hold if support for encodings
* like EBCDIC were supported using this class.
*/
public abstract class ByteXmlWriter
extends XmlWriter
{
/**
* And this value determines size of the intermediate copy buffer
* to use.
*/
final static int DEFAULT_FULL_BUFFER_SIZE = 4000;
/**
* Default intermediate copy buffer size, to be used for efficient
* access to String content. Smaller, since it's in characters, plus
* will not be used for actual write operations
*/
final static int DEFAULT_COPY_BUFFER_SIZE = 1000;
/**
* Let's try avoid short writes, since some output streams have
* high per-call penalty (like network streams).
*/
final static int SMALL_WRITE = 250;
final static byte BYTE_SPACE = (byte) ' ';
final static byte BYTE_COLON = (byte) ':';
final static byte BYTE_SEMICOLON = (byte) ';';
final static byte BYTE_LBRACKET = (byte) '[';
final static byte BYTE_RBRACKET = (byte) ']';
final static byte BYTE_QMARK = (byte) '?';
final static byte BYTE_EQ = (byte) '=';
final static byte BYTE_SLASH = (byte) '/';
final static byte BYTE_HASH = (byte) '#';
final static byte BYTE_HYPHEN = (byte) '-';
final static byte BYTE_LT = (byte) '<';
final static byte BYTE_GT = (byte) '>';
final static byte BYTE_AMP = (byte) '&';
final static byte BYTE_QUOT = (byte) '"';
final static byte BYTE_APOS = (byte) '\'';
final static byte BYTE_A = (byte) 'a';
final static byte BYTE_G = (byte) 'g';
final static byte BYTE_L = (byte) 'l';
final static byte BYTE_M = (byte) 'm';
final static byte BYTE_O = (byte) 'o';
final static byte BYTE_P = (byte) 'p';
final static byte BYTE_Q = (byte) 'q';
final static byte BYTE_S = (byte) 's';
final static byte BYTE_T = (byte) 't';
final static byte BYTE_U = (byte) 'u';
final static byte BYTE_X = (byte) 'x';
final static byte[] BYTES_CDATA_START = getAscii("<![CDATA[");
final static byte[] BYTES_CDATA_END = getAscii("]]>");
final static byte[] BYTES_COMMENT_START = getAscii("<!--");
final static byte[] BYTES_COMMENT_END = getAscii("-->");
final static byte[] BYTES_XMLDECL_START = getAscii("<?xml version='");
final static byte[] BYTES_XMLDECL_ENCODING = getAscii(" encoding='");
final static byte[] BYTES_XMLDECL_STANDALONE = getAscii(" standalone='");
/*
/**********************************************************************
/* Output state, buffering
/**********************************************************************
*/
/**
* Actual Writer to use for outputting buffered data as appropriate.
* During active usage, remains as the writer initially set; set to
* null when this writer is closed.
*/
protected OutputStream _out;
protected byte[] _outputBuffer;
protected int _outputPtr;
protected final int _outputBufferLen;
/**
* In case a split surrogate pair is output (which can occur for only
* some of the methods, possibly depending on encoding),
* the first part is temporarily stored within this member variable.
*/
protected int _surrogate = 0;
/*
/**********************************************************************
/* Validation
/**********************************************************************
*/
/**
* Validation tables used for verifying validity (and need for quoting)
*/
final protected XmlCharTypes _charTypes;
/*
/**********************************************************************
/* Life cycle
/**********************************************************************
*/
protected ByteXmlWriter(WriterConfig cfg, OutputStream out, XmlCharTypes charTypes)
{
super(cfg);
_out = out;
_outputBuffer = cfg.allocFullBBuffer(DEFAULT_FULL_BUFFER_SIZE);
_outputBufferLen = _outputBuffer.length;
_outputPtr = 0;
_charTypes = charTypes;
}
@Override
protected final int getOutputPtr() {
return _outputPtr;
}
/*
/**********************************************************************
/* WNameFactory
/**********************************************************************
*/
@Override
public final WName constructName(String localName)
throws XMLStreamException
{
verifyNameComponent(localName);
return doConstructName(localName);
}
@Override
public WName constructName(String prefix, String localName)
throws XMLStreamException
{
verifyNameComponent(prefix);
verifyNameComponent(localName);
return doConstructName(prefix, localName);
}
protected abstract WName doConstructName(String localName)
throws XMLStreamException;
protected abstract WName doConstructName(String prefix, String localName)
throws XMLStreamException;
/**
* Method used to verify that a name component (prefix, local name)
* is a legal as per xml 1.0 specification.
*
* @throws IllegalArgumentException If name component contains
* an invalid (non-name; or for the initial characters,
* non-name-first) character.
*/
protected void verifyNameComponent(String part)
throws XMLStreamException
{
if (part == null || part.length() == 0) {
reportNwfName(ErrorConsts.WERR_NAME_EMPTY);
}
int ch = (int) part.charAt(0);
int len = part.length();
int i;
// First, special case: surrogates...
if (ch >= SURR1_FIRST && ch <= SURR2_LAST) {
// Can't start with surr2...
if (ch >= SURR2_FIRST) {
reportNwfName("Illegal surrogate pairing in name: first character ("+XmlChars.getCharDesc(ch)+") not valid surrogate first character");
}
// Unpaired? Not good either
if (len < 2) {
reportNwfName("Illegal surrogate pairing in name: incomplete surrogate (missing second half)");
}
// Otherwise let's decode code point for verification
ch = calcSurrogate(ch, part.charAt(1), " in name");
i = 2; // and skip second half of surrogate pair
} else {
i = 1;
}
if (!XmlChars.is10NameStartChar(ch)) {
reportNwfName("Invalid name start character "+XmlChars.getCharDesc(ch)+" (name \""+part+"\")");
}
// Also, names can not use entities, must be natively expressable
final int lastValid = getHighestEncodable();
if (ch > lastValid) {
reportNwfName("Illegal name start character "+XmlChars.getCharDesc(ch)+" (name \""+part+"\"): can not be expressed using effective encoding ("+_config.getActualEncoding()+")");
}
for (; i < len; ++i) {
ch = part.charAt(i);
if (ch >= SURR1_FIRST && ch <= SURR2_LAST) {
// Can't start with surr2...
if (ch >= SURR2_FIRST) {
reportNwfName("Illegal surrogate pairing in name: character at #"+i+" ("+XmlChars.getCharDesc(ch)+") not valid surrogate first character");
}
// Unpaired? Not good either
++i;
if (i >= len) {
reportNwfName("Illegal surrogate pairing in name: name ends with incomplete surrogate pair");
}
// Otherwise let's decode code point for verification
ch = calcSurrogate(ch, part.charAt(i), " in name");
}
if (ch > lastValid) {
reportNwfName("Illegal name character "+XmlChars.getCharDesc(ch)+" (name \""+part+"\", index #"+i+"): can not be expressed using effective encoding ("+_config.getActualEncoding()+")");
}
if (!XmlChars.is10NameChar(ch)) {
reportNwfName("Invalid name character "+XmlChars.getCharDesc(ch)+") in name (\""+part+"\"), index #"+i);
}
}
}
/*
/**********************************************************************
/* Abstract methods
/**********************************************************************
*/
/**
* Method called to output a composite character, result of
* combining 2 surrogate characters.
*/
abstract protected void outputSurrogates(int surr1, int surr2)
throws IOException, XMLStreamException;
abstract protected void output2ByteChar(int ch)
throws IOException, XMLStreamException;
/**
* Method called to output a character beyond basic 1- or 2-byte
* encoding (code 0x0800 and above), without being able to use
* character entities
*/
abstract protected int outputStrictMultiByteChar(int ch, char[] cbuf, int inputOffset, int inputLen)
throws IOException, XMLStreamException;
/**
* Method called to output a character beyond basic 1- or 2-byte
* encoding (code 0x0800 and above); possibly using character
* entities, if necessary
*/
abstract protected int outputMultiByteChar(int ch, char[] cbuf, int inputOffset, int inputLen)
throws IOException, XMLStreamException;
/*
/**********************************************************************
/* Low-level (pass-through) methods
/**********************************************************************
*/
@Override
public void _releaseBuffers()
{
super._releaseBuffers();
if (_outputBuffer != null) {
_config.freeFullBBuffer(_outputBuffer);
_outputBuffer = null;
}
if (_copyBuffer != null) {
_config.freeFullCBuffer(_copyBuffer);
_copyBuffer = null;
}
}
@Override
public void _closeTarget(boolean doClose) throws IOException
{
if (_out != null) { // just in case it's called multiple times
if (doClose) {
_out.close();
_out = null;
}
}
}
@Override
public final void flush() throws IOException
{
if (_out != null) {
flushBuffer();
_out.flush();
}
}
/*
/**********************************************************************
/* Write methods, raw
/**********************************************************************
*/
@Override
public final void writeRaw(String text, int offset, int len)
throws IOException, XMLStreamException
{
while (len > 0) {
char[] buf = _copyBuffer;
final int blen = buf.length;
final int len2 = (len < blen) ? len : blen;
text.getChars(offset, offset+len2, buf, 0);
writeRaw(buf, 0, len2);
offset += len2;
len -= len2;
}
}
/**
* This method is heavily encoding-dependant, so it needs
* to be deferred to sub-classes
*/
@Override
public abstract void writeRaw(char[] cbuf, int offset, int len)
throws IOException, XMLStreamException;
/*
/**********************************************************************
/* Write methods, elements
/**********************************************************************
*/
@Override
public final void writeStartTagStart(WName name)
throws IOException, XMLStreamException
{
if (_surrogate != 0) {
throwUnpairedSurrogate();
}
int ptr = _outputPtr;
if ((ptr + name.serializedLength() + 1) > _outputBufferLen) {
writeName(BYTE_LT, name); // let's offline slow case
return;
}
byte[] bbuf = _outputBuffer;
bbuf[ptr++] = BYTE_LT;
ptr += name.appendBytes(bbuf, ptr);
_outputPtr = ptr;
}
@Override
public final void writeStartTagEnd()
throws IOException, XMLStreamException
{
// inlined writeRaw(), gets called so often
if (_surrogate != 0) {
throwUnpairedSurrogate();
}
if (_outputPtr >= _outputBufferLen) {
flushBuffer();
}
_outputBuffer[_outputPtr++] = BYTE_GT;
}
@Override
public void writeStartTagEmptyEnd()
throws IOException
{
int ptr = _outputPtr;
if ((ptr + 2) > _outputBufferLen) {
flushBuffer();
ptr = _outputPtr;
}
byte[] bbuf = _outputBuffer;
bbuf[ptr++] = BYTE_SLASH;
bbuf[ptr++] = BYTE_GT;
_outputPtr = ptr;
}
@Override
public final void writeEndTag(WName name)
throws IOException, XMLStreamException
{
if (_surrogate != 0) {
throwUnpairedSurrogate();
}
int ptr = _outputPtr;
int len = name.serializedLength();
if ((ptr + len + 3) > _outputBufferLen) {
flushBuffer();
// name longer than the buffer? can write it straight out
if ((len + 3) > _outputBufferLen) {
_out.write(BYTE_LT);
_out.write(BYTE_SLASH);
name.writeBytes(_out);
// Last byte will fit in buffer ok though
_outputBuffer[_outputPtr++] = BYTE_GT;
return;
}
ptr = _outputPtr;
}
byte[] bbuf = _outputBuffer;
bbuf[ptr++] = BYTE_LT;
bbuf[ptr++] = BYTE_SLASH;
ptr += name.appendBytes(bbuf, ptr);
bbuf[ptr++] = BYTE_GT;
_outputPtr = ptr;
}
/*
/**********************************************************************
/* Write methods, attributes
/**********************************************************************
*/
@Override
public final void writeAttribute(WName name, String value)
throws IOException, XMLStreamException
{
int vlen = value.length();
// Let's off-line rare case:
if (vlen > _copyBufferLen) {
writeLongAttribute(name, value, vlen);
return;
}
char[] cbuf = _copyBuffer;
if (vlen > 0) {
value.getChars(0, vlen, cbuf, 0);
}
writeAttribute(name, cbuf, 0, vlen);
}
@Override
public final void writeAttribute(WName name, char[] vbuf, int offset, int vlen)
throws IOException, XMLStreamException
{
if (_surrogate != 0) {
throwUnpairedSurrogate();
}
// Enough room?
int ptr = _outputPtr;
byte[] bbuf = _outputBuffer;
if ((ptr + name.serializedLength()) >= _outputBufferLen) {
writeName(BYTE_SPACE, name);
ptr = _outputPtr;
} else {
bbuf[ptr++] = BYTE_SPACE;
ptr += name.appendBytes(bbuf, ptr);
}
// And then the value
if ((ptr + 3 + vlen) > _outputBufferLen) { // won't fit
_outputPtr = ptr;
flushBuffer();
bbuf[_outputPtr++] = BYTE_EQ;
bbuf[_outputPtr++] = BYTE_QUOT;
if ((_outputPtr + vlen + 1) > _outputBufferLen) {
writeAttrValue(vbuf, offset, vlen);
writeRaw(BYTE_QUOT);
return;
}
ptr = _outputPtr;
} else {
bbuf[ptr++] = BYTE_EQ;
bbuf[ptr++] = BYTE_QUOT;
}
if (vlen > 0) {
ptr = fastWriteAttrValue(vbuf, offset, vlen, bbuf, ptr);
}
bbuf[ptr++] = BYTE_QUOT;
_outputPtr = ptr;
}
/**
* Method called to copy given attribute value, when it's known that
* it will completely fit in the output buffer without further checks
*/
protected final int fastWriteAttrValue(char[] vbuf, int offset, int len,
byte[] bbuf, int ptr)
throws IOException, XMLStreamException
{
len += offset; // now marks the end
main_loop:
while (offset < len) {
final int[] charTypes = _charTypes.ATTR_CHARS;
inner_loop:
while (true) {
int ch = (int) vbuf[offset];
if (ch >= OutputCharTypes.MAIN_TABLE_SIZE) {
break inner_loop;
}
if (charTypes[ch] != XmlCharTypes.CT_OK) {
// Here we do want to quote linefeed, too
break;
}
bbuf[ptr++] = (byte)ch;
if (++offset >= len) {
break main_loop;
}
}
_outputPtr = ptr;
// Ok, so what did we hit? Invalid, or quotable?
int ch = (int) vbuf[offset++];
if (ch < OutputCharTypes.MAIN_TABLE_SIZE) {
switch (charTypes[ch]) {
case CT_INVALID:
reportInvalidChar(ch);
break;
case CT_MULTIBYTE_2:
output2ByteChar(ch);
break;
default:
writeAsEntity(ch);
}
} else {
offset = outputMultiByteChar(ch, vbuf, offset, len);
}
/* Ok, need to mess with buffers a bit: plus, it's possible
* that we may even need to flush the buffer as the guarantee
* for fitting may not necessarily hold (but it will after
* flushing)
* Still enough room? (also for following quote -- caller
* relies on that -- that's why >=, not >)
*/
if ((len - offset) >= (_outputBufferLen - _outputPtr)) {
flushBuffer();
}
ptr = _outputPtr;
}
return ptr;
}
protected final void writeAttrValue(char[] vbuf, int offset, int len)
throws IOException, XMLStreamException
{
if (_surrogate != 0) {
outputSurrogates(_surrogate, vbuf[offset]);
// reset the temporary surrogate storage
_surrogate = 0;
++offset;
--len;
}
len += offset; // now marks the end
main_loop:
while (offset < len) {
final int[] charTypes = _charTypes.ATTR_CHARS;
inner_loop:
while (true) {
int ch = (int) vbuf[offset];
if (ch >= OutputCharTypes.MAIN_TABLE_SIZE) {
break inner_loop;
}
if (charTypes[ch] != XmlCharTypes.CT_OK) {
break;
}
if (_outputPtr >= _outputBufferLen) {
flushBuffer();
}
_outputBuffer[_outputPtr++] = (byte)ch;
if (++offset >= len) {
break main_loop;
}
}
// Ok, so what did we hit?
int ch = (int) vbuf[offset++];
if (ch < OutputCharTypes.MAIN_TABLE_SIZE) {
switch (charTypes[ch]) {
case CT_INVALID:
reportInvalidChar(ch);
case CT_MULTIBYTE_2:
output2ByteChar(ch);
break;
default:
writeAsEntity(ch);
break;
}
} else {
offset = outputMultiByteChar(ch, vbuf, offset, len);
continue main_loop;
}
}
}
protected final void writeLongAttribute(WName name, String value, int vlen)
throws IOException, XMLStreamException
{
writeRaw(BYTE_SPACE);
int nlen = name.serializedLength();
if ((_outputPtr + nlen) > _outputBufferLen) {
flushBuffer();
if (nlen > _outputBufferLen) {
name.writeBytes(_out);
} else {
_outputPtr += name.appendBytes(_outputBuffer, _outputPtr);
}
} else {
_outputPtr += name.appendBytes(_outputBuffer, _outputPtr);
}
writeRaw(BYTE_EQ, BYTE_QUOT);
int offset = 0;
while (vlen > 0) {
char[] buf = _copyBuffer;
final int blen = buf.length;
int len2 = (vlen < blen) ? vlen : blen;
value.getChars(offset, offset+len2, buf, 0);
writeAttrValue(buf, 0, len2);
offset += len2;
vlen -= len2;
}
writeRaw(BYTE_QUOT);
}
/*
/**********************************************************************
/* Write methods, names
/**********************************************************************
*/
protected final void writeName(WName name)
throws IOException
{
int ptr = _outputPtr;
int len = name.serializedLength();
if ((ptr + len) > _outputBufferLen) {
flushBuffer();
// name longer than the buffer? can write it straight out
if (len >= _outputBufferLen) {
name.writeBytes(_out);
return;
}
ptr = _outputPtr;
}
ptr += name.appendBytes(_outputBuffer, ptr);
_outputPtr = ptr;
}
protected final void writeName(byte preChar, WName name)
throws IOException
{
flushBuffer();
// name longer than the buffer? Need to write it straight out
int len = name.serializedLength();
if (len >= _outputBufferLen) {
_out.write(preChar);
name.writeBytes(_out);
return;
}
int ptr = _outputPtr;
byte[] buf = _outputBuffer;
buf[ptr++] = preChar;
ptr += name.appendBytes(buf, ptr);
_outputPtr = ptr;
}
protected final void writeName(WName name, byte postChar)
throws IOException
{
flushBuffer();
// name longer than the buffer? Need to write it straight out
if (name.serializedLength() >= _outputBufferLen) {
name.writeBytes(_out);
_out.write(postChar);
return;
}
int ptr = _outputPtr;
byte[] buf = _outputBuffer;
ptr += name.appendBytes(buf, ptr);
buf[ptr++] = postChar;
_outputPtr = ptr;
}
private final void writeAttrNameEqQ(WName name)
throws IOException, XMLStreamException
{
if (_surrogate != 0) {
throwUnpairedSurrogate();
}
// Enough room for ' attr="' part?
int nlen = name.serializedLength();
int ptr = _outputPtr;
if ((ptr + nlen + 3) >= _outputBufferLen) {
flushBuffer();
ptr = _outputPtr;
// Still won't fit in buffer? Let's output pieces separately
if ((ptr + nlen + 3) >= _outputBufferLen) {
writeName(BYTE_SPACE, name);
writeRaw(BYTE_EQ);
writeRaw(BYTE_QUOT);
return;
}
}
byte[] bbuf = _outputBuffer;
bbuf[ptr++] = BYTE_SPACE;
ptr += name.appendBytes(bbuf, ptr);
bbuf[ptr++] = BYTE_EQ;
bbuf[ptr++] = BYTE_QUOT;
_outputPtr = ptr;
}
/*
/**********************************************************************
/* Write methods, textual content
/**********************************************************************
*/
/**
* @return -1 to indicate succesful write, or index of the problematic
* character in input (first ']' from "]]>" sequence, in non-fixing
* mode)
*/
@Override
public int writeCData(String data)
throws IOException, XMLStreamException
{
writeCDataStart(); // will check surrogates
int len = data.length();
int offset = 0;
while (len > 0) {
char[] buf = _copyBuffer;
int blen = buf.length;
// Can write all the rest?
if (blen > len) {
blen = len;
}
// Nope, can only do part
data.getChars(offset, offset+blen, buf, 0);
int cix = writeCDataContents(buf, 0, blen);
if (cix >= 0) {
return offset+cix;
}
offset += blen;
len -= blen;
}
writeCDataEnd(); // will check surrogates
return -1;
}
@Override
public int writeCData(char[] cbuf, int offset, int len)
throws IOException, XMLStreamException
{
writeCDataStart(); // will check surrogates
int ix = writeCDataContents(cbuf, offset, len);
if (ix < 0) {
writeCDataEnd(); // will check surrogates
}
return ix;
}
protected int writeCDataContents(char[] cbuf, int offset, int len)
throws IOException, XMLStreamException
{
if (_surrogate != 0) {
outputSurrogates(_surrogate, cbuf[offset]);
// reset the temporary surrogate storage
_surrogate = 0;
++offset;
--len;
}
// Unlike with writeCharacters() and fastWriteName(), let's not
// worry about split buffers here: this is unlikely to become
// performance bottleneck. This allows keeping it simple; and
// should it matter, we could start doing fast version here as well.
len += offset; // now marks the end
main_loop:
while (offset < len) {
final int[] charTypes = _charTypes.OTHER_CHARS;
inner_loop:
while (true) {
int ch = (int) cbuf[offset];
if (ch >= OutputCharTypes.MAIN_TABLE_SIZE) {
break inner_loop;
}
if (charTypes[ch] != XmlCharTypes.CT_OK) {
break inner_loop;
}
if (_outputPtr >= _outputBufferLen) {
flushBuffer();
}
_outputBuffer[_outputPtr++] = (byte) ch;
if (++offset >= len) {
break main_loop;
}
}
// Ok, so what did we hit?
int ch = (int) cbuf[offset++];
if (ch < OutputCharTypes.MAIN_TABLE_SIZE) {
switch (charTypes[ch]) {
case CT_INVALID:
reportInvalidChar(ch);
case CT_WS_CR: // No way to escape within CDATA
case CT_WS_LF:
++_locRowNr;
break;
case CT_OUTPUT_MUST_QUOTE: // == MULTIBYTE_N value
reportFailedEscaping("CDATA", ch);
case CT_MULTIBYTE_2:
// To off-line or not?
output2ByteChar(ch);
continue main_loop;
case CT_RBRACKET:
/* !!! TBI: Need to split CData? Can do, but what about
* content split around buffer boundary?
*/
if (offset < len && cbuf[offset] == ']') {
if ((offset+1) < len && cbuf[offset+1] == '>') {
// Ok, need to output ']]' first, then end
offset += 2;
writeRaw(BYTE_RBRACKET, BYTE_RBRACKET);
writeCDataEnd();
// Then new start, and '>'
writeCDataStart();
writeRaw(BYTE_GT);
} else {
// no end found, write first bracket
if (_outputPtr >= _outputBufferLen) {
flushBuffer();
}
_outputBuffer[_outputPtr++] = (byte) ch;
}
continue main_loop;
}
break;
default: // Everything else should be outputtable as is
break;
}
if (_outputPtr >= _outputBufferLen) {
flushBuffer();
}
_outputBuffer[_outputPtr++] = (byte)ch;
} else { // beyond 2-byte encodables; 3-byte, surrogates?
offset = outputMultiByteChar(ch, cbuf, offset, len);
}
}
return -1;
}
@Override
public final void writeCharacters(String text)
throws IOException, XMLStreamException
{
final int len = text.length();
// Not so common case, let's offline:
if (len > _copyBufferLen) {
longWriteCharacters(text);
return;
}
if (len > 0) {
char[] buf = _copyBuffer;
text.getChars(0, len, buf, 0);
writeCharacters(buf, 0, len);
}
}
private final void longWriteCharacters(String text)
throws IOException, XMLStreamException
{
int offset = 0;
int len = text.length();
char[] buf = _copyBuffer;
do {
final int blen = buf.length;
int len2 = (len < blen) ? len : blen;
text.getChars(offset, offset+len2, buf, 0);
writeCharacters(buf, 0, len2);
offset += len2;
len -= len2;
} while (len > 0);
}
@Override
public final void writeCharacters(char[] cbuf, int offset, int len)
throws IOException, XMLStreamException
{
if (_surrogate != 0) {
outputSurrogates(_surrogate, cbuf[offset]);
// reset the temporary surrogate storage
_surrogate = 0;
++offset;
--len;
}
// Ok, let's offline (what's sure to be) slow case first:
// (with multi-byte chars, others may be, too).
int ptr = _outputPtr;
if ((ptr + len) > _outputBufferLen) {
writeSplitCharacters(cbuf, offset, len);
return;
}
len += offset; // now marks the end
main_loop:
while (offset < len) {
final int[] charTypes = _charTypes.TEXT_CHARS;
inner_loop:
while (true) {
int ch = (int) cbuf[offset];
if (ch >= OutputCharTypes.MAIN_TABLE_SIZE) {
break inner_loop;
}
if (charTypes[ch] != XmlCharTypes.CT_OK) {
// This may look weird, but profiling showed that handling of LFs
// for indentation has measurable effect; plus, that checking it
// here will not slow down inner loop either
if (ch != '\n') {
break inner_loop;
}
++_locRowNr;
}
_outputBuffer[ptr++] = (byte) ch;
if (++offset >= len) {
break main_loop;
}
}
// Ok, so what did we hit?
int ch = (int) cbuf[offset++];
if (ch < OutputCharTypes.MAIN_TABLE_SIZE) {
switch (charTypes[ch]) {
case CT_INVALID:
reportInvalidChar(ch);
case CT_WS_CR:
// !!! TBI: line count
// Also, CR to be quoted?
if (_config.willEscapeCR()) {
_outputPtr = ptr;
writeAsEntity(ch);
break;
}
_outputBuffer[ptr++] = (byte)ch;
++_locRowNr;
continue main_loop;
case CT_WS_LF: // never occurs (handled in loop), but don't want to leave gaps
break;
case CT_OUTPUT_MUST_QUOTE: // == MULTIBYTE_N value
case CT_LT:
case CT_AMP:
_outputPtr = ptr;
writeAsEntity(ch);
break;
case CT_MULTIBYTE_2:
// To off-line or not?
_outputPtr = ptr;
output2ByteChar(ch);
break;
case CT_RBRACKET: // may need to quote as well...
// Let's not quote if known not to be followed by '>'
if (offset >= len || cbuf[offset] == '>') {
_outputPtr = ptr;
writeAsEntity(ch);
break;
}