diff --git a/spring-core/src/main/java/org/springframework/util/xml/StaxStreamXMLReader.java b/spring-core/src/main/java/org/springframework/util/xml/StaxStreamXMLReader.java index 26394c56a6..359514b57c 100644 --- a/spring-core/src/main/java/org/springframework/util/xml/StaxStreamXMLReader.java +++ b/spring-core/src/main/java/org/springframework/util/xml/StaxStreamXMLReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -211,11 +211,6 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader { } private void handleCharacters() throws SAXException { - if (getContentHandler() != null && this.reader.isWhiteSpace()) { - getContentHandler().ignorableWhitespace(this.reader.getTextCharacters(), - this.reader.getTextStart(), this.reader.getTextLength()); - return; - } if (XMLStreamConstants.CDATA == this.reader.getEventType() && getLexicalHandler() != null) { getLexicalHandler().startCDATA(); } diff --git a/spring-core/src/test/java/org/springframework/util/xml/AbstractStaxXMLReaderTestCase.java b/spring-core/src/test/java/org/springframework/util/xml/AbstractStaxXMLReaderTestCase.java index 3ced43ba0c..d28ad6e34b 100644 --- a/spring-core/src/test/java/org/springframework/util/xml/AbstractStaxXMLReaderTestCase.java +++ b/spring-core/src/test/java/org/springframework/util/xml/AbstractStaxXMLReaderTestCase.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,19 +16,22 @@ package org.springframework.util.xml; +import java.io.ByteArrayInputStream; import java.io.InputStream; - import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.sax.SAXSource; +import static org.junit.Assert.assertEquals; import org.junit.Before; import org.junit.Test; +import static org.mockito.BDDMockito.*; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; -import org.springframework.tests.MockitoUtils; -import org.springframework.tests.MockitoUtils.InvocationArgumentsAdapter; +import org.w3c.dom.Node; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; @@ -38,7 +41,10 @@ import org.xml.sax.ext.LexicalHandler; import org.xml.sax.helpers.AttributesImpl; import org.xml.sax.helpers.XMLReaderFactory; -import static org.mockito.BDDMockito.*; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.tests.MockitoUtils; +import org.springframework.tests.MockitoUtils.InvocationArgumentsAdapter; public abstract class AbstractStaxXMLReaderTestCase { @@ -104,6 +110,25 @@ public abstract class AbstractStaxXMLReaderTestCase { verifyIdenticalInvocations(standardContentHandler, contentHandler); } + @Test + public void whitespace() throws Exception { + String xml = " Some text "; + + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + + AbstractStaxXMLReader staxXmlReader = createStaxXmlReader( + new ByteArrayInputStream(xml.getBytes("UTF-8"))); + + SAXSource source = new SAXSource(staxXmlReader, new InputSource()); + DOMResult result = new DOMResult(); + + transformer.transform(source, result); + + Node node1 = result.getNode().getFirstChild().getFirstChild(); + assertEquals(" ", node1.getTextContent()); + assertEquals(" Some text ", node1.getNextSibling().getTextContent()); + } + @Test public void lexicalHandler() throws Exception { Resource testLexicalHandlerXml = new ClassPathResource("testLexicalHandler.xml", getClass()); @@ -130,7 +155,7 @@ public abstract class AbstractStaxXMLReaderTestCase { verifyIdenticalInvocations(expectedLexicalHandler, actualLexicalHandler); } - private final LexicalHandler mockLexicalHandler() throws Exception { + private LexicalHandler mockLexicalHandler() throws Exception { LexicalHandler lexicalHandler = mock(LexicalHandler.class); willAnswer(new CopyCharsAnswer()).given(lexicalHandler).comment(any(char[].class), anyInt(), anyInt()); return lexicalHandler; @@ -218,6 +243,9 @@ public abstract class AbstractStaxXMLReaderTestCase { @Override public boolean equals(Object obj) { Attributes other = ((PartialAttributes) obj).attributes; + if (this.attributes.getLength() != other.getLength()) { + return false; + } for (int i = 0; i < other.getLength(); i++) { boolean found = false; for (int j = 0; j < attributes.getLength(); j++) { diff --git a/spring-core/src/test/resources/org/springframework/util/xml/testContentHandler.xml b/spring-core/src/test/resources/org/springframework/util/xml/testContentHandler.xml index 27129db12f..062b7b79b9 100644 --- a/spring-core/src/test/resources/org/springframework/util/xml/testContentHandler.xml +++ b/spring-core/src/test/resources/org/springframework/util/xml/testContentHandler.xml @@ -1,2 +1,2 @@ - \ No newline at end of file + Some text \ No newline at end of file