Skip to content

Commit 38ffd8a

Browse files
committed
minor improvement to error reporting
1 parent 67013f3 commit 38ffd8a

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

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

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
* actual higher-level conversion to JSON tokens.
2020
*<p>
2121
* Beyond initial idea there are also couple of other detours like ability
22-
* to "replay" some tokens, add virtual wrappers (ironically to support "unwrapped"
22+
* to "replay" some tokens, add virtual wrappers (ironically to support "unwrapped"_currentStateDesc
2323
* array values), and to unroll "Objects" into String values in some cases.
2424
*/
2525
public class XmlTokenStream
2626
{
2727
// // // main token states:
28-
28+
2929
public final static int XML_START_ELEMENT = 1;
3030
public final static int XML_END_ELEMENT = 2;
3131
public final static int XML_ATTRIBUTE_NAME = 3;
@@ -227,8 +227,8 @@ public void skipEndElement() throws IOException, XMLStreamException
227227
int type = next();
228228
if (type != XML_END_ELEMENT) {
229229
throw new IOException(String.format(
230-
"Internal error: Expected END_ELEMENT (%d), got event of type %d",
231-
XML_END_ELEMENT, type));
230+
"Internal error: Expected END_ELEMENT, got event of type %s",
231+
_stateDesc(type)));
232232
}
233233
}
234234

@@ -284,16 +284,15 @@ public JsonLocation getTokenLocation() {
284284
*/
285285
protected void repeatStartElement()
286286
{
287-
//System.out.println(" -> repeatStartElement for "+_localName+", _currentWrapper was: "+_currentWrapper);
287+
//System.out.println(" XmlTokenStream.repeatStartElement() for <"+_localName+">, _currentWrapper was: "+_currentWrapper);
288288
// sanity check: can only be used when just returned START_ELEMENT:
289289
if (_currentState != XML_START_ELEMENT) {
290290
// 14-May-2020, tatu: Looks like we DO end up here with empty Lists; if so,
291291
// should NOT actually wrap.
292292
if (_currentState == XML_END_ELEMENT) {
293293
return;
294294
}
295-
throw new IllegalStateException("Current state not XML_START_ELEMENT ("
296-
+XML_START_ELEMENT+") but "+_currentState);
295+
throw new IllegalStateException("Current state not XML_START_ELEMENT but "+_currentStateDesc());
297296
}
298297
// Important: add wrapper, to keep track...
299298
if (_currentWrapper == null) {
@@ -324,20 +323,20 @@ protected void pushbackCurrentToken()
324323
*/
325324
protected void skipAttributes()
326325
{
326+
//System.out.println(" XmlTokenStream.skipAttributes(), state: "+_currentStateDesc());
327327
if (_currentState == XML_ATTRIBUTE_NAME) {
328328
_attributeCount = 0;
329329
_currentState = XML_START_ELEMENT;
330330
} else if (_currentState == XML_START_ELEMENT) {
331-
/* 06-Jan-2012, tatu: As per [#47] it looks like we should NOT do anything
332-
* in this particular case, because it occurs when original element had
333-
* no attributes and we now point to the first child element.
334-
*/
331+
// 06-Jan-2012, tatu: As per [#47] it looks like we should NOT do anything
332+
// in this particular case, because it occurs when original element had
333+
// no attributes and we now point to the first child element.
335334
// _attributeCount = 0;
336335
} else if (_currentState == XML_TEXT) {
337336
; // nothing to do... is it even legal?
338337
} else {
339-
throw new IllegalStateException("Current state not XML_START_ELEMENT or XML_ATTRIBUTE_NAME ("
340-
+XML_START_ELEMENT+") but "+_currentState);
338+
throw new IllegalStateException(
339+
"Current state not XML_START_ELEMENT or XML_ATTRIBUTE_NAME but "+_currentStateDesc());
341340
}
342341
}
343342

@@ -347,6 +346,7 @@ protected void skipAttributes()
347346
*/
348347
protected String convertToString() throws XMLStreamException
349348
{
349+
//System.out.println(" XmlTokenStream.convertToString(), state: "+_currentStateDesc());
350350
// only applicable to cases where START_OBJECT was induced by attributes
351351
if (_currentState != XML_ATTRIBUTE_NAME || _nextAttributeIndex != 0) {
352352
return null;
@@ -381,6 +381,7 @@ protected String convertToString() throws XMLStreamException
381381

382382
private final int _next() throws XMLStreamException
383383
{
384+
//System.out.println(" XmlTokenStream._next(), state: "+_currentStateDesc());
384385
switch (_currentState) {
385386
case XML_ATTRIBUTE_VALUE:
386387
++_nextAttributeIndex;
@@ -747,7 +748,30 @@ protected static boolean _allWs(String str)
747748
return true;
748749
}
749750

751+
protected String _currentStateDesc() {
752+
return _stateDesc(_currentState);
753+
}
754+
755+
protected String _stateDesc(int state) {
756+
switch (state) {
757+
case XML_START_ELEMENT:
758+
return "XML_START_ELEMENT";
759+
case XML_END_ELEMENT:
760+
return "XML_END_ELEMENT";
761+
case XML_ATTRIBUTE_NAME:
762+
return "XML_ATTRIBUTE_NAME";
763+
case XML_ATTRIBUTE_VALUE:
764+
return "XML_ATTRIBUTE_VALUE";
765+
case XML_TEXT:
766+
return "XML_TEXT";
767+
case XML_END:
768+
return "XML_END";
769+
}
770+
return "N/A ("+_currentState+")";
771+
}
772+
750773
// for DEBUGGING
774+
/*
751775
@Override
752776
public String toString()
753777
{
@@ -756,4 +780,5 @@ public String toString()
756780
_currentState, _attributeCount, _nextAttributeIndex,
757781
_localName, _textValue, _repeatElement, _currentWrapper, _repeatElement, _nextLocalName);
758782
}
783+
*/
759784
}

0 commit comments

Comments
 (0)