Skip to content

Commit fca72f6

Browse files
Arjen Poutsmajhoeller
Arjen Poutsma
authored andcommitted
StaxStreamXMLReader ignores significant whitespace
The StaxStreamXMLReader no longer handles all whitespace as ignorable whitespace. Issue: SPR-12000 (cherry picked from commit d6950d8)
1 parent 0dca31c commit fca72f6

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

spring-core/src/main/java/org/springframework/util/xml/StaxStreamXMLReader.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -217,11 +217,6 @@ private void handleEndElement() throws SAXException {
217217
}
218218

219219
private void handleCharacters() throws SAXException {
220-
if (getContentHandler() != null && this.reader.isWhiteSpace()) {
221-
getContentHandler().ignorableWhitespace(this.reader.getTextCharacters(),
222-
this.reader.getTextStart(), this.reader.getTextLength());
223-
return;
224-
}
225220
if (XMLStreamConstants.CDATA == this.reader.getEventType() && getLexicalHandler() != null) {
226221
getLexicalHandler().startCDATA();
227222
}

spring-core/src/test/java/org/springframework/util/xml/AbstractStaxXMLReaderTestCase.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@
1616

1717
package org.springframework.util.xml;
1818

19+
import java.io.ByteArrayInputStream;
1920
import java.io.InputStream;
2021
import javax.xml.stream.XMLInputFactory;
2122
import javax.xml.stream.XMLStreamException;
23+
import javax.xml.transform.Transformer;
24+
import javax.xml.transform.TransformerFactory;
25+
import javax.xml.transform.dom.DOMResult;
26+
import javax.xml.transform.sax.SAXSource;
2227

28+
import static org.junit.Assert.assertEquals;
2329
import org.junit.Before;
2430
import org.junit.Test;
2531
import static org.mockito.BDDMockito.*;
2632
import org.mockito.invocation.InvocationOnMock;
2733
import org.mockito.stubbing.Answer;
34+
import org.w3c.dom.Node;
2835
import org.xml.sax.Attributes;
2936
import org.xml.sax.ContentHandler;
3037
import org.xml.sax.InputSource;
@@ -103,6 +110,25 @@ public void contentHandlerNoNamespacesPrefixes() throws Exception {
103110
verifyIdenticalInvocations(standardContentHandler, contentHandler);
104111
}
105112

113+
@Test
114+
public void whitespace() throws Exception {
115+
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test><node1> </node1><node2> Some text </node2></test>";
116+
117+
Transformer transformer = TransformerFactory.newInstance().newTransformer();
118+
119+
AbstractStaxXMLReader staxXmlReader = createStaxXmlReader(
120+
new ByteArrayInputStream(xml.getBytes("UTF-8")));
121+
122+
SAXSource source = new SAXSource(staxXmlReader, new InputSource());
123+
DOMResult result = new DOMResult();
124+
125+
transformer.transform(source, result);
126+
127+
Node node1 = result.getNode().getFirstChild().getFirstChild();
128+
assertEquals(" ", node1.getTextContent());
129+
assertEquals(" Some text ", node1.getNextSibling().getTextContent());
130+
}
131+
106132
@Test
107133
public void lexicalHandler() throws Exception {
108134
Resource testLexicalHandlerXml = new ClassPathResource("testLexicalHandler.xml", getClass());
@@ -130,7 +156,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
130156
verifyIdenticalInvocations(expectedLexicalHandler, actualLexicalHandler);
131157
}
132158

133-
private final LexicalHandler mockLexicalHandler() throws Exception {
159+
private LexicalHandler mockLexicalHandler() throws Exception {
134160
LexicalHandler lexicalHandler = mock(LexicalHandler.class);
135161
willAnswer(new CopyCharsAnswer()).given(lexicalHandler).comment(any(char[].class), anyInt(), anyInt());
136162
return lexicalHandler;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<h:hello xmlns:h="http://www.greeting.com/hello/" id="a1" h:person="David"><prefix:goodbye
2-
xmlns:prefix="http://www.greeting.com/goodbye/" h:person="Arjen"/></h:hello>
2+
xmlns:prefix="http://www.greeting.com/goodbye/" h:person="Arjen"> Some text </prefix:goodbye><h:so-long> </h:so-long></h:hello>

0 commit comments

Comments
 (0)