Skip to content

Commit 492ba5c

Browse files
cowtowncoderalex-bel-apica
authored andcommitted
# Conflicts: # release-notes/VERSION-2.x # src/test/java/com/fasterxml/jackson/dataformat/xml/failing/MixedContentTreeRead226Test.java
1 parent 4760f8a commit 492ba5c

File tree

4 files changed

+62
-34
lines changed

4 files changed

+62
-34
lines changed

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

+25-10
Original file line numberDiff line numberDiff line change
@@ -592,18 +592,33 @@ public JsonToken nextToken() throws IOException
592592
// we had an empty String (or all white space), and we are
593593
// deserializing an array, we better hide the empty text.
594594
// Also: must skip following END_ELEMENT
595-
_skipEndElement();
596-
if (_parsingContext.inArray()) {
597-
if (XmlTokenStream._allWs(_currText)) {
598-
// 06-Jan-2015, tatu: as per [dataformat-xml#180], need to
599-
// expose as empty Object, not null (or, worse, as used to
600-
// be done, by swallowing the token)
601-
_nextToken = JsonToken.END_OBJECT;
602-
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
603-
return (_currToken = JsonToken.START_OBJECT);
595+
// 05-Jun-2020, tatu: ... if there is one; we may actually alternatively
596+
// get START_ELEMENT for "mixed content" case; if so, need to change to
597+
// expose "XmlText" as separate property
598+
token = _nextToken();
599+
600+
if (token == XmlTokenStream.XML_END_ELEMENT) {
601+
if (_parsingContext.inArray()) {
602+
if (XmlTokenStream._allWs(_currText)) {
603+
// 06-Jan-2015, tatu: as per [dataformat-xml#180], need to
604+
// expose as empty Object, not null (or, worse, as used to
605+
// be done, by swallowing the token)
606+
_nextToken = JsonToken.END_OBJECT;
607+
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
608+
return (_currToken = JsonToken.START_OBJECT);
609+
}
604610
}
611+
return (_currToken = JsonToken.VALUE_STRING);
605612
}
606-
return (_currToken = JsonToken.VALUE_STRING);
613+
if (token != XmlTokenStream.XML_START_ELEMENT) {
614+
throw new JsonParseException(this, String.format(
615+
"Internal error: Expected END_ELEMENT (%d) or START_ELEMENT (%d), got event of type %d",
616+
XmlTokenStream.XML_END_ELEMENT, XmlTokenStream.XML_START_ELEMENT, token));
617+
}
618+
// fall-through, except must create new context AND push back
619+
// START_ELEMENT we just saw:
620+
_xmlTokens.pushbackCurrentToken();
621+
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
607622
}
608623
// [dataformat-xml#177]: empty text may also need to be skipped
609624
// but... [dataformat-xml#191]: looks like we can't short-cut, must

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

+25-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,16 @@ public class XmlTokenStream
9797
protected String _namespaceURI;
9898

9999
protected String _textValue;
100-
100+
101+
/**
102+
* Marker flag set if caller wants to "push back" current token so
103+
* that next call to {@link #next()} should simply be given what was
104+
* already read.
105+
*
106+
* @since 2.12
107+
*/
108+
protected boolean _repeatCurrentToken;
109+
101110
/*
102111
/**********************************************************************
103112
/* State for handling virtual wrapping
@@ -203,6 +212,10 @@ private String _loc() {
203212
// public int next0() throws XMLStreamException
204213
public int next() throws XMLStreamException
205214
{
215+
if (_repeatCurrentToken) {
216+
_repeatCurrentToken = false;
217+
return _currentState;
218+
}
206219
if (_repeatElement != 0) {
207220
return (_currentState = _handleRepeatElement());
208221
}
@@ -292,6 +305,17 @@ protected void repeatStartElement()
292305
_repeatElement = REPLAY_START_DUP;
293306
}
294307

308+
/**
309+
* Method that can be called to ask stream to literally just return current token
310+
* with the next call to {@link #next()}, without more work.
311+
*
312+
* @since 2.12
313+
*/
314+
protected void pushbackCurrentToken()
315+
{
316+
_repeatCurrentToken = true;
317+
}
318+
295319
/**
296320
* Method called to skip any attributes current START_ELEMENT may have,
297321
* so that they are not returned as token.

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

+12
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,16 @@ public void testMultipleMixedContent() throws Exception
3939
assertEquals(JSON_MAPPER.readTree(a2q("{'':['first','second','third'],'a':1,'b':'2','':'after'}")),
4040
XML_MAPPER.readTree("<root>first<a>1</a>second<b>2</b>this</root>"));
4141
}
42+
43+
// [dataformat-xml#226]
44+
public void testMixed226() throws Exception
45+
{
46+
final String XML = "<root>\n"
47+
+"<a>mixed1 <b>leaf</b>"
48+
+" mixed2</a>\n"
49+
+"</root>";
50+
assertEquals(JSON_MAPPER.readTree(
51+
a2q("{'a':{'':['mixed1 ',' mixed2'],'b':'leaf'}}")),
52+
XML_MAPPER.readTree(XML));
53+
}
4254
}

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

-23
This file was deleted.

0 commit comments

Comments
 (0)