Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
25 changes: 24 additions & 1 deletion src/main/java/org/codehaus/stax2/ri/Stax2ReaderAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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

@winfriedgerlach winfriedgerlach Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very similar to next() method right above now

{
// 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
{
Expand Down
109 changes: 109 additions & 0 deletions src/test/java/org/codehaus/stax2/ri/Stax2ReaderAdapterTest.java
Original file line number Diff line number Diff line change
@@ -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.
*<p>
* 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 = "<root> <a><!-- c --><b/></a>\n<?pi?><c></c> </root>";

// "<name>" for START_ELEMENT, "</name>" for END_ELEMENT, each with getDepth()
private static final List<String> EXPECTED = Arrays.asList(
"<root> 1", "<a> 2", "<b> 3", "</b> 3", "</a> 2", "<c> 2", "</c> 2", "</root> 1");

// Element "a" is only partially read as an int array before moving on
private static final String XML_ABANDONED = "<root><a>1 2 3 4 5</a> <b/></root>";

private static final List<String> EXPECTED_ABANDONED = Arrays.asList(
"</a> 2", "<b> 2", "</b> 2", "</root> 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<String> 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<String> 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.getLocalName()+"> "+sr.getDepth());
++i;
}
}
return result;
}

private static List<String> 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<String> 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.getLocalName()+"> "+sr.getDepth());
++i;
}
}
return result;
}
}
Loading