Skip to content

Commit c92a97f

Browse files
committed
Minor improvements to error handling, trying to address #226 (eventually)
1 parent 8b6169b commit c92a97f

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
@@ -639,11 +639,7 @@ public JsonToken nextToken() throws IOException
639639
// we had an empty String (or all white space), and we are
640640
// deserializing an array, we better hide the empty text.
641641
// Also: must skip following END_ELEMENT
642-
try {
643-
_xmlTokens.skipEndElement();
644-
} catch (XMLStreamException e) {
645-
StaxUtil.throwAsParseException(e, this);
646-
}
642+
_skipEndElement();
647643
if (_parsingContext.inArray()) {
648644
if (XmlTokenStream._allWs(_currText)) {
649645
// 06-Jan-2015, tatu: as per [dataformat-xml#180], need to
@@ -793,11 +789,7 @@ public String nextTextValue() throws IOException
793789
if (_mayBeLeaf) {
794790
_mayBeLeaf = false;
795791
// Also: must skip following END_ELEMENT
796-
try {
797-
_xmlTokens.skipEndElement();
798-
} catch (XMLStreamException e) {
799-
StaxUtil.throwAsParseException(e, this);
800-
}
792+
_skipEndElement();
801793
// NOTE: this is different from nextToken() -- NO work-around
802794
// for otherwise empty List/array
803795
// 13-May-2020, tatu: [dataformat-xml#397]: advance `index`
@@ -894,11 +886,7 @@ public String getValueAsString(String defValue) throws IOException
894886
_currToken = JsonToken.VALUE_STRING;
895887
_nextToken = null;
896888
// One more thing: must explicitly skip the END_OBJECT that would follow
897-
try {
898-
_xmlTokens.skipEndElement();
899-
} catch (XMLStreamException e) {
900-
StaxUtil.throwAsParseException(e, this);
901-
}
889+
_skipEndElement();
902890
return (_currText = str);
903891
}
904892
} catch (XMLStreamException e) {
@@ -1094,7 +1082,17 @@ protected ByteArrayBuilder _getByteArrayBuilder()
10941082
return _byteArrayBuilder;
10951083
}
10961084

1097-
private <T> T _internalErrorUnknownToken(Object token) {
1085+
private <T> T _internalErrorUnknownToken(Object token) {
10981086
throw new IllegalStateException("Internal error: unrecognized XmlTokenStream token: "+token);
10991087
}
1088+
1089+
protected void _skipEndElement() throws IOException {
1090+
try {
1091+
_xmlTokens.skipEndElement();
1092+
} catch (XMLStreamException e) {
1093+
StaxUtil.throwAsParseException(e, this);
1094+
} catch (Exception e) {
1095+
throw new JsonParseException(this, e.getMessage(), e);
1096+
}
1097+
}
11001098
}

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
@@ -11,7 +11,10 @@ public class MixedContentTreeRead226Test extends XmlTestBase
1111

1212
public void testMixed226() throws Exception
1313
{
14-
final String XML = "<root>\n<a>lorem <b>ipsum</b> dolor</a>\n</root>";
14+
final String XML = "<root>\n"
15+
+"<a>lorem <b>ipsum</b>\n"
16+
+"dolor</a>\n"
17+
+"</root>";
1518
JsonNode root = MAPPER.readTree(XML);
1619
assertNotNull(root);
1720
}

0 commit comments

Comments
 (0)