From 85b98bb18ba2b0f37fc30be49b97cbcad4bd8265 Mon Sep 17 00:00:00 2001 From: "Gerlach, Winfried" Date: Fri, 9 Oct 2026 23:39:55 +0200 Subject: [PATCH] Fix #60: Stax2ReaderAdapter.nextTag() has to be overridden for correct depth and partial chunked reads --- release-notes/VERSION | 7 +- .../codehaus/stax2/ri/Stax2ReaderAdapter.java | 25 +++- .../stax2/ri/Stax2ReaderAdapterTest.java | 109 ++++++++++++++++++ 3 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/codehaus/stax2/ri/Stax2ReaderAdapterTest.java diff --git a/release-notes/VERSION b/release-notes/VERSION index d5c3899..4547dcb 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -4,7 +4,12 @@ Project: Stax2 API = Releases ------------------------------------------------------------------------ -4.3.2 (not yet released) +4.3.3 (not yet released) + +#60: `Stax2ReaderAdapter.nextTag()` skips the adapter's own state handling + (contributed by @winfriedgerlach) + +4.3.2 (2026-10-09) #51: Fast path for BigInteger decoding (contributed by @winfriedgerlach) diff --git a/src/main/java/org/codehaus/stax2/ri/Stax2ReaderAdapter.java b/src/main/java/org/codehaus/stax2/ri/Stax2ReaderAdapter.java index 56e83c9..6cb1b99 100644 --- a/src/main/java/org/codehaus/stax2/ri/Stax2ReaderAdapter.java +++ b/src/main/java/org/codehaus/stax2/ri/Stax2ReaderAdapter.java @@ -15,7 +15,6 @@ import org.codehaus.stax2.typed.TypedArrayDecoder; import org.codehaus.stax2.typed.TypedValueDecoder; import org.codehaus.stax2.typed.TypedXMLStreamException; -import org.codehaus.stax2.ri.Stax2Util; import org.codehaus.stax2.ri.typed.StringBase64Decoder; import org.codehaus.stax2.ri.typed.ValueDecoderFactory; @@ -135,6 +134,30 @@ public int next() throws XMLStreamException return type; } + /** + * Needs to be overridden along with {@link #next}, since the default + * implementation calls {@code nextTag()} of the wrapped reader directly, + * bypassing the chunked decode check and depth tracking done there. + */ + @Override + public int nextTag() throws XMLStreamException + { + // As with next(): if in the middle of chunked decode, just end it + if (_typedContent != null) { + _typedContent = null; + return XMLStreamConstants.END_ELEMENT; + } + + // Tags are all we can get; anything skipped does not affect depth + int type = super.nextTag(); + if (type == XMLStreamConstants.START_ELEMENT) { + ++_depth; + } else if (type == XMLStreamConstants.END_ELEMENT) { + --_depth; + } + return type; + } + @Override public String getElementText() throws XMLStreamException { diff --git a/src/test/java/org/codehaus/stax2/ri/Stax2ReaderAdapterTest.java b/src/test/java/org/codehaus/stax2/ri/Stax2ReaderAdapterTest.java new file mode 100644 index 0000000..f338a6e --- /dev/null +++ b/src/test/java/org/codehaus/stax2/ri/Stax2ReaderAdapterTest.java @@ -0,0 +1,109 @@ +package org.codehaus.stax2.ri; + +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; + +import org.junit.jupiter.api.Test; + +import org.codehaus.stax2.XMLStreamReader2; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests for state {@link Stax2ReaderAdapter} keeps on top of the wrapped reader, which has to be updated by every + * method that advances it. + *

+ * Covers {@code nextTag()}, and leaving a chunked typed array read early, where the wrapped reader is already at + * END_ELEMENT and the depth must not be decremented a second time. + */ +class Stax2ReaderAdapterTest +{ + // White space, comment and PI between tags, for nextTag() to skip + private static final String XML = " \n "; + + // "" for START_ELEMENT, "" for END_ELEMENT, each with getDepth() + private static final List EXPECTED = Arrays.asList( + " 1", " 2", " 3", " 3", " 2", " 2", " 2", " 1"); + + // Element "a" is only partially read as an int array before moving on + private static final String XML_ABANDONED = "1 2 3 4 5 "; + + private static final List EXPECTED_ABANDONED = Arrays.asList( + " 2", " 2", " 2", " 1"); + + @Test + void depthWithNext() throws Exception + { + assertEquals(EXPECTED, walk(false)); + } + + @Test + void depthWithNextTag() throws Exception + { + assertEquals(EXPECTED, walk(true)); + } + + @Test + void depthAfterAbandonedChunkedReadWithNext() throws Exception + { + assertEquals(EXPECTED_ABANDONED, walkAbandoned(false)); + } + + @Test + void depthAfterAbandonedChunkedReadWithNextTag() throws Exception + { + assertEquals(EXPECTED_ABANDONED, walkAbandoned(true)); + } + + /* + /////////////////////////////////////////// + // Helper methods + /////////////////////////////////////////// + */ + + private static List walk(boolean useNextTag) throws XMLStreamException + { + // not wrapIfNecessary(): must be the adapter even if a Stax2 impl is on classpath + XMLStreamReader2 sr = new Stax2ReaderAdapter(XMLInputFactory.newInstance() + .createXMLStreamReader(new StringReader(XML))); + List result = new ArrayList<>(); + for (int i = 0; i < EXPECTED.size(); ) { + int type = useNextTag ? sr.nextTag() : sr.next(); + if (type == XMLStreamConstants.START_ELEMENT) { + result.add("<"+sr.getLocalName()+"> "+sr.getDepth()); + ++i; + } else if (type == XMLStreamConstants.END_ELEMENT) { + result.add(" "+sr.getDepth()); + ++i; + } + } + return result; + } + + private static List walkAbandoned(boolean useNextTag) throws XMLStreamException + { + XMLStreamReader2 sr = new Stax2ReaderAdapter(XMLInputFactory.newInstance() + .createXMLStreamReader(new StringReader(XML_ABANDONED))); + sr.nextTag(); + sr.nextTag(); + assertEquals(2, sr.readElementAsIntArray(new int[2], 0, 2)); + List result = new ArrayList<>(); + for (int i = 0; i < EXPECTED_ABANDONED.size(); ) { + int type = useNextTag ? sr.nextTag() : sr.next(); + if (type == XMLStreamConstants.START_ELEMENT) { + result.add("<"+sr.getLocalName()+"> "+sr.getDepth()); + ++i; + } else if (type == XMLStreamConstants.END_ELEMENT) { + result.add(" "+sr.getDepth()); + ++i; + } + } + return result; + } +}