From 6eda875e8ba7eab24d12c2ad632f34289b02201d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98istein=20L=C3=B8vik?= Date: Thu, 22 Dec 2016 11:03:16 +0100 Subject: [PATCH] Fix issue #162 --- .../dataformat/xml/deser/XmlTokenStream.java | 18 ++++++++++-------- .../TestStringValues162.java | 10 +++++++++- 2 files changed, 19 insertions(+), 9 deletions(-) rename src/test/java/com/fasterxml/jackson/dataformat/xml/{failing => deser}/TestStringValues162.java (87%) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlTokenStream.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlTokenStream.java index 5e038960b..3789662ad 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlTokenStream.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlTokenStream.java @@ -371,21 +371,23 @@ private final int _next() throws XMLStreamException private final String _collectUntilTag() throws XMLStreamException { - String text = null; + if (_xmlReader.isEmptyElement()) { + _xmlReader.next(); + return null; + } + + StringBuilder text = new StringBuilder(); + while (true) { switch (_xmlReader.next()) { case XMLStreamConstants.START_ELEMENT: case XMLStreamConstants.END_ELEMENT: case XMLStreamConstants.END_DOCUMENT: - return text; - // note: SPACE is ignorable (and seldom seen), not to be included + return text.toString(); + // note: SPACE is ignorable (and seldom seen), not to be included case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.CDATA: - if (text == null) { - text = _xmlReader.getText(); - } else { // can be optimized in future, if need be: - text += _xmlReader.getText(); - } + text.append(_xmlReader.getText()); break; default: // any other type (proc instr, comment etc) is just ignored diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/failing/TestStringValues162.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/deser/TestStringValues162.java similarity index 87% rename from src/test/java/com/fasterxml/jackson/dataformat/xml/failing/TestStringValues162.java rename to src/test/java/com/fasterxml/jackson/dataformat/xml/deser/TestStringValues162.java index 3db80728a..83f655ef6 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/failing/TestStringValues162.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/deser/TestStringValues162.java @@ -1,4 +1,4 @@ -package com.fasterxml.jackson.dataformat.xml.failing; +package com.fasterxml.jackson.dataformat.xml.deser; import java.util.ArrayList; import java.util.List; @@ -40,6 +40,14 @@ public void testEmptyString162() throws Exception assertEquals("", name.last); } + public void testEmptyElement() throws Exception + { + Name name = MAPPER.readValue("", Name.class); + assertNotNull(name); + assertNull(name.first); + assertEquals("", name.last); + } + public void testEmptyStringElement() throws Exception { // then with empty element