diff --git a/release-notes/VERSION b/release-notes/VERSION index 3c1e7f0..fdb21ea 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -8,6 +8,8 @@ Project: Stax2 API #58: `TextBuffer` switch to `StringBuilder` for better performance (contributed by @winfriedgerlach) +#60: `Stax2ReaderAdapter.nextTag()` skips the adapter's own state handling + (contributed by @winfriedgerlach) 4.3.2 (2026-10-09) 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 = "