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
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,25 @@ public int nextTag() throws XMLStreamException {
}

@Override
public void require(int expectedType, String namespaceURI, String localName) throws XMLStreamException {
public void require(int expectedType, @Nullable String namespaceURI, @Nullable String localName)
throws XMLStreamException {

int eventType = getEventType();
if (eventType != expectedType) {
throw new XMLStreamException("Expected [" + expectedType + "] but read [" + eventType + "]");
}
// This reader only exposes a name and namespace for START_ELEMENT and END_ELEMENT,
// so a non-null namespace or local name can only ever match one of those events.
if ((namespaceURI != null || localName != null) &&
eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
throw new XMLStreamException("Current event is not a START_ELEMENT or END_ELEMENT");
}
if (localName != null && !localName.equals(getLocalName())) {
throw new XMLStreamException("Expected local name [" + localName + "] but read [" + getLocalName() + "]");
}
if (namespaceURI != null && !namespaceURI.equals(getNamespaceURI())) {
throw new XMLStreamException("Expected namespace [" + namespaceURI + "] but read [" + getNamespaceURI() + "]");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
Expand All @@ -34,6 +36,7 @@
import org.springframework.core.testfixture.xml.XmlContent;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

class XMLEventStreamReaderTests {

Expand Down Expand Up @@ -68,4 +71,25 @@ void readCorrect() throws Exception {
assertThat(XmlContent.from(writer)).isSimilarTo(XML, nodeFilter);
}

@Test // require(type, namespaceURI, localName) must validate the name and namespace
void requireValidatesNamespaceAndLocalName() throws Exception {
advanceToStartElement("root");

streamReader.require(XMLStreamConstants.START_ELEMENT, null, "root");
streamReader.require(XMLStreamConstants.START_ELEMENT, "namespace", "root");
streamReader.require(XMLStreamConstants.START_ELEMENT, "namespace", null);

assertThatExceptionOfType(XMLStreamException.class).isThrownBy(() ->
streamReader.require(XMLStreamConstants.START_ELEMENT, null, "wrong"));
assertThatExceptionOfType(XMLStreamException.class).isThrownBy(() ->
streamReader.require(XMLStreamConstants.START_ELEMENT, "wrong-namespace", "root"));
}

private void advanceToStartElement(String localName) throws Exception {
while (streamReader.getEventType() != XMLStreamConstants.START_ELEMENT ||
!streamReader.getLocalName().equals(localName)) {
streamReader.next();
}
}

}