-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
FromXmlParser.java
1058 lines (959 loc) · 35.5 KB
/
FromXmlParser.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
package com.fasterxml.jackson.dataformat.xml.deser;
import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Set;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.base.ParserMinimalBase;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import com.fasterxml.jackson.dataformat.xml.PackageVersion;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.util.StaxUtil;
/**
* {@link JsonParser} implementation that exposes XML structure as
* set of JSON events that can be used for data binding.
*/
public class FromXmlParser
extends ParserMinimalBase
{
/**
* The default name placeholder for XML text segments is empty
* String ("").
*/
public final static String DEFAULT_UNNAMED_TEXT_PROPERTY = "";
/**
* Enumeration that defines all togglable features for XML parsers.
*/
public enum Feature implements FormatFeature
{
/**
* Feature that indicates whether XML Empty elements (ones where there are
* no separate start and end tages, but just one tag that ends with "/>")
* are exposed as {@link JsonToken#VALUE_NULL}) or not. If they are not
* returned as `null` tokens, they will be returned as {@link JsonToken#VALUE_STRING}
* tokens with textual value of "" (empty String).
*<p>
* Default setting is `true` for backwards compatibility.
*
* @since 2.9
*/
EMPTY_ELEMENT_AS_NULL(true)
;
final boolean _defaultState;
final int _mask;
/**
* Method that calculates bit set (flags) of all features that
* are enabled by default.
*/
public static int collectDefaults()
{
int flags = 0;
for (Feature f : values()) {
if (f.enabledByDefault()) {
flags |= f.getMask();
}
}
return flags;
}
private Feature(boolean defaultState) {
_defaultState = defaultState;
_mask = (1 << ordinal());
}
@Override public boolean enabledByDefault() { return _defaultState; }
@Override public int getMask() { return _mask; }
@Override public boolean enabledIn(int flags) { return (flags & getMask()) != 0; }
}
/**
* In cases where a start element has both attributes and non-empty textual
* value, we have to create a bogus property; we will use this as
* the property name.
*<p>
* Name used for pseudo-property used for returning XML Text value (which does
* not have actual element name to use). Defaults to empty String, but
* may be changed for interoperability reasons: JAXB, for example, uses
* "value" as name.
*
* @since 2.1
*/
protected String _cfgNameForTextElement = DEFAULT_UNNAMED_TEXT_PROPERTY;
/*
/**********************************************************
/* Configuration
/**********************************************************
*/
/**
* Bit flag composed of bits that indicate which
* {@link FromXmlParser.Feature}s
* are enabled.
*/
protected int _formatFeatures;
protected ObjectCodec _objectCodec;
/*
/**********************************************************
/* I/O state
/**********************************************************
*/
/**
* Flag that indicates whether parser is closed or not. Gets
* set when parser is either closed by explicit call
* ({@link #close}) or when end-of-input is reached.
*/
protected boolean _closed;
final protected IOContext _ioContext;
/*
/**********************************************************
/* Parsing state
/**********************************************************
*/
/**
* Information about parser context, context in which
* the next token is to be parsed (root, array, object).
*/
protected XmlReadContext _parsingContext;
protected final XmlTokenStream _xmlTokens;
/**
*
* We need special handling to keep track of whether a value
* may be exposed as simple leaf value.
*/
protected boolean _mayBeLeaf;
protected JsonToken _nextToken;
protected String _currText;
protected Set<String> _namesToWrap;
/*
/**********************************************************
/* Parsing state, parsed values
/**********************************************************
*/
/**
* ByteArrayBuilder is needed if 'getBinaryValue' is called. If so,
* we better reuse it for remainder of content.
*/
protected ByteArrayBuilder _byteArrayBuilder = null;
/**
* We will hold on to decoded binary data, for duration of
* current event, so that multiple calls to
* {@link #getBinaryValue} will not need to decode data more
* than once.
*/
protected byte[] _binaryValue;
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
public FromXmlParser(IOContext ctxt, int genericParserFeatures, int xmlFeatures,
ObjectCodec codec, XMLStreamReader xmlReader)
throws IOException
{
super(genericParserFeatures);
_formatFeatures = xmlFeatures;
_ioContext = ctxt;
_objectCodec = codec;
_parsingContext = XmlReadContext.createRootContext(-1, -1);
_xmlTokens = new XmlTokenStream(xmlReader, ctxt.getSourceReference(),
_formatFeatures);
// 04-Jan-2019, tatu: Root-level nulls need slightly specific handling;
// changed in 2.10.2
if (_xmlTokens.hasXsiNil()) {
_nextToken = JsonToken.VALUE_NULL;
} else if (_xmlTokens.getCurrentToken() == XmlTokenStream.XML_START_ELEMENT) {
_nextToken = JsonToken.START_OBJECT;
} else {
_reportError("Internal problem: invalid starting state (%d)", _xmlTokens.getCurrentToken());
}
}
@Override
public Version version() {
return PackageVersion.VERSION;
}
@Override
public ObjectCodec getCodec() {
return _objectCodec;
}
@Override
public void setCodec(ObjectCodec c) {
_objectCodec = c;
}
/**
* @since 2.1
*/
public void setXMLTextElementName(String name) {
_cfgNameForTextElement = name;
}
/**
* XML format does require support from custom {@link ObjectCodec}
* (that is, {@link XmlMapper}), so need to return true here.
*
* @return True since XML format does require support from codec
*/
@Override
public boolean requiresCustomCodec() {
return true;
}
/*
/**********************************************************
/* Extended API, configuration
/**********************************************************
*/
public FromXmlParser enable(Feature f) {
_formatFeatures |= f.getMask();
_xmlTokens.setFormatFeatures(_formatFeatures);
return this;
}
public FromXmlParser disable(Feature f) {
_formatFeatures &= ~f.getMask();
_xmlTokens.setFormatFeatures(_formatFeatures);
return this;
}
public final boolean isEnabled(Feature f) {
return (_formatFeatures & f.getMask()) != 0;
}
public FromXmlParser configure(Feature f, boolean state) {
if (state) {
enable(f);
} else {
disable(f);
}
return this;
}
/*
/**********************************************************
/* FormatFeature support
/**********************************************************
*/
@Override
public int getFormatFeatures() {
return _formatFeatures;
}
@Override
public JsonParser overrideFormatFeatures(int values, int mask) {
_formatFeatures = (_formatFeatures & ~mask) | (values & mask);
return this;
}
/*
/**********************************************************
/* Extended API, access to some internal components
/**********************************************************
*/
/**
* Method that allows application direct access to underlying
* Stax {@link XMLStreamWriter}. Note that use of writer is
* discouraged, and may interfere with processing of this writer;
* however, occasionally it may be necessary.
*<p>
* Note: writer instance will always be of type
* {@link org.codehaus.stax2.XMLStreamWriter2} (including
* Typed Access API) so upcasts are safe.
*/
public XMLStreamReader getStaxReader() {
return _xmlTokens.getXmlReader();
}
/*
/**********************************************************
/* Internal API
/**********************************************************
*/
/**
* Method that may be called to indicate that specified names
* (only local parts retained currently: this may be changed in
* future) should be considered "auto-wrapping", meaning that
* they will be doubled to contain two opening elements, two
* matching closing elements. This is needed for supporting
* handling of so-called "unwrapped" array types, something
* XML mappings like JAXB often use.
*<p>
* NOTE: this method is considered part of internal implementation
* interface, and it is <b>NOT</b> guaranteed to remain unchanged
* between minor versions (it is however expected not to change in
* patch versions). So if you have to use it, be prepared for
* possible additional work.
*
* @since 2.1
*/
public void addVirtualWrapping(Set<String> namesToWrap)
{
/* 17-Sep-2012, tatu: Not 100% sure why, but this is necessary to avoid
* problems with Lists-in-Lists properties
*/
String name = _xmlTokens.getLocalName();
if (name != null && namesToWrap.contains(name)) {
_xmlTokens.repeatStartElement();
}
_namesToWrap = namesToWrap;
_parsingContext.setNamesToWrap(namesToWrap);
}
/*
/**********************************************************
/* JsonParser impl
/**********************************************************
*/
/**
* Method that can be called to get the name associated with
* the current event.
*/
@Override
public String getCurrentName() throws IOException
{
// start markers require information from parent
String name;
if (_currToken == JsonToken.START_OBJECT || _currToken == JsonToken.START_ARRAY) {
XmlReadContext parent = _parsingContext.getParent();
name = parent.getCurrentName();
} else {
name = _parsingContext.getCurrentName();
}
// sanity check
if (name == null) {
throw new IllegalStateException("Missing name, in state: "+_currToken);
}
return name;
}
@Override
public void overrideCurrentName(String name)
{
// Simple, but need to look for START_OBJECT/ARRAY's "off-by-one" thing:
XmlReadContext ctxt = _parsingContext;
if (_currToken == JsonToken.START_OBJECT || _currToken == JsonToken.START_ARRAY) {
ctxt = ctxt.getParent();
}
ctxt.setCurrentName(name);
}
@Override
public void close() throws IOException
{
if (!_closed) {
_closed = true;
try {
if (_ioContext.isResourceManaged() || isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)) {
_xmlTokens.closeCompletely();
} else {
_xmlTokens.close();
}
} catch (XMLStreamException e) {
StaxUtil.throwAsParseException(e, this);
} finally {
// Also, internal buffer(s) can now be released as well
_releaseBuffers();
}
}
}
@Override
public boolean isClosed() { return _closed; }
@Override
public XmlReadContext getParsingContext() {
return _parsingContext;
}
/**
* Method that return the <b>starting</b> location of the current
* token; that is, position of the first character from input
* that starts the current token.
*/
@Override
public JsonLocation getTokenLocation() {
return _xmlTokens.getTokenLocation();
}
/**
* Method that returns location of the last processed character;
* usually for error reporting purposes
*/
@Override
public JsonLocation getCurrentLocation() {
return _xmlTokens.getCurrentLocation();
}
/**
* Since xml representation can not really distinguish between array
* and object starts (both are represented with elements), this method
* is overridden and taken to mean that expecation is that the current
* start element is to mean 'start array', instead of default of
* 'start object'.
*/
@Override
public boolean isExpectedStartArrayToken()
{
JsonToken t = _currToken;
if (t == JsonToken.START_OBJECT) {
_currToken = JsonToken.START_ARRAY;
// Ok: must replace current context with array as well
_parsingContext.convertToArray();
//System.out.println(" isExpectedArrayStart: OBJ->Array, wraps now: "+_parsingContext.getNamesToWrap());
// And just in case a field name was to be returned, wipe it
// 06-Jan-2015, tatu: Actually, could also be empty Object buffered; if so, convert...
if (_nextToken == JsonToken.END_OBJECT) {
_nextToken = JsonToken.END_ARRAY;
} else {
_nextToken = null;
}
// and last thing, [dataformat-xml#33], better ignore attributes
_xmlTokens.skipAttributes();
return true;
}
//System.out.println(" isExpectedArrayStart?: t="+t);
return (t == JsonToken.START_ARRAY);
}
// DEBUGGING
/*
@Override
public JsonToken nextToken() throws IOException
{
JsonToken t = nextToken0();
if (t != null) {
switch (t) {
case FIELD_NAME:
System.out.println("JsonToken: FIELD_NAME '"+_parsingContext.getCurrentName()+"'");
break;
case VALUE_STRING:
System.out.println("JsonToken: VALUE_STRING '"+getText()+"'");
break;
default:
System.out.println("JsonToken: "+t);
}
}
return t;
}
*/
// public JsonToken nextToken0() throws IOException
@Override
public JsonToken nextToken() throws IOException
{
_binaryValue = null;
if (_nextToken != null) {
JsonToken t = _nextToken;
_currToken = t;
_nextToken = null;
switch (t) {
case START_OBJECT:
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
break;
case START_ARRAY:
_parsingContext = _parsingContext.createChildArrayContext(-1, -1);
break;
case END_OBJECT:
case END_ARRAY:
_parsingContext = _parsingContext.getParent();
_namesToWrap = _parsingContext.getNamesToWrap();
break;
case FIELD_NAME:
_parsingContext.setCurrentName(_xmlTokens.getLocalName());
break;
default: // VALUE_STRING, VALUE_NULL
// should be fine as is?
}
return t;
}
int token;
try {
token = _xmlTokens.next();
} catch (XMLStreamException e) {
token = StaxUtil.throwAsParseException(e, this);
}
// Need to have a loop just because we may have to eat/convert
// a start-element that indicates an array element.
while (token == XmlTokenStream.XML_START_ELEMENT) {
// If we thought we might get leaf, no such luck
if (_mayBeLeaf) {
// leave _mayBeLeaf set, as we start a new context
_nextToken = JsonToken.FIELD_NAME;
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
return (_currToken = JsonToken.START_OBJECT);
}
if (_parsingContext.inArray()) {
// Yup: in array, so this element could be verified; but it won't be
// reported anyway, and we need to process following event.
try {
token = _xmlTokens.next();
} catch (XMLStreamException e) {
StaxUtil.throwAsParseException(e, this);
}
_mayBeLeaf = true;
continue;
}
String name = _xmlTokens.getLocalName();
_parsingContext.setCurrentName(name);
// Ok: virtual wrapping can be done by simply repeating current START_ELEMENT.
// Couple of ways to do it; but start by making _xmlTokens replay the thing...
if (_namesToWrap != null && _namesToWrap.contains(name)) {
_xmlTokens.repeatStartElement();
}
_mayBeLeaf = true;
// Ok: in array context we need to skip reporting field names.
// But what's the best way to find next token?
return (_currToken = JsonToken.FIELD_NAME);
}
// Ok; beyond start element, what do we get?
while (true) {
switch (token) {
case XmlTokenStream.XML_END_ELEMENT:
// Simple, except that if this is a leaf, need to suppress end:
if (_mayBeLeaf) {
_mayBeLeaf = false;
if (_parsingContext.inArray()) {
// 06-Jan-2015, tatu: as per [dataformat-xml#180], need to
// expose as empty Object, not null
_nextToken = JsonToken.END_OBJECT;
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
return (_currToken = JsonToken.START_OBJECT);
}
// 07-Sep-2019, tatu: for [dataformat-xml#353], must NOT return second null
if (_currToken != JsonToken.VALUE_NULL) {
return (_currToken = JsonToken.VALUE_NULL);
}
}
_currToken = _parsingContext.inArray() ? JsonToken.END_ARRAY : JsonToken.END_OBJECT;
_parsingContext = _parsingContext.getParent();
_namesToWrap = _parsingContext.getNamesToWrap();
return _currToken;
case XmlTokenStream.XML_ATTRIBUTE_NAME:
// If there was a chance of leaf node, no more...
if (_mayBeLeaf) {
_mayBeLeaf = false;
_nextToken = JsonToken.FIELD_NAME;
_currText = _xmlTokens.getText();
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
return (_currToken = JsonToken.START_OBJECT);
}
_parsingContext.setCurrentName(_xmlTokens.getLocalName());
return (_currToken = JsonToken.FIELD_NAME);
case XmlTokenStream.XML_ATTRIBUTE_VALUE:
_currText = _xmlTokens.getText();
return (_currToken = JsonToken.VALUE_STRING);
case XmlTokenStream.XML_TEXT:
_currText = _xmlTokens.getText();
if (_mayBeLeaf) {
_mayBeLeaf = false;
// One more refinement (pronunced like "hack") is that if
// we had an empty String (or all white space), and we are
// deserializing an array, we better hide the empty text.
// Also: must skip following END_ELEMENT
try {
_xmlTokens.skipEndElement();
} catch (XMLStreamException e) {
StaxUtil.throwAsParseException(e, this);
}
if (_parsingContext.inArray()) {
if (_isEmpty(_currText)) {
// 06-Jan-2015, tatu: as per [dataformat-xml#180], need to
// expose as empty Object, not null (or, worse, as used to
// be done, by swallowing the token)
_nextToken = JsonToken.END_OBJECT;
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
return (_currToken = JsonToken.START_OBJECT);
}
}
return (_currToken = JsonToken.VALUE_STRING);
}
// [dataformat-xml#177]: empty text may also need to be skipped
// but... [dataformat-xml#191]: looks like we can't short-cut, must
// loop over again
if (_parsingContext.inObject()) {
if ((_currToken != JsonToken.FIELD_NAME) && _isEmpty(_currText)) {
try {
token = _xmlTokens.next();
} catch (XMLStreamException e) {
StaxUtil.throwAsParseException(e, this);
}
continue;
}
}
// If not a leaf (or otherwise ignorable), need to transform into property...
_parsingContext.setCurrentName(_cfgNameForTextElement);
_nextToken = JsonToken.VALUE_STRING;
return (_currToken = JsonToken.FIELD_NAME);
case XmlTokenStream.XML_END:
return (_currToken = null);
default:
return _internalErrorUnknownToken(token);
}
}
}
/*
/**********************************************************
/* Overrides of specialized nextXxx() methods
/**********************************************************
*/
/*
@Override
public String nextFieldName() throws IOException {
if (nextToken() == JsonToken.FIELD_NAME) {
return getCurrentName();
}
return null;
}
*/
/**
* Method overridden to support more reliable deserialization of
* String collections.
*/
@Override
public String nextTextValue() throws IOException
{
_binaryValue = null;
if (_nextToken != null) {
JsonToken t = _nextToken;
_currToken = t;
_nextToken = null;
// expected case; yes, got a String
if (t == JsonToken.VALUE_STRING) {
return _currText;
}
_updateState(t);
return null;
}
int token;
try {
token = _xmlTokens.next();
} catch (XMLStreamException e) {
token = StaxUtil.throwAsParseException(e, this);
}
// mostly copied from 'nextToken()'
while (token == XmlTokenStream.XML_START_ELEMENT) {
if (_mayBeLeaf) {
_nextToken = JsonToken.FIELD_NAME;
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
_currToken = JsonToken.START_OBJECT;
return null;
}
if (_parsingContext.inArray()) {
try {
token = _xmlTokens.next();
} catch (XMLStreamException e) {
StaxUtil.throwAsParseException(e, this);
}
_mayBeLeaf = true;
continue;
}
String name = _xmlTokens.getLocalName();
_parsingContext.setCurrentName(name);
if (_namesToWrap != null && _namesToWrap.contains(name)) {
_xmlTokens.repeatStartElement();
}
_mayBeLeaf = true;
_currToken = JsonToken.FIELD_NAME;
return null;
}
// Ok; beyond start element, what do we get?
switch (token) {
case XmlTokenStream.XML_END_ELEMENT:
if (_mayBeLeaf) {
// NOTE: this is different from nextToken() -- produce "", NOT null
_mayBeLeaf = false;
_currToken = JsonToken.VALUE_STRING;
return (_currText = "");
}
_currToken = _parsingContext.inArray() ? JsonToken.END_ARRAY : JsonToken.END_OBJECT;
_parsingContext = _parsingContext.getParent();
_namesToWrap = _parsingContext.getNamesToWrap();
break;
case XmlTokenStream.XML_ATTRIBUTE_NAME:
// If there was a chance of leaf node, no more...
if (_mayBeLeaf) {
_mayBeLeaf = false;
_nextToken = JsonToken.FIELD_NAME;
_currText = _xmlTokens.getText();
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
_currToken = JsonToken.START_OBJECT;
} else {
_parsingContext.setCurrentName(_xmlTokens.getLocalName());
_currToken = JsonToken.FIELD_NAME;
}
break;
case XmlTokenStream.XML_ATTRIBUTE_VALUE:
_currToken = JsonToken.VALUE_STRING;
return (_currText = _xmlTokens.getText());
case XmlTokenStream.XML_TEXT:
_currText = _xmlTokens.getText();
if (_mayBeLeaf) {
_mayBeLeaf = false;
// Also: must skip following END_ELEMENT
try {
_xmlTokens.skipEndElement();
} catch (XMLStreamException e) {
StaxUtil.throwAsParseException(e, this);
}
// NOTE: this is different from nextToken() -- NO work-around
// for otherwise empty List/array
_currToken = JsonToken.VALUE_STRING;
return _currText;
}
// If not a leaf, need to transform into property...
_parsingContext.setCurrentName(_cfgNameForTextElement);
_nextToken = JsonToken.VALUE_STRING;
_currToken = JsonToken.FIELD_NAME;
break;
case XmlTokenStream.XML_END:
_currToken = null;
default:
return _internalErrorUnknownToken(token);
}
return null;
}
private void _updateState(JsonToken t)
{
switch (t) {
case START_OBJECT:
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
break;
case START_ARRAY:
_parsingContext = _parsingContext.createChildArrayContext(-1, -1);
break;
case END_OBJECT:
case END_ARRAY:
_parsingContext = _parsingContext.getParent();
_namesToWrap = _parsingContext.getNamesToWrap();
break;
case FIELD_NAME:
_parsingContext.setCurrentName(_xmlTokens.getLocalName());
break;
default:
_internalErrorUnknownToken(t);
}
}
/*
/**********************************************************
/* Public API, access to token information, text
/**********************************************************
*/
@Override
public String getText() throws IOException
{
if (_currToken == null) {
return null;
}
switch (_currToken) {
case FIELD_NAME:
return getCurrentName();
case VALUE_STRING:
return _currText;
default:
return _currToken.asString();
}
}
// @since 2.1
@Override
public final String getValueAsString() throws IOException {
return getValueAsString(null);
}
@Override
public String getValueAsString(String defValue) throws IOException
{
JsonToken t = _currToken;
if (t == null) {
return null;
}
switch (t) {
case FIELD_NAME:
return getCurrentName();
case VALUE_STRING:
return _currText;
case START_OBJECT:
// the interesting case; may be able to convert certain kinds of
// elements (specifically, ones with attributes, CDATA only content)
// into VALUE_STRING
try {
String str = _xmlTokens.convertToString();
if (str != null) {
// need to convert token, as well as "undo" START_OBJECT
// note: Should NOT update context, because we will still be getting
// matching END_OBJECT, which will undo contexts properly
_parsingContext = _parsingContext.getParent();
_namesToWrap = _parsingContext.getNamesToWrap();
_currToken = JsonToken.VALUE_STRING;
_nextToken = null;
// One more thing: must explicitly skip the END_OBJECT that would follow
try {
_xmlTokens.skipEndElement();
} catch (XMLStreamException e) {
StaxUtil.throwAsParseException(e, this);
}
return (_currText = str);
}
} catch (XMLStreamException e) {
StaxUtil.throwAsParseException(e, this);
}
return null;
default:
if (_currToken.isScalarValue()) {
return _currToken.asString();
}
}
return defValue;
}
@Override
public char[] getTextCharacters() throws IOException {
String text = getText();
return (text == null) ? null : text.toCharArray();
}
@Override
public int getTextLength() throws IOException {
String text = getText();
return (text == null) ? 0 : text.length();
}
@Override
public int getTextOffset() throws IOException {
return 0;
}
/**
* XML input actually would offer access to character arrays; but since
* we must coalesce things it cannot really be exposed.
*/
@Override
public boolean hasTextCharacters()
{
return false;
}
@Override // since 2.8
public int getText(Writer writer) throws IOException
{
String str = getText();
if (str == null) {
return 0;
}
writer.write(str);
return str.length();
}
/*
/**********************************************************
/* Public API, access to token information, binary
/**********************************************************
*/
@Override
public Object getEmbeddedObject() throws IOException {
// no way to embed POJOs for now...
return null;
}
@Override
public byte[] getBinaryValue(Base64Variant b64variant) throws IOException
{
if (_currToken != JsonToken.VALUE_STRING &&
(_currToken != JsonToken.VALUE_EMBEDDED_OBJECT || _binaryValue == null)) {
_reportError("Current token ("+_currToken+") not VALUE_STRING or VALUE_EMBEDDED_OBJECT, can not access as binary");
}
/* To ensure that we won't see inconsistent data, better clear up
* state...
*/
if (_binaryValue == null) {
try {
_binaryValue = _decodeBase64(b64variant);
} catch (IllegalArgumentException iae) {
throw _constructError("Failed to decode VALUE_STRING as base64 ("+b64variant+"): "+iae.getMessage());
}
}
return _binaryValue;
}
@SuppressWarnings("resource")
protected byte[] _decodeBase64(Base64Variant b64variant) throws IOException
{
ByteArrayBuilder builder = _getByteArrayBuilder();
final String str = getText();
_decodeBase64(str, builder, b64variant);
return builder.toByteArray();
}
/*
/**********************************************************
/* Numeric accessors
/**********************************************************
*/
@Override
public BigInteger getBigIntegerValue() throws IOException {
// TODO Auto-generated method stub
return null;
}
@Override
public BigDecimal getDecimalValue() throws IOException {
// TODO Auto-generated method stub
return null;
}
@Override
public double getDoubleValue() throws IOException {
// TODO Auto-generated method stub
return 0;
}
@Override
public float getFloatValue() throws IOException {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getIntValue() throws IOException {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getLongValue() throws IOException {
// TODO Auto-generated method stub
return 0;
}
@Override
public NumberType getNumberType() throws IOException {
// TODO Auto-generated method stub
return null;
}
@Override
public Number getNumberValue() throws IOException {
// TODO Auto-generated method stub
return null;
}
/*
/**********************************************************
/* Abstract method impls for stuff from JsonParser
/**********************************************************
*/
/**