fix(logging): Fix reading attribute values

This commit is contained in:
Johannes Frohnmeyer 2024-04-15 10:22:33 +02:00
parent 8e696f8a2c
commit 5c1db8c339
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 45 additions and 2 deletions

View File

@ -352,11 +352,14 @@ public class NativeXmlReader implements Closeable {
throw unexpectedTokenError("ATTRIBUTE_VALUE");
}
char quote = buffer[pos++];
return readUntil((c, i) -> {
String result = readUntil((c, i) -> {
if (!lenient && c < 0x20 && c != 0x09) throw syntaxError("Control character in attribute value");
if (c == '<') throw syntaxError("Expected " + quote + " but was '<'");
return c == quote;
}, true);
pos++;
peeked = PEEKED_NONE;
return result;
}
private String readReference() throws IOException {

View File

@ -30,7 +30,7 @@ import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
@SuppressWarnings("resource")
public final class BaseXmlReaderTest {
public final class NativeXmlReaderTest {
/**
* Test for issue 212.
*/
@ -168,6 +168,46 @@ public final class BaseXmlReaderTest {
assertThat(reader.hasNext()).isFalse();
}
@Test
public void testReadAttributes() throws IOException {
NativeXmlReader reader = new NativeXmlReader(reader("<tag a='1' b='2' c='3'/>"));
assertThat(reader.beginTag()).isEqualTo("tag");
assertThat(reader.nextAttributeName()).isEqualTo("a");
assertThat(reader.nextAttributeValue()).isEqualTo("1");
assertThat(reader.nextAttributeName()).isEqualTo("b");
assertThat(reader.nextAttributeValue()).isEqualTo("2");
assertThat(reader.nextAttributeName()).isEqualTo("c");
assertThat(reader.nextAttributeValue()).isEqualTo("3");
assertThat(reader.endTag()).isEqualTo("tag");
assertThat(reader.peek()).isEqualTo(XmlToken.EOF);
}
@Test
public void testReadAttributesWithBody() throws IOException {
NativeXmlReader reader = new NativeXmlReader(reader("<tag a='1' b='2' c='3'>body<secondElement />body2</tag>"));
assertThat(reader.beginTag()).isEqualTo("tag");
assertThat(reader.peek()).isEqualTo(ATTRIBUTE_NAME);
assertThat(reader.nextAttributeName()).isEqualTo("a");
assertThat(reader.peek()).isEqualTo(ATTRIBUTE_VALUE);
assertThat(reader.nextAttributeValue()).isEqualTo("1");
assertThat(reader.peek()).isEqualTo(ATTRIBUTE_NAME);
assertThat(reader.nextAttributeName()).isEqualTo("b");
assertThat(reader.peek()).isEqualTo(ATTRIBUTE_VALUE);
assertThat(reader.nextAttributeValue()).isEqualTo("2");
assertThat(reader.nextAttributeName()).isEqualTo("c");
assertThat(reader.nextAttributeValue()).isEqualTo("3");
assertThat(reader.peek()).isEqualTo(TEXT);
assertThat(reader.nextText()).isEqualTo("body");
assertThat(reader.peek()).isEqualTo(BEGIN_TAG);
assertThat(reader.beginTag()).isEqualTo("secondElement");
assertThat(reader.peek()).isEqualTo(END_TAG);
assertThat(reader.endTag()).isEqualTo("secondElement");
assertThat(reader.nextText()).isEqualTo("body2");
assertThat(reader.endTag()).isEqualTo("tag");
assertThat(reader.peek()).isEqualTo(XmlToken.EOF);
}
// @Test
// public void testHelloWorld() throws IOException {
// String json =