Skip to content

Commit 181d5b6

Browse files
authored
use _updateToken (#660)
1 parent dd64182 commit 181d5b6

File tree

1 file changed

+80
-74
lines changed

1 file changed

+80
-74
lines changed

src/main/java/com/fasterxml/jackson/dataformat/xml/deser/FromXmlParser.java

Lines changed: 80 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -574,16 +574,22 @@ public JsonLocation getTokenLocation() {
574574
/**
575575
* Since xml representation can not really distinguish between array
576576
* and object starts (both are represented with elements), this method
577-
* is overridden and taken to mean that expecation is that the current
577+
* is overridden and taken to mean that expectation is that the current
578578
* start element is to mean 'start array', instead of default of
579579
* 'start object'.
580+
*
581+
* @throws UncheckedIOException if underlying {@link StreamReadConstraints} constraint fails
580582
*/
581583
@Override
582584
public boolean isExpectedStartArrayToken()
583585
{
584586
JsonToken t = _currToken;
585587
if (t == JsonToken.START_OBJECT) {
586-
_currToken = JsonToken.START_ARRAY;
588+
try {
589+
_updateToken(JsonToken.START_ARRAY);
590+
} catch (StreamConstraintsException e) {
591+
throw new UncheckedIOException(e);
592+
}
587593
// Ok: must replace current context with array as well
588594
_parsingContext.convertToArray();
589595
//System.out.println(" FromXmlParser.isExpectedArrayStart(): OBJ->Array");
@@ -607,6 +613,8 @@ public boolean isExpectedStartArrayToken()
607613
* scalar types (numbers, booleans) -- they are all just Character Data,
608614
* without schema -- we can try to infer type from intent here.
609615
* The main benefit is avoiding checks for coercion.
616+
*
617+
* @throws UncheckedIOException if underlying {@link StreamReadConstraints} constraint fails
610618
*/
611619
@Override
612620
public boolean isExpectedNumberIntToken()
@@ -615,57 +623,57 @@ public boolean isExpectedNumberIntToken()
615623
if (t == JsonToken.VALUE_STRING) {
616624
final String text = _currText.trim();
617625
final int len = _isIntNumber(text);
618-
if (len > 0) {
619-
if (len <= 9) {
620-
_numberInt = NumberInput.parseInt(text);
621-
_numTypesValid = NR_INT;
622-
_currToken = JsonToken.VALUE_NUMBER_INT;
623-
return true;
624-
}
625-
if (len <= 18) { // definitely in long range
626-
long l = NumberInput.parseLong(text);
627-
if (len == 10) {
628-
int asInt = (int) l;
629-
long l2 = (long) asInt;
630-
if (l == l2) {
631-
_numberInt = asInt;
632-
_numTypesValid = NR_INT;
633-
_currToken = JsonToken.VALUE_NUMBER_INT;
634-
return true;
635-
}
636-
}
637-
_numberLong = l;
638-
_numTypesValid = NR_LONG;
639-
_currToken = JsonToken.VALUE_NUMBER_INT;
640-
return true;
641-
}
642-
// Might still fit within `long`
643-
if (len == 19) {
644-
final boolean stillLong;
645-
if (text.charAt(0) == '-') {
646-
stillLong = NumberInput.inLongRange(text.substring(1), true);
647-
} else {
648-
stillLong = NumberInput.inLongRange(text, false);
626+
try {
627+
if (len > 0) {
628+
if (len <= 9) {
629+
_numberInt = NumberInput.parseInt(text);
630+
_numTypesValid = NR_INT;
631+
_updateToken(JsonToken.VALUE_NUMBER_INT);
632+
return true;
649633
}
650-
if (stillLong) {
651-
_numberLong = NumberInput.parseLong(text);
634+
if (len <= 18) { // definitely in long range
635+
long l = NumberInput.parseLong(text);
636+
if (len == 10) {
637+
int asInt = (int) l;
638+
long l2 = (long) asInt;
639+
if (l == l2) {
640+
_numberInt = asInt;
641+
_numTypesValid = NR_INT;
642+
_updateToken(JsonToken.VALUE_NUMBER_INT);
643+
return true;
644+
}
645+
}
646+
_numberLong = l;
652647
_numTypesValid = NR_LONG;
653-
_currToken = JsonToken.VALUE_NUMBER_INT;
648+
_updateToken(JsonToken.VALUE_NUMBER_INT);
654649
return true;
655650
}
656-
}
657-
// finally, need BigInteger
658-
try {
651+
// Might still fit within `long`
652+
if (len == 19) {
653+
final boolean stillLong;
654+
if (text.charAt(0) == '-') {
655+
stillLong = NumberInput.inLongRange(text.substring(1), true);
656+
} else {
657+
stillLong = NumberInput.inLongRange(text, false);
658+
}
659+
if (stillLong) {
660+
_numberLong = NumberInput.parseLong(text);
661+
_numTypesValid = NR_LONG;
662+
_updateToken(JsonToken.VALUE_NUMBER_INT);
663+
return true;
664+
}
665+
}
666+
// finally, need BigInteger
659667
streamReadConstraints().validateIntegerLength(text.length());
660-
} catch (StreamConstraintsException e) {
661-
// Ugh. This method in API ought to expose IOException
662-
throw new UncheckedIOException(e);
663-
}
664-
_numberBigInt = NumberInput.parseBigInteger(
668+
_numberBigInt = NumberInput.parseBigInteger(
665669
text, isEnabled(StreamReadFeature.USE_FAST_BIG_NUMBER_PARSER));
666-
_numTypesValid = NR_BIGINT;
667-
_currToken = JsonToken.VALUE_NUMBER_INT;
668-
return true;
670+
_numTypesValid = NR_BIGINT;
671+
_updateToken(JsonToken.VALUE_NUMBER_INT);
672+
return true;
673+
}
674+
} catch (StreamConstraintsException e) {
675+
// Ugh. This method in API ought to expose IOException
676+
throw new UncheckedIOException(e);
669677
}
670678
}
671679
return (t == JsonToken.VALUE_NUMBER_INT);
@@ -702,8 +710,7 @@ public JsonToken nextToken() throws IOException
702710
_numTypesValid = NR_UNKNOWN;
703711
//System.out.println("FromXmlParser.nextToken0: _nextToken = "+_nextToken);
704712
if (_nextToken != null) {
705-
JsonToken t = _nextToken;
706-
_currToken = t;
713+
final JsonToken t = _updateToken(_nextToken);
707714
_nextToken = null;
708715

709716
switch (t) {
@@ -745,7 +752,7 @@ public JsonToken nextToken() throws IOException
745752
// leave _mayBeLeaf set, as we start a new context
746753
_nextToken = JsonToken.FIELD_NAME;
747754
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
748-
return (_currToken = JsonToken.START_OBJECT);
755+
return _updateToken(JsonToken.START_OBJECT);
749756
}
750757
if (_parsingContext.inArray()) {
751758
// Yup: in array, so this element could be verified; but it won't be
@@ -766,7 +773,7 @@ public JsonToken nextToken() throws IOException
766773
_mayBeLeaf = true;
767774
// Ok: in array context we need to skip reporting field names.
768775
// But what's the best way to find next token?
769-
return (_currToken = JsonToken.FIELD_NAME);
776+
return _updateToken(JsonToken.FIELD_NAME);
770777
}
771778

772779
// Ok; beyond start element, what do we get?
@@ -781,16 +788,16 @@ public JsonToken nextToken() throws IOException
781788
// expose as empty Object, not null
782789
_nextToken = JsonToken.END_OBJECT;
783790
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
784-
return (_currToken = JsonToken.START_OBJECT);
791+
return _updateToken(JsonToken.START_OBJECT);
785792
}
786793
// 07-Sep-2019, tatu: for [dataformat-xml#353], must NOT return second null
787794
if (_currToken != JsonToken.VALUE_NULL) {
788795
// 13-May-2020, tatu: [dataformat-xml#397]: advance `index`
789796
_parsingContext.valueStarted();
790-
return (_currToken = JsonToken.VALUE_NULL);
797+
return _updateToken(JsonToken.VALUE_NULL);
791798
}
792799
}
793-
_currToken = _parsingContext.inArray() ? JsonToken.END_ARRAY : JsonToken.END_OBJECT;
800+
_updateToken(_parsingContext.inArray() ? JsonToken.END_ARRAY : JsonToken.END_OBJECT);
794801
_parsingContext = _parsingContext.getParent();
795802
return _currToken;
796803

@@ -801,15 +808,15 @@ public JsonToken nextToken() throws IOException
801808
_nextToken = JsonToken.FIELD_NAME;
802809
_currText = _xmlTokens.getText();
803810
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
804-
return (_currToken = JsonToken.START_OBJECT);
811+
return _updateToken(JsonToken.START_OBJECT);
805812
}
806813
_parsingContext.setCurrentName(_xmlTokens.getLocalName());
807-
return (_currToken = JsonToken.FIELD_NAME);
814+
return _updateToken(JsonToken.FIELD_NAME);
808815
case XmlTokenStream.XML_ATTRIBUTE_VALUE:
809816
_currText = _xmlTokens.getText();
810817
// 13-May-2020, tatu: [dataformat-xml#397]: advance `index`
811818
_parsingContext.valueStarted();
812-
return (_currToken = JsonToken.VALUE_STRING);
819+
return _updateToken(JsonToken.VALUE_STRING);
813820
case XmlTokenStream.XML_TEXT:
814821
_currText = _xmlTokens.getText();
815822
if (_mayBeLeaf) {
@@ -831,10 +838,10 @@ public JsonToken nextToken() throws IOException
831838
// be done, by swallowing the token)
832839
_nextToken = JsonToken.END_OBJECT;
833840
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
834-
return (_currToken = JsonToken.START_OBJECT);
841+
return _updateToken(JsonToken.START_OBJECT);
835842
}
836843
}
837-
return (_currToken = JsonToken.VALUE_STRING);
844+
return _updateToken(JsonToken.VALUE_STRING);
838845
}
839846
if (token != XmlTokenStream.XML_START_ELEMENT) {
840847
throw new JsonParseException(this, String.format(
@@ -856,7 +863,7 @@ public JsonToken nextToken() throws IOException
856863
// along is not enough.
857864
_nextIsLeadingMixed = true;
858865
_nextToken = JsonToken.FIELD_NAME;
859-
return (_currToken = JsonToken.START_OBJECT);
866+
return _updateToken(JsonToken.START_OBJECT);
860867
} else if (XmlTokenStream._allWs(_currText)) {
861868
token = _nextToken();
862869
continue;
@@ -885,9 +892,9 @@ public JsonToken nextToken() throws IOException
885892
// If not a leaf (or otherwise ignorable), need to transform into property...
886893
_parsingContext.setCurrentName(_cfgNameForTextElement);
887894
_nextToken = JsonToken.VALUE_STRING;
888-
return (_currToken = JsonToken.FIELD_NAME);
895+
return _updateToken(JsonToken.FIELD_NAME);
889896
case XmlTokenStream.XML_END:
890-
return (_currToken = null);
897+
return _updateToken(null);
891898
default:
892899
return _internalErrorUnknownToken(token);
893900
}
@@ -919,8 +926,7 @@ public String nextTextValue() throws IOException
919926
{
920927
_binaryValue = null;
921928
if (_nextToken != null) {
922-
JsonToken t = _nextToken;
923-
_currToken = t;
929+
final JsonToken t = _updateToken(_nextToken);
924930
_nextToken = null;
925931

926932
// expected case; yes, got a String
@@ -940,7 +946,7 @@ public String nextTextValue() throws IOException
940946
if (_mayBeLeaf) {
941947
_nextToken = JsonToken.FIELD_NAME;
942948
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
943-
_currToken = JsonToken.START_OBJECT;
949+
_updateToken(JsonToken.START_OBJECT);
944950
return null;
945951
}
946952
if (_parsingContext.inArray()) {
@@ -955,7 +961,7 @@ public String nextTextValue() throws IOException
955961
_xmlTokens.repeatStartElement();
956962
}
957963
_mayBeLeaf = true;
958-
_currToken = JsonToken.FIELD_NAME;
964+
_updateToken(JsonToken.FIELD_NAME);
959965
return null;
960966
}
961967

@@ -969,12 +975,12 @@ public String nextTextValue() throws IOException
969975
// asked text value -- but that seems incorrect. Hoping this won't
970976
// break anything in 2.15+
971977

972-
_currToken = JsonToken.VALUE_NULL;
978+
_updateToken(JsonToken.VALUE_NULL);
973979
// 13-May-2020, tatu: [dataformat-xml#397]: advance `index`
974980
_parsingContext.valueStarted();
975981
return (_currText = null);
976982
}
977-
_currToken = _parsingContext.inArray() ? JsonToken.END_ARRAY : JsonToken.END_OBJECT;
983+
_updateToken(_parsingContext.inArray() ? JsonToken.END_ARRAY : JsonToken.END_OBJECT);
978984
_parsingContext = _parsingContext.getParent();
979985
break;
980986
case XmlTokenStream.XML_ATTRIBUTE_NAME:
@@ -984,14 +990,14 @@ public String nextTextValue() throws IOException
984990
_nextToken = JsonToken.FIELD_NAME;
985991
_currText = _xmlTokens.getText();
986992
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
987-
_currToken = JsonToken.START_OBJECT;
993+
_updateToken(JsonToken.START_OBJECT);
988994
} else {
989995
_parsingContext.setCurrentName(_xmlTokens.getLocalName());
990-
_currToken = JsonToken.FIELD_NAME;
996+
_updateToken(JsonToken.FIELD_NAME);
991997
}
992998
break;
993999
case XmlTokenStream.XML_ATTRIBUTE_VALUE:
994-
_currToken = JsonToken.VALUE_STRING;
1000+
_updateToken(JsonToken.VALUE_STRING);
9951001
// 13-May-2020, tatu: [dataformat-xml#397]: advance `index`
9961002
_parsingContext.valueStarted();
9971003
return (_currText = _xmlTokens.getText());
@@ -1005,16 +1011,16 @@ public String nextTextValue() throws IOException
10051011
// for otherwise empty List/array
10061012
// 13-May-2020, tatu: [dataformat-xml#397]: advance `index`
10071013
_parsingContext.valueStarted();
1008-
_currToken = JsonToken.VALUE_STRING;
1014+
_updateToken(JsonToken.VALUE_STRING);
10091015
return _currText;
10101016
}
10111017
// If not a leaf, need to transform into property...
10121018
_parsingContext.setCurrentName(_cfgNameForTextElement);
10131019
_nextToken = JsonToken.VALUE_STRING;
1014-
_currToken = JsonToken.FIELD_NAME;
1020+
_updateToken(JsonToken.FIELD_NAME);
10151021
break;
10161022
case XmlTokenStream.XML_END:
1017-
_currToken = null;
1023+
_updateTokenToNull();
10181024
default:
10191025
return _internalErrorUnknownToken(token);
10201026
}

0 commit comments

Comments
 (0)