From ca746c05a4d77f4dd93583c6d901d096268318ee Mon Sep 17 00:00:00 2001 From: JFronny Date: Sat, 20 Apr 2024 22:15:37 +0200 Subject: [PATCH] fix(serialize-xml): Actually consume header --- .../commons/serialize/xml/NativeXmlReader.java | 4 ++++ .../serialize/xml/test/NativeXmlReaderTest.java | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/NativeXmlReader.java b/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/NativeXmlReader.java index 04e1d5f..7c5bd4e 100644 --- a/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/NativeXmlReader.java +++ b/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/NativeXmlReader.java @@ -271,6 +271,9 @@ public class NativeXmlReader implements Closeable { } else if (peekStack == XmlScope.TAG_BODY) { // fall through: a new element is starting } else if (peekStack == XmlScope.EMPTY_DOCUMENT) { + if (lenient) { + consumeHeader(); + } stack[stackSize - 1] = XmlScope.NONEMPTY_DOCUMENT; // fall through: a new element is starting } else if (peekStack == XmlScope.NONEMPTY_DOCUMENT) { @@ -809,6 +812,7 @@ public class NativeXmlReader implements Closeable { // we found a header, consume it pos += 5; skipTo("?>"); + pos += 2; } @Override diff --git a/commons-serialize-xml/src/test/java/io/gitlab/jfronny/commons/serialize/xml/test/NativeXmlReaderTest.java b/commons-serialize-xml/src/test/java/io/gitlab/jfronny/commons/serialize/xml/test/NativeXmlReaderTest.java index fa2c74f..d9fbc69 100644 --- a/commons-serialize-xml/src/test/java/io/gitlab/jfronny/commons/serialize/xml/test/NativeXmlReaderTest.java +++ b/commons-serialize-xml/src/test/java/io/gitlab/jfronny/commons/serialize/xml/test/NativeXmlReaderTest.java @@ -219,6 +219,19 @@ public final class NativeXmlReaderTest { } + @Test + public void testXmlHeader() throws IOException { + NativeXmlReader reader = new NativeXmlReader(reader("content")); + assertThrows(IOException.class, reader::peek); + reader = new NativeXmlReader(reader("content")); + reader.setLenient(true); + assertThat(reader.peek()).isEqualTo(XmlToken.BEGIN_TAG); + assertThat(reader.beginTag()).isEqualTo("tag"); + assertThat(reader.nextText()).isEqualTo("content"); + assertThat(reader.endTag()).isEqualTo("tag"); + assertThat(reader.peek()).isEqualTo(XmlToken.EOF); + } + // @Test // public void testHelloWorld() throws IOException { // String json =