Skip to content

Commit 8d67e50

Browse files
cowtowncoderalex-bel-apica
authored andcommitted
Minor improvements to error handling, trying to address FasterXML#226 (eventually)
1 parent 6751e94 commit 8d67e50

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

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

+14-16
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,7 @@ public JsonToken nextToken() throws IOException
601601
// we had an empty String (or all white space), and we are
602602
// deserializing an array, we better hide the empty text.
603603
// Also: must skip following END_ELEMENT
604-
try {
605-
_xmlTokens.skipEndElement();
606-
} catch (XMLStreamException e) {
607-
StaxUtil.throwAsParseException(e, this);
608-
}
604+
_skipEndElement();
609605
if (_parsingContext.inArray()) {
610606
if (XmlTokenStream._allWs(_currText)) {
611607
// 06-Jan-2015, tatu: as per [dataformat-xml#180], need to
@@ -755,11 +751,7 @@ public String nextTextValue() throws IOException
755751
if (_mayBeLeaf) {
756752
_mayBeLeaf = false;
757753
// Also: must skip following END_ELEMENT
758-
try {
759-
_xmlTokens.skipEndElement();
760-
} catch (XMLStreamException e) {
761-
StaxUtil.throwAsParseException(e, this);
762-
}
754+
_skipEndElement();
763755
// NOTE: this is different from nextToken() -- NO work-around
764756
// for otherwise empty List/array
765757
// 13-May-2020, tatu: [dataformat-xml#397]: advance `index`
@@ -856,11 +848,7 @@ public String getValueAsString(String defValue) throws IOException
856848
_currToken = JsonToken.VALUE_STRING;
857849
_nextToken = null;
858850
// One more thing: must explicitly skip the END_OBJECT that would follow
859-
try {
860-
_xmlTokens.skipEndElement();
861-
} catch (XMLStreamException e) {
862-
StaxUtil.throwAsParseException(e, this);
863-
}
851+
_skipEndElement();
864852
return (_currText = str);
865853
}
866854
} catch (XMLStreamException e) {
@@ -1056,7 +1044,17 @@ protected ByteArrayBuilder _getByteArrayBuilder()
10561044
return _byteArrayBuilder;
10571045
}
10581046

1059-
private <T> T _internalErrorUnknownToken(Object token) {
1047+
private <T> T _internalErrorUnknownToken(Object token) {
10601048
throw new IllegalStateException("Internal error: unrecognized XmlTokenStream token: "+token);
10611049
}
1050+
1051+
protected void _skipEndElement() throws IOException {
1052+
try {
1053+
_xmlTokens.skipEndElement();
1054+
} catch (XMLStreamException e) {
1055+
StaxUtil.throwAsParseException(e, this);
1056+
} catch (Exception e) {
1057+
throw new JsonParseException(this, e.getMessage(), e);
1058+
}
1059+
}
10621060
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ public void skipEndElement() throws IOException, XMLStreamException
213213
{
214214
int type = next();
215215
if (type != XML_END_ELEMENT) {
216-
throw new IOException("Expected END_ELEMENT, got event of type "+type);
216+
throw new IOException(String.format(
217+
"Internal error: Expected END_ELEMENT (%d), got event of type %d",
218+
XML_END_ELEMENT, type));
217219
}
218220
}
219221

src/test/java/com/fasterxml/jackson/dataformat/xml/failing/MixedContentTreeRead226Test.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ public class MixedContentTreeRead226Test extends XmlTestBase
1313

1414
public void testMixed226() throws Exception
1515
{
16-
final String XML = "<root>\n<a>lorem <b>ipsum</b> dolor</a>\n</root>";
16+
final String XML = "<root>\n"
17+
+"<a>lorem <b>ipsum</b>\n"
18+
+"dolor</a>\n"
19+
+"</root>";
1720
JsonNode root = MAPPER.readTree(XML);
1821
assertNotNull(root);
1922
}

0 commit comments

Comments
 (0)